[m-rev.] for review: new options for specifying the environment type

Julien Fischer juliensf at csse.unimelb.edu.au
Fri Dec 17 17:51:50 AEDT 2010


For review by anyone.

Branches: main

Add new command line options for specifying the environment type(s) to the
compiler.  (By environment type, we mean the type of the shell, plus various
system utilities that may be invoked by the compiler.)

One new option, --host-env-type, specifies the environment under which the
compiler will be invoked; the other, --target-env-type, specifies the
environment under which a generated executable will be run.  (In the case,
where they are the same, a third new option, --env-type, can be used to set
both.)

On problem that this is intended to address is that when compiling Mercury
programs in the Java, Erlang or C# grades that are targeted at Windows
(e.g. to be run from the Windows command prompt), we should generate a batch
file rather than a shell script for the wrapper script.  (Moreover, in that
situation a user should still have the option of invoking mmc from the
Cygwin or MinGW shells.)

Future use of this feature will include removing some questionable assumptions
about the environment from the compiler, e.g. fixing the XXX in
options.quote_arg/1, as well improving support for cross-compilation in
general.

The environment types currently recognised are: posix, cygwin, msys and
windows.  Cygwin and MinGW/MSYS are treated separately since they are often
different enough from "posix" that this is required.  "windows"  means the
Windows command prompt, i.e. cmd.exe; although we may add another variant for
use with PowerShell in the future.

This diff adds support for the new options; once it has bootstrapped I will
post a change that fixes the problem with the Java etc grades mentioned above
and add the remainder of the documentation.

compiler/options.m:
 	Add the new options: --host-env-type, --target-env-type, --env-type.

compiler/globals.m:
 	Add a type describing the possible environment types.

 	Add slots to the globals structure for the host and target
 	environment types.  Add predicates for querying the
 	environment types.

compiler/handle_options.m:
 	Handle the new options.

Julien.

Index: compiler/globals.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/globals.m,v
retrieving revision 1.97
diff -u -r1.97 globals.m
--- compiler/globals.m	15 Dec 2010 06:29:36 -0000	1.97
+++ compiler/globals.m	17 Dec 2010 06:39:45 -0000
@@ -154,6 +154,26 @@
      --->    same_cons_id
      ;       within_n_cells_difference(int).

+    % The env_type specifies the environment in which the compiler is either
+    % running or targeting (in the case where we are cross-compiling),
+    % i.e. the shell and system utilities.
+    %
+:- type env_type
+    --->    env_type_posix
+            % A generic POSIX-like environment: this covers most Linux systems,
+            % Mac OS X, FreeBSD, Solaris etc.
+ 
+    ;       env_type_cygwin
+            % The Cygwin shell and utilities on Windows.
+
+    ;       env_type_msys
+            % MinGW with the MSYS environment on Windows.
+
+    ;       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.
+
      % Map from module name to file name.
      %
  :- type source_file_map == map(module_name, string).
@@ -169,6 +189,7 @@
      is semidet.
  :- pred convert_reuse_strategy(string::in, int::in, reuse_strategy::out)
      is semidet.
+:- pred convert_env_type(string::in, env_type::out) is semidet.

  %-----------------------------------------------------------------------------%
  %
@@ -187,8 +208,8 @@
      tags_method::in, termination_norm::in, termination_norm::in,
      trace_level::in, trace_suppress_items::in,
      may_be_thread_safe::in, c_compiler_type::in, reuse_strategy::in,
-    maybe(il_version_number)::in, maybe(feedback_info)::in, globals::out) 
-    is det.
+    maybe(il_version_number)::in, maybe(feedback_info)::in, env_type::in,
+    env_type::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.
@@ -206,6 +227,8 @@
  :- pred get_maybe_il_version_number(globals::in, maybe(il_version_number)::out)
      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_target_env_type(globals::in, env_type::out) is det.

  :- pred set_option(option::in, option_data::in, globals::in, globals::out)
      is det.
@@ -390,7 +413,7 @@
      % one is unknown won't be accepted.  (It wouldn't be useful
      % in any case.)
      %
