[m-rev.] for review: generate strongly named assemblies

Peter Ross peter.ross at miscrit.be
Fri Aug 10 22:34:23 AEST 2001


Hi,


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


Estimated hours taken: 8
Branches: main

Optionally generate strongly named assemblies on the IL backend.

We use the fact that you are allowed use the same key pair to sign more
then one assembly.  The key pair we use is the one used to sign the
mercury std library.  This allows us to place the correct public key
token in the `.assembly extern' reference.

README.DotNet:
    Document how to generate a strongly named assembly.

compiler/mlds_to_csharp.m:
compiler/mlds_to_mcpp.m:
    If --sign-assembly is enabled output a custom attribute which
    references the strong name key file `mercury.sn'.

compiler/mlds_to_il.m:
    If --sign-assembly is enabled add the correct decls to all the
    `.assembly extern' declarations.
    
compiler/modules.m:
    If --sign-assembly we need to generate a dependency between the IL
    file and the strong name key file `mercury.sn', also add the
    variable ILASM_KEYFLAG-<module> so that we add the option to sign
    the assembly with the key file `mercury.sn'.
    
compiler/options.m:
    Add --sign-assembly.

library/Mmakefile:
    Install the library strong name file as `mercury.sn' in the dll
    install directory.

scripts/Mmake.rules:
    Add a rule to copy mercury.sn into the local subdirectory.  This is
    needed so that cpp and csharp modules can reference this file.
    
scripts/Mmake.vars.in:
    Add the ILASM_KEYFLAG variables.


Index: README.DotNet
===================================================================
RCS file: /home/mercury1/repository/mercury/README.DotNet,v
retrieving revision 1.6
diff -u -r1.6 README.DotNet
--- README.DotNet	9 Aug 2001 16:30:24 -0000	1.6
+++ README.DotNet	10 Aug 2001 12:14:43 -0000
@@ -138,6 +138,11 @@
 For example, module.m will be placed into the assembly `module', while
 module.sub.m will also be placed into the assembly `module'.
 
+To create a strongly named assemblies in Mercury you need to pass the
+--sign-assembly flag to the Mercury compiler.  Note that this flag needs
+to be also passed when generating the dependencies for the module being
+compiled.
+
 -----------------------------------------------------------------------------
 
 RESOURCES
Index: compiler/mlds_to_csharp.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mlds_to_csharp.m,v
retrieving revision 1.13
diff -u -r1.13 mlds_to_csharp.m
--- compiler/mlds_to_csharp.m	2 Aug 2001 19:50:36 -0000	1.13
+++ compiler/mlds_to_csharp.m	10 Aug 2001 12:14:43 -0000
@@ -106,6 +106,13 @@
 	generate_foreign_header_code(mercury_module_name_to_mlds(ModuleName),
 		ForeignCode),
 
+	globals__io_lookup_bool_option(sign_assembly, SignAssembly),
+	( { SignAssembly = yes },
+		io__write_string("[assembly:System.Reflection.AssemblyKeyFileAttribute(\"mercury.sn\")]\n")
+	; { SignAssembly = no },
+		[]
+	),
+
 	{ Namespace0 = get_class_namespace(ClassName) },
 	{ list__reverse(Namespace0) = [Head | Tail] ->
 		Namespace = list__reverse([Head ++ "__csharp_code" | Tail])
Index: compiler/mlds_to_il.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mlds_to_il.m,v
retrieving revision 1.63
diff -u -r1.63 mlds_to_il.m
--- compiler/mlds_to_il.m	9 Aug 2001 16:30:25 -0000	1.63
+++ compiler/mlds_to_il.m	10 Aug 2001 12:14:44 -0000
@@ -203,9 +203,12 @@
 			".", AssemblyName),
 	get_il_data_rep(ILDataRep, IO0, IO1),
 	globals__io_lookup_bool_option(debug_il_asm, DebugIlAsm, IO1, IO2),
