Code Monkey home page Code Monkey logo

sokol-zig's Introduction

build

Auto-generated Zig bindings for the sokol headers.

For Zig version 0.13.0 and 0.14.0-dev

In case of breaking changes in Zig, the bindings might fall behind. Please don't hesitate to ping me via a Github issue, or even better, provide a PR :)

Support for stable Zig versions is in branches (e.g. zig-0.12.0), those versions are 'frozen in time' though.

Related projects:

Building the samples

Supported platforms are: Windows, macOS, Linux (with X11) and web

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)

To build the platform-native samples:

# just build:
zig build
# build and run samples:
zig build run-clear
zig build run-triangle
zig build run-quad
zig build run-bufferoffsets
zig build run-cube
zig build run-noninterleaved
zig build run-texcube
zig build run-offscreen
zig build run-instancing
zig build run-mrt
zig build run-saudio
zig build run-sgl
zig build run-sgl-context
zig build run-sgl-points
zig build run-debugtext
zig build run-debugtext-print
zig build run-debugtext-userfont
zig build run-shapes

(also run zig build -l to get a list of build targets)

By default, the backend 3D API will be selected based on the target platform:

  • macOS: Metal
  • Windows: D3D11
  • Linux: GL

To force the GL backend on macOS or Windows, build with -Dgl=true:

> zig build -Dgl=true run-clear

The clear sample prints the selected backend to the terminal:

sokol-zig ➤ zig build -Dgl=true run-clear
Backend: .sokol.gfx.Backend.GLCORE33

For the web-samples, run:

zig build -Dtarget=wasm32-emscripten
# or to build and run one of the samples
zig build run-clear -Dtarget=wasm32-emscripten
...

When building with target wasm32-emscripten for the first time, the build script will install and activate the Emscripten SDK into the Zig package cache for the latest SDK version. There is currently no build system functionality to update or delete the Emscripten SDK after this first install. The current workaround is to delete the global Zig cache (run zig env to see where the Zig cache resides).

Improving the Emscripten SDK integration with the Zig build system is planned for the future.

How to integrate sokol-zig into your project

Add a build.zig.zon file to your project which has at least a .sokol dependency:

.{
    .name = "my_project",
    .version = "0.1.0",
    .paths = .{
        "src",
        "build.zig",
        "build.zig.zon",
    },
    .dependencies = .{
        .sokol = .{
            .url = "git+https://github.com/floooh/sokol-zig.git#[commit-hash]",
            .hash = "[content-hash]",
        },
    },
}

The easiest way to populate the sokol dependency is to run this on the cmdline:

zig fetch --save=sokol git+https://github.com/floooh/sokol-zig.git

This will automatically use the latest sokol-zig commit.

For a native-only project, a build.zig file looks entirely vanilla:

const std = @import("std");
const Build = std.Build;
const OptimizeMode = std.builtin.OptimizeMode;

pub fn build(b: *Build) !void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});
    const dep_sokol = b.dependency("sokol", .{
        .target = target,
        .optimize = optimize,
    });
   const hello = b.addExecutable(.{
        .name = "hello",
        .target = target,
        .optimize = optimize,
        .root_source_file = b.path("src/hello.zig"),
    });
    hello.root_module.addImport("sokol", dep_sokol.module("sokol"));
    b.installArtifact(hello);
    const run = b.addRunArtifact(hello);
    b.step("run", "Run hello").dependOn(&run.step);
}

If you also want to run on the web via -Dtarget=wasm32-emscripten, the web platform build must look special, because Emscripten must be used for linking, and to run the build result in a browser, a special run step must be created.

Such a 'hybrid' build script might look like this (copied straight from pacman.zig):

const std = @import("std");
const Build = std.Build;
const OptimizeMode = std.builtin.OptimizeMode;
const sokol = @import("sokol");

pub fn build(b: *Build) !void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});
    const dep_sokol = b.dependency("sokol", .{
        .target = target,
        .optimize = optimize,
    });

    // special case handling for native vs web build
    if (target.result.isWasm()) {
        try buildWeb(b, target, optimize, dep_sokol);
    } else {
        try buildNative(b, target, optimize, dep_sokol);
    }
}

// this is the regular build for all native platforms, nothing surprising here
fn buildNative(b: *Build, target: Build.ResolvedTarget, optimize: OptimizeMode, dep_sokol: *Build.Dependency) !void {
    const pacman = b.addExecutable(.{
        .name = "pacman",
        .target = target,
        .optimize = optimize,
        .root_source_file = b.path("src/pacman.zig"),
    });
    pacman.root_module.addImport("sokol", dep_sokol.module("sokol"));
    b.installArtifact(pacman);
    const run = b.addRunArtifact(pacman);
    b.step("run", "Run pacman").dependOn(&run.step);
}

// for web builds, the Zig code needs to be built into a library and linked with the Emscripten linker
fn buildWeb(b: *Build, target: Build.ResolvedTarget, optimize: OptimizeMode, dep_sokol: *Build.Dependency) !void {
    const pacman = b.addStaticLibrary(.{
        .name = "pacman",
        .target = target,
        .optimize = optimize,
        .root_source_file = b.path("src/pacman.zig"),
    });
    pacman.root_module.addImport("sokol", dep_sokol.module("sokol"));

    // create a build step which invokes the Emscripten linker
    const emsdk = dep_sokol.builder.dependency("emsdk", .{});
    const link_step = try sokol.emLinkStep(b, .{
        .lib_main = pacman,
        .target = target,
        .optimize = optimize,
        .emsdk = emsdk,
        .use_webgl2 = true,
        .use_emmalloc = true,
        .use_filesystem = false,
        .shell_file_path = dep_sokol.path("src/sokol/web/shell.html").getPath(b),
    });
    // ...and a special run step to start the web build output via 'emrun'
    const run = sokol.emRunStep(b, .{ .name = "pacman", .emsdk = emsdk });
    run.step.dependOn(&link_step.step);
    b.step("run", "Run pacman").dependOn(&run.step);
}

wasm32-emscripten caveats

