-
- //
- // Initializes the code generator variables for interactive use (repl)
- //
- static public void InitDynamic (CompilerContext ctx, string name)
- {
- current_domain = AppDomain.CurrentDomain;
- AssemblyName an = Assembly.GetAssemblyName (name, name);
-
- Assembly.Builder = current_domain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Run | COMPILER_ACCESS);
- RootContext.ToplevelTypes = new ModuleContainer (ctx, true);
- RootContext.ToplevelTypes.Builder = Assembly.Builder.DefineDynamicModule (Basename (name), false);
- Assembly.Name = Assembly.Builder.GetName ();
- }
-
- //
- // Initializes the code generator variables
- //
- static public bool Init (string name, string output, bool want_debugging_support, CompilerContext ctx)
- {
- FileName = output;
- AssemblyName an = Assembly.GetAssemblyName (name, output);
- if (an == null)
- return false;
-
- if (an.KeyPair != null) {
- // If we are going to strong name our assembly make
- // sure all its refs are strong named
- foreach (Assembly a in GlobalRootNamespace.Instance.Assemblies) {
- AssemblyName ref_name = a.GetName ();
- byte [] b = ref_name.GetPublicKeyToken ();
- if (b == null || b.Length == 0) {
- ctx.Report.Error (1577, "Assembly generation failed " +
- "-- Referenced assembly '" +
- ref_name.Name +
- "' does not have a strong name.");
- //Environment.Exit (1);
- }
- }
- }
-
- current_domain = AppDomain.CurrentDomain;
-
- try {
- Assembly.Builder = current_domain.DefineDynamicAssembly (an,
- AssemblyBuilderAccess.RunAndSave | COMPILER_ACCESS, Dirname (name));
- }
- catch (ArgumentException) {
- // specified key may not be exportable outside it's container
- if (RootContext.StrongNameKeyContainer != null) {
- ctx.Report.Error (1548, "Could not access the key inside the container `" +
- RootContext.StrongNameKeyContainer + "'.");
- Environment.Exit (1);
- }
- throw;
- }
- catch (CryptographicException) {
- if ((RootContext.StrongNameKeyContainer != null) || (RootContext.StrongNameKeyFile != null)) {
- ctx.Report.Error (1548, "Could not use the specified key to strongname the assembly.");
- Environment.Exit (1);
- }
- return false;
- }
-
- // Get the complete AssemblyName from the builder
- // (We need to get the public key and token)
- Assembly.Name = Assembly.Builder.GetName ();
-
- //
- // Pass a path-less name to DefineDynamicModule. Wonder how
- // this copes with output in different directories then.
- // FIXME: figure out how this copes with --output /tmp/blah
- //
- // If the third argument is true, the ModuleBuilder will dynamically
- // load the default symbol writer.
- //
- try {
- RootContext.ToplevelTypes.Builder = Assembly.Builder.DefineDynamicModule (
- Basename (name), Basename (output), want_debugging_support);
-
-#if !MS_COMPATIBLE
- // TODO: We should use SymbolWriter from DefineDynamicModule
- if (want_debugging_support && !SymbolWriter.Initialize (RootContext.ToplevelTypes.Builder, output)) {
- ctx.Report.Error (40, "Unexpected debug information initialization error `{0}'",
- "Could not find the symbol writer assembly (Mono.CompilerServices.SymbolWriter.dll)");
- return false;
- }
-#endif
- } catch (ExecutionEngineException e) {
- ctx.Report.Error (40, "Unexpected debug information initialization error `{0}'",
- e.Message);
- return false;
- }
-
- return true;
- }
-
- static public void Save (string name, bool saveDebugInfo, Report Report)
- {
- PortableExecutableKinds pekind;
- ImageFileMachine machine;
-
- switch (RootContext.Platform) {
- case Platform.X86:
- pekind = PortableExecutableKinds.Required32Bit;
- machine = ImageFileMachine.I386;
- break;
- case Platform.X64:
- pekind = PortableExecutableKinds.PE32Plus;
- machine = ImageFileMachine.AMD64;
- break;
- case Platform.IA64:
- pekind = PortableExecutableKinds.PE32Plus;
- machine = ImageFileMachine.IA64;
- break;
- case Platform.AnyCPU:
- default:
- pekind = PortableExecutableKinds.ILOnly;
- machine = ImageFileMachine.I386;
- break;
- }
- try {
- Assembly.Builder.Save (Basename (name), pekind, machine);
- }
- catch (COMException) {
- if ((RootContext.StrongNameKeyFile == null) || (!RootContext.StrongNameDelaySign))
- throw;
-
- // FIXME: it seems Microsoft AssemblyBuilder doesn't like to delay sign assemblies
- Report.Error (1548, "Couldn't delay-sign the assembly with the '" +
- RootContext.StrongNameKeyFile +
- "', Use MCS with the Mono runtime or CSC to compile this assembly.");
- }
- catch (System.IO.IOException io) {
- Report.Error (16, "Could not write to file `"+name+"', cause: " + io.Message);
- return;
- }
- catch (System.UnauthorizedAccessException ua) {
- Report.Error (16, "Could not write to file `"+name+"', cause: " + ua.Message);
- return;
- }
- catch (System.NotImplementedException nie) {
- Report.RuntimeMissingSupport (Location.Null, nie.Message);
- return;
- }
-
- //
- // Write debuger symbol file
- //
- if (saveDebugInfo)
- SymbolWriter.WriteSymbolFile ();
- }
- }