-	globals__io_lookup_bool_option(verifiable_code, VerifiableCode, IO2, IO3),
+	globals__io_lookup_bool_option(verifiable_code,
+			VerifiableCode, IO2, IO3),
 	globals__io_lookup_bool_option(il_byref_tailcalls, ByRefTailCalls,
-			IO3, IO),
+			IO3, IO4),
+	globals__io_lookup_bool_option(sign_assembly, SignAssembly,
+			IO4, IO),
 
 	IlInfo0 = il_info_init(ModuleName, AssemblyName, Imports,
 			ILDataRep, DebugIlAsm, VerifiableCode, ByRefTailCalls),
@@ -249,7 +252,8 @@
 			ForeignCodeAssemblerRefs),
 		AssemblerRefs = list__append(ForeignCodeAssemblerRefs, Imports)
 	),
-	generate_extern_assembly(AssemblyName, AssemblerRefs, ExternAssemblies),
+	generate_extern_assembly(AssemblyName, SignAssembly,
+			AssemblerRefs, ExternAssemblies),
 	Namespace = [namespace(NamespaceName, ILDecls)],
 	ILAsm = list__condense([ThisAssembly, ExternAssemblies, Namespace]).
 
@@ -3461,15 +3465,21 @@
 %-----------------------------------------------------------------------------
 
 	% Generate extern decls for any assembly we reference.
-:- pred mlds_to_il__generate_extern_assembly(string, mlds__imports, list(decl)).
-:- mode mlds_to_il__generate_extern_assembly(in, in, out) is det.
+:- pred mlds_to_il__generate_extern_assembly(string::in, bool::in,
+		mlds__imports::in, list(decl)::out) is det.
 
-mlds_to_il__generate_extern_assembly(CurrentAssembly, Imports, AllDecls) :-
+mlds_to_il__generate_extern_assembly(CurrentAssembly, SignAssembly,
+		Imports, AllDecls) :-
+	( SignAssembly = yes,
+		AsmDecls = mercury_strong_name_assembly_decls
+	; SignAssembly = no,
+		AsmDecls = []
+	),
 	Gen = (pred(Import::in, Decl::out) is semidet :-
 		AsmName = mlds_module_name_to_assembly_name(Import),
 		( AsmName = assembly(Assembly),
 			Assembly \= "mercury",
-			Decl = [extern_assembly(Assembly, [])]
+			Decl = [extern_assembly(Assembly, AsmDecls)]
 		; AsmName = module(ModuleName, Assembly),
 			( Assembly = CurrentAssembly ->
 				ModuleStr = ModuleName ++ ".dll",
@@ -3477,7 +3487,7 @@
 					extern_module(ModuleStr)]
 			;
 				Assembly \= "mercury",
-				Decl = [extern_assembly(Assembly, [])]
+				Decl = [extern_assembly(Assembly, AsmDecls)]
 			)
 		)
 	),
@@ -3505,6 +3515,17 @@
 				int8(0xe0), int8(0x3b), int8(0xe0), int8(0x95)
 			])
 		]) | Decls].
+
+:- func mercury_strong_name_assembly_decls = list(assembly_decl).
+
+mercury_strong_name_assembly_decls
+	= [
+		version(0, 0, 0, 0),
+		public_key_token([
+			int8(0x22), int8(0x8C), int8(0x16), int8(0x7D),
+			int8(0x12), int8(0xAA), int8(0x0B), int8(0x0B)
+		])
+	].
 
 %-----------------------------------------------------------------------------
 
Index: compiler/mlds_to_mcpp.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mlds_to_mcpp.m,v
retrieving revision 1.16
diff -u -r1.16 mlds_to_mcpp.m
--- compiler/mlds_to_mcpp.m	2 Aug 2001 19:50:37 -0000	1.16
+++ compiler/mlds_to_mcpp.m	10 Aug 2001 12:14:45 -0000
@@ -116,6 +116,13 @@
 		"extern ""C"" int _fltused=0;\n",
 		"\n"]),
 
