Skip to content

Commit 3614a3c

Browse files
committed
Merge branch 'apotvin/xcodeStripOptions' into 'main'
Add xcode stripping options See merge request Sharpmake/sharpmake!596
2 parents ea2405b + 478b68b commit 3614a3c

File tree

4 files changed

+91
-13
lines changed

4 files changed

+91
-13
lines changed

Sharpmake.Generators/Apple/XCodeProj.Template.cs

+3
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,9 @@ private static class Template
305305
PROVISIONING_PROFILE_SPECIFIER = ""[item.Options.ProvisioningProfile]"";
306306
SKIP_INSTALL = [item.Options.SkipInstall];
307307
STRIP_INSTALLED_PRODUCT = [item.Options.StripLinkedProduct];
308+
STRIP_STYLE= [item.Options.StripStyle];
309+
STRIPFLAGS = ""[item.Options.AdditionalStripFlags]"";
310+
STRIP_SWIFT_SYMBOLS = [item.Options.StripSwiftSymbols];
308311
SYMROOT = ""[item.Options.BuildDirectory]"";
309312
VALID_ARCHS = ""[item.Options.ValidArchs]"";
310313
GENERATE_MASTER_OBJECT_FILE = [item.Options.GenerateMasterObjectFile];

Sharpmake.Platforms/Sharpmake.CommonPlatforms/Apple/BaseApplePlatform.cs

