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 22, 2024
1 parent df1a787 commit 57b113a
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 15 deletions.
39 changes: 27 additions & 12 deletions .github/workflows/zig-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ jobs:
fail-fast: false

matrix:
# TODO: move from nightly to zig 0.12 final when released.
zig_version: [ "0.12.0-dev.3666+a2b834e8c" ]
os: [ macos-latest, ubuntu-latest ]
zig_version: [ 0.12.0 ]
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 ]
Expand Down Expand Up @@ -79,17 +79,12 @@ jobs:

steps:
- uses: actions/checkout@v4
- name: "Install zig on Linux/x86_64"
if: runner.os == 'Linux'
run: |
mkdir zig && curl https://ziglang.org/builds/zig-linux-x86_64-${{matrix.zig_version}}.tar.xz | tar Jx --directory=zig --strip-components=1
- name: "Install zig on MacOS/x86_64"
if: runner.os == 'macOS'
run: |
mkdir zig && curl https://ziglang.org/builds/zig-macos-x86_64-${{matrix.zig_version}}.tar.xz | tar Jx --directory=zig --strip-components=1
- uses: korandoru/setup-zig@v1
with:
zig-version: ${{ matrix.zig_version }}
- name: Build
run: >
zig/zig build
zig build
-DBUILD_SHARED_LIBS=${{ matrix.shared_libs }}
-Ddisable_handle_fork=${{ matrix.disable_handle_fork }}
-Denable_gc_assertions=${{ matrix.gc_assertions }}
Expand All @@ -101,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
92 changes: 89 additions & 3 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,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;
// TODO: support build_cord
Expand All @@ -66,7 +67,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 @@ -395,7 +397,13 @@ pub fn build(b: *std.Build) void {
// -U GC_MISSING_EXECINFO_H
// -U GC_NO_SIGSETJMP
flags.append("-D HAVE_SYS_TYPES_H") catch unreachable;
flags.append("-D HAVE_UNISTD_H") catch unreachable;
if (t.abi != .msvc)
flags.append("-D HAVE_UNISTD_H") catch unreachable
else {
flags.append("-D _CRT_SECURE_NO_WARNINGS") catch unreachable;
flags.append("-D NO_SEH_AVAILABLE") catch unreachable;
flags.append("-D DONT_USE_USER32_DLL") catch unreachable;
}

const have_getcontext = !t.abi.isMusl();
if (!have_getcontext) {
Expand Down Expand Up @@ -467,6 +475,15 @@ pub fn build(b: *std.Build) void {
}

b.installArtifact(lib);
const libtga: ?*std.Build.Step.Compile = if(enable_throw_bad_alloc_library)
buildGCTBA(b, lib, flags, build_shared_libs) else null;
const libcpp: ?*std.Build.Step.Compile = if(enable_cplusplus)
buildGCCpp(b, lib, flags, build_shared_libs) else null;

if(libtga)|tga|
b.installArtifact(tga);
if(libcpp)|cpp|
b.installArtifact(cpp);

// Note: there is no "build_tests" option, as the tests are built
// only if "test" step is requested.
Expand All @@ -481,6 +498,9 @@ pub fn build(b: *std.Build) void {
if (enable_gc_debug) {
addTest(b, lib, 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, lib, test_step, flags,
"atomicopstest", "tests/atomicops.c");
Expand All @@ -503,6 +523,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, lib: *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 57b113a

Please sign in to comment.