فهرست منبع

[cli] add --jvm

Aleksandr Kuzmenko 5 سال پیش
والد
کامیت
0abd794725
5فایلهای تغییر یافته به همراه53 افزوده شده و 32 حذف شده
  1. 4 0
      extra/CHANGES.txt
  2. 12 4
      src/compiler/haxe.ml
  3. 11 6
      src/core/path.ml
  4. 25 20
      src/generators/genjvm.ml
  5. 1 2
      tests/unit/compile-jvm.hxml

+ 4 - 0
extra/CHANGES.txt

@@ -1,5 +1,9 @@
 2020-XX-XX: 4.1.1
 2020-XX-XX: 4.1.1
 
 
+	New features:
+
+	jvm : added `--jvm path/to.jar` CLI argument instead of `-D jvm --java path`
+
 	Bugfixes:
 	Bugfixes:
 
 
 	all : fixed arguments ordering for @:structInit constructors (#9418)
 	all : fixed arguments ordering for @:structInit constructors (#9418)

+ 12 - 4
src/compiler/haxe.ml

@@ -280,7 +280,7 @@ module Initialize = struct
 				"eval"
 				"eval"
 end
 end
 
 
-let generate tctx ext interp swf_header =
+let generate tctx ext interp jvm_flag swf_header =
 	let com = tctx.Typecore.com in
 	let com = tctx.Typecore.com in
 	(* check file extension. In case of wrong commandline, we don't want
 	(* check file extension. In case of wrong commandline, we don't want
 		to accidentaly delete a source file. *)
 		to accidentaly delete a source file. *)
@@ -299,7 +299,8 @@ let generate tctx ext interp swf_header =
 	begin match com.platform with
 	begin match com.platform with
 		| Neko | Hl | Eval when interp -> ()
 		| Neko | Hl | Eval when interp -> ()
 		| Cpp when Common.defined com Define.Cppia -> ()
 		| Cpp when Common.defined com Define.Cppia -> ()
-		| Cpp | Cs | Java | Php -> Path.mkdir_from_path (com.file ^ "/.")
+		| Cpp | Cs | Php -> Path.mkdir_from_path (com.file ^ "/.")
+		| Java when not jvm_flag -> Path.mkdir_from_path (com.file ^ "/.")
 		| _ -> Path.mkdir_from_path com.file
 		| _ -> Path.mkdir_from_path com.file
 	end;
 	end;
 	if interp then
 	if interp then
@@ -324,7 +325,7 @@ let generate tctx ext interp swf_header =
 			Gencs.generate,"cs"
 			Gencs.generate,"cs"
 		| Java ->
 		| Java ->
 			if Common.defined com Jvm then
 			if Common.defined com Jvm then
-				Genjvm.generate,"java"
+				Genjvm.generate jvm_flag,"java"
 			else
 			else
 				Genjava.generate,"java"
 				Genjava.generate,"java"
 		| Python ->
 		| Python ->
@@ -696,6 +697,7 @@ try
 	let force_typing = ref false in
 	let force_typing = ref false in
 	let pre_compilation = ref [] in
 	let pre_compilation = ref [] in
 	let interp = ref false in
 	let interp = ref false in
+	let jvm_flag = ref false in
 	let swf_version = ref false in
 	let swf_version = ref false in
 	let native_libs = ref [] in
 	let native_libs = ref [] in
 	let add_native_lib file extern = native_libs := (file,extern) :: !native_libs in
 	let add_native_lib file extern = native_libs := (file,extern) :: !native_libs in
@@ -736,6 +738,12 @@ try
 			cp_libs := "hxjava" :: !cp_libs;
 			cp_libs := "hxjava" :: !cp_libs;
 			Initialize.set_platform com Java dir;
 			Initialize.set_platform com Java dir;
 		),"<directory>","generate Java code into target directory");
 		),"<directory>","generate Java code into target directory");
+		("Target",["--jvm"],["-jvm"],Arg.String (fun dir ->
+			cp_libs := "hxjava" :: !cp_libs;
+			Common.define com Define.Jvm;
+			jvm_flag := true;
+			Initialize.set_platform com Java dir;
+		),"<directory>","generate JVM bytecode into target file");
 		("Target",["--python"],["-python"],Arg.String (fun dir ->
 		("Target",["--python"],["-python"],Arg.String (fun dir ->
 			Initialize.set_platform com Python dir;
 			Initialize.set_platform com Python dir;
 		),"<file>","generate Python code as target file");
 		),"<file>","generate Python code as target file");
@@ -1057,7 +1065,7 @@ try
 		if ctx.has_error then raise Abort;
 		if ctx.has_error then raise Abort;
 		check_auxiliary_output com !xml_out !json_out;
 		check_auxiliary_output com !xml_out !json_out;
 		com.stage <- CGenerationStart;
 		com.stage <- CGenerationStart;
-		if not !no_output then generate tctx ext !interp !swf_header;
+		if not !no_output then generate tctx ext !interp !jvm_flag !swf_header;
 		com.stage <- CGenerationDone;
 		com.stage <- CGenerationDone;
 	end;
 	end;
 	Sys.catch_break false;
 	Sys.catch_break false;

+ 11 - 6
src/core/path.ml

