Complete the support for building libraries with `mmc --make'.

Estimated hours taken: 2.5
Branches: main

Complete the support for building libraries with `mmc --make'.

compiler/make.m:
compiler/make.module_target.m:
compiler/mercury_compile.m:
compiler/compile_target_code.m:
	Generate the `.pic_o' files needed to build shared libraries.

scripts/mmc.in:
	Add `--pic-object-extension', `--create-archive-command'
	`--create-archive-command-output-flag',
	`--create-archive-commandflags' and `--ranlib-command'
	options to DEFAULT_MCFLAGS.

	Allow DEFAULT_MCFLAGS to be overridden in the environment,
	for consistency with the other *FLAGS variables.
This commit is contained in:
Simon Taylor
2002-05-06 11:43:56 +00:00
parent 3f5b8a33f8
commit 78495d8b99
5 changed files with 102 additions and 67 deletions

View File

@@ -18,19 +18,30 @@
:- import_module bool, list, io, std_util.
% compile_c_file(ErrorStream, CFile, ObjFile, Succeeded).
:- pred compile_c_file(io__output_stream, string, string, bool,
% Are we generating position indepedent code (for use in a
% shared library)? On some architectures, pic and non-pic
% code is incompatible, so we need to generate `.o' and `.pic_o'
% files.
:- type pic
---> pic
; non_pic
.
% compile_c_file(ErrorStream, PIC, CFile, ObjFile, Succeeded).
:- pred compile_c_file(io__output_stream, pic, string, string, bool,
io__state, io__state).
:- mode compile_c_file(in, in, in, in, out, di, uo) is det.
% compile_c_file(ErrorStream, PIC, ModuleName, Succeeded).
:- pred compile_c_file(io__output_stream, pic, module_name, bool,
io__state, io__state).
:- mode compile_c_file(in, in, in, out, di, uo) is det.
% compile_c_file(ErrorStream, ModuleName, Succeeded).
:- pred compile_c_file(io__output_stream, module_name, bool,
io__state, io__state).
:- mode compile_c_file(in, in, out, di, uo) is det.
% assemble(ErrorStream, ModuleName, Succeeded).
:- pred assemble(io__output_stream, module_name, bool, io__state, io__state).
:- mode assemble(in, in, out, di, uo) is det.
% assemble(ErrorStream, PIC, ModuleName, Succeeded).
:- pred assemble(io__output_stream, pic, module_name,
bool, io__state, io__state).
:- mode assemble(in, in, in, out, di, uo) is det.
% compile_java_file(ErrorStream, ModuleName, Succeeded).
:- pred compile_java_file(io__output_stream, module_name, bool,
@@ -249,7 +260,7 @@ split_c_to_obj(ErrorStream, ModuleName,
".c", C_File),
module_name_to_split_c_file_name(ModuleName, Chunk,
Obj, O_File),
compile_c_file(ErrorStream,
compile_c_file(ErrorStream, non_pic,
C_File, O_File, Succeeded0),
( { Succeeded0 = no } ->
{ Succeeded = no }
@@ -265,13 +276,20 @@ split_c_to_obj(ErrorStream, ModuleName,
:- type compiler_type ---> gcc ; lcc ; unknown.
compile_c_file(ErrorStream, ModuleName, Succeeded) -->
compile_c_file(ErrorStream, PIC, ModuleName, Succeeded) -->
module_name_to_file_name(ModuleName, ".c", yes, C_File),
globals__io_lookup_string_option(object_file_extension, ObjExt),
(
{ PIC = pic },
globals__io_lookup_string_option(pic_object_file_extension,
ObjExt)
;
{ PIC = non_pic },
globals__io_lookup_string_option(object_file_extension, ObjExt)
),
module_name_to_file_name(ModuleName, ObjExt, yes, O_File),
compile_c_file(ErrorStream, C_File, O_File, Succeeded).
compile_c_file(ErrorStream, PIC, C_File, O_File, Succeeded).
compile_c_file(ErrorStream, C_File, O_File, Succeeded) -->
compile_c_file(ErrorStream, PIC, C_File, O_File, Succeeded) -->
globals__io_lookup_bool_option(verbose, Verbose),
globals__io_lookup_string_option(c_flag_to_name_object_file,
NameObjectFile),
@@ -282,6 +300,15 @@ compile_c_file(ErrorStream, C_File, O_File, Succeeded) -->
globals__io_lookup_accumulating_option(cflags, C_Flags_List),
{ join_string_list(C_Flags_List, "", "", " ", CFLAGS) },
(
{ PIC = pic },
globals__io_lookup_string_option(cflags_for_pic,
CFLAGS_FOR_PIC)
;
{ PIC = non_pic },
{ CFLAGS_FOR_PIC = "" }
),
globals__io_lookup_bool_option(use_subdirs, UseSubdirs),
globals__io_lookup_bool_option(split_c_files, SplitCFiles),
{ (UseSubdirs = yes ; SplitCFiles = yes) ->
@@ -505,7 +532,7 @@ compile_c_file(ErrorStream, C_File, O_File, Succeeded) -->
HighLevelCodeOpt, NestedFunctionsOpt, HighLevelDataOpt,
RegOpt, GotoOpt, AsmOpt,
CFLAGS_FOR_REGS, " ", CFLAGS_FOR_GOTOS, " ",
CFLAGS_FOR_THREADS, " ",
CFLAGS_FOR_THREADS, " ", CFLAGS_FOR_PIC, " ",
GC_Opt, ProfileCallsOpt, ProfileTimeOpt, ProfileMemoryOpt,
ProfileDeepOpt, PIC_Reg_Opt, TagsOpt, NumTagBitsOpt,
Target_DebugOpt, LL_DebugOpt,
@@ -555,9 +582,15 @@ compile_java_file(ErrorStream, ModuleName, Succeeded) -->
%-----------------------------------------------------------------------------%
assemble(ErrorStream, ModuleName, Succeeded) -->
assemble(ErrorStream, PIC, ModuleName, Succeeded) -->
globals__io_lookup_bool_option(pic, Pic),
{ AsmExt = (Pic = yes -> ".pic_s" ; ".s") },
{ ( Pic = yes ; PIC = pic ) ->
AsmExt = ".pic_s",
GCCFLAGS_FOR_PIC = ""
;
AsmExt = ".s",
GCCFLAGS_FOR_PIC = "-fpic"
},
module_name_to_file_name(ModuleName, AsmExt, no, AsmFile),
globals__io_lookup_string_option(object_file_extension, Obj),
module_name_to_file_name(ModuleName, Obj, yes, ObjFile),
@@ -575,7 +608,7 @@ assemble(ErrorStream, ModuleName, Succeeded) -->
{ join_string_list(C_Flags_List, "", "", " ", CFLAGS) },
% Be careful with the order here.
% Also be careful that each option is separated by spaces.
{ string__append_list([CC, " ", CFLAGS,
{ string__append_list([CC, " ", CFLAGS, " ", GCCFLAGS_FOR_PIC,
" -c ", AsmFile, " ", NameObjectFile, ObjFile], Command) },
invoke_system_command(ErrorStream, verbose_commands,
Command, Succeeded).
@@ -741,7 +774,7 @@ make_init_obj_file(ErrorStream, MustCompile, ModuleName,
maybe_write_string(Verbose,
"% Compiling initialization file...\n"),
compile_c_file(ErrorStream, InitCFileName,
compile_c_file(ErrorStream, non_pic, InitCFileName,
InitObjFileName, CompileOK),
maybe_report_stats(Stats),
( { CompileOK = no } ->
@@ -844,12 +877,11 @@ create_archive(ErrorStream, LibFileName, ObjectList, MakeLibCmdOK) -->
globals__io_lookup_string_option(
create_archive_command_output_flag, ArOutputFlag),
globals__io_lookup_string_option(ranlib_command, RanLib),
{ list__append(
[ArCmd, " ", ArFlags, " ", ArOutputFlag, " ",
LibFileName, " " | ObjectList],
[" && ", RanLib, " ", LibFileName],
MakeLibCmdList) },
{ string__append_list(MakeLibCmdList, MakeLibCmd) },
{ join_string_list(ObjectList, "", "", " ", Objects) },
{ MakeLibCmd = string__append_list([
ArCmd, " ", ArFlags, " ", ArOutputFlag, " ",
LibFileName, " ", Objects,
" && ", RanLib, " ", LibFileName]) },
invoke_system_command(ErrorStream, verbose_commands,
MakeLibCmd, MakeLibCmdOK).

View File

@@ -115,7 +115,10 @@
:- type compilation_task_type
---> process_module(module_compilation_task_type)
; target_code_to_object_code
% The `pic' argument is only used for
% `--target c' and `--target asm'.
; target_code_to_object_code(pic)
.
:- type module_compilation_task_type
@@ -146,15 +149,6 @@
; object_code(pic)
.
% Are we generating position indepedent code (for use in a
% shared library)? On some architectures, pic and non-pic
% code is incompatible, so we need to generate `.o' and `.pic_o'
% files.
:- type pic
---> pic
; non_pic
.
% :- type linked_target_type in mercury_compile.m.
:- type misc_target_type

View File

@@ -261,7 +261,7 @@ build_target_2(ModuleName, process_module(ModuleTask), _Imports,
[]
).
build_target_2(ModuleName, target_code_to_object_code,
build_target_2(ModuleName, target_code_to_object_code(PIC),
Imports, _, ErrorStream, Succeeded, Info0, Info) -->
get_target_code_to_object_code_foreign_files(ModuleName,
ForeignCodeFiles, Info0, Info),
@@ -269,9 +269,9 @@ build_target_2(ModuleName, target_code_to_object_code,
{ CompileTargetCode =
(pred(Succeeded1::out, di, uo) is det -->
build_object_code(ModuleName, CompilationTarget,
build_object_code(ModuleName, CompilationTarget, PIC,
ErrorStream, Imports, Succeeded0),
list__map_foldl(compile_foreign_code_file(ErrorStream),
list__map_foldl(compile_foreign_code_file(ErrorStream, PIC),
ForeignCodeFiles, ForeignCodeSucceeded),
{
Succeeded0 = yes,
@@ -289,37 +289,40 @@ build_target_2(ModuleName, target_code_to_object_code,
call_in_forked_process(CompileTargetCode,
CompileTargetCode, Succeeded).
:- pred build_object_code(module_name::in, compilation_target::in,
:- pred build_object_code(module_name::in, compilation_target::in, pic::in,
io__output_stream::in, module_imports::in, bool::out,
io__state::di, io__state::uo) is det.
build_object_code(ModuleName, c, ErrorStream, _Imports, Succeeded) -->
compile_target_code__compile_c_file(ErrorStream, ModuleName, Succeeded).
build_object_code(ModuleName, asm, ErrorStream, _Imports, Succeeded) -->
compile_target_code__assemble(ErrorStream, ModuleName, Succeeded).
build_object_code(ModuleName, java, ErrorStream, _Imports, Succeeded) -->
compile_target_code__compile_java_file(ErrorStream, ModuleName, Succeeded).
build_object_code(ModuleName, il, ErrorStream, Imports, Succeeded) -->
build_object_code(ModuleName, c, PIC, ErrorStream, _Imports, Succeeded) -->
compile_target_code__compile_c_file(ErrorStream, PIC, ModuleName,
Succeeded).
build_object_code(ModuleName, asm, PIC, ErrorStream, _Imports, Succeeded) -->
compile_target_code__assemble(ErrorStream, PIC, ModuleName,
Succeeded).
build_object_code(ModuleName, java, _, ErrorStream, _Imports, Succeeded) -->
compile_target_code__compile_java_file(ErrorStream,
ModuleName, Succeeded).
build_object_code(ModuleName, il, _, ErrorStream, Imports, Succeeded) -->
compile_target_code__il_assemble(ErrorStream, ModuleName,
Imports ^ has_main, Succeeded).
:- pred compile_foreign_code_file(io__output_stream::in, foreign_code_file::in,
bool::out, io__state::di, io__state::uo) is det.
:- pred compile_foreign_code_file(io__output_stream::in, pic::in,
foreign_code_file::in, bool::out, io__state::di, io__state::uo) is det.
compile_foreign_code_file(ErrorStream, foreign_code_file(c, CFile, ObjFile),
Succeeded) -->
compile_target_code__compile_c_file(ErrorStream,
compile_foreign_code_file(ErrorStream, PIC,
foreign_code_file(c, CFile, ObjFile), Succeeded) -->
compile_target_code__compile_c_file(ErrorStream, PIC,
CFile, ObjFile, Succeeded).
compile_foreign_code_file(ErrorStream, foreign_code_file(il, ILFile, DLLFile),
Succeeded) -->
compile_foreign_code_file(ErrorStream, _,
foreign_code_file(il, ILFile, DLLFile), Succeeded) -->
compile_target_code__il_assemble(ErrorStream, ILFile, DLLFile,
no_main, Succeeded).
compile_foreign_code_file(ErrorStream,
compile_foreign_code_file(ErrorStream, _,
foreign_code_file(managed_cplusplus, MCPPFile, DLLFile),
Succeeded) -->
compile_target_code__compile_managed_cplusplus_file(ErrorStream,
MCPPFile, DLLFile, Succeeded).
compile_foreign_code_file(ErrorStream,
compile_foreign_code_file(ErrorStream, _,
foreign_code_file(csharp, CSharpFile, DLLFile),
Succeeded) -->
compile_target_code__compile_csharp_file(ErrorStream,
@@ -419,14 +422,14 @@ compilation_task(_, c_code) = process_module(compile_to_target_code) -
["--compile-to-c"].
compilation_task(_, il_code) = process_module(compile_to_target_code) -
["--il-only"].
compilation_task(_, il_asm) = target_code_to_object_code - [].
compilation_task(_, il_asm) = target_code_to_object_code(non_pic) - [].
compilation_task(_, java_code) = process_module(compile_to_target_code) -
["--java-only"].
compilation_task(_, asm_code(PIC)) =
process_module(compile_to_target_code) -
( PIC = pic -> ["--pic"] ; [] ).
compilation_task(Globals, object_code(PIC)) =
target_code_to_object_code - Flags :-
target_code_to_object_code(PIC) - Flags :-
globals__get_target(Globals, Target),
( PIC = pic ->
Flags = ( Target = asm -> ["--pic"] ; ["--pic-reg"] )
@@ -575,7 +578,7 @@ touched_files(TargetFile, process_module(Task), TouchedTargetFiles,
{ TouchedFileNames = list__condense([ForeignCodeFiles,
TimestampFileNames]) }.
touched_files(TargetFile, target_code_to_object_code,
touched_files(TargetFile, target_code_to_object_code(_),
[TargetFile], ForeignObjectFiles, Info0, Info) -->
{ TargetFile = ModuleName - _ },
get_target_code_to_object_code_foreign_files(ModuleName,

View File

@@ -383,7 +383,7 @@ compile_using_gcc_backend(FirstFileOrModule, CallBack, ModulesToLink) -->
( { Result = ok, TargetCodeOnly = no } ->
io__output_stream(OutputStream),
compile_target_code__assemble(OutputStream,
ModuleName, _AssembleOK)
non_pic, ModuleName, _AssembleOK)
;
[]
)
@@ -1176,8 +1176,9 @@ mercury_compile(Module, NestedSubModules, FindTimestampFiles) -->
yes, CCode_O_File),
io__output_stream(OutputStream),
compile_target_code__compile_c_file(
OutputStream, CCode_C_File,
CCode_O_File, _CompileOK),
OutputStream, non_pic,
CCode_C_File, CCode_O_File,
_CompileOK),
% add this object file to the list
% of extra object files to link in
globals__io_lookup_accumulating_option(
@@ -1204,7 +1205,7 @@ mercury_compile(Module, NestedSubModules, FindTimestampFiles) -->
O_File),
io__output_stream(OutputStream),
compile_target_code__compile_c_file(
OutputStream, C_File, O_File,
OutputStream, non_pic, C_File, O_File,
_CompileOK)
)
;
@@ -3376,7 +3377,7 @@ mercury_compile__c_to_obj(ErrorStream, ModuleName, NumChunks, Succeeded) -->
globals__io_lookup_string_option(object_file_extension, Obj),
module_name_to_file_name(ModuleName, ".c", no, C_File),
module_name_to_file_name(ModuleName, Obj, yes, O_File),
compile_target_code__compile_c_file(ErrorStream,
compile_target_code__compile_c_file(ErrorStream, non_pic,
C_File, O_File, Succeeded)
).

View File

@@ -33,7 +33,7 @@ esac
#
# The default optimization level should be after
# all the options that describe the machine configuration.
DEFAULT_MCFLAGS="\
DEFAULT_MCFLAGS=${DEFAULT_MCFLAGS="\
$MERCURY_ALL_MC_C_INCL_DIRS \
--cc \"${MERCURY_C_COMPILER=@CC@}\" \
--grade \"${MERCURY_DEFAULT_GRADE=@DEFAULT_GRADE@}\" \
@@ -43,6 +43,11 @@ DEFAULT_MCFLAGS="\
--cflags-for-pic \"@CFLAGS_FOR_PIC@\" \
--c-flag-to-name-object-file \"@OBJFILE_OPT@\" \
--object-file-extension \".@OBJ_SUFFIX@\" \
--pic-object-file-extension \".@EXT_FOR_PIC_OBJECTS@\" \
--create-archive-command \"@AR@\" \
--create-archive-command-output-flag \"@AR_LIBFILE_OPT@\" \
--create-archive-command-flags \"@ARFLAGS@\" \
--ranlib-command \"@RANLIB@\" \
--fullarch \"@FULLARCH@\" \
--num-real-r-regs \"@NUM_REAL_R_REGS@\" \
--num-real-r-temps \"@NUM_REAL_R_TEMPS@\" \
@@ -53,8 +58,8 @@ DEFAULT_MCFLAGS="\
@HAVE_DELAY_SLOT@ \
@HAVE_BOXED_FLOATS@ \
${MERCURY_DEFAULT_OPT_LEVEL=-O2} \
$STDLIB_DIR_OPT
"
$STDLIB_DIR_OPT \
"}
export DEFAULT_MCFLAGS
case $# in