Skip to content

Commit

Permalink
Add a LinkTimeOptimization property to configure LTO.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexrp committed Jul 23, 2021
1 parent 4b6a336 commit 4deb807
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
22 changes: 13 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,18 +234,18 @@ as sensible for historical reasons.
version that `ZigVersion` defaults to.
* `AccessControl` (`true`, `false`): Enable/disable access control in C++
projects. Defaults to `true`.
* `BlockExtensions` (`true`, `false`): Enables/disables Clang's block language
* `BlockExtensions` (`true`, `false`): Enable/disable Clang's block language
extensions. Defaults to `false`.
* `CxxExceptions` (`true`, `false`): Enables/disables C++ exceptions. In C
* `CxxExceptions` (`true`, `false`): Enable/disable C++ exceptions. In C
projects, this controls whether the C code will be unwindable by C++
exceptions. Defaults to `true`.
* `CxxReflection` (`true`, `false`): Enables/disables generating C++ run-time
type information. This feature is required for some uses of `dynamic_cast`.
* `CxxReflection` (`true`, `false`): Enable/disable generating C++ run-time type
information. This feature is required for some uses of `dynamic_cast`.
Defaults to `true`.
* `MicrosoftExtensions` (`true`, `false`): Enables/disables a variety of
* `MicrosoftExtensions` (`true`, `false`): Enable/disable a variety of
Microsoft C/C++ extensions. Defaults to `false`, but note that the compiler
itself always enables this when targeting Windows as Windows headers require
it.
itself always enables some parts of this when targeting Windows as Windows
headers require it.

#### Static Analysis

Expand Down Expand Up @@ -281,9 +281,13 @@ as sensible for historical reasons.
* `Configuration` (`Debug`, `Release`): Specifies the overarching configuration.
When `Release` is specified, `ReleaseMode` comes into effect. Defaults to
`Debug`. Usually specified by the user as e.g. `dotnet build -c Release`.
* `FastMath` (`true`, `false`): Enables/disables certain lossy floating point
* `FastMath` (`true`, `false`): Enable/disable certain lossy floating point
optimizations that may not be standards-compliant. Defaults to `false`.
* `DebugSymbols` (`true`, `false`): Enables/disables emitting debug symbols.
* `LinkTimeOptimization` (`true`, `false`): Enable/disable link-time
optimization when `Configuration` is set to `Release`. Note that link-time
optimization is known not to work well on some targets and so should be used
selectively. Defaults to `false`.
* `DebugSymbols` (`true`, `false`): Enable/disable emitting debug symbols.
Defaults to `true` if `Configuration` is `Debug`; otherwise, `false`.
* `ReleaseMode` (`Fast`, `Safe`, `Small`): The
[build mode](https://ziglang.org/documentation/master/#Build-Mode) to use when
Expand Down
7 changes: 5 additions & 2 deletions src/sdk/Zig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ public string Configuration
[Required]
public string LanguageStandard { get; set; } = null!;

[Required]
public bool LinkTimeOptimization { get; set; }

[Required]
public bool MicrosoftExtensions { get; set; }

Expand Down Expand Up @@ -513,8 +516,8 @@ void TryAppendWarningSwitch(string name)
}
}

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

foreach (var define in (DefineConstants ?? string.Empty).Split(new[] { ';' }, SplitOptions))
{
Expand Down
1 change: 1 addition & 0 deletions src/sdk/build/Zig.Sdk.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
FastMath="$(FastMath)"
IncludeDirectories="@(IncludeDirectory)"
LanguageStandard="$(LanguageStandard)"
LinkTimeOptimization="$(LinkTimeOptimization)"
MicrosoftExtensions="$(MicrosoftExtensions)"
NullabilityAnalysis="$(NullabilityAnalysis)"
OutputBinary="@(IntermediateAssembly)"
Expand Down
2 changes: 2 additions & 0 deletions src/sdk/build/Zig.Sdk.Defaults.targets
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@

<PropertyGroup>
<FastMath Condition="'$(FastMath)' == ''">false</FastMath>
<!-- TODO: https://github.com/alexrp/zig-msbuild-sdk/issues/33 -->
<LinkTimeOptimization Condition="'$(LinkTimeOptimization)' == ''">false</LinkTimeOptimization>
<ReleaseMode Condition="'$(ReleaseMode)' == ''">Fast</ReleaseMode>
<SymbolExports Condition="'$(SymbolExports)' == ''">Used</SymbolExports>
<SymbolVisibility Condition="'$(SymbolVisibility)' == ''">Default</SymbolVisibility>
Expand Down
1 change: 1 addition & 0 deletions src/sdk/build/Zig.Sdk.Test.targets
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
FastMath="$(FastMath)"
IncludeDirectories="@(IncludeDirectory)"
LanguageStandard="$(LanguageStandard)"
LinkTimeOptimization="$(LinkTimeOptimization)"
MicrosoftExtensions="$(MicrosoftExtensions)"
NullabilityAnalysis="$(NullabilityAnalysis)"
OutputBinary="@(TestAssembly)"
Expand Down

0 comments on commit 4deb807

Please sign in to comment.