[m-rev.] diff: fix bug #339: --restricted-command-line broken with recent Java compilers

Julien Fischer jfischer at opturion.com
Wed Jul 15 17:10:35 AEST 2015


On Wed, 15 Jul 2015, Julien Fischer wrote:

> On Wed, 15 Jul 2015, Sebastian Godelet wrote:
>
>> The compiler is compiled in a msys environment,
>> But I tested it in a normal Windows command shell.
>> In my Mercury.config file I have --env-type windows.
>> Nevertheless, if I open an msys shell and use:
>> 
>> $ mmc --use-grade-subdirs --env-type msys -s java -m fmt_bug
>> cp: cannot stat `Mercury\\java\\i686-pc-mingw32\\Mercury\\bin\\fmt_bug': No 
>> such file or directory
>> Made symlink/copy of Mercury\java\i686-pc-mingw32\Mercury\jars\fmt_bug.jar
>> mercury_compile.exe: error copying 
>> `Mercury\java\i686-pc-mingw32\Mercury\bin\fmt_bug' to `fmt_bug', can't open 
>> input file: No such file or directory
>> 
>> The same problem applies.
>
> Ok, I'll take a look at it.

I haven't been able to reproduce this one.

>> Again, without the --use-grade-subdirs the correct file gets copid.
>> I should mention that in the former case (Windows cmd), the subdir
>> Mercury\java\i686-pc-mingw32\Mercury\bats
>> Does contain the bat file, just it seems that the `cp' command is not 
>> looking in that folder.
>> 
>> On a side note, if I use --env-type msys, the java class path still uses 
>> the c:/ Windows path convention in the
>> Shell script and thus cannot be executed (though I think that is a 
>> different problem).
>
> I don't think the MSYS use case was really considered when we added
> support for launcher scripts.  (The reason is probably that it's
> somewhat awkward as it requires Unix conventions in some spots and
> Windows ones in others -- also, at the time I don't think the Mercury
> compiler had sufficient information about the environment to actually
> handle it properly anyway.)  I'll take a look at this one as well

The following patch should fix the latter problem, although I don't have
a Windows machine at hand to test it at the moment.

Cheers,
Julien.

diff --git a/compiler/module_cmds.m b/compiler/module_cmds.m
index 72fff72..bc68591 100644
--- a/compiler/module_cmds.m
+++ b/compiler/module_cmds.m
@@ -768,12 +768,16 @@ create_java_shell_script(Globals, MainModuleName, Succeeded, !IO) :-
      (
          ( TargetEnvType = env_type_posix
          ; TargetEnvType = env_type_cygwin
-        ; TargetEnvType = env_type_msys
          ),
          create_launcher_shell_script(Globals, MainModuleName,
              write_java_shell_script(Globals, MainModuleName, JarFileName),
              Succeeded, !IO)
      ;
+        TargetEnvType = env_type_msys,
+        create_launcher_shell_script(Globals, MainModuleName,
+            write_java_msys_shell_script(Globals, MainModuleName, JarFileName),
+            Succeeded, !IO)
+    ;
          % XXX should create a .ps1 file on PowerShell.
          ( TargetEnvType = env_type_win_cmd
          ; TargetEnvType = env_type_powershell
@@ -812,6 +816,44 @@ write_java_shell_script(Globals, MainModuleName, JarFileName, Stream, !IO) :-
          "exec \"$JAVA\" jmercury.", ClassName, " \"$@\"\n"
      ], !IO).

+    % For the MSYS version of the Java launcher script, there are a few
+    % differences:
+    %
+    % 1. The value of the CLASSPATH environment variable we construct for
+    % the Java interpreter must contain Windows style paths.
+    %
+    % 2. We use forward slashes as directory separators rather than back
+    % slashes since the latter require escaping inside the shell script.
+    %
+    % 3. The path of the Java interpreter must be a Unix style path as it
+    % will be invoked directly from the MSYS shell.
+    %
+:- pred write_java_msys_shell_script(globals::in, module_name::in,
+    file_name::in, io.text_output_stream::in, io::di, io::uo) is det.
+
+write_java_msys_shell_script(Globals, MainModuleName, JarFileName, Stream, !IO) :-
+    get_mercury_std_libs_for_java(Globals, MercuryStdLibs),
+    globals.lookup_accumulating_option(Globals, java_classpath,
+        UserClasspath),
+    % We prepend the .class files' directory and the current CLASSPATH.
+    Java_Incl_Dirs = ["\"$DIR/" ++ JarFileName ++ "\""] ++
+        MercuryStdLibs ++ ["$CLASSPATH" | UserClasspath],
+    ClassPath0 = string.join_list(";", Java_Incl_Dirs),
+    ClassPath = string.replace_all(ClassPath0, "\\", "/"),
+
+    globals.lookup_string_option(Globals, java_interpreter, Java),
+    mangle_sym_name_for_java(MainModuleName, module_qual, ".", ClassName),
+
+    io.write_strings(Stream, [
+        "#!/bin/sh\n",
+        "DIR=${0%/*}\n",
+        "DIR=$( cd \"${DIR}\" && pwd -W )\n",
+        "CLASSPATH=", ClassPath, "\n",
+        "export CLASSPATH\n",
+        "JAVA=${JAVA:-", Java, "}\n",
+        "exec \"$JAVA\" jmercury.", ClassName, " \"$@\"\n"
+    ], !IO).
+
  :- pred write_java_batch_file(globals::in, module_name::in, file_name::in,
      io.text_output_stream::in, io::di, io::uo) is det.














More information about the reviews mailing list