[m-rev.] diff: fix problems with --restricted-command-line (bug #99)

Julien Fischer juliensf at csse.unimelb.edu.au
Sun Oct 9 03:08:02 AEDT 2011


Branches: main, 11.07

Fix the remainder of bug #99: when using --restricted-command-line with MSVC we
need to manually tell the linker some extra details that it usually infers from
the object files.  (With --restricted-command-line all the object files are
placed in a temporary archive beforehand, so there are no object files on the
command line used to invoke the linker.)

compiler/compile_target_code.m:
 	When using MSVC and --restricted-command-line to link an executable
 	manually set the target machine type, program entry point and subsystem
 	type.  Also, specify that we need to link against libc.

 	Add a note about revisiting some of this when we add support for
 	64-bit Windows.

Julien.

Index: compiler/compile_target_code.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/compile_target_code.m,v
retrieving revision 1.175
diff -u -r1.175 compile_target_code.m
--- compiler/compile_target_code.m	18 Sep 2011 15:14:33 -0000	1.175
+++ compiler/compile_target_code.m	8 Oct 2011 15:42:44 -0000
@@ -1905,6 +1905,13 @@

      % Find which system libraries are needed.
      get_system_libs(Globals, LinkTargetType, SystemLibs),
+ 
+    % With --restricted-command-line we may need to some additional
+    % options to the linker.
+    % (See the comment above get_restricted_command_lin_link_opts/3 for
+    % details.)
+    get_restricted_command_line_link_opts(Globals, LinkTargetType,
+        ResCmdLinkOpts),

      globals.lookup_accumulating_option(Globals, LDFlagsOpt, LDFlagsList),
      join_string_list(LDFlagsList, "", "", " ", LDFlags),
@@ -2003,6 +2010,7 @@
                      InstallNameOpt, " ",
                      DebugOpts, " ",
                      Frameworks, " ",
+                    ResCmdLinkOpts, " ",
                      LDFlags, " ",
                      LinkLibraries, " ",
                      MercuryStdLibs, " ",
@@ -2386,7 +2394,61 @@
      globals.lookup_bool_option(Globals, parallel, Parallel),
      globals.get_gc_method(Globals, GCMethod),
      UseThreadLibs = ( ( Parallel = yes ; GCMethod = gc_mps ) -> yes ; no ).
- 
+
+    % When using --restricted-command-line with Visual C we add all the object
+    % files to a temporary archive before linking an executable.
+    % However, if only .lib files are given on the command line then 
+    % the linker needs to manually told some details that it usually infers
+    % from the object files, for example the program entry point and the
+    % target machine type.
+    %
+:- pred get_restricted_command_line_link_opts(globals::in,
+    linked_target_type::in, string::out) is det.
+
+get_restricted_command_line_link_opts(Globals, LinkTargetType, ResCmdLinkOpts) :-
+    globals.lookup_bool_option(Globals, restricted_command_line,
+        RestrictedCommandLine),
+    (
+        RestrictedCommandLine = yes,
+        (
+            LinkTargetType = executable,
+            get_c_compiler_type(Globals, C_CompilerType),
+            (
+                C_CompilerType = cc_cl(_),
+                % XXX WIN64 - this will need to be revisited when we begin
+                % supporting 64-bit Windows.
+                ResCmdLinkFlags = [
+                    "-subsystem:console",
+                    "-machine:x86",
+                    "-entry:mainCRTStartup",
+                    "-defaultlib:libcmt"
+                ],
+                join_string_list(ResCmdLinkFlags, "", "", " ", ResCmdLinkOpts)
+            ;
+                ( C_CompilerType = cc_gcc(_, _, _)
+                ; C_CompilerType = cc_clang(_)
+                ; C_CompilerType = cc_lcc
+                ; C_CompilerType = cc_unknown
+                ),
+                ResCmdLinkOpts = ""
+            )
+        ;
+            ( LinkTargetType = static_library
+            ; LinkTargetType = shared_library
+            ; LinkTargetType = csharp_executable
+            ; LinkTargetType = csharp_library
+            ; LinkTargetType = java_launcher
+            ; LinkTargetType = java_archive
+            ; LinkTargetType = erlang_launcher
+            ; LinkTargetType = erlang_archive
+            ),
+            ResCmdLinkOpts = ""
+        )
+    ;
+        RestrictedCommandLine = no,
+        ResCmdLinkOpts = ""
+    ).
+
  post_link_make_symlink_or_copy(ErrorStream, LinkTargetType, ModuleName,
          Globals, Succeeded, MadeSymlinkOrCopy, !IO) :-
      globals.lookup_bool_option(Globals, use_grade_subdirs, UseGradeSubdirs),

--------------------------------------------------------------------------
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