Exemple #1
0
        /// <summary>
        /// Gets a collection of assembly attributes exposed by the assembly.
        /// </summary>
        /// <returns>A System.Specialized.KeyValueCollection of assembly attributes.</returns>
        public NameValueCollection GetAttributes()
        {
            NameValueCollection assemblyAttributes = new NameValueCollection();

            //Add some values that are not in AssemblyInfo.
            assemblyAttributes.Add("Full Name", FullName);
            assemblyAttributes.Add("Name", Name);
            assemblyAttributes.Add("Version", Version.ToString());
            assemblyAttributes.Add("Image Runtime Version", ImageRuntimeVersion);
            assemblyAttributes.Add("Build Date", BuildDate.ToString());
            assemblyAttributes.Add("Location", Location);
            assemblyAttributes.Add("Code Base", CodeBase);
            assemblyAttributes.Add("GAC Loaded", GACLoaded.ToString());

            //Add all attributes available from AssemblyInfo.
            assemblyAttributes.Add("Title", Title);
            assemblyAttributes.Add("Description", Description);
            assemblyAttributes.Add("Company", Company);
            assemblyAttributes.Add("Product", Product);
            assemblyAttributes.Add("Copyright", Copyright);
            assemblyAttributes.Add("Trademark", Trademark);
            assemblyAttributes.Add("Configuration", Configuration);
            assemblyAttributes.Add("Delay Sign", DelaySign.ToString());
            assemblyAttributes.Add("Informational Version", InformationalVersion);
            assemblyAttributes.Add("Key File", KeyFile);
            assemblyAttributes.Add("Culture Name", CultureName);
            assemblyAttributes.Add("Satellite Contract Version", SatelliteContractVersion);
            assemblyAttributes.Add("Com Compatible Version", ComCompatibleVersion);
            assemblyAttributes.Add("Com Visible", ComVisible.ToString());
            assemblyAttributes.Add("Guid", Guid);
            assemblyAttributes.Add("Type Lib Version", TypeLibVersion);
            assemblyAttributes.Add("CLS Compliant", CLSCompliant.ToString());

            return(assemblyAttributes);
        }
