Skip to content

Commit 0240940

Browse files
committed
[msbuild] Sign simulator apps by default. Fixes #18469.
Fixes #18469.
1 parent 91ffc65 commit 0240940

File tree

16 files changed

+120
-164
lines changed

16 files changed

+120
-164
lines changed

msbuild/Xamarin.MacDev.Tasks/Tasks/Codesign.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,11 @@ bool ExecuteUnsafe ()
426426
var info = new SignInfo { Item = item };
427427
if (!Validate (info))
428428
continue;
429-
if (NeedsCodesign (resourcesToSign, i, info.GetStampFileContents (this)))
429+
if (NeedsCodesign (resourcesToSign, i, info.GetStampFileContents (this))) {
430430
itemsToSign.Add (info);
431+
} else {
432+
resourcesToSign [i] = null;
433+
}
431434
}
432435

433436
if (Log.HasLoggedErrors)

msbuild/Xamarin.MacDev.Tasks/Tasks/CompileEntitlements.cs

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,22 @@ protected string EntitlementBundlePath {
138138
}
139139
}
140140

141+
bool IsDeviceOrDesktop {
142+
get {
143+
switch (Platform) {
144+
case ApplePlatform.iOS:
145+
case ApplePlatform.TVOS:
146+
case ApplePlatform.WatchOS:
147+
return !SdkIsSimulator;
148+
case ApplePlatform.MacOSX:
149+
case ApplePlatform.MacCatalyst:
150+
return true;
151+
default:
152+
throw new InvalidOperationException (string.Format (MSBStrings.InvalidPlatform, Platform));
153+
}
154+
}
155+
}
156+
141157
PString MergeEntitlementString (PString pstr, MobileProvision? profile, bool expandWildcards, string? key)
142158
{
143159
string TeamIdentifierPrefix;
@@ -146,7 +162,7 @@ PString MergeEntitlementString (PString pstr, MobileProvision? profile, bool exp
146162
if (string.IsNullOrEmpty (pstr.Value))
147163
return (PString) pstr.Clone ();
148164

149-
if (profile is null) {
165+
if (profile is null && IsDeviceOrDesktop) {
150166
if (!warnedTeamIdentifierPrefix && pstr.Value.Contains ("$(TeamIdentifierPrefix)")) {
151167
Log.LogWarning (null, null, null, Entitlements, 0, 0, 0, 0, MSBStrings.W0108b /* Cannot expand $(TeamIdentifierPrefix) in Entitlements.plist without a provisioning profile for key '{0}' with value '{1}' */, key, pstr.Value);
152168
warnedTeamIdentifierPrefix = true;
@@ -456,7 +472,7 @@ public override bool Execute ()
456472
MobileProvision? profile;
457473
PDictionary template;
458474
PDictionary compiled;
459-
PDictionary archived;
475+
PDictionary? archived = null;
460476
string path;
461477

462478
switch (SdkPlatform) {
@@ -509,7 +525,27 @@ public override bool Execute ()
509525
}
510526

511527
compiled = GetCompiledEntitlements (profile, template);
512-
archived = GetArchivedExpandedEntitlements (template, compiled);
528+
529+
Directory.CreateDirectory (Path.GetDirectoryName (CompiledEntitlements!.ItemSpec));
530+
531+
if (SdkIsSimulator) {
532+
var simulatedEntitlements = compiled;
533+
var simulatedXcent = Path.ChangeExtension (CompiledEntitlements.ItemSpec, "").TrimEnd ('.') + "-Simulated.xcent";
534+
try {
535+
WriteXcent (simulatedEntitlements, simulatedXcent);
536+
} catch (Exception ex) {
537+
Log.LogError (MSBStrings.E0114, simulatedXcent, ex.Message);
538+
return false;
539+
}
540+
541+
EntitlementsInExecutable = new TaskItem (simulatedXcent);
542+
543+
// No matter what, I've only been able to make Xcode apply a single entitlement to simulator builds: com.apple.security.get-task-allow
544+
compiled = new PDictionary ();
545+
compiled.Add ("com.apple.security.get-task-allow", new PBoolean (true));
546+
} else {
547+
archived = GetArchivedExpandedEntitlements (template, compiled);
548+
}
513549

514550
try {
515551
Directory.CreateDirectory (Path.GetDirectoryName (CompiledEntitlements!.ItemSpec));
@@ -519,17 +555,10 @@ public override bool Execute ()
519555
return false;
520556
}
521557

522-
SaveArchivedExpandedEntitlements (archived);
558+
if (archived is not null)
559+
SaveArchivedExpandedEntitlements (archived);
523560

524-
if (Platform == Utils.ApplePlatform.MacCatalyst) {
525-
EntitlementsInSignature = CompiledEntitlements;
526-
} else if (SdkIsSimulator) {
527-
if (compiled.Count > 0) {
528-
EntitlementsInExecutable = CompiledEntitlements;
529-
}
530-
} else {
531-
EntitlementsInSignature = CompiledEntitlements;
532-
}
561+
EntitlementsInSignature = CompiledEntitlements;
533562

534563
return !Log.HasLoggedErrors;
535564
}

msbuild/Xamarin.MacDev.Tasks/Tasks/DetectSigningIdentity.cs

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -591,64 +591,7 @@ public override bool Execute ()
591591
return !Log.HasLoggedErrors;
592592
}
593593
} else {
594-
// Framework is either iOS, tvOS or watchOS
595-
if (SdkIsSimulator) {
596-
if (AppleSdkSettings.XcodeVersion.Major >= 8 && RequireProvisioningProfile) {
597-
// Note: Starting with Xcode 8.0, we need to codesign iOS Simulator builds that enable Entitlements
598-
// in order for them to run. The "-" key is a special value allowed by the codesign utility that
599-
// allows us to get away with not having an actual codesign key.
600-
DetectedCodeSigningKey = "-";
601-
602-
if (!IsAutoCodeSignProfile (ProvisioningProfile)) {
603-
identity.Profile = MobileProvisionIndex.GetMobileProvision (platform, ProvisioningProfile);
604-
605-
if (identity.Profile is null) {
606-
Log.LogError (MSBStrings.E0140, PlatformName, ProvisioningProfile);
607-
return false;
608-
}
609-
610-
identity.AppId = ConstructValidAppId (identity.Profile, identity.BundleId);
611-
if (identity.AppId is null) {
612-
Log.LogError (MSBStrings.E0141, identity.BundleId, ProvisioningProfile);
613-
return false;
614-
}
615-
616-
provisioningProfileName = identity.Profile.Name;
617-
618-
DetectedProvisioningProfile = identity.Profile.Uuid;
619-
DetectedDistributionType = identity.Profile.DistributionType.ToString ();
620-
} else {
621-
certs = new X509Certificate2 [0];
622-
623-
if ((profiles = GetProvisioningProfiles (platform, type, identity, certs)) is null)
624-
return false;
625-
626-
if ((pairs = GetCodeSignIdentityPairs (profiles, certs)) is null)
627-
return false;
628-
629-
var match = GetBestMatch (pairs, identity);
630-
identity.Profile = match.Profile;
631-
identity.AppId = match.AppId;
632-
633-
if (identity.Profile is not null) {
634-
DetectedDistributionType = identity.Profile.DistributionType.ToString ();
635-
DetectedProvisioningProfile = identity.Profile.Uuid;
636-
provisioningProfileName = identity.Profile.Name;
637-
}
638-
639-
DetectedAppId = identity.AppId;
640-
}
641-
} else {
642-
// Note: Do not codesign. Codesigning seems to break the iOS Simulator in older versions of Xcode.
643-
DetectedCodeSigningKey = null;
644-
}
645-
646-
ReportDetectedCodesignInfo ();
647-
648-
return !Log.HasLoggedErrors;
649-
}
650-
651-
if (!SdkIsSimulator && !RequireCodeSigning) {
594+
if (SdkIsSimulator || !RequireCodeSigning) {
652595
// The "-" key is a special value allowed by the codesign utility that
653596
// allows us to get away with not having an actual codesign key.
654597
DetectedCodeSigningKey = "-";

msbuild/Xamarin.MacDev.Tasks/Tasks/LinkNativeCode.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ bool ExecuteUnsafe ()
203203
foreach (var obj in ObjectFiles)
204204
arguments.Add (Path.GetFullPath (obj.ItemSpec));
205205

206-
arguments.AddRange (GetEmbedEntitlementsInExecutableLinkerFlags (EntitlementsInExecutable));
206+
arguments.AddRange (GetEmbedEntitlementsWithDerInExecutableLinkerFlags (EntitlementsInExecutable));
207207

208208
arguments.Add ("-o");
209209
arguments.Add (Path.GetFullPath (OutputFile));
@@ -243,6 +243,20 @@ bool ExecuteUnsafe ()
243243
return !Log.HasLoggedErrors;
244244
}
245245

246+
IEnumerable<string> GetEmbedEntitlementsWithDerInExecutableLinkerFlags (string entitlements)
247+
{
248+
var rv = GetEmbedEntitlementsInExecutableLinkerFlags (entitlements).ToList ();
249+
if (rv.Count > 0) {
250+
rv.AddRange (new string [] {
251+
"-Xlinker", "-sectcreate",
252+
"-Xlinker", "__TEXT",
253+
"-Xlinker", "__ents_der",
254+
"-Xlinker", ConvertEntitlementsToDerEntitlements (Path.GetFullPath (entitlements)),
255+
});
256+
}
257+
return rv;
258+
}
259+
246260
public static string [] GetEmbedEntitlementsInExecutableLinkerFlags (string entitlements)
247261
{
248262
if (string.IsNullOrEmpty (entitlements))
@@ -259,6 +273,21 @@ public static string [] GetEmbedEntitlementsInExecutableLinkerFlags (string enti
259273
};
260274
}
261275

276+
string ConvertEntitlementsToDerEntitlements (string entitlements)
277+
{
278+
var derEntitlements = entitlements + ".der";
279+
var arguments = new List<string> () {
280+
"derq",
281+
"query",
282+
"-f", "xml",
283+
"-i", entitlements,
284+
"-o", derEntitlements,
285+
"--raw",
286+
};
287+
ExecuteAsync ("xcrun", arguments, sdkDevPath: SdkDevPath).Wait ();
288+
return derEntitlements;
289+
}
290+
262291
static bool EntitlementsRequireLinkerFlags (string path)
263292
{
264293
try {

msbuild/Xamarin.Shared/Xamarin.Shared.props

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,12 @@ Copyright (C) 2020 Microsoft. All rights reserved.
143143
</PropertyGroup>
144144

145145
<!-- RequireCodeSigning -->
146-
<!-- iOS/watchOS/tvOS is simple: device builds require code signing, simulator builds do not. This is a big lie, for some simulator builds need to be signed, but the _DetectCodeSigning task handles those cases. -->
146+
<!-- iOS/watchOS/tvOS is simple: device builds require code signing, simulator builds technically don't even though some important features won't work unless the app is signed (launch screen won't show for instance) -->
147147
<PropertyGroup Condition="'$(_PlatformName)' != 'macOS' And '$(_PlatformName)' != 'MacCatalyst'">
148148
<!-- Make it possible to override the default logic by setting EnableCodeSigning -->
149149
<_RequireCodeSigning Condition="'$(_RequireCodeSigning)' == ''">$(EnableCodeSigning)</_RequireCodeSigning>
150-
<!-- Device builds must be signed -->
151-
<_RequireCodeSigning Condition="'$(_RequireCodeSigning)' == '' And '$(ComputedPlatform)' == 'iPhone'">true</_RequireCodeSigning>
152-
<!-- Otherwise code signing is disabled by default (simulator builds)-->
153-
<_RequireCodeSigning Condition="'$(_RequireCodeSigning)' == ''">false</_RequireCodeSigning>
150+
<!-- Device builds must be signed, and some features won't work in the simulator if the app isn't signed (launch screen for instance), so default to always sign -->
151+
<_RequireCodeSigning Condition="'$(_RequireCodeSigning)' == ''">true</_RequireCodeSigning>
154152
</PropertyGroup>
155153
<!-- macOS is a bit more complicated:
156154
* 'EnableCodeSigning' specifies whether the app is signed or not, and this defaults to false if it's not set.

tests/common/MonoNativeConfig.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ public enum MonoNativeLinkMode {
99
Static,
1010
Dynamic,
1111
Framework,
12-
Symlink,
1312
}
1413

1514
public enum MonoNativeFlavor {
@@ -24,8 +23,6 @@ public static MonoNativeLinkMode LinkMode {
2423
return MonoNativeLinkMode.Static;
2524
#elif MONO_NATIVE_DYNAMIC
2625
return MonoNativeLinkMode.Dynamic;
27-
#elif MONO_NATIVE_SYMLINK
28-
return MonoNativeLinkMode.Symlink;
2926
#else
3027
return MonoNativeLinkMode.None;
3128
#endif
@@ -76,8 +73,6 @@ public static string GetPInvokeLibraryName (MonoNativeFlavor flavor, MonoNativeL
7673
return null;
7774
case MonoNativeLinkMode.Dynamic:
7875
return GetDynamicLibraryName (flavor);
79-
case MonoNativeLinkMode.Symlink:
80-
return "libmono-native.dylib";
8176
default:
8277
Assert.Fail ($"Invalid link mode: {MonoNativeConfig.LinkMode}");
8378
throw new NotImplementedException ();

tests/dotnet/UnitTests/BundleStructureTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ public enum CodeSignature {
594594
// Debug
595595
[TestCase (ApplePlatform.iOS, "ios-arm64", CodeSignature.All, "Debug")]
596596
[TestCase (ApplePlatform.iOS, "ios-arm64;ios-arm", CodeSignature.All, "Debug")]
597-
[TestCase (ApplePlatform.iOS, "iossimulator-x64", CodeSignature.Frameworks, "Debug")]
597+
[TestCase (ApplePlatform.iOS, "iossimulator-x64", CodeSignature.All, "Debug")]
598598
[TestCase (ApplePlatform.MacCatalyst, "maccatalyst-x64", CodeSignature.All, "Debug")]
599599
[TestCase (ApplePlatform.MacCatalyst, "maccatalyst-x64;maccatalyst-arm64", CodeSignature.All, "Debug")]
600600
[TestCase (ApplePlatform.MacOSX, "osx-x64", CodeSignature.Frameworks, "Debug")]

tests/introspection/iOS/introspection-ios.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<DebugType>full</DebugType>
2424
<Optimize>False</Optimize>
2525
<OutputPath>bin\iPhoneSimulator\$(Configuration)-unified</OutputPath>
26-
<DefineConstants>DEBUG;MONOTOUCH;MONO_NATIVE_SYMLINK;$(DefineConstants)</DefineConstants>
26+
<DefineConstants>DEBUG;MONOTOUCH;MONO_NATIVE_STATIC;$(DefineConstants)</DefineConstants>
2727
<ErrorReport>prompt</ErrorReport>
2828
<WarningLevel>0</WarningLevel>
2929
<MtouchLink>None</MtouchLink>

tests/mono-native/Introspection.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,6 @@ void CheckStaticLibrary ()
4949
Assert.That (count, Is.EqualTo (0), "zero mono-native libraries.");
5050
}
5151

52-
void CheckSymlinkedLibrary ()
53-
{
54-
AssertShouldExist ("libmono-native.dylib");
55-
AssertShouldNotExist ("libmono-native-compat.dylib");
56-
AssertShouldNotExist ("libmono-native-unified.dylib");
57-
58-
var count = CountFiles ("libmono-native*");
59-
Assert.That (count, Is.EqualTo (1), "exactly one mono-native library.");
60-
}
61-
6252
[Test]
6353
public void CheckLibrary ()
6454
{
@@ -69,9 +59,6 @@ public void CheckLibrary ()
6959
case MonoNativeLinkMode.Static:
7060
CheckStaticLibrary ();
7161
break;
72-
case MonoNativeLinkMode.Symlink:
73-
CheckSymlinkedLibrary ();
74-
break;
7562
default:
7663
Assert.Fail ($"Unknown link mode: {MonoNativeConfig.LinkMode}");
7764
break;

tests/mono-native/iOS/mono-native.csproj.template

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
2121
</PropertyGroup>
2222
<PropertyGroup>
23-
<MonoNativeMode Condition="'$(TargetFrameworkIdentifier)|$(Configuration)|$(Platform)' == 'Xamarin.iOS|Debug|iPhoneSimulator'">MONO_NATIVE_SYMLINK</MonoNativeMode>
2423
<MonoNativeMode Condition="'$(MonoNativeMode)' == ''">MONO_NATIVE_STATIC</MonoNativeMode>
2524
<DefineConstants>$(MonoNativeMode);$(DefineConstants)</DefineConstants>
2625
</PropertyGroup>

tests/monotouch-test/dotnet/shared.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<LinkMode>None</LinkMode>
1616
<!-- Don't remove native symbols, because it makes debugging native crashes harder -->
1717
<MtouchNoSymbolStrip>true</MtouchNoSymbolStrip>
18-
<CodesignEntitlements Condition="'$(Platform)' == 'iPhoneSimulator'">$(MonoTouchTestDirectory)\Entitlements.plist</CodesignEntitlements>
18+
<CodesignEntitlements Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'iOS' Or $([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tvOS'">$(MonoTouchTestDirectory)\Entitlements.plist</CodesignEntitlements>
1919

2020
<DefineConstants Condition="'$(Configuration)' == 'Debug'">$(DefineConstants);DEBUG</DefineConstants>
2121

0 commit comments

Comments
 (0)