[m-rev.] for review: new options for querying C compiler flags

Julien Fischer juliensf at csse.unimelb.edu.au
Tue Jan 17 04:49:31 AEDT 2012


Branches: main

Add two new options to the compiler for querying what flags are passed to the
C compiler at a finer level of detail than that provided by --output-cflags.
The first new option, --output-grade-defines, prints the flags passed to the C
compiler to define the macros that specify the current compilation grade.
The second new option, --output-c-include-dir-flags, prints the flags passed
to C compiler that tell it where to search for C header files.

The rationale for the addition of the new options is that when compiling C++ or
Objective-C programs that call exported Mercury procedures via a standalone
interface we need both the flags for the grade defines and the include search
directories but other flags that we usually pass the C compiler may not be
appropriate for either a C++ or Objective-C compiler.

compiler/options.m:
 	Recognise the new options.

compiler/mercury_compile.m:
 	Handle the new options.

compiler/compile_target_code.m:
 	Separate out the code that generates the grade macro define
 	flags into a separate predicate.

 	Do likewise with the code that generates the flags for specifying
 	C compiler include search directories.

doc/use_guide.texi:
 	Document the new options.

samples/c_interface/standalone_c/Makefile:
 	Illustrate how to use the new options when compiling a program that
 	uses a standalone interface.  Explain why it may not be appropriate
 	to use the output of --output-cflags when compiling code that calls
 	exported Mercury procedures.

Julien.

Index: compiler/compile_target_code.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/compile_target_code.m,v
retrieving revision 1.184
diff -u -r1.184 compile_target_code.m
--- compiler/compile_target_code.m	16 Jan 2012 06:07:40 -0000	1.184
+++ compiler/compile_target_code.m	16 Jan 2012 17:43:03 -0000
@@ -218,6 +218,21 @@
  :- pred output_c_compiler_flags(globals::in, io.output_stream::in,
      io::di, io::uo) is det.

+    % Output the C compiler flags that define the macros used to specify the
+    % current compilation grade to the given stream.
+    % This predicate is used to implement the `--output-grade-defines' option.
+    %
+:- pred output_grade_defines(globals::in, io.output_stream::in,
+    io::di, io::uo) is det.
+ 
+    % Output the C compiler flags that specify where the C compiler should
+    % search for header files to the given stream.
+    % This predicate is used to implement the `--output-c-include-dir-flags'
+    % option.
+    %
+:- pred output_c_include_directory_flags(globals::in, io.output_stream::in,
+    io::di, io::uo) is det.
+
      % Output the list of flags required to link against the selected set
      % of Mercury libraries (the standard libraries, plus any other specified
      % via the --ml option) in the current grade.
@@ -429,13 +444,199 @@
          UseSubdirs = no,
          SubDirInclOpt = ""
      ),
-    globals.lookup_accumulating_option(Globals, c_include_directory,
-        C_Incl_Dirs),
-    InclOpt = string.append_list(list.condense(list.map(
-        (func(C_INCL) = ["-I", quote_arg(C_INCL), " "]), C_Incl_Dirs))),

+    gather_c_include_dir_flags(Globals, InclOpt),
      get_framework_directories(Globals, FrameworkInclOpt),
+    gather_grade_defines(Globals, PIC, GradeDefinesOpts),

