| author | |
| committer | |
| log | ecb71d1dd34e98fc9813cffa473f83127e6e3d01 |
| tree | dba3e086b6655bf75a9efae2e67cfc5aa5c629cd |
| parent | fb492d19ebb56466f04a2a88c7d3c0a9833f2e0d |
closes #3295 files changed, 56 insertions(+), 1 deletions(-)
example/mix_o_files/base64.zig created+7| ... | @@ -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 created+20| ... | @@ -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 created+16| ... | @@ -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+12-1| ... | @@ -128,7 +128,7 @@ pub const Builder = struct { | ... | @@ -128,7 +128,7 @@ pub const Builder = struct { |
| 128 | 128 | ||
| 129 | pub fn addObject(self: &Builder, name: []const u8, root_src: []const u8) -> &ObjectStep { | 129 | pub fn addObject(self: &Builder, name: []const u8, root_src: []const u8) -> &ObjectStep { |
| 130 | const obj_step = %%self.allocator.create(ObjectStep); | 130 | const obj_step = %%self.allocator.create(ObjectStep); |
| 131 | *obj_step = ObjectStep.init(self, name, src); | 131 | *obj_step = ObjectStep.init(self, name, root_src); |
| 132 | return obj_step; | 132 | return obj_step; |
| 133 | } | 133 | } |
| 134 | 134 | ||
| ... | @@ -1567,6 +1567,17 @@ pub const CExecutable = struct { | ... | @@ -1567,6 +1567,17 @@ pub const CExecutable = struct { |
| 1567 | %%self.include_dirs.append(self.builder.out_dir); | 1567 | %%self.include_dirs.append(self.builder.out_dir); |
| 1568 | } | 1568 | } |
| 1569 | 1569 | ||
| 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 | |||
| 1570 | pub fn addSourceFile(self: &CExecutable, file: []const u8) { | 1581 | pub fn addSourceFile(self: &CExecutable, file: []const u8) { |
| 1571 | %%self.source_files.append(file); | 1582 | %%self.source_files.append(file); |
| 1572 | } | 1583 | } |
test/build_examples.zig+1| ... | @@ -6,4 +6,5 @@ pub fn addCases(cases: &tests.BuildExamplesContext) { | ... | @@ -6,4 +6,5 @@ pub fn addCases(cases: &tests.BuildExamplesContext) { |
| 6 | cases.add("example/cat/main.zig"); | 6 | cases.add("example/cat/main.zig"); |
| 7 | cases.add("example/guess_number/main.zig"); | 7 | cases.add("example/guess_number/main.zig"); |
| 8 | cases.addBuildFile("example/shared_library/build.zig"); | 8 | cases.addBuildFile("example/shared_library/build.zig"); |
| 9 | cases.addBuildFile("example/mix_o_files/build.zig"); | ||
| 9 | } | 10 | } |