Skip to content

Commit 209daf7

Browse files
committed
Implement fan-out build and clean behavior.
Fixes #22.
1 parent 1eb09e2 commit 209daf7

File tree

6 files changed

+74
-38
lines changed

6 files changed

+74
-38
lines changed

README.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ Here are some of the `Zig.Sdk` highlights:
1818
[Clang](https://clang.llvm.org). So, whichever language you prefer, `Zig.Sdk`
1919
has you covered.
2020
* **Cross-compilation:** Thanks to the Zig compiler's excellent cross-targeting
21-
support, you can build native code for a variety of target platforms no matter
22-
what platform you are on. Gone are the days of having to do overly complicated
23-
cross toolchain setup, or resorting to building on multiple platforms for
24-
releases. Cross-compilation is a first-class citizen in `Zig.Sdk`.
21+
support, cross-compilation is a first-class citizen in `Zig.Sdk`. Gone are the
22+
days of having to do overly complicated cross toolchain setup, or resorting to
23+
building on multiple platforms for releases - just type `dotnet build` to
24+
compile for all targets supported by your project.
2525
* **Binary emulator support:** When cross-compiling, `Zig.Sdk` will look at the
2626
host and target platforms and try to pick an appropriate emulator. In the
2727
majority of cases, this allows you to run and unit test the foreign binary.
@@ -37,10 +37,10 @@ Here are some of the `Zig.Sdk` highlights:
3737
expected, enabling a rapid development loop.
3838
* **Sensible NuGet packaging:** Out of the box, `dotnet pack` with `Zig.Sdk`
3939
will produce NuGet packages containing cross-built binaries for all platforms
40-
that your project declares support for. Also, your public C and C++ header
41-
files will be bundled, as will your Zig source code. This makes the resulting
42-
NuGet package easy to consume both in `Microsoft.NET.Sdk` projects and in
43-
other `Zig.Sdk` projects.
40+
that your project supports. Also, your public C and C++ header files will be
41+
bundled, as will your Zig source code. This makes the resulting NuGet package
42+
easy to consume both in `Microsoft.NET.Sdk` projects and in other `Zig.Sdk`
43+
projects.
4444
* **Multi-project solutions:**
4545
[Soon™.](https://github.com/alexrp/zig-msbuild-sdk/issues/8)
4646
* **Easy to use:** Just add an entry to your `global.json`, and create a project
@@ -244,10 +244,11 @@ as sensible for historical reasons.
244244
#### Cross-Compilation
245245

246246
* `RuntimeIdentifier`: Specifies the runtime identifier (i.e. platform) to
247-
target. Defaults to the runtime identifier of the current platform. Usually
248-
specified by the user as e.g. `dotnet build -r linux-x64`.
247+
target. When unset, `Build` and `Clean` will run for all runtime identifiers
248+
specified in `RuntimeIdentifiers`. Usually specified by the user as e.g.
249+
`dotnet build -r linux-x64`. Unset by default.
249250
* `RuntimeIdentifiers`: A semicolon-separated list of runtime identifiers that
250-
your project supports. All targets on this list will be cross-compiled when
251+
the project supports. All targets on this list will be cross-compiled as
251252
necessary. Defaults to all targets that the Zig compiler has known-good
252253
support for.
253254
* `UseEmulator` (`true`, `false`): Enable/disable usage of an appropriate binary

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,29 @@
11
<Project>
2+
<PropertyGroup>
3+
<_RealOrFanOutBuild>_InnerBuild</_RealOrFanOutBuild>
4+
<_RealOrFanOutBuild Condition="'$(_BuildCleanFanOut)' == 'true'">_FanOutBuild</_RealOrFanOutBuild>
5+
</PropertyGroup>
6+
7+
<Target Name="Build"
8+
DependsOnTargets="$(_RealOrFanOutBuild)"
9+
Condition="'$(_InvalidConfigurationWarning)' != 'true'" />
10+
11+
<Target Name="_FanOutBuild">
12+
<ItemGroup>
13+
<_RuntimeIdentifiers Include="$(RuntimeIdentifiers)" />
14+
<_InnerProjects Include="$(MSBuildProjectFullPath)"
15+
Properties="RuntimeIdentifier=%(_RuntimeIdentifiers.Identity)" />
16+
</ItemGroup>
17+
18+
<MSBuild Projects="@(_InnerProjects)"
19+
Targets="_InnerBuild"
20+
BuildInParallel="$(BuildInParallel)" />
21+
</Target>
22+
23+
<Target Name="_InnerBuild"
24+
DependsOnTargets="$(BuildDependsOn)"
25+
Returns="@(TargetPathWithTargetPlatformMoniker)" />
26+
227
<!--
328
This is the target that actually compiles source code to a native binary.
429
-->

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,28 @@
77
<ItemGroup>
88
<Clean Include="$(CachePath)/**" />
99
</ItemGroup>
10+
11+
<PropertyGroup>
12+
<_RealOrFanOutClean>_InnerClean</_RealOrFanOutClean>
13+
<_RealOrFanOutClean Condition="'$(_BuildCleanFanOut)' == 'true'">_FanOutClean</_RealOrFanOutClean>
14+
</PropertyGroup>
15+
16+
<Target Name="Clean"
17+
DependsOnTargets="$(_RealOrFanOutClean)"
18+
Condition="'$(_InvalidConfigurationWarning)' != 'true'" />
19+
20+
<Target Name="_FanOutClean">
21+
<ItemGroup>
22+
<_RuntimeIdentifiers Include="$(RuntimeIdentifiers)" />
23+
<_InnerProjects Include="$(MSBuildProjectFullPath)"
24+
Properties="RuntimeIdentifier=%(_RuntimeIdentifiers.Identity)" />
25+
</ItemGroup>
26+
27+
<MSBuild Projects="@(_InnerProjects)"
28+
Targets="_InnerClean"
29+
BuildInParallel="$(BuildInParallel)" />
30+
</Target>
31+
32+
<Target Name="_InnerClean"
33+
DependsOnTargets="$(CleanDependsOn)" />
1034
</Project>

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,19 @@
1212
<EnableDefaultEmbeddedResourceItems>false</EnableDefaultEmbeddedResourceItems>
1313
<GenerateDependencyFile>false</GenerateDependencyFile>
1414
<IncludeBuildOutput>false</IncludeBuildOutput>
15-
<RuntimeIdentifier>$(NETCoreSdkPortableRuntimeIdentifier)</RuntimeIdentifier>
1615
<ShouldBeValidatedAsExecutableReference>false</ShouldBeValidatedAsExecutableReference>
1716
<TargetFramework>netstandard1.0</TargetFramework>
1817
</PropertyGroup>
18+
19+
<!--
20+
If a RuntimeIdentifier value has not been set, it means the Build and Clean
21+
targets should fan out and run for every RID supported by the project. That
22+
said, for running and testing, we still need to run natively by default, so
23+
we still have to set a default RID. We use _BuildCleanFanOut to indicate
24+
whether the fan-out behavior is required.
25+
-->
26+
<PropertyGroup Condition="'$(RuntimeIdentifier)' == ''">
27+
<RuntimeIdentifier>$(NETCoreSdkPortableRuntimeIdentifier)</RuntimeIdentifier>
28+
<_BuildCleanFanOut>true</_BuildCleanFanOut>
29+
</PropertyGroup>
1930
</Project>

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

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@
1010
</PackDependsOn>
1111
</PropertyGroup>
1212

13-
<!--
14-
Build all RIDs (or just the current RID) and add native binaries to the
15-
appropriate folders in the NuGet package.
16-
-->
1713
<Target Name="_PackNativeAssets">
1814
<!--
1915
Since we claim to support netstandard1.0 but do not package any managed
@@ -58,37 +54,15 @@
5854
<ItemGroup>
5955
<None Include="@(_NativeBinary)" />
6056
</ItemGroup>
61-
62-
<!--
63-
Prevent NuGet from doing a pointless current-RID build after us since we
64-
have already built all the RIDs the user requested.
65-
-->
66-
<ItemGroup>
67-
<_GenerateNuspecDependsOn Include="$(GenerateNuspecDependsOn)" />
68-
<_GenerateNuspecDependsOn Remove="Build" />
69-
</ItemGroup>
70-
71-
<PropertyGroup>
72-
<GenerateNuspecDependsOn>@(_GenerateNuspecDependsOn)</GenerateNuspecDependsOn>
73-
</PropertyGroup>
7457
</Target>
7558

7659
<Target Name="_PackNativeBinary"
7760
Returns="@(_NativeBinary)">
78-
<MSBuild Projects="$(MSBuildProjectFullPath)"
79-
Targets="Build"
80-
Condition="'$(NoBuild)' != 'true'" />
81-
8261
<PropertyGroup>
8362
<_PackageNativePath>runtimes</_PackageNativePath>
8463
<_PackageNativePath Condition="'$(OutputType)' == 'Exe'">tools</_PackageNativePath>
8564
</PropertyGroup>
8665

87-
<!--
88-
When NoBuild is set and a build for this RID has not been run, nothing
89-
will exist at TargetPath. This will cause NuGet to fail to pack later
90-
on, which is the behavior we want. So, do not add a condition here.
91-
-->
9266
<ItemGroup>
9367
<_NativeBinary Include="$(TargetPath)"
9468
Pack="true"

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
we will just override the target entirely.
99
-->
1010
<Target Name="VSTest"
11+
DependsOnTargets="$(VSTestDependsOn)"
1112
Condition="'$(CompilerMode)' == 'Zig' and '$(IsTestable)' == 'true'">
1213
<Message Text="Running tests natively..."
1314
Importance="high"

0 commit comments

Comments
 (0)