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 27, 2024
1 parent 13d9306 commit ca9a05c
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 7 deletions.
31 changes: 26 additions & 5 deletions .github/workflows/zig-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on: [ push, pull_request ]

jobs:
build:
name: ${{ matrix.os }} ${{ matrix.ttriple }} thr:${{ matrix.enable_threads }} rwlock:${{ matrix.enable_rwlock }} redir:${{ matrix.redirect_malloc }} gcdeb:${{ matrix.enable_gc_debug }} munmap:${{ matrix.enable_munmap }} paramark:${{ matrix.parallel_mark }} thrlocal:${{ matrix.thread_local_alloc }} dll:${{ matrix.shared_libs }}
name: ${{ matrix.os }} ${{ matrix.triple }} thr:${{ matrix.enable_threads }} rwlock:${{ matrix.enable_rwlock }} redir:${{ matrix.redirect_malloc }} gcdeb:${{ matrix.enable_gc_debug }} munmap:${{ matrix.enable_munmap }} paramark:${{ matrix.parallel_mark }} thrlocal:${{ matrix.thread_local_alloc }} dll:${{ matrix.shared_libs }}
runs-on: ${{ matrix.os }}
timeout-minutes: 7

Expand All @@ -15,9 +15,10 @@ jobs:
matrix:
zig_version: [ "0.12.0" ]
os: [ macos-latest, ubuntu-latest, windows-latest ]
ttriple: [ native, native-native-msvc ]
triple: [ 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 All @@ -29,9 +30,9 @@ jobs:
shared_libs: [ false, true ]
exclude:
- os: macos-latest
ttriple: native-native-msvc
triple: native-native-msvc
- os: ubuntu-latest
ttriple: native-native-msvc
triple: native-native-msvc
- os: windows-latest
disable_handle_fork: true
- enable_threads: false
Expand Down Expand Up @@ -90,7 +91,7 @@ jobs:
zig-version: ${{ matrix.zig_version }}
- name: Build
run: >
zig build -Dtarget=${{ matrix.ttriple }}
zig build -Dtarget=${{ matrix.triple }}
-DBUILD_SHARED_LIBS=${{ matrix.shared_libs }}
-Ddisable_handle_fork=${{ matrix.disable_handle_fork }}
-Denable_gc_assertions=${{ matrix.gc_assertions }}
Expand All @@ -102,5 +103,25 @@ 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
- name: "MSVC target"
if: runner.os == 'Windows'
run: >
zig build -Dtarget=native-native-msvc
-DBUILD_SHARED_LIBS=${{ matrix.shared_libs }}
-Ddisable_handle_fork=${{ matrix.disable_handle_fork }}
-Denable_gc_assertions=${{ matrix.gc_assertions }}
-Denable_gc_debug=${{ matrix.enable_gc_debug }}
-Denable_large_config=${{ matrix.large_config }}
-Denable_munmap=${{ matrix.enable_munmap }}
-Denable_parallel_mark=${{ matrix.parallel_mark }}
-Denable_redirect_malloc=${{ matrix.redirect_malloc }}
-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=false
test
88 changes: 86 additions & 2 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 @@ -563,6 +565,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 +597,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 +621,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 ca9a05c

Please sign in to comment.