From 2cd589015706574367baa1e6bed7aaf5cbb2226f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20C=2E=20Fran=C3=A7a?= Date: Fri, 19 Apr 2024 10:18:49 -0300 Subject: [PATCH] zig-build: add msvc target (rebased) 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++' --- .github/workflows/zig-build.yml | 23 ++++++++- build.zig | 88 ++++++++++++++++++++++++++++++++- 2 files changed, 108 insertions(+), 3 deletions(-) diff --git a/.github/workflows/zig-build.yml b/.github/workflows/zig-build.yml index ef93b3acf..6b3867b60 100644 --- a/.github/workflows/zig-build.yml +++ b/.github/workflows/zig-build.yml @@ -14,9 +14,10 @@ jobs: matrix: zig_version: [ "0.12.0" ] - os: [ macos-latest, ubuntu-latest ] + os: [ macos-latest, ubuntu-latest, windows-latest ] gc_assertions: [ true ] large_config: [ false ] + enable_cplusplus: [ true ] enable_threads: [ false, true ] disable_handle_fork: [ false ] enable_rwlock: [ false, true ] @@ -95,5 +96,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 diff --git a/build.zig b/build.zig index c99a1e450..67a50cefa 100644 --- a/build.zig +++ b/build.zig @@ -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[=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", @@ -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", @@ -547,6 +549,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. @@ -566,6 +581,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, @@ -587,6 +605,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 {