Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/building-apps/build-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -1556,6 +1556,13 @@ However, the either of the following works:

Note: this property will always be `false` on macOS and Mac Catalyst.

## StripFrameworkHeaders

A boolean property that specifies whether the `Headers`, `PrivateHeaders`, and
`Modules` directories are removed from embedded frameworks before code signing.

The default value is `true`. Set it to `false` to preserve these directories.

## StripPath

The full path to the `strip` command-line tool.
Expand Down
14 changes: 13 additions & 1 deletion dotnet/targets/Xamarin.Shared.Sdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,18 @@
This target will compute that stamp file location.
-->
<Target Name="_CreateStampLocationForCopyDirectoriesToBundle">
<PropertyGroup>
<_CopyDirectoriesToBundleInputFile>$(DeviceSpecificIntermediateOutputPath)CopyDirectoriesToBundleInputs.cache</_CopyDirectoriesToBundleInputFile>
</PropertyGroup>
<WriteLinesToFile
File="$(_CopyDirectoriesToBundleInputFile)"
Lines="StripFrameworkHeaders=$(StripFrameworkHeaders)"
Overwrite="true"
WriteOnlyWhenDifferent="true"
/>
<ItemGroup>
<FileWrites Include="$(_CopyDirectoriesToBundleInputFile)" />
</ItemGroup>
<ComputeHashForItems
Input="@(_DirectoriesToPublish)"
InputMetadata="Identity;TargetDirectory"
Expand Down Expand Up @@ -1153,7 +1165,7 @@

<Target Name="_CopyDirectoriesToBundle"
DependsOnTargets="_CollectDecompressedPlugins;_ComputeFrameworkFilesToPublish;_CollectDecompressedXpcServices;_CreateStampLocationForCopyDirectoriesToBundle"
Inputs="@(_DirectoriesToPublish)"
Inputs="@(_DirectoriesToPublish);$(_CopyDirectoriesToBundleInputFile)"
Outputs="@(_DirectoriesToPublish -> '%(StampLocation)')"
>

Expand Down
91 changes: 91 additions & 0 deletions msbuild/Xamarin.MacDev.Tasks/Tasks/StripFrameworkHeaders.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

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

using Xamarin.Messaging.Build.Client;

#nullable enable

namespace Xamarin.MacDev.Tasks {
public class StripFrameworkHeaders : XamarinTask, ITaskCallback {
static readonly HashSet<string> directoriesToStrip = new HashSet<string> (StringComparer.Ordinal) {
"Headers",
"Modules",
"PrivateHeaders",
};

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

public override bool Execute ()
{
if (ShouldExecuteRemotely ())
return ExecuteRemotely ();

StripFrameworksInDirectory (AppBundleDir);

return !Log.HasLoggedErrors;
}

void StripFrameworksInDirectory (string directory)
{
foreach (var subdirectory in Directory.GetDirectories (directory)) {
if (IsSymbolicLink (subdirectory))
continue;

if (subdirectory.EndsWith (".framework", StringComparison.OrdinalIgnoreCase))
StripFramework (subdirectory);

StripFrameworksInDirectory (subdirectory);
}
}

void StripFramework (string framework)
{
foreach (var directoryName in directoriesToStrip)
RemoveDirectory (Path.Combine (framework, directoryName));

var versionsDirectory = Path.Combine (framework, "Versions");
if (!Directory.Exists (versionsDirectory) || IsSymbolicLink (versionsDirectory))
return;

foreach (var versionDirectory in Directory.GetDirectories (versionsDirectory).Where (directory => !IsSymbolicLink (directory))) {
foreach (var directoryName in directoriesToStrip)
RemoveDirectory (Path.Combine (versionDirectory, directoryName));
}
}

void RemoveDirectory (string directory)
{
if (!Directory.Exists (directory))
return;

Log.LogMessage (MessageImportance.Low, $"Removing framework directory '{directory}'.");
if (IsSymbolicLink (directory))
File.Delete (directory);
else
Directory.Delete (directory, true);
}

static bool IsSymbolicLink (string path)
{
return (File.GetAttributes (path) & FileAttributes.ReparsePoint) != 0;
}

public IEnumerable<ITaskItem> GetAdditionalItemsToBeCopied ()
{
return Enumerable.Empty<ITaskItem> ();
}

public bool ShouldCopyToBuildServer (ITaskItem item) => false;

public bool ShouldCreateOutputFile (ITaskItem item) => false;
}
}
3 changes: 2 additions & 1 deletion msbuild/Xamarin.Shared/Xamarin.Shared.props
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ Copyright (C) 2020 Microsoft. All rights reserved.
<OptimizePropertyLists Condition="'$(_PlatformName)' == 'macOS' And '$(OptimizePropertyLists)' == ''">false</OptimizePropertyLists>
<OptimizePropertyLists Condition="'$(_PlatformName)' != 'macOS' And '$(OptimizePropertyLists)' == ''">true</OptimizePropertyLists>