@@ -269,13 +269,18 @@ module FilePath = struct
 			in
 			in
 			let file,ext = if String.length path = 0 then
 			let file,ext = if String.length path = 0 then
 				None,None
 				None,None
-			else begin
+			else begin try
 				let cp = String.rindex path '.' in
 				let cp = String.rindex path '.' in
-				if cp <> -1 then begin
-					let file,ext = split path cp in
-					Some file,Some ext
-				end else
-					Some path,None
+				let file,ext = split path cp in
+				Some file,Some ext
+			with Not_found ->
+				Some path,None
 			end in
 			end in
 			create dir file ext backslash
 			create dir file ext backslash
+
+	let name_and_extension path = match path.file_name with
+		| None -> failwith "File path has no name"
+		| Some name -> match path.extension with
+			| None -> name
+			| Some ext -> name ^ "." ^ ext
 end
 end

+ 25 - 20
src/generators/genjvm.ml

@@ -2789,25 +2789,30 @@ module Preprocessor = struct
 		) gctx.com.types
 		) gctx.com.types
 end
 end
 
 
-let file_name_and_extension file =
-	match List.rev (ExtString.String.nsplit file "/") with
-	| e1 :: _ -> e1
-	| _ -> die "" __LOC__
-
-let generate com =
-	mkdir_from_path com.file;
-	let jar_name,manifest_suffix,entry_point = match get_entry_point com with
-		| Some (jarname,cl,expr) ->
-			let pack = match fst cl.cl_path with
-				| [] -> ["haxe";"root"]
-				| pack -> pack
-			in
-			jarname,"\nMain-Class: " ^ (s_type_path (pack,snd cl.cl_path)), Some (cl,expr)
-		| None -> "jar","",None
+let generate jvm_flag com =
+	let path = FilePath.parse com.file in
+	let jar_name,entry_point = match get_entry_point com with
+		| Some (jarname,cl,expr) -> jarname, Some (cl,expr)
+		| None -> "jar",None
 	in
 	in
-	let jar_name = if com.debug then jar_name ^ "-Debug" else jar_name in
-	let jar_dir = add_trailing_slash com.file in
-	let jar_path = Printf.sprintf "%s%s.jar" jar_dir jar_name in
+	let jar_dir,jar_path = if jvm_flag then begin
+		match path.file_name with
+		| Some _ ->
+			begin match path.directory with
+				| None ->
+					"./","./" ^ com.file
+				| Some dir ->
+					mkdir_from_path dir;
+					add_trailing_slash dir,com.file
+			end
+		| None ->
+			failwith "Please specify an output file name"
+	end else begin
+		let jar_name = if com.debug then jar_name ^ "-Debug" else jar_name in
+		let jar_dir = add_trailing_slash com.file in
+		let jar_path = Printf.sprintf "%s%s.jar" jar_dir jar_name in
+		jar_dir,jar_path
+	end in
 	let anon_identification = new tanon_identification haxe_dynamic_object_path in
 	let anon_identification = new tanon_identification haxe_dynamic_object_path in
 	let gctx = {
 	let gctx = {
 		com = com;
 		com = com;
@@ -2835,7 +2840,7 @@ let generate com =
 		else begin
 		else begin
 			let dir = Printf.sprintf "%slib/" jar_dir in
 			let dir = Printf.sprintf "%slib/" jar_dir in
 			Path.mkdir_from_path dir;
 			Path.mkdir_from_path dir;
-			let name = file_name_and_extension java_lib#get_file_path in
+			let name = FilePath.name_and_extension (FilePath.parse java_lib#get_file_path) in
 			let ch_in = open_in_bin java_lib#get_file_path in
 			let ch_in = open_in_bin java_lib#get_file_path in
 			let ch_out = open_out_bin (Printf.sprintf "%s%s" dir name) in
 			let ch_out = open_out_bin (Printf.sprintf "%s%s" dir name) in
 			let b = IO.read_all (IO.input_channel ch_in) in
 			let b = IO.read_all (IO.input_channel ch_in) in
@@ -2849,7 +2854,7 @@ let generate com =
 		"Manifest-Version: 1.0\n" ^
 		"Manifest-Version: 1.0\n" ^
 		(match class_paths with [] -> "" | _ -> "Class-Path: " ^ (String.concat " " class_paths ^ "\n")) ^
 		(match class_paths with [] -> "" | _ -> "Class-Path: " ^ (String.concat " " class_paths ^ "\n")) ^
 		"Created-By: Haxe (Haxe Foundation)" ^
 		"Created-By: Haxe (Haxe Foundation)" ^
-		manifest_suffix ^
+		(Option.map_default (fun (cl,_) -> "\nMain-Class: " ^ (s_type_path cl.cl_path)) "" entry_point) ^
 		"\n\n"
 		"\n\n"
 	in
 	in
 	Zip.add_entry manifest_content gctx.jar "META-INF/MANIFEST.MF";
 	Zip.add_entry manifest_content gctx.jar "META-INF/MANIFEST.MF";

+ 1 - 2
tests/unit/compile-jvm.hxml

@@ -6,5 +6,4 @@
 compile-each.hxml
 compile-each.hxml
 --main unit.TestMain
 --main unit.TestMain
 --java-lib native_java/native.jar
 --java-lib native_java/native.jar
--java bin/jvm
--D jvm
+-jvm bin/jvm/TestMain-debug.jar