Skip to content

Commit

Permalink
[release/8.0.1xx-xcode15.1] [msbuild] Make the level of parallelism i…
Browse files Browse the repository at this point in the history
…n the codesign task configureable.

We currently have an issue with codesigning duplicated files in parallel
(xamarin#20193). The proper fix is somewhat complex, so this implements a simple
workaround for customers, where they can just disable parallelism if they run
into this problem (and since it's simple, it's easier to backport too).

Additionally, it's not a bad idea to be able to configure the level of parallelism.

Ref: xamarin#20193.

Backport of xamarin#20242.
  • Loading branch information
rolfbjarne committed Mar 5, 2024
1 parent 2cb9d66 commit 37216f6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
9 changes: 9 additions & 0 deletions msbuild/Xamarin.Localization.MSBuild/MSBStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1533,4 +1533,13 @@
{0}: the path to an assembly
</comment>
</data>

<data name="W7121" xml:space="preserve">
<value>Unable to parse the value '{0}' for the property 'MaxDegreeOfParallelism'. Falling back to the default value (number of processors / 2).</value>
<comment>
{0}: a developer-supplied property value.
MaxDegreeOfParallelism: name of the property (do not translate)
</comment>
</data>

</root>
16 changes: 15 additions & 1 deletion msbuild/Xamarin.MacDev.Tasks/Tasks/CodesignTaskBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public abstract class CodesignTaskBase : XamarinTask {
// Can also be specified per resource using the 'CodesignDeep' metadata (yes, the naming difference is correct and due to historical reasons)
public bool IsAppExtension { get; set; }

// How many codesign tasks we execute in parallel. Default is number of processors / 2.
// Contrary to many of the other codesigning properties, this can not be set per resource.
public string MaxDegreeOfParallelism { get; set; }

// Can also be specified per resource using the 'CodesignUseHardenedRuntime' metadata
public bool UseHardenedRuntime { get; set; }

Expand Down Expand Up @@ -96,6 +100,16 @@ string GetCodesignAllocate (ITaskItem item)
return GetNonEmptyStringOrFallback (item, "CodesignAllocate", CodesignAllocate, "CodesignAllocate", required: true);
}

int GetMaxDegreeOfParallelism ()
{
if (!string.IsNullOrEmpty (MaxDegreeOfParallelism)) {
if (int.TryParse (MaxDegreeOfParallelism, out var max))
return max;
Log.LogWarning (MSBStrings.W7121 /* Unable to parse the value '{0}' for the property 'MaxDegreeOfParallelism'. Falling back to the default value (number of processors / 2). */, MaxDegreeOfParallelism);
}
return Math.Max (Environment.ProcessorCount / 2, 1);
}

// 'sortedItems' is sorted by length of path, longest first.
bool NeedsCodesign (ITaskItem [] sortedItems, int index, string stampFileContents)
{
Expand Down Expand Up @@ -460,7 +474,7 @@ bool ExecuteUnsafe ()

for (var b = 0; b < buckets.Count; b++) {
var bucket = buckets [b];
Parallel.ForEach (bucket, new ParallelOptions { MaxDegreeOfParallelism = Math.Max (Environment.ProcessorCount / 2, 1) }, (item) => {
Parallel.ForEach (bucket, new ParallelOptions { MaxDegreeOfParallelism = GetMaxDegreeOfParallelism () }, (item) => {
Codesign (item);
var files = GetCodesignedFiles (item.Item);
Expand Down
1 change: 1 addition & 0 deletions msbuild/Xamarin.Shared/Xamarin.Shared.targets
Original file line number Diff line number Diff line change
Expand Up @@ -2257,6 +2257,7 @@ Copyright (C) 2018 Microsoft. All rights reserved.
<Codesign
SessionId="$(BuildSessionId)"
Condition="'$(IsMacEnabled)' == 'true'"
MaxDegreeOfParallelism="$(CodesignMaxDegreeOfParallelism)"
Resources="@(_ComputedCodesignItems)"
ToolExe="$(CodesignExe)"
ToolPath="$(CodesignPath)"
Expand Down

0 comments on commit 37216f6

Please sign in to comment.