Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate zig package #6

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@ $ dotnet publish -r linux-x64
Microsoft.NETCore.Native.Publish.targets(59,5): error : Cross-OS native compilation is not supported.
```

This nuget package allows using [Zig](https://ziglang.org/) as the linker/sysroot to allow crosscompiling to linux-x64/linux-arm64/linux-musl-x64/linux-musl-arm64 from a Windows machine.
This NuGet package allows using [Zig](https://ziglang.org/) as the linker/sysroot to allow crosscompiling to linux-x64/linux-arm64/linux-musl-x64/linux-musl-arm64 from a Windows machine.

1. [Download](https://ziglang.org/download/) an archive with Zig for your host machine, extract it and place it on your PATH. I'm using zig-windows-x86_64-0.11.0-dev.4006+bf827d0b5.zip.
2. Optional: [download](https://releases.llvm.org/) LLVM. We only need llvm-objcopy executable so if you care about size, you can delete the rest. The executable needs to be on PATH - you could copy it next to the Zig executable. This step is optional and is required only to strip symbols (make the produced executables smaller). If you don't care about stripping symbols, you can skip it.
3. To your project that is already using Native AOT, add a reference to this NuGet package.
4. Publish for one of the newly available RIDs:
By default it relies on Zig provided by the unofficial [Vezel.Zig.Toolsets](https://github.com/vezel-dev/zig-toolsets) NuGet package. You can specify version of this package using the `ZigVersion` property. Instructions for using your own Zig binaries are near the end of this document.

1. Optional: [download](https://releases.llvm.org/) LLVM. We only need llvm-objcopy executable so if you care about size, you can delete the rest. The executable needs to be on PATH. This step is optional and is required only to strip symbols (make the produced executables smaller). If you don't care about stripping symbols, you can skip it.
2. To your project that is already using Native AOT, add a reference to this NuGet package.
3. Publish for one of the newly available RIDs:
* `dotnet publish -r linux-x64`
* `dotnet publish -r linux-arm64`
* `dotnet publish -r linux-musl-x64`
* `dotnet publish -r linux-musl-arm64`

If you skipped the second optional step to download llvm-objcopy, you must also pass `/p:StripSymbols=false` to the publish command, or you'll see an error instructing you to do that.


If you don't want to use Zig from the Vezel.Zig.Toolsets NuGet package, you can specify `/p:UseExternalZig=true`. This will use whatever Zig is on your PATH. [Download](https://ziglang.org/download/) an archive with Zig for your host machine, extract it and place it on your PATH.


Even though Zig allows crosscompiling for Windows as well, it's not possible to crosscompile PublishAot like this due to ABI differences (MSVC vs. MingW ABI).
25 changes: 25 additions & 0 deletions src/Crosscompile.targets
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@

<PropertyGroup>
<DisableUnsupportedError>true</DisableUnsupportedError>
<HostRuntimeIdentifier Condition="'$(HostRuntimeIdentifier)' == ''">$(NETCoreSdkPortableRuntimeIdentifier)</HostRuntimeIdentifier>
<ZigVersion Condition="'$(ZigVersion)' == ''">0.11.0.1</ZigVersion>
<UseExternalZig Condition="'$(UseExternalZig)' == ''">false</UseExternalZig>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Vezel.Zig.Toolsets.$(HostRuntimeIdentifier)"
Version="$(ZigVersion)"
PrivateAssets="all"
IsImplicitlyDefined="true"
GeneratePathProperty="true"
Condition="'$(UseExternalZig)' == 'false'" />
</ItemGroup>

<UsingTask TaskName="PrependPath"
TaskFactory="RoslynCodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
Expand Down Expand Up @@ -31,6 +43,19 @@

</Target>

<Target Name="SetPathToZig"
BeforeTargets="SetupOSSpecificProps">

<PropertyGroup Condition="$(HostRuntimeIdentifier.StartsWith('win-'))">
<ZigPath Condition="$(HostRuntimeIdentifier.EndsWith('-x86'))">$(PkgVezel_Zig_Toolsets_win-x86)/tools</ZigPath>
<ZigPath Condition="$(HostRuntimeIdentifier.EndsWith('-x64'))">$(PkgVezel_Zig_Toolsets_win-x64)/tools</ZigPath>
<ZigPath Condition="$(HostRuntimeIdentifier.EndsWith('-arm64'))">$(PkgVezel_Zig_Toolsets_win-arm64)/tools</ZigPath>
</PropertyGroup>

<PrependPath Condition="'$(UseExternalZig)' == 'false'" Value="$(ZigPath)" />

</Target>

<!-- BEGIN: Works around ILCompiler targets not detecting this as a cross compilation -->
<Target Name="OverwriteTargetTriple"
AfterTargets="SetupOSSpecificProps"
Expand Down