Skip to content

Commit 3cdfb0a

Browse files
committed
Add a LinkTimeOptimization property to configure LTO.
1 parent 0ebe9ba commit 3cdfb0a

File tree

5 files changed

+22
-11
lines changed

5 files changed

+22
-11
lines changed

README.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -234,18 +234,18 @@ as sensible for historical reasons.
234234
version that `ZigVersion` defaults to.
235235
* `AccessControl` (`true`, `false`): Enable/disable access control in C++
236236
projects. Defaults to `true`.
237-
* `BlockExtensions` (`true`, `false`): Enables/disables Clang's block language
237+
* `BlockExtensions` (`true`, `false`): Enable/disable Clang's block language
238238
extensions. Defaults to `false`.
239-
* `CxxExceptions` (`true`, `false`): Enables/disables C++ exceptions. In C
239+
* `CxxExceptions` (`true`, `false`): Enable/disable C++ exceptions. In C
240240
projects, this controls whether the C code will be unwindable by C++
241241
exceptions. Defaults to `true`.
242-
* `CxxReflection` (`true`, `false`): Enables/disables generating C++ run-time
243-
type information. This feature is required for some uses of `dynamic_cast`.
242+
* `CxxReflection` (`true`, `false`): Enable/disable generating C++ run-time type
243+
information. This feature is required for some uses of `dynamic_cast`.
244244
Defaults to `true`.
245-
* `MicrosoftExtensions` (`true`, `false`): Enables/disables a variety of
245+
* `MicrosoftExtensions` (`true`, `false`): Enable/disable a variety of
246246
Microsoft C/C++ extensions. Defaults to `false`, but note that the compiler
247-
itself always enables this when targeting Windows as Windows headers require
248-
it.
247+
itself always enables some parts of this when targeting Windows as Windows
248+
headers require it.
249249

250250
#### Static Analysis
251251

@@ -281,9 +281,13 @@ as sensible for historical reasons.
281281
* `Configuration` (`Debug`, `Release`): Specifies the overarching configuration.
282282
When `Release` is specified, `ReleaseMode` comes into effect. Defaults to
283283
`Debug`. Usually specified by the user as e.g. `dotnet build -c Release`.
284-
* `FastMath` (`true`, `false`): Enables/disables certain lossy floating point
284+
* `FastMath` (`true`, `false`): Enable/disable certain lossy floating point
285285
optimizations that may not be standards-compliant. Defaults to `false`.
286-
* `DebugSymbols` (`true`, `false`): Enables/disables emitting debug symbols.
286+
* `LinkTimeOptimization` (`true`, `false`): Enable/disable link-time
287+
optimization when `Configuration` is set to `Release`. Note that link-time
288+
optimization is known not to work well on some targets and so should be used
289+
selectively. Defaults to `false`.
290+
* `DebugSymbols` (`true`, `false`): Enable/disable emitting debug symbols.
287291
Defaults to `true` if `Configuration` is `Debug`; otherwise, `false`.
288292
* `ReleaseMode` (`Fast`, `Safe`, `Small`): The
289293
[build mode](https://ziglang.org/documentation/master/#Build-Mode) to use when

src/sdk/Zig.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ public string Configuration
6666
[Required]
6767
public string LanguageStandard { get; set; } = null!;
6868

69+
[Required]
70+
public bool LinkTimeOptimization { get; set; }
71+
6972
[Required]
7073
public bool MicrosoftExtensions { get; set; }
7174

@@ -506,8 +509,8 @@ void TryAppendWarningSwitch(string name)
506509
}
507510
}
508511

509-
// TODO: https://github.com/alexrp/zig-msbuild-sdk/issues/33
510-
builder.AppendSwitch("-fno-lto");
512+
if (!LinkTimeOptimization)
513+
builder.AppendSwitch("-fno-lto");
511514

512515
foreach (var define in (DefineConstants ?? string.Empty).Split(new[] { ';' }, SplitOptions))
513516
{

src/sdk/build/Zig.Sdk.Build.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
FastMath="$(FastMath)"
4949
IncludeDirectories="@(IncludeDirectory)"
5050
LanguageStandard="$(LanguageStandard)"
51+
LinkTimeOptimization="$(LinkTimeOptimization)"
5152
MicrosoftExtensions="$(MicrosoftExtensions)"
5253
NullabilityAnalysis="$(NullabilityAnalysis)"
5354
OutputBinary="@(IntermediateAssembly)"

src/sdk/build/Zig.Sdk.Defaults.targets

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252

5353
<PropertyGroup>
5454
<FastMath Condition="'$(FastMath)' == ''">false</FastMath>
55+
<!-- TODO: https://github.com/alexrp/zig-msbuild-sdk/issues/33 -->
56+
<LinkTimeOptimization Condition="'$(LinkTimeOptimization)' == ''">false</LinkTimeOptimization>
5557
<ReleaseMode Condition="'$(ReleaseMode)' == ''">Fast</ReleaseMode>
5658
<SymbolExports Condition="'$(SymbolExports)' == ''">Used</SymbolExports>
5759
<SymbolVisibility Condition="'$(SymbolVisibility)' == ''">Default</SymbolVisibility>

src/sdk/build/Zig.Sdk.Test.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
FastMath="$(FastMath)"
3131
IncludeDirectories="@(IncludeDirectory)"
3232
LanguageStandard="$(LanguageStandard)"
33+
LinkTimeOptimization="$(LinkTimeOptimization)"
3334
MicrosoftExtensions="$(MicrosoftExtensions)"
3435
NullabilityAnalysis="$(NullabilityAnalysis)"
3536
OutputBinary="@(TestAssembly)"

0 commit comments

Comments
 (0)