[m-rev.] diff: initial powershell integration

Julien Fischer juliensf at csse.unimelb.edu.au
Fri Dec 14 03:21:59 AEDT 2012


Branches: main, 12.08 (probably)

Begin work on integrating Mercury with Windows PowerShell.

In the absence of Cygwin or MinGW/MSYS, using the Mercury compiler on Windows
has been rather troublesome, mostly due to the fact that cmd.exe is rubbish.
I've been experimenting with using PowerShell in place of cmd.exe recently
and my initial impressions are that it works much better.

This diff fixes a few issues that arise when COMSPEC is set to powershell.exe
rather than cmd.exe, in addition to adding powershell to the list of supported
environment types.  This is still work-in-progress.

compiler/globals.m:
 	Add PowerShell to the list of environment types.

compiler/compile_target_code.m:
 	With PowerShell we need to escape the `@' character: do so
 	when generating command lines that use @files.

compiler/module_cmds.m:
 	Unix and cmd.exe style redirection of standard input doesn't
 	work on PowerShell.

 	XXX add a couple of notes about things that could be improved.

Julien.

Index: compiler/compile_target_code.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/compile_target_code.m,v
retrieving revision 1.191
diff -u -r1.191 compile_target_code.m
--- compiler/compile_target_code.m	24 Oct 2012 05:49:43 -0000	1.191
+++ compiler/compile_target_code.m	13 Dec 2012 15:54:23 -0000
@@ -1867,7 +1867,10 @@
          % These may be shell scripts or batch files.
          globals.get_target_env_type(Globals, TargetEnvType),
          (
-            TargetEnvType = env_type_win_cmd,
+            % XXX we should actually generate a .ps1 file for PowerShell.
+            ( TargetEnvType = env_type_win_cmd
+            ; TargetEnvType = env_type_powershell
+            ),
              Ext = ".bat"
          ;
              ( TargetEnvType = env_type_posix
@@ -2948,6 +2951,12 @@
  csharp_file_name(env_type_win_cmd, csharp_unknown, Filename) =
      convert_to_windows_path_format(Filename).

+csharp_file_name(env_type_powershell, csharp_microsoft, Filename) =
+    convert_to_windows_path_format(Filename).
+csharp_file_name(env_type_powershell, csharp_mono, Filename) = Filename.
+csharp_file_name(env_type_powershell, csharp_unknown, Filename) =
+    convert_to_windows_path_format(Filename).
+
  :- func convert_to_windows_path_format(file_name) = file_name.

  convert_to_windows_path_format(FileName) =
@@ -3428,7 +3437,6 @@

  %-----------------------------------------------------------------------------%

-    %
      % invoke_long_system_command attempts to use the @file style of
      % calling to avoid command line length arguments on various systems.
      % If the underlying tool chain doesn't support this it just calls
@@ -3479,7 +3487,7 @@
                  VeryVerbose = no
              ),

-            FullCmd = Cmd ++ " @" ++ TmpFile,
+            FullCmd = Cmd ++ " " ++ at_file_name(Globals, TmpFile),
              invoke_system_command(Globals, ErrorStream, Verbosity, FullCmd,
                  Succeeded0, !IO),

@@ -3503,6 +3511,25 @@
              Verbosity, FullCmd, MaybeProcessOutput, Succeeded, !IO)
      ).

+    % Form the name of an @file given a file name.
+    % On some systems we need to escape the `@' character.
+    %
+:- func at_file_name(globals, string) = string.
+
+at_file_name(Globals, FileName) = AtFileName :-
+    get_host_env_type(Globals, EnvType),
+    (
+        EnvType = env_type_powershell,
+        AtFileName = "`@" ++ FileName
+    ;
+        ( EnvType = env_type_posix
+        ; EnvType = env_type_cygwin
+        ; EnvType = env_type_msys
+        ; EnvType = env_type_win_cmd
+        ),
+        AtFileName = "@" ++ FileName
+    ).
+
  %-----------------------------------------------------------------------------%
  %
  % C compiler flags.
