[m-rev.] for reviews: add --system-env-type

Julien Fischer jfischer at opturion.com
Thu Sep 26 11:05:51 AEST 2013


For review by anyone.

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

Add support for specifying the "system" environment type.

The "system" environment type is the environment type that external programs
invoked by the Mercury compiler (e.g. the C compiler) are run in.  That is, it
is the environment type that you get when you run a command use the C standard
library's system() function.  (Or equivalently, the Mercury standard library
predicate io.call_system/4.)

On sane operating systems this is identical to the host environment type.  On
native Windows**, the system environment is whatever command interpreter the
environment variable COMSPEC is pointing to (by default, cmd.exe), regardless
of what value the Mercury option --host-env-type has.  For example. if
--host-env-type is set to "msys" or "powershell" the system environment is
cmd.exe.  The new option allows us to fully describe to the compiler which
environment types we intend it to interact with.  Not being able to specify
this information has been one of the things preventing the powershell
environment from working properly.

** that is, Windows where the Mercury compiler is a native Windows executable,
so not on Cygwin when compiled against cygwin.dll, but on MinGW/MSYS.

compiler/options.m:
 	Add a new option, --system-env-type, which allows the "system"
 	environment to be specified.

 	Make the --env-type option also set the system environment.

compiler/handle_options.m:
 	Handle the new option.  Make --system-env-type default to the value
 	of --host-env-type if the former is not given on the command line.

compiler/compile_target_code.m:
 	Use the system env type in place of the host env type in spots where
 	the former is what is actually meant.

compiler/globals.m:
 	Add a slot to the globals structure to hold the system environment
 	type.

doc/user_guide.texi:
 	Document the new option.

Julien.

diff --git a/compiler/compile_target_code.m b/compiler/compile_target_code.m
index 3390575..1893393 100644
--- a/compiler/compile_target_code.m
+++ b/compiler/compile_target_code.m
@@ -2830,7 +2830,7 @@ create_archive(Globals, ErrorStream, LibFileName, Quote, ObjectList,

  create_csharp_exe_or_lib(Globals, ErrorStream, LinkTargetType, MainModuleName,
          OutputFileName0, SourceList0, Succeeded, !IO) :-
-    get_host_env_type(Globals, EnvType),
+    get_system_env_type(Globals, EnvType),
      get_csharp_compiler_type(Globals, CSharpCompilerType),

      OutputFileName = csharp_file_name(EnvType, CSharpCompilerType,
@@ -3551,7 +3551,7 @@ invoke_long_system_command_maybe_filter_output(Globals, ErrorStream, Verbosity,
  :- func at_file_name(globals, string) = string.

  at_file_name(Globals, FileName) = AtFileName :-
-    get_host_env_type(Globals, EnvType),
+    get_system_env_type(Globals, EnvType),
      (
          EnvType = env_type_powershell,
          AtFileName = "`@" ++ FileName
diff --git a/compiler/globals.m b/compiler/globals.m
index 99e3932..6f714bb 100644
--- a/compiler/globals.m
+++ b/compiler/globals.m
@@ -254,7 +254,7 @@
      may_be_thread_safe::in, c_compiler_type::in, csharp_compiler_type::in,
      reuse_strategy::in,
      maybe(il_version_number)::in, maybe(feedback_info)::in, env_type::in,
-    env_type::in, file_install_cmd::in, globals::out) is det.
+    env_type::in, env_type::in, file_install_cmd::in, globals::out) is det.

  :- pred get_options(globals::in, option_table::out) is det.
  :- pred get_target(globals::in, compilation_target::out) is det.
@@ -276,6 +276,7 @@
      is det.
  :- pred get_maybe_feedback_info(globals::in, maybe(feedback_info)::out) is det.
  :- pred get_host_env_type(globals::in, env_type::out) is det.
+:- pred get_system_env_type(globals::in, env_type::out) is det.
  :- pred get_target_env_type(globals::in, env_type::out) is det.
  :- pred get_file_install_cmd(globals::in, file_install_cmd::out) is det.

@@ -616,6 +617,7 @@ gc_is_conservative(gc_automatic) = no.
                  g_maybe_il_version_number   :: maybe(il_version_number),
                  g_maybe_feedback            :: maybe(feedback_info),
                  g_host_env_type             :: env_type,
+                g_system_env_type           :: env_type,
                  g_target_env_type           :: env_type,
                  g_file_install_cmd          :: file_install_cmd
              ).
@@ -624,12 +626,14 @@ globals_init(Options, Target, GC_Method, TagsMethod,
          TerminationNorm, Termination2Norm, TraceLevel, TraceSuppress,
          SSTraceLevel, MaybeThreadSafe, C_CompilerType, CSharp_CompilerType,
          ReuseStrategy, MaybeILVersion,
-        MaybeFeedback, HostEnvType, TargetEnvType, FileInstallCmd, Globals) :-
+        MaybeFeedback, HostEnvType, SystemEnvType, TargetEnvType,
+        FileInstallCmd, Globals) :-
      Globals = globals(Options, Target, GC_Method, TagsMethod,
          TerminationNorm, Termination2Norm, TraceLevel, TraceSuppress,
          SSTraceLevel, MaybeThreadSafe, C_CompilerType, CSharp_CompilerType,
          ReuseStrategy, MaybeILVersion,
-        MaybeFeedback, HostEnvType, TargetEnvType, FileInstallCmd).
+        MaybeFeedback, HostEnvType, SystemEnvType, TargetEnvType,
+        FileInstallCmd).

  get_options(Globals, Globals ^ g_options).
  get_target(Globals, Globals ^ g_target).
