Skip to content

Commit de44cdc

Browse files
committed
Adding sokol-zig into the repo instead of a submodule so it can be used with the Zig package manager
1 parent d0d99e3 commit de44cdc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+57684
-8
lines changed

.gitmodules

Lines changed: 0 additions & 6 deletions
This file was deleted.

3rdparty/sokol-zig

Lines changed: 0 additions & 1 deletion
This file was deleted.

3rdparty/sokol-zig/.editorconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
root=true
2+
[**.zig]
3+
end_of_line=lf
4+
[**]
5+
indent_style=space
6+
indent_size=4
7+
trim_trailing_whitespace=true
8+
insert_final_newline=true
9+
10+
11+

3rdparty/sokol-zig/.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.zig text eol=lf
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Zig
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
build:
7+
strategy:
8+
matrix:
9+
os: [ubuntu-latest, macos-latest, windows-latest]
10+
runs-on: ${{matrix.os}}
11+
steps:
12+
- uses: actions/checkout@v3
13+
- uses: goto-bus-stop/setup-zig@v2
14+
with:
15+
version: 0.11.0
16+
- name: prepare-linux
17+
if: runner.os == 'Linux'
18+
run: |
19+
sudo apt-get update
20+
sudo apt-get install libglu1-mesa-dev mesa-common-dev xorg-dev libasound-dev
21+
- name: build
22+
run: zig build

3rdparty/sokol-zig/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.vscode
2+
zig-*/
3+
NUL