+    globals.lookup_bool_option(Globals, gcc_global_registers, GCC_Regs),
+    (
+        GCC_Regs = yes,
+        globals.lookup_string_option(Globals, cflags_for_regs,
+            CFLAGS_FOR_REGS)
+    ;
+        GCC_Regs = no,
+        CFLAGS_FOR_REGS = ""
+    ),
+    globals.lookup_bool_option(Globals, gcc_non_local_gotos, GCC_Gotos),
+    (
+        GCC_Gotos = yes,
+        globals.lookup_string_option(Globals, cflags_for_gotos,
+            CFLAGS_FOR_GOTOS)
+    ;
+        GCC_Gotos = no,
+        CFLAGS_FOR_GOTOS = ""
+    ),
+    globals.lookup_bool_option(Globals, parallel, Parallel),
+    (
+        Parallel = yes,
+        globals.lookup_string_option(Globals, cflags_for_threads,
+            CFLAGS_FOR_THREADS)
+    ;
+        Parallel = no,
+        CFLAGS_FOR_THREADS = ""
+    ),
+    (
+        PIC = pic,
+        globals.lookup_string_option(Globals, cflags_for_pic, CFLAGS_FOR_PIC)
+    ;
+        ( PIC = link_with_pic
+        ; PIC = non_pic
+        ),
+        CFLAGS_FOR_PIC = ""
+    ),
+    globals.lookup_bool_option(Globals, target_debug, Target_Debug),
+    (
+        Target_Debug = yes,
+        globals.lookup_string_option(Globals, cflags_for_debug,
+            Target_DebugOpt0),
+        Target_DebugOpt = Target_DebugOpt0 ++ " "
+    ;
+        Target_Debug = no,
+        Target_DebugOpt = ""
+    ),
+    globals.lookup_bool_option(Globals, use_trail, UseTrail),
+    (
+        UseTrail = yes,
+        % With tagged trail entries function trailing will not work unless the
+        % C functions stored on the trail are aligned on word boundaries (or a
+        % multiple thereof).  The assemblers on some systems, and some gcc
+        % optimisation settings, do not align functions, so we need to
+        % explicitly pass -falign-functions in trailing grades to ensure that
+        % C functions are appropriately aligned.
+        %
+        % Note that this will also affect the untagged version of the trail,
+        % but that shouldn't matter.
+        %
+        globals.get_c_compiler_type(Globals, C_CompilerType),
+        (
+            C_CompilerType = cc_gcc(_, _, _),
+            globals.lookup_int_option(Globals, bytes_per_word, BytesPerWord),
+            C_FnAlignOpt = string.format("-falign-functions=%d ",
+                [i(BytesPerWord)])
+        ;
+            % XXX Check whether we need to do anything for these C compilers?
+            ( C_CompilerType = cc_clang(_)
+            ; C_CompilerType = cc_lcc
+            ; C_CompilerType = cc_cl(_)
+            ),
+            C_FnAlignOpt = ""
+        ;
+            C_CompilerType = cc_unknown,
+            C_FnAlignOpt = ""
+        )
+    ;
+        UseTrail = no,
+        C_FnAlignOpt = ""
+    ),
+    globals.lookup_bool_option(Globals, type_layout, TypeLayoutOption),
+    (
+        TypeLayoutOption = no,
+        TypeLayoutOpt = "-DMR_NO_TYPE_LAYOUT "
+    ;
+        TypeLayoutOption = yes,
+        TypeLayoutOpt = ""
+    ),
+    globals.lookup_bool_option(Globals, c_optimize, C_optimize),
+    (
+        C_optimize = yes,
+        globals.lookup_string_option(Globals, cflags_for_optimization,
+            OptimizeOpt)
+    ;
+        C_optimize = no,
+        OptimizeOpt = ""
+    ),
+    globals.lookup_bool_option(Globals, ansi_c, Ansi),
+    (
+        Ansi = yes,
+        globals.lookup_string_option(Globals, cflags_for_ansi, AnsiOpt)
+    ;
+        Ansi = no,
+        AnsiOpt = ""
+    ),
+    globals.lookup_bool_option(Globals, inline_alloc, InlineAlloc),
+    (
+        InlineAlloc = yes,
+        % XXX disabled because inline allocation is broken in gc7.0 alpha6.
+        % InlineAllocOpt = "-DMR_INLINE_ALLOC "
+        InlineAllocOpt = ""
+    ;
+        InlineAlloc = no,
+        InlineAllocOpt = ""
+    ),
+    globals.lookup_bool_option(Globals, warn_target_code, Warn),
+    (
+        Warn = yes,
+        globals.lookup_string_option(Globals, cflags_for_warnings, WarningOpt)
+    ;
+        Warn = no,
+        WarningOpt = ""
+    ),
+
+    % The -floop-optimize option is incompatible with the global
+    % register code we generate on Darwin PowerPC.
+    % See the hard_coded/ppc_bug test case for an example
+    % program which fails with this optimization.
+
+    globals.lookup_string_option(Globals, fullarch, FullArch),
+    (
+        globals.lookup_bool_option(Globals, highlevel_code, no),
+        globals.lookup_bool_option(Globals, gcc_global_registers, yes),
+        string.prefix(FullArch, "powerpc-apple-darwin")
+    ->
+        AppleGCCRegWorkaroundOpt = "-fno-loop-optimize "
+    ;
+        AppleGCCRegWorkaroundOpt = ""
+    ),
+ 
+    % Workaround performance problem(s) with gcc that causes the C files
+    % generated in debugging grades to compile very slowly at -O1 and above.
+    % (Changes here need to be reflected in scripts/mgnuc.in.)
+    (
+        globals.lookup_bool_option(Globals, exec_trace, yes),
+        arch_is_apple_darwin(FullArch)
+    ->
+        OverrideOpts = "-O0"
+    ;
+        OverrideOpts = ""
+    ),
+ 
+    % Be careful with the order here!  Some options override others,
+    % e.g. CFLAGS_FOR_REGS must come after OptimizeOpt so that
+    % it can override -fomit-frame-pointer with -fno-omit-frame-pointer.
+    % Also be careful that each option is separated by spaces.
+    % 
+    % In general, user supplied C compiler flags, i.e. CFLAGS and
+    % CC_Specific_CFLAGS below, should be able to override those introduced by
+    % the Mercury compiler.
+    % In some circumstances we want to prevent the user doing this, typically
+    % where we know the behaviour of a particular C compiler is buggy; the
+    % last option, OverrideOpts, does this -- because of this it must be
+    % listed after CFLAGS and CC_Specific_CFLAGS.
+    %
+    string.append_list([
+        SubDirInclOpt, InclOpt, " ",
+        FrameworkInclOpt, " ",
+        OptimizeOpt, " ",
+        GradeDefinesOpts,
+        CFLAGS_FOR_REGS, " ", CFLAGS_FOR_GOTOS, " ",
+        CFLAGS_FOR_THREADS, " ", CFLAGS_FOR_PIC, " ",
+        Target_DebugOpt,
+        TypeLayoutOpt,
+        InlineAllocOpt,
+        AnsiOpt, " ", 
+        AppleGCCRegWorkaroundOpt,
+        C_FnAlignOpt, 
+        WarningOpt, " ", 
+        CFLAGS, " ",
+        CC_Specific_CFLAGS, " ",
+        OverrideOpts], AllCFlags).
+
+%-----------------------------------------------------------------------------%
+
+:- pred gather_grade_defines(globals::in, pic::in, string::out) is det.
+
+gather_grade_defines(Globals, PIC, GradeDefines) :-
      globals.lookup_bool_option(Globals, highlevel_code, HighLevelCode),
      (
          HighLevelCode = yes,
@@ -464,24 +665,18 @@
      globals.lookup_bool_option(Globals, gcc_global_registers, GCC_Regs),
      (
          GCC_Regs = yes,
-        globals.lookup_string_option(Globals, cflags_for_regs,
-            CFLAGS_FOR_REGS),
          RegOpt = "-DMR_USE_GCC_GLOBAL_REGISTERS "
      ;
          GCC_Regs = no,
-        CFLAGS_FOR_REGS = "",
          RegOpt = ""
      ),
      globals.lookup_bool_option(Globals, gcc_non_local_gotos, GCC_Gotos),
      (
          GCC_Gotos = yes,
-        GotoOpt = "-DMR_USE_GCC_NONLOCAL_GOTOS ",
-        globals.lookup_string_option(Globals, cflags_for_gotos,
-            CFLAGS_FOR_GOTOS)
+        GotoOpt = "-DMR_USE_GCC_NONLOCAL_GOTOS "
      ;
          GCC_Gotos = no,
-        GotoOpt = "",
-        CFLAGS_FOR_GOTOS = ""
+        GotoOpt = ""
      ),
      globals.lookup_bool_option(Globals, asm_labels, ASM_Labels),
      (
@@ -494,13 +689,10 @@
      globals.lookup_bool_option(Globals, parallel, Parallel),
      (
          Parallel = yes,
-        ParallelOpt = "-DMR_THREAD_SAFE ",
-        globals.lookup_string_option(Globals, cflags_for_threads,
-            CFLAGS_FOR_THREADS)
+        ParallelOpt = "-DMR_THREAD_SAFE "
      ;
          Parallel = no,
-        ParallelOpt = "",
-        CFLAGS_FOR_THREADS = ""
+        ParallelOpt = ""
      ),
      globals.lookup_bool_option(Globals, threadscope, Threadscope),
      (
@@ -592,15 +784,12 @@
      ),
      (
          PIC = pic,
-        globals.lookup_string_option(Globals, cflags_for_pic, CFLAGS_FOR_PIC),
          PIC_Reg = yes
      ;
          PIC = link_with_pic,
-        CFLAGS_FOR_PIC = "",
          PIC_Reg = yes
      ;
          PIC = non_pic,
-        CFLAGS_FOR_PIC = "",
          globals.lookup_bool_option(Globals, pic_reg, PIC_Reg)
      ),
      (
@@ -670,16 +859,6 @@
          ExtendOpt = unexpected($module, $pred,
              "--extend-stacks-when-needed and --stack-segments")
      ),
-    globals.lookup_bool_option(Globals, target_debug, Target_Debug),
-    (
-        Target_Debug = yes,
-        globals.lookup_string_option(Globals, cflags_for_debug,
-            Target_DebugOpt0),
-        Target_DebugOpt = Target_DebugOpt0 ++ " "
-    ;
-        Target_Debug = no,
-        Target_DebugOpt = ""
-    ),
      globals.lookup_bool_option(Globals, low_level_debug, LL_Debug),
      (
          LL_Debug = yes,
@@ -692,33 +871,6 @@
      (
          UseTrail = yes,
          UseTrailOpt = "-DMR_USE_TRAIL ",
-        % With tagged trail entries function trailing will not work unless the
-        % C functions stored on the trail are aligned on word boundaries (or a
-        % multiple thereof).  The assemblers on some systems, and some gcc
-        % optimisation settings, do not align functions, so we need to
-        % explicitly pass -falign-functions in trailing grades to ensure that
-        % C functions are appropriately aligned.
-        %
-        % Note that this will also affect the untagged version of the trail,
-        % but that shouldn't matter.
-        %
-        globals.get_c_compiler_type(Globals, C_CompilerType),
-        (
-            C_CompilerType = cc_gcc(_, _, _),
-            globals.lookup_int_option(Globals, bytes_per_word, BytesPerWord),
-            C_FnAlignOpt = string.format("-falign-functions=%d ",
-                [i(BytesPerWord)])
-        ;
-            % XXX Check whether we need to do anything for these C compilers?
-            ( C_CompilerType = cc_clang(_)
-            ; C_CompilerType = cc_lcc
-            ; C_CompilerType = cc_cl(_)
-            ),
-            C_FnAlignOpt = ""
-        ;
-            C_CompilerType = cc_unknown,
-            C_FnAlignOpt = ""
-        ),
          globals.lookup_bool_option(Globals, trail_segments, TrailSegments),
          (
              TrailSegments = yes,
@@ -730,7 +882,6 @@
      ;
          UseTrail = no,
          UseTrailOpt = "",
-        C_FnAlignOpt = "",
          TrailSegOpt = ""
      ),
      globals.lookup_bool_option(Globals, use_minimal_model_stack_copy,
@@ -804,102 +955,12 @@
          UseRegions = no,
          UseRegionsOpt = ""
      ),
-    globals.lookup_bool_option(Globals, type_layout, TypeLayoutOption),
-    (
-        TypeLayoutOption = no,
-        TypeLayoutOpt = "-DMR_NO_TYPE_LAYOUT "
-    ;
-        TypeLayoutOption = yes,
-        TypeLayoutOpt = ""
-    ),
-    globals.lookup_bool_option(Globals, c_optimize, C_optimize),
-    (
-        C_optimize = yes,
-        globals.lookup_string_option(Globals, cflags_for_optimization,
-            OptimizeOpt)
-    ;
-        C_optimize = no,
-        OptimizeOpt = ""
-    ),
-    globals.lookup_bool_option(Globals, ansi_c, Ansi),
-    (
-        Ansi = yes,
-        globals.lookup_string_option(Globals, cflags_for_ansi, AnsiOpt)
-    ;
-        Ansi = no,
-        AnsiOpt = ""
-    ),
-    globals.lookup_bool_option(Globals, inline_alloc, InlineAlloc),
-    (
-        InlineAlloc = yes,
-        % XXX disabled because inline allocation is broken in gc7.0 alpha6.
-        % InlineAllocOpt = "-DMR_INLINE_ALLOC "
-        InlineAllocOpt = ""
-    ;
-        InlineAlloc = no,
-        InlineAllocOpt = ""
-    ),
-    globals.lookup_bool_option(Globals, warn_target_code, Warn),
-    (
-        Warn = yes,
-        globals.lookup_string_option(Globals, cflags_for_warnings, WarningOpt)
-    ;
-        Warn = no,
-        WarningOpt = ""
-    ),
-
-    % The -floop-optimize option is incompatible with the global
-    % register code we generate on Darwin PowerPC.
-    % See the hard_coded/ppc_bug test case for an example
-    % program which fails with this optimization.
-
-    globals.lookup_string_option(Globals, fullarch, FullArch),
-    (
-        HighLevelCode = no,
-        GCC_Regs = yes,
-        string.prefix(FullArch, "powerpc-apple-darwin")
-    ->
-        AppleGCCRegWorkaroundOpt = "-fno-loop-optimize "
-    ;
-        AppleGCCRegWorkaroundOpt = ""
-    ),
- 
-    % Workaround performance problem(s) with gcc that causes the C files
-    % generated in debugging grades to compile very slowly at -O1 and above.
-    % (Changes here need to be reflected in scripts/mgnuc.in.)
-    (
-        ExecTrace = yes,
-        arch_is_apple_darwin(FullArch)
-    ->
-        OverrideOpts = "-O0"
-    ;
-        OverrideOpts = ""
-    ),
- 
-    % Be careful with the order here!  Some options override others,
-    % e.g. CFLAGS_FOR_REGS must come after OptimizeOpt so that
-    % it can override -fomit-frame-pointer with -fno-omit-frame-pointer.
-    % Also be careful that each option is separated by spaces.
-    % 
-    % In general, user supplied C compiler flags, i.e. CFLAGS and
-    % CC_Specific_CFLAGS below, should be able to override those introduced by
-    % the Mercury compiler.
-    % In some circumstances we want to prevent the user doing this, typically
-    % where we know the behaviour of a particular C compiler is buggy; the
-    % last option, OverrideOpts, does this -- because of this it must be
-    % listed after CFLAGS and CC_Specific_CFLAGS.
-    %
      string.append_list([
-        SubDirInclOpt, InclOpt, " ",
-        FrameworkInclOpt, " ",
-        OptimizeOpt, " ",
          HighLevelCodeOpt,
          NestedFunctionsOpt,
          HighLevelDataOpt,
          RegOpt, GotoOpt, AsmOpt,
          ParallelOpt,
-        CFLAGS_FOR_REGS, " ", CFLAGS_FOR_GOTOS, " ",
-        CFLAGS_FOR_THREADS, " ", CFLAGS_FOR_PIC, " ",
          ThreadscopeOpt,
          GC_Opt,
          ProfileCallsOpt, ProfileTimeOpt, 
@@ -908,23 +969,26 @@
          PIC_Reg_Opt,
          TagsOpt, NumTagBitsOpt,
          ExtendOpt,
-        Target_DebugOpt, LL_DebugOpt, DeclDebugOpt,
+        LL_DebugOpt, DeclDebugOpt,
          SourceDebugOpt,
          ExecTraceOpt,
          UseTrailOpt,
          TrailSegOpt,
          MinimalModelOpt,
          SinglePrecFloatOpt,
-        UseRegionsOpt,
-        TypeLayoutOpt,
-        InlineAllocOpt,
-        AnsiOpt, " ", 
-        AppleGCCRegWorkaroundOpt,
-        C_FnAlignOpt, 
-        WarningOpt, " ", 
-        CFLAGS, " ",
-        CC_Specific_CFLAGS, " ",
-        OverrideOpts], AllCFlags).
+        UseRegionsOpt], GradeDefines).
+ 
+%-----------------------------------------------------------------------------%
+
+:- pred gather_c_include_dir_flags(globals::in, string::out) is det.
+
+gather_c_include_dir_flags(Globals, InclOpt) :-
+    globals.lookup_accumulating_option(Globals, c_include_directory,
+        C_Incl_Dirs),
+    InclOpt = string.append_list(list.condense(list.map(
+        (func(C_INCL) = ["-I", quote_arg(C_INCL), " "]), C_Incl_Dirs))).
+
+%-----------------------------------------------------------------------------%

  :- pred gather_compiler_specific_flags(globals::in, string::out) is det.

@@ -3424,6 +3488,27 @@
      get_object_code_type(Globals, executable, PIC),
      gather_c_compiler_flags(Globals, PIC, CFlags),
      io.write_string(Stream, CFlags, !IO). 
+ 
+%-----------------------------------------------------------------------------%
+%
+% Grade defines flags.
+%
+
+output_grade_defines(Globals, Stream, !IO) :-
+    get_object_code_type(Globals, executable, PIC),
+    gather_grade_defines(Globals, PIC, GradeDefines),
+    io.write_string(Stream, GradeDefines, !IO),
+    io.nl(Stream, !IO).
+
+%-----------------------------------------------------------------------------%
+%
+% C include directory flags.
+%
+
+output_c_include_directory_flags(Globals, Stream, !IO) :-
+    gather_c_include_dir_flags(Globals, InclOpts),
+    io.write_string(Stream, InclOpts, !IO),
+    io.nl(Stream, !IO).

  %-----------------------------------------------------------------------------%
  %
Index: compiler/mercury_compile.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/mercury_compile.m,v
retrieving revision 1.519
diff -u -r1.519 mercury_compile.m
--- compiler/mercury_compile.m	10 Jan 2012 05:27:36 -0000	1.519
+++ compiler/mercury_compile.m	16 Jan 2012 14:52:14 -0000
@@ -340,9 +340,14 @@
      globals.lookup_bool_option(Globals, output_cc, OutputCC),
      globals.lookup_bool_option(Globals, output_c_compiler_type, OutputCCType),
      globals.lookup_bool_option(Globals, output_cflags, OutputCFlags),
-    globals.lookup_bool_option(Globals, output_csharp_compiler_type, OutputCSCType),
+    globals.lookup_bool_option(Globals, output_csharp_compiler_type,
+        OutputCSCType),
      globals.lookup_bool_option(Globals, output_library_link_flags,
          OutputLibraryLinkFlags),
+    globals.lookup_bool_option(Globals, output_grade_defines,
+        OutputGradeDefines),
+    globals.lookup_bool_option(Globals, output_c_include_directory_flags,
+        OutputCInclDirFlags),
      globals.lookup_bool_option(Globals, make, Make),
      globals.lookup_maybe_string_option(Globals,
          generate_standalone_interface, GenerateStandaloneInt),
@@ -401,6 +406,12 @@
      ; OutputLibraryLinkFlags = yes ->
          io.stdout_stream(StdOut, !IO),
          output_library_link_flags(Globals, StdOut, !IO)
+    ; OutputGradeDefines = yes ->
+        io.stdout_stream(StdOut, !IO),
+        output_grade_defines(Globals, StdOut, !IO)
+    ; OutputCInclDirFlags = yes ->
+        io.stdout_stream(StdOut, !IO),
+        output_c_include_directory_flags(Globals, StdOut, !IO)
      ; GenerateMapping = yes ->
          source_file_map.write_source_file_map(Globals, Args, !IO)
      ; GenerateStandaloneInt = yes(StandaloneIntBasename) ->
Index: compiler/options.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/options.m,v
retrieving revision 1.712
diff -u -r1.712 options.m
--- compiler/options.m	10 Jan 2012 05:27:36 -0000	1.712
+++ compiler/options.m	16 Jan 2012 16:32:47 -0000
@@ -210,6 +210,8 @@
      ;       output_csharp_compiler_type
      ;       output_cflags
      ;       output_library_link_flags
+    ;       output_grade_defines
+    ;       output_c_include_directory_flags

      % Auxiliary output options
      ;       smart_recompilation
@@ -1195,7 +1197,9 @@
      output_c_compiler_type              -   bool(no),
      output_csharp_compiler_type         -   bool(no),
      output_cflags                       -   bool(no),
-    output_library_link_flags           -   bool(no)
+    output_library_link_flags           -   bool(no),
+    output_grade_defines                -   bool(no),
+    output_c_include_directory_flags    -   bool(no)
  ]).
  option_defaults_2(aux_output_option, [
      % Auxiliary Output Options
@@ -2082,6 +2086,11 @@
  long_option("output-csharp-compiler-type", output_csharp_compiler_type).
  long_option("output-cflags",            output_cflags).
  long_option("output-library-link-flags",    output_library_link_flags).
+long_option("output-grade-defines",     output_grade_defines).
+long_option("output-c-include-directory-flags",
+    output_c_include_directory_flags).
+long_option("output-c-include-dir-flags",
+    output_c_include_directory_flags).

  % aux output options
  long_option("smart-recompilation",      smart_recompilation).
@@ -3808,9 +3817,18 @@
          "\tagainst the current set of libraries.  This includes the",
          "\tstandard library as well as any other libraries specified",
          "\tvia the --ml option.  The flags are printed to the standard",
-        "\toutput."
+        "\toutput.",
+        "--output-grade-defines",
+        "\tPrint the flags that are passed to the C compiler to define the",
+        "\tmacros used to specify the compilation grade.",
+        "\tThe flags are printed to the standard output.",
+        "--output-c-include-dir-flags, --output-c-include-directory-flags", 
+        "\tPrint the flags that are passed to the C compiler to specify",
+        "\twhich directories to search for C header files.",
+        "\tThis includes the C header files from the standard library.",
+        "\tThe flags are printed to the standard output."
      ]).
