Skip to content

Commit

Permalink
[msbuild] Make the level of parallelism in the codesign task configur…
Browse files Browse the repository at this point in the history
…eable. (#20242)

We currently have an issue with codesigning duplicated files in parallel
(#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: #20193.
  • Loading branch information
rolfbjarne committed Mar 5, 2024
1 parent 6666537 commit 11f676c
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 @@ -1545,4 +1545,13 @@
{0}: the path to a file
</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/Codesign.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public class Codesign : XamarinTask, ITaskCallback, ICancelableTask {
// 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 @@ -101,6 +105,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 @@ -468,7 +482,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) => {
Sign (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

8 comments on commit 11f676c

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

Please sign in to comment.