@@ -647,6 +651,7 @@ get_reuse_strategy(Globals, Globals ^ g_reuse_strategy).
  get_maybe_il_version_number(Globals, Globals ^ g_maybe_il_version_number).
  get_maybe_feedback_info(Globals, Globals ^ g_maybe_feedback).
  get_host_env_type(Globals, Globals ^ g_host_env_type).
+get_system_env_type(Globals, Globals ^ g_system_env_type).
  get_target_env_type(Globals, Globals ^ g_target_env_type).
  get_file_install_cmd(Globals, Globals ^ g_file_install_cmd).

diff --git a/compiler/handle_options.m b/compiler/handle_options.m
index 64f4006..f3c3e6f 100644
--- a/compiler/handle_options.m
+++ b/compiler/handle_options.m
@@ -218,14 +218,16 @@ convert_option_table_result_to_globals(ok(OptionTable0), Errors,
          TagsMethod, TermNorm, Term2Norm, TraceLevel, TraceSuppress,
          SSTraceLevel, MaybeThreadSafe, C_CompilerType, CSharp_CompilerType,
          ReuseStrategy, MaybeILVersion,
-        MaybeFeedbackInfo, HostEnvType, TargetEnvType, [], CheckErrors, !IO),
+        MaybeFeedbackInfo, HostEnvType, SystemEnvType, TargetEnvType,
+        [], CheckErrors, !IO),
      (
          CheckErrors = [],
          convert_options_to_globals(OptionTable, Target, GC_Method,
              TagsMethod, TermNorm, Term2Norm, TraceLevel,
              TraceSuppress, SSTraceLevel, MaybeThreadSafe, C_CompilerType,
              CSharp_CompilerType, ReuseStrategy,
-            MaybeILVersion, MaybeFeedbackInfo, HostEnvType, TargetEnvType,
+            MaybeILVersion, MaybeFeedbackInfo,
+            HostEnvType, SystemEnvType, TargetEnvType,
              [], Errors, Globals, !IO)
      ;
          CheckErrors = [_ | _],
@@ -239,14 +241,14 @@ convert_option_table_result_to_globals(ok(OptionTable0), Errors,
      trace_suppress_items::out, ssdb_trace_level::out, may_be_thread_safe::out,
      c_compiler_type::out, csharp_compiler_type::out,
      reuse_strategy::out, maybe(il_version_number)::out,
-    maybe(feedback_info)::out, env_type::out, env_type::out,
+    maybe(feedback_info)::out, env_type::out, env_type::out, env_type::out,
      list(string)::in, list(string)::out, io::di, io::uo) is det.

  check_option_values(!OptionTable, Target, GC_Method, TagsMethod,
          TermNorm, Term2Norm, TraceLevel, TraceSuppress, SSTraceLevel,
          MaybeThreadSafe, C_CompilerType, CSharp_CompilerType,
          ReuseStrategy, MaybeILVersion, MaybeFeedbackInfo,
-        HostEnvType, TargetEnvType, !Errors, !IO) :-
+        HostEnvType, SystemEnvType, TargetEnvType, !Errors, !IO) :-
      map.lookup(!.OptionTable, target, Target0),
      (
          Target0 = string(TargetStr),
@@ -575,6 +577,23 @@ check_option_values(!OptionTable, Target, GC_Method, TagsMethod,
              "\t(must be `posix', `cygwin', `msys' or `windows').",
              !Errors)
      ),
