Skip to content

Commit

Permalink
zig-build: add msvc target (rebased)
Browse files Browse the repository at this point in the history
zig: add build-lib c++ & tba support
ci: C++ test
ci: msvc enable_werror falsed
zig issue - warn to error: argument unused during compilation: '-nostdinc++'
  • Loading branch information
kassane committed Apr 28, 2024
1 parent 13d9306 commit bfd7e20
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/zig-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
ttriple: [ native, native-native-msvc ]
gc_assertions: [ true ]
large_config: [ false ]
enable_cplusplus: [ true ]
enable_threads: [ false, true ]
disable_handle_fork: [ false ]
enable_rwlock: [ false, true ]
Expand Down Expand Up @@ -102,5 +103,6 @@ jobs:
-Denable_rwlock=${{ matrix.enable_rwlock }}
-Denable_thread_local_alloc=${{ matrix.thread_local_alloc }}
-Denable_threads=${{ matrix.enable_threads }}
-Denable_cplusplus=${{ matrix.enable_cplusplus }}
-Denable_werror
test
93 changes: 89 additions & 4 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ pub fn build(b: *std.Build) void {
const default_enable_threads = !t.isWasm(); // both emscripten and wasi

// Customize build by passing "-D<option_name>[=false]" in command line.
// TODO: support enable_cplusplus
const enable_cplusplus = b.option(bool, "enable_cplusplus",
"C++ support") orelse false;
const build_shared_libs = b.option(bool, "BUILD_SHARED_LIBS",
"Build shared libraries (otherwise static ones)") orelse true;
const build_cord = b.option(bool, "build_cord",
Expand All @@ -68,7 +69,8 @@ pub fn build(b: *std.Build) void {
"Enable threads discovery in GC") orelse true;
const enable_rwlock = b.option(bool, "enable_rwlock",
"Enable reader mode of the allocator lock") orelse false;
// TODO: support enable_throw_bad_alloc_library
const enable_throw_bad_alloc_library = b.option(bool, "enable_throw_bad_alloc_library",
"Turn on C++ gctba library build") orelse true;
const enable_gcj_support = b.option(bool, "enable_gcj_support",
"Support for gcj") orelse true;
const enable_sigrt_signals = b.option(bool, "enable_sigrt_signals",
Expand Down Expand Up @@ -363,8 +365,9 @@ pub fn build(b: *std.Build) void {
"checksums.c",
}) catch unreachable;
}

if (enable_werror) {
// zig msvc target turn unused flags on error
// refer: https://github.com/ziglang/zig/issues/5312
if (enable_werror and t.abi != .msvc) {
flags.appendSlice(&.{
"-Werror",
}) catch unreachable;
Expand Down Expand Up @@ -563,6 +566,19 @@ pub fn build(b: *std.Build) void {
if (build_cord) {
b.installArtifact(cord);
}
const libtga: ?*std.Build.Step.Compile = if(enable_throw_bad_alloc_library)
buildGCTBA(b, gc, flags, build_shared_libs) else null;
const libcpp: ?*std.Build.Step.Compile = if(enable_cplusplus)
buildGCCpp(b, gc, flags, build_shared_libs) else null;

if(libtga)|tga| {
b.installArtifact(tga);
}
if(libcpp)|cpp| {
b.installArtifact(cpp);
installHeader(b, cpp, "gc_cpp.h");
installHeader(b, cpp, "gc/gc_cpp.h");
}

// Note: there is no "build_tests" option, as the tests are built
// only if "test" step is requested.
Expand All @@ -582,6 +598,9 @@ pub fn build(b: *std.Build) void {
if (enable_gc_debug) {
addTest(b, gc, test_step, flags, "tracetest", "tests/trace.c");
}
if(libcpp)|cpp| {
addTest(b, cpp, test_step, flags, "cpptest", "tests/cpp.cc");
}
if (enable_threads) {
addTest(b, gc, test_step, flags, "atomicopstest", "tests/atomicops.c");
addTest(b, gc, test_step, flags,
Expand All @@ -603,6 +622,72 @@ pub fn build(b: *std.Build) void {
}
}

fn buildGCCpp(b: *std.Build, lib: *std.Build.Step.Compile, flags: std.ArrayList([]const u8), build_shared_libs: bool) *std.Build.Step.Compile {
const t = lib.rootModuleTarget();
const gccpp = if(build_shared_libs) b.addSharedLibrary(.{
.name = "gccpp",
.target = lib.root_module.resolved_target.?,
.optimize = lib.root_module.optimize.?,
}) else b.addStaticLibrary(.{
.name = "gccpp",
.target = lib.root_module.resolved_target.?,
.optimize = lib.root_module.optimize.?,
});
gccpp.addIncludePath(b.path("include"));
// Note: for C++ prefer `addCSourceFiles` over `addCSourceFile`, no warnings for c++ flags, like -std=c++11
gccpp.addCSourceFiles(.{
.files = if (t.abi != .msvc)
&.{
"gc_cpp.cc",
"gc_badalc.cc",
}
else
&.{
"gc_cpp.cpp",
"gc_badalc.cpp",
},
.flags = flags.items,
});
gccpp.linkLibrary(lib);
if(t.abi != .msvc)
gccpp.linkLibCpp()
else
gccpp.linkLibC();
return gccpp;
}

fn buildGCTBA(b: *std.Build, lib: *std.Build.Step.Compile, flags: std.ArrayList([]const u8), build_shared_libs: bool) *std.Build.Step.Compile {
const t = lib.rootModuleTarget();
const gctba = if(build_shared_libs) b.addSharedLibrary(.{
.name = "gctba",
.target = lib.root_module.resolved_target.?,
.optimize = lib.root_module.optimize.?,
}) else b.addStaticLibrary(.{
.name = "gctba",
.target = lib.root_module.resolved_target.?,
.optimize = lib.root_module.optimize.?,
});
gctba.addIncludePath(b.path("include"));
gctba.addCSourceFiles(.{
.files = if (t.abi != .msvc)
&.{
"gc_badalc.cc",
}
else
&.{

"gc_badalc.cpp",
},
.flags = flags.items,
});
gctba.linkLibrary(lib);
if(t.abi != .msvc)
gctba.linkLibCpp()
else
gctba.linkLibC();
return gctba;
}

fn addTest(b: *std.Build, gc: *std.Build.Step.Compile,
test_step: *std.Build.Step, flags: std.ArrayList([]const u8),
testname: []const u8, filename: []const u8) void {
Expand Down

0 comments on commit bfd7e20

Please sign in to comment.