Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,12 @@ public static bool IsFrameworkItem (this ITaskItem item)
item.GetMetadata ("ResolvedFrom") == "{TargetFrameworkDirectory}" ||
item.GetMetadata ("ResolvedFrom") == "ImplicitlyExpandDesignTimeFacades";
}

public static void SetMetadataIfNotSet (this ITaskItem self, string metadata, string value)
{
if (!string.IsNullOrEmpty (self.GetMetadata (metadata)))
return;
self.SetMetadata (metadata, value);
}
}
}
38 changes: 36 additions & 2 deletions msbuild/Xamarin.MacDev.Tasks.Core/Tasks/CodesignTaskBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,40 @@ bool ParseBoolean (ITaskItem item, string metadataName, bool fallbackValue)
return string.Equals (metadataValue, "true", StringComparison.OrdinalIgnoreCase);
}

string ResolvePath (ITaskItem item, string path)
{
if (string.IsNullOrEmpty (path))
return path;

path = PathUtils.ConvertToMacPath (path);
if (Path.IsPathRooted (path))
return path;

var sourceProjectPath = GetNonEmptyStringOrFallback (item, "SourceProjectPath", null);
if (sourceProjectPath is null)
return path;

return Path.Combine (sourceProjectPath, path);
}

string GetCodesignResourceRules (ITaskItem item)
{
var rv = GetNonEmptyStringOrFallback (item, "CodesignResourceRules", out var foundInMetadata, ResourceRules);
// The ResourceRules value is a path, and as such it might be a relative path from a different project, in which case we have to resolve it accordingly.
if (foundInMetadata)
rv = ResolvePath (item, rv);
return rv;
}

string GetCodesignEntitlements (ITaskItem item)
{
var rv = GetNonEmptyStringOrFallback (item, "CodesignEntitlements", out var foundInMetadata, ResourceRules);
// The ResourceRules value is a path, and as such it might be a relative path from a different project, in which case we have to resolve it accordingly.
if (foundInMetadata)
rv = ResolvePath (item, rv);
return rv;
}

