Browse Source

[jvm] support `--jvm path.jar` instead of directories

corporate told me to
Simon Krajewski 5 years ago
parent
commit
1c11022b4d
4 changed files with 34 additions and 17 deletions
  1. 8 5
      src/compiler/haxe.ml
  2. 5 6
      src/core/path.ml
  3. 20 5
      src/generators/genjvm.ml
  4. 1 1
      tests/unit/compile-jvm.hxml

+ 8 - 5
src/compiler/haxe.ml

@@ -280,7 +280,7 @@ module Initialize = struct
 				"eval"
 end
 
-let generate tctx ext interp swf_header =
+let generate tctx ext interp jvm_flag swf_header =
 	let com = tctx.Typecore.com in
 	(* check file extension. In case of wrong commandline, we don't want
 		to accidentaly delete a source file. *)
@@ -299,7 +299,8 @@ let generate tctx ext interp swf_header =
 	begin match com.platform with
 		| Neko | Hl | Eval when interp -> ()
 		| 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
 	end;
 	if interp then
@@ -324,7 +325,7 @@ let generate tctx ext interp swf_header =
 			Gencs.generate,"cs"
 		| Java ->
 			if Common.defined com Jvm then
-				Genjvm.generate,"java"
+				Genjvm.generate jvm_flag,"java"
 			else
 				Genjava.generate,"java"
 		| Python ->
@@ -696,6 +697,7 @@ try
 	let force_typing = ref false in
 	let pre_compilation = ref [] in
 	let interp = ref false in
+	let jvm_flag = ref false in
 	let swf_version = ref false in
 	let native_libs = ref [] in
 	let add_native_lib file extern = native_libs := (file,extern) :: !native_libs in
@@ -739,8 +741,9 @@ try
 		("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 directory");
+		),"<directory>","generate JVM bytecode into target file");
 		("Target",["--python"],["-python"],Arg.String (fun dir ->
 			Initialize.set_platform com Python dir;
 		),"<file>","generate Python code as target file");
@@ -1062,7 +1065,7 @@ try
 		if ctx.has_error then raise Abort;
 		check_auxiliary_output com !xml_out !json_out;
 		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;
 	end;
 	Sys.catch_break false;

+ 5 - 6
src/core/path.ml

@@ -269,13 +269,12 @@ module FilePath = struct
 			in
 			let file,ext = if String.length path = 0 then
 				None,None
-			else begin
+			else begin try
 				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
 			create dir file ext backslash
 end

+ 20 - 5
src/generators/genjvm.ml

@@ -2813,15 +2813,30 @@ let file_name_and_extension file =
 	| e1 :: _ -> e1
 	| _ -> die "" __LOC__
 
-let generate com =
-	mkdir_from_path com.file;
+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
-	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 gctx = {
 		com = com;

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

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