This list might grow longer over time!

  • Zig allocators use the @returnAddress builtin, which isn't supported in the Emscripten runtime out of the box (you'll get a runtime error in the browser's Javascript console looking like this: Cannot use convertFrameToPC (needed by __builtin_return_address) without -sUSE_OFFSET_CONVERTER. To make it work, do as the error message says, to add the -sUSE_OFFSET_CONVERTER arg to the Emscripten linker step in your build.zig file:

        const link_step = try sokol.emLinkStep(b, .{
          // ...other settings here
          .extra_args = &.{"-sUSE_OFFSET_CONVERTER=1"},
      });

    Also see the kc85.zig build.zig as example!

  • the Zig stdlib only has limited support for the wasm32-emscripten target, for instance using std.fs functions will most likely fail to compile (the sokol-zig bindings might add more sokol headers in the future to fill some of the gaps)

Dear ImGui support

The sokol-zig bindings come with sokol_imgui.h (exposed as the Zig package sokol.imgui), but integration into a project's build.zig requires some extra steps, mainly because I didn't want to add a cimgui dependency to the sokol-zig package (especially since cimgui uses git submodule which are not supported by the Zig package manager).

The main steps to create Dear ImGui apps with sokol-zig are:

  1. 'bring your own cimgui'

  2. tell the sokol dependency that it needs to include sokol_imgui.h into the compiled C library:

    const dep_sokol = b.dependency("sokol", .{
        .target = target,
        .optimize = optimize,
        .with_sokol_imgui = true,
    });
  3. inject the path to the cimgui directory into the sokol dependency so that C compilation works (this needs to find the cimgui.h header)

    dep_sokol.artifact("sokol_clib").addIncludePath(cimgui_root);

Also see the following example project:

https://github.com/floooh/sokol-zig-imgui-sample/

sokol-zig's People

Contributors

20kano avatar abhirag avatar benbot avatar darltrash avatar floooh avatar geezerlmao avatar geooot avatar julhe avatar kassane avatar kcbanner avatar ktravis avatar michaelbartnett avatar mustardfrog avatar silbinarywolf avatar trace-andreason avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sokol-zig's Issues

`standardReleaseOptions` removed in Zig 0.11.0

Zig version: 0.11.0-dev.3879+5804f3f75

C:\Users\Terry\sokol-zig>zig build run-triangle
C:\Users\Terry\sokol-zig\build.zig:150:19: error: no field or member function named 'standardReleaseOptions' in 'Build'
    const mode = b.standardReleaseOptions();
                 ~^~~~~~~~~~~~~~~~~~~~~~~
C:\Program Files\zig\lib\std\Build.zig:1:1: note: struct declared here
const std = @import("std.zig");
^~~~~
referenced by:
    runBuild__anon_7280: C:\Program Files\zig\lib\std\Build.zig:1600:27
    steps__anon_7043: C:\Program Files\zig\lib\build_runner.zig:914:20
    remaining reference traces hidden; use '-freference-trace' to see all reference traces

See here: https://devlog.hexops.com/2023/zig-0-11-breaking-build-changes/

[Help] Compilation for Android

When I compiled it after a long time, the zig specification seems to have changed and the following code returned an error. (0.12.0-dev.3154+0b744da84)

Any advice on how to resolve this would be appreciated.

const std = @import("std");

pub fn build(b: *std.Build) void {
	// const lib = try build_linux(b,);
	const lib = try build_android(b);
	b.installArtifact(lib);
}

fn build_android(
	b: *std.Build,
) !*std.Build.Step.Compile {
	const target = b.resolveTargetQuery(.{
		.cpu_arch = .aarch64,
		.os_tag = .linux,
		.abi = .android,
		.cpu_model = .baseline,
		.cpu_features_add = std.Target.aarch64.featureSet(&.{.v8a}),
	});
	const optimize = b.standardOptimizeOption(.{});
	
	const lib = b.addStaticLibrary(.{
		.name = "sokol-android",
		.target = target,
		.optimize = optimize,
		.link_libc = true,
	});
	lib.setLibCFile(.{.path="android-libc.conf"});
	lib.libc_file.?.addStepDependencies(&lib.step);
	
	lib.linkSystemLibrary("GLESv3");
	lib.linkSystemLibrary("EGL");
	lib.linkSystemLibrary("android");
	lib.linkSystemLibrary("log");
	
	const csrc_root = "src/sokol/c/";
	const cflags = &.{ "-DIMPL", "-DSOKOL_GLES3"};
	const csources = [_][]const u8{
		"sokol_log.c",
		// "sokol_app.c",
		// "sokol_gfx.c",
		// "sokol_gl.c",
		// "sokol_debugtext.c",
		// "sokol_debugtext.c",
		// "sokol_fontstash.c",
	};
	inline for (csources) |csrc| lib.addCSourceFile(.{
		.file = .{ .path = csrc_root ++ csrc },
		.flags = cflags,
	});
	return lib;
}

android-libc.conf

include_dir=/opt/android-sdk/ndk/25.2.9519653/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include
sys_include_dir=/opt/android-sdk/ndk/25.2.9519653/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include
crt_dir=/opt/android-sdk/ndk/25.2.9519653/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android/33
msvc_lib_dir=
kernel32_lib_dir=
gcc_dir=
[18:34:41] tsukasa@TO21 /home/tsukasa/develop/zigprojects/android-learn/buildSokolAndroid [1] 
> zig build
install
└─ install sokol-android
   └─ zig build-lib sokol-android Debug aarch64-linux-android failure
error: error: unable to find Dynamic system library 'GLESv3' using strategy 'paths_first'. searched paths: none
error: unable to find Dynamic system library 'EGL' using strategy 'paths_first'. searched paths: none
error: unable to find Dynamic system library 'android' using strategy 'paths_first'. searched paths: none
error: unable to find Dynamic system library 'log' using strategy 'paths_first'. searched paths: none

error: the following command exited with error code 1:
/home/tsukasa/lang/zig/zig build-lib -lGLESv3 -lEGL -landroid -llog -cflags -DIMPL -DSOKOL_GLES3 -- /home/tsukasa/develop/zigprojects/android-learn/buildSokolAndroid/src/sokol/c/sokol_log.c -ODebug -target aarch64-linux-android -mcpu baseline+v8a -Mroot -lc --libc /home/tsukasa/develop/zigprojects/android-learn/buildSokolAndroid/android-libc.conf --cache-dir /home/tsukasa/develop/zigprojects/android-learn/buildSokolAndroid/zig-cache --global-cache-dir /home/tsukasa/.cache/zig --name sokol-android -static --listen=- 
Build Summary: 0/3 steps succeeded; 1 failed (disable with --summary none)
install transitive failure
└─ install sokol-android transitive failure
   └─ zig build-lib sokol-android Debug aarch64-linux-android failure
