Skip to content

Commit

Permalink
Zig build api change (#15)
Browse files Browse the repository at this point in the history
* update api

* update readme

* add ci for zig 0.11

* update hash

* remove useless code, link library in module
  • Loading branch information
jinzhongjia authored Jan 5, 2024
1 parent 7b606cf commit 1aa827c
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 8 deletions.
14 changes: 13 additions & 1 deletion .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:
workflow_call:

jobs:
build:
build_nightly:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -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
14 changes: 13 additions & 1 deletion .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:
workflow_call:

jobs:
build:
build_nightly:
runs-on: macos-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -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
14 changes: 13 additions & 1 deletion .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:
workflow_call:

jobs:
build:
build_nightly:
runs-on: windows-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
150 changes: 147 additions & 3 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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);
}
}
4 changes: 2 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -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 = .{
Expand Down

0 comments on commit 1aa827c

Please sign in to comment.