From 1aa827c66f325f86f331d8283935fbefd49c15a0 Mon Sep 17 00:00:00 2001 From: jinzhongjia Date: Sat, 6 Jan 2024 00:34:24 +0800 Subject: [PATCH] Zig build api change (#15) * update api * update readme * add ci for zig 0.11 * update hash * remove useless code, link library in module --- .github/workflows/linux.yml | 14 +++- .github/workflows/macos.yml | 14 +++- .github/workflows/windows.yml | 14 +++- README.md | 2 + build.zig | 150 +++++++++++++++++++++++++++++++++- build.zig.zon | 4 +- 6 files changed, 190 insertions(+), 8 deletions(-) diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml index 4040047..565c282 100644 --- a/.github/workflows/linux.yml +++ b/.github/workflows/linux.yml @@ -11,7 +11,7 @@ on: workflow_call: jobs: - build: + build_nightly: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 @@ -20,3 +20,15 @@ jobs: run: zig build build_all - name: build_examples_dynamic run: zig build build_all -Dis_static=false + + build_release: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: goto-bus-stop/setup-zig@v2 + with: + version: 0.11.0 + - name: build_examples + run: zig build build_all + - name: build_examples_dynamic + run: zig build build_all -Dis_static=false diff --git a/.github/workflows/macos.yml b/.github/workflows/macos.yml index 505f924..cc8cef5 100644 --- a/.github/workflows/macos.yml +++ b/.github/workflows/macos.yml @@ -11,7 +11,7 @@ on: workflow_call: jobs: - build: + build_nightly: runs-on: macos-latest steps: - uses: actions/checkout@v3 @@ -20,3 +20,15 @@ jobs: run: zig build build_all - name: build_examples_dynamic run: zig build build_all -Dis_static=false + + build_release: + runs-on: macos-latest + steps: + - uses: actions/checkout@v3 + - uses: goto-bus-stop/setup-zig@v2 + with: + version: 0.11.0 + - name: build_examples + run: zig build build_all + - name: build_examples_dynamic + run: zig build build_all -Dis_static=false diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index 84e2bd3..7b8c7d1 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -11,7 +11,7 @@ on: workflow_call: jobs: - build: + build_nightly: runs-on: windows-latest steps: - uses: actions/checkout@v3 @@ -20,3 +20,15 @@ jobs: run: zig build build_all - name: build_examples_dynamic run: zig build build_all -Dis_static=false + + build_release: + runs-on: windows-latest + steps: + - uses: actions/checkout@v3 + - uses: goto-bus-stop/setup-zig@v2 + with: + version: 0.11.0 + - name: build_examples + run: zig build build_all + - name: build_examples_dynamic + run: zig build build_all -Dis_static=false diff --git a/README.md b/README.md index 871b1d9..0ca962e 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,8 @@ exe.linkLibrary(zig_webui.artifact("webui")); ### Zig `nightly` +> To be honest, I don’t recommend using the nightly version because the API of the build system is not yet stable, which means that there may be problems with not being able to build after nightly is updated. + 1. Add to `build.zig.zon` ```sh diff --git a/build.zig b/build.zig index 91a7bca..5cd0716 100644 --- a/build.zig +++ b/build.zig @@ -26,6 +26,14 @@ comptime { } pub fn build(b: *Build) void { + if (current_zig.minor == 11) { + build_11(b); + } else if (current_zig.minor == 12) { + build_12(b); + } +} + +fn build_11(b: *Build) void { const isStatic = b.option(bool, "is_static", "whether lib is static") orelse default_isStatic; const enableTLS = b.option(bool, "enable_tls", "whether lib enable tls") orelse default_enableTLS; @@ -70,13 +78,62 @@ pub fn build(b: *Build) void { .is_static = isStatic, }).artifact("webui"); - b.installArtifact(webui); + // build examples + build_examples_11(b, optimize, target, webui_module, webui); +} + +fn build_12(b: *Build) void { + const isStatic = b.option(bool, "is_static", "whether lib is static") orelse default_isStatic; + const enableTLS = b.option(bool, "enable_tls", "whether lib enable tls") orelse default_enableTLS; + + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + log.info("link mode is {s}", .{if (isStatic) "static" else "dynamic"}); + + if (enableTLS) { + log.info("enable TLS support", .{}); + if (!target.query.isNative()) { + log.info("when enable tls, not support cross compile", .{}); + std.os.exit(1); + } + } + + // create a options for command paramter + const flags_options = b.addOptions(); + + // add option + flags_options.addOption(bool, "enableTLS", enableTLS); + + // create a new module for flags options + const flags_module = flags_options.createModule(); + + const webui = b.dependency("webui", .{ + .target = target, + .optimize = optimize, + .enable_tls = enableTLS, + .is_static = isStatic, + }).artifact("webui"); + + const webui_module = b.addModule("webui", .{ + .root_source_file = .{ + .path = "src/webui.zig", + }, + .imports = &.{ + .{ + .name = "flags", + .module = flags_module, + }, + }, + }); + + webui_module.linkLibrary(webui); // build examples - build_examples(b, optimize, target, webui_module, webui); + build_examples_12(b, optimize, target, webui_module, webui); } -fn build_examples(b: *Build, optimize: OptimizeMode, target: CrossTarget, webui_module: *Module, webui_lib: *Compile) void { +fn build_examples_11(b: *Build, optimize: OptimizeMode, target: CrossTarget, webui_module: *Module, webui_lib: *Compile) void { // we use lazyPath to get absolute path of package var lazy_path = Build.LazyPath{ .path = "src/examples", @@ -162,3 +219,90 @@ fn build_examples(b: *Build, optimize: OptimizeMode, target: CrossTarget, webui_ std.os.exit(1); } } + +fn build_examples_12(b: *Build, optimize: OptimizeMode, target: Build.ResolvedTarget, webui_module: *Module, webui_lib: *Compile) void { + // we use lazyPath to get absolute path of package + var lazy_path = Build.LazyPath{ + .path = "src/examples", + }; + + const build_all_step = b.step("build_all", "build all examples"); + + const examples_path = lazy_path.getPath(b); + var iter_dir = if (comptime current_zig.minor == 11) + std.fs.openIterableDirAbsolute(examples_path, .{}) catch |err| { + log.err("open examples_path failed, err is {}", .{err}); + std.os.exit(1); + } + else + std.fs.openDirAbsolute(examples_path, .{ .iterate = true }) catch |err| { + log.err("open examples_path failed, err is {}", .{err}); + std.os.exit(1); + }; + defer iter_dir.close(); + + var itera = iter_dir.iterate(); + + while (itera.next()) |val| { + if (val) |entry| { + if (entry.kind == .directory) { + const example_name = entry.name; + const path = std.fmt.allocPrint(b.allocator, "src/examples/{s}/main.zig", .{example_name}) catch |err| { + log.err("fmt path for examples failed, err is {}", .{err}); + std.os.exit(1); + }; + + const exe = b.addExecutable(.{ + .name = example_name, + .root_source_file = .{ .path = path }, + .target = target, + .optimize = optimize, + }); + + exe.root_module.addImport("webui", webui_module); + exe.linkLibrary(webui_lib); + + const exe_install = b.addInstallArtifact(exe, .{}); + + build_all_step.dependOn(&exe_install.step); + + const exe_run = b.addRunArtifact(exe); + exe_run.step.dependOn(&exe_install.step); + + if (comptime (current_zig.minor > 11)) { + const cwd = std.fmt.allocPrint(b.allocator, "src/examples/{s}", .{example_name}) catch |err| { + log.err("fmt path for examples failed, err is {}", .{err}); + std.os.exit(1); + }; + exe_run.setCwd(.{ + .path = cwd, + }); + } else { + const cwd = std.fmt.allocPrint(b.allocator, "{s}/{s}", .{ examples_path, example_name }) catch |err| { + log.err("fmt path for examples failed, err is {}", .{err}); + std.os.exit(1); + }; + exe_run.cwd = cwd; + } + + const step_name = std.fmt.allocPrint(b.allocator, "run_{s}", .{example_name}) catch |err| { + log.err("fmt step_name for examples failed, err is {}", .{err}); + std.os.exit(1); + }; + + const step_desc = std.fmt.allocPrint(b.allocator, "run_{s}", .{example_name}) catch |err| { + log.err("fmt step_desc for examples failed, err is {}", .{err}); + std.os.exit(1); + }; + + const exe_run_step = b.step(step_name, step_desc); + exe_run_step.dependOn(&exe_run.step); + } + } else { + break; + } + } else |err| { + log.err("iterate examples_path failed, err is {}", .{err}); + std.os.exit(1); + } +} diff --git a/build.zig.zon b/build.zig.zon index 22425d3..a39c8d5 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -4,8 +4,8 @@ .minimum_zig_version = "0.11.0", .dependencies = .{ .webui = .{ - .url = "https://github.com/webui-dev/webui/archive/70def025c8116be8bc79b3eba441933aa6efe575.tar.gz", - .hash = "12209f377933ee6336dbdec9fad612c483f4ea0b276c9b9000a5b9b1d783092fb4f2", + .url = "https://github.com/webui-dev/webui/archive/c56260ff0de8f13a49559ca592009093a5279e25.tar.gz", + .hash = "1220b7199d576b0aa86449975518dfd7c694c12ea348b8c13223332e265216497c3b", }, }, .paths = .{