[m-rev.] diff: workaround a problem with 'mmc --make in the Java grade

Julien Fischer jfischer at opturion.com
Thu Sep 3 20:01:08 AEST 2015


A couple of things:

1. I think it would be useful if the predicate target_is_java (along with
target_is_c, target_is_csharp etc) were added to the builtin module.  Any
objections?

2. In conjunction with the change to build Java executables as Java 
archives, it's now fairly straightforward to get a working Mercury
compiler compiled in Java.  (For "working" read "can compile the sample
programs in the Java grade".)

-------------------

Workaround a problem with 'mmc --make' in the Java grade.

Workaround a problem with 'mmc --make' when the Mercury compiler is built in
the Java grade.  In that grade io.progname will return the program name as
'top_level' because that is what the name of the top-level compiler module is
(in the Java grade the program name is set in the top-level module).  This
means that when '--make' attempts to invoke the compiler that it tries to
invoke the executable 'top_level', which won't exist because we rename it to
'mercury_compile'.

compiler/make.module_target.m:
     If the compiler is built in the Java grade then don't attempt to invoke
     it using the name returned by io.progname, instead just use the value
     of the environment variable MERCURY_COMPILER or, if that is not set, just
     'mmc'.

     Simplify how we generate an error message.

Julien.

diff --git a/compiler/make.module_target.m b/compiler/make.module_target.m
index 1a0df51..c07627a 100644
--- a/compiler/make.module_target.m
+++ b/compiler/make.module_target.m
@@ -668,7 +668,17 @@ call_mercury_compile_main(Globals, Args, Succeeded, !IO) :-

  invoke_mmc(Globals, ErrorStream, MaybeArgFileName, Args, Succeeded, !IO) :-
      io.progname("", ProgName, !IO),
-    ( ProgName = "" ->
+    ( if
+        % NOTE: if the compiler is built in the Java grade then ProgName will
+        % be set to "top_level" (which was the name compiled into the module
+        % containing the predicate main/2).   We don't want to attempt to
+        % invoke an executable named "top_level" however, since the wrapper
+        % script will have been renamed to "mercury_compile" by the Mmakefile
+        % in the compiler directory.
+        ( ProgName = ""
+        ; target_is_java
+        )
+    then
          io.get_environment_var("MERCURY_COMPILER", MaybeMercuryCompiler, !IO),
          (
              MaybeMercuryCompiler = yes(MercuryCompiler)
@@ -676,7 +686,7 @@ invoke_mmc(Globals, ErrorStream, MaybeArgFileName, Args, Succeeded, !IO) :-
              MaybeMercuryCompiler = no,
              MercuryCompiler = "mmc"
          )
-    ;
+    else
          MercuryCompiler = ProgName
      ),

@@ -715,14 +725,24 @@ invoke_mmc(Globals, ErrorStream, MaybeArgFileName, Args, Succeeded, !IO) :-
      ;
          ArgFileOpenRes = error(Error),
          Succeeded = no,
-        io.write_string("Error opening `", !IO),
-        io.write_string(ArgFileName, !IO),
-        io.write_string("' for output: ", !IO),
-        io.write_string(io.error_message(Error), !IO),
-        io.nl(!IO)
+        io.error_message(Error, ErrorMsg),
+        io.format("Error opening `%s' for output: %s\n",
+            [s(ArgFileName), s(ErrorMsg)], !IO)
      ),
      io.remove_file(ArgFileName, _, !IO).

+:- pred target_is_java is semidet.
+
+:- pragma foreign_proc("Java",
+    target_is_java,
+    [promise_pure, will_not_call_mercury, thread_safe],
+"
+    SUCCESS_INDICATOR = true;
+").
+
+target_is_java :-
+    semidet_fail.
+
  %-----------------------------------------------------------------------------%

  record_made_target(Globals, TargetFile, CompilationTask, Succeeded,



More information about the reviews mailing list