Exemple #2
0
		private void DoIt () {
			AssemblyName aname = new AssemblyName ();
			aname.Name = Path.GetFileNameWithoutExtension (outFile);
			if (culture != null)
				aname.CultureInfo = new CultureInfo (culture);

			string fileName = Path.GetFileName (outFile);

			AssemblyBuilder ab;
			
			/*
			 * Emit Manifest
			 * */

			if (isTemplateFile) {
				// LAMESPEC: according to MSDN, the template assembly must have a
				// strong name but this is not enforced
				Assembly assembly = Assembly.LoadFrom (templateFile);

				// inherit signing related settings from template, but do not
				// override command-line options
				object [] attrs = assembly.GetCustomAttributes (true);
				foreach (object o in attrs) {
					if (o is AssemblyKeyFileAttribute) {
						if (keyfile != null)
							// ignore if specified on command line
							continue;
						AssemblyKeyFileAttribute keyFileAttr = (AssemblyKeyFileAttribute) o;
						// ignore null or zero-length keyfile
						if (keyFileAttr.KeyFile == null || keyFileAttr.KeyFile.Length == 0)
							continue;
						keyfile = Path.Combine (Path.GetDirectoryName(templateFile),
							keyFileAttr.KeyFile);
					} else if (o is AssemblyDelaySignAttribute) {
						if (delaysign != DelaySign.NotSet)
							// ignore if specified on command line
							continue;
						AssemblyDelaySignAttribute delaySignAttr = (AssemblyDelaySignAttribute) o;
						delaysign = delaySignAttr.DelaySign ? DelaySign.Yes :
							DelaySign.No;
					} else if (o is AssemblyKeyNameAttribute) {
						if (keyname != null)
							// ignore if specified on command line
							continue;
						AssemblyKeyNameAttribute keynameAttr = (AssemblyKeyNameAttribute) o;
						// ignore null or zero-length keyname
						if (keynameAttr.KeyName == null || keynameAttr.KeyName.Length == 0)
							continue;
						keyname = keynameAttr.KeyName;
					}
				}
				aname.Version = assembly.GetName().Version;
				aname.HashAlgorithm = assembly.GetName().HashAlgorithm;
			}

			SetKeyPair (aname);

			if (fileName != outFile)
				ab = AppDomain.CurrentDomain.DefineDynamicAssembly (aname, AssemblyBuilderAccess.Save, Path.GetDirectoryName (outFile));
			else
				ab = AppDomain.CurrentDomain.DefineDynamicAssembly (aname, AssemblyBuilderAccess.Save);

			foreach (CustomAttributeBuilder cb in cattrs)
				ab.SetCustomAttribute (cb);

			/*
			 * Emit modules
			 */

			foreach (ModuleInfo mod in inputFiles) {
				MethodInfo mi = typeof (AssemblyBuilder).GetMethod ("AddModule", BindingFlags.Instance|BindingFlags.Public|BindingFlags.NonPublic);
				if (mi == null)
					Report (0, "Cannot add modules on this runtime: try the Mono runtime instead.");

				if (mod.target != null) {
					File.Copy (mod.fileName, mod.target, true);
					mod.fileName = mod.target;
				}

				bool isAssembly = false;
				try {
					AssemblyName.GetAssemblyName (mod.fileName);
					isAssembly = true;
				}
				catch (Exception) {
				}

				if (isAssembly)
					ReportWarning (1020, "Ignoring included assembly '" + mod.fileName + "'");
				else
					mi.Invoke (ab, new object [] { mod.fileName });
			}

			/*
			 * Set entry point
			 */

			if (entryPoint != null) {
				string mainClass = entryPoint.Substring (0, entryPoint.LastIndexOf ('.'));
				string mainMethod = entryPoint.Substring (entryPoint.LastIndexOf ('.') + 1);

				MethodInfo mainMethodInfo = null;

				try {
					Type mainType = ab.GetType (mainClass);
					if (mainType != null)
						mainMethodInfo = mainType.GetMethod (mainMethod);
				}
				catch (Exception ex) {
					Console.WriteLine (ex);
				}
				if (mainMethodInfo != null)
					ab.SetEntryPoint (mainMethodInfo);
				else
					Report (1037, "Unable to find the entry point method '" + entryPoint + "'");
			}

			/*
			 * Emit resources
			 */

			ab.DefineVersionInfoResource ();

			if (win32IconFile != null) {
				try {
					MethodInfo mi = typeof (AssemblyBuilder).GetMethod ("DefineIconResource", BindingFlags.Instance|BindingFlags.Public|BindingFlags.NonPublic);
					if (mi == null)
						Report (0, "Cannot embed win32 icons on this runtime: try the Mono runtime instead.");
					mi.Invoke (ab, new object [] {  win32IconFile });
				}
				catch (Exception ex) {
					Report (1031, "Error reading icon '" + win32IconFile + "' --" + ex);
				}
			}

			if (win32ResFile != null) {
				try {
					ab.DefineUnmanagedResource (win32ResFile);
				}
				catch (Exception ex) {
					Report (1019, "Metadata failure creating assembly -- " + ex);
				}
			}

			foreach (ResourceInfo res in resources) {
				if (res.name == null)
					res.name = Path.GetFileName (res.fileName);

				foreach (ResourceInfo res2 in resources)
					if ((res != res2) && (res.name == res2.name))
						Report (1046, String.Format ("Resource identifier '{0}' has already been used in this assembly", res.name));

				if (res.isEmbedded) {
					MethodInfo mi = typeof (AssemblyBuilder).GetMethod ("EmbedResourceFile", BindingFlags.Instance|BindingFlags.Public|BindingFlags.NonPublic,
						null, CallingConventions.Any, new Type [] { typeof (string), typeof (string) }, null);
					if (mi == null)
						Report (0, "Cannot embed resources on this runtime: try the Mono runtime instead.");
					mi.Invoke (ab, new object [] { res.name, res.fileName });
				}
				else {
					if (res.target != null) {
						File.Copy (res.fileName, res.target, true);
						res.fileName = res.target;
					}
					
					// AddResourceFile must receive a file name and not a path.
					// Drop directory and give warning if we have a path.
					var resourceFileName = Path.GetFileName(res.fileName);
					if (Path.GetDirectoryName (res.fileName) != null || Path.IsPathRooted(res.fileName)) {
						ReportWarning (99999, 
							String.Format ("Path '{0}' in the resource name is not supported. Using just file name '{1}'",
								res.fileName,
								resourceFileName));
					}

					ab.AddResourceFile (res.name, resourceFileName,
							res.isPrivate ? ResourceAttributes.Private : ResourceAttributes.Public);
				}
			}

			try {
				ab.Save (fileName);
			}
			catch (Exception ex) {
				Report (1019, "Metadata failure creating assembly -- " + ex);
			}
		}