<StripFrameworkHeaders Condition="'$(StripFrameworkHeaders)' == ''">true</StripFrameworkHeaders>

<!-- EnableOnDemandResources: default to true for Xamarin.iOS and false for Xamarin.Mac -->
<EnableOnDemandResources Condition="'$(_PlatformName)' == 'macOS' And '$(EnableOnDemandResources)' == ''">false</EnableOnDemandResources>
<EnableOnDemandResources Condition="'$(_PlatformName)' != 'macOS' And '$(EnableOnDemandResources)' == ''">true</EnableOnDemandResources>
Expand Down Expand Up @@ -299,4 +301,3 @@ Copyright (C) 2020 Microsoft. All rights reserved.
<Import Project="$(MSBuildThisFileDirectory)$(MSBuildThisFileName).After.targets"
Condition="Exists('$(MSBuildThisFileDirectory)$(MSBuildThisFileName).After.targets')"/>
</Project>

9 changes: 9 additions & 0 deletions msbuild/Xamarin.Shared/Xamarin.Shared.targets
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ Copyright (C) 2018 Microsoft. All rights reserved.
<UsingTask Runtime="$(_TaskRuntime)" TaskName="Xamarin.MacDev.Tasks.ScnTool" AssemblyFile="$(_TaskAssemblyName)" />
<UsingTask Runtime="$(_TaskRuntime)" TaskName="Xamarin.MacDev.Tasks.SmartCopy" AssemblyFile="$(_TaskAssemblyName)" />
<UsingTask Runtime="$(_TaskRuntime)" TaskName="Xamarin.MacDev.Tasks.SpotlightIndexer" AssemblyFile="$(_TaskAssemblyName)" />
<UsingTask Runtime="$(_TaskRuntime)" TaskName="Xamarin.MacDev.Tasks.StripFrameworkHeaders" AssemblyFile="$(_TaskAssemblyName)" />
<UsingTask Runtime="$(_TaskRuntime)" TaskName="Xamarin.MacDev.Tasks.SymbolStrip" AssemblyFile="$(_TaskAssemblyName)" />
<UsingTask Runtime="$(_TaskRuntime)" TaskName="Xamarin.MacDev.Tasks.TextureAtlas" AssemblyFile="$(_TaskAssemblyName)" />
<UsingTask Runtime="$(_TaskRuntime)" TaskName="Xamarin.MacDev.Tasks.UnpackLibraryResources" AssemblyFile="$(_TaskAssemblyName)" />
Expand Down Expand Up @@ -2439,12 +2440,20 @@ Copyright (C) 2018 Microsoft. All rights reserved.
$(CodesignDependsOn);
_CreateAppBundle;
BeforeCodeSign;
_StripFrameworkHeaders;
CoreCodeSign;
AfterCodeSign;
</CodesignDependsOn>
</PropertyGroup>

<Target Name="BeforeCodesign" />
<Target Name="_StripFrameworkHeaders" Condition="'$(StripFrameworkHeaders)' == 'true'">
<StripFrameworkHeaders
SessionId="$(BuildSessionId)"
Condition="'$(IsMacEnabled)' == 'true'"
AppBundleDir="$(AppBundleDir)"
/>
</Target>
<Target Name="CoreCodesign" DependsOnTargets="$(CoreCodesignDependsOn)" />
<Target Name="AfterCodesign" />

Expand Down
60 changes: 45 additions & 15 deletions tests/dotnet/UnitTests/BundleStructureTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ internal static void CheckAppBundleContents (ApplePlatform platform, IEnumerable
AddMultiRidAssembly (platform, expectedFiles, assemblyDirectory, "bindings-framework-test", runtimeIdentifiers, forceSingleRid: platform != ApplePlatform.MacCatalyst || !isReleaseBuild, includeDebugFiles: includeDebugFiles);
AddExpectedFrameworkFiles (platform, expectedFiles, "XTest", isSigned);

AddExpectedFrameworkFiles (platform, expectedFiles, "FrameworkWithLongFileNames", isSigned, longHeader: true);
AddExpectedFrameworkFiles (platform, expectedFiles, "FrameworkWithLongFileNames", isSigned, longFile: true);

// various directories
expectedFiles.Add (frameworksDirectory);
Expand Down Expand Up @@ -541,34 +541,32 @@ static void AddExpectedExtensionFiles (ApplePlatform platform, List<string> expe
}
}