-
+
  :- pred options_help_aux_output(io::di, io::uo) is det.

  options_help_aux_output -->
Index: doc/user_guide.texi
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/doc/user_guide.texi,v
retrieving revision 1.647
diff -u -r1.647 user_guide.texi
--- doc/user_guide.texi	15 Jan 2012 23:38:21 -0000	1.647
+++ doc/user_guide.texi	16 Jan 2012 16:31:59 -0000
@@ -7003,6 +7003,23 @@
  library as well as any other libraries specified via the
  @samp{--ml} option.  The flags are printed to the standard output.

+ at sp 1
+ at item --output-grade-defines
+ at findex --output-grade-defines
+Print the flags that are passed to the C compiler to define the macros used to
+specify the compilation grade.
+The flags are printed to the standard outut.
+
+ at sp 1
+ at item --output-c-include-dir-flags
+ at item --output-c-include-directory-flags
+ at findex --output-c-include-dir-flags
+ at findex --output-c-include-directory-flags
+Print the flags that are passed to the C compiler to specify which directories
+to search for C header files.
+This includes the C header files from the standard library.
+The flags are printed to the standard output.
+
  @end table

  @node Auxiliary output options
Index: samples/c_interface/standalone_c/Makefile
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/samples/c_interface/standalone_c/Makefile,v
retrieving revision 1.4
diff -u -r1.4 Makefile
--- samples/c_interface/standalone_c/Makefile	3 Jun 2011 05:42:39 -0000	1.4
+++ samples/c_interface/standalone_c/Makefile	16 Jan 2012 17:43:35 -0000
@@ -65,9 +65,40 @@
  #
  CC = $(shell $(MMC) --output-cc)