error: the following build command failed with exit code 1:
/home/tsukasa/develop/zigprojects/android-learn/buildSokolAndroid/zig-cache/o/947db9d3280989bb219b99d0e0d3c84a/build /home/tsukasa/lang/zig/zig /home/tsukasa/develop/zigprojects/android-learn/buildSokolAndroid /home/tsukasa/develop/zigprojects/android-learn/buildSokolAndroid/zig-cache /home/tsukasa/.cache/zig --seed 0x647efe82 -Zcdadd5fcc5e174a0

Compilation fails on macOS

zig 0.8.0-dev.1039+bea791b63
macOS 11.1 (Big Sur)
Xcode 12.3

frarees@MBP18 sokol-zig % zig build run-triangle
error(compilation): clang failed with stderr: In file included from /Users/frarees/Documents/sokol-zig/src/sokol/c/sokol_audio.c:5:
In file included from /Users/frarees/Documents/sokol-zig/src/sokol/c/sokol_audio.h:511:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/AudioToolbox.framework/Headers/AudioToolbox.h:19:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/AudioToolbox.framework/Headers/AUGraph.h:30:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/AudioToolbox.framework/Headers/AudioUnit.h:21:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/AudioToolbox.framework/Headers/AudioUnitProperties.h:55:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup.h:30:
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_object.h:48:19: error: a parameter list without types is only allowed in a function definition
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_object.h:93:24: error: unknown type name 'os_workgroup_t'
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_object.h:116:1: error: unknown type name 'os_workgroup_t'
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_object.h:116:16: error: nullability specifier '_Nullable' cannot be applied to non-pointer type 'int'
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_object.h:137:1: error: unknown type name 'os_workgroup_t'
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_object.h:138:65: error: unknown type name 'os_workgroup_t'
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_object.h:137:16: error: nullability specifier '_Nullable' cannot be applied to non-pointer type 'int'
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_object.h:177:19: error: unknown type name 'os_workgroup_t'
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_object.h:202:20: error: unknown type name 'os_workgroup_t'
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_object.h:241:32: error: unknown type name 'os_workgroup_t'
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_object.h:271:32: error: unknown type name 'os_workgroup_t'
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_object.h:299:21: error: unknown type name 'os_workgroup_t'
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_object.h:311:25: error: unknown type name 'os_workgroup_t'
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_object.h:350:35: error: unknown type name 'os_workgroup_t'
In file included from /Users/frarees/Documents/sokol-zig/src/sokol/c/sokol_audio.c:5:
In file included from /Users/frarees/Documents/sokol-zig/src/sokol/c/sokol_audio.h:511:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/AudioToolbox.framework/Headers/AudioToolbox.h:19:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/AudioToolbox.framework/Headers/AUGraph.h:30:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/AudioToolbox.framework/Headers/AudioUnit.h:21:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/AudioToolbox.framework/Headers/AudioUnitProperties.h:55:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup.h:31:
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_interval.h:41:28: error: redefinition of parameter 'os_workgroup_interval'
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_interval.h:41:28: error: a parameter list without types is only allowed in a function definition
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_interval.h:94:29: error: unknown type name 'os_workgroup_interval_t'
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_interval.h:123:30: error: unknown type name 'os_workgroup_interval_t'
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_interval.h:148:30: error: unknown type name 'os_workgroup_interval_t'
fatal error: too many errors emitted, stopping now [-ferror-limit=]

/Users/frarees/Documents/sokol-zig/src/sokol/c/sokol_audio.c:1:1: error: unable to build C object: clang exited with code 1
sokol...The following command exited with error code 1:
/Users/frarees/Documents/zig/build/bin/zig build-lib -lc -cflags -ObjC -DIMPL -- /Users/frarees/Documents/sokol-zig/src/sokol/c/sokol_app_gfx.c -cflags -ObjC -DIMPL -- /Users/frarees/Documents/sokol-zig/src/sokol/c/sokol_time.c -cflags -ObjC -DIMPL -- /Users/frarees/Documents/sokol-zig/src/sokol/c/sokol_audio.c -cflags -ObjC -DIMPL -- /Users/frarees/Documents/sokol-zig/src/sokol/c/sokol_gl.c -cflags -ObjC -DIMPL -- /Users/frarees/Documents/sokol-zig/src/sokol/c/sokol_debugtext.c -cflags -ObjC -DIMPL -- /Users/frarees/Documents/sokol-zig/src/sokol/c/sokol_shape.c --cache-dir /Users/frarees/Documents/sokol-zig/zig-cache --global-cache-dir /Users/frarees/.cache/zig --name sokol -I /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -F /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks -framework Cocoa -framework AudioToolbox -framework QuartzCore -framework MetalKit -framework Metal --enable-cache 
error: the following build command failed with exit code 1:
/Users/frarees/Documents/sokol-zig/zig-cache/o/adc71d1a5b6b66a2383e69b42697249b/build /Users/frarees/Documents/zig/build/bin/zig /Users/frarees/Documents/sokol-zig /Users/frarees/Documents/sokol-zig/zig-cache /Users/frarees/.cache/zig run-triangle
frarees@MBP18 sokol-zig % /Users/frarees/Documents/zig/build/bin/zig build-lib -lc -cflags -ObjC -DIMPL -- /Users/frarees/Documents/sokol-zig/src/sokol/c/sokol_app_gfx.c -cflags -ObjC -DIMPL -- /Users/frarees/Documents/sokol-zig/src/sokol/c/sokol_time.c -cflags -ObjC -DIMPL -- /Users/frarees/Documents/sokol-zig/src/sokol/c/sokol_audio.c -cflags -ObjC -DIMPL -- /Users/frarees/Documents/sokol-zig/src/sokol/c/sokol_gl.c -cflags -ObjC -DIMPL -- /Users/frarees/Documents/sokol-zig/src/sokol/c/sokol_debugtext.c -cflags -ObjC -DIMPL -- /Users/frarees/Documents/sokol-zig/src/sokol/c/sokol_shape.c --cache-dir /Users/frarees/Documents/sokol-zig/zig-cache --global-cache-dir /Users/frarees/.cache/zig --name sokol -I /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -F /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks -framework Cocoa -framework AudioToolbox -framework QuartzCore -framework MetalKit -framework Metal --enable-cache

