Skip to content

Commit

Permalink
build: Add zig build system support (#10)
Browse files Browse the repository at this point in the history
* build: Add zig build system support

Signed-off-by: Chawye Hsu <su+git@chawyehsu.com>

* note about recent zig requirement

Signed-off-by: Chawye Hsu <su+git@chawyehsu.com>

* add build.ps1

Signed-off-by: Chawye Hsu <su+git@chawyehsu.com>

* build: move zig build script to build folder

Signed-off-by: Chawye Hsu <su+git@chawyehsu.com>

* build(zig): set subsystem to console

Signed-off-by: Chawye Hsu <su+git@chawyehsu.com>

* conditional corecrt_wstdio.h inclusion

Signed-off-by: Chawye Hsu <su+git@chawyehsu.com>

* build: fix build.ps1 and support building for x86

Signed-off-by: Chawye Hsu <su+git@chawyehsu.com>

* build: update build.ps1

Signed-off-by: Chawye Hsu <su+git@chawyehsu.com>

* chore: i386 -> ia32

Signed-off-by: Chawye Hsu <su+git@chawyehsu.com>

---------

Signed-off-by: Chawye Hsu <su+git@chawyehsu.com>
  • Loading branch information
chawyehsu authored Oct 30, 2024
1 parent 606aaa1 commit 0e99827
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 1 deletion.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ obj/
bin/
archive/
build/**
!build/*.ninja
!build/*.ninja
!build/build.ps1
!build/build.zig
.zig-cache/
zig-out/
94 changes: 94 additions & 0 deletions build/build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#!/usr/bin/env pwsh
#Requires -Version 7

<#
.SYNOPSIS
Build shim.exe using Zig.
.PARAMETER BuildMode
The build mode. Valid values are Debug, ReleaseSafe, ReleaseFast, ReleaseSmall
Default is ReleaseSmall
.PARAMETER Target
The target architecture. Valid values are x86-windows-gnu, x86_64-windows-gnu, aarch64-windows-gnu
Default is undefined (all valid targets)
.PARAMETER Zip
Generate checksums and pack the artifacts into a zip file for distribution
#>
param(
[ValidateSet('Debug', 'ReleaseSafe', 'ReleaseFast', 'ReleaseSmall')]
[string]$BuildMode = "ReleaseSmall",
[ValidateSet('x86-windows-gnu', 'x86_64-windows-gnu', 'aarch64-windows-gnu')]
[string]$Target,
[switch]$Zip = $false
)

$oldErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = "Stop"

if (-not [bool](Get-Command zig -ErrorAction SilentlyContinue)) {
Write-Host "Zig is not installed. Please install Zig before running this script." -ForegroundColor Yellow
exit 1
}

Remove-Item -Path "$PSScriptRoot\zig-out" -Recurse -Force -ErrorAction SilentlyContinue

Push-Location $PSScriptRoot

if (-not $Target -or $Target -eq 'x86-windows-gnu') {
Write-Host "Build shim.exe for x86-windows-gnu target..." -ForegroundColor Cyan
Start-Process -FilePath "zig" -ArgumentList "build -Dtarget=x86-windows-gnu -Doptimize=$BuildMode" -Wait -NoNewWindow
Rename-Item -Path "$PSScriptRoot\zig-out\bin\shim.exe" -NewName "$PSScriptRoot\zig-out\bin\shim-ia32.exe"
}

if (-not $Target -or $Target -eq 'x86_64-windows-gnu') {
Write-Host "Build shim.exe for x86_64-windows-gnu target..." -ForegroundColor Cyan
Start-Process -FilePath "zig" -ArgumentList "build -Dtarget=x86_64-windows-gnu -Doptimize=$BuildMode" -Wait -NoNewWindow
Rename-Item -Path "$PSScriptRoot\zig-out\bin\shim.exe" -NewName "$PSScriptRoot\zig-out\bin\shim-amd64.exe"
}

if (-not $Target -or $Target -eq 'aarch64-windows-gnu') {
Write-Host "Build shim.exe for aarch64-windows-gnu target..." -ForegroundColor Cyan
Start-Process -FilePath "zig" -ArgumentList "build -Dtarget=aarch64-windows-gnu -Doptimize=$BuildMode" -Wait -NoNewWindow
Rename-Item -Path "$PSScriptRoot\zig-out\bin\shim.exe" -NewName "$PSScriptRoot\zig-out\bin\shim-aarch64.exe"
}

if ($Zip) {
Write-Host "Generate checksums..." -ForegroundColor Cyan

# shim-ia32.exe
if (-not $Target -or $Target -eq 'x86-windows-gnu') {
$sha256 = (Get-FileHash "$PSScriptRoot\zig-out\bin\shim-ia32.exe" -Algorithm SHA256).Hash.ToLower()
"$sha256 shim-ia32.exe" | Out-File "$PSScriptRoot\zig-out\bin\shim-ia32.exe.sha256"
$sha512 = (Get-FileHash "$PSScriptRoot\zig-out\bin\shim-ia32.exe" -Algorithm SHA512).Hash.ToLower()
"$sha512 shim-ia32.exe" | Out-File "$PSScriptRoot\zig-out\bin\shim-ia32.exe.sha512"
}

# shim-amd64.exe
if (-not $Target -or $Target -eq 'x86_64-windows-gnu') {
$sha256 = (Get-FileHash "$PSScriptRoot\zig-out\bin\shim-amd64.exe" -Algorithm SHA256).Hash.ToLower()
"$sha256 shim-amd64.exe" | Out-File "$PSScriptRoot\zig-out\bin\shim-amd64.exe.sha256"
$sha512 = (Get-FileHash "$PSScriptRoot\zig-out\bin\shim-amd64.exe" -Algorithm SHA512).Hash.ToLower()
"$sha512 shim-amd64.exe" | Out-File "$PSScriptRoot\zig-out\bin\shim-amd64.exe.sha512"
}

# shim-aarch64.exe
if (-not $Target -or $Target -eq 'aarch64-windows-gnu') {
$sha256 = (Get-FileHash "$PSScriptRoot\zig-out\bin\shim-aarch64.exe" -Algorithm SHA256).Hash.ToLower()
"$sha256 shim-aarch64.exe" | Out-File "$PSScriptRoot\zig-out\bin\shim-aarch64.exe.sha256"
$sha512 = (Get-FileHash "$PSScriptRoot\zig-out\bin\shim-aarch64.exe" -Algorithm SHA512).Hash.ToLower()
"$sha512 shim-aarch64.exe" | Out-File "$PSScriptRoot\zig-out\bin\shim-aarch64.exe.sha512"
}

Write-Host "Packaging..." -ForegroundColor Cyan

$version = (Get-Content "$PSScriptRoot\..\version").Trim()
Compress-Archive -Path "$PSScriptRoot\zig-out\bin\shim-*" -DestinationPath "$PSScriptRoot\zig-out\shimexe-$version.zip"

$sha256 = (Get-FileHash "$PSScriptRoot\zig-out\shimexe-$version.zip" -Algorithm SHA256).Hash.ToLower()
"$sha256 shimexe-$version.zip" | Out-File "$PSScriptRoot\zig-out\shimexe-$version.zip.sha256"
}

Write-Host "Artifacts available in $PSScriptRoot\zig-out" -ForegroundColor Green

Pop-Location

$ErrorActionPreference = $oldErrorActionPreference
55 changes: 55 additions & 0 deletions build/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const builtin = @import("builtin");
const std = @import("std");
const CrossTarget = std.Target.Query;

// Usage:
// zig build -Dtarget=<target> -Doptimize=<optimization level>
// Supported targets:
// x86-windows-gnu
// x86-windows-msvc
// x86_64-windows-gnu
// x86_64-windows-msvc
// aarch64-windows-gnu
// aarch64-windows-msvc

const required_version = std.SemanticVersion.parse("0.13.0") catch unreachable;
const compatible = builtin.zig_version.order(required_version) == .gt;

pub fn build(b: *std.Build) void {
if (!compatible) {
std.log.err("Unsupported Zig compiler version", .{});
return;
}

const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{ .default_target = CrossTarget{
.os_tag = .windows,
.abi = .gnu,
} });

if (target.result.os.tag != .windows) {
std.log.err("Non-Windows target is not supported", .{});
return;
}

const exe = b.addExecutable(.{
.name = "shim",
.target = target,
.optimize = optimize,
.win32_manifest = b.path("../shim.manifest"),
});

exe.addCSourceFile(.{ .file = b.path("../shim.cpp"), .flags = &.{"-std=c++20"} });
exe.linkSystemLibrary("shlwapi");

if (target.result.abi == .msvc) {
exe.linkLibC();
} else {
exe.linkLibCpp();
exe.subsystem = .Console;
// NOTE: This requires a recent Zig version (0.12.0-dev.3493+3661133f9 or later)
exe.mingw_unicode_entry_point = true;
}

b.installArtifact(exe);
}
2 changes: 2 additions & 0 deletions shim.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#ifdef _MSC_VER
#include <corecrt_wstdio.h>
#endif
#pragma comment(lib, "SHELL32.LIB")
#pragma comment(lib, "SHLWAPI.LIB")
#include <windows.h>
Expand Down

0 comments on commit 0e99827

Please sign in to comment.