Skip to content

Commit

Permalink
[msbuild] Improve the Strip task.
Browse files Browse the repository at this point in the history
* Enable nullability and fix code accordingly.
* Augment it to be able to take multiple files to strip at the same time.
* Strip in parallel.
* Execute using xcrun (ref: xamarin#3931)
* Pass the full path to the executable file to strip, to make command lines
  easier to copy-paste.
* Remove test that is now outdated. We have other tests that run strip
  anyways, so this shouldn't be a problem.
  • Loading branch information
rolfbjarne committed Mar 8, 2022
1 parent 0897150 commit 72bbc02
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 76 deletions.
10 changes: 0 additions & 10 deletions msbuild/Xamarin.MacDev.Tasks.Core/Tasks/CodesignTaskBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,6 @@ bool ParseBoolean (ITaskItem item, string metadataName, bool fallbackValue)
return string.Equals (metadataValue, "true", StringComparison.OrdinalIgnoreCase);
}

string GetNonEmptyStringOrFallback (ITaskItem item, string metadataName, string fallbackValue, string fallbackName = null, bool required = false)
{
var metadataValue = item.GetMetadata (metadataName);
if (!string.IsNullOrEmpty (metadataValue))
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}. */, "Codesign", fallbackName, metadataName, item.ItemSpec);
return fallbackValue;
}

IList<string> GenerateCommandLineArguments (ITaskItem item)
{
var args = new List<string> ();
Expand Down
65 changes: 34 additions & 31 deletions msbuild/Xamarin.MacDev.Tasks.Core/Tasks/SymbolStripTaskBase.cs
Original file line number Diff line number Diff line change
@@ -1,64 +1,67 @@
using System;
using System.IO;
using System.Collections.Generic;

using Parallel = System.Threading.Tasks.Parallel;
using ParallelOptions = System.Threading.Tasks.ParallelOptions;

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

#nullable enable

namespace Xamarin.MacDev.Tasks
{
public abstract class SymbolStripTaskBase : XamarinToolTask
public abstract class SymbolStripTaskBase : XamarinTask
{
#region Inputs

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

public string SymbolFile { get; set; }
// This can also be specified as metadata on the Executable item (as 'SymbolFile')
public string SymbolFile { get; set; } = string.Empty;

[Required]
// This can also be specified as metadata on the Executable item (as 'IsFramework')
public bool IsFramework { get; set; }

#endregion

protected override string ToolName {
get { return "strip"; }
}

protected override string GenerateFullPathToTool ()
bool GetIsFramework (ITaskItem item)
{
if (!string.IsNullOrEmpty (ToolPath))
return Path.Combine (ToolPath, ToolExe);

var path = Path.Combine (AppleSdkSettings.DeveloperRoot, "Toolchains", "XcodeDefault.xctoolchain", "usr", "bin", ToolExe);

return File.Exists (path) ? path : ToolExe;
var value = GetNonEmptyStringOrFallback (item, "IsFramework", IsFramework ? "true" : "false", required: true);
return string.Equals (value, "true", StringComparison.OrdinalIgnoreCase);
}

protected override string GenerateCommandLineCommands ()
void ExecuteStrip (ITaskItem item)
{
var args = new CommandLineBuilder ();
var args = new List<string> ();

if (!string.IsNullOrEmpty (SymbolFile)) {
args.AppendSwitch ("-i");
args.AppendSwitch ("-s");
args.AppendFileNameIfNotNull (SymbolFile);
args.Add ("strip");

var symbolFile = GetNonEmptyStringOrFallback (item, "SymbolFile", SymbolFile);
if (!string.IsNullOrEmpty (symbolFile)) {
args.Add ("-i");
args.Add ("-s");
args.Add (symbolFile);
}

if (IsFramework) {
if (GetIsFramework (item)) {
// Only remove debug symbols from frameworks.
args.AppendSwitch ("-S");
args.AppendSwitch ("-x");
args.Add ("-S");
args.Add ("-x");
}

args.AppendFileNameIfNotNull (Executable);
args.Add (Path.GetFullPath (item.ItemSpec));

return args.ToString ();
ExecuteAsync ("xcrun", args).Wait ();
}

protected override void LogEventsFromTextOutput (string singleLine, MessageImportance messageImportance)
public override bool Execute ()
{
// TODO: do proper parsing of error messages and such
Log.LogMessage (messageImportance, "{0}", singleLine);
Parallel.ForEach (Executable, new ParallelOptions { MaxDegreeOfParallelism = Math.Max (Environment.ProcessorCount / 2, 1) }, (item) => {
ExecuteStrip (item);
});

return !Log.HasLoggedErrors;
}
}
}
10 changes: 10 additions & 0 deletions msbuild/Xamarin.MacDev.Tasks.Core/Tasks/XamarinTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,15 @@ protected void FileCopierLogCallback (int min_verbosity, string format, params o
}
Log.LogMessage (importance, format, arguments);
}

protected string GetNonEmptyStringOrFallback (ITaskItem item, string metadataName, string fallbackValue, string fallbackName = null, bool required = false)
{
var metadataValue = item.GetMetadata (metadataName);
if (!string.IsNullOrEmpty (metadataValue))
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);
return fallbackValue;
}
}
}
11 changes: 2 additions & 9 deletions msbuild/Xamarin.MacDev.Tasks/Tasks/SymbolStrip.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Build.Framework;
using Xamarin.Messaging.Build.Client;

#nullable enable

namespace Xamarin.MacDev.Tasks
{
public class SymbolStrip : SymbolStripTaskBase
Expand All @@ -12,13 +13,5 @@ public override bool Execute ()

return base.Execute ();
}

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

base.Execute ();
}
}
}

This file was deleted.

0 comments on commit 72bbc02

Please sign in to comment.