From 0e9982755ad85c31355a11f4582e3cea06fd7e30 Mon Sep 17 00:00:00 2001 From: Chawye Hsu Date: Thu, 31 Oct 2024 03:37:55 +0800 Subject: [PATCH] build: Add zig build system support (#10) * build: Add zig build system support Signed-off-by: Chawye Hsu * note about recent zig requirement Signed-off-by: Chawye Hsu * add build.ps1 Signed-off-by: Chawye Hsu * build: move zig build script to build folder Signed-off-by: Chawye Hsu * build(zig): set subsystem to console Signed-off-by: Chawye Hsu * conditional corecrt_wstdio.h inclusion Signed-off-by: Chawye Hsu * build: fix build.ps1 and support building for x86 Signed-off-by: Chawye Hsu * build: update build.ps1 Signed-off-by: Chawye Hsu * chore: i386 -> ia32 Signed-off-by: Chawye Hsu --------- Signed-off-by: Chawye Hsu --- .gitignore | 6 +++- build/build.ps1 | 94 +++++++++++++++++++++++++++++++++++++++++++++++++ build/build.zig | 55 +++++++++++++++++++++++++++++ shim.cpp | 2 ++ 4 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 build/build.ps1 create mode 100644 build/build.zig diff --git a/.gitignore b/.gitignore index 112f792..ea616c5 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,8 @@ obj/ bin/ archive/ build/** -!build/*.ninja \ No newline at end of file +!build/*.ninja +!build/build.ps1 +!build/build.zig +.zig-cache/ +zig-out/ \ No newline at end of file diff --git a/build/build.ps1 b/build/build.ps1 new file mode 100644 index 0000000..82e6f27 --- /dev/null +++ b/build/build.ps1 @@ -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 diff --git a/build/build.zig b/build/build.zig new file mode 100644 index 0000000..652239d --- /dev/null +++ b/build/build.zig @@ -0,0 +1,55 @@ +const builtin = @import("builtin"); +const std = @import("std"); +const CrossTarget = std.Target.Query; + +// Usage: +// zig build -Dtarget= -Doptimize= +// 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); +} diff --git a/shim.cpp b/shim.cpp index 25ea794..71272bb 100644 --- a/shim.cpp +++ b/shim.cpp @@ -1,4 +1,6 @@ +#ifdef _MSC_VER #include +#endif #pragma comment(lib, "SHELL32.LIB") #pragma comment(lib, "SHLWAPI.LIB") #include