Exemple #3
0
		private bool ParseOption (string str)
		{
			string arg;
			string opt = GetCommand (str, out arg);

			switch (opt) {
			case "help":
			case "?":
				Usage ();
				return true;

			case "embed": {
					if (arg == null)
						ReportMissingFileSpec (opt);
					ResourceInfo res = new ResourceInfo ();
					res.isEmbedded = true;
					String [] parts = arg.Split (',');
					res.fileName = parts [0];
					if (parts.Length > 1)
						res.name = parts [1];
					if (parts.Length > 2) {
						switch (parts [2]) {
						case "public":
							break;
						case "private":
							res.isPrivate = true;
							break;
						default:
							ReportInvalidArgument (opt, parts [2]);
							break;
						}
					}
					resources.Add (res);
					return true;
				}

			case "link": {
					if (arg == null)
						ReportMissingFileSpec (opt);
					ResourceInfo res = new ResourceInfo ();
					String [] parts = arg.Split (',');
					res.fileName = parts [0];
					if (parts.Length > 1)
						res.name = parts [1];
					if (parts.Length > 2)
						res.target = parts [2];
					if (parts.Length > 3) {
						switch (parts [3]) {
						case "public":
							break;
						case "private":
							res.isPrivate = true;
							break;
						default:
							ReportInvalidArgument (opt, parts [3]);
							break;
						}
					}
					resources.Add (res);
					return true;
				}

			case "algid":
				if (arg == null)
					ReportMissingArgument (opt);
				try {
					string realArg = arg;
					if (realArg.StartsWith ("0x"))
						realArg = realArg.Substring (2);
					uint val = Convert.ToUInt32 (realArg, 16);
					AddCattr (typeof (AssemblyAlgorithmIdAttribute), typeof (uint), val);
				} catch (Exception) {
					ReportInvalidArgument (opt, arg);
				}
				return true;

			case "base":
				ReportNotImplemented (opt);
				return true;

			case "baseaddress":
				ReportNotImplemented (opt);
				return true;

			case "bugreport":
				ReportNotImplemented (opt);
				return true;

			case "comp":
			case "company":
				if (arg == null)
					ReportMissingText (opt);
				AddCattr (typeof (AssemblyCompanyAttribute), arg);
				return true;

			case "config":
			case "configuration":
				if (arg == null)
					ReportMissingText (opt);
				AddCattr (typeof (AssemblyConfigurationAttribute), arg);
				return true;

			case "copy":
			case "copyright":
				if (arg == null)
					ReportMissingText (opt);
				AddCattr (typeof (AssemblyCopyrightAttribute), arg);
				return true;

			case "c":
			case "culture":
				if (arg == null)
					ReportMissingText (opt);
				culture = arg;
				return true;

			case "delay":
			case "delaysign":
			case "delay+":
			case "delaysign+":
				delaysign = DelaySign.Yes;
				return true;

			case "delay-":
			case "delaysign-":
				delaysign = DelaySign.No;
				return true;

			case "descr":
			case "description":
				if (arg == null)
					ReportMissingText (opt);
				AddCattr (typeof (AssemblyDescriptionAttribute), arg);
				return true;

			case "e":
			case "evidence": {
				if (arg == null)
					ReportMissingFileSpec (opt);
				ResourceInfo res = new ResourceInfo ();
				res.name = "Security.Evidence";
				res.fileName = arg;
				res.isEmbedded = true;
				res.isPrivate = true;
				resources.Add (res);
				return true;
			}
			case "fileversion":
				if (arg == null)
					ReportMissingText (opt);

				AddCattr (typeof (AssemblyFileVersionAttribute), arg);
				return true;

			case "flags":
				if (arg == null)
					ReportMissingArgument (opt);
				try {
					string realArg = arg;
					if (realArg.StartsWith ("0x"))
						realArg = realArg.Substring (2);
					uint val = Convert.ToUInt32 (realArg, 16);
					AddCattr (typeof (AssemblyFlagsAttribute), typeof (uint), val);
				} catch (Exception) {
					ReportInvalidArgument (opt, arg);
				}
				return true;

			case "fullpaths":
				fullPaths = true;
				return true;

			case "keyf":
			case "keyfile":
				if (arg == null)
					ReportMissingText (opt);
				keyfile = arg;
				return true;

			case "keyn":
			case "keyname":
				if (arg == null)
					ReportMissingText (opt);
				keyname = arg;
				return true;

			case "main":
				if (arg == null)
					ReportMissingText (opt);
				entryPoint = arg;
				return true;

			case "nologo":
				return true;

			case "out":
				if (arg == null)
					ReportMissingFileSpec (opt);
				outFile = arg;
				return true;

			case "prod":
			case "product":
				if (arg == null)
					ReportMissingText (opt);
				AddCattr (typeof (AssemblyProductAttribute), arg);
				return true;

			case "productv":
			case "productversion":
				if (arg == null)
					ReportMissingText (opt);
				AddCattr (typeof (AssemblyInformationalVersionAttribute), arg);
				return true;

			case "t":
			case "target":
				if (arg == null)
					ReportMissingText (opt);
				switch (arg) {
				case "lib":
				case "library":
					target = Target.Dll;
					break;
				case "exe":
					target = Target.Exe;
					break;
				case "win":
				case "winexe":
					Report (0, "target:win is not implemented");
					break;
				default:
					ReportInvalidArgument (opt, arg);
					break;
				}
				return true;

			case "template":
				if (arg == null)
					ReportMissingFileSpec (opt);
				isTemplateFile = true;
				templateFile = Path.Combine (Directory.GetCurrentDirectory (), arg);
				return true;

			case "title":
				if (arg == null)
					ReportMissingText (opt);
				AddCattr (typeof (AssemblyTitleAttribute), arg);
				return true;

			case "trade":
			case "trademark":
				if (arg == null)
					ReportMissingText (opt);
				AddCattr (typeof (AssemblyTrademarkAttribute), arg);
				return true;

			case "v":
			case "version":
				// This option conflicts with the standard UNIX meaning
				if (arg == null) {
					Version ();
					break;
				}
				AddCattr (typeof (AssemblyVersionAttribute), arg);
				return true;

			case "win32icon":
				if (arg == null)
					ReportMissingFileSpec (opt);
				win32IconFile = arg;
				return true;

			case "win32res":
				if (arg == null)
					ReportMissingFileSpec (opt);
				win32ResFile = arg;
				return true;
			}
			return false;
		}
        private void DoIt()
        {
            AssemblyName aname = new AssemblyName();

            aname.Name = Path.GetFileNameWithoutExtension(outFile);
            if (culture != null)
            {
                aname.CultureInfo = new CultureInfo(culture);
            }

            string fileName = Path.GetFileName(outFile);

            AssemblyBuilder ab;

            /*
             * Emit Manifest
             * */

            if (isTemplateFile)
            {
                // LAMESPEC: according to MSDN, the template assembly must have a
                // strong name but this is not enforced
                Assembly assembly = Assembly.LoadFrom(templateFile);

                // inherit signing related settings from template, but do not
                // override command-line options
                object [] attrs = assembly.GetCustomAttributes(true);
                foreach (object o in attrs)
                {
                    if (o is AssemblyKeyFileAttribute)
                    {
                        if (keyfile != null)
                        {
                            // ignore if specified on command line
                            continue;
                        }
                        AssemblyKeyFileAttribute keyFileAttr = (AssemblyKeyFileAttribute)o;
                        // ignore null or zero-length keyfile
                        if (keyFileAttr.KeyFile == null || keyFileAttr.KeyFile.Length == 0)
                        {
                            continue;
                        }
                        keyfile = Path.Combine(Path.GetDirectoryName(templateFile),
                                               keyFileAttr.KeyFile);
                    }
                    else if (o is AssemblyDelaySignAttribute)
                    {
                        if (delaysign != DelaySign.NotSet)
                        {
                            // ignore if specified on command line
                            continue;
                        }
                        AssemblyDelaySignAttribute delaySignAttr = (AssemblyDelaySignAttribute)o;
                        delaysign = delaySignAttr.DelaySign ? DelaySign.Yes :
                                    DelaySign.No;
                    }
                    else if (o is AssemblyKeyNameAttribute)
                    {
                        if (keyname != null)
                        {
                            // ignore if specified on command line
                            continue;
                        }
                        AssemblyKeyNameAttribute keynameAttr = (AssemblyKeyNameAttribute)o;
                        // ignore null or zero-length keyname
                        if (keynameAttr.KeyName == null || keynameAttr.KeyName.Length == 0)
                        {
                            continue;
                        }
                        keyname = keynameAttr.KeyName;
                    }
                }
                aname.Version       = assembly.GetName().Version;
                aname.HashAlgorithm = assembly.GetName().HashAlgorithm;
            }

            SetKeyPair(aname);

            if (fileName != outFile)
            {
                ab = AppDomain.CurrentDomain.DefineDynamicAssembly(aname, AssemblyBuilderAccess.Save, Path.GetDirectoryName(outFile));
            }
            else
            {
                ab = AppDomain.CurrentDomain.DefineDynamicAssembly(aname, AssemblyBuilderAccess.Save);
            }

            foreach (CustomAttributeBuilder cb in cattrs)
            {
                ab.SetCustomAttribute(cb);
            }

            /*
             * Emit modules
             */

            foreach (ModuleInfo mod in inputFiles)
            {
                MethodInfo mi = typeof(AssemblyBuilder).GetMethod("AddModule", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                if (mi == null)
                {
                    Report(0, "Cannot add modules on this runtime: try the Mono runtime instead.");
                }

                if (mod.target != null)
                {
                    File.Copy(mod.fileName, mod.target, true);
                    mod.fileName = mod.target;
                }

                bool isAssembly = false;
                try
                {
                    AssemblyName.GetAssemblyName(mod.fileName);
                    isAssembly = true;
                }
                catch (Exception)
                {
                }

                if (isAssembly)
                {
                    ReportWarning(1020, "Ignoring included assembly '" + mod.fileName + "'");
                }
                else
                {
                    mi.Invoke(ab, new object [] { mod.fileName });
                }
            }

            /*
             * Set entry point
             */

            if (entryPoint != null)
            {
                string mainClass  = entryPoint.Substring(0, entryPoint.LastIndexOf('.'));
                string mainMethod = entryPoint.Substring(entryPoint.LastIndexOf('.') + 1);

                MethodInfo mainMethodInfo = null;

                try
                {
                    Type mainType = ab.GetType(mainClass);
                    if (mainType != null)
                    {
                        mainMethodInfo = mainType.GetMethod(mainMethod);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
                if (mainMethodInfo != null)
                {
                    ab.SetEntryPoint(mainMethodInfo);
                }
                else
                {
                    Report(1037, "Unable to find the entry point method '" + entryPoint + "'");
                }
            }

            /*
             * Emit resources
             */

            ab.DefineVersionInfoResource();

            if (win32IconFile != null)
            {
                try
                {
                    MethodInfo mi = typeof(AssemblyBuilder).GetMethod("DefineIconResource", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                    if (mi == null)
                    {
                        Report(0, "Cannot embed win32 icons on this runtime: try the Mono runtime instead.");
                    }
                    mi.Invoke(ab, new object [] { win32IconFile });
                }
                catch (Exception ex)
                {
                    Report(1031, "Error reading icon '" + win32IconFile + "' --" + ex);
                }
            }

            if (win32ResFile != null)
            {
                try
                {
                    ab.DefineUnmanagedResource(win32ResFile);
                }
                catch (Exception ex)
                {
                    Report(1019, "Metadata failure creating assembly -- " + ex);
                }
            }

            foreach (ResourceInfo res in resources)
            {
                if (res.name == null)
                {
                    res.name = Path.GetFileName(res.fileName);
                }

                foreach (ResourceInfo res2 in resources)
                {
                    if ((res != res2) && (res.name == res2.name))
                    {
                        Report(1046, String.Format("Resource identifier '{0}' has already been used in this assembly", res.name));
                    }
                }

                if (res.isEmbedded)
                {
                    MethodInfo mi = typeof(AssemblyBuilder).GetMethod("EmbedResourceFile", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
                                                                      null, CallingConventions.Any, new Type [] { typeof(string), typeof(string) }, null);
                    if (mi == null)
                    {
                        Report(0, "Cannot embed resources on this runtime: try the Mono runtime instead.");
                    }
                    mi.Invoke(ab, new object [] { res.name, res.fileName });
                }
                else
                {
                    if (res.target != null)
                    {
                        File.Copy(res.fileName, res.target, true);
                        res.fileName = res.target;
                    }

                    ab.AddResourceFile(res.name, res.fileName,
                                       res.isPrivate ? ResourceAttributes.Private : ResourceAttributes.Public);
                }
            }

            try
            {
                ab.Save(fileName);
            }
            catch (Exception ex)
            {
                Report(1019, "Metadata failure creating assembly -- " + ex);
            }
        }
        private bool ParseOption(string str)
        {
            string arg;
            string opt = GetCommand(str, out arg);

            switch (opt)
            {
            case "help":
            case "?":
                Usage();
                return(true);

            case "embed":
            {
                if (arg == null)
                {
                    ReportMissingFileSpec(opt);
                }
                ResourceInfo res = new ResourceInfo();
                res.isEmbedded = true;
                String [] parts = arg.Split(',');
                res.fileName = parts [0];
                if (parts.Length > 1)
                {
                    res.name = parts [1];
                }
                if (parts.Length > 2)
                {
                    switch (parts [2])
                    {
                    case "public":
                        break;

                    case "private":
                        res.isPrivate = true;
                        break;

                    default:
                        ReportInvalidArgument(opt, parts [2]);
                        break;
                    }
                }
                resources.Add(res);
                return(true);
            }

            case "link":
            {
                if (arg == null)
                {
                    ReportMissingFileSpec(opt);
                }
                ResourceInfo res   = new ResourceInfo();
                String []    parts = arg.Split(',');
                res.fileName = parts [0];
                if (parts.Length > 1)
                {
                    res.name = parts [1];
                }
                if (parts.Length > 2)
                {
                    res.target = parts [2];
                }
                if (parts.Length > 3)
                {
                    switch (parts [3])
                    {
                    case "public":
                        break;

                    case "private":
                        res.isPrivate = true;
                        break;

                    default:
                        ReportInvalidArgument(opt, parts [3]);
                        break;
                    }
                }
                resources.Add(res);
                return(true);
            }

            case "algid":
                if (arg == null)
                {
                    ReportMissingArgument(opt);
                }
                try
                {
                    string realArg = arg;
                    if (realArg.StartsWith("0x"))
                    {
                        realArg = realArg.Substring(2);
                    }
                    uint val = Convert.ToUInt32(realArg, 16);
                    AddCattr(typeof(AssemblyAlgorithmIdAttribute), typeof(uint), val);
                }
                catch (Exception)
                {
                    ReportInvalidArgument(opt, arg);
                }
                return(true);

            case "base":
                ReportNotImplemented(opt);
                return(true);

            case "baseaddress":
                ReportNotImplemented(opt);
                return(true);

            case "bugreport":
                ReportNotImplemented(opt);
                return(true);

            case "comp":
            case "company":
                if (arg == null)
                {
                    ReportMissingText(opt);
                }
                AddCattr(typeof(AssemblyCompanyAttribute), arg);
                return(true);

            case "config":
            case "configuration":
                if (arg == null)
                {
                    ReportMissingText(opt);
                }
                AddCattr(typeof(AssemblyConfigurationAttribute), arg);
                return(true);

            case "copy":
            case "copyright":
                if (arg == null)
                {
                    ReportMissingText(opt);
                }
                AddCattr(typeof(AssemblyCopyrightAttribute), arg);
                return(true);

            case "c":
            case "culture":
                if (arg == null)
                {
                    ReportMissingText(opt);
                }
                culture = arg;
                return(true);

            case "delay":
            case "delaysign":
            case "delay+":
            case "delaysign+":
                delaysign = DelaySign.Yes;
                return(true);

            case "delay-":
            case "delaysign-":
                delaysign = DelaySign.No;
                return(true);

            case "descr":
            case "description":
                if (arg == null)
                {
                    ReportMissingText(opt);
                }
                AddCattr(typeof(AssemblyDescriptionAttribute), arg);
                return(true);

            case "e":
            case "evidence":
                if (arg == null)
                {
                    ReportMissingFileSpec(opt);
                }
                ResourceInfo res = new ResourceInfo();
                res.name       = "Security.Evidence";
                res.fileName   = arg;
                res.isEmbedded = true;
                res.isPrivate  = true;
                resources.Add(res);
                return(true);

            case "fileversion":
                if (arg == null)
                {
                    ReportMissingText(opt);
                }

                AddCattr(typeof(AssemblyFileVersionAttribute), arg);
                return(true);

            case "flags":
                if (arg == null)
                {
                    ReportMissingArgument(opt);
                }
                try
                {
                    string realArg = arg;
                    if (realArg.StartsWith("0x"))
                    {
                        realArg = realArg.Substring(2);
                    }
                    uint val = Convert.ToUInt32(realArg, 16);
                    AddCattr(typeof(AssemblyFlagsAttribute), typeof(uint), val);
                }
                catch (Exception)
                {
                    ReportInvalidArgument(opt, arg);
                }
                return(true);

            case "fullpaths":
                fullPaths = true;
                return(true);

            case "keyf":
            case "keyfile":
                if (arg == null)
                {
                    ReportMissingText(opt);
                }
                keyfile = arg;
                return(true);

            case "keyn":
            case "keyname":
                if (arg == null)
                {
                    ReportMissingText(opt);
                }
                keyname = arg;
                return(true);

            case "main":
                if (arg == null)
                {
                    ReportMissingText(opt);
                }
                entryPoint = arg;
                return(true);

            case "nologo":
                return(true);

            case "out":
                if (arg == null)
                {
                    ReportMissingFileSpec(opt);
                }
                outFile = arg;
                return(true);

            case "prod":
            case "product":
                if (arg == null)
                {
                    ReportMissingText(opt);
                }
                AddCattr(typeof(AssemblyProductAttribute), arg);
                return(true);

            case "productv":
            case "productversion":
                if (arg == null)
                {
                    ReportMissingText(opt);
                }
                AddCattr(typeof(AssemblyInformationalVersionAttribute), arg);
                return(true);

            case "t":
            case "target":
                if (arg == null)
                {
                    ReportMissingText(opt);
                }
                switch (arg)
                {
                case "lib":
                case "library":
                    target = Target.Dll;
                    break;

                case "exe":
                    target = Target.Exe;
                    break;

                case "win":
                case "winexe":
                    Report(0, "target:win is not implemented");
                    break;

                default:
                    ReportInvalidArgument(opt, arg);
                    break;
                }
                return(true);

            case "template":
                if (arg == null)
                {
                    ReportMissingFileSpec(opt);
                }
                isTemplateFile = true;
                templateFile   = Path.Combine(Directory.GetCurrentDirectory(), arg);
                return(true);

            case "title":
                if (arg == null)
                {
                    ReportMissingText(opt);
                }
                AddCattr(typeof(AssemblyTitleAttribute), arg);
                return(true);

            case "trade":
            case "trademark":
                if (arg == null)
                {
                    ReportMissingText(opt);
                }
                AddCattr(typeof(AssemblyTrademarkAttribute), arg);
                return(true);

            case "v":
            case "version":
                // This option conflicts with the standard UNIX meaning
                if (arg == null)
                {
                    Version();
                    break;
                }
                AddCattr(typeof(AssemblyVersionAttribute), arg);
                return(true);

            case "win32icon":
                if (arg == null)
                {
                    ReportMissingFileSpec(opt);
                }
                win32IconFile = arg;
                return(true);

            case "win32res":
                if (arg == null)
                {
                    ReportMissingFileSpec(opt);
                }
                win32ResFile = arg;
                return(true);
            }
            return(false);
        }
Exemple #6
0
        private AssemblyName ReadCustomAttributesFromTemplateFile(string templateFile, AssemblyName aname)
        {
            // LAMESPEC: according to MSDN, the template assembly must have a
            // strong name but this is not enforced
            const IKR.UniverseOptions options = IKR.UniverseOptions.MetadataOnly;

            var universe = new IKR.Universe(options);
            var asm      = universe.LoadFile(templateFile);

            // Create missing assemblies, we don't want to load them!
            // Code taken from ikdasm
            var names = new HashSet <string> ();

            IKR.AssemblyName[] assembly_refs = asm.ManifestModule.__GetReferencedAssemblies();

            var resolved_assemblies = new IKR.Assembly [assembly_refs.Length];

            for (int i = 0; i < resolved_assemblies.Length; i++)
            {
                string name = assembly_refs [i].Name;

                while (names.Contains(name))
                {
                    name = name + "_" + i;
                }
                names.Add(name);
                resolved_assemblies [i] = universe.CreateMissingAssembly(assembly_refs [i].FullName);
            }
            asm.ManifestModule.__ResolveReferencedAssemblies(resolved_assemblies);

            foreach (var attr_data in asm.__GetCustomAttributes(null, false))
            {
                string asm_name = attr_data.AttributeType.Assembly.GetName().Name;
                if (asm_name != "mscorlib")
                {
                    continue;
                }

                switch (attr_data.AttributeType.FullName)
                {
                case "System.Reflection.AssemblyKeyFileAttribute": {
                    if (keyfile != null)
                    {
                        // ignore if specified on command line
                        continue;
                    }

                    // / AssemblyKeyFileAttribute .ctor(string keyFile)
                    string key_file_value = (string)attr_data.ConstructorArguments [0].Value;

                    if (!String.IsNullOrEmpty(key_file_value))
                    {
                        keyfile = Path.Combine(Path.GetDirectoryName(templateFile), key_file_value);
                    }
                }
                break;

                case "System.Reflection.AssemblyDelaySignAttribute": {
                    if (delaysign != DelaySign.NotSet)
                    {
                        // ignore if specified on command line
                        continue;
                    }

                    // AssemblyDelaySignAttribute .ctor(bool delaySign)
                    bool delay_sign_value = (bool)attr_data.ConstructorArguments [0].Value;
                    delaysign = delay_sign_value ? DelaySign.Yes : DelaySign.No;
                }
                break;

                case "System.Reflection.AssemblyKeyNameAttribute": {
                    if (keyname != null)
                    {
                        // ignore if specified on command line
                        continue;
                    }

                    // AssemblyKeyNameAttribute .ctor(string keyName)
                    string key_name_value = (string)attr_data.ConstructorArguments [0].Value;

                    // ignore null or zero-length keyname
                    if (!String.IsNullOrEmpty(key_name_value))
                    {
                        keyname = key_name_value;
                    }
                }
                break;
                }
            }

            var asm_name_for_template_file = asm.GetName();

            aname.Version       = asm_name_for_template_file.Version;
            aname.HashAlgorithm = asm_name_for_template_file.HashAlgorithm;

            return(aname);
        }
Exemple #7
0
		private AssemblyName ReadCustomAttributesFromTemplateFile (string templateFile, AssemblyName aname)
		{
			// LAMESPEC: according to MSDN, the template assembly must have a
			// strong name but this is not enforced
			const IKR.UniverseOptions options = IKR.UniverseOptions.MetadataOnly;

			var universe = new IKR.Universe (options);
			var asm = universe.LoadFile (templateFile);

			// Create missing assemblies, we don't want to load them!
			// Code taken from ikdasm
			var names = new HashSet<string> ();
			IKR.AssemblyName[] assembly_refs = asm.ManifestModule.__GetReferencedAssemblies ();

			var resolved_assemblies = new IKR.Assembly [assembly_refs.Length];
			for (int i = 0; i < resolved_assemblies.Length; i++) {
				string name = assembly_refs [i].Name;

				while (names.Contains (name)) {
					name = name + "_" + i;
				}
				names.Add (name);
				resolved_assemblies [i] = universe.CreateMissingAssembly (assembly_refs [i].FullName);
			}
			asm.ManifestModule.__ResolveReferencedAssemblies (resolved_assemblies);

			foreach (var attr_data in asm.__GetCustomAttributes (null, false)) {
				string asm_name = attr_data.AttributeType.Assembly.GetName ().Name;
				if (asm_name != "mscorlib")
					continue;

				switch (attr_data.AttributeType.FullName) {
					case "System.Reflection.AssemblyKeyFileAttribute": {
						if (keyfile != null)
							// ignore if specified on command line
							continue;

						// / AssemblyKeyFileAttribute .ctor(string keyFile)
						string key_file_value = (string) attr_data.ConstructorArguments [0].Value;

						if (!String.IsNullOrEmpty (key_file_value))
							keyfile = Path.Combine (Path.GetDirectoryName (templateFile), key_file_value);
					}
					break;

					case "System.Reflection.AssemblyDelaySignAttribute": {
						if (delaysign != DelaySign.NotSet)
							// ignore if specified on command line
							continue;

						// AssemblyDelaySignAttribute .ctor(bool delaySign)
						bool delay_sign_value = (bool) attr_data.ConstructorArguments [0].Value;
						delaysign = delay_sign_value ? DelaySign.Yes : DelaySign.No;
					}
					break;

					case "System.Reflection.AssemblyKeyNameAttribute": {
						if (keyname != null)
							// ignore if specified on command line
							continue;

						// AssemblyKeyNameAttribute .ctor(string keyName)
						string key_name_value = (string) attr_data.ConstructorArguments [0].Value;

						// ignore null or zero-length keyname
						if (!String.IsNullOrEmpty (key_name_value))
							keyname = key_name_value;
					}
					break;
				}
			}

			var asm_name_for_template_file = asm.GetName ();
			aname.Version = asm_name_for_template_file.Version;
			aname.HashAlgorithm = asm_name_for_template_file.HashAlgorithm;

			return aname;
		}