-    % <major> must be >= 2 (Mercury won't work with anthing older
+    % <major> must be >= 2 (Mercury won't work with anything older
      % than that and <minor> and <patch> must be non-negative.
      %
  :- pred convert_gcc_version(string::in, string::in, string::in,
@@ -434,6 +457,11 @@
          false
      ).

+convert_env_type("posix",   env_type_posix).
+convert_env_type("cygwin",  env_type_cygwin).
+convert_env_type("msys",    env_type_msys).
+convert_env_type("windows", env_type_win_cmd).
+
  convert_reuse_strategy("same_cons_id", _, same_cons_id).
  convert_reuse_strategy("within_n_cells_difference", NCells,
      within_n_cells_difference(NCells)).
@@ -482,17 +510,19 @@
                  g_c_compiler_type           :: c_compiler_type,
                  g_reuse_strategy            :: reuse_strategy,
                  g_maybe_il_version_number   :: maybe(il_version_number),
-                g_maybe_feedback            :: maybe(feedback_info)
+                g_maybe_feedback            :: maybe(feedback_info),
+                g_host_env_type             :: env_type,
+                g_target_env_type           :: env_type
              ).

  globals_init(Options, Target, GC_Method, TagsMethod,
          TerminationNorm, Termination2Norm, TraceLevel, TraceSuppress,
          MaybeThreadSafe, C_CompilerType, ReuseStrategy, MaybeILVersion,
-        MaybeFeedback, Globals) :-
+        MaybeFeedback, HostEnvType, TargetEnvType, Globals) :-
      Globals = globals(Options, Target, GC_Method, TagsMethod,
          TerminationNorm, Termination2Norm, TraceLevel, TraceSuppress,
          MaybeThreadSafe, C_CompilerType, ReuseStrategy, MaybeILVersion,
-        MaybeFeedback).
+        MaybeFeedback, HostEnvType, TargetEnvType).

  get_options(Globals, Globals ^ g_options).
  get_target(Globals, Globals ^ g_target).
@@ -507,6 +537,8 @@
  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_target_env_type(Globals, Globals ^ g_target_env_type).

  get_backend_foreign_languages(Globals, ForeignLangs) :-
      lookup_accumulating_option(Globals, backend_foreign_languages, LangStrs),
Index: compiler/handle_options.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/handle_options.m,v
retrieving revision 1.356
diff -u -r1.356 handle_options.m
--- compiler/handle_options.m	14 Oct 2010 05:00:05 -0000	1.356
+++ compiler/handle_options.m	17 Dec 2010 06:39:46 -0000
@@ -215,13 +215,14 @@
      check_option_values(OptionTable0, OptionTable, Target, GC_Method,
          TagsMethod, TermNorm, Term2Norm, TraceLevel, TraceSuppress,
          MaybeThreadSafe, C_CompilerType, ReuseStrategy, MaybeILVersion,
-        MaybeFeedbackInfo, [], CheckErrors, !IO),
+        MaybeFeedbackInfo, HostEnvType, TargetEnvType, [], CheckErrors, !IO),
      (
          CheckErrors = [],
          convert_options_to_globals(OptionTable, Target, GC_Method,
              TagsMethod, TermNorm, Term2Norm, TraceLevel,
              TraceSuppress, MaybeThreadSafe, C_CompilerType, ReuseStrategy,
-            MaybeILVersion, MaybeFeedbackInfo, [], Errors, Globals, !IO)
+            MaybeILVersion, MaybeFeedbackInfo, HostEnvType, TargetEnvType,
+            [], Errors, Globals, !IO)
      ;
          CheckErrors = [_ | _],
          Errors = CheckErrors,
@@ -233,13 +234,13 @@
      termination_norm::out, termination_norm::out, trace_level::out,
      trace_suppress_items::out, may_be_thread_safe::out,
      c_compiler_type::out, reuse_strategy::out, maybe(il_version_number)::out,
-    maybe(feedback_info)::out, list(string)::in, list(string)::out,
-    io::di, io::uo) is det.
+    maybe(feedback_info)::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, MaybeThreadSafe,
          C_CompilerType, ReuseStrategy, MaybeILVersion, MaybeFeedbackInfo,
