[m-rev.] for review: add --output-cflags option
Julien Fischer
juliensf at csse.unimelb.edu.au
Fri Jan 25 16:54:02 AEDT 2008
For review by anyone.
(This is still pending a bootcheck.)
Estimated hours taken: 1.5
Branches: main
Add a new option, `--output-cflags', that causes the compiler to print
out the flags it will pass to the C compiler. This addition is intended
to simplify the use of stand-alone interfaces and remove their current
dependence on the mgnuc script.
The idea is that users who have C files that make calls to exported
Mercury code should be able to add something like the following to
their Makefiles,
CC := $(shell mmc --output-cc)
CFLAGS := $(shell mmc --output-cflags)
CFLAGS will then contain the appropriate -I and -D options to process the
code in any Mercury headers #included in the C file.
XXX we also need something similar for linking. The documentation and
samples for stand-alone interfaces also need to be updated. I will do
both of those things as separate changes.
compiler/options.m:
Add the --output-cflags option.
compiler/mercury_compile.m:
Implement the option.
compiler/compile_target_code.m:
Factor out the code that determines what flags are passed to the
C compiler, based on grade settings and alike, into a separate
predicate, gather_c_compiler_flags/4.
Export a new predicate, output_c_compiler_flags/3, that is
called when --output-cflags is given. This predicate just
prints out the result of gather_c_compiler_flags/4 to the
specified output stream.
doc/user_guide.texi:
Document the new option.
Julien.
Index: compiler/compile_target_code.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/compile_target_code.m,v
retrieving revision 1.126
diff -u -r1.126 compile_target_code.m
--- compiler/compile_target_code.m 24 Jan 2008 04:10:30 -0000 1.126
+++ compiler/compile_target_code.m 25 Jan 2008 05:49:34 -0000
@@ -195,6 +195,11 @@
%
:- pred make_standalone_interface(string::in, io::di, io::uo) is det.
+ % Output the C compiler flags to the given stream.
+ % This predicate is used to implement the `--output-cflags' option.
+ %
+:- pred output_c_compiler_flags(io.output_stream::in, io::di, io::uo) is det.
+
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
@@ -359,6 +364,18 @@
maybe_write_string(Verbose, C_File, !IO),
maybe_write_string(Verbose, "':\n", !IO),
globals.io_lookup_string_option(cc, CC, !IO),
+ gather_c_compiler_flags(PIC, AllCFlags, !IO),
+ string.append_list([
+ CC, " ",
+ AllCFlags,
+ " -c ", C_File, " ",
+ NameObjectFile, O_File], Command),
+ invoke_system_command(ErrorStream, cmd_verbose_commands,
+ Command, Succeeded, !IO).
+
+:- pred gather_c_compiler_flags(pic::in, string::out, io::di, io::uo) is det.
+
+gather_c_compiler_flags(PIC, AllCFlags, !IO) :-
globals.io_lookup_accumulating_option(cflags, C_Flags_List, !IO),
join_string_list(C_Flags_List, "", "", " ", CFLAGS),
@@ -783,13 +800,12 @@
;
AppleGCCRegWorkaroundOpt = ""
),
-
+
% 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.
string.append_list([
- CC, " ",
SubDirInclOpt, InclOpt,
OptimizeOpt, " ",
HighLevelCodeOpt,
@@ -818,11 +834,7 @@
AppleGCCRegWorkaroundOpt,
C_FnAlignOpt,
WarningOpt, " ",
- CFLAGS,
- " -c ", C_File, " ",
- NameObjectFile, O_File], Command),
- invoke_system_command(ErrorStream, cmd_verbose_commands,
- Command, Succeeded, !IO).
+ CFLAGS], AllCFlags).
%-----------------------------------------------------------------------------%
@@ -2625,6 +2637,16 @@
).
%-----------------------------------------------------------------------------%
+%
+% C compiler flags
+%
+
+output_c_compiler_flags(Stream, !IO) :-
+ get_object_code_type(executable, PIC, !IO),
+ gather_c_compiler_flags(PIC, CFlags, !IO),
+ io.write_string(Stream, CFlags, !IO).
+
+%-----------------------------------------------------------------------------%
:- func this_file = string.
Index: compiler/mercury_compile.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/mercury_compile.m,v
retrieving revision 1.458
diff -u -r1.458 mercury_compile.m
--- compiler/mercury_compile.m 18 Dec 2007 06:52:32 -0000 1.458
+++ compiler/mercury_compile.m 25 Jan 2008 05:49:34 -0000
@@ -388,6 +388,7 @@
globals.lookup_bool_option(Globals, output_libgrades,
OutputLibGrades),
globals.lookup_bool_option(Globals, output_cc, OutputCC),
+ globals.lookup_bool_option(Globals, output_cflags, OutputCFlags),
globals.lookup_bool_option(Globals, make, Make),
globals.lookup_maybe_string_option(Globals, generate_standalone_interface,
GenerateStandaloneInt),
@@ -429,6 +430,9 @@
globals.lookup_string_option(Globals, cc, CC),
io.stdout_stream(StdOut, !IO),
io.write_string(StdOut, CC ++ "\n", !IO)
+ ; OutputCFlags = yes ->
+ io.stdout_stream(StdOut, !IO),
+ output_c_compiler_flags(StdOut, !IO)
; GenerateMapping = yes ->
source_file_map.write_source_file_map(Args, !IO)
; GenerateStandaloneInt = yes(StandaloneIntBasename) ->
Index: compiler/options.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/options.m,v
retrieving revision 1.611
diff -u -r1.611 options.m
--- compiler/options.m 23 Jan 2008 13:12:16 -0000 1.611
+++ compiler/options.m 25 Jan 2008 05:49:35 -0000
@@ -183,6 +183,7 @@
; output_shared_lib_link_command
; output_libgrades
; output_cc
+ ; output_cflags
% Auxiliary output options
; smart_recompilation
@@ -1028,7 +1029,9 @@
output_link_command - bool(no),
output_shared_lib_link_command - bool(no),
output_libgrades - bool(no),
- output_cc - bool(no)
+ output_cc - bool(no),
+ output_cflags - bool(no)
+
]).
option_defaults_2(aux_output_option, [
@@ -1823,6 +1826,7 @@
long_option("output-shared-lib-link-command", output_shared_lib_link_command).
long_option("output-libgrades", output_libgrades).
long_option("output-cc", output_cc).
+long_option("output-cflags", output_cflags).
% aux output options
long_option("smart-recompilation", smart_recompilation).
@@ -3348,7 +3352,10 @@
"\tPrint the list of compilation grades in which a library",
"\tto be installed should be built to the standard output.",
"--output-cc",
- "\tPrint the name of the C compiler to the standard output."
+ "\tPrint the name of the C compiler to the standard output.",
+ "--output-cflags",
+ "\tPrint the flags with which the C compiler will be invoked.",
+ "\tto the standard output."
]).
:- pred options_help_aux_output(io::di, io::uo) is det.
Index: doc/user_guide.texi
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/doc/user_guide.texi,v
retrieving revision 1.557
diff -u -r1.557 user_guide.texi
--- doc/user_guide.texi 24 Jan 2008 04:31:48 -0000 1.557
+++ doc/user_guide.texi 25 Jan 2008 05:49:35 -0000
@@ -6633,6 +6633,12 @@
@findex --output-cc
Print the name of the C compiler to the standard output.
+ at sp 1
+ at item --output-cflags
+ at findex --output-cflags
+Print the flags with which the C compiler will be invoked
+to the standard output.
+
@end table
@node Auxiliary output options
--------------------------------------------------------------------------
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