Skip to content

Commit

Permalink
make the examples optinal and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
MoAlyousef committed Jul 30, 2024
1 parent 152a3d2 commit ac84eb0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 101 deletions.
67 changes: 5 additions & 62 deletions README.md
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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.
Expand Down
40 changes: 21 additions & 19 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
Expand All @@ -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");
Expand Down Expand Up @@ -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);
}
}
}
21 changes: 1 addition & 20 deletions build_utils.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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});
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit ac84eb0

Please sign in to comment.