Skip to content

Commit

Permalink
update to zig 0.12
Browse files Browse the repository at this point in the history
  • Loading branch information
MoAlyousef committed May 5, 2024
1 parent afe3c2b commit 6b21c60
Show file tree
Hide file tree
Showing 19 changed files with 113 additions and 79 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- uses: actions/checkout@v2
- uses: goto-bus-stop/setup-zig@v2
with:
version: 0.11.0
version: 0.12.0
- name: Build
run: zig build -Dzfltk-use-zigcc=true
build_macos:
Expand All @@ -25,7 +25,7 @@ jobs:
- uses: actions/checkout@v2
- uses: goto-bus-stop/setup-zig@v2
with:
version: 0.11.0
version: 0.12.0
- name: Build
run: zig build -Dzfltk-use-zigcc=true
build_windows-mingw:
Expand All @@ -40,17 +40,17 @@ jobs:
run: |
pacman -Sy
pacman --noconfirm -S $MINGW_PACKAGE_PREFIX-cmake $MINGW_PACKAGE_PREFIX-gcc $MINGW_PACKAGE_PREFIX-ninja wget unzip git --needed
wget https://ziglang.org/builds/zig-windows-x86_64-0.11.0.zip
unzip -qq zig-windows-x86_64-0.11.0.zip
wget https://ziglang.org/builds/zig-windows-x86_64-0.12.0.zip
unzip -qq zig-windows-x86_64-0.12.0.zip
git submodule update --init --recursive
zig-windows-x86_64-0.11.0/zig build --search-prefix $MINGW_PREFIX
zig-windows-x86_64-0.12.0/zig build --search-prefix $MINGW_PREFIX
# build_windows:
# runs-on: windows-latest
# steps:
# - uses: actions/checkout@v2
# - uses: goto-bus-stop/setup-zig@v2
# with:
# version: 0.11.0
# version: 0.12.0
# - uses: seanmiddleditch/gha-setup-ninja@master
# - name: build
# shell: bash
Expand Down
37 changes: 29 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@ If you're using the official package manager:
.{
.name = "app",
.version = "0.0.1",
.paths = .{
"src",
"build.zig",
"build.zig.zon",
"LICENSE",
"README.md",
}
.dependencies = .{
.zfltk = .{
.url = "https://github.com/MoAlyousef/zfltk/archive/refs/tags/pkg0.1.0.tar.gz",
.url = "https://github.com/MoAlyousef/zfltk/archive/refs/tags/pkg0.2.0.tar.gz",
},
}
}
Expand All @@ -48,7 +55,7 @@ pub fn build(b: *Build) !void {
const sdk = try Sdk.init(b);
const zfltk_module = sdk.getZfltkModule(b);
exe.addModule("zfltk", zfltk_module);
exe.root_module.addImport("zfltk", zfltk_module);
try sdk.link(exe);
b.installArtifact(exe);
Expand Down Expand Up @@ -100,7 +107,7 @@ pub fn build(b: *Build) !void {
const sdk = try Sdk.init(b);
const zfltk_module = sdk.getZfltkModule(b);
exe.addModule("zfltk", zfltk_module);
exe.root_module.addImport("zfltk", zfltk_module);
try sdk.link(exe);
b.installArtifact(exe);
Expand Down Expand Up @@ -282,23 +289,37 @@ pub fn main() !void {
Using the C Api directly:
```zig
const c = @cImport({
@cInclude("cfl.h"); // Fl_run
@cInclude("cfl.h"); // Fl_init_all, Fl_run
@cInclude("cfl_enums.h"); // Fl_Color_*
@cInclude("cfl_image.h"); // Fl_register_images
@cInclude("cfl_button.h"); // Fl_Button
@cInclude("cfl_box.h"); // Fl_Box
@cInclude("cfl_window.h"); // Fl_Window
});
// fltk initizialization of optional functionalities
pub fn fltkInit() void {
c.Fl_init_all(); // inits all styles, if needed
c.Fl_register_images(); // register image types supported by fltk, if needed
_ = c.Fl_lock(); // enable multithreading, if needed
}
// Button callback
pub fn butCb(w: ?*c.Fl_Widget, data: ?*anyopaque) callconv(.C) void {
c.Fl_Box_set_label(@ptrCast(data), "Hello World!");
c.Fl_Button_set_color(@ptrCast(w), c.Fl_Color_Cyan);
}
pub fn main() void {
pub fn main() !void {
fltkInit();
_ = c.Fl_set_scheme("gtk+");
var win = c.Fl_Window_new(100, 100, 400, 300, "Hello");
var but = c.Fl_Button_new(160, 220, 80, 40, "Click me!");
var box = c.Fl_Box_new(10, 10, 380, 180, "");
const win = c.Fl_Window_new(100, 100, 400, 300, "Hello");
const but = c.Fl_Button_new(160, 220, 80, 40, "Click me!");
const box = c.Fl_Box_new(10, 10, 380, 180, "");
c.Fl_Box_set_box(box, c.Fl_BoxType_UpBox);
c.Fl_Box_set_label_size(box, 18);
c.Fl_Box_set_label_font(box, c.Fl_Font_Courier);
c.Fl_Window_end(win);
c.Fl_Window_show(win);
c.Fl_Button_set_callback(but, butCb, box);
Expand Down
10 changes: 6 additions & 4 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const std = @import("std");
const Build = std.Build;
const CompileStep = std.build.CompileStep;
const CompileStep = Build.Step.Compile;
const utils = @import("build_utils.zig");

pub const SdkOpts = struct {
Expand Down Expand Up @@ -56,9 +56,11 @@ pub fn initWithOpts(b: *Build, opts: SdkOpts) !*Sdk {

pub fn getZfltkModule(sdk: *Sdk, b: *Build) *Build.Module {
_ = sdk;
return b.createModule(.{
.source_file = .{ .path = utils.thisDir() ++ "/src/zfltk.zig" },
var mod = b.createModule(.{
.root_source_file = .{ .path = utils.thisDir() ++ "/src/zfltk.zig" },
});
mod.addIncludePath(b.path("zig-out/cfltk/include"));
return mod;
}

pub fn link(sdk: *Sdk, exe: *CompileStep) !void {
Expand Down Expand Up @@ -86,7 +88,7 @@ pub fn build(b: *Build) !void {
.optimize = mode,
.target = target,
});
exe.addModule("zfltk", zfltk_module);
exe.root_module.addImport("zfltk", zfltk_module);
try sdk.link(exe);
examples_step.dependOn(&exe.step);
b.installArtifact(exe);
Expand Down
10 changes: 9 additions & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
.{
.name = "zfltk",
.version = "0.0.1",
.version = "0.0.2", // zig 0.12.0
.paths = .{
"src",
"build.zig",
"build_utils.zig",
"build.zig.zon",
"LICENSE",
"README.md",
}
}
83 changes: 43 additions & 40 deletions build_utils.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const std = @import("std");
const Build = std.Build;
const CompileStep = Build.CompileStep;
const CompileStep = Build.Step.Compile;

pub const FinalOpts = struct {
use_wayland: bool = false,
Expand Down Expand Up @@ -51,7 +51,7 @@ pub const examples = &[_]Example{
};

pub fn cfltk_build_from_source(b: *Build, finalize_cfltk: *Build.Step, install_prefix: []const u8, opts: FinalOpts) !void {
const zig_exe = b.zig_exe;
const zig_exe = b.graph.zig_exe;
var zig_cc_buf: [250]u8 = undefined;
var zig_cpp_buf: [250]u8 = undefined;
const use_zig_cc = switch (opts.use_zig_cc) {
Expand All @@ -62,29 +62,29 @@ pub fn cfltk_build_from_source(b: *Build, finalize_cfltk: *Build.Step, install_p
false => "",
true => try std.fmt.bufPrint(zig_cpp_buf[0..], "-DCMAKE_CXX_COMPILER={s};c++", .{zig_exe}),
};
const target = b.host.target;
const target = b.host.result;
var buf: [1024]u8 = undefined;
var sdk_lib_dir = try std.fmt.bufPrint(buf[0..], "{s}/cfltk/lib", .{install_prefix});
const sdk_lib_dir = try std.fmt.bufPrint(buf[0..], "{s}/cfltk/lib", .{install_prefix});
_ = std.fs.cwd().openDir(sdk_lib_dir, .{}) catch |err| {
std.debug.print("Warning: {!}. The cfltk library will be rebuilt from source!\n", .{err});
var bin_buf: [250]u8 = undefined;
var src_buf: [250]u8 = undefined;
var inst_buf: [250]u8 = undefined;
var cmake_bin_path = try std.fmt.bufPrint(bin_buf[0..], "{s}/cfltk/bin", .{install_prefix});
var cmake_src_path = try std.fmt.bufPrint(src_buf[0..], "{s}/cfltk", .{install_prefix});
var cmake_inst_path = try std.fmt.bufPrint(inst_buf[0..], "-DCMAKE_INSTALL_PREFIX={s}/cfltk/lib", .{install_prefix});
const cmake_bin_path = try std.fmt.bufPrint(bin_buf[0..], "{s}/cfltk/bin", .{install_prefix});
const cmake_src_path = try std.fmt.bufPrint(src_buf[0..], "{s}/cfltk", .{install_prefix});
const cmake_inst_path = try std.fmt.bufPrint(inst_buf[0..], "-DCMAKE_INSTALL_PREFIX={s}/cfltk/lib", .{install_prefix});
var zfltk_config: *std.Build.Step.Run = undefined;
const which_png = switch (opts.system_png) {
false => "-DOPTION_USE_SYSTEM_LIBPNG=OFF",
true => "-DOPTION_USE_SYSTEM_LIBPNG=ON",
false => "-DFLTK_USE_SYSTEM_LIBPNG=OFF",
true => "-DFLTK_USE_SYSTEM_LIBPNG=ON",
};
const which_jpeg = switch (opts.system_jpeg) {
false => "-DOPTION_USE_SYSTEM_LIBJPEG=OFF",
true => "-DOPTION_USE_SYSTEM_LIBJPEG=ON",
false => "-DFLTK_USE_SYSTEM_LIBJPEG=OFF",
true => "-DFLTK_USE_SYSTEM_LIBJPEG=ON",
};
const which_zlib = switch (opts.system_zlib) {
false => "-DOPTION_USE_SYSTEM_ZLIB=OFF",
true => "-DOPTION_USE_SYSTEM_ZLIB=ON",
false => "-DFLTK_USE_SYSTEM_ZLIB=OFF",
true => "-DFLTK_USE_SYSTEM_ZLIB=ON",
};
if (target.os.tag == .windows) {
zfltk_config = b.addSystemCommand(&[_][]const u8{
Expand All @@ -102,7 +102,7 @@ pub fn cfltk_build_from_source(b: *Build, finalize_cfltk: *Build.Step, install_p
which_png,
which_jpeg,
which_zlib,
"-DOPTION_USE_GL=ON",
"-DFLTK_BUILD_GL=ON",
"-DCFLTK_USE_OPENGL=ON",
"-DFLTK_BUILD_FLUID=OFF",
"-DFLTK_BUILD_FLTK_OPTIONS=OFF",
Expand All @@ -122,7 +122,7 @@ pub fn cfltk_build_from_source(b: *Build, finalize_cfltk: *Build.Step, install_p
which_png,
which_jpeg,
which_zlib,
"-DOPTION_USE_GL=ON",
"-DFLTK_BUILD_GL=ON",
"-DCFLTK_USE_OPENGL=ON",
"-DFLTK_BUILD_FLUID=OFF",
"-DFLTK_BUILD_FLTK_OPTIONS=OFF",
Expand All @@ -143,12 +143,12 @@ pub fn cfltk_build_from_source(b: *Build, finalize_cfltk: *Build.Step, install_p
which_png,
which_jpeg,
which_zlib,
"-DOPTION_USE_GL=ON",
"-DFLTK_BUILD_GL=ON",
"-DCFLTK_USE_OPENGL=ON",
"-DOPTION_USE_WAYLAND=ON",
"-DFLTK_BACKEND_WAYLAND=ON",
"-DFLTK_BUILD_FLUID=OFF",
"-DFLTK_BUILD_FLTK_OPTIONS=OFF",
"-DOPTION_ALLOW_GTK_PLUGIN=OFF",
"-DFLTK_USE_LIBDECOR_GTK=OFF",
});
} else {
zfltk_config = b.addSystemCommand(&[_][]const u8{
Expand All @@ -165,16 +165,17 @@ pub fn cfltk_build_from_source(b: *Build, finalize_cfltk: *Build.Step, install_p
which_png,
which_jpeg,
which_zlib,
"-DOPTION_USE_PANGO=ON", // enable if rtl/cjk font support is needed
"-DOPTION_USE_GL=ON",
"-DFLTK_USE_PANGO=ON", // enable if rtl/cjk font support is needed
"-DFLTK_BUILD_GL=ON",
"-DCFLTK_USE_OPENGL=ON",
"-DOPTION_USE_WAYLAND=OFF",
"-DOPTION_USE_CAIRO=ON",
"-DFLTK_BACKEND_WAYLAND=OFF",
"-DFLTK_GRAPHICS_CAIRO=ON",
"-DFLTK_BUILD_FLUID=OFF",
"-DFLTK_BUILD_FLTK_OPTIONS=OFF",
});
}
}
zfltk_config.setCwd(.{ .path = cmake_src_path});
_ = std.fs.cwd().openDir(cmake_src_path, .{}) catch |git_err| {
std.debug.print("Warning: {!}. The cfltk library will be grabbed!\n", .{git_err});
const cfltk_fetch = b.addSystemCommand(&[_][]const u8{ "git", "clone", "https://github.com/MoAlyousef/cfltk", cmake_src_path, "--depth=1", "--recurse-submodules" });
Expand All @@ -192,6 +193,7 @@ pub fn cfltk_build_from_source(b: *Build, finalize_cfltk: *Build.Step, install_p
"--parallel",
jobs,
});
zfltk_build.setCwd(.{ .path = cmake_src_path});
zfltk_build.step.dependOn(&zfltk_config.step);

// This only needs to run once!
Expand All @@ -200,18 +202,19 @@ pub fn cfltk_build_from_source(b: *Build, finalize_cfltk: *Build.Step, install_p
"--install",
cmake_bin_path,
});
zfltk_install.setCwd(.{ .path = cmake_src_path});
zfltk_install.step.dependOn(&zfltk_build.step);
finalize_cfltk.dependOn(&zfltk_install.step);
};
}

pub fn cfltk_link(exe: *CompileStep, install_prefix: []const u8, opts: FinalOpts) !void {
var buf: [1024]u8 = undefined;
const target = exe.target;
var inc_dir = try std.fmt.bufPrint(buf[0..], "{s}/cfltk/include", .{install_prefix});
exe.addIncludePath(Build.LazyPath{ .path = inc_dir });
var lib_dir = try std.fmt.bufPrint(buf[0..], "{s}/cfltk/lib/lib", .{install_prefix});
exe.addLibraryPath(Build.LazyPath{ .path = lib_dir });
const target = exe.root_module.resolved_target.?.result;
const inc_dir = try std.fmt.bufPrint(buf[0..], "{s}/cfltk/include", .{install_prefix});
exe.addIncludePath(.{ .path = inc_dir });
const lib_dir = try std.fmt.bufPrint(buf[0..], "{s}/cfltk/lib/lib", .{install_prefix});
exe.addLibraryPath(.{ .path = lib_dir });
exe.linkSystemLibrary("cfltk");
exe.linkSystemLibrary("fltk");
exe.linkSystemLibrary("fltk_images");
Expand All @@ -233,7 +236,7 @@ pub fn cfltk_link(exe: *CompileStep, install_prefix: []const u8, opts: FinalOpts
exe.linkSystemLibrary("fltk_gl");
exe.linkLibC();
exe.linkLibCpp();
if (target.isWindows()) {
if (target.os.tag == .windows) {
exe.linkSystemLibrary("ws2_32");
exe.linkSystemLibrary("comctl32");
exe.linkSystemLibrary("gdi32");
Expand Down Expand Up @@ -284,25 +287,25 @@ pub fn cfltk_link(exe: *CompileStep, install_prefix: []const u8, opts: FinalOpts
}

pub fn link_using_fltk_config(b: *Build, exe: *CompileStep, finalize_cfltk: *Build.Step, install_prefix: []const u8) !void {
const target = exe.target;
const target = exe.root_module.resolved_target.?.result;
exe.linkLibC();
exe.linkLibCpp();
var buf: [1024]u8 = undefined;
var inc_dir = try std.fmt.bufPrint(buf[0..], "{s}/cfltk/include", .{install_prefix});
exe.addIncludePath(Build.LazyPath{ .path = inc_dir });
var cmake_src_path = try std.fmt.allocPrint(b.allocator, "{s}/cfltk", .{install_prefix});
const inc_dir = try std.fmt.bufPrint(buf[0..], "{s}/cfltk/include", .{install_prefix});
exe.addIncludePath(.{ .path = inc_dir });
const cmake_src_path = try std.fmt.allocPrint(b.allocator, "{s}/cfltk", .{install_prefix});
_ = std.fs.cwd().openDir(cmake_src_path, .{}) catch |git_err| {
std.debug.print("Warning: {!}. The cfltk library will be grabbed!\n", .{git_err});
const cfltk_fetch = b.addSystemCommand(&[_][]const u8{ "git", "clone", "https://github.com/MoAlyousef/cfltk", cmake_src_path, "--depth=1" });
finalize_cfltk.dependOn(&cfltk_fetch.step);
};
var lib = b.addStaticLibrary(.{
.name = "cfltk",
.target = exe.target,
.optimize = exe.optimize,
.target = exe.root_module.resolved_target.?,
.optimize = exe.root_module.optimize.?,
.use_llvm = false,
});
const proc = try std.ChildProcess.exec(.{
const proc = try std.ChildProcess.run(.{
.allocator = b.allocator,
.argv = &[_][]const u8{ "fltk-config", "--use-images", "--use-gl", "--cflags" },
});
Expand All @@ -316,7 +319,7 @@ pub fn link_using_fltk_config(b: *Build, exe: *CompileStep, finalize_cfltk: *Bui
try cflags.append("-I/usr/local/include");
try cflags.append(try std.fmt.allocPrint(b.allocator, "-I{s}", .{inc_dir}));
try cflags.append("-DCFLTK_USE_GL");
lib.addCSourceFiles(&[_][]const u8{
lib.addCSourceFiles(.{.files = &[_][]const u8{
try std.fmt.allocPrint(b.allocator, "{s}/cfltk/src/cfl_new.cpp", .{install_prefix}),
try std.fmt.allocPrint(b.allocator, "{s}/cfltk/src/cfl_lock.cpp", .{install_prefix}),
try std.fmt.allocPrint(b.allocator, "{s}/cfltk/src/cfl.cpp", .{install_prefix}),
Expand All @@ -340,11 +343,11 @@ pub fn link_using_fltk_config(b: *Build, exe: *CompileStep, finalize_cfltk: *Bui
try std.fmt.allocPrint(b.allocator, "{s}/cfltk/src/cfl_font.cpp", .{install_prefix}),
try std.fmt.allocPrint(b.allocator, "{s}/cfltk/src/cfl_utils.cpp", .{install_prefix}),
try std.fmt.allocPrint(b.allocator, "{s}/cfltk/src/cfl_printer.cpp", .{install_prefix}),
}, cflags.items);
}, .flags = cflags.items});
if (target.isDarwin()) {
lib.addCSourceFile(Build.Step.Compile.CSourceFile{ .file = Build.LazyPath{ .path = try std.fmt.allocPrint(b.allocator, "{s}/cfltk/src/cfl_nswindow.m", .{install_prefix}) }, .flags = cflags.items });
lib.addCSourceFile(.{ .file = .{ .path = try std.fmt.allocPrint(b.allocator, "{s}/cfltk/src/cfl_nswindow.m", .{install_prefix}) }, .flags = cflags.items });
}
const proc2 = try std.ChildProcess.exec(.{
const proc2 = try std.ChildProcess.run(.{
.allocator = b.allocator,
.argv = &[_][]const u8{ "fltk-config", "--use-images", "--use-gl", "--ldflags" },
});
Expand All @@ -357,7 +360,7 @@ pub fn link_using_fltk_config(b: *Build, exe: *CompileStep, finalize_cfltk: *Bui
if (std.mem.startsWith(u8, x, "-L")) try Lflags.append(x[2..]);
}
for (Lflags.items) |f| {
lib.addLibraryPath(Build.LazyPath{ .path = f });
lib.addLibraryPath(.{ .path = f });
}
for (lflags.items) |f| {
lib.linkSystemLibrary(f);
Expand Down
6 changes: 3 additions & 3 deletions examples/capi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ pub fn main() !void {
fltkInit();

_ = c.Fl_set_scheme("gtk+");
var win = c.Fl_Window_new(100, 100, 400, 300, "Hello");
var but = c.Fl_Button_new(160, 220, 80, 40, "Click me!");
var box = c.Fl_Box_new(10, 10, 380, 180, "");
const win = c.Fl_Window_new(100, 100, 400, 300, "Hello");
const but = c.Fl_Button_new(160, 220, 80, 40, "Click me!");
const box = c.Fl_Box_new(10, 10, 380, 180, "");
c.Fl_Box_set_box(box, c.Fl_BoxType_UpBox);
c.Fl_Box_set_label_size(box, 18);
c.Fl_Box_set_label_font(box, c.Fl_Font_Courier);
Expand Down
Loading

0 comments on commit 6b21c60

Please sign in to comment.