IList<string> GenerateCommandLineArguments (ITaskItem item)
{
var args = new List<string> ();
Expand All @@ -163,8 +197,8 @@ IList<string> GenerateCommandLineArguments (ITaskItem item)
var disableTimestamp = ParseBoolean (item, "CodesignDisableTimestamp", DisableTimestamp);
var signingKey = GetNonEmptyStringOrFallback (item, "CodesignSigningKey", SigningKey, "SigningKey", required: true);
var keychain = GetNonEmptyStringOrFallback (item, "CodesignKeychain", Keychain);
var resourceRules = GetNonEmptyStringOrFallback (item, "CodesignResourceRules", ResourceRules);
var entitlements = GetNonEmptyStringOrFallback (item, "CodesignEntitlements", Entitlements);
var resourceRules = GetCodesignResourceRules (item);
var entitlements = GetCodesignEntitlements (item);
var extraArgs = GetNonEmptyStringOrFallback (item, "CodesignExtraArgs", ExtraArgs);

args.Add ("-v");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
using System;
using System.IO;
using System.Collections.Generic;

using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

using Xamarin.Localization.MSBuild;
using Xamarin.Utils;

#nullable enable

namespace Xamarin.MacDev.Tasks {
//
// This task is responsible for computing everything we need to know for code
// signing.
//
// For each app bundle to be signed, a few more items to be code signed:
//
// * All *.dylib and *.metallib files
// * All *.framework directories
//
// In both cases we iterate over what we find in the app bundle instead of
// relying on what the msbuild tasks built and copied in the app bundle,
// because customer logic might have added additional frameworks, and those
// need to be signed too.
//
// This task will also figure out a stamp file path we use to determine if
// something needs (re-)signing.
//
public abstract class ComputeCodesignItemsTaskBase : XamarinTask {

[Required]
public string AppBundleDir { get; set; } = string.Empty;

[Required]
public ITaskItem [] CodesignBundle { get; set; } = Array.Empty<ITaskItem> ();

[Required]
public ITaskItem [] CodesignItems { get; set; } = Array.Empty<ITaskItem> ();

[Required]
public string CodesignStampPath { get; set; } = string.Empty;

[Output]
public ITaskItem[] OutputCodesignItems { get; set; } = Array.Empty<ITaskItem> ();

public override bool Execute ()
{
var output = new List<ITaskItem> ();

// Make sure AppBundleDir has a trailing slash
var appBundlePath = PathUtils.EnsureTrailingSlash (Path.GetFullPath (AppBundleDir));

// Add the app bundles themselves
foreach (var bundle in CodesignBundle) {
// An app bundle is signed if either 'RequireCodeSigning' is true
// or a 'CodesignSigningKey' has been provided.
var requireCodeSigning = bundle.GetMetadata ("RequireCodeSigning");
var codesignSigningKey = bundle.GetMetadata ("CodesignSigningKey");
if (!string.Equals (requireCodeSigning, "true") && string.IsNullOrEmpty (codesignSigningKey))
continue;

// Create a new item for the app bundle, and copy any metadata over.
var bundlePath = Path.Combine (Path.GetDirectoryName (AppBundleDir), bundle.ItemSpec);
var item = new TaskItem (bundlePath);
bundle.CopyMetadataTo (item);

// Compute the stamp file to use
item.SetMetadataIfNotSet ("CodesignStampFile", Path.Combine (bundlePath, CodeSignatureRelativePath, "_CodeSignature", "CodeResources"));

// Get any additional stamp files we must touch when the item is signed.
var additionalStampFiles = new List<string> ();
// We must touch the dSYM directory's Info.plist, to ensure that we don't end up running dsymutil again after codesigning in the next build
var dSYMUtilStampFile = Path.Combine (Path.GetDirectoryName (AppBundleDir), Path.GetFileName (item.ItemSpec) + ".dSYM", "Contents", "Info.plist");
additionalStampFiles.Add (dSYMUtilStampFile);
// Set the CodesignAdditionalFilesToTouch metadata (merge with any existing values)
if (additionalStampFiles.Count > 0) {
additionalStampFiles.AddRange (item.GetMetadata ("CodesignAdditionalFilesToTouch").Split (','));
additionalStampFiles.RemoveAll (v => string.IsNullOrEmpty (v));
item.SetMetadata ("CodesignAdditionalFilesToTouch", string.Join (";", additionalStampFiles));
}

output.Add (item);
}

// Find all:
// - *.dylib and *.metallib files
// - *.framework directories
foreach (var bundle in CodesignBundle) {
var bundlePath = Path.Combine (Path.GetDirectoryName (Path.GetDirectoryName (appBundlePath)), bundle.ItemSpec);
var filesToSign = FindFilesToSign (bundlePath);
foreach (var lib in filesToSign) {
var relativeLib = Path.Combine (AppBundleDir, lib.Substring (appBundlePath.Length));
var item = new TaskItem (relativeLib);
bundle.CopyMetadataTo (item);

// These items must not use the entitlements for the app
item.RemoveMetadata ("CodesignEntitlements");

// These files are a bit special, because they're always signed. This is done
// by setting the signing key to '-' if it's not set.
item.SetMetadataIfNotSet ("CodesignSigningKey", "-");

// Set the stamp file even if already set (because any existing values would be copied from
// the bundle, which would be the wrong stamp file, so it must be overridden)
if (Directory.Exists (relativeLib)) {
item.SetMetadata ("CodesignStampFile", Path.Combine (CodesignStampPath, relativeLib, ".stampfile"));
} else {
item.SetMetadata ("CodesignStampFile", Path.Combine (CodesignStampPath, relativeLib));
}

output.Add (item);
}
}

// Add all additional items
foreach (var item in CodesignItems) {
// Set the stamp file if not already set.
item.SetMetadataIfNotSet ("CodesignStampFile", Path.Combine (CodesignStampPath, item.ItemSpec));

output.Add (item);
}

OutputCodesignItems = output.ToArray ();

return !Log.HasLoggedErrors;
}

string CodeSignatureRelativePath {
get {
switch (Platform) {
case ApplePlatform.iOS:
case ApplePlatform.TVOS:
case ApplePlatform.WatchOS:
return string.Empty;
case ApplePlatform.MacOSX:
case ApplePlatform.MacCatalyst:
return "Contents";
default:
throw new InvalidOperationException (string.Format (MSBStrings.InvalidPlatform, Platform));
}
}
}

IEnumerable<string> FindFilesToSign (string appPath)
{
var rv = new List<string> ();

// Canonicalize the app path, so string comparisons work later on
appPath = PathUtils.ResolveSymbolicLinks (Path.GetFullPath (appPath));

// Make sure path ends with trailing slash to ease logic
appPath = PathUtils.EnsureTrailingSlash (appPath);

string dylibDirectory;
string metallibDirectory;
string frameworksDirectory;
switch (Platform) {
case ApplePlatform.iOS:
case ApplePlatform.TVOS:
case ApplePlatform.WatchOS:
dylibDirectory = appPath;
metallibDirectory = appPath;
frameworksDirectory = Path.Combine (appPath, "Frameworks");
break;
case ApplePlatform.MacOSX:
case ApplePlatform.MacCatalyst:
dylibDirectory = Path.Combine (appPath, "Contents");
metallibDirectory = Path.Combine (appPath, "Contents", "Resources");
frameworksDirectory = Path.Combine (appPath, "Contents", "Frameworks");
break;
default:
throw new InvalidOperationException (string.Format (MSBStrings.InvalidPlatform, Platform));
}

dylibDirectory = PathUtils.EnsureTrailingSlash (dylibDirectory);
metallibDirectory = PathUtils.EnsureTrailingSlash (metallibDirectory);

foreach (var entry in Directory.EnumerateFileSystemEntries (appPath, "*", SearchOption.AllDirectories)) {
var relativePath = entry.Substring (appPath.Length);
// Don't recurse into the PlugIns directory, that's already handled for any app bundle inside the PlugIns directory
if (relativePath.StartsWith ("PlugIns" + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase))
continue;
// Don't recurse into the Watch directory, for the same reason
if (relativePath.StartsWith ("Watch" + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase))
continue;

if (entry.EndsWith (".dylib", StringComparison.OrdinalIgnoreCase) && entry.StartsWith (dylibDirectory, StringComparison.OrdinalIgnoreCase)) {
// We find *.dylibs in any subdirectory
rv.Add (entry);
} else if (entry.EndsWith (".metallib", StringComparison.OrdinalIgnoreCase) && entry.StartsWith (metallibDirectory, StringComparison.OrdinalIgnoreCase)) {
// We find *.metallib in any subdirectory
rv.Add (entry);
} else if (entry.EndsWith (".framework", StringComparison.OrdinalIgnoreCase) && string.Equals (Path.GetDirectoryName (entry), frameworksDirectory, StringComparison.OrdinalIgnoreCase)) {
// We only find *.frameworks inside the Frameworks subdirectory, not recursively
// (not quite sure if this is the right thing to do, but it's what we've been doing so far).
rv.Add (entry);
}
}

return rv;
}
}
}
10 changes: 9 additions & 1 deletion msbuild/Xamarin.MacDev.Tasks.Core/Tasks/XamarinTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,20 @@ protected void FileCopierLogCallback (int min_verbosity, string format, params o
}

protected string GetNonEmptyStringOrFallback (ITaskItem item, string metadataName, string fallbackValue, string fallbackName = null, bool required = false)
{
return GetNonEmptyStringOrFallback (item, metadataName, out var _, fallbackValue, fallbackName, required);
}

protected string GetNonEmptyStringOrFallback (ITaskItem item, string metadataName, out bool foundInMetadata, string fallbackValue, string fallbackName = null, bool required = false)
{
var metadataValue = item.GetMetadata (metadataName);
if (!string.IsNullOrEmpty (metadataValue))
if (!string.IsNullOrEmpty (metadataValue)) {
foundInMetadata = true;
return metadataValue;
}
if (required && string.IsNullOrEmpty (fallbackValue))
Log.LogError (MSBStrings.E7085 /* The "{0}" task was not given a value for the required parameter "{1}", nor was there a "{2}" metadata on the resource {3}. */, GetType ().Name, fallbackName ?? metadataName, metadataName, item.ItemSpec);
foundInMetadata = false;
return fallbackValue;
}
}
Expand Down
32 changes: 32 additions & 0 deletions msbuild/Xamarin.MacDev.Tasks/Tasks/ComputeCodesignItems.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Collections.Generic;
using System.Linq;

using Microsoft.Build.Framework;

using Xamarin.Messaging.Build.Client;

namespace Xamarin.MacDev.Tasks {
public class ComputeCodesignItems : ComputeCodesignItemsTaskBase, ITaskCallback, ICancelableTask {
public override bool Execute ()
{
if (ShouldExecuteRemotely ())
return new TaskRunner (SessionId, BuildEngine4).RunAsync (this).Result;

return base.Execute ();
}

public IEnumerable<ITaskItem> GetAdditionalItemsToBeCopied () => Enumerable.Empty<ITaskItem> ();

// This task does not create or modify any files, and it should only
// deal with files that are already on the mac, so no need to copy any
// files either way.
public bool ShouldCopyToBuildServer (ITaskItem item) => false;
public bool ShouldCreateOutputFile (ITaskItem item) => false;

public void Cancel ()
{
if (ShouldExecuteRemotely ())
BuildConnection.CancelAsync (SessionId, BuildEngine4).Wait ();
}
}
}
Loading