Index: compiler/globals.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/globals.m,v
retrieving revision 1.107
diff -u -r1.107 globals.m
--- compiler/globals.m	24 Oct 2012 05:49:43 -0000	1.107
+++ compiler/globals.m	13 Dec 2012 15:22:05 -0000
@@ -184,10 +184,12 @@
      ;       env_type_msys
              % MinGW with the MSYS environment on Windows.

-    ;       env_type_win_cmd.
+    ;       env_type_win_cmd
              % The Windows command-line interpreter (cmd.exe). 
-            % XXX there are probably more variants of Windows, but it isn't
-            % clear what they are yet.
+ 
+    ;       env_type_powershell.
+            % Windows PowerShell.
+            % (NOTE: COMSPEC must be pointing to powershell.exe not cmd.exe.)

      % The tracing levels to use for a module when doing the source to source
      % debugging tranformation.
@@ -543,6 +545,7 @@
  convert_env_type("cygwin",  env_type_cygwin).
  convert_env_type("msys",    env_type_msys).
  convert_env_type("windows", env_type_win_cmd).
+convert_env_type("powerhsell", env_type_powershell).

  convert_ssdb_trace_level("default", yes, deep).
  convert_ssdb_trace_level("default", no, none).
Index: compiler/module_cmds.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/module_cmds.m,v
retrieving revision 1.23
diff -u -r1.23 module_cmds.m
--- compiler/module_cmds.m	24 Oct 2012 05:49:45 -0000	1.23
+++ compiler/module_cmds.m	13 Dec 2012 15:53:14 -0000
@@ -612,13 +612,22 @@
          MaybeProcessOutput = yes(ProcessOutput)
      ->
          io.make_temp(ProcessedTmpFile, !IO),
- 
+ 
+        % XXX we should get rid of use_win32
          ( use_win32 ->
-            % On windows we can't in general redirect standard
-            % error in the shell.
-            ProcessOutputRedirected = string.append_list(
-                [ProcessOutput, " < ", TmpFile, " > ",
-                    ProcessedTmpFile])
+            get_host_env_type(Globals, HostEnvType),
+            ( HostEnvType = env_type_powershell ->
+                % XXX we should be able to redirect stderr here too.
+                ProcessOutputRedirected = string.append_list(
+                    ["Get-Content ", TmpFile, " | ", ProcessOutput,
+                        " > ", ProcessedTmpFile])
+            ;
+                % On windows we can't in general redirect standard
+                % error in the shell.
+                ProcessOutputRedirected = string.append_list(
+                    [ProcessOutput, " < ", TmpFile, " > ",
+                        ProcessedTmpFile])
+            )
          ;
              ProcessOutputRedirected = string.append_list(
                  [ProcessOutput, " < ", TmpFile, " > ",
@@ -752,7 +761,10 @@
              write_java_shell_script(Globals, MainModuleName),
              Succeeded, !IO)
      ;
-        TargetEnvType = env_type_win_cmd,
+        % XXX should create a .ps1 file on PowerShell.
+        ( TargetEnvType = env_type_win_cmd
+        ; TargetEnvType = env_type_powershell
+        ),
          create_launcher_batch_file(Globals, MainModuleName,
              write_java_batch_file(Globals, MainModuleName),
              Succeeded, !IO)
@@ -959,7 +971,9 @@
              write_erlang_shell_script(Globals, MainModuleName),
              Succeeded, !IO)
      ;
-        TargetEnvType = env_type_win_cmd,
+        ( TargetEnvType = env_type_win_cmd
+        ; TargetEnvType = env_type_powershell
+        ),
          create_launcher_batch_file(Globals, MainModuleName,
              write_erlang_batch_file(Globals, MainModuleName),
              Succeeded, !IO)



More information about the reviews mailing list