diff --git a/README.md b/README.md index bab8615..bd931ab 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,18 @@ # zfltk A Zig wrapper for the FLTK gui library. -## Running the examples +## Building the package from source ``` git clone https://github.com/MoAlyousef/zfltk cd zfltk -zig build run-simple -zig build run-capi -zig build run-editor -zig build run-input -zig build run-image -zig build run-mixed +zig build ``` +To build the examples, pass `-Dzfltk-build-examples=true` to your `zig build` command. + ## Usage -### Official Zig package manager: -If you're using the official package manager: +zfltk supports the zig package manager. You can add it as a dependency to your project in your build.zig.zon: ```zig -// in your build.zig.zon .{ .name = "app", .version = "0.0.1", @@ -72,58 +67,6 @@ Then you can run: zig build run ``` -### Via vendoring -You can either use git clone or git submodules: -``` -# via git submodule -git submodule add https://github.com/moalyousef/zfltk -cd zfltk -cd .. -``` -``` -# via git clone -git clone https://github.com/moalyousef/zfltk -cd zfltk -cd .. -``` -then you will need a build.zig file as follows: -```zig -const std = @import("std"); -const Sdk = @import("zfltk/build.zig"); -const Build = std.Build; - -pub fn build(b: *Build) !void { - const target = b.standardTargetOptions(.{}); - const mode = b.standardOptimizeOption(.{}); - - const exe = b.addExecutable(.{ - .name = "app", - .root_source_file = b.path("src/main.zig"), - .optimize = mode, - .target = target, - }); - - const sdk = try Sdk.init(b); - const zfltk_module = sdk.getZfltkModule(b); - exe.root_module.addImport("zfltk", zfltk_module); - try sdk.link(exe); - - b.installArtifact(exe); - - const run_cmd = b.addRunArtifact(exe); - if (b.args) |args| { - run_cmd.addArgs(args); - } - - const run_step = b.step("run", "Run the app"); - run_step.dependOn(&run_cmd.step); -} -``` -Then you can run: -``` -zig build run -``` - ## Dependencies This repo tracks cfltk, the C bindings to FLTK. It (along with FLTK) is statically linked to your application. diff --git a/build.zig b/build.zig index 663cdf8..64f5ccb 100644 --- a/build.zig +++ b/build.zig @@ -8,15 +8,15 @@ pub const SdkOpts = struct { system_jpeg: bool = false, system_png: bool = false, system_zlib: bool = false, - use_zig_cc: bool = false, use_fltk_config: bool = false, + build_examples: bool = false, fn finalOpts(self: SdkOpts) utils.FinalOpts { return utils.FinalOpts{ .use_wayland = self.use_wayland, .system_jpeg = self.system_jpeg, .system_png = self.system_png, .system_zlib = self.system_zlib, - .use_zig_cc = self.use_zig_cc, + .build_examples = self.build_examples, .use_fltk_config = self.use_fltk_config, }; } @@ -38,7 +38,7 @@ pub fn initWithOpts(b: *Build, opts: SdkOpts) !*Sdk { final_opts.system_jpeg = b.option(bool, "zfltk-system-libjpeg", "link system libjpeg") orelse opts.system_jpeg; final_opts.system_png = b.option(bool, "zfltk-system-libpng", "link system libpng") orelse opts.system_png; final_opts.system_zlib = b.option(bool, "zfltk-system-zlib", "link system zlib") orelse opts.system_zlib; - final_opts.use_zig_cc = b.option(bool, "zfltk-use-zigcc", "use zig cc and zig c++ to build FLTK and cfltk") orelse opts.use_zig_cc; + final_opts.build_examples = b.option(bool, "zfltk-build-examples", "Build zfltk examples") orelse opts.build_examples; final_opts.use_fltk_config = b.option(bool, "zfltk-use-fltk-config", "use fltk-config instead of building fltk from source") orelse opts.use_fltk_config; const install_prefix = b.install_prefix; const finalize_cfltk = b.step("finalize cfltk install", "Installs cfltk"); @@ -79,23 +79,25 @@ pub fn build(b: *Build) !void { const mode = b.standardOptimizeOption(.{}); const sdk = try Sdk.init(b); const zfltk_module = sdk.getZfltkModule(b); - const examples_step = b.step("examples", "build the examples"); - b.default_step.dependOn(examples_step); + if (sdk.opts.build_examples) { + const examples_step = b.step("examples", "build the examples"); + b.default_step.dependOn(examples_step); - for (utils.examples) |example| { - const exe = b.addExecutable(.{ - .name = example.output, - .root_source_file = b.path(example.input), - .optimize = mode, - .target = target, - }); - exe.root_module.addImport("zfltk", zfltk_module); - try sdk.link(exe); - examples_step.dependOn(&exe.step); - b.installArtifact(exe); + for (utils.examples) |example| { + const exe = b.addExecutable(.{ + .name = example.output, + .root_source_file = b.path(example.input), + .optimize = mode, + .target = target, + }); + exe.root_module.addImport("zfltk", zfltk_module); + try sdk.link(exe); + examples_step.dependOn(&exe.step); + b.installArtifact(exe); - const run_cmd = b.addRunArtifact(exe); - const run_step = b.step(b.fmt("run-{s}", .{example.output}), example.description.?); - run_step.dependOn(&run_cmd.step); + const run_cmd = b.addRunArtifact(exe); + const run_step = b.step(b.fmt("run-{s}", .{example.output}), example.description.?); + run_step.dependOn(&run_cmd.step); + } } } diff --git a/build_utils.zig b/build_utils.zig index f9ecb37..63b4643 100644 --- a/build_utils.zig +++ b/build_utils.zig @@ -7,7 +7,7 @@ pub const FinalOpts = struct { system_jpeg: bool = false, system_png: bool = false, system_zlib: bool = false, - use_zig_cc: bool = false, + build_examples: bool = false, use_fltk_config: bool = false, }; @@ -51,17 +51,6 @@ 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.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) { - false => "", - true => try std.fmt.bufPrint(zig_cc_buf[0..], "-DCMAKE_C_COMPILER={s};cc", .{zig_exe}), - }; - const use_zig_cpp = switch (opts.use_zig_cc) { - false => "", - true => try std.fmt.bufPrint(zig_cpp_buf[0..], "-DCMAKE_CXX_COMPILER={s};c++", .{zig_exe}), - }; const target = b.host.result; var buf: [1024]u8 = undefined; const sdk_lib_dir = try std.fmt.bufPrint(buf[0..], "{s}/cfltk/lib", .{install_prefix}); @@ -95,8 +84,6 @@ pub fn cfltk_build_from_source(b: *Build, finalize_cfltk: *Build.Step, install_p cmake_src_path, "-GNinja", "-DCMAKE_BUILD_TYPE=Release", - use_zig_cc, - use_zig_cpp, cmake_inst_path, "-DFLTK_BUILD_TEST=OFF", which_png, @@ -115,8 +102,6 @@ pub fn cfltk_build_from_source(b: *Build, finalize_cfltk: *Build.Step, install_p "-S", cmake_src_path, "-DCMAKE_BUILD_TYPE=Release", - use_zig_cc, - use_zig_cpp, cmake_inst_path, "-DFLTK_BUILD_TEST=OFF", which_png, @@ -136,8 +121,6 @@ pub fn cfltk_build_from_source(b: *Build, finalize_cfltk: *Build.Step, install_p "-S", cmake_src_path, "-DCMAKE_BUILD_TYPE=Release", - use_zig_cc, - use_zig_cpp, cmake_inst_path, "-DFLTK_BUILD_TEST=OFF", which_png, @@ -158,8 +141,6 @@ pub fn cfltk_build_from_source(b: *Build, finalize_cfltk: *Build.Step, install_p "-S", cmake_src_path, "-DCMAKE_BUILD_TYPE=Release", - use_zig_cc, - use_zig_cpp, cmake_inst_path, "-DFLTK_BUILD_TEST=OFF", which_png,