-
Notifications
You must be signed in to change notification settings - Fork 294
Build with zig #431
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
Merged
Merged
Build with zig #431
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5eded15
build with Zig
inge4pres 50676c2
ci: add zig build workflow
inge4pres 595be4e
build(zig): aarch64 macros on Linux
inge4pres d79830b
CI: split zig build by platform
inge4pres 14af8d0
CI: release script updates version in build.zig.zon
inge4pres File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| name: aarch64 Linux Zig | ||
|
|
||
| on: [push, pull_request] | ||
|
|
||
| jobs: | ||
| run: | ||
| strategy: | ||
| matrix: | ||
| target: | ||
| - aarch64-linux-musl | ||
| - aarch64-linux-gnu | ||
| runs-on: ubuntu-24.04-arm | ||
| steps: | ||
| - uses: actions/checkout@v5 | ||
| - uses: mlugg/setup-zig@v2 | ||
| with: | ||
| version: latest | ||
| - name: Build | ||
| run: zig build -Dtarget=${{ matrix.target }} | ||
| - name: Run list_cpu_features | ||
| run: ./zig-out/bin/list_cpu_features --json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| name: amd64 Linux Zig | ||
|
|
||
| on: [push, pull_request] | ||
|
|
||
| jobs: | ||
| run: | ||
| strategy: | ||
| matrix: | ||
| target: | ||
| - x86_64-linux-musl | ||
| - x86_64-linux-gnu | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v5 | ||
| - uses: mlugg/setup-zig@v2 | ||
| with: | ||
| version: latest | ||
| - name: Build | ||
| run: zig build -Dtarget=${{ matrix.target }} | ||
| - name: Run list_cpu_features | ||
| run: ./zig-out/bin/list_cpu_features --json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| name: Zig Check | ||
|
|
||
| on: [push, pull_request] | ||
|
|
||
| jobs: | ||
| zig-fmt: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v5 | ||
| - uses: mlugg/setup-zig@v2 | ||
Mizux marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| with: | ||
| version: latest | ||
| - run: zig fmt --check . | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,3 +18,7 @@ MODULE.bazel.lock | |
|
|
||
| # Per-user bazelrc files | ||
| user.bazelrc | ||
|
|
||
| # Zig build artifacts | ||
| .zig-cache | ||
| zig-out | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,248 @@ | ||
| const std = @import("std"); | ||
|
|
||
| pub fn build(b: *std.Build) void { | ||
| // Standard target and optimization options | ||
| const target = b.standardTargetOptions(.{}); | ||
| const optimize = b.standardOptimizeOption(.{}); | ||
|
|
||
| // Build options (mirroring CMake options) | ||
| const build_executable = b.option(bool, "BUILD_EXECUTABLE", "Build list_cpu_features executable") orelse true; | ||
| const enable_install = b.option(bool, "ENABLE_INSTALL", "Enable install targets") orelse true; | ||
|
|
||
| // Create the cpu_features static library | ||
| const cpu_features = b.addLibrary(.{ | ||
| .name = "cpu_features", | ||
| .linkage = .static, | ||
| .root_module = b.createModule(.{ | ||
| .target = target, | ||
| .optimize = optimize, | ||
| .link_libc = true, | ||
| }), | ||
| }); | ||
|
|
||
| cpu_features.addIncludePath(b.path("include")); | ||
| cpu_features.addIncludePath(b.path("include/internal")); | ||
|
|
||
| // Public compile definitions | ||
| cpu_features.root_module.addCMacro("STACK_LINE_READER_BUFFER_SIZE", "1024"); | ||
|
|
||
| // Platform-specific defines | ||
| const os_tag = target.result.os.tag; | ||
| const cpu_arch = target.result.cpu.arch; | ||
|
|
||
| if (os_tag.isDarwin()) { | ||
| cpu_features.root_module.addCMacro("HAVE_SYSCTLBYNAME", "1"); | ||
| } else if (os_tag == .linux) { | ||
| // Linux (including musl) provides getauxval() for hardware capability detection | ||
| cpu_features.root_module.addCMacro("HAVE_STRONG_GETAUXVAL", "1"); | ||
| cpu_features.root_module.addCMacro("HAVE_DLFCN_H", "1"); | ||
| } | ||
|
|
||
| // Utility sources (always included) | ||
| const utility_sources = [_][]const u8{ | ||
| "src/filesystem.c", | ||
| "src/stack_line_reader.c", | ||
| "src/string_view.c", | ||
| }; | ||
|
|
||
| // Common C flags for all source files | ||
| const c_flags = [_][]const u8{ | ||
| "-Wall", | ||
| "-Wextra", | ||
| "-Wmissing-declarations", | ||
| "-Wmissing-prototypes", | ||
| "-Wno-implicit-fallthrough", | ||
| "-Wno-unused-function", | ||
| "-Wold-style-definition", | ||
| "-Wshadow", | ||
| "-Wsign-compare", | ||
| "-Wstrict-prototypes", | ||
| "-std=c99", | ||
| "-fno-sanitize=undefined", // Disable UBSan for C code with intentional unaligned access | ||
| }; | ||
|
|
||
| for (utility_sources) |source| { | ||
| cpu_features.addCSourceFile(.{ | ||
| .file = b.path(source), | ||
| .flags = &c_flags, | ||
| }); | ||
| } | ||
|
|
||
| // Unix-based hardware detection (for non-x86 Unix platforms) | ||
| // Note: Android is represented as Linux in Zig's target system | ||
| if (os_tag != .windows and !cpu_arch.isX86()) { | ||
| const hwcaps_sources = [_][]const u8{ | ||
| "src/hwcaps.c", | ||
| "src/hwcaps_linux_or_android.c", | ||
| "src/hwcaps_freebsd_or_openbsd.c", | ||
| }; | ||
|
|
||
| for (hwcaps_sources) |source| { | ||
| cpu_features.addCSourceFile(.{ | ||
| .file = b.path(source), | ||
| .flags = &c_flags, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| // Architecture-specific implementation files | ||
| // Determine which implementation files to include based on target architecture | ||
| // Note: Android is represented as Linux in Zig's target system | ||
| switch (cpu_arch) { | ||
| .x86, .x86_64 => { | ||
| // x86/x86_64 architecture | ||
| const source: ?[]const u8 = if (os_tag == .linux) | ||
| "src/impl_x86_linux_or_android.c" | ||
| else if (os_tag.isDarwin()) | ||
| "src/impl_x86_macos.c" | ||
| else if (os_tag == .windows) | ||
| "src/impl_x86_windows.c" | ||
| else if (os_tag == .freebsd) | ||
| "src/impl_x86_freebsd.c" | ||
| else | ||
| null; | ||
|
|
||
| if (source) |s| { | ||
| cpu_features.addCSourceFile(.{ | ||
| .file = b.path(s), | ||
| .flags = &c_flags, | ||
| }); | ||
| } | ||
| }, | ||
| .aarch64, .aarch64_be => { | ||
| // AArch64 architecture - always needs cpuid | ||
| cpu_features.addCSourceFile(.{ | ||
| .file = b.path("src/impl_aarch64_cpuid.c"), | ||
| .flags = &c_flags, | ||
| }); | ||
|
|
||
| const source: ?[]const u8 = if (os_tag == .linux) | ||
| "src/impl_aarch64_linux_or_android.c" | ||
| else if (os_tag.isDarwin()) | ||
| "src/impl_aarch64_macos_or_iphone.c" | ||
| else if (os_tag == .windows) | ||
| "src/impl_aarch64_windows.c" | ||
| else if (os_tag == .freebsd or os_tag == .openbsd) | ||
| "src/impl_aarch64_freebsd_or_openbsd.c" | ||
| else | ||
| null; | ||
|
|
||
| if (source) |s| { | ||
| cpu_features.addCSourceFile(.{ | ||
| .file = b.path(s), | ||
| .flags = &c_flags, | ||
| }); | ||
| } | ||
| }, | ||
| .arm, .armeb, .thumb, .thumbeb => { | ||
| // ARM (32-bit) architecture | ||
| if (os_tag == .linux) { | ||
| cpu_features.addCSourceFile(.{ | ||
| .file = b.path("src/impl_arm_linux_or_android.c"), | ||
| .flags = &c_flags, | ||
| }); | ||
| } | ||
| }, | ||
| .mips, .mipsel, .mips64, .mips64el => { | ||
| // MIPS architecture | ||
| if (os_tag == .linux) { | ||
| cpu_features.addCSourceFile(.{ | ||
| .file = b.path("src/impl_mips_linux_or_android.c"), | ||
| .flags = &c_flags, | ||
| }); | ||
| } | ||
| }, | ||
| .powerpc, .powerpcle, .powerpc64, .powerpc64le => { | ||
| // PowerPC architecture | ||
| if (os_tag == .linux) { | ||
| cpu_features.addCSourceFile(.{ | ||
| .file = b.path("src/impl_ppc_linux.c"), | ||
| .flags = &c_flags, | ||
| }); | ||
| } | ||
| }, | ||
| .riscv32, .riscv64 => { | ||
| // RISC-V architecture | ||
| if (os_tag == .linux) { | ||
| cpu_features.addCSourceFile(.{ | ||
| .file = b.path("src/impl_riscv_linux.c"), | ||
| .flags = &c_flags, | ||
| }); | ||
| } | ||
| }, | ||
| .s390x => { | ||
| // s390x architecture | ||
| if (os_tag == .linux) { | ||
| cpu_features.addCSourceFile(.{ | ||
| .file = b.path("src/impl_s390x_linux.c"), | ||
| .flags = &c_flags, | ||
| }); | ||
| } | ||
| }, | ||
| .loongarch64 => { | ||
| // LoongArch architecture | ||
| if (os_tag == .linux) { | ||
| cpu_features.addCSourceFile(.{ | ||
| .file = b.path("src/impl_loongarch_linux.c"), | ||
| .flags = &c_flags, | ||
| }); | ||
| } | ||
| }, | ||
| else => { | ||
| std.debug.print("Warning: Unsupported architecture {s}\n", .{@tagName(cpu_arch)}); | ||
| }, | ||
| } | ||
|
|
||
| // Link against dl library on Unix-like systems | ||
| if (os_tag != .windows and os_tag != .wasi) { | ||
| cpu_features.linkSystemLibrary("dl"); | ||
| } | ||
|
|
||
| // Install the library if enabled | ||
| if (enable_install) { | ||
| b.installArtifact(cpu_features); | ||
|
|
||
| // Install headers | ||
| const install_headers = b.addInstallDirectory(.{ | ||
| .source_dir = b.path("include"), | ||
| .install_dir = .header, | ||
| .install_subdir = "cpu_features", | ||
| .exclude_extensions = &.{}, | ||
| }); | ||
| b.getInstallStep().dependOn(&install_headers.step); | ||
| } | ||
|
|
||
| // Build list_cpu_features executable if requested | ||
| if (build_executable) { | ||
| const list_cpu_features = b.addExecutable(.{ | ||
| .name = "list_cpu_features", | ||
| .root_module = b.createModule(.{ | ||
| .target = target, | ||
| .optimize = optimize, | ||
| .link_libc = true, | ||
| }), | ||
| }); | ||
|
|
||
| list_cpu_features.linkLibrary(cpu_features); | ||
| list_cpu_features.addIncludePath(b.path("include")); | ||
|
|
||
| list_cpu_features.addCSourceFile(.{ | ||
| .file = b.path("src/utils/list_cpu_features.c"), | ||
| .flags = &c_flags, | ||
| }); | ||
|
|
||
| if (enable_install) { | ||
| b.installArtifact(list_cpu_features); | ||
| } | ||
|
|
||
| // Add a run step for convenience | ||
| const run_cmd = b.addRunArtifact(list_cpu_features); | ||
| run_cmd.step.dependOn(b.getInstallStep()); | ||
| if (b.args) |args| { | ||
| run_cmd.addArgs(args); | ||
| } | ||
|
|
||
| const run_step = b.step("run", "Run list_cpu_features"); | ||
| run_step.dependOn(&run_cmd.step); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| .{ | ||
| .name = .cpu_features, | ||
| .version = "0.0.1", | ||
Mizux marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .minimum_zig_version = "0.15.1", | ||
| .paths = .{ | ||
| "build.zig", | ||
| "build.zig.zon", | ||
| "src", | ||
| "include", | ||
| }, | ||
| .fingerprint = 0xbd52e965d3c20004, | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.