authorgravatar for xavierb@gmail.comxavier <xavierb@gmail.com> 2021-05-25 00:11:00+02:00
committergravatar for xavierb@gmail.comxavier <xavierb@gmail.com> 2021-05-25 00:42:31+02:00
log32154fbf0d14527f9f144bd5979a25d77e782ccf
tree09c8f4c00c9b0721db87c96d1d6bbaf0d7730d09
parent7e4f28fac9a20fb548c85cf81f4a62b069cf4ac8

add a standalone for zig as a c/c++ compiler


4 files changed, 105 insertions(+), 0 deletions(-)

test/standalone.zig+1
...@@ -30,4 +30,5 @@ pub fn addCases(cases: *tests.StandaloneContext) void {...@@ -30,4 +30,5 @@ pub fn addCases(cases: *tests.StandaloneContext) void {
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}