From e3b275fa47e44d0a77e513d03a3b37b0f9ea0c0d Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 3 Mar 2019 12:35:09 -0500 Subject: [PATCH] fix build.zig not respecting --static closes #2027 --- std/build.zig | 2 +- test/build_examples.zig | 1 + test/standalone/static_c_lib/build.zig | 18 ++++++++++++++++++ test/standalone/static_c_lib/foo.c | 4 ++++ test/standalone/static_c_lib/foo.h | 2 ++ test/standalone/static_c_lib/foo.zig | 8 ++++++++ 6 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 test/standalone/static_c_lib/build.zig create mode 100644 test/standalone/static_c_lib/foo.c create mode 100644 test/standalone/static_c_lib/foo.h create mode 100644 test/standalone/static_c_lib/foo.zig diff --git a/std/build.zig b/std/build.zig index f2e53cce10..2316a87b31 100644 --- a/std/build.zig +++ b/std/build.zig @@ -1326,7 +1326,7 @@ pub const LibExeObjStep = struct { zig_args.append("--ver-patch") catch unreachable; zig_args.append(builder.fmt("{}", self.version.patch)) catch unreachable; } - if ((self.kind == Kind.Exe or self.kind == Kind.Test) and self.static) { + if (self.static) { zig_args.append("--static") catch unreachable; } diff --git a/test/build_examples.zig b/test/build_examples.zig index 3931648f19..c51fdfdf89 100644 --- a/test/build_examples.zig +++ b/test/build_examples.zig @@ -10,6 +10,7 @@ pub fn addCases(cases: *tests.BuildExamplesContext) void { cases.addBuildFile("test/standalone/main_pkg_path/build.zig"); cases.addBuildFile("example/shared_library/build.zig"); cases.addBuildFile("example/mix_o_files/build.zig"); + cases.addBuildFile("test/standalone/static_c_lib/build.zig"); if (builtin.os != builtin.Os.macosx) { // TODO https://github.com/ziglang/zig/issues/1126 cases.addBuildFile("test/standalone/issue_339/build.zig"); diff --git a/test/standalone/static_c_lib/build.zig b/test/standalone/static_c_lib/build.zig new file mode 100644 index 0000000000..afbb63c6bf --- /dev/null +++ b/test/standalone/static_c_lib/build.zig @@ -0,0 +1,18 @@ +const Builder = @import("std").build.Builder; + +pub fn build(b: *Builder) void { + const mode = b.standardReleaseOptions(); + + const foo = b.addStaticLibrary("foo", null); + foo.addCSourceFile("foo.c", [][]const u8{}); + foo.setBuildMode(mode); + foo.addIncludeDir("."); + + const test_exe = b.addTest("foo.zig"); + test_exe.setBuildMode(mode); + test_exe.linkLibrary(foo); + test_exe.addIncludeDir("."); + + const test_step = b.step("test", "Test it"); + test_step.dependOn(&test_exe.step); +} diff --git a/test/standalone/static_c_lib/foo.c b/test/standalone/static_c_lib/foo.c new file mode 100644 index 0000000000..2366282d47 --- /dev/null +++ b/test/standalone/static_c_lib/foo.c @@ -0,0 +1,4 @@ +#include "foo.h" +uint32_t add(uint32_t a, uint32_t b) { + return a + b; +} diff --git a/test/standalone/static_c_lib/foo.h b/test/standalone/static_c_lib/foo.h new file mode 100644 index 0000000000..e8ebb9842e --- /dev/null +++ b/test/standalone/static_c_lib/foo.h @@ -0,0 +1,2 @@ +#include +uint32_t add(uint32_t a, uint32_t b); diff --git a/test/standalone/static_c_lib/foo.zig b/test/standalone/static_c_lib/foo.zig new file mode 100644 index 0000000000..edf94539c8 --- /dev/null +++ b/test/standalone/static_c_lib/foo.zig @@ -0,0 +1,8 @@ +const std = @import("std"); +const expect = std.testing.expect; +const c = @cImport(@cInclude("foo.h")); + +test "C add" { + const result = c.add(1, 2); + expect(result == 3); +}