3rdparty/sokol-zig/README.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
[![build](https://github.com/floooh/sokol-zig/actions/workflows/main.yml/badge.svg)](https://github.com/floooh/sokol-zig/actions/workflows/main.yml)
2+
3+
Auto-generated Zig bindings for the [sokol headers](https://github.com/floooh/sokol).
4+
5+
For Zig version 0.11.0
6+
7+
Related projects:
8+
9+
- [pacman.zig](https://github.com/floooh/pacman.zig)
10+
- [kc85.zig](https://github.com/floooh/kc85.zig)
11+
12+
> NOTE: for experimental package manager support see the branch [package](https://github.com/floooh/sokol-zig/tree/package),
13+
> and as example for how to integrate sokol-zig as package the [pacman.zig branch sokol-package](https://github.com/floooh/pacman.zig/tree/sokol-package)
14+
15+
## BUILD
16+
17+
Supported platforms are: Windows, macOS, Linux (with X11)
18+
19+
On Linux install the following packages: libglu1-mesa-dev, mesa-common-dev, xorg-dev, libasound-dev
20+
(or generally: the dev packages required for X11, GL and ALSA development)
21+
22+
```sh
23+
# just build:
24+
> zig build
25+
# build and run samples:
26+
> zig build run-clear
27+
> zig build run-triangle
28+
> zig build run-quad
29+
> zig build run-bufferoffsets
30+
> zig build run-cube
31+
> zig build run-noninterleaved
32+
> zig build run-texcube
33+
> zig build run-offscreen
34+
> zig build run-instancing
35+
> zig build run-mrt
36+
> zig build run-saudio
37+
> zig build run-sgl
38+
> zig build run-sgl-context
39+
> zig build run-sgl-points
40+
> zig build run-debugtext
41+
> zig build run-debugtext-print
42+
> zig build run-debugtext-userfont
43+
> zig build run-shapes
44+
```
45+
46+
(also run ```zig build --help``` to inspect the build targets)
47+
48+
By default, the backend 3D API will be selected based on the target platform:
49+
50+
- macOS: Metal
51+
- Windows: D3D11
52+
- Linux: GL
53+
54+
To force the GL backend on macOS or Windows, build with ```-Dgl=true```:
55+
56+
```
57+
> zig build -Dgl=true run-clear
58+
```
59+
60+
The ```clear``` sample prints the selected backend to the terminal:
61+
62+
```
63+
sokol-zig ➤ zig build -Dgl=true run-clear
64+
Backend: .sokol.gfx.Backend.GLCORE33
65+
```
66+
67+
## Use as Library
68+
69+
Clone this repo into your project via ``git submodule add https://github.com/floooh/sokol-zig.git`` (for this example into a folder called ``lib`` within your project).
70+
71+
Add to your ``build.zig``:
72+
```zig
73+
const sokol = @import("lib/sokol-zig/build.zig");
74+
75+
// ...
76+
// pub fn build(b: *std.build.Builder) void {
77+
// ...
78+
79+
const sokol_build = sokol.buildSokol(b, target, optimize, .{}, "lib/sokol-zig/");
80+
81+
// ...
82+
// const exe = b.addExecutable("demo", "src/main.zig");
83+
// ...
84+
85+
exe.addAnonymousModule("sokol", .{ .source_file = .{ .path = "lib/sokol-zig/src/sokol/sokol.zig" } });
86+
exe.linkLibrary(sokol_build);
87+
```

3rdparty/sokol-zig/build.zig

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
const std = @import("std");
2+
const builtin = @import("builtin");
3+
const Builder = std.build.Builder;
4+
const CompileStep = std.build.CompileStep;
5+
const CrossTarget = std.zig.CrossTarget;
6+
const OptimizeMode = std.builtin.OptimizeMode;
7+
8+
pub const Backend = enum {
9+
auto, // Windows: D3D11, macOS/iOS: Metal, otherwise: GL
10+
d3d11,
11+
metal,
12+
gl,
13+
gles2,
14+
gles3,
15+
wgpu,
16+
};
17+
18+
pub const Config = struct {
19+
backend: Backend = .auto,
20+
force_egl: bool = false,
21+
enable_x11: bool = true,
22+
enable_wayland: bool = false,
23+
};
24+
25+
// build sokol into a static library
26+
pub fn buildSokol(b: *Builder, target: CrossTarget, optimize: OptimizeMode, config: Config, comptime prefix_path: []const u8) *CompileStep {
27+
const lib = b.addStaticLibrary(.{
28+
.name = "sokol",
29+
.target = target,
30+
.optimize = optimize,
31+
});
32+
lib.linkLibC();
33+
const sokol_path = prefix_path ++ "src/sokol/c/";
34+
const csources = [_][]const u8{
35+
"sokol_log.c",
36+
"sokol_app.c",
37+
"sokol_gfx.c",
38+
"sokol_time.c",
39+
"sokol_audio.c",
40+
"sokol_gl.c",
41+
"sokol_debugtext.c",
42+
"sokol_shape.c",
43+
};
44+
var _backend = config.backend;
45+
if (_backend == .auto) {
46+
if (lib.target.isDarwin()) {
47+
_backend = .metal;
48+
} else if (lib.target.isWindows()) {
49+
_backend = .d3d11;
50+
} else {
51+
_backend = .gl;
52+
}
53+
}
54+
const backend_option = switch (_backend) {
55+
.d3d11 => "-DSOKOL_D3D11",
56+
.metal => "-DSOKOL_METAL",
57+
.gl => "-DSOKOL_GLCORE33",
58+
.gles2 => "-DSOKOL_GLES2",
59+
.gles3 => "-DSOKOL_GLES3",
60+
.wgpu => "-DSOKOL_WGPU",
61+
else => unreachable,
62+
};
63+
64+
if (lib.target.isDarwin()) {
65+
inline for (csources) |csrc| {
66+
lib.addCSourceFile(.{
67+
.file = .{ .path = sokol_path ++ csrc },
68+
.flags = &[_][]const u8{ "-ObjC", "-DIMPL", backend_option },
69+
});
70+
}
71+
lib.linkFramework("Foundation");
72+
lib.linkFramework("AudioToolbox");
73+
if (.metal == _backend) {
74+
lib.linkFramework("MetalKit");
75+
lib.linkFramework("Metal");
76+
}
77+
if (lib.target.getOsTag() == .ios) {
78+
lib.linkFramework("UIKit");
79+
lib.linkFramework("AVFoundation");
80+
if (.gl == _backend) {
81+
lib.linkFramework("OpenGLES");
82+
lib.linkFramework("GLKit");
83+
}
84+
} else if (lib.target.getOsTag() == .macos) {
85+
lib.linkFramework("Cocoa");
86+
lib.linkFramework("QuartzCore");
87+
if (.gl == _backend) {
88+
lib.linkFramework("OpenGL");
89+
}
90+
}
91+
} else {
92+
var egl_flag = if (config.force_egl) "-DSOKOL_FORCE_EGL " else "";
93+
var x11_flag = if (!config.enable_x11) "-DSOKOL_DISABLE_X11 " else "";
94+
var wayland_flag = if (!config.enable_wayland) "-DSOKOL_DISABLE_WAYLAND" else "";
95+
96+
inline for (csources) |csrc| {
97+
lib.addCSourceFile(.{
98+
.file = .{ .path = sokol_path ++ csrc },
99+
.flags = &[_][]const u8{ "-DIMPL", backend_option, egl_flag, x11_flag, wayland_flag },
100+
});
101+
}
102+
103+
if (lib.target.isLinux()) {
104+
var link_egl = config.force_egl or config.enable_wayland;
105+
var egl_ensured = (config.force_egl and config.enable_x11) or config.enable_wayland;
106+
107+
lib.linkSystemLibrary("asound");
108+
109+
if (.gles2 == _backend) {
110+
lib.linkSystemLibrary("glesv2");
111+
if (!egl_ensured) {
112+
@panic("GLES2 in Linux only available with Config.force_egl and/or Wayland");
113+
}
114+
} else {
115+
lib.linkSystemLibrary("GL");
116+
}
117+
if (config.enable_x11) {
118+
lib.linkSystemLibrary("X11");
119+
lib.linkSystemLibrary("Xi");
120+
lib.linkSystemLibrary("Xcursor");
121+
}
122+
if (config.enable_wayland) {
123+
lib.linkSystemLibrary("wayland-client");
124+
lib.linkSystemLibrary("wayland-cursor");
125+
lib.linkSystemLibrary("wayland-egl");
126+
lib.linkSystemLibrary("xkbcommon");
127+
}
128+
if (link_egl) {
129+
lib.linkSystemLibrary("egl");
130+
}
131+
} else if (lib.target.isWindows()) {
132+
lib.linkSystemLibraryName("kernel32");
133+
lib.linkSystemLibraryName("user32");
134+
lib.linkSystemLibraryName("gdi32");
135+
lib.linkSystemLibraryName("ole32");
136+
if (.d3d11 == _backend) {
137+
lib.linkSystemLibraryName("d3d11");
138+
lib.linkSystemLibraryName("dxgi");
139+
}
140+
}
141+
}
142+
return lib;
143+
}
144+
145+
// build one of the example exes
146+
fn buildExample(b: *Builder, target: CrossTarget, optimize: OptimizeMode, sokol: *CompileStep, comptime name: []const u8) void {
147+
const e = b.addExecutable(.{
148+
.name = name,
149+
.root_source_file = .{ .path = "src/examples/" ++ name ++ ".zig" },
150+
.target = target,
151+
.optimize = optimize,
152+
});
153+
e.linkLibrary(sokol);
154+
e.addAnonymousModule("sokol", .{ .source_file = .{ .path = "src/sokol/sokol.zig" } });
155+
b.installArtifact(e);
156+
const run = b.addRunArtifact(e);
157+
b.step("run-" ++ name, "Run " ++ name).dependOn(&run.step);
158+
}
159+
160+
pub fn build(b: *Builder) void {
161+
var config: Config = .{};
162+
163+
const force_gl = b.option(bool, "gl", "Force GL backend") orelse false;
164+
config.backend = if (force_gl) .gl else .auto;
165+
166+
// NOTE: Wayland support is *not* currently supported in the standard sokol-zig bindings,
167+
// you need to generate your own bindings using this PR: https://github.com/floooh/sokol/pull/425
168+
config.enable_wayland = b.option(bool, "wayland", "Compile with wayland-support (default: false)") orelse false;
169+
config.enable_x11 = b.option(bool, "x11", "Compile with x11-support (default: true)") orelse true;
170+
config.force_egl = b.option(bool, "egl", "Use EGL instead of GLX if possible (default: false)") orelse false;
171+
172+
const target = b.standardTargetOptions(.{});
173+
const optimize = b.standardOptimizeOption(.{});
174+
const sokol = buildSokol(b, target, optimize, config, "");
175+
const examples = .{
176+
"clear",
177+
"triangle",
178+
"quad",
179+
"bufferoffsets",
180+
"cube",
181+
"noninterleaved",
182+
"texcube",
183+
"blend",
184+
"offscreen",
185+
"instancing",
186+
"mrt",
187+
"saudio",
188+
"sgl",
189+
"sgl-context",
190+
"sgl-points",
191+
"debugtext",
192+
"debugtext-print",
193+
"debugtext-userfont",
194+
"shapes",
195+
};
196+
inline for (examples) |example| {
197+
buildExample(b, target, optimize, sokol, example);
198+
}
199+
buildShaders(b);
200+
}
201+
202+
// a separate step to compile shaders, expects the shader compiler in ../sokol-tools-bin/
203+
fn buildShaders(b: *Builder) void {
204+
const sokol_tools_bin_dir = "../sokol-tools-bin/bin/";
205+
const shaders_dir = "src/examples/shaders/";
206+
const shaders = .{
207+
"bufferoffsets.glsl",
208+
"cube.glsl",
209+
"instancing.glsl",
210+
"mrt.glsl",
211+
"noninterleaved.glsl",
212+
"offscreen.glsl",
213+
"quad.glsl",
214+
"shapes.glsl",
215+
"texcube.glsl",
216+
"blend.glsl",
217+
};
218+
const optional_shdc: ?[:0]const u8 = comptime switch (builtin.os.tag) {
219+
.windows => "win32/sokol-shdc.exe",
220+
.linux => "linux/sokol-shdc",
221+
.macos => if (builtin.cpu.arch.isX86()) "osx/sokol-shdc" else "osx_arm64/sokol-shdc",
222+
else => null,
223+
};
224+
if (optional_shdc == null) {
225+
std.log.warn("unsupported host platform, skipping shader compiler step", .{});
226+
return;
227+
}
228+
const shdc_path = sokol_tools_bin_dir ++ optional_shdc.?;
229+
const shdc_step = b.step("shaders", "Compile shaders (needs ../sokol-tools-bin)");
230+
inline for (shaders) |shader| {
231+
const cmd = b.addSystemCommand(&.{
232+
shdc_path,
233+
"-i",
234+
shaders_dir ++ shader,
235+
"-o",
236+
shaders_dir ++ shader ++ ".zig",
237+
"-l",
238+
"glsl330:metal_macos:hlsl4",
239+
"-f",
240+
"sokol_zig",
241+
});
242+
shdc_step.dependOn(&cmd.step);
243+
}
244+
}

0 commit comments

Comments
 (0)