+    map.lookup(!.OptionTable, system_env_type, SystemEnvType0),
+    (
+        SystemEnvType0 = string(SystemEnvTypeStr),
+        ( if SystemEnvTypeStr = "" then
+            SystemEnvTypePrime = HostEnvType
+        else 
+            convert_env_type(SystemEnvTypeStr, SystemEnvTypePrime) 
+        )
+    ->
+        SystemEnvType = SystemEnvTypePrime
+    ;
+        SystemEnvType = env_type_posix,    % dummy
+        add_error(
+            "Invalid argument to option `--system-env-type'\n" ++
+            "\t(must be `posix', `cygwin', `msys' or `windows').",
+            !Errors)
+    ),
      map.lookup(!.OptionTable, target_env_type, TargetEnvType0),
      (
          TargetEnvType0 = string(TargetEnvTypeStr),
@@ -613,14 +632,16 @@ add_error(Error, Errors0, Errors) :-
      trace_suppress_items::in, ssdb_trace_level::in, may_be_thread_safe::in,
      c_compiler_type::in, csharp_compiler_type::in,
      reuse_strategy::in, maybe(il_version_number)::in, maybe(feedback_info)::in,
-    env_type::in, env_type::in, list(string)::in, list(string)::out,
+    env_type::in, env_type::in, env_type::in,
+    list(string)::in, list(string)::out,
      globals::out, io::di, io::uo) is det.

  convert_options_to_globals(OptionTable0, Target, GC_Method, TagsMethod0,
          TermNorm, Term2Norm, TraceLevel, TraceSuppress, SSTraceLevel,
          MaybeThreadSafe, C_CompilerType, CSharp_CompilerType,
          ReuseStrategy, MaybeILVersion, MaybeFeedbackInfo,
-        HostEnvType, TargetEnvType, !Errors, !:Globals, !IO) :-
+        HostEnvType, SystemEnvType, TargetEnvType,
+        !Errors, !:Globals, !IO) :-

      lookup_string_option(OptionTable0, install_command, InstallCmd),
      ( if InstallCmd = "" then
@@ -636,7 +657,8 @@ convert_options_to_globals(OptionTable0, Target, GC_Method, TagsMethod0,
          TermNorm, Term2Norm, TraceLevel, TraceSuppress, SSTraceLevel,
          MaybeThreadSafe, C_CompilerType, CSharp_CompilerType,
          ReuseStrategy, MaybeILVersion, MaybeFeedbackInfo,
-        HostEnvType, TargetEnvType, FileInstallCmd, !:Globals),
+        HostEnvType, SystemEnvType, TargetEnvType, FileInstallCmd,
+        !:Globals),

      globals.lookup_string_option(!.Globals, event_set_file_name,
          EventSetFileName0),