error(compilation): clang failed with stderr: In file included from /Users/frarees/Documents/sokol-zig/src/sokol/c/sokol_audio.c:5:
In file included from /Users/frarees/Documents/sokol-zig/src/sokol/c/sokol_audio.h:511:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/AudioToolbox.framework/Headers/AudioToolbox.h:19:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/AudioToolbox.framework/Headers/AUGraph.h:30:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/AudioToolbox.framework/Headers/AudioUnit.h:21:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/AudioToolbox.framework/Headers/AudioUnitProperties.h:55:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup.h:30:
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_object.h:48:19: error: a parameter list without types is only allowed in a function definition
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_object.h:93:24: error: unknown type name 'os_workgroup_t'
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_object.h:116:1: error: unknown type name 'os_workgroup_t'
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_object.h:116:16: error: nullability specifier '_Nullable' cannot be applied to non-pointer type 'int'
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_object.h:137:1: error: unknown type name 'os_workgroup_t'
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_object.h:138:65: error: unknown type name 'os_workgroup_t'
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_object.h:137:16: error: nullability specifier '_Nullable' cannot be applied to non-pointer type 'int'
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_object.h:177:19: error: unknown type name 'os_workgroup_t'
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_object.h:202:20: error: unknown type name 'os_workgroup_t'
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_object.h:241:32: error: unknown type name 'os_workgroup_t'
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_object.h:271:32: error: unknown type name 'os_workgroup_t'
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_object.h:299:21: error: unknown type name 'os_workgroup_t'
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_object.h:311:25: error: unknown type name 'os_workgroup_t'
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_object.h:350:35: error: unknown type name 'os_workgroup_t'
In file included from /Users/frarees/Documents/sokol-zig/src/sokol/c/sokol_audio.c:5:
In file included from /Users/frarees/Documents/sokol-zig/src/sokol/c/sokol_audio.h:511:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/AudioToolbox.framework/Headers/AudioToolbox.h:19:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/AudioToolbox.framework/Headers/AUGraph.h:30:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/AudioToolbox.framework/Headers/AudioUnit.h:21:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/AudioToolbox.framework/Headers/AudioUnitProperties.h:55:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup.h:31:
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_interval.h:41:28: error: redefinition of parameter 'os_workgroup_interval'
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_interval.h:41:28: error: a parameter list without types is only allowed in a function definition
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_interval.h:94:29: error: unknown type name 'os_workgroup_interval_t'
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_interval.h:123:30: error: unknown type name 'os_workgroup_interval_t'
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/os/workgroup_interval.h:148:30: error: unknown type name 'os_workgroup_interval_t'
fatal error: too many errors emitted, stopping now [-ferror-limit=]

/Users/frarees/Documents/sokol-zig/src/sokol/c/sokol_audio.c:1:1: error: unable to build C object: clang exited with code 1

Using OpenGL on macOS

Is it just me or it looks like there is no way to force sokol to use OpenGL when compiling for macOS?

[Help] Zig sokol-fetch bindings

I'd love to get the sokol fetch bindings available. I'd be happy to help with this, but I'm not sure how the zig bindings that currently exist are auto generated (a simple zig translate-c doesn't seem to align with the existing bindings). From the last issue I opened, it didn't sound like there was a reason they couldn't exist. Any direction on this would be appreciated!

sg.makeImage does not work outside the callbacks

Hello! My name is Neil and it's my first time using Sokol, Zig and "low level"-ish stuff in general.

I was working on my game/engine and I realized something, while sg.makeImage() worked inside the sokol callbacks (src/main.zig), it didnt work on other files (src/game.zig) that were accessing functions/methods/whatever (Texture.fromPNGPath) from where the callbacks were stored (src/main.zig).

OS: Void Linux + Musl libc
Arch: x86_64
Zig version: 0.7.1

Full error message:

Assertion failed: _sg.valid (/home/darltrash/Projects/zigengine/src/sokol/c/sokol_gfx.h: sg_make_image: 14662)
The following command terminated unexpectedly:
cd /home/darltrash/Projects/zigengine && /home/darltrash/Projects/zigengine/zig-cache/o/c21211b4a21069a353acbf41275cdaba/PROJECT SCRUMPTIOUS 
error: the following build command failed with exit code 1:
/home/darltrash/Projects/zigengine/zig-cache/o/26725e637f9cba583ffd76ff7c7a828b/build /usr/bin/zig /home/darltrash/Projects/zigengine /home/darltrash/Projects/zigengine/zig-cache /home/darltrash/.cache/zig run

If this is my fault, please let me know. I apologize beforehand.

Conclusions?

Can you share your conclusions so far? Is there anything you miss in zig? Why did you need those conv_xxx() adapter functions?

I'm considering starting a project using vulkan and I guess I'll face similar problems...

Add Zig 8.0 support

Hello! I was trying to compile Sokol with the latest Zig and it gave me an error caused by the change from pub inline fn to fn XXX() callconv(.Inline), so i decided to fix it. here is my attempt.

I just changed line 436 from the gen_zig.py bindgen to:

l(f"pub fn {zig_func_name}({funcdecl_args_zig(decl, prefix)}) callconv(.Inline) {funcdecl_res_zig(decl, prefix)} {{")

It seems to be working just fine, but some examples break (the instancing one, for example (no pun intended)).

The example error was this:

./src/examples/instancing.zig:136:25: error: expected type 'i32', found 'u32'
    sg.draw(0, 24, state.cur_num_particles);
                        ^
./src/examples/instancing.zig:136:25: note: signed 32-bit int cannot represent all possible unsigned 32-bit values
    sg.draw(0, 24, state.cur_num_particles);
                        ^
