From 9fbe002cf2c93213f8f914fb8a83cab803d0773b Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Tue, 29 Sep 2020 12:53:13 +0200 Subject: [PATCH] [msbuild] Merge the CodesignNativeLibraries task into the Codesign task. Merge the CodesignNativeLibraries task into the Codesign task, since they're almost identical (so this turns out to be a significant code reduction). * Add a StampPath property to Codesign task, which, if set, specifies if and where a stamp file should be created for each signed file/directory. This replaces the IntermediateOutputPath property from the CodesignNativeLibraries task. * Copy logic to detect if a file/directory needs to be resigned from the CodesignNativeLibraries task to the Codesign task, using any stamp files. * Use the more descriptive error messages E0004/E0005 from the CodesignNativeLibraries task. * The CodesignNativeLibraries task recursed into directories to find *.dylibs and *.metallib files to sign; replace this with MSBuild item group globbing. --- .../Tasks/CodesignTaskBase.cs | 34 ++- msbuild/Xamarin.Shared/Xamarin.Shared.targets | 1 - .../Tasks/CodesignNativeLibrariesTaskBase.cs | 200 ------------------ .../Xamarin.iOS.Common.targets | 35 ++- .../Tasks/CodesignNativeLibraries.cs | 6 - 5 files changed, 58 insertions(+), 218 deletions(-) delete mode 100644 msbuild/Xamarin.iOS.Tasks.Core/Tasks/CodesignNativeLibrariesTaskBase.cs delete mode 100644 msbuild/Xamarin.iOS.Tasks/Tasks/CodesignNativeLibraries.cs diff --git a/msbuild/Xamarin.MacDev.Tasks.Core/Tasks/CodesignTaskBase.cs b/msbuild/Xamarin.MacDev.Tasks.Core/Tasks/CodesignTaskBase.cs index e3ef5f9b24c1..856eb77004e6 100644 --- a/msbuild/Xamarin.MacDev.Tasks.Core/Tasks/CodesignTaskBase.cs +++ b/msbuild/Xamarin.MacDev.Tasks.Core/Tasks/CodesignTaskBase.cs @@ -23,6 +23,8 @@ public abstract class CodesignTaskBase : XamarinTask #region Inputs + public string StampPath { get; set; } + [Required] public string CodesignAllocate { get; set; } @@ -86,6 +88,27 @@ ProcessStartInfo GetProcessStartInfo (string tool, string args) return startInfo; } + string GetOutputPath (ITaskItem item) + { + return Path.Combine (StampPath, item.ItemSpec); + } + + bool NeedsCodesign (ITaskItem item) + { + if (string.IsNullOrEmpty (StampPath)) + return true; + + var output = GetOutputPath (item); + + if (!File.Exists (output)) + return true; + + if (File.GetLastWriteTimeUtc (item.ItemSpec) >= File.GetLastWriteTimeUtc (output)) + return true; + + return false; + } + string GenerateCommandLineArguments (ITaskItem item) { var args = new CommandLineArgumentBuilder (); @@ -164,9 +187,13 @@ void Codesign (ITaskItem item) if (exitCode != 0) { if (errors.Length > 0) - Log.LogError (null, null, null, item.ItemSpec, 0, 0, 0, 0, "{0}", errors); + Log.LogError (MSBStrings.E0004, item.ItemSpec, errors); else - Log.LogError (null, null, null, item.ItemSpec, 0, 0, 0, 0, MSBStrings.E0098, startInfo.FileName); + Log.LogError (MSBStrings.E0005, item.ItemSpec); + } else if (!string.IsNullOrEmpty (StampPath)) { + var outputPath = GetOutputPath (item); + Directory.CreateDirectory (Path.GetDirectoryName (outputPath)); + File.WriteAllText (outputPath, string.Empty); } } @@ -176,8 +203,9 @@ public override bool Execute () return true; var codesignedFiles = new List (); + var resourcesToSign = Resources.Where (v => NeedsCodesign (v)); - Parallel.ForEach (Resources, new ParallelOptions { MaxDegreeOfParallelism = Math.Max (Environment.ProcessorCount / 2, 1) }, (item) => { + Parallel.ForEach (resourcesToSign, new ParallelOptions { MaxDegreeOfParallelism = Math.Max (Environment.ProcessorCount / 2, 1) }, (item) => { Codesign (item); var files = GetCodesignedFiles (item); diff --git a/msbuild/Xamarin.Shared/Xamarin.Shared.targets b/msbuild/Xamarin.Shared/Xamarin.Shared.targets index a24be44ed69f..e77deaa5b675 100644 --- a/msbuild/Xamarin.Shared/Xamarin.Shared.targets +++ b/msbuild/Xamarin.Shared/Xamarin.Shared.targets @@ -38,7 +38,6 @@ Copyright (C) 2018 Microsoft. All rights reserved. - diff --git a/msbuild/Xamarin.iOS.Tasks.Core/Tasks/CodesignNativeLibrariesTaskBase.cs b/msbuild/Xamarin.iOS.Tasks.Core/Tasks/CodesignNativeLibrariesTaskBase.cs deleted file mode 100644 index f9c374b38055..000000000000 --- a/msbuild/Xamarin.iOS.Tasks.Core/Tasks/CodesignNativeLibrariesTaskBase.cs +++ /dev/null @@ -1,200 +0,0 @@ -using System; -using System.IO; -using System.Text; -using System.Diagnostics; -using System.Collections.Generic; - -using Parallel = System.Threading.Tasks.Parallel; -using ParallelOptions = System.Threading.Tasks.ParallelOptions; - -using Microsoft.Build.Framework; -using Microsoft.Build.Utilities; - -using Xamarin.MacDev; -using Xamarin.MacDev.Tasks; -using Xamarin.Localization.MSBuild; - -namespace Xamarin.iOS.Tasks -{ - public abstract class CodesignNativeLibrariesTaskBase : XamarinTask - { - const string ToolName = "codesign"; - string toolExe; - - #region Inputs - - [Required] - public string AppBundleDir { get; set; } - - [Required] - public string IntermediateOutputPath { get; set; } - - [Required] - public string CodesignAllocate { get; set; } - - public bool DisableTimestamp { get; set; } - - public string Keychain { get; set; } - - [Required] - public string SigningKey { get; set; } - - public string ExtraArgs { get; set; } - - public string ToolExe { - get { return toolExe ?? ToolName; } - set { toolExe = value; } - } - - public string ToolPath { get; set; } - - #endregion - - string GetFullPathToTool () - { - if (!string.IsNullOrEmpty (ToolPath)) - return Path.Combine (ToolPath, ToolExe); - - var path = Path.Combine ("/usr/bin", ToolExe); - - return File.Exists (path) ? path : ToolExe; - } - - ProcessStartInfo GetProcessStartInfo (string tool, string args) - { - var startInfo = new ProcessStartInfo (tool, args); - - startInfo.WorkingDirectory = Environment.CurrentDirectory; - startInfo.EnvironmentVariables["CODESIGN_ALLOCATE"] = CodesignAllocate; - - startInfo.CreateNoWindow = true; - - return startInfo; - } - - string GenerateCommandLineArguments (string dylib) - { - var args = new CommandLineArgumentBuilder (); - - args.Add ("-v"); - args.Add ("--force"); - - args.Add ("--sign"); - args.AddQuoted (SigningKey); - - if (!string.IsNullOrEmpty (Keychain)) { - args.Add ("--keychain"); - args.AddQuoted (Path.GetFullPath (Keychain)); - } - - if (DisableTimestamp) - args.Add ("--timestamp=none"); - - if (!string.IsNullOrEmpty (ExtraArgs)) - args.Add (ExtraArgs); - - args.AddQuoted (dylib); - - return args.ToString (); - } - - void Codesign (string dylib) - { - var startInfo = GetProcessStartInfo (GetFullPathToTool (), GenerateCommandLineArguments (dylib)); - var messages = new StringBuilder (); - var errors = new StringBuilder (); - int exitCode; - - try { - Log.LogMessage (MessageImportance.Normal, MSBStrings.M0001, startInfo.FileName, startInfo.Arguments); - - using (var stdout = new StringWriter (messages)) { - using (var stderr = new StringWriter (errors)) { - var process = ProcessUtils.StartProcess (startInfo, stdout, stderr); - - process.Wait (); - - exitCode = process.Result; - } - - Log.LogMessage (MessageImportance.Low, MSBStrings.M0002, startInfo.FileName, exitCode); - } - } catch (Exception ex) { - Log.LogError (MSBStrings.E0003, startInfo.FileName, ex.Message); - return; - } - - if (messages.Length > 0) - Log.LogMessage (MessageImportance.Normal, "{0}", messages.ToString ()); - - if (exitCode != 0) { - if (errors.Length > 0) - Log.LogError (MSBStrings.E0004, dylib, errors); - else - Log.LogError (MSBStrings.E0005, dylib); - } else { - var output = GetOutputPath (dylib); - var dir = Path.GetDirectoryName (output); - - Directory.CreateDirectory (dir); - - File.WriteAllText (output, string.Empty); - } - } - - string GetOutputPath (string path) - { - var rpath = PathUtils.AbsoluteToRelative (AppBundleDir, path); - - return Path.Combine (IntermediateOutputPath, rpath); - } - - bool NeedsCodesign (string path) - { - var output = GetOutputPath (path); - - return !File.Exists (output) || File.GetLastWriteTimeUtc (path) >= File.GetLastWriteTimeUtc (output); - } - - public override bool Execute () - { - if (!Directory.Exists (AppBundleDir)) - return true; - - var subdirs = new List (); - var dylibs = new List (); - - foreach (var path in Directory.EnumerateFileSystemEntries (AppBundleDir)) { - if (Directory.Exists (path)) { - var name = Path.GetFileName (path); - - if (name != "PlugIns" && name != "Watch") - subdirs.Add (path); - } else { - if (path.EndsWith (".metallib", StringComparison.Ordinal) || path.EndsWith (".dylib", StringComparison.Ordinal)) { - if (NeedsCodesign (path)) - dylibs.Add (path); - } - } - } - - foreach (var subdir in subdirs) { - foreach (var dylib in Directory.EnumerateFiles (subdir, "*.*", SearchOption.AllDirectories)) { - if (dylib.EndsWith (".metallib", StringComparison.Ordinal) || dylib.EndsWith (".dylib", StringComparison.Ordinal)) { - if (NeedsCodesign (dylib)) - dylibs.Add (dylib); - } - } - } - - if (dylibs.Count == 0) - return true; - - Parallel.ForEach (dylibs, new ParallelOptions { MaxDegreeOfParallelism = Math.Max (Environment.ProcessorCount / 2, 1) }, (dylib) => { - Codesign (dylib); - }); - - return !Log.HasLoggedErrors; - } - } -} diff --git a/msbuild/Xamarin.iOS.Tasks.Core/Xamarin.iOS.Common.targets b/msbuild/Xamarin.iOS.Tasks.Core/Xamarin.iOS.Common.targets index dadb02589758..3fbd7f1684aa 100644 --- a/msbuild/Xamarin.iOS.Tasks.Core/Xamarin.iOS.Common.targets +++ b/msbuild/Xamarin.iOS.Tasks.Core/Xamarin.iOS.Common.targets @@ -1383,20 +1383,27 @@ Copyright (C) 2013-2016 Xamarin. All rights reserved. <_LibraryCodeSigningKey Condition="'$(_LibraryCodeSigningKey)' == ''">- - + <_CodesignNativeLibrary + Include="$(_AppBundlePath)\**\*.dylib;$(_AppBundlePath)\**\*.metallib" + Exclude="$(_AppBundlePath)\Watch\**;$(_AppBundlePath)\PlugIns\**" + /> + + + - + - + <_CodesignAppExtensionsLibraryPath>$(_AppBundlePath)PlugIns\%(_AppExtensionCodesignProperties.Identity)\ + + + + <_CodesignAppExtensionsLibrary + Include="$(_CodesignAppExtensionsLibraryPath)**\*.dylib;$(_CodesignAppExtensionsLibraryPath)**\*.metallib" + Exclude="$(_CodesignAppExtensionsLibraryPath)Watch\**;$(_CodesignAppExtensionsLibraryPath)PlugIns\**" + > + + + + - +