-# Ask the Mercury compiler what flags it passes to the C compiler?
+# We need to tell the C compiler to define the macros used to specify the
+# compilation grade in which any Mercury libraries we are using were compiled.
+# We also need to tell the C compiler where to find the C header files
+# associated with those libraries.  The simplest way to find out both of these
+# is to ask the Mercury compiler what flags it passes to the C compiler.
+# This can be done as follows:
+#
+# CFLAGS = $(shell $(MMC) $(GRADEOPT) --output-cflags)
+#
+# Note that the output of the `--output-cflags' option also includes any other
+# flags that the Mercury compiler passes to the C compiler, for example options
+# that control optimisation settings. 
+# 
+# We use a finer grained approach here that only queries the Mercury compiler
+# about which macros to define for the current grade and what directories to
+# search for header files in.  This finer grained approach is useful if a
+# foreign application is written in, for example, C++ instead of C.  In that
+# case not all of the flags in output of --output-cflags may be valid for the
+# C++ compiler.
+
+# Ask the Mercury compiler what flags to pass to the C compiler in order to
+# define the macros used to specify the compilation grade we are using.
+#
+CFLAGS_FOR_GRADE = $(shell $(MMC) $(GRADEOPT) --output-grade-defines)
+
+# Ask the Mercury compiler what flags to pass to the C compiler in order to
+# tell it where to search for C header files that are part of any Mercury
+# libraries we are using (including the standard library).
  #
-CFLAGS=$(shell $(MMC) $(GRADEOPT) --output-cflags)
+CFLAGS_FOR_INCLUDES = $(shell $(MMC) $(GRADEOPT) --output-c-include-dir-flags)
+
+# Gather together all the flags to pass to the C compiler.
+#
+CFLAGS = $(CFLAGS_FOR_GRADE) $(CFLAGS_FOR_INCLUDES)

  # Ask the Mercury compiler what command it uses to invoke the linker when
  # creating an executable?

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