-        !Errors, !IO) :-
+        HostEnvType, TargetEnvType, !Errors, !IO) :-
      map.lookup(!.OptionTable, target, Target0),
      (
          Target0 = string(TargetStr),
@@ -458,8 +459,34 @@
      ;
          % No feedback info.
          MaybeFeedbackInfo = no
-    ). 
- 
+    ),
+    map.lookup(!.OptionTable, host_env_type, HostEnvType0),
+    (
+        HostEnvType0 = string(HostEnvTypeStr),
+        convert_env_type(HostEnvTypeStr, HostEnvTypePrime)
+    ->
+        HostEnvType = HostEnvTypePrime
+    ;
+        HostEnvType = env_type_posix,   % dummy
+        add_error(
+            "Invalid argument to option `--host-env-type'\n" ++
+            "\t(must be `posix', `cygwin', `msys' or `windows').",
+            !Errors)
+    ),
+    map.lookup(!.OptionTable, target_env_type, TargetEnvType0),
+    (
+        TargetEnvType0 = string(TargetEnvTypeStr),
+        convert_env_type(TargetEnvTypeStr, TargetEnvTypePrime)
+    ->
+        TargetEnvType = TargetEnvTypePrime
+    ;
+        TargetEnvType = env_type_posix,   % dummy
+        add_error(
+            "Invalid argument to option `--target-env-type'\n" ++
+            "\t(must be `posix', `cygwin', `msys' or `windows').",
+            !Errors)
+    ).
+
  :- pred add_error(string::in, list(string)::in, list(string)::out) is det.

  add_error(Error, Errors0, Errors) :-
@@ -474,16 +501,17 @@
      termination_norm::in, termination_norm::in, trace_level::in,
      trace_suppress_items::in, may_be_thread_safe::in, c_compiler_type::in,
      reuse_strategy::in, maybe(il_version_number)::in, maybe(feedback_info)::in,
-    list(string)::in, list(string)::out, globals::out, io::di, io::uo) is det.
+    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, MaybeThreadSafe,
          C_CompilerType, ReuseStrategy, MaybeILVersion, MaybeFeedbackInfo,
-        !Errors, !:Globals, !IO) :-
+        HostEnvType, TargetEnvType, !Errors, !:Globals, !IO) :-
      globals_init(OptionTable0, Target, GC_Method, TagsMethod0,
          TermNorm, Term2Norm, TraceLevel, TraceSuppress, MaybeThreadSafe,
          C_CompilerType, ReuseStrategy, MaybeILVersion, MaybeFeedbackInfo,
-        !:Globals),
+        HostEnvType, TargetEnvType, !:Globals),

      globals.lookup_string_option(!.Globals, event_set_file_name,
          EventSetFileName0),
Index: compiler/options.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/options.m,v
retrieving revision 1.682
diff -u -r1.682 options.m
--- compiler/options.m	15 Dec 2010 06:29:53 -0000	1.682
+++ compiler/options.m	17 Dec 2010 06:39:48 -0000
@@ -949,6 +949,9 @@
      ;       show_make_times
      ;       extra_library_header
      ;       restricted_command_line
+    ;       env_type
+    ;       host_env_type
+    ;       target_env_type

      % Miscellaneous Options
      ;       filenames_from_stdin
@@ -1812,7 +1815,10 @@
      order_make_by_timestamp             -   bool(no),
      show_make_times                     -   bool(no),
      extra_library_header                -   accumulating([]),
-    restricted_command_line             -   bool(no)
+    restricted_command_line             -   bool(no),
+    env_type                            -   string_special,
+    host_env_type                       -   string("posix"),
+    target_env_type                     -   string("posix")
  ]).
  option_defaults_2(miscellaneous_option, [
      % Miscellaneous Options
@@ -2732,6 +2738,9 @@
  long_option("extra-lib-header",     extra_library_header).
  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("target-env-type",         target_env_type).

  % misc options
  long_option("typecheck-ambiguity-warn-limit",
@@ -2973,6 +2982,12 @@
              """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)).
+
  %-----------------------------------------------------------------------------%

  option_table_add_mercury_library_directory(OptionTable0, Dir) =
@@ -5598,6 +5613,20 @@
          "\tEnable this option if your shell doesn't support long command lines.",
          "\tThis option uses temporary files to pass arguments to sub-commands.",
          "\t(This option is only supported by `mmc --make'.)"
+        %
+        % XXX the following are commented out until they are actually useful.
+        %
+        %"--env-type <type>",
+        %"\tSpecify the the environment type for the compiler."
+        %"\tThe <type> should be one of `posix', `cygwin', `msys', or `windows'."
+        %"\tThis option is equivalent to setting both --host-env-type and"
+        %"\t--target-env-type to <type>."
+        %"--host-env-type <type>",
+        %"\tSpecify the environment type under which the compiler will be",
+        %"\tinvoked."
+        %"--target-env-type <type>",
+        %"\tSpecify the environment type user which compiled programs will be",
+        %"\tinvoked."
      ]).

  :- pred options_help_misc(io::di, io::uo) is det.

--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list