instancing...The following command exited with error code 1:
/usr/bin/zig build-exe /home/darltrash/Projects/sokol/bindgen/sokol-zig/src/examples/instancing.zig /home/darltrash/Projects/sokol/bindgen/sokol-zig/zig-cache/o/5e25f44492e48a144bd0f39181fc832c/libsokol.a -lc -lX11 -lXi -lXcursor -lGL -lasound --cache-dir /home/darltrash/Projects/sokol/bindgen/sokol-zig/zig-cache --global-cache-dir /home/darltrash/.cache/zig --name instancing --pkg-begin sokol /home/darltrash/Projects/sokol/bindgen/sokol-zig/src/sokol/sokol.zig --pkg-end --enable-cache 
error: the following build command failed with exit code 1:
/home/darltrash/Projects/sokol/bindgen/sokol-zig/zig-cache/o/3f62bb538e9d96b9e2c16e827bcef4b7/build /usr/bin/zig /home/darltrash/Projects/sokol/bindgen/sokol-zig /home/darltrash/Projects/sokol/bindgen/sokol-zig/zig-cache /home/darltrash/.cache/zig run-instancing

Windows now needs gdi32

I'm not sure if there's a smarter way to do this, but in order to get head sokol, bindings newly generated, to build, I needed to add this to my project:

    } else if (lib.target.isWindows()) {
        lib.linkSystemLibrary("gdi32");
    }

Because of the icon support in sokol_app it seems.

Question: 0.11.0

There is no log.zig in the zig-0.11.0 branch, is this intentional?

incompatible with latest zig?

Things were working a month ago, but now I'm on 0.10.0-dev.3978+4fd4c733d, and getting new mystery errors ~ I was wondering if this looks like something I might be able to patch quickly, or if something more fundamental is going on here?

/Users/nporcino/dev/time_hierarchies/third-party/sokol-zig/src/sokol/app.zig:221:5: error: extern structs cannot contain fields of type '?fn() callconv(.C) void'
    init_cb: ?fn() callconv(.C) void = null,
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/nporcino/dev/time_hierarchies/third-party/sokol-zig/src/sokol/app.zig:221:5: note: only pointer like optionals are extern compatible
/Users/nporcino/dev/time_hierarchies/third-party/sokol-zig/src/sokol/gfx.zig:658:5: error: extern structs cannot contain fields of type '?fn(usize, ?*anyopaque) callconv(.C) ?*anyopaque'
    alloc: ?fn(usize, ?*anyopaque) callconv(.C) ?*anyopaque = null,
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/nporcino/dev/time_hierarchies/third-party/sokol-zig/src/sokol/gfx.zig:658:5: note: only pointer like optionals are extern compatible

Add License

It would be good to add a LICENSE file, so it is clear how this code can be used.

I was thinking about maybe pulling out the emsdk setup code into a separate dependency, so that the emsdk code can be reused with other projects, but stopped because of missing license info.

EGL Support?

This issue was mostly made to point to a problem I had trying to use GLES2 with sokol_app.h in Linux, which would require me to link towards EGL and use the EGL X11 backend instead.

I've already made some patches and published them as pull requests, it's a bit unclean as i have mentioned in the PR, but I hope it's helpful :)

Update Readme for 0.11

The example for Use as Library should be updated as follows to work on Zig 0.11

const sokol = @import("lib/sokol-zig/build.zig");

// ...
// pub fn build(b: *std.build.Builder) void {
// ...

const sokol_build = sokol.buildSokol(b, target, optimize, .{}, "lib/sokol-zig/");

// ...
// const exe = b.addExecutable("demo", "src/main.zig");
// ...

exe.addAnonymousModule("sokol", .{ .source_file = .{ .path = "lib/sokol-zig/src/sokol/sokol.zig" } });
exe.linkLibrary(sokol_build);

exe.addPackagePath is now exe.addAnonymouseModule with a different input for the source file
mode is now named optimize

Example of how to use `sokol-zig` with `sokol_imgui.h`?

Hello, thanks for this library and the many clean and well-commented examples.

Is there an example of how I can use sokol_imgui.h with the sokol-zig bindings. I started to attempt @cImport-ing the header file directly, but ran into some errors("Please include sokol_app.h before sokol_imgui.h") probably because I'm using the Zig bindings.

Do you plan on providing Zig bindings for sokol_imgui.h? Or is there a way of using sokol-zig and sokol_imgui.h?

Finding OpenGL/gl.h on mac

const usrinclude_dir = try mem.concat(b.allocator, u8, &[_][]const u8 { sdk_dir, "/usr/include"});

I guess, but am not sure, that this include of /usr/include was meant to pick up <OpenGL/gl.h> $SDK_ROOT/usr/include still exists, but there's nothing at $SDK_ROOT/usr/include/OpenGL in Catalina or Big Sur. I was able to get past the issue I ran into of zig not finding OpenGL/gl.h by invoking ln -s $SDK_ROOT/Frameworks/OpenGL/Headers OpenGL where zig can see it, making <OpenGL/gl.h> a legitimate path.

If I misunderstand why you have /usr/include specified, please disregard :)

Reminder: Stage2 compiler and new function pointer syntax.

See here: ziglang/zig#89 (comment)

Changing the bindings manually for the new syntax triggers problems further down though:

  • in the stage2 compiler, the triangle sample just renders a blank screen
  • more complex samples (e.g. the cube sample) just crash the compiler
  • the clear sample works though!

In the stage1 compiler, the code compiles with the new function pointer syntax, but then crashes when calling a callback out of the C code. ...this is because stage one interprets the new function point syntax as a function-pointer-pointer.

...all this only tested on an M1 Mac so far.

Idea: move Emscripten include path injection and depending on emscripten sdk into build.zig

...we're iterating over the C library dependencies here anyway:

sokol-zig/build.zig

Lines 351 to 367 in d9f3ef9

emcc.addArtifactArg(options.lib_main);
var it = options.lib_main.root_module.iterateDependencies(options.lib_main, false);
while (it.next()) |item| {
for (item.module.link_objects.items) |link_object| {
switch (link_object) {
.other_step => |compile_step| {
switch (compile_step.kind) {
.lib => {
emcc.addArtifactArg(compile_step);
},
else => {},
}
},
else => {},
}
}
}

