[m-rev.] for review: add foreign code support to mmake for the IL backend

Peter Ross peter.ross at miscrit.be
Tue Jul 17 23:46:46 AEST 2001


Hi,


===================================================================


Estimated hours taken: 5
Branches: main

Add mmake support for building modules which contain foreign code on the
IL backend.
Note that currently we don't install mercury_il.dll and mercury_mcpp.dll
somewhere where the C compiler can find them, so this change doesn't
work out of the box.

compiler/mlds_to_ilasm.m:
    Change the output file name for cpp files to <modulename>__cpp_code.cpp

compiler/mlds_to_mcpp.m:
    No longer output a reference to <modulename>.dll, as the cpp code
    doesn't refer to any of the entities defined in this dll.

compiler/modules.m:
    If a module contains some foreign code and we are on the IL backend
    generate a dependency which causes the foreign code dll to be built.
    Generate the make variable .foreign, .foreign_cs, .foreign_os and
    .foreign_dlls which refer to all the foreign modules.
    Generate rules to clean the foreign modules up.

scripts/Mmake.rules:
    Turn some unix paths into windows paths.
    
library/Mmakefile:
    Use the new .foreign variables to build the library instead of the
    previous hacks.

Index: compiler/mlds_to_ilasm.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mlds_to_ilasm.m,v
retrieving revision 1.14
diff -u -r1.14 mlds_to_ilasm.m
--- compiler/mlds_to_ilasm.m	8 May 2001 16:46:37 -0000	1.14
+++ compiler/mlds_to_ilasm.m	17 Jul 2001 13:41:05 -0000
@@ -79,7 +79,7 @@
 		pred(mlds, io__state, io__state)::out(pred(in, di, uo) is det))
 		is det.
 
-handle_foreign_lang(managed_cplusplus, "__c_code.cpp", output_mcpp_code).
+handle_foreign_lang(managed_cplusplus, "__cpp_code.cpp", output_mcpp_code).
 handle_foreign_lang(csharp, "__csharp_code.cs", output_csharp_code).
 handle_foreign_lang(c, _, _) :-
 	sorry(this_file, "language C foreign code not supported").
Index: compiler/mlds_to_mcpp.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mlds_to_mcpp.m,v
retrieving revision 1.11
diff -u -r1.11 mlds_to_mcpp.m
--- compiler/mlds_to_mcpp.m	12 Jul 2001 15:44:58 -0000	1.11
+++ compiler/mlds_to_mcpp.m	17 Jul 2001 13:41:06 -0000
@@ -102,7 +102,7 @@
 		"#include ""mercury_mcpp.h""\n",
 		"#using ""mercury_mcpp.dll""\n",
 		"#using ""mercury_il.dll""\n",
-		"#using """, ModuleNameStr, ".dll""\n",
+		"// #using """, ModuleNameStr, ".dll""\n",
 
 		% XXX We have to use the mercury namespace, as
 		% llds_out still generates some of the code used in the
Index: compiler/modules.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/modules.m,v
retrieving revision 1.172
diff -u -r1.172 modules.m
--- compiler/modules.m	17 Jul 2001 08:56:30 -0000	1.172
+++ compiler/modules.m	17 Jul 2001 13:41:10 -0000
@@ -1776,7 +1776,7 @@
 write_dependency_file(Module, AllDepsSet, MaybeTransOptDeps) -->
 	{ Module = module_imports(SourceFileName, ModuleName, ParentDeps,
 			IntDeps, ImplDeps, IndirectDeps, _InclDeps, FactDeps0,
-			_ContainsForeignCode, _Items, _Error, _Timestamps) },
+			ContainsForeignCode, Items, _Error, _Timestamps) },
 	globals__io_lookup_bool_option(verbose, Verbose),
 	{ module_name_to_make_var_name(ModuleName, MakeVarName) },
 	module_name_to_file_name(ModuleName, ".d", yes, DependencyFileName),
@@ -2030,9 +2030,46 @@
 				SourceFileName, "\n",
 			"\trm -rf ", DirFileName, "\n",
 			"\t$(MCS) $(ALL_GRADEFLAGS) $(ALL_MCSFLAGS) ",
-				SourceFileName, "\n"
+				SourceFileName, "\n\n"
 		]),
 
