Skip to content

Commit

Permalink
Do not define pthread-related macros for zig windows-gnu build
Browse files Browse the repository at this point in the history
(fix of commit 2666e3e)

PR #598 (bdwgc).

Zig windows-gnu target is MinGW-based build.  In this case pthreads
library is not used by bdwgc.

* build.zig [enable_threads] (build): Move GC_THREADS and PARALLEL_MARK
macros definition upper; add TODO item about Cygwin; add gc_dlopen.c,
specific.c to source_files only if t.os.tag!=.windows; define
_REENTRANT, HANDLE_FORK, GC_USESIGRT_SIGNALS macros only if
t.os.tag!=.windows; do not add threadkeytest to tests if
t.os.tag==.windows.
* build.zig (build): Set have_getcontext to false if
t.os.tag==.windows.
* build.zig (build): Do not define HAVE_DLADDR macro if
t.os.tag==.windows.
* build.zig [install_headers && enable_threads] (build): Do not install
gc_pthread_redirects.h if t.os.tag!=.windows.
  • Loading branch information
ivmai committed Apr 26, 2024
1 parent b97d425 commit 2ebe563
Showing 1 changed file with 62 additions and 45 deletions.
107 changes: 62 additions & 45 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -176,51 +176,64 @@ pub fn build(b: *std.Build) void {
"typd_mlc.c",
}) catch unreachable;

if (enable_threads) { // assume pthreads
// Zig comes with clang which supports GCC atomic intrinsics.
flags.append("-D GC_BUILTIN_ATOMIC") catch unreachable;
// TODO: define and use THREADDLLIBS_LIST

source_files.appendSlice(&.{
"gc_dlopen.c",
"pthread_start.c",
"pthread_support.c",
}) catch unreachable;
if (t.os.tag == .windows) {
source_files.appendSlice(&.{
"win32_threads.c",
}) catch unreachable;
} else if (t.isDarwin()) {
source_files.appendSlice(&.{
"darwin_stop_world.c",
}) catch unreachable;
} else {
source_files.appendSlice(&.{
"pthread_stop_world.c",
}) catch unreachable;
}
// Common defines for POSIX platforms.
if (enable_threads) {
flags.append("-D GC_THREADS") catch unreachable;
flags.append("-D _REENTRANT") catch unreachable;
// TODO: some targets might need _PTHREADS defined too.
if (enable_parallel_mark) {
flags.append("-D PARALLEL_MARK") catch unreachable;
}
if (enable_thread_local_alloc) {
flags.append("-D THREAD_LOCAL_ALLOC") catch unreachable;
if (t.os.tag != .windows) { // assume pthreads
// TODO: support cygwin when supported by zig
// Zig comes with clang which supports GCC atomic intrinsics.
flags.append("-D GC_BUILTIN_ATOMIC") catch unreachable;
// TODO: define and use THREADDLLIBS_LIST
source_files.appendSlice(&.{
"specific.c",
"thread_local_alloc.c",
"gc_dlopen.c",
"pthread_start.c",
"pthread_support.c",
}) catch unreachable;
if (t.isDarwin()) {
source_files.appendSlice(&.{
"darwin_stop_world.c",
}) catch unreachable;
} else {
source_files.appendSlice(&.{
"pthread_stop_world.c",
}) catch unreachable;
}
// Common defines for POSIX platforms.
flags.append("-D _REENTRANT") catch unreachable;
// TODO: some targets might need _PTHREADS defined too.
if (enable_thread_local_alloc) {
flags.append("-D THREAD_LOCAL_ALLOC") catch unreachable;
source_files.appendSlice(&.{
"specific.c",
"thread_local_alloc.c",
}) catch unreachable;
}
// Message for clients: Explicit GC_INIT() calls may be required.
if (enable_handle_fork and !disable_handle_fork) {
flags.append("-D HANDLE_FORK") catch unreachable;
}
if (enable_sigrt_signals) {
flags.append("-D GC_USESIGRT_SIGNALS") catch unreachable;
}
} else {
// Assume the GCC atomic intrinsics are supported.
flags.append("-D GC_BUILTIN_ATOMIC") catch unreachable;
if (enable_thread_local_alloc
and (enable_parallel_mark or !build_shared_libs)) {
// Imply THREAD_LOCAL_ALLOC unless GC_DLL.
flags.append("-D THREAD_LOCAL_ALLOC") catch unreachable;
source_files.appendSlice(&.{
"thread_local_alloc.c",
}) catch unreachable;
}
flags.append("-D EMPTY_GETENV_RESULTS") catch unreachable;
source_files.appendSlice(&.{
"pthread_start.c", // just if client defines GC_WIN32_PTHREADS
"pthread_support.c",
"win32_threads.c",
}) catch unreachable;
}
// Message for clients: Explicit GC_INIT() calls may be required.
if (t.os.tag == .windows) {
// Does not provide process fork functionality.
} else if (enable_handle_fork and !disable_handle_fork) {
flags.append("-D HANDLE_FORK") catch unreachable;
}
if (enable_sigrt_signals) {
flags.append("-D GC_USESIGRT_SIGNALS") catch unreachable;
}
}

Expand Down Expand Up @@ -396,7 +409,7 @@ pub fn build(b: *std.Build) void {
flags.append("-D HAVE_SYS_TYPES_H") catch unreachable;
flags.append("-D HAVE_UNISTD_H") catch unreachable;

const have_getcontext = !t.abi.isMusl();
const have_getcontext = !t.abi.isMusl() and t.os.tag != .windows;
if (!have_getcontext) {
flags.append("-D NO_GETCONTEXT") catch unreachable;
}
Expand Down Expand Up @@ -424,8 +437,10 @@ pub fn build(b: *std.Build) void {
// and HAVE_PTHREAD_SET_NAME_NP targets.
}

// Define to use 'dladdr' function (used for debugging).
flags.append("-D HAVE_DLADDR") catch unreachable;
if (t.os.tag != .windows) {
// Define to use 'dladdr' function (used for debugging).
flags.append("-D HAVE_DLADDR") catch unreachable;
}

// Extra user-defined flags (if any) to pass to the compiler.
if (cflags_extra.len > 0) {
Expand Down Expand Up @@ -460,7 +475,7 @@ pub fn build(b: *std.Build) void {
if (enable_gcj_support) {
installHeader(b, lib, "gc/gc_gcj.h");
}
if (enable_threads) {
if (enable_threads and t.os.tag != .windows) {
installHeader(b, lib, "gc/gc_pthread_redirects.h");
}
// TODO: compose and install bdw-gc.pc and pkgconfig.
Expand Down Expand Up @@ -490,8 +505,10 @@ pub fn build(b: *std.Build) void {
"subthreadcreatetest", "tests/subthreadcreate.c");
addTest(b, lib, test_step, flags,
"threadleaktest", "tests/threadleak.c");
addTest(b, lib, test_step, flags,
"threadkeytest", "tests/threadkey.c");
if (t.os.tag != .windows) {
addTest(b, lib, test_step, flags,
"threadkeytest", "tests/threadkey.c");
}
}
if (enable_disclaim) {
addTest(b, lib, test_step, flags,
Expand Down

0 comments on commit 2ebe563

Please sign in to comment.