+	globals__io_lookup_bool_option(sign_assembly, SignAssembly),
+	( { SignAssembly = yes },
+		io__write_string("[assembly:System::Reflection::AssemblyKeyFileAttribute(\"mercury.sn\")];\n")
+	; { SignAssembly = no },
+		[]
+	),
+
 	{ Namespace0 = get_class_namespace(ClassName) },
 	{ list__reverse(Namespace0) = [Head | Tail] ->
 		Namespace = list__reverse([Head ++ "__cpp_code" | Tail])
Index: compiler/modules.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/modules.m,v
retrieving revision 1.190
diff -u -r1.190 modules.m
--- compiler/modules.m	9 Aug 2001 16:30:26 -0000	1.190
+++ compiler/modules.m	10 Aug 2001 12:14:47 -0000
@@ -2050,6 +2050,7 @@
 		]),
 
 		globals__io_get_target(Target),
+		globals__io_lookup_bool_option(sign_assembly, SignAssembly),
 		globals__io_get_globals(Globals),
 
 			
@@ -2069,21 +2070,35 @@
 			[]
 		),
 		
+		{ ContainsForeignCode = contains_foreign_code(LangSet)
+		; ContainsForeignCode = unknown,
+			get_item_list_foreign_code(Globals, Items, LangSet)
+		; ContainsForeignCode = no_foreign_code,
+			set__init(LangSet)
+		},
 		(
 			{ Target = il },
-			{
-				ContainsForeignCode = 
-					contains_foreign_code(LangSet)
-			;
-				ContainsForeignCode = unknown,
-				get_item_list_foreign_code(Globals, Items,
-					LangSet),
-				not set__empty(LangSet)
-			}
+			{ not set__empty(LangSet) }
 		->
 			{ Langs = set__to_sorted_list(LangSet) },
 			list__foldl(write_foreign_dependency_for_il(DepStream,
 				ModuleName, AllDeps), Langs)
+		;
+			[]
+		),
+
+			% If we are signing the assembly, then we will
+			% need the strong key to sign the il file with
+			% so add a dependency that the il file requires
+			% the strong name file `mercury.sn'.
+			% Also add the variable ILASM_KEYFLAG-<module> which
+			% is used to build the command line for ilasm.
+		( { Target = il, SignAssembly = yes } ->
+			{ prog_out__sym_name_to_string(ModuleName,
+					ModuleNameString) },
+			module_name_to_file_name(ModuleName, ".il",
+					no, IlFileName),
+			
+			io__write_strings(DepStream, [
+				"ILASM_KEYFLAG-", ModuleNameString,
+						" = /keyf=mercury.sn\n",
+				IlFileName, " : mercury.sn\n"])
 		;
 			[]
 		),
Index: compiler/options.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/options.m,v
retrieving revision 1.332
diff -u -r1.332 options.m
--- compiler/options.m	2 Aug 2001 19:50:37 -0000	1.332
+++ compiler/options.m	10 Aug 2001 12:14:49 -0000
@@ -121,6 +121,7 @@
 		;	generate_schemas
 		;	dump_rl
 		;	dump_rl_bytecode
+		;	sign_assembly
 	% Language semantics options
 		;	reorder_conj
 		;	reorder_disj
@@ -575,6 +576,7 @@
 	dump_mlds		-	accumulating([]),
 	dump_rl			-	bool(no),
 	dump_rl_bytecode	-	bool(no),
+	sign_assembly		-	bool(no),
 	generate_schemas	-	bool(no)
 ]).
 option_defaults_2(language_semantics_option, [
@@ -1016,6 +1018,7 @@
 long_option("dump-mlds",		dump_mlds).
 long_option("dump-rl",			dump_rl).
 long_option("dump-rl-bytecode",		dump_rl_bytecode).
+long_option("sign-assembly",		sign_assembly).
 long_option("generate-schemas",		generate_schemas).
 
 % language semantics options
@@ -1938,7 +1941,12 @@
 		"\tto `<module>.base_schema' and for Aditi derived",
 		"\trelations to `<module>.derived_schema'.",
 		"\tA schema string is a representation of the types",
-		"\tof a relation."
+		"\tof a relation.",
+
+		"--sign-assembly",
+		"\tSign the current assembly with the mercury strong name.",
+		"\tTo use assemblies created with this command all the mercury",
+		"\tmodules must be compiled with this option enabled."
 	]).
 
 :- pred options_help_semantics(io__state::di, io__state::uo) is det.
