Skip to content

Commit ecb71d1

Browse files
committed
add example which exercises addObject in the zig build system
closes #329
1 parent fb492d1 commit ecb71d1

File tree

5 files changed

+56
-1
lines changed

5 files changed

+56
-1
lines changed

example/mix_o_files/base64.zig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const base64 = @import("std").base64;
2+
3+
export fn decode_base_64(dest_ptr: &u8, dest_len: usize, source_ptr: &const u8, source_len: usize) -> usize {
4+
const src = source_ptr[0...source_len];
5+
const dest = dest_ptr[0...dest_len];
6+
return base64.decode(dest, src).len;
7+
}

example/mix_o_files/build.zig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const Builder = @import("std").build.Builder;
2+
3+
pub fn build(b: &Builder) {
4+
const obj = b.addObject("base64", "base64.zig");
5+
6+
const exe = b.addCExecutable("test");
7+
exe.addCompileFlags([][]const u8 {
8+
"-std=c99",
9+
});
10+
exe.addSourceFile("test.c");
11+
exe.addObject(obj);
12+
13+
b.default_step.dependOn(&exe.step);
14+
15+
const run_cmd = b.addCommand(b.out_dir, b.env_map, "./test", [][]const u8{});
16+
run_cmd.step.dependOn(&exe.step);
17+
18+
const test_step = b.step("test", "Test the program");
19+
test_step.dependOn(&run_cmd.step);
20+
}

example/mix_o_files/test.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// This header is generated by zig from base64.zig
2+
#include "base64.h"
3+
4+
#include <assert.h>
5+
#include <string.h>
6+
7+
int main(int argc, char **argv) {
8+
const char *encoded = "YWxsIHlvdXIgYmFzZSBhcmUgYmVsb25nIHRvIHVz";
9+
char buf[200];
10+
11+
size_t len = decode_base_64(buf, 200, encoded, strlen(encoded));
12+
buf[len] = 0;
13+
assert(strcmp(buf, "all your base are belong to us") == 0);
14+
15+
return 0;
16+
}

std/build.zig

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ pub const Builder = struct {
128128

129129
pub fn addObject(self: &Builder, name: []const u8, root_src: []const u8) -> &ObjectStep {
130130
const obj_step = %%self.allocator.create(ObjectStep);
131-
*obj_step = ObjectStep.init(self, name, src);
131+
*obj_step = ObjectStep.init(self, name, root_src);
132132
return obj_step;
133133
}
134134

@@ -1567,6 +1567,17 @@ pub const CExecutable = struct {
15671567
%%self.include_dirs.append(self.builder.out_dir);
15681568
}
15691569

1570+
pub fn addObject(self: &CExecutable, obj: &ObjectStep) {
1571+
self.step.dependOn(&obj.step);
1572+
1573+
// TODO make it so we always know where this will be
1574+
%%self.object_files.append(%%os.path.join(self.builder.allocator, self.builder.out_dir,
1575+
self.builder.fmt("{}{}", obj.name, obj.target.oFileExt())));
1576+
1577+
// TODO should be some kind of isolated directory that only has this header in it
1578+
%%self.include_dirs.append(self.builder.out_dir);
1579+
}
1580+
15701581
pub fn addSourceFile(self: &CExecutable, file: []const u8) {
15711582
%%self.source_files.append(file);
15721583
}

test/build_examples.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ pub fn addCases(cases: &tests.BuildExamplesContext) {
66
cases.add("example/cat/main.zig");
77
cases.add("example/guess_number/main.zig");
88
cases.addBuildFile("example/shared_library/build.zig");
9+
cases.addBuildFile("example/mix_o_files/build.zig");
910
}

0 commit comments

Comments
 (0)