-
-
Notifications
You must be signed in to change notification settings - Fork 18
Open
Labels
enhancementNew feature or requestNew feature or request
Description
Example of not being able to import certain .h files due to limitations of the build system step:
diff --git a/build.zig b/build.zig
index 738bc4f..52d6f01 100644
--- a/build.zig
+++ b/build.zig
@@ -5,6 +5,12 @@ pub fn build(b: *std.Build) void {
const optimize = b.standardOptimizeOption(.{});
const use_llvm = b.option(bool, "use-llvm", "use the LLVM backend");
+ const translate_c = b.addTranslateC(.{
+ .root_source_file = b.path("src/c.h"),
+ .target = target,
+ .optimize = optimize,
+ .link_libc = true,
+ });
const exe = b.addExecutable(.{
.name = "tetris",
.root_source_file = b.path("src/main.zig"),
@@ -13,10 +19,10 @@ pub fn build(b: *std.Build) void {
.use_llvm = use_llvm,
.use_lld = use_llvm,
});
-
- exe.linkLibC();
+ exe.root_module.addImport("c", translate_c.createModule());
exe.linkSystemLibrary("glfw");
exe.linkSystemLibrary("epoxy");
+
b.installArtifact(exe);
const play = b.step("play", "Play the game");
diff --git a/src/c.zig b/src/c.zig
deleted file mode 100644
index 7a9f0a2..0000000
--- a/src/c.zig
+++ /dev/null
@@ -1,8 +0,0 @@
-pub usingnamespace @cImport({
- @cInclude("stdio.h");
- @cInclude("math.h");
- @cInclude("time.h");
- @cInclude("stdlib.h");
- @cInclude("epoxy/gl.h");
- @cInclude("GLFW/glfw3.h");
-});
diff --git a/src/main.zig b/src/main.zig
index c3b8498..2ca6c56 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -8,7 +8,7 @@ const Tetris = @import("tetris.zig").Tetris;
const std = @import("std");
const assert = std.debug.assert;
const bufPrint = std.fmt.bufPrint;
-const c = @import("c.zig");
+const c = @import("c");
const debug_gl = @import("debug_gl.zig");
const AllShaders = @import("all_shaders.zig").AllShaders;
const StaticGeometry = @import("static_geometry.zig").StaticGeometry;
[nix-shell:~/dev/tetris]$ zig build
warning: file_system_inputs empty
install
└─ install tetris
└─ zig build-exe tetris Debug native
└─ translate-c 1 errors
/home/andy/dev/tetris/src/c.h:5:10: error: 'epoxy/gl.h' file not found
#include <epoxy/gl.h>
^
error: the following command failed with 1 compilation errors:
/home/andy/local/bin/zig translate-c -lc --listen=- /home/andy/dev/tetris/src/c.h
Build Summary: 0/4 steps succeeded; 1 failed
install transitive failure
└─ install tetris transitive failure
└─ zig build-exe tetris Debug native transitive failure
└─ translate-c 1 errors
error: the following build command failed with exit code 1:
/home/andy/dev/tetris/.zig-cache/o/7fddf9d37ec7fba8c98bb526aad30ae4/build /home/andy/local/bin/zig /home/andy/local/lib/zig /home/andy/dev/tetris /home/andy/dev/tetris/.zig-cache /home/andy/.cache/zig --seed 0xf1bb8c15 -Z7d71b57141fa9992
[nix-shell:~/dev/tetris]$ zig build
/home/andy/dev/tetris/build.zig:14:16: error: no field or member function named 'linkSystemLibrary' in 'Build.Step.TranslateC'
translate_c.linkSystemLibrary("glfw");
~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
/home/andy/local/lib/zig/std/Build/Step/TranslateC.zig:1:1: note: struct declared here
const std = @import("std");
^~~~~
referenced by:
runBuild__anon_10304: /home/andy/local/lib/zig/std/Build.zig:2140:33
main: /home/andy/local/lib/zig/compiler/build_runner.zig:314:29
3 reference(s) hidden; use '-freference-trace=5' to see all references
In the compiler, zig translate-c
is handled with generally the same code path as build-obj
, build-exe
, and build-lib
. It branches off near the end. std.Build.Step.TranslateC
either needs to be absorbed by, or duplicate a large portion of the API of std.Build.Step.Compile
.
Furthermore, there is interest in reducing the amount of unique C imports. Perhaps the build system API should provide some mechanism that can help modules from different packages coordinate about this.
Related to ziglang/zig#7687.
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request