+39-11
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ public void SetupExtraLinkerSettings(IFileGenerator fileGenerator, Project.Confi
221221
configuration.Output == Project.Configuration.OutputType.Dll
222222
))
223223
{
224-
var debugFormat = Options.GetObject<Sharpmake.Options.XCode.Compiler.DebugInformationFormat>(configuration);
224+
var debugFormat = Options.GetObject<Options.XCode.Compiler.DebugInformationFormat>(configuration);
225225
if (debugFormat == Options.XCode.Compiler.DebugInformationFormat.DwarfWithDSym)
226226
{
227227
string outputPath = Path.Combine(configuration.TargetPath, configuration.TargetFileFullNameWithExtension + ".dSYM");
@@ -233,22 +233,37 @@ public void SetupExtraLinkerSettings(IFileGenerator fileGenerator, Project.Confi
233233
$"{fastBuildOutputFile} -o {outputPath}",
234234
useStdOutAsOutput: true);
235235

236-
var stripDebugSymbols = Options.GetObject<Options.XCode.Linker.StripLinkedProduct>(configuration);
237-
if (stripDebugSymbols == Options.XCode.Linker.StripLinkedProduct.Enable)
236+
// Stripping
237+
if (Options.GetObject<Options.XCode.Linker.StripLinkedProduct>(configuration) == Options.XCode.Linker.StripLinkedProduct.Enable)
238238
{
239+
List<string> stripOptionList = new List<string>();
240+
switch (Options.GetObject<Options.XCode.Linker.StripStyle>(configuration))
241+
{
242+
case Options.XCode.Linker.StripStyle.AllSymbols:
243+
stripOptionList.Add("-s");
244+
break;
245+
case Options.XCode.Linker.StripStyle.NonGlobalSymbols:
246+
stripOptionList.Add("-x");
247+
break;
248+
case Options.XCode.Linker.StripStyle.DebuggingSymbolsOnly:
249+
stripOptionList.Add("-S");
250+
break;
251+
}
252+
if (Options.GetObject<Options.XCode.Linker.StripSwiftSymbols>(configuration) == Options.XCode.Linker.StripSwiftSymbols.Enable)
253+
stripOptionList.Add("-T");
254+
255+
var additionalStripFlags = Options.GetObject<Options.XCode.Linker.AdditionalStripFlags>(configuration);
256+
if (additionalStripFlags != null)
257+
stripOptionList.Add(XCodeUtil.ResolveProjectVariable(configuration.Project, additionalStripFlags.Value));
258+
259+
string stripOptions = string.Join(" ", stripOptionList);
260+
239261
string strippedSentinelFile = Path.Combine(configuration.IntermediatePath, configuration.TargetFileName + ".stripped");
240262
yield return new Project.Configuration.BuildStepExecutable(
241263
"/usr/bin/strip",
242264
asStampSteps ? string.Empty : dsymutilSentinelFile,
243265
asStampSteps ? string.Empty : strippedSentinelFile,
244-
// From MacOS strip manual page:
245-
// -r : Save all symbols referenced dynamically.
246-
// -S : Remove the debuging symbol table entries (those created by the -g optin to cc and other compilers).
247-
// -T : The intent of this flag is to remove Swift symbols from the Mach-O symbol table,
248-
// It removes the symbols whose names begin with '_$S' or '_$s' only when it finds an __objc_imageinfo section with and it has non-zero swift version.
249-
// In the future the implementation of this flag may change to match the intent.
250-
// -x : Remove all local symbols (saving only global symbols)
251-
$"-rSTx {fastBuildOutputFile}",
266+
$"{stripOptions} {fastBuildOutputFile}",
252267
useStdOutAsOutput: true
253268
);
254269
}
@@ -1372,6 +1387,19 @@ public virtual void SelectLinkerOptions(IGenerationContext context)
13721387
Options.Option(Options.XCode.Linker.StripLinkedProduct.Enable, () => options["StripLinkedProduct"] = "YES")
13731388
);
13741389

1390+
context.SelectOption(
1391+
Options.Option(Options.XCode.Linker.StripStyle.AllSymbols, () => options["StripStyle"] = "all"),
1392+
Options.Option(Options.XCode.Linker.StripStyle.NonGlobalSymbols, () => options["StripStyle"] = "non-global"),
1393+
Options.Option(Options.XCode.Linker.StripStyle.DebuggingSymbolsOnly, () => options["StripStyle"] = "debugging")
1394+
);
1395+
1396+
context.SelectOption(
1397+
Options.Option(Options.XCode.Linker.StripSwiftSymbols.Disable, () => options["StripSwiftSymbols"] = "NO"),
1398+
Options.Option(Options.XCode.Linker.StripSwiftSymbols.Enable, () => options["StripSwiftSymbols"] = "YES")
1399+
);
1400+
1401+
options["AdditionalStripFlags"] = XCodeUtil.ResolveProjectVariable(context.Project, Options.StringOption.Get<Options.XCode.Linker.AdditionalStripFlags>(conf));
1402+
13751403
context.SelectOption(
13761404
Options.Option(Options.XCode.Linker.PerformSingleObjectPrelink.Disable, () => options["GenerateMasterObjectFile"] = "NO"),
13771405
Options.Option(Options.XCode.Linker.PerformSingleObjectPrelink.Enable, () => options["GenerateMasterObjectFile"] = "YES")

Sharpmake/Options.XCode.cs

+43-1
Original file line numberDiff line numberDiff line change
@@ -610,10 +610,15 @@ public enum AssetCatalogWarnings
610610
Enable
611611
}
612612

613+
/// <summary>
614+
/// Testability is used for automated tests in XCode.
615+
/// Activating it will disable other options, like stripping and private symbols.
616+
/// See https://developer.apple.com/documentation/xcode/build-settings-reference#Enable-Testability
617+
/// </summary>
613618
public enum Testability
614619
{
615-
[Default]
616620
Enable,
621+
[Default]
617622
Disable
618623
}
619624

@@ -1119,13 +1124,50 @@ public enum UIAppSupportsHDR
11191124

11201125
public static class Linker
11211126
{
1127+
/// <summary>
1128+
/// Enables symbols stripping after the binary is linked.
1129+
/// </summary>
11221130
public enum StripLinkedProduct
11231131
{
1132+
[Default]
11241133
Disable,
1134+
Enable
1135+
}
1136+
1137+
/// <summary>
1138+
/// When stripping is enabled.
1139+
/// The level of symbol stripping to be performed on the linked product of the build.
1140+
/// </summary>
1141+
public enum StripStyle
1142+
{
1143+
AllSymbols, // Completely strips the binary, removing the symbol table and relocation information (strip option -s)
1144+
NonGlobalSymbols, // Strips non-global symbols, but saves external symbols (strip option -x)
11251145
[Default]
1146+
DebuggingSymbolsOnly // Strips debugging symbols, but saves local and global symbols (strip option -S)
1147+
}
1148+
1149+
/// <summary>
1150+
/// When stripping is enabled.
1151+
/// Adjust the level of symbol stripping so that when the linked product of the build is stripped, all Swift symbols will be removed.
1152+
/// </summary>
1153+
public enum StripSwiftSymbols
1154+
{
1155+
[Default]
1156+
Disable,
11261157
Enable
11271158
}
11281159

1160+
/// <summary>
1161+
/// Additional Strip Flags.
1162+
/// For a complete list, see documentation for /usr/bin/strip.
1163+
/// </summary>
1164+
public class AdditionalStripFlags : StringOption
1165+
{
1166+
public AdditionalStripFlags(string value) : base(value)
1167+
{
1168+
}
1169+
}
1170+
11291171
/// <summary>
11301172
/// Xcode has a setting called Single-Object Prelink, which allows libraries and frameworks to include the necessary symbols
11311173
/// from other libraries so that the underlying libraries do not need to be linked against in an application using your framework.

samples/HelloXCode/HelloXCode.CommonProject.sharpmake.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,13 @@ public virtual void ConfigureAll(Configuration conf, CommonTarget target)
6060
conf.Output = Configuration.OutputType.Lib; // defaults to creating static libs
6161
conf.Options.Add(Options.XCode.Editor.Indent.Spaces);
6262

63-
if (target.Optimization == Optimization.Debug)
63+
if (target.Optimization == Optimization.Release)
64+
{
6465
conf.Options.Add(Sharpmake.Options.XCode.Compiler.DebugInformationFormat.DwarfWithDSym);
66+
conf.Options.Add(Sharpmake.Options.XCode.Linker.StripLinkedProduct.Enable);
67+
conf.Options.Add(Sharpmake.Options.XCode.Linker.StripStyle.DebuggingSymbolsOnly);
68+
conf.Options.Add(Sharpmake.Options.XCode.Linker.StripSwiftSymbols.Enable);
69+
}
6570
else
6671
conf.Options.Add(Sharpmake.Options.XCode.Compiler.DebugInformationFormat.Dwarf);
6772
}

0 commit comments

Comments
 (0)