diff --git a/compiler/module_cmds.m b/compiler/module_cmds.m
index 06dc54d..19dd78a 100644
--- a/compiler/module_cmds.m
+++ b/compiler/module_cmds.m
@@ -609,8 +609,8 @@ invoke_system_command_maybe_filter_output(Globals, ErrorStream, Verbosity,

          % XXX we should get rid of use_win32
          ( use_win32 ->
-            get_host_env_type(Globals, HostEnvType),
-            ( HostEnvType = env_type_powershell ->
+            get_system_env_type(Globals, SystemEnvType),
+            ( SystemEnvType = env_type_powershell ->
                  ProcessOutputRedirected = string.append_list(
                      ["Get-Content ", TmpFile, " | ", ProcessOutput,
                          " > ", ProcessedTmpFile, " 2>&1"])
diff --git a/compiler/options.m b/compiler/options.m
index 4a07250..20acd83 100644
--- a/compiler/options.m
+++ b/compiler/options.m
@@ -1001,6 +1001,7 @@
      ;       restricted_command_line
      ;       env_type
      ;       host_env_type
+    ;       system_env_type
      ;       target_env_type

      % Miscellaneous Options
@@ -1917,6 +1918,7 @@ option_defaults_2(build_system_option, [
      restricted_command_line             -   bool(no),
      env_type                            -   string_special,
      host_env_type                       -   string("posix"),
+    system_env_type                     -   string(""),
      target_env_type                     -   string("posix")
  ]).
  option_defaults_2(miscellaneous_option, [
@@ -2893,6 +2895,7 @@ long_option("extra-library-header", extra_library_header).
  long_option("restricted-command-line", restricted_command_line).
  long_option("env-type",                env_type).
  long_option("host-env-type",           host_env_type).
+long_option("system-env-type",         system_env_type).
  long_option("target-env-type",         target_env_type).

  % misc options
@@ -3147,11 +3150,12 @@ special_handler(mercury_linkage_special, string(Flag),
              """shared"" or ""static"".")
      ).

-special_handler(env_type, string(EnvTypeStr), OptionTable0, ok(OptionTable)) :-
-    OptionTable =
-        map.set(map.set(OptionTable0,
-        host_env_type, string(EnvTypeStr)),
-        target_env_type, string(EnvTypeStr)).
+special_handler(env_type, string(EnvTypeStr), !.OptionTable, ok(!:OptionTable)) :-
+    override_options([
+            host_env_type   - string(EnvTypeStr),
+            system_env_type - string(EnvTypeStr),
+            target_env_type - string(EnvTypeStr)
+        ], !OptionTable).

  special_handler(inform_inferred, bool(Inform), !.OptionTable,
          ok(!:OptionTable)) :-
@@ -5876,11 +5880,15 @@ options_help_build_system -->
          "\tprograms will be invoked.",
          "\tThe <type> should be one of `posix', `cygwin', `msys', or",
          "\t`windows'.",
-        "\tThis option is equivalent to setting both --host-env-type and",
-        "\t--target-env-type to <type>.",
+        "\tThis option is equivalent to setting all of --host-env-type,",
+        "\t--system-env-type and --target-env-type to <type>.",
          "--host-env-type <type>",
          "\tSpecify the environment type in which the compiler will be",
          "\tinvoked.",
+        "--system-env-type <type>",
+        "\tSpecify the environment type in which external programs invoked by the",
+        "\tcompiler will run.",
+        "\tIf not specified, this defaults to the value given by --host-env-type.",
          "--target-env-type <type>",
          "\tSpecify the environment type in which generated programs will be",
          "\tinvoked."
diff --git a/doc/user_guide.texi b/doc/user_guide.texi
index 29992bd..39debd2 100644
--- a/doc/user_guide.texi
+++ b/doc/user_guide.texi
@@ -9549,8 +9549,8 @@ The environment type controls how the compiler and generated programs
  interact with the shell and other system tools.
  The @var{type} should be one of @samp{posix}, @samp{cygwin}, @samp{msys},
  or @samp{windows}.
-This option is equivalent to setting both @samp{--host-env-type} and
- at samp{--target-env-type} to @var{type}.
+This option is equivalent to setting all of @samp{--host-env-type},
+ at samp{--system-env-type} and @samp{--target-env-type} to @var{type}.

  @sp 1
  @item --host-env-type @var{type}
@@ -9559,6 +9559,13 @@ Specify the environment type in which the compiler will be invoked.
  (See above for a list of supported environment types.)

  @sp 1
+ at item --system-env-type @var{type}
+ at findex --system-env-type
+Sspecify the environment type in which external programs invoked by the compiler
+will run.
+If not specified, this defaults to the value given by @samp{--host-envy-type}.
+
+ at sp 1
  @item --target-env-type @var{type}
  @findex --target-env-type
  Specify the environment type in which generated programs will be invoked.



More information about the reviews mailing list