Index: library/Mmakefile
===================================================================
RCS file: /home/mercury1/repository/mercury/library/Mmakefile,v
retrieving revision 1.76
diff -u -r1.76 Mmakefile
--- library/Mmakefile	3 Aug 2001 12:30:50 -0000	1.76
+++ library/Mmakefile	10 Aug 2001 12:14:52 -0000
@@ -337,7 +337,7 @@
 ifneq (,$(findstring il,$(GRADE)))
 
 .PHONY: install_library
-install_library: mercury.dll install_grade_dirs install_gac
+install_library: mercury.dll install_grade_dirs install_gac install_strong_name
 	cp `vpath_find $(library.foreign_dlls) $(library.dlls) \
 		mercury.dll` $(INSTALL_MERC_LIB_DIR)
 
@@ -347,6 +347,11 @@
 .PHONY: install_gac
 install_gac: mercury.dll
 	gacutil -i mercury.dll
+
+.PHONY: install_strong_name
+install_strong_name: library_strong_name.sn
+	cp `vpath_find library_strong_name.sn` \
+		$(INSTALL_MERC_LIB_DIR)/mercury.sn
 	
 else
 
Index: scripts/Mmake.rules
===================================================================
RCS file: /home/mercury1/repository/mercury/scripts/Mmake.rules,v
retrieving revision 1.108
diff -u -r1.108 Mmake.rules
--- scripts/Mmake.rules	3 Aug 2001 12:30:52 -0000	1.108
+++ scripts/Mmake.rules	10 Aug 2001 12:14:53 -0000
@@ -264,6 +264,9 @@
 
 $(os_subdir)%.dll : %.il
 	$(MS_ILASM) $(ALL_MS_ILASMFLAGS) /dll /quiet /OUT=$@ $<
+
+mercury.sn:
+	cp $(MERC_DLL_DIR)/mercury.sn .
 endif
 
 # For --split-c-files, we generate the .$O files directly from the .m files.
Index: scripts/Mmake.vars.in
===================================================================
RCS file: /home/mercury1/repository/mercury/scripts/Mmake.vars.in,v
retrieving revision 1.56
diff -u -r1.56 Mmake.vars.in
--- scripts/Mmake.vars.in	6 Aug 2001 14:13:06 -0000	1.56
+++ scripts/Mmake.vars.in	10 Aug 2001 12:14:53 -0000
@@ -165,7 +165,7 @@
 # into bytecode.
 MS_ILASM		= @ILASM@
 ALL_MS_ILASMFLAGS= $(MS_ILASMFLAGS) $(EXTRA_MS_ILASMFLAGS) \
-		$(TARGET_MS_ILASMFLAGS) $(LIB_MS_ILASMFLAGS)
+		$(TARGET_MS_ILASMFLAGS) $(LIB_MS_ILASMFLAGS) $(ILASM_KEYFLAG)
 MS_ILASMFLAGS	=
 EXTRA_MS_ILASMFLAGS =
 LIB_MS_ILASMFLAGS= $(patsubst %,-I %,$(EXTRA_C_INCL_DIRS))
@@ -394,6 +394,11 @@
    $(maybe-base-MS_ILASMFLAGS-$(findstring undefined,$(origin MS_ILASMFLAGS-$*)))
 maybe-base-MS_ILASMFLAGS- = $(MS_ILASMFLAGS-$*)
 maybe-base-MS_ILASMFLAGS-undefined =
+
+ILASM_KEYFLAG = \
+  $(maybe-target-ILASM_KEYFLAG-$(findstring undefined,$(origin ILASM_KEYFLAG-$*)))
+maybe-target-ILASM_KEYFLAG- = $(ILASM_KEYFLAG-$*)
+maybe-target-ILASM_KEYFLAG-undefined =
 
 # Support for compiling Mercury programs with Prolog will probably be
 # dropped one of these days, so it's probably not worth bothering with these.

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