diff --git a/tools/common/DerivedLinkContext.cs b/tools/common/DerivedLinkContext.cs index 7f6e4d516ad9..c16ea72890ac 100644 --- a/tools/common/DerivedLinkContext.cs +++ b/tools/common/DerivedLinkContext.cs @@ -43,6 +43,14 @@ public class DerivedLinkContext : LinkContext { // so we need a second dictionary Dictionary LinkedAwayTypeMap = new Dictionary (); +#if NET + public DerivedLinkContext (Xamarin.Linker.LinkerConfiguration configuration, Target target) + : base (configuration) + { + this.Target = target; + } +#endif + public Application App { get { return Target.App; diff --git a/tools/dotnet-linker/ApplyPreserveAttributeBase.cs b/tools/dotnet-linker/ApplyPreserveAttributeBase.cs index d36cc04307f6..22b3b998bbaa 100644 --- a/tools/dotnet-linker/ApplyPreserveAttributeBase.cs +++ b/tools/dotnet-linker/ApplyPreserveAttributeBase.cs @@ -10,6 +10,8 @@ using Mono.Cecil; +#nullable enable + namespace Mono.Tuner { public abstract class ApplyPreserveAttributeBase : BaseSubStep { @@ -72,16 +74,16 @@ void MarkMethodIfPreserved (MethodDefinition method) MarkMethod (method, attribute); } - void MarkMethod (MethodDefinition method, CustomAttribute preserve_attribute) + void MarkMethod (MethodDefinition? method, CustomAttribute? preserve_attribute) { - if (method == null) + if (method is null) return; Mark (method, preserve_attribute); Annotations.SetAction (method, MethodAction.Parse); } - void Mark (IMetadataTokenProvider provider, CustomAttribute preserve_attribute) + void Mark (IMetadataTokenProvider provider, CustomAttribute? preserve_attribute) { if (IsConditionalAttribute (preserve_attribute)) { PreserveConditional (provider); @@ -103,9 +105,9 @@ void PreserveConditional (IMetadataTokenProvider provider) Annotations.AddPreservedMethod (method.DeclaringType, method); } - static bool IsConditionalAttribute (CustomAttribute attribute) + static bool IsConditionalAttribute (CustomAttribute? attribute) { - if (attribute == null) + if (attribute is null) return false; foreach (var named_argument in attribute.Fields) diff --git a/tools/dotnet-linker/BackingFieldDelayHandler.cs b/tools/dotnet-linker/BackingFieldDelayHandler.cs index fd06fe81f129..be9b46adf828 100644 --- a/tools/dotnet-linker/BackingFieldDelayHandler.cs +++ b/tools/dotnet-linker/BackingFieldDelayHandler.cs @@ -6,6 +6,8 @@ using Mono.Linker.Steps; using Xamarin.Tuner; +#nullable enable + namespace Xamarin.Linker { // Problems: diff --git a/tools/dotnet-linker/Compat.cs b/tools/dotnet-linker/Compat.cs index 762644efc481..f919b9651bae 100644 --- a/tools/dotnet-linker/Compat.cs +++ b/tools/dotnet-linker/Compat.cs @@ -12,14 +12,17 @@ using Xamarin.Linker; using Xamarin.Utils; +#nullable enable + namespace Xamarin.Bundler { public partial class Application { - public LinkerConfiguration Configuration { get; private set; } - public string RuntimeConfigurationFile { get; set; } + LinkerConfiguration? configuration; + public LinkerConfiguration Configuration { get => configuration!; } + public string? RuntimeConfigurationFile { get; set; } public Application (LinkerConfiguration configuration) { - this.Configuration = configuration; + this.configuration = configuration; } public string ProductName { @@ -103,7 +106,7 @@ public static string GetArch64Directory (Application app) throw new NotImplementedException (); } - public static string SdkRoot { + public static string? SdkRoot { get => sdk_root; set => sdk_root = value; } @@ -114,6 +117,11 @@ public static string SdkRoot { public class DotNetLinkContext { public LinkerConfiguration LinkerConfiguration; + public DotNetLinkContext (LinkerConfiguration configuration) + { + this.LinkerConfiguration = configuration; + } + public AssemblyAction UserAction { get { throw new NotImplementedException (); } set { throw new NotImplementedException (); } diff --git a/tools/dotnet-linker/DotNetResolver.cs b/tools/dotnet-linker/DotNetResolver.cs index 84f16349b31a..37e04bb9e1f9 100644 --- a/tools/dotnet-linker/DotNetResolver.cs +++ b/tools/dotnet-linker/DotNetResolver.cs @@ -4,6 +4,8 @@ using Xamarin.Bundler; +#nullable enable + namespace Xamarin.Linker { public class DotNetResolver : CoreResolver { public override AssemblyDefinition Resolve (AssemblyNameReference name, ReaderParameters parameters) diff --git a/tools/dotnet-linker/Extensions.cs b/tools/dotnet-linker/Extensions.cs index 756e071a4f1d..24523465d968 100644 --- a/tools/dotnet-linker/Extensions.cs +++ b/tools/dotnet-linker/Extensions.cs @@ -3,6 +3,8 @@ using Mono.Tuner; using System; +#nullable enable + namespace Xamarin.Linker { public static class Extensions { public static bool Inherits (this TypeReference self, string @namespace, string name, IMetadataResolver resolver) diff --git a/tools/dotnet-linker/LinkerConfiguration.cs b/tools/dotnet-linker/LinkerConfiguration.cs index 1cbd7ffc2e05..1e122675e1f1 100644 --- a/tools/dotnet-linker/LinkerConfiguration.cs +++ b/tools/dotnet-linker/LinkerConfiguration.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; using System.Runtime.CompilerServices; @@ -12,31 +13,31 @@ using Xamarin.Utils; using Xamarin.Tuner; -using ObjCRuntime; +#nullable enable namespace Xamarin.Linker { public class LinkerConfiguration { string LinkerFile; - public List Abis; - public string AOTCompiler; - public string AOTOutputDirectory; - public string CacheDirectory { get; private set; } - public Version DeploymentTarget { get; private set; } + public List Abis = new List (); + public string AOTCompiler = string.Empty; + public string AOTOutputDirectory = string.Empty; + public string CacheDirectory { get; private set; } = string.Empty; + public Version? DeploymentTarget { get; private set; } public HashSet FrameworkAssemblies { get; private set; } = new HashSet (); - public string GlobalizationDataFile { get; private set; } - public string IntermediateLinkDir { get; private set; } + public string GlobalizationDataFile { get; private set; } = string.Empty; + public string IntermediateLinkDir { get; private set; } = string.Empty; public bool InvariantGlobalization { get; private set; } - public string ItemsDirectory { get; private set; } + public string ItemsDirectory { get; private set; } = string.Empty; public bool IsSimulatorBuild { get; private set; } - public string PartialStaticRegistrarLibrary { get; set; } + public string PartialStaticRegistrarLibrary { get; set; } = string.Empty; public ApplePlatform Platform { get; private set; } - public string PlatformAssembly { get; private set; } - public string RelativeAppBundlePath { get; private set; } - public Version SdkVersion { get; private set; } - public string SdkRootDirectory { get; private set; } + public string PlatformAssembly { get; private set; } = string.Empty; + public string RelativeAppBundlePath { get; private set; } = string.Empty; + public Version? SdkVersion { get; private set; } + public string SdkRootDirectory { get; private set; } = string.Empty; public int Verbosity => Driver.Verbosity; - public string XamarinNativeLibraryDirectory { get; private set; } + public string XamarinNativeLibraryDirectory { get; private set; } = string.Empty; static ConditionalWeakTable configurations = new ConditionalWeakTable (); @@ -46,22 +47,28 @@ public class LinkerConfiguration { public IList RegistrationMethods { get; set; } = new List (); public CompilerFlags CompilerFlags; - public LinkContext Context { get; private set; } + LinkContext? context; + public LinkContext Context { get => context!; private set { context = value; } } public DerivedLinkContext DerivedLinkContext { get; private set; } public Profile Profile { get; private set; } // The list of assemblies is populated in CollectAssembliesStep. public List Assemblies = new List (); - string user_optimize_flags; + string? user_optimize_flags; Dictionary> msbuild_items = new Dictionary> (); - internal PInvokeWrapperGenerator PInvokeWrapperGenerationState; + internal PInvokeWrapperGenerator? PInvokeWrapperGenerationState; - public static LinkerConfiguration GetInstance (LinkContext context, bool createIfNotFound = true) + public static bool TryGetInstance (LinkContext context, [NotNullWhen (true)] out LinkerConfiguration? configuration) { - if (!configurations.TryGetValue (context, out var instance) && createIfNotFound) { + return configurations.TryGetValue (context, out configuration); + } + + public static LinkerConfiguration GetInstance (LinkContext context) + { + if (!TryGetInstance (context, out var instance)) { if (!context.TryGetCustomData ("LinkerOptionsFile", out var linker_options_file)) throw new Exception ($"No custom linker options file was passed to the linker (using --custom-data LinkerOptionsFile=..."); instance = new LinkerConfiguration (linker_options_file) { @@ -82,9 +89,9 @@ public static LinkerConfiguration GetInstance (LinkContext context, bool createI LinkerFile = linker_file; Profile = new BaseProfile (this); - DerivedLinkContext = new DerivedLinkContext { LinkerConfiguration = this, }; Application = new Application (this); Target = new Target (Application); + DerivedLinkContext = new DerivedLinkContext (this, Target); CompilerFlags = new CompilerFlags (Target); var use_llvm = false; @@ -266,7 +273,6 @@ public static LinkerConfiguration GetInstance (LinkContext context, bool createI if (!Enum.TryParse (value, out var arch)) throw new InvalidOperationException ($"Unknown target architectures: {value} in {linker_file}"); // Add to the list of Abis as separate entries (instead of a flags enum value), because that way it's easier to enumerate over them. - Abis = new List (); for (var b = 0; b < 32; b++) { var a = (Abi) (1 << b); if ((a & arch) == a) @@ -322,11 +328,13 @@ public static LinkerConfiguration GetInstance (LinkContext context, bool createI Application.CreateCache (significantLines.ToArray ()); Application.Cache.Location = CacheDirectory; - Application.DeploymentTarget = DeploymentTarget; - Application.SdkVersion = SdkVersion; - Application.NativeSdkVersion = SdkVersion; + if (DeploymentTarget is not null) + Application.DeploymentTarget = DeploymentTarget; + if (SdkVersion is not null) { + Application.SdkVersion = SdkVersion; + Application.NativeSdkVersion = SdkVersion; + } - DerivedLinkContext.Target = Target; Target.Abis = Abis; Target.LinkContext = DerivedLinkContext; Application.Abis = Abis; @@ -351,12 +359,14 @@ public static LinkerConfiguration GetInstance (LinkContext context, bool createI Application.Initialize (); } - bool TryParseOptionalBoolean (string input, out bool? value) + bool TryParseOptionalBoolean (string input, [NotNullWhen (true)] out bool? value) { value = null; - if (string.IsNullOrEmpty (input)) + if (string.IsNullOrEmpty (input)) { + value = true; return true; + } if (string.Equals (input, "true", StringComparison.OrdinalIgnoreCase)) { value = true; @@ -480,7 +490,7 @@ public static void Report (LinkContext context, IList exceptions) var list = ErrorHelper.CollectExceptions (exceptions); var allWarnings = list.All (v => v is ProductException pe && !pe.Error); if (!allWarnings) { - var instance = GetInstance (context, false); + TryGetInstance (context, out var instance); var platform = (instance?.Platform)?.ToString () ?? "unknown"; var msg = MessageContainer.CreateCustomErrorMessage (Errors.MX7000 /* An error occured while executing the custom linker steps. Please review the build log for more information. */, 7000, platform); context.LogMessage (msg); @@ -494,4 +504,14 @@ public static void Report (LinkContext context, IList exceptions) public class MSBuildItem { public string Include; public Dictionary Metadata = new Dictionary (); + + public MSBuildItem (string include) + { + Include = include; + } + public MSBuildItem (string include, Dictionary metadata) + { + Include = include; + Metadata = metadata; + } } diff --git a/tools/dotnet-linker/MarkIProtocolHandler.cs b/tools/dotnet-linker/MarkIProtocolHandler.cs index 9c2d3c8b305a..b295ae6d385c 100644 --- a/tools/dotnet-linker/MarkIProtocolHandler.cs +++ b/tools/dotnet-linker/MarkIProtocolHandler.cs @@ -4,6 +4,8 @@ using Mono.Linker; using Mono.Linker.Steps; +#nullable enable + namespace Xamarin.Linker { public class MarkIProtocolHandler : ConfigurationAwareMarkHandler { diff --git a/tools/dotnet-linker/SetupStep.cs b/tools/dotnet-linker/SetupStep.cs index 855d990dfbc6..3d1ecf296cf3 100644 --- a/tools/dotnet-linker/SetupStep.cs +++ b/tools/dotnet-linker/SetupStep.cs @@ -11,6 +11,8 @@ using Xamarin.Linker; using Xamarin.Linker.Steps; +#nullable enable + namespace Xamarin { public class SetupStep : ConfigurationAwareStep { diff --git a/tools/dotnet-linker/Steps/AttributeIteratorBaseStep.cs b/tools/dotnet-linker/Steps/AttributeIteratorBaseStep.cs index a43d28999f63..f7719863a761 100644 --- a/tools/dotnet-linker/Steps/AttributeIteratorBaseStep.cs +++ b/tools/dotnet-linker/Steps/AttributeIteratorBaseStep.cs @@ -7,6 +7,8 @@ using Mono.Cecil; using Xamarin.Tuner; +#nullable enable + namespace Xamarin.Linker.Steps { public abstract class AttributeIteratorBaseStep : BaseSubStep { @@ -50,7 +52,7 @@ public override void ProcessType (TypeDefinition type) void ProcessAttributeProviderCollection (IList list) { for (int i = 0; i < list.Count; i++) - ProcessAttributeProvider ((ICustomAttributeProvider) list [i]); + ProcessAttributeProvider ((ICustomAttributeProvider) list [i]!); } public override void ProcessField (FieldDefinition field) diff --git a/tools/dotnet-linker/Steps/CollectAssembliesStep.cs b/tools/dotnet-linker/Steps/CollectAssembliesStep.cs index dc9d08dd3489..9828787abd75 100644 --- a/tools/dotnet-linker/Steps/CollectAssembliesStep.cs +++ b/tools/dotnet-linker/Steps/CollectAssembliesStep.cs @@ -1,6 +1,8 @@ using System.Collections.Generic; using Mono.Cecil; +#nullable enable + namespace Xamarin.Linker { public class CollectAssembliesStep : ConfigurationAwareStep { protected override string Name { get; } = "Collect Assemblies"; @@ -16,8 +18,8 @@ protected override void TryProcess () // This step now runs at the very beginning, using reflection to call into the linker to load all // the referenced assemblies, so that we can then have another step before MarkStep that does the // custom marking we need to do. - var getReferencedAssemblies = Configuration.Context.GetType ().GetMethod ("GetReferencedAssemblies"); - var assemblies = (IEnumerable) getReferencedAssemblies.Invoke (Configuration.Context, new object [0]); + var getReferencedAssemblies = Configuration.Context.GetType ().GetMethod ("GetReferencedAssemblies")!; + var assemblies = (IEnumerable) getReferencedAssemblies.Invoke (Configuration.Context, new object [0])!; Configuration.Assemblies.AddRange (assemblies); } } diff --git a/tools/dotnet-linker/Steps/CollectUnmarkedMembers.cs b/tools/dotnet-linker/Steps/CollectUnmarkedMembers.cs index ca3a82162a6b..0e257297ead4 100644 --- a/tools/dotnet-linker/Steps/CollectUnmarkedMembers.cs +++ b/tools/dotnet-linker/Steps/CollectUnmarkedMembers.cs @@ -3,6 +3,8 @@ using Mono.Cecil; using Mono.Linker.Steps; +#nullable enable + namespace Xamarin.Linker { // The static registrar may need access to information that has been linked away, // in particular types and interfaces, so we need to store those somewhere diff --git a/tools/dotnet-linker/Steps/ComputeAOTArguments.cs b/tools/dotnet-linker/Steps/ComputeAOTArguments.cs index 8d13589e3cf8..205bb37cd616 100644 --- a/tools/dotnet-linker/Steps/ComputeAOTArguments.cs +++ b/tools/dotnet-linker/Steps/ComputeAOTArguments.cs @@ -3,9 +3,10 @@ using System.Linq; using System.IO; -using Xamarin.Bundler; using Xamarin.Utils; +#nullable enable + namespace Xamarin.Linker { public class ComputeAOTArguments : ConfigurationAwareStep { protected override string Name { get; } = "Compute AOT Arguments"; @@ -25,9 +26,7 @@ protected override void TryEndProcess () if (!isAOTCompiled) continue; - var item = new MSBuildItem { - Include = Path.Combine (Configuration.IntermediateLinkDir, asm.FileName), - }; + var item = new MSBuildItem (Path.Combine (Configuration.IntermediateLinkDir, asm.FileName)); var input = asm.FullPath; var abis = app.Abis.Select (v => v.AsString ()).ToArray (); @@ -38,7 +37,7 @@ protected override void TryEndProcess () var aotData = Path.Combine (outputDirectory, arch, Path.GetFileNameWithoutExtension (input) + ".aotdata"); var llvmFile = Configuration.Application.IsLLVM ? Path.Combine (outputDirectory, arch, Path.GetFileName (input) + ".llvm.o") : string.Empty; var objectFile = Path.Combine (outputDirectory, arch, Path.GetFileName (input) + ".o"); - app.GetAotArguments (asm.FullPath, abi, outputDirectory, aotAssembly, llvmFile, aotData, out var processArguments, out var aotArguments, Path.GetDirectoryName (Configuration.AOTCompiler)); + app.GetAotArguments (asm.FullPath, abi, outputDirectory, aotAssembly, llvmFile, aotData, out var processArguments, out var aotArguments, Path.GetDirectoryName (Configuration.AOTCompiler)!); item.Metadata.Add ("Arguments", StringUtils.FormatArguments (aotArguments)); item.Metadata.Add ("ProcessArguments", StringUtils.FormatArguments (processArguments)); item.Metadata.Add ("Abi", abiString); diff --git a/tools/dotnet-linker/Steps/ComputeNativeBuildFlagsStep.cs b/tools/dotnet-linker/Steps/ComputeNativeBuildFlagsStep.cs index b4a9ae509381..757ce2f6c086 100644 --- a/tools/dotnet-linker/Steps/ComputeNativeBuildFlagsStep.cs +++ b/tools/dotnet-linker/Steps/ComputeNativeBuildFlagsStep.cs @@ -2,6 +2,8 @@ using Xamarin.Utils; +#nullable enable + namespace Xamarin.Linker { // The static registrar may need access to information that has been linked away, // in particular types and interfaces, so we need to store those somewhere @@ -19,10 +21,10 @@ protected override void TryEndProcess () switch (Configuration.Platform) { case ApplePlatform.iOS: case ApplePlatform.MacCatalyst: - linkerFrameworks.Add (new MSBuildItem { - Include = "GSS", - Metadata = { { "IsWeak", "false" } }, - }); + linkerFrameworks.Add (new MSBuildItem ( + "GSS", + new Dictionary { { "IsWeak", "false" } } + )); break; } @@ -34,12 +36,12 @@ protected override void TryEndProcess () if (asm.LinkerFlags == null) continue; foreach (var arg in asm.LinkerFlags) { - var item = new MSBuildItem { - Include = arg, - Metadata = new Dictionary { - { "Assembly", asm.Identity }, - }, - }; + var item = new MSBuildItem ( + arg, + new Dictionary { + { "Assembly", asm.Identity } + } + ); linkerFlags.Add (item); } } @@ -50,7 +52,7 @@ protected override void TryEndProcess () var mainLinkerFlags = new List (); if (Configuration.Application.DeadStrip) { - mainLinkerFlags.Add (new MSBuildItem { Include = "-dead_strip" }); + mainLinkerFlags.Add (new MSBuildItem ("-dead_strip")); } Configuration.WriteOutputForMSBuild ("_AssemblyLinkerFlags", mainLinkerFlags); diff --git a/tools/dotnet-linker/Steps/ConfigurationAwareMarkHandler.cs b/tools/dotnet-linker/Steps/ConfigurationAwareMarkHandler.cs index a00d87a975a4..bad3ea5a55b9 100644 --- a/tools/dotnet-linker/Steps/ConfigurationAwareMarkHandler.cs +++ b/tools/dotnet-linker/Steps/ConfigurationAwareMarkHandler.cs @@ -3,16 +3,18 @@ using Xamarin.Bundler; +#nullable enable + namespace Xamarin.Linker { public abstract class ConfigurationAwareMarkHandler : ExceptionalMarkHandler { protected override void Report (Exception exception) { - LinkerConfiguration.Report (context, exception); + LinkerConfiguration.Report (Context, exception); } protected void Report (List exceptions) { - LinkerConfiguration.Report (context, exceptions); + LinkerConfiguration.Report (Context, exceptions); } } } diff --git a/tools/dotnet-linker/Steps/ConfigurationAwareStep.cs b/tools/dotnet-linker/Steps/ConfigurationAwareStep.cs index 54d984aeed9d..76e0e3c1e97a 100644 --- a/tools/dotnet-linker/Steps/ConfigurationAwareStep.cs +++ b/tools/dotnet-linker/Steps/ConfigurationAwareStep.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; using Mono.Cecil; @@ -7,6 +8,8 @@ using Xamarin.Bundler; +#nullable enable + namespace Xamarin.Linker { public abstract class ConfigurationAwareStep : BaseStep { public LinkerConfiguration Configuration { @@ -67,7 +70,7 @@ protected virtual void TryEndProcess () // failure overrides, with defaults - bool CollectProductExceptions (Exception e, out List productExceptions) + bool CollectProductExceptions (Exception e, [NotNullWhen (true)] out List? productExceptions) { if (e is ProductException pe) { productExceptions = new List (); diff --git a/tools/dotnet-linker/Steps/ConfigurationAwareSubStep.cs b/tools/dotnet-linker/Steps/ConfigurationAwareSubStep.cs index a183b58ce8ef..ac9ae66b04b8 100644 --- a/tools/dotnet-linker/Steps/ConfigurationAwareSubStep.cs +++ b/tools/dotnet-linker/Steps/ConfigurationAwareSubStep.cs @@ -3,6 +3,8 @@ using Xamarin.Bundler; +#nullable enable + namespace Xamarin.Linker { public abstract class ConfigurationAwareSubStep : ExceptionalSubStep { protected override void Report (Exception exception) diff --git a/tools/dotnet-linker/Steps/DoneStep.cs b/tools/dotnet-linker/Steps/DoneStep.cs index add294939b09..5ee53c6b76f3 100644 --- a/tools/dotnet-linker/Steps/DoneStep.cs +++ b/tools/dotnet-linker/Steps/DoneStep.cs @@ -1,3 +1,5 @@ +#nullable enable + namespace Xamarin.Linker { public class DoneStep : ConfigurationAwareStep { protected override string Name { get; } = "Done"; diff --git a/tools/dotnet-linker/Steps/DotNetSubStepDispatcher.cs b/tools/dotnet-linker/Steps/DotNetSubStepDispatcher.cs index c54081c14718..73669ca2c083 100644 --- a/tools/dotnet-linker/Steps/DotNetSubStepDispatcher.cs +++ b/tools/dotnet-linker/Steps/DotNetSubStepDispatcher.cs @@ -1,5 +1,7 @@ using Mono.Linker.Steps; +#nullable enable + namespace Xamarin.Linker.Steps { // SubStepsDispatcher is abstract, so create a subclass we can instantiate class DotNetSubStepDispatcher : SubStepsDispatcher { diff --git a/tools/dotnet-linker/Steps/ExceptionalMarkHandler.cs b/tools/dotnet-linker/Steps/ExceptionalMarkHandler.cs index 93c69a8215da..4eda1e9f18f3 100644 --- a/tools/dotnet-linker/Steps/ExceptionalMarkHandler.cs +++ b/tools/dotnet-linker/Steps/ExceptionalMarkHandler.cs @@ -2,7 +2,6 @@ using System; using Mono.Cecil; -using Mono.Tuner; using Xamarin.Bundler; using Xamarin.Tuner; @@ -10,11 +9,15 @@ using Mono.Linker; using Mono.Linker.Steps; +#nullable enable + namespace Xamarin.Linker { // Similar to ExceptionalSubStep, but this only runs for marked members // that were registered for handling by the subclass. public abstract class ExceptionalMarkHandler : IMarkHandler { + LinkContext? context; + public abstract void Initialize (LinkContext context, MarkContext markContext); public virtual void Initialize (LinkContext context) @@ -24,10 +27,10 @@ public virtual void Initialize (LinkContext context) protected DerivedLinkContext LinkContext => Configuration.DerivedLinkContext; - protected LinkContext context { get; private set; } + protected LinkContext Context { get { return context!; } } - protected AnnotationStore Annotations => context.Annotations; - protected LinkerConfiguration Configuration => LinkerConfiguration.GetInstance (context); + protected AnnotationStore Annotations => Context.Annotations; + protected LinkerConfiguration Configuration => LinkerConfiguration.GetInstance (Context); protected Profile Profile => Configuration.Profile; diff --git a/tools/dotnet-linker/Steps/ExtractBindingLibrariesStep.cs b/tools/dotnet-linker/Steps/ExtractBindingLibrariesStep.cs index 677db68dfa3f..b0da3c72a22e 100644 --- a/tools/dotnet-linker/Steps/ExtractBindingLibrariesStep.cs +++ b/tools/dotnet-linker/Steps/ExtractBindingLibrariesStep.cs @@ -4,6 +4,8 @@ using Xamarin.Utils; +#nullable enable + namespace Xamarin.Linker { public class ExtractBindingLibrariesStep : ConfigurationAwareStep { @@ -22,13 +24,13 @@ protected override void TryEndProcess () var linkWith = new List (); foreach (var asm in Configuration.Target.Assemblies) { foreach (var arg in asm.LinkWith) { - var item = new MSBuildItem { - Include = arg, - Metadata = new Dictionary { + var item = new MSBuildItem ( + arg, + new Dictionary { { "ForceLoad", asm.ForceLoad ? "true" : "false" }, { "Assembly", asm.Identity }, - }, - }; + } + ); linkWith.Add (item); } } @@ -38,22 +40,22 @@ protected override void TryEndProcess () var frameworks = new List (); foreach (var asm in Configuration.Target.Assemblies) { foreach (var fw in asm.Frameworks) { - var item = new MSBuildItem { - Include = fw, - Metadata = new Dictionary { + var item = new MSBuildItem ( + fw, + new Dictionary { { "Assembly", asm.Identity }, - }, - }; + } + ); frameworks.Add (item); } foreach (var fw in asm.WeakFrameworks) { - var item = new MSBuildItem { - Include = fw, - Metadata = new Dictionary { + var item = new MSBuildItem ( + fw, + new Dictionary { { "IsWeak", "true " }, { "Assembly", asm.Identity }, - }, - }; + } + ); frameworks.Add (item); } } @@ -79,9 +81,7 @@ protected override void TryEndProcess () var executable = Path.Combine (fwkDirectory, Path.GetFileNameWithoutExtension (fwkDirectory)); - var item = new MSBuildItem { - Include = executable, - }; + var item = new MSBuildItem (executable); frameworksToPublish.Add (item); } } @@ -93,12 +93,12 @@ protected override void TryEndProcess () if (!arg.EndsWith (".dylib", StringComparison.OrdinalIgnoreCase)) continue; - var item = new MSBuildItem { - Include = arg, - Metadata = new Dictionary { + var item = new MSBuildItem ( + arg, + new Dictionary { { "RelativePath", Path.Combine (Configuration.RelativeAppBundlePath, Configuration.Application.RelativeDylibPublishPath, Path.GetFileName (arg)) }, - }, - }; + } + ); dynamicLibraryToPublish.Add (item); } } diff --git a/tools/dotnet-linker/Steps/GatherFrameworksStep.cs b/tools/dotnet-linker/Steps/GatherFrameworksStep.cs index b2edae63f71e..b47116f1bfa2 100644 --- a/tools/dotnet-linker/Steps/GatherFrameworksStep.cs +++ b/tools/dotnet-linker/Steps/GatherFrameworksStep.cs @@ -5,6 +5,8 @@ using Xamarin.Linker; +#nullable enable + namespace Xamarin { public class GatherFrameworksStep : ConfigurationAwareStep { @@ -40,20 +42,20 @@ protected override void TryEndProcess () // Write out the frameworks we found and pass them to the MSBuild tasks var items = new List (); foreach (var fw in Frameworks.OrderBy (v => v)) { - items.Add (new MSBuildItem { - Include = fw, - Metadata = { + items.Add (new MSBuildItem ( + fw, + new Dictionary { { "IsWeak", "false" }, - }, - }); + } + )); } foreach (var fw in WeakFrameworks.OrderBy (v => v)) { - items.Add (new MSBuildItem { - Include = fw, - Metadata = { + items.Add (new MSBuildItem ( + fw, + new Dictionary { { "IsWeak", "true" }, - }, - }); + } + )); } Configuration.WriteOutputForMSBuild ("_LinkerFrameworks", items); diff --git a/tools/dotnet-linker/Steps/GenerateMainStep.cs b/tools/dotnet-linker/Steps/GenerateMainStep.cs index 07827265cc7e..63908c4c3d57 100644 --- a/tools/dotnet-linker/Steps/GenerateMainStep.cs +++ b/tools/dotnet-linker/Steps/GenerateMainStep.cs @@ -3,7 +3,8 @@ using System.Text; using Xamarin.Linker; -using Xamarin.Utils; + +#nullable enable namespace Xamarin { @@ -46,24 +47,24 @@ protected override void TryEndProcess () Configuration.Target.GenerateMain (contents, app.Platform, abi, file, registration_methods); - var item = new MSBuildItem { - Include = file, - Metadata = { + var item = new MSBuildItem ( + file, + new Dictionary { { "Arch", abi.AsArchString () }, - }, - }; + } + ); if (app.EnableDebug) item.Metadata.Add ("Arguments", "-DDEBUG"); items.Add (item); if (app.RequiresPInvokeWrappers) { - var state = Configuration.PInvokeWrapperGenerationState; - item = new MSBuildItem { - Include = state.SourcePath, - Metadata = { + var state = Configuration.PInvokeWrapperGenerationState!; + item = new MSBuildItem ( + state.SourcePath, + new Dictionary { { "Arch", abi.AsArchString () }, - }, - }; + } + ); if (app.EnableDebug) item.Metadata.Add ("Arguments", "-DDEBUG"); items.Add (item); @@ -75,23 +76,21 @@ protected override void TryEndProcess () var linkWith = new List (); if (Configuration.CompilerFlags.LinkWithLibraries != null) { foreach (var lib in Configuration.CompilerFlags.LinkWithLibraries) { - linkWith.Add (new MSBuildItem { - Include = lib, - }); + linkWith.Add (new MSBuildItem (lib)); } } if (Configuration.CompilerFlags.ForceLoadLibraries != null) { foreach (var lib in Configuration.CompilerFlags.ForceLoadLibraries) { - linkWith.Add (new MSBuildItem { - Include = lib, - Metadata = { + linkWith.Add (new MSBuildItem ( + lib, + new Dictionary { { "ForceLoad", "true" }, - }, - }); + } + )); } } - string extensionlib = null; + string? extensionlib = null; if (app.IsTVExtension) { extensionlib = "libtvextension-dotnet.a"; } else if (app.IsExtension) { @@ -102,12 +101,12 @@ protected override void TryEndProcess () } } if (!string.IsNullOrEmpty (extensionlib)) { - linkWith.Add (new MSBuildItem { - Include = Path.Combine (Configuration.XamarinNativeLibraryDirectory, extensionlib), - Metadata = { + linkWith.Add (new MSBuildItem ( + Path.Combine (Configuration.XamarinNativeLibraryDirectory, extensionlib), + new Dictionary { { "ForceLoad", "true" }, } - }); + )); } Configuration.WriteOutputForMSBuild ("_MainLinkWith", linkWith); diff --git a/tools/dotnet-linker/Steps/GenerateReferencesStep.cs b/tools/dotnet-linker/Steps/GenerateReferencesStep.cs index 1cdae8828843..4ab65c74cfbb 100644 --- a/tools/dotnet-linker/Steps/GenerateReferencesStep.cs +++ b/tools/dotnet-linker/Steps/GenerateReferencesStep.cs @@ -7,6 +7,8 @@ using Xamarin.Bundler; using Xamarin.Linker; +#nullable enable + namespace Xamarin { public class GenerateReferencesStep : ConfigurationAwareStep { @@ -25,17 +27,17 @@ protected override void TryEndProcess () case SymbolMode.Ignore: break; case SymbolMode.Code: - string reference_m = Path.Combine (Configuration.CacheDirectory, "reference.m"); + var reference_m = Path.Combine (Configuration.CacheDirectory, "reference.m"); reference_m = Configuration.Target.GenerateReferencingSource (reference_m, required_symbols); if (!string.IsNullOrEmpty (reference_m)) { - var item = new MSBuildItem { Include = reference_m }; + var item = new MSBuildItem (reference_m); items.Add (item); } Configuration.WriteOutputForMSBuild ("_ReferencesFile", items); break; case SymbolMode.Linker: foreach (var symbol in required_symbols) { - var item = new MSBuildItem { Include = "-u" + symbol.Prefix + symbol.Name }; + var item = new MSBuildItem ("-u" + symbol.Prefix + symbol.Name); items.Add (item); } Configuration.WriteOutputForMSBuild ("_ReferencesLinkerFlags", items); diff --git a/tools/dotnet-linker/Steps/LoadNonSkippedAssembliesStep.cs b/tools/dotnet-linker/Steps/LoadNonSkippedAssembliesStep.cs index c5687a3836ed..366c3b11a7bc 100644 --- a/tools/dotnet-linker/Steps/LoadNonSkippedAssembliesStep.cs +++ b/tools/dotnet-linker/Steps/LoadNonSkippedAssembliesStep.cs @@ -3,6 +3,8 @@ using Mono.Cecil; using Mono.Linker; +#nullable enable + namespace Xamarin.Linker { // List all the assemblies we care about (i.e. the ones that have not been linked away) public class LoadNonSkippedAssembliesStep : ConfigurationAwareStep { diff --git a/tools/dotnet-linker/Steps/MarkDispatcher.cs b/tools/dotnet-linker/Steps/MarkDispatcher.cs index c7bfc4ce0efb..d2c7497d7439 100644 --- a/tools/dotnet-linker/Steps/MarkDispatcher.cs +++ b/tools/dotnet-linker/Steps/MarkDispatcher.cs @@ -1,6 +1,8 @@ using Mono.Linker.Steps; using Xamarin.Linker; +#nullable enable + namespace Xamarin.Linker.Steps { class MarkDispatcher : MarkSubStepsDispatcher { public MarkDispatcher () diff --git a/tools/dotnet-linker/Steps/PostSweepDispatcher.cs b/tools/dotnet-linker/Steps/PostSweepDispatcher.cs index 2209b381900a..bb042e6d9be3 100644 --- a/tools/dotnet-linker/Steps/PostSweepDispatcher.cs +++ b/tools/dotnet-linker/Steps/PostSweepDispatcher.cs @@ -1,6 +1,8 @@ using Mono.Linker.Steps; using Xamarin.Linker; +#nullable enable + namespace Xamarin.Linker.Steps { class PostSweepDispatcher : SubStepsDispatcher { public PostSweepDispatcher () diff --git a/tools/dotnet-linker/Steps/PreMarkDispatcher.cs b/tools/dotnet-linker/Steps/PreMarkDispatcher.cs index 3ddc76d1f849..fa091bf17865 100644 --- a/tools/dotnet-linker/Steps/PreMarkDispatcher.cs +++ b/tools/dotnet-linker/Steps/PreMarkDispatcher.cs @@ -1,6 +1,8 @@ using Mono.Linker.Steps; using Xamarin.Linker; +#nullable enable + namespace Xamarin.Linker.Steps { class PreMarkDispatcher : SubStepsDispatcher { public PreMarkDispatcher () diff --git a/tools/dotnet-linker/Steps/PreOutputDispatcher.cs b/tools/dotnet-linker/Steps/PreOutputDispatcher.cs index 9dbee29cba0d..b9a4eb7ea4ca 100644 --- a/tools/dotnet-linker/Steps/PreOutputDispatcher.cs +++ b/tools/dotnet-linker/Steps/PreOutputDispatcher.cs @@ -1,6 +1,8 @@ using Mono.Linker.Steps; using Xamarin.Linker; +#nullable enable + namespace Xamarin.Linker.Steps { class PreOutputDispatcher : SubStepsDispatcher { public PreOutputDispatcher () diff --git a/tools/dotnet-linker/Steps/PreserveBlockCodeHandler.cs b/tools/dotnet-linker/Steps/PreserveBlockCodeHandler.cs index ffe8bc50a202..96db30d0dd34 100644 --- a/tools/dotnet-linker/Steps/PreserveBlockCodeHandler.cs +++ b/tools/dotnet-linker/Steps/PreserveBlockCodeHandler.cs @@ -9,6 +9,8 @@ using Xamarin.Bundler; +#nullable enable + namespace Xamarin.Linker.Steps { public class PreserveBlockCodeHandler : ConfigurationAwareMarkHandler { @@ -78,8 +80,8 @@ and the Invoke method. return; // The type was used, so preserve the method and field - context.Annotations.Mark (method); - context.Annotations.Mark (field); + Context.Annotations.Mark (method); + Context.Annotations.Mark (field); } } } diff --git a/tools/dotnet-linker/Steps/RegistrarStep.cs b/tools/dotnet-linker/Steps/RegistrarStep.cs index 7ac3a2410b3f..b11e090641b4 100644 --- a/tools/dotnet-linker/Steps/RegistrarStep.cs +++ b/tools/dotnet-linker/Steps/RegistrarStep.cs @@ -6,6 +6,8 @@ using Mono.Cecil; +#nullable enable + namespace Xamarin.Linker { public class RegistrarStep : ConfigurationAwareStep { protected override string Name { get; } = "Registrar"; @@ -40,13 +42,13 @@ protected override void TryEndProcess () var items = new List (); foreach (var abi in Configuration.Abis) { - items.Add (new MSBuildItem { - Include = code, - Metadata = { + items.Add (new MSBuildItem ( + code, + new Dictionary { { "Arch", abi.AsArchString () }, { "Arguments", "-std=c++14" }, - }, - }); + } + )); } Configuration.WriteOutputForMSBuild ("_RegistrarFile", items); diff --git a/tools/dotnet-linker/Steps/RemoveAttributesStep.cs b/tools/dotnet-linker/Steps/RemoveAttributesStep.cs index d6936c764b24..af9acee94086 100644 --- a/tools/dotnet-linker/Steps/RemoveAttributesStep.cs +++ b/tools/dotnet-linker/Steps/RemoveAttributesStep.cs @@ -2,6 +2,8 @@ using Mono.Cecil; +#nullable enable + namespace Xamarin.Linker.Steps { // The .NET linker comes with a way to remove attributes (by passing '--link-attributes // some.xml' as a command-line argument), but this has a few problems: diff --git a/tools/dotnet-linker/Steps/StoreAttributesStep.cs b/tools/dotnet-linker/Steps/StoreAttributesStep.cs index 22aac8e25511..4a93a6bbbd73 100644 --- a/tools/dotnet-linker/Steps/StoreAttributesStep.cs +++ b/tools/dotnet-linker/Steps/StoreAttributesStep.cs @@ -2,6 +2,8 @@ using Mono.Cecil; +#nullable enable + namespace Xamarin.Linker.Steps { // The registrar needs some of the system attributes that the linker might remove, so store those elsewhere for the static registrar's use. public class StoreAttributesStep : AttributeIteratorBaseStep { diff --git a/tools/linker/MonoTouch.Tuner/PreserveSmartEnumConversions.cs b/tools/linker/MonoTouch.Tuner/PreserveSmartEnumConversions.cs index a5c9302641c7..f9c92f6d7fed 100644 --- a/tools/linker/MonoTouch.Tuner/PreserveSmartEnumConversions.cs +++ b/tools/linker/MonoTouch.Tuner/PreserveSmartEnumConversions.cs @@ -61,8 +61,8 @@ public override bool IsActiveFor (AssemblyDefinition assembly) #if NET void Mark (Tuple pair) { - context.Annotations.Mark (pair.Item1); - context.Annotations.Mark (pair.Item2); + Context.Annotations.Mark (pair.Item1); + Context.Annotations.Mark (pair.Item2); } #else void Preserve (Tuple pair, MethodDefinition conditionA, MethodDefinition conditionB = null)