...maybe the Emscripten include path could be injected there, as well as depending on the Emscripten SDK setup step? That way the toplevel project wouldn't need to take care of that, and it's needed for every C library anyway.

Wrong type for `getClipboardString()`

When calling getClipboardString zig will fail to compile.

./lib/sokol-zig/src/sokol/app.zig:380:37: error: expected type '[:0]const u8', found '[*c]const u8'
    return sapp_get_clipboard_string();
                                    ^

A local solution I've found is to change the return type of getClipboardString() to [*c]const u8 and then run std.mem.span(sapp.getClipboardString()) as the result.

Since Sokol-zig is a autogenerated wrapper for Sokol the main reason I'm creating this issue is to get guidance on what sort of direction we want to create the fix to keep it inline with the spirit of the project. Once I receive that guidance I'll create a PR in the main floooh/sokol repository based on that response.

Compilation Fails zig 0.8.0-dev.1141+68e772647 on Windows 10

Hello, I saw in the one other issue currently open that you are sticking with zig 7.1 right now... but thought it would be worthwhile giving you a heads up anyway. The line it is complaining about seems okay to me?


PS F:\github\paulevans\sokol-zig> zig version
0.8.0-dev.1141+68e772647
PS F:\github\paulevans\sokol-zig> zig build
Semantic Analysis... .\src\sokol\gfx.zig:718:5: error: expected function or variable declaration after pub
pub inline fn setup(desc: Desc) void {
    ^
clear...The following command exited with error code 1:
C:\Apps\zig-windows-x86_64-0.8.0-dev.1141+68e772647\zig.exe build-exe F:\github\paulevans\sokol-zig\src\examples\clear.zig F:\github\paulevans\sokol-zig\zig-cache\o\127299955b41b17a241a44ab60483b30\sokol.lib -lc --cache-dir F:\github\paulevans\sokol-zig\zig-cache --global-cache-dir C:\Users\pevans\AppData\Local\zig --name clear --pkg-begin sokol F:\github\paulevans\sokol-zig\src\sokol\sokol.zig --pkg-end --enable-cache
error: the following build command failed with exit code 1:
F:\github\paulevans\sokol-zig\zig-cache\o\bb6c8c71e8d64ef9dad598f452b0d922\build.exe C:\Apps\zig-windows-x86_64-0.8.0-dev.1141+68e772647\zig.exe F:\github\paulevans\sokol-zig F:\github\paulevans\sokol-zig\zig-cache C:\Users\pevans\AppData\Local\zig

Change `var`s to `const`s

Though the readme officially says that the bindings are compatible with Zig 0.11, in reality they are perfectly compatible with Zig 0.12 as well, except having a conflict with one of the cornerstone features of that version. A conflict that is easy to fix though!

As you might know, Zig 0.12 introduces a new error about having unchanged vars in your code: https://ziggit.dev/t/error-local-variable-is-never-mutated/2238

So while Zig 0.12 is still in dev, fixing this problem would make them compatible without breaking anything, even if the official support still stays on Zig 0.11 for now. But it would remove the hindrance when updating from the repository.
So it would be great if the bindings were updated to get rid of this "sloppy code".

cross compiling sokol_app.h for windows

I have a sokol app written in C that I would like to cross compile from mac to windows. (there is no zig code, I'm just using zig as a C cross compiler)

building with the following command produces an exe that shows a "missing ucrtbase.dll" error when launched on windows 7.

daniels-mac$ zig cc -I include -ld3d11 -lgdi32 -target x86_64-windows-gnu -DSOKOL_WIN32_FORCE_MAIN -DSOKOL_D3D11 -o test.exe src/main.c

I tried instead compiling these other ways but they each produced other errors:

zig cc -I include -ld3d11 -lgdi32 -target x86_64-windows-gnu -DSOKOL_D3D11 -o $@ src/main.c  # linker error: missing main symbol

zig cc -I include -ld3d11 -lgdi32 -target x86_64-windows-msvc -DSOKOL_D3D11 -o $@ src/sokol_main.c # linker error: unable to find Dynamic system library 'd3d11' using strategy 'paths_first'. searched paths: none

let me know if you need a project zip to reproduce.

wrapping c errors as zig errors

makeBuffer and many other make functions can return null

pub fn makeBuffer(desc: BufferDesc) Buffer {
    return sg_make_buffer(&desc);
}

pub fn makeBuffer(desc: BufferDesc) !Buffer {
    const res = sg_make_buffer(&desc);
    if(res.id == 0) return error.PoolExhausted;
    return res;
}

I think this could be done with a minor modification to gen_zig.py def gen_func_zig but I'm not sure if there's any way for it to know which functions to do this for or what field to check.

def gen_func_zig(decl, prefix):
    c_func_name = decl['name']
    zig_func_name = as_camel_case(check_name_override(decl['name']))
    zig_res_type = funcdecl_result_zig(decl, prefix)
+   can_error = decl['name'].startsWith("sg_make_")
    l(f"pub fn {zig_func_name}({funcdecl_args_zig(decl, prefix)}) {zig_res_type} {{")
+   if can_error:
+       s = f"    const res = {c_func_name}("
-   if zig_res_type != 'void':
+   elif zig_res_type != 'void':
        s = f"    return {c_func_name}("
    else:
        s = f"    {c_func_name}("
    for i, param_decl in enumerate(decl['params']):
        if i > 0:
            s += ", "
        arg_name = param_decl['name']
        arg_type = param_decl['type']
        if is_const_struct_ptr(arg_type):
            s += f"&{arg_name}"
        elif is_string_ptr(arg_type):
            s += f"@ptrCast([*c]const u8,{arg_name})"
        else:
            s += arg_name
    s += ");"
    l(s)
+   if can_error:
+       l("    if (res.id == 0) return error.PoolExhausted;")
+       l("    return res;")
    l("}")

Feature request: generate `inline` for simple wrapper functions

A lot of generated functions are merely small wrappers around C functions, e.g.:

pub fn makeImage(desc: ImageDesc) Image {
    return sg_make_image(&desc);
}

These will almost certainly be inlined by the compiler, but generating them as pub inline fn would ensure it (and, presumably, mean less work on the compiler for optimization, since it doesn't have to check whether to do optimization).

Add sokol-shdc compiled shader for triangle sample.

The triangle sample currently doesn't work in WASM because there's no embedded shader for WebGL2, and this embedded shader makes the triangle sample kinda awkward anyway, instead use a regular .glsl shader.

sokol_imgui.h: C++ error: member reference base type 'ImDrawList **' (aka 'struct ImDrawList **') is not a structure or union

Hi!

I have added sokol-zig to my project per this url in build.zig.zon: git+https://github.com/floooh/sokol-zig.git#ca76d1e66b2539a6614535aaad39d0168985a2e9

and then of course my build.zig:

const dep_sokol = b.dependency("sokol", .{
    .target = target,
    .optimize = optimize,
    .with_sokol_imgui = true,
});
dep_sokol.artifact("sokol_clib").addIncludePath(b.path("deps/")); // I have custom cimgui.h which includes the original "cimgui/cimgui.h" from there
exe.root_module.addImport("sokol", dep_sokol.module("sokol"));

And here I got some C++ error:

/Users/namek/.cache/zig/p/122055d69396139fc4b51ba859a2af304608e336572084ed44c5c79504be9ae2aba8/src/sokol/c/sokol_imgui.h:2587:35: error: member reference base type 'ImDrawList **' (aka 'struct ImDrawList **') is not a structure or union
        return draw_data->CmdLists.Data[cl_index];
               ~~~~~~~~~~~~~~~~~~~^~
/Users/namek/.cache/zig/p/122055d69396139fc4b51ba859a2af304608e336572084ed44c5c79504be9ae2aba8/src/sokol/c/sokol_imgui.c:9:10: note: in file included from /Users/namek/.cache/zig/p/122055d69396139fc4b51ba859a2af304608e336572084ed44c5c79504be9ae2aba8/src/sokol/c/sokol_imgui.c:9:
#include "sokol_imgui.h"
         ^

Any ideas what's wrong?

My (c)imgui is "1.89.7"

Can not compile in Windows

Hi!

I don't think it's a bug in sokol, most likely it's a bug in the Zig compiler or a user error.
But I was wondering if you could help me.
Have you been able to compile any sokol app in Windows recenlty?
I'm pretty sure that I've been able to compile my sokol app at some point, but it stopped working. There seems to be an error linking WinMain. I have found that it might be a more general problem. But I'm left wondering why I'm experiencing this issue now, and if other people have this issue too.

Thanks for making these awesome libraries!

[Help] Build targeting emscripten fails due to cImport of stb_image.h

I'm working on this repo https://github.com/trace-andreason/zig-learn-opengl-in-sokol where I'm going through the examples in the sokol learn openGL repo https://github.com/zeromake/learnopengl-examples and converting them over to zig. I can't figure out how to get the examples that need image files for textures to build using the emscripten target. Its complaining about not being able to find stdio.h.

I've looked through your other repos, and I see that files are never loaded due to issues with zig and wasm. I'm new to basically everything involved here so I'm not sure if there is anything I can do to get this working. I've posted the full verbose build output below.

I guess my question is, is loading images for textures just not possible right now? Should I just move on? Thanks in advanced for the help!

zig build run-texture -Dtarget=wasm32-emscripten --verbose
/opt/homebrew/Cellar/zig/0.12.0/bin/zig build-lib -cflags -DIMPL -DSOKOL_GLES3 -- /Users/twork/.cache/zig/p/12204b76dc14c74da9d61d01b5bd7a4fbfd0614aa7cc0a7428de0742129203ca5008/src/sokol/c/sokol_log.c -cflags -DIMPL -DSOKOL_GLES3 -- /Users/twork/.cache/zig/p/12204b76dc14c74da9d61d01b5bd7a4fbfd0614aa7cc0a7428de0742129203ca5008/src/sokol/c/sokol_app.c -cflags -DIMPL -DSOKOL_GLES3 -- /Users/twork/.cache/zig/p/12204b76dc14c74da9d61d01b5bd7a4fbfd0614aa7cc0a7428de0742129203ca5008/src/sokol/c/sokol_gfx.c -cflags -DIMPL -DSOKOL_GLES3 -- /Users/twork/.cache/zig/p/12204b76dc14c74da9d61d01b5bd7a4fbfd0614aa7cc0a7428de0742129203ca5008/src/sokol/c/sokol_time.c -cflags -DIMPL -DSOKOL_GLES3 -- /Users/twork/.cache/zig/p/12204b76dc14c74da9d61d01b5bd7a4fbfd0614aa7cc0a7428de0742129203ca5008/src/sokol/c/sokol_audio.c -cflags -DIMPL -DSOKOL_GLES3 -- /Users/twork/.cache/zig/p/12204b76dc14c74da9d61d01b5bd7a4fbfd0614aa7cc0a7428de0742129203ca5008/src/sokol/c/sokol_gl.c -cflags -DIMPL -DSOKOL_GLES3 -- /Users/twork/.cache/zig/p/12204b76dc14c74da9d61d01b5bd7a4fbfd0614aa7cc0a7428de0742129203ca5008/src/sokol/c/sokol_debugtext.c -cflags -DIMPL -DSOKOL_GLES3 -- /Users/twork/.cache/zig/p/12204b76dc14c74da9d61d01b5bd7a4fbfd0614aa7cc0a7428de0742129203ca5008/src/sokol/c/sokol_shape.c -cflags -DIMPL -DSOKOL_GLES3 -- /Users/twork/.cache/zig/p/12204b76dc14c74da9d61d01b5bd7a4fbfd0614aa7cc0a7428de0742129203ca5008/src/sokol/c/sokol_glue.c -ODebug -target wasm32-emscripten -mcpu baseline -isystem /Users/twork/.cache/zig/p/122034cf4aca9f97fea3c34f3e9fe92e56f08e2160efe3c95d7ec89260e621426a81/upstream/emscripten/cache/sysroot/include -Mroot -lc --cache-dir /Users/twork/Documents/zig/test/zig-cache --global-cache-dir /Users/twork/.cache/zig --name sokol -static --listen=-
/opt/homebrew/Cellar/zig/0.12.0/bin/zig build-lib /Users/twork/Documents/zig/test/src/stb_image.c -ODebug -target wasm32-emscripten -mcpu baseline -I /Users/twork/Documents/zig/test/src --dep sokol -Mroot=/Users/twork/Documents/zig/test/src/1-6-textures/1-texture.zig -I /Users/twork/Documents/zig/test/zig-cache/o/bab42bbbea71a70c78c338ea9331fb6e -Msokol=/Users/twork/.cache/zig/p/12204b76dc14c74da9d61d01b5bd7a4fbfd0614aa7cc0a7428de0742129203ca5008/src/sokol/sokol.zig -lc --cache-dir /Users/twork/Documents/zig/test/zig-cache --global-cache-dir /Users/twork/.cache/zig --name texture -static --listen=-
run-texture
└─ run /Users/twork/.cache/zig/p/122034cf4aca9f97fea3c34f3e9fe92e56f08e2160efe3c95d7ec89260e621426a81/upstream/emscripten/emrun
   └─ emcc
      └─ zig build-lib texture Debug wasm32-emscripten 4 errors
/Users/twork/Documents/zig/test/src/stb_image.c:2:23: error: extra tokens at end of #include directive
/Users/twork/Documents/zig/test/src/stb_image.h:370:10: error: 'stdio.h' file not found
#include <stdio.h>
         ^~~~~~~~~~
/Users/twork/Documents/zig/test/src/stb_image.c:2:10: note: in file included from /Users/twork/Documents/zig/test/src/stb_image.c:2:
src/1-6-textures/1-texture.zig:10:11: error: C import failed
const c = @cImport({
          ^~~~~~~~
referenced by:
    loadImage: src/1-6-textures/1-texture.zig:63:18
    init: src/1-6-textures/1-texture.zig:26:5
    remaining reference traces hidden; use '-freference-trace' to see all reference traces
/Users/twork/Documents/zig/test/src/stb_image.h:370:10: error: 'stdio.h' file not found
#include <stdio.h>
         ^
error: the following command failed with 4 compilation errors:
/opt/homebrew/Cellar/zig/0.12.0/bin/zig build-lib /Users/twork/Documents/zig/test/src/stb_image.c -ODebug -target wasm32-emscripten -mcpu baseline -I /Users/twork/Documents/zig/test/src --dep sokol -Mroot=/Users/twork/Documents/zig/test/src/1-6-textures/1-texture.zig -I /Users/twork/Documents/zig/test/zig-cache/o/bab42bbbea71a70c78c338ea9331fb6e -Msokol=/Users/twork/.cache/zig/p/12204b76dc14c74da9d61d01b5bd7a4fbfd0614aa7cc0a7428de0742129203ca5008/src/sokol/sokol.zig -lc --cache-dir /Users/twork/Documents/zig/test/zig-cache --global-cache-dir /Users/twork/.cache/zig --name texture -static --listen=-
Build Summary: 2/6 steps succeeded; 1 failed (disable with --summary none)
run-texture transitive failure
└─ run /Users/twork/.cache/zig/p/122034cf4aca9f97fea3c34f3e9fe92e56f08e2160efe3c95d7ec89260e621426a81/upstream/emscripten/emrun transitive failure
   └─ emcc transitive failure
      └─ zig build-lib texture Debug wasm32-emscripten 4 errors
error: the following build command failed with exit code 1:
/Users/twork/Documents/zig/test/zig-cache/o/d5acdff3f8d287c7642b9d7180ca1cb8/build /opt/homebrew/Cellar/zig/0.12.0/bin/zig /Users/twork/Documents/zig/test /Users/twork/Documents/zig/test/zig-cache /Users/twork/.cache/zig --seed 0xb5aabee1 -Zc15ddec284e8ad70 run-texture -Dtarget=wasm32-emscripten --verbose

sokol_nuklear zig bindings

Hello!
I was looking to add sokol_nuklear bindings to this repository for usage inside of my project, but I noticed that the bindings were all machine-generated. I couldn't find a tool anywhere that would let me generate these bindings, but I did see your blog post.

Could you shed some light on how I can make these bindings please?
Thank you

Custom c definitions

I'm trying to provide a custom definition for SOKOL_LOG but the only simple way I could come up with was editing sokol_defines.h directly. That's not ideal since that file is overwritten with every update of the library.

What I currently do is edit sokol_defines.h like so:

// sokol_defines.h
#define SOKOL_ZIG_BINDINGS
#define SOKOL_NO_ENTRY
#define SOKOL_LOG(msg) sokol_log_callback(msg)
#if defined(_WIN32)
    #define SOKOL_WIN32_FORCE_MAIN
    // #define SOKOL_LOG(msg) OutputDebugStringA(msg)
#endif
// FIXME: macOS Zig HACK without this, some C stdlib headers throw errors
#if defined(__APPLE__)
#include <TargetConditionals.h>
#endif

And then in my zig code that statically links sokol, I do

export fn sokol_log_callback(msg: [*c]const u8) void {
    std.log.scoped(.sokol).err("{s}", .{msg});
}

It would be nice if there was a way to customize sokol from build.zig. One way would be to provide an option for a path to your own sokol_defines.h and fall back to the default one if not specified.

// Sidenote: Maybe consider replacing some of these parameters with a struct, which could then also have default-initialized options like the backend.
pub fn buildSokol(b: *Builder, [...], comptime defines_file: ?[]const u8) *LibExeObjStep {
    // ...
    lib.addCSourceFile(sokol_path ++ csrc, &[_][]const u8{"-DIMPL", backend_option, "-DSOKOL_DEFINES=" ++ (defines_file orelse (sokol_path ++ "sokol_defines.h"))});
    // ...
}

And then in all the sokol_*.c files, use it like this:

// sokol_gfx.c
#if defined(IMPL)
#define SOKOL_GFX_IMPL
#endif
#include SOKOL_DEFINES
#include "sokol_gfx.h"

But, to be honest, I feel like you would want to provide this customizability per sokol library. So maybe let the user provide their own versions of sokol_*.c? Or even just a single custom sokol.c that would include all sokol headers as needed.

Another way I thought about was "fixing" up the LibExeObjStep instance produced by buildSokol, iterating over all link_objects, pull out the c files, and replace them with my own. But that felt pretty hacky so I didn't even try this route.

Thoughts?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.