+		% If we are on the IL backend and the current module
+		% contains some foreign code, generate a dependency
+		% between the dll containing the mercury code and the
+		% dll containing the foreign code.
+		globals__io_get_target(Target),
+		(
+			{ Target = il },
+			{
+				ContainsForeignCode = contains_foreign_code
+			;
+				ContainsForeignCode = unknown,
+				item_list_contains_foreign_code(Items)
+			}
+		->
+			globals__io_lookup_foreign_language_option(
+					backend_foreign_language, ForeignLang),
+			{ ForeignExt = simple_foreign_language_string(
+					ForeignLang) },
+			{ ForeignCodeExt = "__" ++ ForeignExt ++ "_code." },
+			module_name_to_file_name(ModuleName,
+					ForeignCodeExt ++ ForeignExt,
+					no, ForeignFileName),
+			module_name_to_file_name(ModuleName, ".il", no,
+					IlFileName),
+			module_name_to_file_name(ModuleName, ".dll", no,
+					DllFileName),
+			module_name_to_file_name(ModuleName,
+					ForeignCodeExt ++ "dll",
+					no, ForeignDllFileName),
+			io__write_strings(DepStream, [
+				DllFileName, " : ", ForeignDllFileName, "\n",
+				ForeignDllFileName, " : ", ForeignFileName,"\n",
+				ForeignFileName, " : ", IlFileName, "\n\n"])
+		;
+			[]
+		),
+
 		module_name_to_file_name(ModuleName, ".int0", no,
 							Int0FileName),
 		module_name_to_file_name(ModuleName, ".int", no,
@@ -2862,20 +2899,56 @@
 	io__write_string(DepStream, MakeVarName),
 	io__write_string(DepStream, ".mods ="),
 	write_dependencies_list(Modules, "", DepStream),
+	io__write_string(DepStream, "\n"),
+
+	globals__io_get_target(Target),
+	globals__io_lookup_foreign_language_option(
+			backend_foreign_language, ForeignLang),
+	{ ForeignExt = "." ++ simple_foreign_language_string(ForeignLang) },
+	( { Target = il } ->
+		{ ForeignModules = foreign_modules(ForeignLang,
+				Modules, DepsMap) }
+	;
+		{ ForeignModules = [] }
+	),
+	io__write_string(DepStream, MakeVarName),
+	io__write_string(DepStream, ".foreign ="),
+	write_dependencies_list(ForeignModules, "", DepStream),
 	io__write_string(DepStream, "\n\n"),
 
 	globals__io_lookup_bool_option(assume_gmake, Gmake),
 	( { Gmake = yes } ->
 		{ string__append(MakeVarName, ".mods", ModsVarName) },
-		{ Basis = yes(ModsVarName - "") }
+		{ Basis = yes(ModsVarName - "") },
+
+		{ string__append(MakeVarName, ".foreign", ForeignVarName) },
+		{ ForeignBasis = yes(ForeignVarName - "") }
 	;
-		{ Basis = no }
+		{ Basis = no },
+		{ ForeignBasis = no }
 	),
 
-	globals__io_get_target(Target),
 	{ get_extra_link_objects(Modules, DepsMap, Target, ExtraLinkObjs) },
 
 	io__write_string(DepStream, MakeVarName),
+	io__write_string(DepStream, ".foreign_cs = "),
+	write_compact_dependencies_list(ForeignModules, "$(os_subdir)",
+					ForeignExt, ForeignBasis, DepStream),
+	io__write_string(DepStream, "\n"),
+
+	io__write_string(DepStream, MakeVarName),
+	io__write_string(DepStream, ".foreign_os = "),
+	write_compact_dependencies_list(ForeignModules, "$(os_subdir)", ".obj",
+					ForeignBasis, DepStream),
+	io__write_string(DepStream, "\n"),
+
+	io__write_string(DepStream, MakeVarName),
+	io__write_string(DepStream, ".foreign_dlls = "),
+	write_compact_dependencies_list(ForeignModules, "$(dlls_subdir)",
+					".dll", ForeignBasis, DepStream),
+	io__write_string(DepStream, "\n"),
+
+	io__write_string(DepStream, MakeVarName),
 	io__write_string(DepStream, ".init_cs = "),
 	write_compact_dependencies_list(Modules, "$(cs_subdir)", ".c",
 					Basis, DepStream),
@@ -3507,7 +3580,6 @@
 		CleanTargetName, " :\n",
 		"\t-rm -rf $(", MakeVarName, ".dirs)\n",
 		"\t-rm -f $(", MakeVarName, ".cs) ", InitCFileName, "\n",
-		"\t-rm -f $(", MakeVarName, ".dlls)\n",
 		"\t-rm -f $(", MakeVarName, ".all_ss) ", InitAsmFileName, "\n",
 		"\t-rm -f $(", MakeVarName, ".all_pic_ss) ",
 					InitAsmFileName, "\n",
@@ -3522,6 +3594,8 @@
 		"\t-rm -f $(", MakeVarName, ".ils)\n",
 		"\t-rm -f $(", MakeVarName, ".profs)\n",
 		"\t-rm -f $(", MakeVarName, ".errs)\n",
+		"\t-rm -f $(", MakeVarName, ".foreign_cs)\n",
+		"\t-rm -f $(", MakeVarName, ".foreign_os)\n",
 		"\t-rm -f $(", MakeVarName, ".schemas)\n"
 	]),
 
