authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-25 15:44:38-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-05-25 15:44:38-04:00
log2f538f30fda59d259b2d3c890a7ade6d35085da0
tree378b9fc0f9e54475c02ed520d3d8c9096e10fa17
parent5c251604521868ccd0cb49beef59e5524869281a
parentacaa0b8f085b386fabeb0fb92a3cb5818d1f3061
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #8894 from xxxbxxx/standalone_buildmodes

add standalone test for c compiler with build modes

7 files changed, 154 insertions(+), 26 deletions(-)

build.zig+1-1
...@@ -328,7 +328,7 @@ pub fn build(b: *Builder) !void {...@@ -328,7 +328,7 @@ pub fn build(b: *Builder) !void {
328 ));328 ));
329329
330 toolchain_step.dependOn(tests.addCompareOutputTests(b, test_filter, modes));330 toolchain_step.dependOn(tests.addCompareOutputTests(b, test_filter, modes));
331 toolchain_step.dependOn(tests.addStandaloneTests(b, test_filter, modes));331 toolchain_step.dependOn(tests.addStandaloneTests(b, test_filter, modes, skip_non_native, target));
332 toolchain_step.dependOn(tests.addStackTraceTests(b, test_filter, modes));332 toolchain_step.dependOn(tests.addStackTraceTests(b, test_filter, modes));
333 toolchain_step.dependOn(tests.addCliTests(b, test_filter, modes));333 toolchain_step.dependOn(tests.addCliTests(b, test_filter, modes));
334 toolchain_step.dependOn(tests.addAssembleAndLinkTests(b, test_filter, modes));334 toolchain_step.dependOn(tests.addAssembleAndLinkTests(b, test_filter, modes));
src/libcxx.zig+1
...@@ -129,6 +129,7 @@ pub fn buildLibCXX(comp: *Compilation) !void {...@@ -129,6 +129,7 @@ pub fn buildLibCXX(comp: *Compilation) !void {
129 try cflags.append("-D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS");129 try cflags.append("-D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS");
130 try cflags.append("-D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS");130 try cflags.append("-D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS");
131 try cflags.append("-D_LIBCPP_HAS_NO_VENDOR_AVAILABILITY_ANNOTATIONS");131 try cflags.append("-D_LIBCPP_HAS_NO_VENDOR_AVAILABILITY_ANNOTATIONS");
132 try cflags.append("-D_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS");
132 try cflags.append("-fvisibility=hidden");133 try cflags.append("-fvisibility=hidden");
133 try cflags.append("-fvisibility-inlines-hidden");134 try cflags.append("-fvisibility-inlines-hidden");
134135
test/standalone.zig+19-18
...@@ -8,26 +8,27 @@ pub fn addCases(cases: *tests.StandaloneContext) void {...@@ -8,26 +8,27 @@ pub fn addCases(cases: *tests.StandaloneContext) void {
8 cases.add("test/standalone/guess_number/main.zig");8 cases.add("test/standalone/guess_number/main.zig");
9 cases.add("test/standalone/main_return_error/error_u8.zig");9 cases.add("test/standalone/main_return_error/error_u8.zig");
10 cases.add("test/standalone/main_return_error/error_u8_non_zero.zig");10 cases.add("test/standalone/main_return_error/error_u8_non_zero.zig");
11 cases.addBuildFile("test/standalone/main_pkg_path/build.zig");11 cases.addBuildFile("test/standalone/main_pkg_path/build.zig", .{});
12 cases.addBuildFile("test/standalone/shared_library/build.zig");12 cases.addBuildFile("test/standalone/shared_library/build.zig", .{});
13 cases.addBuildFile("test/standalone/mix_o_files/build.zig");13 cases.addBuildFile("test/standalone/mix_o_files/build.zig", .{});
14 cases.addBuildFile("test/standalone/global_linkage/build.zig");14 cases.addBuildFile("test/standalone/global_linkage/build.zig", .{});
15 cases.addBuildFile("test/standalone/static_c_lib/build.zig");15 cases.addBuildFile("test/standalone/static_c_lib/build.zig", .{});
16 cases.addBuildFile("test/standalone/link_interdependent_static_c_libs/build.zig");16 cases.addBuildFile("test/standalone/link_interdependent_static_c_libs/build.zig", .{});
17 cases.addBuildFile("test/standalone/link_static_lib_as_system_lib/build.zig");17 cases.addBuildFile("test/standalone/link_static_lib_as_system_lib/build.zig", .{});
18 cases.addBuildFile("test/standalone/issue_339/build.zig");18 cases.addBuildFile("test/standalone/issue_339/build.zig", .{});
19 cases.addBuildFile("test/standalone/issue_8550/build.zig");19 cases.addBuildFile("test/standalone/issue_8550/build.zig", .{});
20 cases.addBuildFile("test/standalone/issue_794/build.zig");20 cases.addBuildFile("test/standalone/issue_794/build.zig", .{});
21 cases.addBuildFile("test/standalone/issue_5825/build.zig");21 cases.addBuildFile("test/standalone/issue_5825/build.zig", .{});
22 cases.addBuildFile("test/standalone/pkg_import/build.zig");22 cases.addBuildFile("test/standalone/pkg_import/build.zig", .{});
23 cases.addBuildFile("test/standalone/use_alias/build.zig");23 cases.addBuildFile("test/standalone/use_alias/build.zig", .{});
24 cases.addBuildFile("test/standalone/brace_expansion/build.zig");24 cases.addBuildFile("test/standalone/brace_expansion/build.zig", .{});
25 cases.addBuildFile("test/standalone/empty_env/build.zig");25 cases.addBuildFile("test/standalone/empty_env/build.zig", .{});
26 cases.addBuildFile("test/standalone/issue_7030/build.zig");26 cases.addBuildFile("test/standalone/issue_7030/build.zig", .{});
27 if (std.Target.current.os.tag != .wasi) {27 if (std.Target.current.os.tag != .wasi) {
28 cases.addBuildFile("test/standalone/load_dynamic_library/build.zig");28 cases.addBuildFile("test/standalone/load_dynamic_library/build.zig", .{});
29 }29 }
30 if (std.Target.current.cpu.arch == .x86_64) { // TODO add C ABI support for other architectures30 if (std.Target.current.cpu.arch == .x86_64) { // TODO add C ABI support for other architectures
31 cases.addBuildFile("test/stage1/c_abi/build.zig");31 cases.addBuildFile("test/stage1/c_abi/build.zig", .{});
32 }32 }
33 cases.addBuildFile("test/standalone/c_compiler/build.zig", .{ .build_modes = true, .cross_targets = true });
33}34}
test/standalone/c_compiler/build.zig created+53
...@@ -0,0 +1,53 @@
1const std = @import("std");
2const Builder = std.build.Builder;
3const CrossTarget = std.zig.CrossTarget;
4
5fn isRunnableTarget(t: CrossTarget) bool {
6 if (t.isNative()) return true;
7
8 return (t.getOsTag() == std.Target.current.os.tag and
9 t.getCpuArch() == std.Target.current.cpu.arch);
10}
11
12pub fn build(b: *Builder) void {
13 const mode = b.standardReleaseOptions();
14 const target = b.standardTargetOptions(.{});
15
16 const test_step = b.step("test", "Test the program");
17
18 const exe_c = b.addExecutable("test_c", null);
19 b.default_step.dependOn(&exe_c.step);
20 exe_c.addCSourceFile("test.c", &[0][]const u8{});
21 exe_c.setBuildMode(mode);
22 exe_c.setTarget(target);
23 exe_c.linkLibC();
24
25 const exe_cpp = b.addExecutable("test_cpp", null);
26 b.default_step.dependOn(&exe_cpp.step);
27 exe_cpp.addCSourceFile("test.cpp", &[0][]const u8{});
28 exe_cpp.setBuildMode(mode);
29 exe_cpp.setTarget(target);
30 exe_cpp.linkSystemLibrary("c++");
31
32 // disable broken LTO links:
33 switch(target.getOsTag()) {
34 .windows => {
35 exe_cpp.want_lto = false;
36 },
37 .macos => {
38 exe_cpp.want_lto = false;
39 exe_c.want_lto = false;
40 },
41 else => {},
42 }
43
44 if (isRunnableTarget(target)) {
45 const run_c_cmd = exe_c.run();
46 test_step.dependOn(&run_c_cmd.step);
47 const run_cpp_cmd = exe_cpp.run();
48 test_step.dependOn(&run_cpp_cmd.step);
49 } else {
50 test_step.dependOn(&exe_c.step);
51 test_step.dependOn(&exe_cpp.step);
52 }
53}
test/standalone/c_compiler/test.c created+25
...@@ -0,0 +1,25 @@
1#include <assert.h>
2#include <stdio.h>
3
4typedef struct {
5 int val;
6} STest;
7
8int getVal(STest* data) { return data->val; }
9
10int main (int argc, char *argv[])
11{
12 STest* data = (STest*)malloc(sizeof(STest));
13 data->val = 123;
14
15 assert(getVal(data) != 456);
16 int ok = (getVal(data) == 123);
17
18 if (argc>1) fprintf(stdout, "val=%d\n", data->val);
19
20 free(data);
21
22 if (!ok) abort();
23
24 return 0;
25}
test/standalone/c_compiler/test.cpp created+26
...@@ -0,0 +1,26 @@
1#include <iostream>
2#include <cassert>
3
4class CTest {
5public:
6 CTest(int val) : m_val(val) {};
7 virtual ~CTest() {}
8
9 virtual int getVal() const { return m_val; }
10 virtual void printVal() { std::cout << "val=" << m_val << std::endl; }
11private:
12 int m_val;
13};
14
15int main (int argc, char *argv[])
16{
17 auto* t = new CTest(123);
18 assert(t->getVal()!=456);
19 if (argc>1) t->printVal();
20 bool ok = t->getVal() == 123;
21 delete t;
22
23 if (!ok) abort();
24
25 return 0;
26}
test/tests.zig+29-7
...@@ -399,7 +399,7 @@ pub fn addCompileErrorTests(b: *build.Builder, test_filter: ?[]const u8, modes:...@@ -399,7 +399,7 @@ pub fn addCompileErrorTests(b: *build.Builder, test_filter: ?[]const u8, modes:
399 return cases.step;399 return cases.step;
400}400}
401401
402pub fn addStandaloneTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const Mode) *build.Step {402pub fn addStandaloneTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const Mode, skip_non_native: bool, target: std.zig.CrossTarget) *build.Step {
403 const cases = b.allocator.create(StandaloneContext) catch unreachable;403 const cases = b.allocator.create(StandaloneContext) catch unreachable;
404 cases.* = StandaloneContext{404 cases.* = StandaloneContext{
405 .b = b,405 .b = b,
...@@ -407,6 +407,8 @@ pub fn addStandaloneTests(b: *build.Builder, test_filter: ?[]const u8, modes: []...@@ -407,6 +407,8 @@ pub fn addStandaloneTests(b: *build.Builder, test_filter: ?[]const u8, modes: []
407 .test_index = 0,407 .test_index = 0,
408 .test_filter = test_filter,408 .test_filter = test_filter,
409 .modes = modes,409 .modes = modes,
410 .skip_non_native = skip_non_native,
411 .target = target,
410 };412 };
411413
412 standalone.addCases(cases);414 standalone.addCases(cases);
...@@ -1136,6 +1138,8 @@ pub const StandaloneContext = struct {...@@ -1136,6 +1138,8 @@ pub const StandaloneContext = struct {
1136 test_index: usize,1138 test_index: usize,
1137 test_filter: ?[]const u8,1139 test_filter: ?[]const u8,
1138 modes: []const Mode,1140 modes: []const Mode,
1141 skip_non_native: bool,
1142 target: std.zig.CrossTarget,
11391143
1140 pub fn addC(self: *StandaloneContext, root_src: []const u8) void {1144 pub fn addC(self: *StandaloneContext, root_src: []const u8) void {
1141 self.addAllArgs(root_src, true);1145 self.addAllArgs(root_src, true);
...@@ -1145,10 +1149,10 @@ pub const StandaloneContext = struct {...@@ -1145,10 +1149,10 @@ pub const StandaloneContext = struct {
1145 self.addAllArgs(root_src, false);1149 self.addAllArgs(root_src, false);
1146 }1150 }
11471151
1148 pub fn addBuildFile(self: *StandaloneContext, build_file: []const u8) void {1152 pub fn addBuildFile(self: *StandaloneContext, build_file: []const u8, features: struct { build_modes: bool = false, cross_targets: bool = false }) void {
1149 const b = self.b;1153 const b = self.b;
11501154
1151 const annotated_case_name = b.fmt("build {s} (Debug)", .{build_file});1155 const annotated_case_name = b.fmt("build {s}", .{build_file});
1152 if (self.test_filter) |filter| {1156 if (self.test_filter) |filter| {
1153 if (mem.indexOf(u8, annotated_case_name, filter) == null) return;1157 if (mem.indexOf(u8, annotated_case_name, filter) == null) return;
1154 }1158 }
...@@ -1167,12 +1171,30 @@ pub const StandaloneContext = struct {...@@ -1167,12 +1171,30 @@ pub const StandaloneContext = struct {
1167 zig_args.append("--verbose") catch unreachable;1171 zig_args.append("--verbose") catch unreachable;
1168 }1172 }
11691173
1170 const run_cmd = b.addSystemCommand(zig_args.items);1174 if (features.cross_targets and !self.target.isNative()) {
1175 const target_arg = fmt.allocPrint(b.allocator, "-Dtarget={s}", .{self.target.zigTriple(b.allocator) catch unreachable}) catch unreachable;
1176 zig_args.append(target_arg) catch unreachable;
1177 }
1178
1179 const modes = if (features.build_modes) self.modes else &[1]Mode{.Debug};
1180 for (modes) |mode| {
1181 const arg = switch (mode) {
1182 .Debug => "",
1183 .ReleaseFast => "-Drelease-fast",
1184 .ReleaseSafe => "-Drelease-safe",
1185 .ReleaseSmall => "-Drelease-small",
1186 };
1187 const zig_args_base_len = zig_args.items.len;
1188 if (arg.len > 0)
1189 zig_args.append(arg) catch unreachable;
1190 defer zig_args.resize(zig_args_base_len) catch unreachable;
11711191
1172 const log_step = b.addLog("PASS {s}\n", .{annotated_case_name});1192 const run_cmd = b.addSystemCommand(zig_args.items);
1173 log_step.step.dependOn(&run_cmd.step);1193 const log_step = b.addLog("PASS {s} ({s})\n", .{ annotated_case_name, @tagName(mode) });
1194 log_step.step.dependOn(&run_cmd.step);
11741195
1175 self.step.dependOn(&log_step.step);1196 self.step.dependOn(&log_step.step);
1197 }
1176 }1198 }
11771199
1178 pub fn addAllArgs(self: *StandaloneContext, root_src: []const u8, link_libc: bool) void {1200 pub fn addAllArgs(self: *StandaloneContext, root_src: []const u8, link_libc: bool) void {