[m-rev.] For review: Implementation of linking stage for Java

James Goddard goddardjames at yahoo.com
Tue Jan 27 14:43:57 AEDT 2004


Estimated hours taken: 6
Branches: main

Implement linking stage of compiler for grade Java.

compiler/make.program_target.m:
	Added new predicate create_java_shell_script/4 which is called at
	the linking stage of compilation  for grade Java.  This predicate
	creates a shell script which in theory should:

	* change to the directory where the module's class files are kept.
	* set the classpath to include the standard and runtime librarys as well
	  as the current directory.
	* and finally invoke the java interpreter.

	At the moment it performs steps 1 and 3 fine, but the classpath must
	be set manually as an environmental variable by the user.  Once this
	issue is resolved, you should be able to use the shell script in the
	same way as a C-generated executable.

compiler/options.m:
	Added the option to specify the Java interpreter.


Index: make.program_target.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/make.program_target.m,v
retrieving revision 1.22
diff -u -d -r1.22 make.program_target.m
--- make.program_target.m	24 Sep 2003 06:35:26 -0000	1.22
+++ make.program_target.m	27 Jan 2004 03:28:20 -0000
@@ -388,9 +388,9 @@
 			{ Succeeded = yes }
 		;
 			{ CompilationTarget = java },
-			{ Succeeded = no },
-			io__write_string(
-			"Sorry, not implemented, linking for `--target java'")
+			module_name_to_file_name(MainModuleName, "", no,
+					FileName),
+			create_java_shell_script(FileName, Succeeded)
 		),
 
 		{ Info5 = Info4 ^ command_line_targets :=
@@ -406,6 +406,98 @@
 		)
 	),
 	globals__io_set_option(link_objects, accumulating(LinkObjects)).
+
+	% create_java_shell_script:
+	%	Create a shell script with the same name as the given module
+	%	to invoke Java with the appropriate options on the class of the
+	%	same name.
+	
+:- pred create_java_shell_script(string::in, bool::out,
+		io__state::di, io__state::uo) is det.
+
+create_java_shell_script(MainModuleName, Succeeded) -->
+        globals__io_lookup_bool_option(verbose, Verbose),
+        maybe_write_string(Verbose, "% Generating shell script `" ++
+        		MainModuleName ++ "':\n"),
+
+        globals__io_lookup_bool_option(use_subdirs, UseSubdirs),
+        globals__io_lookup_bool_option(use_grade_subdirs, UseGradeSubdirs),
+        globals__io_lookup_string_option(fullarch, FullArch),
+        globals__io_get_globals(Globals),
+        ( { UseSubdirs = yes } ->
+                { UseGradeSubdirs = yes ->
+                        grade_directory_component(Globals, Grade),
+                        DirName = "Mercury"/Grade/FullArch/"Mercury"/"classs"
+                ;
+                        DirName = "Mercury"/"classs"
+                }
+	;
+		{ DirName = "." }
+	),
+	% XXX The correct classpath needs to be set somewhere.
+	%     It should take the form:
+	%     <path>/mer_std.jar:<path>/mer_std.runtime.jar:.
+	%     Currently this variable is empty, which causes problems.
+	% globals__io_lookup_accumulating_option(java_classpath,
+	%		Java_Incl_Dirs),
+	% ( { Java_Incl_Dirs = [] } ->
+	%	{ ClassPath = "" }
+	% ;
+	% 	% XXX PathSeparator should be ";" on Windows
+	%	{ PathSeparator = ":" },
+	%	{ join_string_list(Java_Incl_Dirs, "", "",
+	%			PathSeparator, ClassPath) }
+	% ),
+	globals__io_lookup_string_option(java_interpreter, Java),
+
+	io__open_output(MainModuleName, OpenResult),
+	(
+		{ OpenResult = ok(ShellScript) },
+		io__write_string(ShellScript, "#!"/"bin"/"sh\n"),
+		io__write_string(ShellScript, "cd " ++ DirName ++ "\n"),
+		% XXX See comment above
+		% io__write_string(ShellScript, "CLASSPATH=" ++ ClassPath),
+		io__write_string(ShellScript, " " ++ Java ++ " "),
+		io__write_string(ShellScript, MainModuleName ++ "\n"),
+		io__close_output(ShellScript),
+		io__call_system("chmod a+x " ++ MainModuleName, ChmodResult),
+		(
+			{ ChmodResult = ok(Status) },
+			{ Status = 0 ->
+				Succeeded = yes
+			;
+				error("chmod exit status != 0"),
+				Succeeded = no
+			}
+		;
+			{ ChmodResult = error(Message) },
+			{ error(io__error_message(Message)) },
+			{ Succeeded = no }
+		)
+	;
+		{ OpenResult = error(Message) },
+		{ error(io__error_message(Message)) },
+		{ Succeeded = no }
+	).
+
+	% join_string_list(Strings, Prefix, Suffix, Serarator, Result)
+	%
+	% Appends the strings in the list `Strings' together into the
+	% string Result. Each string is prefixed by Prefix, suffixed by
+	% Suffix and separated by Separator.
+
+:- pred join_string_list(list(string), string, string, string, string).
+:- mode join_string_list(in, in, in, in, out) is det.
+
+join_string_list([], _Prefix, _Suffix, _Separator, "").
+join_string_list([String | Strings], Prefix, Suffix, Separator, Result) :-
+	( Strings = [] ->
+		string__append_list([Prefix, String, Suffix], Result)
+	;
+		join_string_list(Strings, Prefix, Suffix, Separator, Result0),
+		string__append_list([Prefix, String, Suffix, Separator,
+			Result0], Result)
+	).
 
 :- pred linked_target_cleanup(module_name::in, linked_target_type::in,
 	file_name::in, compilation_target::in, make_info::in, make_info::out,
Index: options.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/options.m,v
retrieving revision 1.421
diff -u -d -r1.421 options.m
--- options.m	19 Jan 2004 01:01:58 -0000	1.421
+++ options.m	27 Jan 2004 02:53:30 -0000
@@ -557,6 +557,7 @@
 
 			% Java
 		;	java_compiler
+		;	java_interpreter
 		;	java_flags
 		;	quoted_java_flag
 		;	java_classpath
@@ -1164,6 +1165,7 @@
 
 % Java
 	java_compiler		-	string("javac"),
+	java_interpreter	-	string("java"),
 	java_flags		-	accumulating([]),
 	quoted_java_flag	-	string_special,
 	java_classpath  	-	accumulating([]),
@@ -1840,6 +1842,7 @@
 
 long_option("java-compiler",		java_compiler).
 long_option("javac",			java_compiler).
+long_option("java-interpreter",		java_interpreter).
 long_option("java-flags",		java_flags).
 long_option("java-flag",		quoted_java_flag).
 	% XXX we should consider the relationship between java_debug and
@@ -3851,6 +3854,10 @@
 		"--java-compiler <javac>",
 		"\tSpecify which Java compiler to use.  The default is `javac'.",
 		
+		"--java-interpreter <java>",
+		"\tSpecify which Java interpreter to use.",
+		"\tThe default is `java'",
+
 		"--java-flags <options>, --java-flag <option>",
 		"\tSpecify options to be passed to the Java compiler.",
 		"\t`--java-flag' should be used for single words which need",
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list