@@ -3547,6 +3621,8 @@
 		"\t-rm -f $(", MakeVarName, ".trans_opts)\n",
 		"\t-rm -f $(", MakeVarName, ".ds)\n",
 		"\t-rm -f $(", MakeVarName, ".all_hs)\n",
+		"\t-rm -f $(", MakeVarName, ".dlls)\n",
+		"\t-rm -f $(", MakeVarName, ".foreign_dlls)\n",
 		"\t-rm -f $(", MakeVarName, ".rlos)\n"
 	]),
 	io__write_strings(DepStream, [
@@ -3599,6 +3675,22 @@
 modules_that_need_headers(Modules, DepsMap) =
 	list__filter(module_needs_header(DepsMap), Modules).
 
+:- func foreign_modules(foreign_language, list(module_name), deps_map)  =
+		list(module_name).
+
+foreign_modules(ForeignLang, Modules, DepsMap) = ForeignModules :-
+	P = (pred(M::in, FM::out) is semidet :-
+		Ext = "__" ++ simple_foreign_language_string(ForeignLang) ++
+				"_code",
+		module_needs_header(DepsMap, M),
+		( M = unqualified(Name),
+			FM = unqualified(Name ++ Ext)
+		; M = qualified(Module, Name),
+			FM = qualified(Module, Name ++ Ext)
+		)
+	),
+	list__filter_map(P, Modules, ForeignModules).
+
 	% Succeed iff we need to generate a C header file for the specified
 	% module, assuming we're compiling with `--target asm'.
 :- pred module_needs_header(deps_map::in, module_name::in) is semidet.
@@ -3912,10 +4004,11 @@
 	get_fact_table_dependencies(Items, FactTableDeps),
 
 	% Figure out whether the items contain foreign code.
-	% As an optimization, we do this only if target = asm,
-	% since that is the only time we'll need that field.
+	% As an optimization, we do this only if target = asm or target = il
+	% since those are the only times we'll need that field.
+	globals__get_target(Globals, Target),
 	ContainsForeignCode =
-		(if globals__get_target(Globals, asm) then
+		(if (Target = asm ; Target = il) then
 			(if item_list_contains_foreign_code(Items) then
 				contains_foreign_code
 			else
Index: library/Mmakefile
===================================================================
RCS file: /home/mercury1/repository/mercury/library/Mmakefile,v
retrieving revision 1.70
diff -u -r1.70 Mmakefile
--- library/Mmakefile	27 Jun 2001 05:04:38 -0000	1.70
+++ library/Mmakefile	17 Jul 2001 13:41:12 -0000
@@ -211,44 +211,6 @@
 # runtime files.
 RUNTIME_DLLS=../runtime/mercury_mcpp.dll ../runtime/mercury_il.dll
 
-MODULES_CONTAINING_C_CODE=	\
-		array 		\
-		benchmarking	\
-		builtin		\
-		char		\
-		exception	\
-		float		\
-		gc		\
-		int		\
-		io		\
-		library		\
-		math		\
-		private_builtin	\
-		sparse_bitset	\
-		std_util	\
-		store		\
-		string		\
-		table_builtin	\
-		time
-
-CPP_DLLS=$(MODULES_CONTAINING_C_CODE:%=%__c_code.dll)
-
-ALL_DLLS=$(CPP_DLLS) $(library.dlls) $(RUNTIME_DLLS) 
-
-ALL_DLLS_BASE  = $(ALL_DLLS:%.dll=%)
-EMBED_ALL_DLLS = $(foreach dll_name,$(ALL_DLLS_BASE),$(EMBED_ONE_DLL))
-EMBED_ONE_DLL  = /embed:$(dll_name).dll,$(dll_name)
-
-# For the moment we generate the library using one big DLL, as there are bugs
-# in the assembly cache installer (gacutil) that prevent us from installing
-# large numbers of assemblies.  The HACK_* versions of the variables implement
-# the big file hack.
-HACK_ALL_DLLS=$(CPP_DLLS) mercury_all.dll $(RUNTIME_DLLS)
-
-HACK_ALL_DLLS_BASE  = $(HACK_ALL_DLLS:%.dll=%)
-HACK_EMBED_ALL_DLLS = $(foreach dll_name,$(HACK_ALL_DLLS_BASE),$(EMBED_ONE_DLL))
-
-
 # Turn this on if you wish to enable .NET debugging.
 #MS_ILASMFLAGS = /debug
 
@@ -265,15 +227,9 @@
 	rm -f $@
 	$(MS_ILASM) $(ALL_MS_ILASMFLAGS) /dll /quiet /OUT=$@ $(library.ils)
 
-mercury.dll: $(HACK_ALL_DLLS) mercury_all.dll library_strong_name.sn
+mercury.dll: mercury_all.dll $(library.foreign_dlls) library_strong_name.sn
 	cp $(RUNTIME_DLLS) .
-	$(MS_AL) -v:0.0.0.0 -keyf:library_strong_name.sn -out:mercury.dll $(HACK_ALL_DLLS)
-
-
-# al is the assembly linker, it will create an assembly that references
-# all the modules (.dll files) in the library and runtime.
-#mercury.dll: $(ALL_DLLS) library_strong_name.sn
-#	$(MS_AL) -v:0.0.0.0 -keyf:library_strong_name.sn -out:mercury.dll $(ALL_DLLS) $(EMBED_ALL_DLLS) 
+	$(MS_AL) -v:0.0.0.0 -keyf:library_strong_name.sn -out:mercury.dll mercury_all.dll $(library.foreign_dlls) $(RUNTIME_DLLS) 
 
 else
 
@@ -379,8 +335,8 @@
 ifneq (,$(findstring il,$(GRADE)))
 
 .PHONY: install_library
-install_library: $(library.dlls) $(CPP_DLLS) install_grade_dirs
-	cp `vpath_find $(library.dlls) $(CPP_DLLS) mercury.dll` \
+install_library: $(library.dlls) $(library.foreign_dlls) install_grade_dirs
+	cp `vpath_find $(library.dlls) $(library.foreign_dlls) mercury.dll` \
 		$(INSTALL_MERC_LIB_DIR)
 	
 else
Index: scripts/Mmake.rules
===================================================================
RCS file: /home/mercury1/repository/mercury/scripts/Mmake.rules,v
retrieving revision 1.100
diff -u -r1.100 Mmake.rules
--- scripts/Mmake.rules	8 Jul 2001 11:02:25 -0000	1.100
+++ scripts/Mmake.rules	17 Jul 2001 13:41:12 -0000
@@ -247,9 +247,9 @@
 # IL to generate a DLL if we are in a non-IL grade.
 ifeq ($(findstring il,$(GRADE)),il)        
 $(os_subdir)%.dll : %.cpp
-	$(MS_CL) -CLR$(MS_CL_NOASM) -I$(MERC_C_INCL_DIR) \
-		-I$(MERC_DLL_DIR) $(ALL_MS_CLFLAGS) $< -link -noentry \
-		mscoree.lib -dll $(MS_CL_LIBS) -out:$@
+	$(MS_CL) -CLR$(MS_CL_NOASM) -I`cygpath -w $(MERC_C_INCL_DIR)` \
+		-I`cygpath -w $(MERC_DLL_DIR)` $(ALL_MS_CLFLAGS) $< \
+		-link -noentry mscoree.lib -dll $(MS_CL_LIBS) -out:$@
 
 $(os_subdir)%.dll : %.il
 	$(MS_ILASM) $(ALL_MS_ILASMFLAGS) /dll /quiet /OUT=$@ $<

--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list