Skip to content
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

Enhance #18

Merged
merged 7 commits into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .github/workflows/schedule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# http://webui.me
# https://github.com/webui-dev/webui
# Copyright (c) 2020-2023 Hassan Draga.
# Licensed under MIT License.
# All rights reserved.
#
# Special Thanks to Turiiya (https://github.com/ttytm)
name: schedule

on:
schedule:
- cron: '0 2 * * *'

jobs:
windows:
uses: ./.github/workflows/windows.yml
permissions:
contents: write

macos:
uses: ./.github/workflows/macos.yml
permissions:
contents: write

linux:
uses: ./.github/workflows/linux.yml
permissions:
contents: write
8 changes: 4 additions & 4 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ jobs:
- uses: goto-bus-stop/setup-zig@v2
- name: build_examples_static
run: zig build build_all
- name: build_examples_dynamic
run: zig build build_all -Dis_static=false
# - name: build_examples_dynamic
# run: zig build build_all -Dis_static=false

build_release:
runs-on: windows-latest
Expand All @@ -30,5 +30,5 @@ jobs:
version: 0.11.0
- name: build_examples
run: zig build build_all
- name: build_examples_dynamic
run: zig build build_all -Dis_static=false
# - name: build_examples_dynamic
# run: zig build build_all -Dis_static=false
289 changes: 2 additions & 287 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
const std = @import("std");
const builtin = @import("builtin");
const build_11 = @import("build_11.zig").build_11;
const build_12 = @import("build_12.zig").build_12;

const Build = std.Build;
const OptimizeMode = std.builtin.OptimizeMode;
const CrossTarget = std.zig.CrossTarget;
const Compile = Build.Step.Compile;
const Module = Build.Module;

const log = std.log.scoped(.WebUI);

const min_zig_string = "0.11.0";

const default_isStatic = true;
const default_enableTLS = false;

const current_zig = builtin.zig_version;

// NOTE: we should note that when enable tls support we cannot compile with musl
Expand All @@ -32,281 +25,3 @@ pub fn build(b: *Build) void {
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;

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.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_module = b.addModule("webui", .{
.source_file = .{
.path = "src/webui.zig",
},
.dependencies = &.{
.{
.name = "flags",
.module = flags_module,
},
},
});

const webui = b.dependency("webui", .{
.target = target,
.optimize = optimize,
.enable_tls = enableTLS,
.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");

b.installArtifact(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_12(b, optimize, target, webui_module, webui);
}

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",
};

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.addModule("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);
}
}

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);
}
}
Loading
Loading