summaryrefslogtreecommitdiff
path: root/CMakeLists.txt
diff options
context:
space:
mode:
authorBruce Mitchener <bruce.mitchener@gmail.com>2023-03-05 18:32:53 +0700
committerIvan Maidanski <ivmai@mail.ru>2023-03-10 12:37:32 +0300
commitf21eefafc047240a4d971de5147bf64dd46725fe (patch)
tree164aa43d63854fec7f820efd0916652097bf30cc /CMakeLists.txt
parentcf0689ec6ec2e5e9b975a2c3626fef42006b7d62 (diff)
downloadbdwgc-f21eefafc047240a4d971de5147bf64dd46725fe.tar.gz
Change enable_threads default to off if threads are unsupported (CMake)
PR #535 (bdwgc). In case of an absent multi-threading support for the target platform, like for Emscripten or WASI, cmake build should not fail by default (i.e. threads support should not be on by default). This matches the relevant behavior of configure one. * CMakeLists.txt (default_enable_threads): New variable (ON by default). * CMakeLists.txt: Call find_package(Threads) quietly regardless of enable_threads. * CMakeLists.txt [EMSCRIPTEN || WASI || !(CMAKE_USE_PTHREADS_INIT || CMAKE_USE_WIN32_THREADS_INIT)] (default_enable_threads): Set to OFF. * CMakeLists.txt (enable_threads): Specify the default value to default_enable_threads. * CMakeLists.txt: Check CMAKE_USE_PTHREADS_INIT and CMAKE_USE_WIN32_THREADS_INIT only if enable_threads. Co-authored-by: Ivan Maidanski <ivmai@mail.ru>
Diffstat (limited to 'CMakeLists.txt')
-rw-r--r--CMakeLists.txt135
1 files changed, 70 insertions, 65 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index e7587f74..26ef527d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -59,12 +59,18 @@ if (NOT (${CMAKE_VERSION} VERSION_LESS "3.18.0"))
include(CheckLinkerFlag)
endif()
+set(default_enable_threads ON)
+find_package(Threads QUIET)
+if (NOT (CMAKE_USE_PTHREADS_INIT OR CMAKE_USE_WIN32_THREADS_INIT) OR EMSCRIPTEN OR WASI)
+ set(default_enable_threads OFF)
+endif()
+
# Customize the build by passing "-D<option_name>=ON|OFF" in the command line.
option(BUILD_SHARED_LIBS "Build shared libraries" ON)
option(build_cord "Build cord library" ON)
option(build_tests "Build tests" OFF)
option(enable_docs "Build and install documentation" ON)
-option(enable_threads "Support threads" ON)
+option(enable_threads "Support threads" ${default_enable_threads})
option(enable_parallel_mark "Parallelize marking and free list construction" ON)
option(enable_thread_local_alloc "Turn on thread-local allocation optimization" ON)
option(enable_threads_discovery "Enable threads discovery in GC" ON)
@@ -213,71 +219,70 @@ if (enable_threads)
else()
set(THREADDLLIBS_LIST ${THREADDLLIBS_LIST} ${CMAKE_DL_LIBS})
endif()
-endif(enable_threads)
-
-# Thread support detection.
-if (CMAKE_USE_PTHREADS_INIT)
- set(SRC ${SRC} gc_dlopen.c pthread_start.c pthread_support.c)
- if (CYGWIN OR MSYS)
- set(SRC ${SRC} win32_threads.c)
- else()
- if (APPLE)
- set(SRC ${SRC} darwin_stop_world.c)
+ # Thread support detection.
+ if (CMAKE_USE_PTHREADS_INIT)
+ set(SRC ${SRC} gc_dlopen.c pthread_start.c pthread_support.c)
+ if (CYGWIN OR MSYS)
+ set(SRC ${SRC} win32_threads.c)
else()
- set(SRC ${SRC} pthread_stop_world.c)
+ if (APPLE)
+ set(SRC ${SRC} darwin_stop_world.c)
+ else()
+ set(SRC ${SRC} pthread_stop_world.c)
+ endif()
endif()
+ if (HOST MATCHES .*-.*-hpux10.*)
+ message(FATAL_ERROR "HP/UX 10 POSIX threads are not supported.")
+ endif()
+ # Common defines for POSIX platforms.
+ add_definitions("-DGC_THREADS -D_REENTRANT")
+ if (enable_parallel_mark)
+ add_definitions("-DPARALLEL_MARK")
+ endif()
+ if (enable_thread_local_alloc)
+ add_definitions("-DTHREAD_LOCAL_ALLOC")
+ set(SRC ${SRC} specific.c thread_local_alloc.c)
+ endif()
+ message("Explicit GC_INIT() calls may be required.")
+ if (HOST MATCHES .*-.*-hpux11.*)
+ message("Only HP/UX 11 POSIX threads are supported.")
+ add_definitions("-D_POSIX_C_SOURCE=199506L")
+ set(NEED_LIB_RT ON)
+ elseif (HOST MATCHES .*-.*-netbsd.*)
+ message("Only on NetBSD 2.0 or later.")
+ add_definitions("-D_PTHREADS")
+ set(NEED_LIB_RT ON)
+ elseif (CMAKE_C_COMPILER_ID STREQUAL "SunPro")
+ set(NEED_LIB_RT ON)
+ endif()
+ if (MSYS)
+ # Does not provide process fork functionality.
+ elseif (APPLE)
+ # The incremental mode conflicts with fork handling (for now).
+ # Thus, HANDLE_FORK is not defined.
+ elseif (enable_handle_fork AND NOT disable_handle_fork)
+ add_definitions("-DHANDLE_FORK")
+ endif()
+ if (enable_sigrt_signals)
+ add_definitions("-DGC_USESIGRT_SIGNALS")
+ endif()
+ elseif (CMAKE_USE_WIN32_THREADS_INIT)
+ add_definitions("-DGC_THREADS")
+ if (enable_parallel_mark)
+ add_definitions("-DPARALLEL_MARK")
+ endif()
+ if (enable_thread_local_alloc AND (enable_parallel_mark OR NOT BUILD_SHARED_LIBS))
+ # Imply THREAD_LOCAL_ALLOC unless GC_DLL.
+ add_definitions("-DTHREAD_LOCAL_ALLOC")
+ set(SRC ${SRC} thread_local_alloc.c)
+ endif()
+ add_definitions("-DEMPTY_GETENV_RESULTS")
+ set(SRC ${SRC} pthread_start.c) # in case client defines GC_WIN32_PTHREADS
+ set(SRC ${SRC} pthread_support.c win32_threads.c)
+ elseif (CMAKE_HP_PTHREADS_INIT OR CMAKE_USE_SPROC_INIT)
+ message(FATAL_ERROR "Unsupported thread package")
endif()
- if (HOST MATCHES .*-.*-hpux10.*)
- message(FATAL_ERROR "HP/UX 10 POSIX threads are not supported.")
- endif()
- # Common defines for POSIX platforms.
- add_definitions("-DGC_THREADS -D_REENTRANT")
- if (enable_parallel_mark)
- add_definitions("-DPARALLEL_MARK")
- endif()
- if (enable_thread_local_alloc)
- add_definitions("-DTHREAD_LOCAL_ALLOC")
- set(SRC ${SRC} specific.c thread_local_alloc.c)
- endif()
- message("Explicit GC_INIT() calls may be required.")
- if (HOST MATCHES .*-.*-hpux11.*)
- message("Only HP/UX 11 POSIX threads are supported.")
- add_definitions("-D_POSIX_C_SOURCE=199506L")
- set(NEED_LIB_RT ON)
- elseif (HOST MATCHES .*-.*-netbsd.*)
- message("Only on NetBSD 2.0 or later.")
- add_definitions("-D_PTHREADS")
- set(NEED_LIB_RT ON)
- elseif (CMAKE_C_COMPILER_ID STREQUAL "SunPro")
- set(NEED_LIB_RT ON)
- endif()
- if (MSYS)
- # Does not provide process fork functionality.
- elseif (APPLE)
- # The incremental mode conflicts with fork handling (for now).
- # Thus, HANDLE_FORK is not defined.
- elseif (enable_handle_fork AND NOT disable_handle_fork)
- add_definitions("-DHANDLE_FORK")
- endif()
- if (enable_sigrt_signals)
- add_definitions("-DGC_USESIGRT_SIGNALS")
- endif()
-elseif (CMAKE_USE_WIN32_THREADS_INIT)
- add_definitions("-DGC_THREADS")
- if (enable_parallel_mark)
- add_definitions("-DPARALLEL_MARK")
- endif()
- if (enable_thread_local_alloc AND (enable_parallel_mark OR NOT BUILD_SHARED_LIBS))
- # Imply THREAD_LOCAL_ALLOC unless GC_DLL.
- add_definitions("-DTHREAD_LOCAL_ALLOC")
- set(SRC ${SRC} thread_local_alloc.c)
- endif()
- add_definitions("-DEMPTY_GETENV_RESULTS")
- set(SRC ${SRC} pthread_start.c) # in case client defines GC_WIN32_PTHREADS
- set(SRC ${SRC} pthread_support.c win32_threads.c)
-elseif (CMAKE_HP_PTHREADS_INIT OR CMAKE_USE_SPROC_INIT)
- message(FATAL_ERROR "Unsupported thread package")
-endif()
+endif(enable_threads)
# Check whether -lrt linker option is needed to use clock_gettime.
if (NOT NEED_LIB_RT)
@@ -407,7 +412,7 @@ endif(enable_werror)
if (enable_single_obj_compilation OR (BUILD_SHARED_LIBS AND NOT disable_single_obj_compilation))
set(SRC extra/gc.c) # override SRC
- if (CMAKE_USE_PTHREADS_INIT AND NOT (APPLE OR CYGWIN OR MSYS))
+ if (enable_threads AND CMAKE_USE_PTHREADS_INIT AND NOT (APPLE OR CYGWIN OR MSYS))
add_definitions("-DGC_PTHREAD_START_STANDALONE")
set(SRC ${SRC} pthread_start.c)
endif()
@@ -502,7 +507,7 @@ if (HAVE_WCSLEN)
endif()
# pthread_setname_np, if available, may have 1, 2 or 3 arguments.
-if (CMAKE_USE_PTHREADS_INIT)
+if (enable_threads AND CMAKE_USE_PTHREADS_INIT)
check_c_source_compiles("
#define _GNU_SOURCE 1\n
#include <pthread.h>\n