mirror of
https://github.com/ziglang/zig.git
synced 2025-12-08 07:13:08 +00:00
add a standalone for zig as a c/c++ compiler
This commit is contained in:
parent
7e4f28fac9
commit
32154fbf0d
@ -30,4 +30,5 @@ pub fn addCases(cases: *tests.StandaloneContext) void {
|
|||||||
if (std.Target.current.cpu.arch == .x86_64) { // TODO add C ABI support for other architectures
|
if (std.Target.current.cpu.arch == .x86_64) { // TODO add C ABI support for other architectures
|
||||||
cases.addBuildFile("test/stage1/c_abi/build.zig", .{});
|
cases.addBuildFile("test/stage1/c_abi/build.zig", .{});
|
||||||
}
|
}
|
||||||
|
cases.addBuildFile("test/standalone/c_compiler/build.zig", .{ .build_modes = true, .cross_targets = true });
|
||||||
}
|
}
|
||||||
|
|||||||
53
test/standalone/c_compiler/build.zig
Normal file
53
test/standalone/c_compiler/build.zig
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
const Builder = std.build.Builder;
|
||||||
|
const CrossTarget = std.zig.CrossTarget;
|
||||||
|
|
||||||
|
fn isRunnableTarget(t: CrossTarget) bool {
|
||||||
|
if (t.isNative()) return true;
|
||||||
|
|
||||||
|
return (t.getOsTag() == std.Target.current.os.tag and
|
||||||
|
t.getCpuArch() == std.Target.current.cpu.arch);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn build(b: *Builder) void {
|
||||||
|
const mode = b.standardReleaseOptions();
|
||||||
|
const target = b.standardTargetOptions(.{});
|
||||||
|
|
||||||
|
const test_step = b.step("test", "Test the program");
|
||||||
|
|
||||||
|
const exe_c = b.addExecutable("test_c", null);
|
||||||
|
b.default_step.dependOn(&exe_c.step);
|
||||||
|
exe_c.addCSourceFile("test.c", &[0][]const u8{});
|
||||||
|
exe_c.setBuildMode(mode);
|
||||||
|
exe_c.setTarget(target);
|
||||||
|
exe_c.linkLibC();
|
||||||
|
|
||||||
|
const exe_cpp = b.addExecutable("test_cpp", null);
|
||||||
|
b.default_step.dependOn(&exe_cpp.step);
|
||||||
|
exe_cpp.addCSourceFile("test.cpp", &[0][]const u8{});
|
||||||
|
exe_cpp.setBuildMode(mode);
|
||||||
|
exe_cpp.setTarget(target);
|
||||||
|
exe_cpp.linkSystemLibrary("c++");
|
||||||
|
|
||||||
|
// disable broken LTO links:
|
||||||
|
switch(target.getOsTag()) {
|
||||||
|
.windows => {
|
||||||
|
exe_cpp.want_lto = false;
|
||||||
|
},
|
||||||
|
.macos => {
|
||||||
|
exe_cpp.want_lto = false;
|
||||||
|
exe_c.want_lto = false;
|
||||||
|
},
|
||||||
|
else => {},
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isRunnableTarget(target)) {
|
||||||
|
const run_c_cmd = exe_c.run();
|
||||||
|
test_step.dependOn(&run_c_cmd.step);
|
||||||
|
const run_cpp_cmd = exe_cpp.run();
|
||||||
|
test_step.dependOn(&run_cpp_cmd.step);
|
||||||
|
} else {
|
||||||
|
test_step.dependOn(&exe_c.step);
|
||||||
|
test_step.dependOn(&exe_cpp.step);
|
||||||
|
}
|
||||||
|
}
|
||||||
25
test/standalone/c_compiler/test.c
Normal file
25
test/standalone/c_compiler/test.c
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#include <assert.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int val;
|
||||||
|
} STest;
|
||||||
|
|
||||||
|
int getVal(STest* data) { return data->val; }
|
||||||
|
|
||||||
|
int main (int argc, char *argv[])
|
||||||
|
{
|
||||||
|
STest* data = (STest*)malloc(sizeof(STest));
|
||||||
|
data->val = 123;
|
||||||
|
|
||||||
|
assert(getVal(data) != 456);
|
||||||
|
int ok = (getVal(data) == 123);
|
||||||
|
|
||||||
|
if (argc>1) fprintf(stdout, "val=%d\n", data->val);
|
||||||
|
|
||||||
|
free(data);
|
||||||
|
|
||||||
|
if (!ok) abort();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
26
test/standalone/c_compiler/test.cpp
Normal file
26
test/standalone/c_compiler/test.cpp
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
|
class CTest {
|
||||||
|
public:
|
||||||
|
CTest(int val) : m_val(val) {};
|
||||||
|
virtual ~CTest() {}
|
||||||
|
|
||||||
|
virtual int getVal() const { return m_val; }
|
||||||
|
virtual void printVal() { std::cout << "val=" << m_val << std::endl; }
|
||||||
|
private:
|
||||||
|
int m_val;
|
||||||
|
};
|
||||||
|
|
||||||
|
int main (int argc, char *argv[])
|
||||||
|
{
|
||||||
|
auto* t = new CTest(123);
|
||||||
|
assert(t->getVal()!=456);
|
||||||
|
if (argc>1) t->printVal();
|
||||||
|
bool ok = t->getVal() == 123;
|
||||||
|
delete t;
|
||||||
|
|
||||||
|
if (!ok) abort();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user