static void AddExpectedFrameworkFiles (ApplePlatform platform, List<string> expectedFiles, string frameworkName, CodeSignature signature, string subdirectory = "", bool longHeader = false)
static void AddExpectedFrameworkFiles (ApplePlatform platform, List<string> expectedFiles, string frameworkName, CodeSignature signature, string subdirectory = "", bool longFile = false)
{
var isSigned = signature != CodeSignature.None;
var frameworksDirectory = "Frameworks";
var headersDirectoryInFramework = "Headers";
var longFileInFramework = Path.Combine ("Resources", "full-paths-exceeding-two-hundred-and-sixty-characters", "often-cause-trouble-on-windows", "where-the-maximum-is-by-default-two-hundred-and-sixty-characters", "because-frameworks-and-by-extension-xcframeworks", "very-often-have-paths-longer-than-this-limit", "especially-when-contained-in-other-directories.txt");
switch (platform) {
case ApplePlatform.iOS:
case ApplePlatform.TVOS:
break;
case ApplePlatform.MacCatalyst:
case ApplePlatform.MacOSX:
frameworksDirectory = Path.Combine ("Contents", "Frameworks");
headersDirectoryInFramework = Path.Combine ("Versions", "A", "Headers");
longFileInFramework = Path.Combine ("Versions", "A", longFileInFramework);
break;
default:
throw new NotImplementedException ($"Unknown platform: {platform}");
}

var headers = new List<string> ();
if (longHeader)
headers.Add (Path.Combine (headersDirectoryInFramework, "full-paths-exceeding-two-hundred-and-sixty-characters", "often-cause-trouble-on-windows", "where-the-maximum-is-by-default-two-hundred-and-sixty-characters", "because-frameworks-and-by-extension-xcframeworks", "very-often-have-paths-longer-than-this-limit", "especially-when-contained-in-other-directories.h"));

expectedFiles.Add (Path.Combine (frameworksDirectory, $"{frameworkName}.framework"));
expectedFiles.Add (Path.Combine (frameworksDirectory, $"{frameworkName}.framework", frameworkName));
switch (platform) {
case ApplePlatform.iOS:
case ApplePlatform.TVOS:
expectedFiles.Add (Path.Combine (frameworksDirectory, $"{frameworkName}.framework", "Info.plist"));
if (longFile)
expectedFiles.Add (Path.Combine (frameworksDirectory, $"{frameworkName}.framework", "Resources"));
break;
case ApplePlatform.MacCatalyst:
case ApplePlatform.MacOSX:
Expand All @@ -580,19 +578,17 @@ static void AddExpectedFrameworkFiles (ApplePlatform platform, List<string> expe
expectedFiles.Add (Path.Combine (frameworksDirectory, $"{frameworkName}.framework", "Versions", "A", frameworkName));
expectedFiles.Add (Path.Combine (frameworksDirectory, $"{frameworkName}.framework", "Versions", "Current"));

if (headers.Any ())
expectedFiles.Add (Path.Combine (frameworksDirectory, $"{frameworkName}.framework", "Headers"));
break;
default:
throw new NotImplementedException ($"Unknown platform: {platform}");
}

foreach (var header in headers) {
if (longFile) {
var path = Path.Combine (frameworksDirectory, $"{frameworkName}.framework");
var headerComponents = header.Split ('\\', '/');
for (var i = 0; i < headerComponents.Length; i++) {
path = Path.Combine (path, headerComponents [i]);
expectedFiles.Add (path);
foreach (var component in longFileInFramework.Split ('\\', '/')) {
path = Path.Combine (path, component);
if (!expectedFiles.Contains (path))
expectedFiles.Add (path);
}
}

Expand Down Expand Up @@ -620,6 +616,40 @@ public enum CodeSignature {
All,
}

[Test]
public void PreserveFrameworkHeaders ()
{
var platform = ApplePlatform.iOS;
var runtimeIdentifier = "iossimulator-arm64";
Configuration.IgnoreIfIgnoredPlatform (platform);
Configuration.AssertRuntimeIdentifiersAvailable (platform, runtimeIdentifier);

var projectPath = GetProjectPath ("BundleStructure", runtimeIdentifiers: runtimeIdentifier, platform: platform, out var appPath);
Clean (projectPath);

var properties = GetDefaultProperties (runtimeIdentifier);
properties ["EnableCodeSigning"] = "false";
DotNet.AssertBuild (projectPath, properties);

var header = Path.Combine (appPath, "Frameworks", "FrameworkWithLongFileNames.framework", "Headers", "test.h");
Assert.That (header, Does.Not.Exist);

properties ["StripFrameworkHeaders"] = "false";
DotNet.AssertBuild (projectPath, properties);

Assert.That (header, Does.Exist);

properties ["StripFrameworkHeaders"] = "true";
DotNet.AssertBuild (projectPath, properties);

Assert.That (header, Does.Not.Exist);

properties ["StripFrameworkHeaders"] = "false";
DotNet.AssertBuild (projectPath, properties);

Assert.That (header, Does.Exist);
}

[Test]
// Debug
[TestCase (ApplePlatform.iOS, "ios-arm64", CodeSignature.All, "Debug")]
Expand Down
6 changes: 3 additions & 3 deletions tests/dotnet/UnitTests/WindowsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ void AssertMaxFileLengthInBinAndObjDirectories (ApplePlatform platform, string p
{
// 'full-paths-exceeding-two-hundred-and-sixty-characters' is a subdirectory inside the FrameworkWithLongFileNames framework
"full-paths-exceeding-two-hundred-and-sixty-characters",
// 'especially-when-contained-in-other-directories.h' is a file inside the FrameworkWithLongFileNames framework
"especially-when-contained-in-other-directories.h",
// 'especially-when-contained-in-other-directories.txt' is a file inside the FrameworkWithLongFileNames framework
"especially-when-contained-in-other-directories.txt",
};
foreach (var ip in invalidPaths) {
var withLongFilenames = allFiles.Where (v => v.RelativePath.Contains (ip)).ToArray ();
Assert.That (longerThanMax, Is.Empty, $"No paths with '{ip}'");
Assert.That (withLongFilenames, Is.Empty, $"No paths with '{ip}'");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.IO;

using NUnit.Framework;

using Xamarin.MacDev.Tasks;
using Xamarin.Utils;

#nullable enable

namespace Xamarin.MacDev.Tasks.Tests {
[TestFixture]
public class StripFrameworkHeadersTaskTests : TestBase {
[Test]
public void StripFrameworkDirectories ()
{
var appBundle = Path.Combine (Cache.CreateTemporaryDirectory (), "Test.app");
var flatFramework = Path.Combine (appBundle, "Frameworks", "Flat.framework");
var versionedFramework = Path.Combine (appBundle, "Frameworks", "Versioned.framework");
var nestedFramework = Path.Combine (appBundle, "PlugIns", "Extension.appex", "Frameworks", "Nested.framework");
var unrelatedHeaders = Path.Combine (appBundle, "Headers");
var resourceHeaders = Path.Combine (versionedFramework, "Versions", "A", "Resources", "Headers");
var symbolicLinks = new [] {
Path.Combine (versionedFramework, "Headers"),
Path.Combine (versionedFramework, "Modules"),
Path.Combine (versionedFramework, "PrivateHeaders"),
};
var directoriesToStrip = new [] {
Path.Combine (flatFramework, "Headers"),
Path.Combine (flatFramework, "Modules"),
Path.Combine (flatFramework, "PrivateHeaders"),
Path.Combine (versionedFramework, "Versions", "A", "Headers"),
Path.Combine (versionedFramework, "Versions", "A", "Modules"),
Path.Combine (versionedFramework, "Versions", "A", "PrivateHeaders"),
Path.Combine (nestedFramework, "Headers"),
};

foreach (var directory in directoriesToStrip) {
Directory.CreateDirectory (directory);
File.WriteAllText (Path.Combine (directory, "content"), "content");
}
Directory.CreateDirectory (unrelatedHeaders);
Directory.CreateDirectory (resourceHeaders);

if (!OperatingSystem.IsWindows ()) {
foreach (var symbolicLink in symbolicLinks)
Directory.CreateSymbolicLink (symbolicLink, Path.Combine ("Versions", "A", Path.GetFileName (symbolicLink)));
}

var task = CreateTask<StripFrameworkHeaders> ();
task.AppBundleDir = appBundle;
ExecuteTask (task);

foreach (var directory in directoriesToStrip)
Assert.That (directory, Does.Not.Exist);
foreach (var symbolicLink in symbolicLinks) {
Assert.That (symbolicLink, Does.Not.Exist);
Assert.That (new DirectoryInfo (symbolicLink).LinkTarget, Is.Null);
}
Assert.That (unrelatedHeaders, Does.Exist);
Assert.That (resourceHeaders, Does.Exist);
}
}
}
Loading
Loading