Debian Codegraph @

gc/CMakeLists.txt

raw
1 #
2 # Copyright (c) 1994 by Xerox Corporation. All rights reserved.
3 # Copyright (c) 1996 by Silicon Graphics. All rights reserved.
4 # Copyright (c) 1998 by Fergus Henderson. All rights reserved.
5 # Copyright (c) 2000-2010 by Hewlett-Packard Company. All rights reserved.
6 # Copyright (c) 2010-2021 Ivan Maidanski
7 ##
8 # THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
9 # OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
10 ##
11 # Permission is hereby granted to use or copy this program
12 # for any purpose, provided the above notices are retained on all copies.
13 # Permission to modify the code and to distribute modified code is granted,
14 # provided the above notices are retained, and a notice that the code was
15 # modified is included with the above copyright notice.
16 ##
17
18 #
19 # get cmake and run:
20 # cmake -G "Visual Studio 8 2005"
21 # in the same dir as this file
22 # this will generate gc.sln
23 #
24
25 cmake_minimum_required(VERSION 3.5)
26
27 set(PACKAGE_VERSION 8.2.8)
28 # Version must match that in AC_INIT of configure.ac and that in README.
29 # Version must conform to: [0-9]+[.][0-9]+[.][0-9]+
30
31 # Info (current:revision:age) for the Libtool versioning system.
32 # These values should match those in cord/cord.am and Makefile.am.
33 set(LIBCORD_VER_INFO 6:1:5)
34 set(LIBGC_VER_INFO 6:4:5)
35 set(LIBGCCPP_VER_INFO 6:0:5)
36
37 option(enable_cplusplus "C++ support" OFF)
38 if (enable_cplusplus)
39 project(gc)
40 else()
41 project(gc C)
42 endif()
43
44 if (POLICY CMP0057)
45 # Required for CheckLinkerFlag, at least.
46 cmake_policy(SET CMP0057 NEW)
47 endif()
48
49 include(CheckCCompilerFlag)
50 include(CheckCSourceCompiles)
51 include(CheckFunctionExists)
52 include(CheckIncludeFile)
53 include(CheckSymbolExists)
54 include(CMakePackageConfigHelpers)
55 include(CTest)
56 include(GNUInstallDirs)
57
58 if (NOT (${CMAKE_VERSION} VERSION_LESS "3.18.0"))
59 include(CheckLinkerFlag)
60 endif()
61
62 # Customize the build by passing "-D<option_name>=ON|OFF" in the command line.
63 option(BUILD_SHARED_LIBS "Build shared libraries" ON)
64 option(build_cord "Build cord library" ON)
65 option(build_tests "Build tests" OFF)
66 option(enable_docs "Build and install documentation" ON)
67 option(enable_threads "Support threads" ON)
68 option(enable_parallel_mark "Parallelize marking and free list construction" ON)
69 option(enable_thread_local_alloc "Turn on thread-local allocation optimization" ON)
70 option(enable_threads_discovery "Enable threads discovery in GC" ON)
71 option(enable_throw_bad_alloc_library "Turn on C++ gctba library build" ON)
72 option(enable_gcj_support "Support for gcj" ON)
73 option(enable_sigrt_signals "Use SIGRTMIN-based signals for thread suspend/resume" OFF)
74 option(enable_gc_debug "Support for pointer back-tracing" OFF)
75 option(disable_gc_debug "Disable debugging like GC_dump and its callees" OFF)
76 option(enable_java_finalization "Support for java finalization" ON)
77 option(enable_atomic_uncollectable "Support for atomic uncollectible allocation" ON)
78 option(enable_redirect_malloc "Redirect malloc and friends to GC routines" OFF)
79 option(enable_disclaim "Support alternative finalization interface" ON)
80 option(enable_large_config "Optimize for large heap or root set" OFF)
81 option(enable_gc_assertions "Enable collector-internal assertion checking" OFF)
82 option(enable_mmap "Use mmap instead of sbrk to expand the heap" OFF)
83 option(enable_munmap "Return page to the OS if empty for N collections" ON)
84 option(enable_dynamic_loading "Enable tracing of dynamic library data roots" ON)
85 option(enable_register_main_static_data "Perform the initial guess of data root sets" ON)
86 option(enable_checksums "Report erroneously cleared dirty bits" OFF)
87 option(enable_werror "Pass -Werror to the C compiler (treat warnings as errors)" OFF)
88 option(enable_single_obj_compilation "Compile all libgc source files into single .o" OFF)
89 option(enable_handle_fork "Attempt to ensure a usable collector after fork()" ON)
90 option(disable_handle_fork "Prohibit installation of pthread_atfork() handlers" OFF)
91 option(enable_emscripten_asyncify "Use Emscripten asyncify feature" OFF)
92 option(install_headers "Install header and pkg-config metadata files" ON)
93 option(without_libatomic_ops "Use atomic_ops.h in libatomic_ops/src" OFF)
94
95 # Override the default build type to RelWithDebInfo (this instructs cmake to
96 # pass -O2 -g -DNDEBUG options to the compiler by default).
97 if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
98 set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE
99 STRING "Choose the type of build." FORCE)
100 set_property(CACHE CMAKE_BUILD_TYPE PROPERTY
101 STRINGS "Debug" "Release" "RelWithDebInfo" "MinSizeRel")
102 endif()
103
104 # Convert VER_INFO values to [SO]VERSION ones.
105 if (BUILD_SHARED_LIBS)
106 # cord:
107 string(REGEX REPLACE "(.+):.+:.+" "\\1" cord_cur ${LIBCORD_VER_INFO})
108 string(REGEX REPLACE ".+:(.+):.+" "\\1" cord_rev ${LIBCORD_VER_INFO})
109 string(REGEX REPLACE ".+:.+:(.+)$" "\\1" cord_age ${LIBCORD_VER_INFO})
110 math(EXPR CORD_SOVERSION "${cord_cur} - ${cord_age}")
111 set(CORD_VERSION_PROP "${CORD_SOVERSION}.${cord_age}.${cord_rev}")
112 message(STATUS "CORD_VERSION_PROP = ${CORD_VERSION_PROP}")
113 # gc:
114 string(REGEX REPLACE "(.+):.+:.+" "\\1" gc_cur ${LIBGC_VER_INFO})
115 string(REGEX REPLACE ".+:(.+):.+" "\\1" gc_rev ${LIBGC_VER_INFO})
116 string(REGEX REPLACE ".+:.+:(.+)$" "\\1" gc_age ${LIBGC_VER_INFO})
117 math(EXPR GC_SOVERSION "${gc_cur} - ${gc_age}")
118 set(GC_VERSION_PROP "${GC_SOVERSION}.${gc_age}.${gc_rev}")
119 message(STATUS "GC_VERSION_PROP = ${GC_VERSION_PROP}")
120 # gccpp and gctba:
121 string(REGEX REPLACE "(.+):.+:.+" "\\1" gccpp_cur ${LIBGCCPP_VER_INFO})
122 string(REGEX REPLACE ".+:(.+):.+" "\\1" gccpp_rev ${LIBGCCPP_VER_INFO})
123 string(REGEX REPLACE ".+:.+:(.+)$" "\\1" gccpp_age ${LIBGCCPP_VER_INFO})
124 math(EXPR GCCPP_SOVERSION "${gccpp_cur} - ${gccpp_age}")
125 set(GCCPP_VERSION_PROP "${GCCPP_SOVERSION}.${gccpp_age}.${gccpp_rev}")
126 message(STATUS "GCCPP_VERSION_PROP = ${GCCPP_VERSION_PROP}")
127 endif(BUILD_SHARED_LIBS)
128
129 add_definitions("-DALL_INTERIOR_POINTERS -DNO_EXECUTE_PERMISSION")
130
131 # Set struct packing alignment to word (instead of 1-byte).
132 if (BORLAND)
133 add_compile_options(/a4)
134 elseif (WATCOM)
135 add_compile_options(/zp4)
136 endif()
137
138 # Output all warnings.
139 if (BORLAND)
140 # All warnings except for particular ones.
141 add_compile_options(/w /w-pro /w-aus /w-par /w-ccc /w-inl /w-rch)
142 elseif (MSVC)
143 # All warnings but ignoring "unreferenced formal parameter" and
144 # "conditional expression is constant" ones.
145 add_compile_options(/W4 /wd4100 /wd4127)
146 elseif (WATCOM)
147 add_compile_options(/wx)
148 if (NOT enable_threads)
149 # Suppress "unreachable code" warning in GC_ASSERT(I_HOLD_LOCK()).
150 add_compile_options(/wcd=201)
151 endif()
152 else()
153 # TODO add -[W]pedantic -Wno-long-long
154 add_compile_options(-Wall -Wextra)
155 endif()
156
157 if (WIN32)
158 # Disable MS crt security warnings reported e.g. for getenv, strcpy.
159 add_definitions("-D_CRT_SECURE_NO_DEPRECATE")
160 endif()
161
162 include_directories(include)
163
164 set(SRC alloc.c reclaim.c allchblk.c misc.c mach_dep.c os_dep.c
165 mark_rts.c headers.c mark.c obj_map.c blacklst.c finalize.c
166 new_hblk.c dbg_mlc.c malloc.c dyn_load.c typd_mlc.c ptr_chck.c
167 mallocx.c)
168 set(THREADDLLIBS_LIST)
169 set(NEED_LIB_RT)
170
171 set(_HOST ${CMAKE_SYSTEM_PROCESSOR}-unknown-${CMAKE_SYSTEM})
172 string(TOLOWER ${_HOST} HOST)
173 message(STATUS "TARGET = ${HOST}")
174
175 if (enable_threads)
176 find_package(Threads REQUIRED)
177 message(STATUS "Thread library: ${CMAKE_THREAD_LIBS_INIT}")
178 if (without_libatomic_ops OR BORLAND OR MSVC OR WATCOM)
179 include_directories(libatomic_ops/src)
180 # Note: alternatively, use CFLAGS_EXTRA to pass -I<...>/libatomic_ops/src.
181 else()
182 # Assume the compiler supports GCC atomic intrinsics.
183 add_definitions("-DGC_BUILTIN_ATOMIC")
184 endif()
185 include_directories(${Threads_INCLUDE_DIR})
186 set(THREADDLLIBS_LIST ${CMAKE_THREAD_LIBS_INIT})
187 if (${CMAKE_DL_LIBS} MATCHES ^[^-].*)
188 # Some cmake versions have a broken non-empty CMAKE_DL_LIBS omitting "-l".
189 # Assume CMAKE_DL_LIBS contains just one library.
190 set(THREADDLLIBS_LIST ${THREADDLLIBS_LIST} -l${CMAKE_DL_LIBS})
191 else()
192 set(THREADDLLIBS_LIST ${THREADDLLIBS_LIST} ${CMAKE_DL_LIBS})
193 endif()
194 endif(enable_threads)
195
196 set(ATOMIC_OPS_LIBS "") # TODO: Assume libatomic_ops library is not needed.
197
198 # Thread support detection.
199 if (CMAKE_USE_PTHREADS_INIT)
200 set(SRC ${SRC} gc_dlopen.c)
201 if (CYGWIN OR WIN32)
202 set(SRC ${SRC} win32_threads.c)
203 else()
204 set(SRC ${SRC} pthread_start.c pthread_stop_world.c pthread_support.c)
205 endif()
206 if (APPLE)
207 set(SRC ${SRC} darwin_stop_world.c)
208 endif()
209 if (HOST MATCHES .*-.*-hpux10.*)
210 message(FATAL_ERROR "HP/UX 10 POSIX threads are not supported.")
211 endif()
212 # Common defines for POSIX platforms.
213 add_definitions("-DGC_THREADS -D_REENTRANT")
214 if (enable_parallel_mark)
215 add_definitions("-DPARALLEL_MARK")
216 endif()
217 if (enable_thread_local_alloc)
218 add_definitions("-DTHREAD_LOCAL_ALLOC")
219 set(SRC ${SRC} specific.c thread_local_alloc.c)
220 endif()
221 message("Explicit GC_INIT() calls may be required.")
222 if (HOST MATCHES .*-.*-hpux11.*)
223 message("Only HP/UX 11 POSIX threads are supported.")
224 add_definitions("-D_POSIX_C_SOURCE=199506L")
225 set(NEED_LIB_RT ON)
226 elseif (HOST MATCHES .*-.*-netbsd.*)
227 message("Only on NetBSD 2.0 or later.")
228 add_definitions("-D_PTHREADS")
229 set(NEED_LIB_RT ON)
230 endif()
231 if (WIN32) # AND NOT CYGWIN
232 # Does not provide process fork functionality.
233 elseif (enable_handle_fork AND NOT disable_handle_fork)
234 add_definitions("-DHANDLE_FORK")
235 endif()
236 if (enable_sigrt_signals)
237 add_definitions("-DGC_USESIGRT_SIGNALS")
238 endif()
239 elseif (CMAKE_USE_WIN32_THREADS_INIT)
240 add_definitions("-DGC_THREADS")
241 if (enable_parallel_mark)
242 add_definitions("-DPARALLEL_MARK")
243 endif()
244 if (enable_thread_local_alloc AND (enable_parallel_mark OR NOT BUILD_SHARED_LIBS))
245 # Imply THREAD_LOCAL_ALLOC unless GC_DLL.
246 add_definitions("-DTHREAD_LOCAL_ALLOC")
247 set(SRC ${SRC} thread_local_alloc.c)
248 endif()
249 add_definitions("-DEMPTY_GETENV_RESULTS")
250 set(SRC ${SRC} win32_threads.c)
251 elseif (CMAKE_HP_PTHREADS_INIT OR CMAKE_USE_SPROC_INIT)
252 message(FATAL_ERROR "Unsupported thread package")
253 endif()
254
255 # Check whether -lrt linker option is needed to use clock_gettime.
256 if (NOT NEED_LIB_RT)
257 check_function_exists(clock_gettime HAVE_CLOCK_GETTIME_DIRECTLY)
258 if (NOT HAVE_CLOCK_GETTIME_DIRECTLY)
259 # Use of clock_gettime() probably requires linking with "rt" library.
260 set(NEED_LIB_RT ON)
261 endif()
262 endif()
263
264 # Locate and use "rt" library if needed (and the library is available).
265 if (NEED_LIB_RT)
266 find_library(LIBRT rt)
267 if (LIBRT)
268 set(THREADDLLIBS_LIST ${THREADDLLIBS_LIST} ${LIBRT})
269 endif()
270 endif(NEED_LIB_RT)
271
272 if (disable_handle_fork)
273 add_definitions("-DNO_HANDLE_FORK")
274 endif()
275
276 if (enable_gcj_support)
277 add_definitions("-DGC_GCJ_SUPPORT")
278 if (enable_threads AND NOT (enable_thread_local_alloc AND HOST MATCHES .*-.*-kfreebsd.*-gnu))
279 # FIXME: For a reason, gctest hangs up on kFreeBSD if both of
280 # THREAD_LOCAL_ALLOC and GC_ENABLE_SUSPEND_THREAD are defined.
281 add_definitions("-DGC_ENABLE_SUSPEND_THREAD")
282 endif()
283 set(SRC ${SRC} gcj_mlc.c)
284 endif(enable_gcj_support)
285
286 if (enable_disclaim)
287 add_definitions("-DENABLE_DISCLAIM")
288 set(SRC ${SRC} fnlz_mlc.c)
289 endif()
290
291 if (enable_java_finalization)
292 add_definitions("-DJAVA_FINALIZATION")
293 endif()
294
295 if (enable_atomic_uncollectable)
296 add_definitions("-DGC_ATOMIC_UNCOLLECTABLE")
297 endif()
298
299 if (enable_gc_debug)
300 add_definitions("-DDBG_HDRS_ALL -DKEEP_BACK_PTRS")
301 if (HOST MATCHES i.86-.*-dgux.*|ia64-.*-linux.*|i586-.*-linux.*|i686-.*-linux.*|x86-.*-linux.*|x86_64-.*-linux.*)
302 add_definitions("-DMAKE_BACK_GRAPH")
303 if (HOST MATCHES .*-.*-.*linux.*)
304 add_definitions("-DSAVE_CALL_COUNT=8")
305 endif()
306 set(SRC ${SRC} backgraph.c)
307 endif()
308 endif(enable_gc_debug)
309
310 if (disable_gc_debug)
311 add_definitions("-DNO_DEBUGGING")
312 elseif (WINCE)
313 # Read environment variables from "<program>.gc.env" file.
314 add_definitions("-DGC_READ_ENV_FILE")
315 endif()
316
317 if (enable_redirect_malloc)
318 if (enable_gc_debug)
319 add_definitions("-DREDIRECT_MALLOC=GC_debug_malloc_replacement")
320 add_definitions("-DREDIRECT_REALLOC=GC_debug_realloc_replacement")
321 add_definitions("-DREDIRECT_FREE=GC_debug_free")
322 else()
323 add_definitions("-DREDIRECT_MALLOC=GC_malloc")
324 endif()
325 if (WIN32)
326 add_definitions("-DREDIRECT_MALLOC_IN_HEADER")
327 else()
328 add_definitions("-DGC_USE_DLOPEN_WRAP")
329 endif()
330 endif(enable_redirect_malloc)
331
332 if (enable_munmap)
333 add_definitions("-DUSE_MMAP -DUSE_MUNMAP")
334 elseif (enable_mmap)
335 add_definitions("-DUSE_MMAP")
336 endif()
337
338 if (NOT enable_dynamic_loading)
339 add_definitions("-DIGNORE_DYNAMIC_LOADING")
340 endif()
341
342 if (NOT enable_register_main_static_data)
343 add_definitions("-DGC_DONT_REGISTER_MAIN_STATIC_DATA")
344 endif()
345
346 if (enable_large_config)
347 add_definitions("-DLARGE_CONFIG")
348 endif()
349
350 if (enable_gc_assertions)
351 add_definitions("-DGC_ASSERTIONS")
352 endif()
353
354 if (NOT enable_threads_discovery)
355 add_definitions("-DGC_NO_THREADS_DISCOVERY")
356 endif()
357
358 if (enable_checksums)
359 if (enable_munmap OR enable_threads)
360 message(FATAL_ERROR "CHECKSUMS not compatible with USE_MUNMAP or threads")
361 endif()
362 add_definitions("-DCHECKSUMS")
363 set(SRC ${SRC} checksums.c)
364 endif(enable_checksums)
365
366 if (enable_werror)
367 if (BORLAND)
368 add_compile_options(/w!)
369 if (enable_threads)
370 # Workaround "function should return a value" warning for several
371 # asm functions in atomic_ops/sysdeps/msftc/x86.h.
372 add_compile_options(/w-rvl)
373 endif(enable_threads)
374 elseif (MSVC)
375 add_compile_options(/WX)
376 # Workaround "typedef ignored on left of ..." warning reported in
377 # imagehlp.h of e.g. Windows Kit 8.1.
378 add_compile_options(/wd4091)
379 elseif (WATCOM)
380 add_compile_options(/we)
381 else()
382 add_compile_options(-Werror)
383 if (APPLE)
384 # _dyld_bind_fully_image_containing_address is deprecated in OS X 10.5+
385 add_compile_options(-Wno-deprecated-declarations)
386 endif()
387 endif()
388 endif(enable_werror)
389
390 if (enable_single_obj_compilation OR BUILD_SHARED_LIBS)
391 set(SRC extra/gc.c) # override SRC
392 if (CMAKE_USE_PTHREADS_INIT AND NOT (CYGWIN OR WIN32))
393 add_definitions("-DGC_PTHREAD_START_STANDALONE")
394 set(SRC ${SRC} pthread_start.c)
395 endif()
396 elseif (BORLAND)
397 # Suppress "GC_push_contents_hdr() is declared but never used" warning.
398 add_compile_options(/w-use)
399 endif()
400
401 # Add implementation of backtrace() and backtrace_symbols().
402 if (MSVC)
403 set(SRC ${SRC} extra/msvc_dbg.c)
404 endif()
405
406 # Declare that the libraries do not refer to external symbols.
407 if (BUILD_SHARED_LIBS AND NOT (APPLE OR ${CMAKE_SYSTEM_NAME} MATCHES "BSD"))
408 # Note: performed before CMAKE_REQUIRED_FLAGS is updated with "-c".
409 if (${CMAKE_VERSION} VERSION_LESS "3.18.0")
410 set(WL_NO_UNDEFINED_OPT "-Wl,--no-undefined")
411 check_c_compiler_flag(${WL_NO_UNDEFINED_OPT} HAVE_FLAG_WL_NO_UNDEFINED)
412 else()
413 set(WL_NO_UNDEFINED_OPT "LINKER:--no-undefined")
414 check_linker_flag(C "${WL_NO_UNDEFINED_OPT}" HAVE_FLAG_WL_NO_UNDEFINED)
415 endif()
416 endif()
417
418 # Instruct check_c_source_compiles to skip linking.
419 # Alternatively, we could set CMAKE_REQUIRED_LIBRARIES properly.
420 SET(CMAKE_REQUIRED_FLAGS "-c")
421
422 # Instruct check_c_source_compiles and similar CMake checks not to ignore
423 # compiler warnings (like "implicit declaration of function").
424 if (NOT BORLAND AND NOT MSVC AND NOT WATCOM)
425 check_c_compiler_flag(-Werror HAVE_FLAG_WERROR)
426 if (HAVE_FLAG_WERROR)
427 check_c_compiler_flag(-Wno-unused-command-line-argument
428 HAVE_FLAG_WNO_UNUSED_CMDLINE_ARG)
429 SET(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror")
430 # Prevent "linker input unused" error in further check_c_compiler_flag.
431 if (HAVE_FLAG_WNO_UNUSED_CMDLINE_ARG)
432 SET(CMAKE_REQUIRED_FLAGS
433 "${CMAKE_REQUIRED_FLAGS} -Wno-unused-command-line-argument")
434 endif()
435 endif(HAVE_FLAG_WERROR)
436 endif()
437
438 if (BUILD_SHARED_LIBS)
439 add_definitions("-DGC_DLL")
440 # Pass -fvisibility=hidden option if supported.
441 check_c_compiler_flag(-fvisibility=hidden HAVE_FLAG_F_VISIBILITY_HIDDEN)
442 if (HAVE_FLAG_F_VISIBILITY_HIDDEN)
443 add_definitions("-DGC_VISIBILITY_HIDDEN_SET")
444 add_compile_options(-fvisibility=hidden)
445 else()
446 add_definitions("-DGC_NO_VISIBILITY")
447 endif()
448 else()
449 add_definitions("-DGC_NOT_DLL")
450 if (WIN32)
451 # Do not require the clients to link with "user32" system library.
452 add_definitions("-DDONT_USE_USER32_DLL")
453 endif(WIN32)
454 endif()
455
456 # Disable strict aliasing optimizations.
457 # It could re-enabled back by a flag passed in CFLAGS_EXTRA.
458 check_c_compiler_flag(-fno-strict-aliasing HAVE_FLAG_F_NO_STRICT_ALIASING)
459 if (HAVE_FLAG_F_NO_STRICT_ALIASING)
460 add_compile_options(-fno-strict-aliasing)
461 endif()
462
463 # Prevent "__builtin_return_address with a nonzero argument is unsafe" warning.
464 if (NOT BORLAND AND NOT MSVC AND NOT WATCOM)
465 check_c_compiler_flag(-Wno-frame-address HAVE_FLAG_WNO_FRAME_ADDRESS)
466 if (HAVE_FLAG_WNO_FRAME_ADDRESS)
467 add_compile_options(-Wno-frame-address)
468 endif(HAVE_FLAG_WNO_FRAME_ADDRESS)
469 endif()
470
471 # Extra user-defined flags to pass both to C and C++ compilers.
472 if (DEFINED CFLAGS_EXTRA)
473 separate_arguments(CFLAGS_EXTRA_LIST UNIX_COMMAND "${CFLAGS_EXTRA}")
474 add_compile_options(${CFLAGS_EXTRA_LIST})
475 endif()
476
477 # Check whether execinfo.h header file is present.
478 check_include_file(execinfo.h HAVE_EXECINFO_H)
479 if (NOT HAVE_EXECINFO_H)
480 add_definitions("-DGC_MISSING_EXECINFO_H")
481 endif()
482
483 # Check for getcontext (uClibc can be configured without it, for example).
484 check_function_exists(getcontext HAVE_GETCONTEXT)
485 if (HAVE_GETCONTEXT AND NOT APPLE)
486 # Double check getcontext() is available (needed at least on OpenBSD 7.3).
487 # Note: OS X is excluded here because the header filename differs.
488 check_c_source_compiles("
489 #include <ucontext.h>\n
490 int main(void) { ucontext_t ctxt; (void)getcontext(&ctxt); return 0; }"
491 HAVE_GETCONTEXT_FUNC)
492 if (NOT HAVE_GETCONTEXT_FUNC)
493 set(HAVE_GETCONTEXT OFF)
494 endif()
495 endif()
496 if (NOT HAVE_GETCONTEXT)
497 add_definitions("-DNO_GETCONTEXT")
498 endif()
499
500 # Check whether dl_iterate_phdr exists (as a strong symbol).
501 check_function_exists(dl_iterate_phdr HAVE_DL_ITERATE_PHDR)
502 if (HAVE_DL_ITERATE_PHDR)
503 add_definitions("-DHAVE_DL_ITERATE_PHDR")
504 endif()
505
506 check_symbol_exists(sigsetjmp setjmp.h HAVE_SIGSETJMP)
507 if (NOT HAVE_SIGSETJMP)
508 add_definitions("-DGC_NO_SIGSETJMP")
509 endif()
510
511 # pthread_setname_np, if available, may have 1, 2 or 3 arguments.
512 if (CMAKE_USE_PTHREADS_INIT)
513 check_c_source_compiles("
514 #define _GNU_SOURCE 1\n
515 #include <pthread.h>\n
516 int main(void) { (void)pthread_setname_np(\"thread-name\"); return 0; }"
517 HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID)
518 if (HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID)
519 # Define to use 'pthread_setname_np(const char*)' function.
520 add_definitions("-DHAVE_PTHREAD_SETNAME_NP_WITHOUT_TID")
521 else()
522 check_c_source_compiles("
523 #define _GNU_SOURCE 1\n
524 #include <pthread.h>\n
525 int main(void) {\n
526 (void)pthread_setname_np(pthread_self(), \"thread-name-%u\", 0); return 0; }"
527 HAVE_PTHREAD_SETNAME_NP_WITH_TID_AND_ARG)
528 if (HAVE_PTHREAD_SETNAME_NP_WITH_TID_AND_ARG)
529 # Define to use 'pthread_setname_np(pthread_t, const char*, void *)'.
530 add_definitions("-DHAVE_PTHREAD_SETNAME_NP_WITH_TID_AND_ARG")
531 else()
532 check_c_source_compiles("
533 #define _GNU_SOURCE 1\n
534 #include <pthread.h>\n
535 int main(void) {\n
536 (void)pthread_setname_np(pthread_self(), \"thread-name\"); return 0; }"
537 HAVE_PTHREAD_SETNAME_NP_WITH_TID)
538 if (HAVE_PTHREAD_SETNAME_NP_WITH_TID)
539 # Define to use 'pthread_setname_np(pthread_t, const char*)' function.
540 add_definitions("-DHAVE_PTHREAD_SETNAME_NP_WITH_TID")
541 endif()
542 endif(HAVE_PTHREAD_SETNAME_NP_WITH_TID_AND_ARG)
543 endif(HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID)
544 endif()
545
546 # Check for dladdr (used for debugging).
547 check_c_source_compiles("
548 #define _GNU_SOURCE 1\n
549 #include <dlfcn.h>\n
550 int main(void) { Dl_info info; (void)dladdr(\"\", &info); return 0; }"
551 HAVE_DLADDR)
552 if (HAVE_DLADDR)
553 # Define to use 'dladdr' function.
554 add_definitions("-DHAVE_DLADDR")
555 endif()
556
557 # Check for emscripten; use asyncify feature if requested.
558 check_c_source_compiles("
559 #ifndef __EMSCRIPTEN__\n
560 # error This is not Emscripten\n
561 #endif\n
562 int main(void) { return 0; }"
563 EMSCRIPTEN)
564 if (EMSCRIPTEN AND enable_emscripten_asyncify)
565 # Use this option if your program is targeting -sASYNCIFY. The latter is
566 # required to scan the stack, ASYNCIFY_STACK_SIZE is probably needed for
567 # gctest only.
568 add_definitions("-DEMSCRIPTEN_ASYNCIFY")
569 set(CMAKE_EXE_LINKER_FLAGS
570 "${CMAKE_EXE_LINKER_FLAGS} -sASYNCIFY -sASYNCIFY_STACK_SIZE=128000")
571 endif()
572
573 add_library(gc ${SRC})
574 target_link_libraries(gc PRIVATE ${THREADDLLIBS_LIST} ${ATOMIC_OPS_LIBS})
575 target_include_directories(gc INTERFACE
576 "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
577 "$<INSTALL_INTERFACE:include>")
578
579 if (enable_cplusplus)
580 add_library(gccpp gc_badalc.cc gc_cpp.cc)
581 target_link_libraries(gccpp PRIVATE gc)
582 target_include_directories(gccpp INTERFACE
583 "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
584 "$<INSTALL_INTERFACE:include>")
585 if (enable_throw_bad_alloc_library)
586 # The same as gccpp but contains only gc_badalc.
587 add_library(gctba gc_badalc.cc)
588 target_link_libraries(gctba PRIVATE gc)
589 target_include_directories(gctba INTERFACE
590 "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
591 "$<INSTALL_INTERFACE:include>")
592 endif(enable_throw_bad_alloc_library)
593 endif()
594
595 if (build_cord)
596 set(CORD_SRC cord/cordbscs.c cord/cordprnt.c cord/cordxtra.c)
597 add_library(cord ${CORD_SRC})
598 target_link_libraries(cord PRIVATE gc)
599 target_include_directories(cord INTERFACE
600 "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
601 "$<INSTALL_INTERFACE:include>")
602 if (BUILD_SHARED_LIBS)
603 set_property(TARGET cord PROPERTY VERSION ${CORD_VERSION_PROP})
604 set_property(TARGET cord PROPERTY SOVERSION ${CORD_SOVERSION})
605 endif()
606 install(TARGETS cord EXPORT BDWgcTargets
607 LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
608 ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
609 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
610 INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
611 endif(build_cord)
612
613 if (BUILD_SHARED_LIBS AND HAVE_FLAG_WL_NO_UNDEFINED)
614 # Declare that the libraries do not refer to external symbols.
615 if (${CMAKE_VERSION} VERSION_LESS "3.13.0")
616 target_link_libraries(gc PRIVATE ${WL_NO_UNDEFINED_OPT})
617 if (enable_cplusplus)
618 target_link_libraries(gccpp PRIVATE ${WL_NO_UNDEFINED_OPT})
619 if (enable_throw_bad_alloc_library)
620 target_link_libraries(gctba PRIVATE ${WL_NO_UNDEFINED_OPT})
621 endif(enable_throw_bad_alloc_library)
622 endif(enable_cplusplus)
623 if (build_cord)
624 target_link_libraries(cord PRIVATE ${WL_NO_UNDEFINED_OPT})
625 endif(build_cord)
626 else()
627 target_link_options(gc PRIVATE ${WL_NO_UNDEFINED_OPT})
628 if (enable_cplusplus)
629 target_link_options(gccpp PRIVATE ${WL_NO_UNDEFINED_OPT})
630 if (enable_throw_bad_alloc_library)
631 target_link_options(gctba PRIVATE ${WL_NO_UNDEFINED_OPT})
632 endif(enable_throw_bad_alloc_library)
633 endif(enable_cplusplus)
634 if (build_cord)
635 target_link_options(cord PRIVATE ${WL_NO_UNDEFINED_OPT})
636 endif(build_cord)
637 endif()
638 endif()
639
640 if (BUILD_SHARED_LIBS)
641 set_property(TARGET gc PROPERTY VERSION ${GC_VERSION_PROP})
642 set_property(TARGET gc PROPERTY SOVERSION ${GC_SOVERSION})
643 endif()
644 install(TARGETS gc EXPORT BDWgcTargets
645 LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
646 ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
647 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
648 INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
649
650 if (enable_cplusplus)
651 if (BUILD_SHARED_LIBS)
652 set_property(TARGET gccpp PROPERTY VERSION ${GCCPP_VERSION_PROP})
653 set_property(TARGET gccpp PROPERTY SOVERSION ${GCCPP_SOVERSION})
654 endif()
655 install(TARGETS gccpp EXPORT BDWgcTargets
656 LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
657 ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
658 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
659 INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
660 if (enable_throw_bad_alloc_library)
661 if (BUILD_SHARED_LIBS)
662 set_property(TARGET gctba PROPERTY VERSION ${GCCPP_VERSION_PROP})
663 set_property(TARGET gctba PROPERTY SOVERSION ${GCCPP_SOVERSION})
664 endif()
665 install(TARGETS gctba EXPORT BDWgcTargets
666 LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
667 ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
668 RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
669 INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
670 endif(enable_throw_bad_alloc_library)
671 endif(enable_cplusplus)
672
673 if (install_headers)
674 install(FILES include/gc.h
675 include/gc_backptr.h
676 include/gc_config_macros.h
677 include/gc_inline.h
678 include/gc_mark.h
679 include/gc_tiny_fl.h
680 include/gc_typed.h
681 include/gc_version.h
682 include/javaxfc.h
683 include/leak_detector.h
684 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/gc")
685 install(FILES include/extra/gc.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
686 if (enable_cplusplus)
687 install(FILES include/gc_allocator.h
688 include/gc_cpp.h
689 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/gc")
690 install(FILES include/extra/gc_cpp.h
691 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
692 endif()
693 if (enable_disclaim)
694 install(FILES include/gc_disclaim.h
695 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/gc")
696 endif()
697 if (enable_gcj_support)
698 install(FILES include/gc_gcj.h
699 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/gc")
700 endif()
701 if (enable_threads AND CMAKE_USE_PTHREADS_INIT)
702 install(FILES include/gc_pthread_redirects.h
703 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/gc")
704 endif()
705 if (build_cord)
706 install(FILES include/cord.h
707 include/cord_pos.h
708 include/ec.h
709 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/gc")
710 endif()
711
712 # Provide pkg-config metadata.
713 set(prefix "${CMAKE_INSTALL_PREFIX}")
714 set(exec_prefix \${prefix})
715 set(includedir "${CMAKE_INSTALL_FULL_INCLUDEDIR}")
716 set(libdir "${CMAKE_INSTALL_FULL_LIBDIR}")
717 string(REPLACE ";" " " THREADDLLIBS "${THREADDLLIBS_LIST}")
718 # ATOMIC_OPS_LIBS, PACKAGE_VERSION are defined above.
719 configure_file(bdw-gc.pc.in bdw-gc.pc @ONLY)
720 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/bdw-gc.pc"
721 DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
722 endif(install_headers)
723
724 if (build_tests)
725 if (build_cord)
726 add_executable(cordtest cord/tests/cordtest.c)
727 target_link_libraries(cordtest PRIVATE cord gc)
728 add_test(NAME cordtest COMMAND cordtest)
729
730 if (WIN32 AND NOT CYGWIN)
731 if (NOT (CMAKE_C_COMPILER_ID STREQUAL "Clang"))
732 # Workaround MS Clang failure to compile a resource file.
733 set(DE_WIN_RC cord/tests/de_win.rc)
734 endif()
735 add_executable(de cord/tests/de.c cord/tests/de_win.c
736 ${DE_WIN_RC})
737 set_target_properties(de PROPERTIES WIN32_EXECUTABLE TRUE)
738 target_link_libraries(de PRIVATE cord gc gdi32)
739 endif()
740 endif(build_cord)
741
742 # Compile some tests as C++ to test extern "C" in header files.
743 if (enable_cplusplus)
744 set_source_files_properties(tests/leak_test.c PROPERTIES LANGUAGE CXX)
745 if (NOT MSVC)
746 # WinMain-based test hangs at startup if compiled by VC as C++ code.
747 set_source_files_properties(tests/test.c PROPERTIES LANGUAGE CXX)
748 endif()
749 # To avoid "treating 'c' input as 'c++' when in C++ mode" Clang warning.
750 if (NOT (BORLAND OR MSVC OR WATCOM))
751 add_compile_options(-x c++)
752 endif()
753 endif(enable_cplusplus)
754
755 add_executable(gctest WIN32 tests/test.c)
756 target_link_libraries(gctest PRIVATE gc ${THREADDLLIBS_LIST})
757 add_test(NAME gctest COMMAND gctest)
758 if (WATCOM)
759 # Suppress "conditional expression in if statement is always true/false"
760 # and "unreachable code" warnings in GC_MALLOC_[ATOMIC_]WORDS.
761 target_compile_options(gctest PRIVATE
762 /wcd=13 /wcd=201 /wcd=367 /wcd=368 /wcd=726)
763 endif()
764
765 add_executable(hugetest tests/huge_test.c)
766 target_link_libraries(hugetest PRIVATE gc)
767 add_test(NAME hugetest COMMAND hugetest)
768
769 add_executable(leaktest tests/leak_test.c)
770 target_link_libraries(leaktest PRIVATE gc)
771 add_test(NAME leaktest COMMAND leaktest)
772
773 add_executable(middletest tests/middle.c)
774 target_link_libraries(middletest PRIVATE gc)
775 add_test(NAME middletest COMMAND middletest)
776
777 add_executable(realloc_test tests/realloc_test.c)
778 target_link_libraries(realloc_test PRIVATE gc)
779 add_test(NAME realloc_test COMMAND realloc_test)
780
781 add_executable(smashtest tests/smash_test.c)
782 target_link_libraries(smashtest PRIVATE gc)
783 add_test(NAME smashtest COMMAND smashtest)
784
785 if (NOT (BUILD_SHARED_LIBS AND WIN32))
786 add_library(staticrootslib_test tests/staticrootslib.c)
787 target_link_libraries(staticrootslib_test PRIVATE gc)
788 add_library(staticrootslib2_test tests/staticrootslib.c)
789 target_compile_options(staticrootslib2_test PRIVATE "-DSTATICROOTSLIB2")
790 target_link_libraries(staticrootslib2_test PRIVATE gc)
791 add_executable(staticrootstest tests/staticrootstest.c)
792 target_compile_options(staticrootstest PRIVATE "-DSTATICROOTSLIB2")
793 target_link_libraries(staticrootstest PRIVATE
794 gc staticrootslib_test staticrootslib2_test)
795 add_test(NAME staticrootstest COMMAND staticrootstest)
796 endif()
797
798 if (enable_gc_debug)
799 add_executable(tracetest tests/trace_test.c)
800 target_link_libraries(tracetest PRIVATE gc)
801 add_test(NAME tracetest COMMAND tracetest)
802 endif()
803
804 if (enable_threads)
805 add_executable(test_atomic_ops tests/test_atomic_ops.c)
806 target_link_libraries(test_atomic_ops PRIVATE gc)
807 add_test(NAME test_atomic_ops COMMAND test_atomic_ops)
808
809 add_executable(threadleaktest tests/thread_leak_test.c)
810 target_link_libraries(threadleaktest PRIVATE gc ${THREADDLLIBS_LIST})
811 add_test(NAME threadleaktest COMMAND threadleaktest)
812
813 if (NOT WIN32)
814 add_executable(threadkey_test tests/threadkey_test.c)
815 target_link_libraries(threadkey_test PRIVATE gc ${THREADDLLIBS_LIST})
816 add_test(NAME threadkey_test COMMAND threadkey_test)
817 endif()
818
819 add_executable(subthreadcreate_test tests/subthread_create.c)
820 target_link_libraries(subthreadcreate_test PRIVATE gc ${THREADDLLIBS_LIST})
821 add_test(NAME subthreadcreate_test COMMAND subthreadcreate_test)
822
823 add_executable(initsecondarythread_test tests/initsecondarythread.c)
824 target_link_libraries(initsecondarythread_test
825 PRIVATE gc ${THREADDLLIBS_LIST})
826 add_test(NAME initsecondarythread_test COMMAND initsecondarythread_test)
827 endif(enable_threads)
828
829 if (enable_cplusplus)
830 add_executable(test_cpp WIN32 tests/test_cpp.cc)
831 target_link_libraries(test_cpp PRIVATE gc gccpp)
832 add_test(NAME test_cpp COMMAND test_cpp)
833 endif()
834
835 if (enable_disclaim)
836 add_executable(disclaim_bench tests/disclaim_bench.c)
837 target_link_libraries(disclaim_bench PRIVATE gc)
838 add_test(NAME disclaim_bench COMMAND disclaim_bench)
839
840 add_executable(disclaim_test tests/disclaim_test.c)
841 target_link_libraries(disclaim_test PRIVATE gc ${THREADDLLIBS_LIST})
842 add_test(NAME disclaim_test COMMAND disclaim_test)
843
844 add_executable(disclaim_weakmap_test tests/disclaim_weakmap_test.c)
845 target_link_libraries(disclaim_weakmap_test
846 PRIVATE gc ${THREADDLLIBS_LIST})
847 add_test(NAME disclaim_weakmap_test COMMAND disclaim_weakmap_test)
848 endif()
849 endif(build_tests)
850
851 if (enable_docs)
852 # Note: documentation which is not installed: README.QUICK
853 install(FILES AUTHORS README.md
854 DESTINATION "${CMAKE_INSTALL_DOCDIR}")
855 install(DIRECTORY doc/ DESTINATION "${CMAKE_INSTALL_DOCDIR}"
856 FILES_MATCHING
857 PATTERN "README.*"
858 PATTERN "*.md")
859
860 install(FILES doc/gc.man DESTINATION "${CMAKE_INSTALL_MANDIR}/man3"
861 RENAME gc.3)
862 endif(enable_docs)
863
864 # CMake config/targets files.
865 install(EXPORT BDWgcTargets FILE BDWgcTargets.cmake
866 NAMESPACE BDWgc:: DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/bdwgc")
867
868 configure_package_config_file("${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in"
869 "${CMAKE_CURRENT_BINARY_DIR}/BDWgcConfig.cmake"
870 INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/bdwgc"
871 NO_SET_AND_CHECK_MACRO)
872
873 write_basic_package_version_file(
874 "${CMAKE_CURRENT_BINARY_DIR}/BDWgcConfigVersion.cmake"
875 VERSION "${PACKAGE_VERSION}" COMPATIBILITY AnyNewerVersion)
876
877 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/BDWgcConfig.cmake"
878 "${CMAKE_CURRENT_BINARY_DIR}/BDWgcConfigVersion.cmake"
879 DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/bdwgc")
880
881 export(EXPORT BDWgcTargets
882 FILE "${CMAKE_CURRENT_BINARY_DIR}/BDWgcTargets.cmake")