Skip to content

Commit

Permalink
zig-build: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
kassane committed Jan 9, 2024
1 parent 0710071 commit ad3ca49
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 48 deletions.
7 changes: 1 addition & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,4 @@ jobs:
sudo apt-get install libglu1-mesa-dev mesa-common-dev xorg-dev libasound-dev
- name: Build
run: |
zig build debugtext_print -DZigCC -DBetterC --summary all
zig build clear -DZigCC -DBetterC --summary all
zig build sgl_context -DBetterC -DZigCC --summary all
zig build cube -DZigCC --summary all
zig build blend -DZigCC --summary all
run: zig build -DZigCC --summary all
29 changes: 21 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

[![CI Build](https://github.com/kassane/sokol-d/actions/workflows/build.yml/badge.svg)](https://github.com/kassane/sokol-d/actions/workflows/build.yml)

Auto-generated [D](https://dlang.org) bindings for the [sokol headers](https://github.com/floooh/sokol).

#### Status: Experimental

## BUILD

**Required**
Expand All @@ -13,18 +17,18 @@ Supported platforms are: Windows, macOS, Linux (with X11)
On Linux install the following packages: libglu1-mesa-dev, mesa-common-dev, xorg-dev, libasound-dev (or generally: the dev packages required for X11, GL and ALSA development)

```bash
# build sokol library only [default: static library]
# build sokol library + all examples [default: static library]
zig build -Doptimize=ReleaseFast

# build sokol shared library
# build sokol shared library + all examples
zig build -Doptimize=ReleaseFast -DShared

# build samples [WiP]
zig build clear -Doptimize=ReleaseFast # works
zig build debugtext_print -Doptimize=ReleaseFast # works
zig build sgl_context -Doptimize=ReleaseFast # run, but no anims (fixme)
zig build cube -Doptimize=ReleaseFast # run, but no anims (fixme)
zig build blend -Doptimize=ReleaseFast # run, but no anims (fixme)
# Run Examples
zig build run-clear -Doptimize=ReleaseFast # works
zig build run-debugtext_print -Doptimize=ReleaseFast # works
zig build run-sgl_context -Doptimize=ReleaseFast # run, but no anims (fixme)
zig build run-cube -Doptimize=ReleaseFast # run, but no anims (fixme)
zig build run-blend -Doptimize=ReleaseFast # run, but no anims (fixme)

zig build --help
# Project-Specific Options:
Expand All @@ -46,15 +50,24 @@ zig build --help
```

## Shaders

Checkout [sokol-tools](https://github.com/floooh/sokol-tools) for a sokol shader pipeline! It supports these D bindings and all shaders in the examples folder
here have been compiled using it with `-f sokol_d`!

```bash
zig build shaders # (re)generate D bindings from shaders.
```

<br>

## License and attributinos

<sub>
This code is released under the zlib license (see `LICENSE` for info). Parts of `gen_d.py` have been copied and modified from
the zig-bindings (https://github.com/floooh/sokol-zig/) and rust-bindings (https://github.com/floooh/sokol-rust/) for sokol.
</sub>

<sub>
The sokol headers are created by Andre Weissflog (floooh) and sokol is released under its own license here: https://github.com/floooh/sokol/blob/master/LICENSE
</sub>
</br>
57 changes: 23 additions & 34 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ pub fn build(b: *Builder) !void {

const optimize = b.standardOptimizeOption(.{});
const sokol = buildSokol(b, target, optimize, config, "");
b.installArtifact(sokol);

// LDC-config options
const enable_betterC = b.option(bool, "BetterC", "Omit generating some runtime information and helper functions. [default: false]") orelse false;
Expand Down Expand Up @@ -230,14 +229,12 @@ pub fn build(b: *Builder) !void {
.sources = if (std.mem.eql(u8, example, "cube"))
&.{
b.fmt("{s}/src/examples/{s}.d", .{ rootPath(), example }),
b.fmt("{s}/src/examples/math.d", .{rootPath()}),
b.fmt("{s}/src/examples/shaders/cube.glsl.d", .{rootPath()}),
b.fmt("{s}/src/examples/shaders/{s}.glsl.d", .{ rootPath(), example }),
}
else if (std.mem.eql(u8, example, "blend"))
&.{
b.fmt("{s}/src/examples/{s}.d", .{ rootPath(), example }),
b.fmt("{s}/src/examples/math.d", .{rootPath()}),
b.fmt("{s}/src/examples/shaders/blend.glsl.d", .{rootPath()}),
b.fmt("{s}/src/examples/shaders/{s}.glsl.d", .{ rootPath(), example }),
}
else
&.{
Expand All @@ -252,10 +249,14 @@ pub fn build(b: *Builder) !void {
// fixme: https://github.com/kassane/sokol-d/issues/1 - betterC works on darwin
.zig_cc = if (target.result.isDarwin() and !enable_betterC) false else enable_zigcc,
});
ldc.setName("ldc2");
ldc.step.dependOn(b.getInstallStep());
const run = b.step(b.fmt("{s}", .{example}), b.fmt("Build example {s}", .{example}));
run.dependOn(&ldc.step);
ldc.setName(example);

ldc.addArtifactArg(sokol);
b.getInstallStep().dependOn(&ldc.step);

const example_run = b.addSystemCommand(&.{b.pathJoin(&.{ b.install_path, "bin", example })});
const run = b.step(b.fmt("run-{s}", .{example}), b.fmt("Run example {s}", .{example}));
run.dependOn(&example_run.step);
}
buildShaders(b);
}
Expand Down Expand Up @@ -306,6 +307,7 @@ fn buildShaders(b: *Builder) void {

// Use LDC2 (https://github.com/ldc-developers/ldc) to compile the D examples
fn buildLDC(b: *Builder, lib: *CompileStep, config: ldcConfig) !*RunStep {
// ldmd2: ldc2 wrapped w/ dmd flags
const ldc = try b.findProgram(&.{"ldmd2"}, &.{});

var cmds = std.ArrayList([]const u8).init(b.allocator);
Expand Down Expand Up @@ -382,29 +384,22 @@ fn buildLDC(b: *Builder, lib: *CompileStep, config: ldcConfig) !*RunStep {
// object files with fully qualified names
try cmds.append("--oq");

// disable LLVM-IR verifier
// https://llvm.org/docs/Passes.html#verify-module-verifier
try cmds.append("--disable-verify");

// keep all function bodies in .di files
try cmds.append("--Hkeep-all-bodies");

// sokol D files and include path
try cmds.append(b.fmt("-I{s}", .{b.pathJoin(&.{ rootPath(), "src", "sokol" })}));
const srcs = &.{
"app",
"audio",
"gl",
"gfx",
"glue",
"log",
"shape",
"time",
"debugtext",
"utils",
};
// Include imported modules in the compilation
// automatically finds needed library files and builds
try cmds.append("-i");
inline for (srcs) |src| {
try cmds.append(b.fmt("{s}.d", .{b.pathJoin(&.{ rootPath(), "src", "sokol", src })}));
}

// sokol D files and include path
try cmds.append(b.fmt("-I{s}", .{b.pathJoin(&.{
rootPath(),
"src",
})}));

// example D file
for (config.sources) |src| {
Expand All @@ -417,10 +412,6 @@ fn buildLDC(b: *Builder, lib: *CompileStep, config: ldcConfig) !*RunStep {
try cmds.append(b.fmt("-L-L{s}", .{libpath.path}));
}

// sokol library path (zig-out/lib/libsokol.{a|lib})
try cmds.append(b.fmt("-L-L{s}", .{b.pathJoin(&.{ b.install_prefix, "lib" })}));
try cmds.append(b.fmt("-L-l{s}", .{lib.name}));

// link system libs
for (lib.root_module.link_objects.items) |link_object| {
if (link_object != .system_lib) continue;
Expand Down Expand Up @@ -462,7 +453,7 @@ fn buildLDC(b: *Builder, lib: *CompileStep, config: ldcConfig) !*RunStep {

if (b.verbose) {
try cmds.append("-vdmd");
// try cmds.append("-v");
// try cmds.append("-v"); // very long
try cmds.append("-Xcc=-v");
}

Expand All @@ -475,9 +466,6 @@ fn buildLDC(b: *Builder, lib: *CompileStep, config: ldcConfig) !*RunStep {
if (ubsan)
try cmds.append("--fsanitize=address");

if (lib.dead_strip_dylibs)
try cmds.append("--disable-linker-strip-dead");

if (lib.root_module.omit_frame_pointer) |enabled| {
if (enabled)
try cmds.append("--frame-pointer=none")
Expand All @@ -499,6 +487,7 @@ fn buildLDC(b: *Builder, lib: *CompileStep, config: ldcConfig) !*RunStep {

// cpu model (e.g. "baseline")
// try cmds.append(b.fmt("--mcpu={s}", .{lib.rootModuleTarget().cpu.model.name}));

// output file
try cmds.append(b.fmt("--of={s}", .{b.pathJoin(&.{ b.install_prefix, "bin", config.name orelse "d_binary" })}));

Expand Down
File renamed without changes.

0 comments on commit ad3ca49

Please sign in to comment.