Commit ecb71d1dd3

Andrew Kelley <superjoe30@gmail.com>
2017-04-21 08:26:48
add example which exercises addObject in the zig build system
closes #329
1 parent fb492d1
Changed files (5)
example/mix_o_files/base64.zig
@@ -0,0 +1,7 @@
+const base64 = @import("std").base64;
+
+export fn decode_base_64(dest_ptr: &u8, dest_len: usize, source_ptr: &const u8, source_len: usize) -> usize {
+    const src = source_ptr[0...source_len];
+    const dest = dest_ptr[0...dest_len];
+    return base64.decode(dest, src).len;
+}
example/mix_o_files/build.zig
@@ -0,0 +1,20 @@
+const Builder = @import("std").build.Builder;
+
+pub fn build(b: &Builder) {
+    const obj = b.addObject("base64", "base64.zig");
+
+    const exe = b.addCExecutable("test");
+    exe.addCompileFlags([][]const u8 {
+        "-std=c99",
+    });
+    exe.addSourceFile("test.c");
+    exe.addObject(obj);
+
+    b.default_step.dependOn(&exe.step);
+
+    const run_cmd = b.addCommand(b.out_dir, b.env_map, "./test", [][]const u8{});
+    run_cmd.step.dependOn(&exe.step);
+
+    const test_step = b.step("test", "Test the program");
+    test_step.dependOn(&run_cmd.step);
+}
example/mix_o_files/test.c
@@ -0,0 +1,16 @@
+// This header is generated by zig from base64.zig
+#include "base64.h"
+
+#include <assert.h>
+#include <string.h>
+
+int main(int argc, char **argv) {
+    const char *encoded = "YWxsIHlvdXIgYmFzZSBhcmUgYmVsb25nIHRvIHVz";
+    char buf[200];
+
+    size_t len = decode_base_64(buf, 200, encoded, strlen(encoded));
+    buf[len] = 0;
+    assert(strcmp(buf, "all your base are belong to us") == 0);
+
+    return 0;
+}
std/build.zig
@@ -128,7 +128,7 @@ pub const Builder = struct {
 
     pub fn addObject(self: &Builder, name: []const u8, root_src: []const u8) -> &ObjectStep {
         const obj_step = %%self.allocator.create(ObjectStep);
-        *obj_step = ObjectStep.init(self, name, src);
+        *obj_step = ObjectStep.init(self, name, root_src);
         return obj_step;
     }
 
@@ -1567,6 +1567,17 @@ pub const CExecutable = struct {
         %%self.include_dirs.append(self.builder.out_dir);
     }
 
+    pub fn addObject(self: &CExecutable, obj: &ObjectStep) {
+        self.step.dependOn(&obj.step);
+
+        // TODO make it so we always know where this will be
+        %%self.object_files.append(%%os.path.join(self.builder.allocator, self.builder.out_dir,
+            self.builder.fmt("{}{}", obj.name, obj.target.oFileExt())));
+
+        // TODO should be some kind of isolated directory that only has this header in it
+        %%self.include_dirs.append(self.builder.out_dir);
+    }
+
     pub fn addSourceFile(self: &CExecutable, file: []const u8) {
         %%self.source_files.append(file);
     }
test/build_examples.zig
@@ -6,4 +6,5 @@ pub fn addCases(cases: &tests.BuildExamplesContext) {
     cases.add("example/cat/main.zig");
     cases.add("example/guess_number/main.zig");
     cases.addBuildFile("example/shared_library/build.zig");
+    cases.addBuildFile("example/mix_o_files/build.zig");
 }