Workaround 'NULL==*flh is always true' cppcheck style warning in allocobj
[platform/upstream/libgc.git] / CMakeLists.txt
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 ##
7 # THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
8 # OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
9 ##
10 # Permission is hereby granted to use or copy this program
11 # for any purpose,  provided the above notices are retained on all copies.
12 # Permission to modify the code and to distribute modified code is granted,
13 # provided the above notices are retained, and a notice that the code was
14 # modified is included with the above copyright notice.
15 ##
16
17 #
18 #  get cmake and run:
19 #    cmake -G "Visual Studio 8 2005"
20 #  in the same dir as this file
21 #  this will generate gc.sln
22 #
23
24 cmake_minimum_required(VERSION 3.1)
25
26 option(enable_cplusplus "C++ support" OFF)
27 if (enable_cplusplus)
28   project(gc)
29 else()
30   project(gc C)
31 endif()
32
33 include(CTest)
34
35 # Customize the build by passing "-D<option_name>=ON|OFF" in the command line.
36 option(BUILD_SHARED_LIBS "Build shared libraries" ON)
37 option(build_cord "Build cord library" ON)
38 option(build_tests "Build tests" OFF)
39 option(enable_threads "Support threads" ON)
40 option(enable_parallel_mark "Parallelize marking and free list construction" ON)
41 option(enable_thread_local_alloc "Turn on thread-local allocation optimization" ON)
42 option(enable_threads_discovery "Enable threads discovery in GC" ON)
43 option(enable_throw_bad_alloc_library "Turn on C++ gctba library build" ON)
44 option(enable_gcj_support "Support for gcj" ON)
45 option(enable_sigrt_signals "Use SIGRTMIN-based signals for thread suspend/resume" OFF)
46 option(enable_gc_debug "Support for pointer back-tracing" OFF)
47 option(enable_java_finalization "Support for java finalization" ON)
48 option(enable_atomic_uncollectable "Support for atomic uncollectible allocation" ON)
49 option(enable_redirect_malloc "Redirect malloc and friends to GC routines" OFF)
50 option(enable_disclaim "Support alternative finalization interface" ON)
51 option(enable_large_config "Optimize for large heap or root set" OFF)
52 option(enable_gc_assertions "Enable collector-internal assertion checking" OFF)
53 option(enable_mmap "Use mmap instead of sbrk to expand the heap" OFF)
54 option(enable_munmap "Return page to the OS if empty for N collections" ON)
55 option(enable_dynamic_loading "Enable tracing of dynamic library data roots" ON)
56 option(enable_register_main_static_data "Perform the initial guess of data root sets" ON)
57 option(enable_checksums "Report erroneously cleared dirty bits" OFF)
58 option(enable_werror "Pass -Werror to the C compiler (treat warnings as errors)" OFF)
59 option(enable_single_obj_compilation "Compile all libgc source files into single .o" OFF)
60 option(enable_handle_fork "Attempt to ensure a usable collector after fork()" ON)
61 option(install_headers "Install header files" ON)
62
63 add_definitions("-DALL_INTERIOR_POINTERS -DNO_EXECUTE_PERMISSION")
64
65 if (APPLE AND ("${CMAKE_OSX_ARCHITECTURES}" STREQUAL ""))
66     set(CMAKE_OSX_ARCHITECTURES "x86_64;i386"
67         CACHE STRING "Build architectures for Mac OS X" FORCE)
68 endif()
69
70 # Set struct packing alignment to word (instead of 1-byte).
71 if (BORLAND)
72   add_compile_options(/a4)
73 elseif (WATCOM)
74   add_compile_options(/zp4)
75 endif()
76
77 # Output all warnings.
78 if (BORLAND)
79   # All warnings except for particular ones.
80   add_compile_options(/w /w-pro /w-aus /w-par /w-ccc /w-inl /w-rch)
81 elseif (MSVC)
82   # All warnings but ignoring "unreferenced formal parameter" and
83   # "conditional expression is constant" ones.
84   add_compile_options(/W4 /wd4100 /wd4127)
85   # Disable crt security warnings, since unfortunately they warn about all
86   # sorts of safe uses of strncpy.
87   add_definitions("-D_CRT_SECURE_NO_DEPRECATE")
88 elseif (WATCOM)
89   add_compile_options(/wx)
90 else()
91   # TODO add -[W]pedantic -Wno-long-long
92   add_compile_options(-Wall -Wextra)
93 endif()
94
95 include_directories(include)
96
97 set(SRC alloc.c reclaim.c allchblk.c misc.c mach_dep.c os_dep.c
98         mark_rts.c headers.c mark.c obj_map.c blacklst.c finalize.c
99         new_hblk.c dbg_mlc.c malloc.c dyn_load.c typd_mlc.c ptr_chck.c
100         mallocx.c)
101 set(THREADDLLIBS)
102
103 set(_HOST ${CMAKE_SYSTEM_PROCESSOR}-unknown-${CMAKE_SYSTEM})
104 string(TOLOWER ${_HOST} HOST)
105 message(STATUS "TARGET = ${HOST}")
106
107 if (enable_threads)
108   find_package(Threads REQUIRED)
109   message(STATUS "Thread library: ${CMAKE_THREAD_LIBS_INIT}")
110   include_directories(libatomic_ops/src)
111   include_directories(${Threads_INCLUDE_DIR})
112   set(THREADDLLIBS ${CMAKE_THREAD_LIBS_INIT})
113   if (NOT (APPLE OR CYGWIN OR MSYS OR WIN32 OR HOST MATCHES mips-.*-irix6.*))
114     set(THREADDLLIBS ${THREADDLLIBS} -ldl)
115                                 # The predefined CMAKE_DL_LIBS may be broken.
116   endif()
117 endif(enable_threads)
118
119 # Thread support detection.
120 if (CMAKE_USE_PTHREADS_INIT)
121   set(SRC ${SRC} pthread_start.c pthread_support.c pthread_stop_world.c)
122   if (HOST MATCHES .*-.*-hpux10.*)
123     message(FATAL_ERROR "HP/UX 10 POSIX threads are not supported.")
124   endif()
125   # Assume the compiler supports C11 (GCC) atomic intrinsics.
126   add_definitions("-DGC_BUILTIN_ATOMIC")
127   # Common defines for POSIX platforms.
128   add_definitions("-DGC_THREADS -D_REENTRANT")
129   if (enable_parallel_mark)
130     add_definitions("-DPARALLEL_MARK")
131   endif()
132   if (enable_thread_local_alloc)
133     add_definitions("-DTHREAD_LOCAL_ALLOC")
134     set(SRC ${SRC} thread_local_alloc.c)
135   endif()
136   message("Explicit GC_INIT() calls may be required.")
137   if (HOST MATCHES .*-.*-hpux11.*)
138     message("Only HP/UX 11 POSIX threads are supported.")
139     add_definitions("-D_POSIX_C_SOURCE=199506L")
140   elseif (HOST MATCHES .*-.*-netbsd.*)
141     message("Only on NetBSD 2.0 or later.")
142     add_definitions("-D_PTHREADS")
143   endif()
144   if (ANDROID OR MSYS) # ANDROID variable is defined by CMake v3.7.0+.
145     # Android NDK does not provide pthread_atfork.
146   elseif (APPLE)
147     if (enable_handle_fork)
148       # The incremental mode conflicts with fork handling.
149       if (enable_parallel_mark)
150         add_definitions("-DHANDLE_FORK")
151       endif()
152     endif(enable_handle_fork)
153     set(SRC ${SRC} darwin_stop_world.c)
154   elseif (enable_handle_fork)
155     add_definitions("-DHANDLE_FORK")
156   endif()
157   if (enable_sigrt_signals)
158     add_definitions("-DGC_USESIGRT_SIGNALS")
159   endif()
160   if (CYGWIN OR MSYS)
161     set(SRC ${SRC} win32_threads.c)
162   endif()
163 elseif (CMAKE_USE_WIN32_THREADS_INIT)
164   add_definitions("-DGC_THREADS")
165   if (enable_parallel_mark)
166     add_definitions("-DPARALLEL_MARK")
167   endif()
168   if (enable_thread_local_alloc AND (enable_parallel_mark OR NOT BUILD_SHARED_LIBS))
169     # Imply THREAD_LOCAL_ALLOC unless GC_DLL.
170     add_definitions("-DTHREAD_LOCAL_ALLOC")
171     set(SRC ${SRC} thread_local_alloc.c)
172   endif()
173   add_definitions("-DEMPTY_GETENV_RESULTS")
174   set(SRC ${SRC} win32_threads.c)
175 elseif (CMAKE_HP_PTHREADS_INIT OR CMAKE_USE_SPROC_INIT)
176   message(FATAL_ERROR "Unsupported thread package")
177 endif()
178
179 if (enable_gcj_support)
180   add_definitions("-DGC_GCJ_SUPPORT")
181   if (enable_threads AND NOT (enable_thread_local_alloc AND HOST MATCHES .*-.*-kfreebsd.*-gnu))
182     # FIXME: For a reason, gctest hangs up on kFreeBSD if both of
183     # THREAD_LOCAL_ALLOC and GC_ENABLE_SUSPEND_THREAD are defined.
184     add_definitions("-DGC_ENABLE_SUSPEND_THREAD")
185   endif()
186   set(SRC ${SRC} gcj_mlc.c)
187 endif(enable_gcj_support)
188
189 if (enable_disclaim)
190   add_definitions("-DENABLE_DISCLAIM")
191   set(SRC ${SRC} fnlz_mlc.c)
192 endif()
193
194 if (enable_java_finalization)
195   add_definitions("-DJAVA_FINALIZATION")
196 endif()
197
198 if (enable_atomic_uncollectable)
199   add_definitions("-DGC_ATOMIC_UNCOLLECTABLE")
200 endif()
201
202 if (enable_gc_debug)
203   add_definitions("-DDBG_HDRS_ALL -DKEEP_BACK_PTRS")
204   if (HOST MATCHES i.86-.*-dgux.*|ia64-.*-linux.*|i586-.*-linux.*|i686-.*-linux.*|x86-.*-linux.*|x86_64-.*-linux.*)
205     add_definitions("-DMAKE_BACK_GRAPH")
206     if (HOST MATCHES .*-.*-.*linux.*)
207       add_definitions("-DSAVE_CALL_COUNT=8")
208     endif()
209     set(SRC ${SRC} backgraph.c)
210   endif()
211 endif(enable_gc_debug)
212
213 if (enable_redirect_malloc)
214   if (enable_gc_debug)
215     add_definitions("-DREDIRECT_MALLOC=GC_debug_malloc_replacement")
216     add_definitions("-DREDIRECT_REALLOC=GC_debug_realloc_replacement")
217     add_definitions("-DREDIRECT_FREE=GC_debug_free")
218   else()
219     add_definitions("-DREDIRECT_MALLOC=GC_malloc")
220   endif()
221   add_definitions("-DGC_USE_DLOPEN_WRAP")
222 endif(enable_redirect_malloc)
223
224 if (enable_munmap)
225   add_definitions("-DUSE_MMAP -DUSE_MUNMAP")
226 elseif (enable_mmap)
227   add_definitions("-DUSE_MMAP")
228 endif()
229
230 if (NOT enable_dynamic_loading)
231   add_definitions("-DIGNORE_DYNAMIC_LOADING")
232 endif()
233
234 if (NOT enable_register_main_static_data)
235   add_definitions("-DGC_DONT_REGISTER_MAIN_STATIC_DATA")
236 endif()
237
238 if (enable_large_config)
239   add_definitions("-DLARGE_CONFIG")
240 endif()
241
242 if (enable_gc_assertions)
243   add_definitions("-DGC_ASSERTIONS")
244 endif()
245
246 if (NOT enable_threads_discovery)
247   add_definitions("-DGC_NO_THREADS_DISCOVERY")
248 endif()
249
250 if (enable_checksums)
251   if (enable_munmap OR enable_threads)
252     message(FATAL_ERROR "CHECKSUMS not compatible with USE_MUNMAP or threads")
253   endif()
254   add_definitions("-DCHECKSUMS")
255   set(SRC ${SRC} checksums.c)
256 endif(enable_checksums)
257
258 if (enable_werror)
259   if (BORLAND)
260     add_compile_options(/w!)
261   elseif (MSVC)
262     add_compile_options(/WX)
263     # Workaround "typedef ignored on left of ..." warning reported in
264     # imagehlp.h of e.g. Windows Kit 8.1.
265     add_compile_options(/wd4091)
266   elseif (WATCOM)
267     add_compile_options(/we)
268   else()
269     add_compile_options(-Werror)
270     if (APPLE)
271       # _dyld_bind_fully_image_containing_address is deprecated in OS X 10.5+
272       add_compile_options(-Wno-deprecated-declarations)
273     endif()
274   endif()
275 endif(enable_werror)
276
277 if (enable_single_obj_compilation OR BUILD_SHARED_LIBS)
278   set(SRC extra/gc.c) # override SRC
279   if (CMAKE_USE_PTHREADS_INIT)
280     add_definitions("-DGC_PTHREAD_START_STANDALONE")
281     set(SRC ${SRC} pthread_start.c)
282   endif(CMAKE_USE_PTHREADS_INIT)
283 elseif (BORLAND)
284   # Suppress "GC_push_contents_hdr() is declared but never used" warning.
285   add_compile_options(/w-use)
286 endif()
287
288 # Add implementation of backtrace() and backtrace_symbols().
289 if (MSVC)
290   set(SRC ${SRC} extra/msvc_dbg.c)
291 endif()
292
293 if (BUILD_SHARED_LIBS)
294   add_definitions("-DGC_DLL")
295 else()
296   add_definitions("-DGC_NOT_DLL")
297   if (WIN32)
298     # Do not require the clients to link with "user32" system library.
299     add_definitions("-DDONT_USE_USER32_DLL")
300   endif(WIN32)
301 endif()
302
303 # Extra user-defined flags to pass both to C and C++ compilers.
304 if (DEFINED CFLAGS_EXTRA)
305   add_compile_options(${CFLAGS_EXTRA})
306 endif()
307
308 add_library(gc ${SRC})
309 if (enable_threads)
310   target_link_libraries(gc PRIVATE ${THREADDLLIBS})
311 endif()
312
313 if (enable_cplusplus)
314   add_library(gccpp gc_badalc.cc gc_cpp.cc)
315   target_link_libraries(gccpp PRIVATE gc)
316   if (enable_throw_bad_alloc_library)
317     # The same as gccpp but contains only gc_badalc.
318     add_library(gctba gc_badalc.cc)
319     target_link_libraries(gctba PRIVATE gc)
320   endif(enable_throw_bad_alloc_library)
321 endif()
322
323 if (build_cord)
324   set(CORD_SRC cord/cordbscs.c cord/cordprnt.c cord/cordxtra.c)
325   add_library(cord ${CORD_SRC})
326   target_link_libraries(cord PRIVATE gc)
327   install(TARGETS cord EXPORT cordExports
328           LIBRARY DESTINATION lib
329           ARCHIVE DESTINATION lib
330           RUNTIME DESTINATION bin
331           INCLUDES DESTINATION include)
332 endif()
333
334 install(TARGETS gc EXPORT gcExports
335         LIBRARY DESTINATION lib
336         ARCHIVE DESTINATION lib
337         RUNTIME DESTINATION bin
338         INCLUDES DESTINATION include)
339
340 if (enable_cplusplus)
341   install(TARGETS gccpp EXPORT gccppExports
342           LIBRARY DESTINATION lib
343           ARCHIVE DESTINATION lib
344           RUNTIME DESTINATION bin
345           INCLUDES DESTINATION include)
346 endif()
347
348 if (install_headers)
349   install(FILES include/gc.h
350                 include/gc_backptr.h
351                 include/gc_config_macros.h
352                 include/gc_gcj.h
353                 include/gc_inline.h
354                 include/gc_mark.h
355                 include/gc_pthread_redirects.h
356                 include/gc_tiny_fl.h
357                 include/gc_typed.h
358                 include/gc_version.h
359                 include/javaxfc.h
360                 include/leak_detector.h
361           DESTINATION include/gc)
362   install(FILES include/extra/gc.h DESTINATION include)
363   if (enable_cplusplus)
364     install(FILES include/gc_allocator.h
365                   include/gc_cpp.h
366             DESTINATION include/gc)
367     install(FILES include/extra/gc_cpp.h DESTINATION include)
368   endif()
369   if (enable_disclaim)
370     install(FILES include/gc_disclaim.h DESTINATION include/gc)
371   endif()
372   if (build_cord)
373     install(FILES include/cord.h
374                   include/cord_pos.h
375                   include/ec.h
376             DESTINATION include/gc)
377   endif()
378 endif(install_headers)
379
380 if (build_tests)
381   if (build_cord)
382     add_executable(cordtest cord/tests/cordtest.c)
383     target_link_libraries(cordtest PRIVATE cord gc)
384     add_test(NAME cordtest COMMAND cordtest)
385
386     if (WIN32 AND NOT CYGWIN)
387       add_executable(de cord/tests/de.c cord/tests/de_win.c
388                      cord/tests/de_win.rc)
389       set_target_properties(de PROPERTIES WIN32_EXECUTABLE TRUE)
390       target_link_libraries(de PRIVATE cord gc gdi32)
391     endif()
392   endif(build_cord)
393
394   # Compile some tests as C++ to test extern "C" in header files.
395   if (enable_cplusplus)
396     set_source_files_properties(tests/leak_test.c tests/test.c
397                                 PROPERTIES LANGUAGE CXX)
398     # To avoid "treating 'c' input as 'c++' when in C++ mode" Clang warning.
399     if (NOT (BORLAND OR MSVC OR WATCOM))
400       add_compile_options(-x c++)
401     endif()
402   endif(enable_cplusplus)
403
404   add_executable(gctest WIN32 tests/test.c)
405   target_link_libraries(gctest PRIVATE gc ${THREADDLLIBS})
406   add_test(NAME gctest COMMAND gctest)
407   if (WATCOM)
408     # Suppress "conditional expression in if statement is always true/false"
409     # and "unreachable code" warnings in GC_MALLOC_[ATOMIC_]WORDS.
410     target_compile_options(gctest PRIVATE
411                            /wcd=13 /wcd=201 /wcd=367 /wcd=368 /wcd=726)
412   endif()
413
414   add_executable(hugetest tests/huge_test.c)
415   target_link_libraries(hugetest PRIVATE gc)
416   add_test(NAME hugetest COMMAND hugetest)
417
418   add_executable(leaktest tests/leak_test.c)
419   target_link_libraries(leaktest PRIVATE gc)
420   add_test(NAME leaktest COMMAND leaktest)
421
422   add_executable(middletest tests/middle.c)
423   target_link_libraries(middletest PRIVATE gc)
424   add_test(NAME middletest COMMAND middletest)
425
426   add_executable(realloc_test tests/realloc_test.c)
427   target_link_libraries(realloc_test PRIVATE gc)
428   add_test(NAME realloc_test COMMAND realloc_test)
429
430   add_executable(smashtest tests/smash_test.c)
431   target_link_libraries(smashtest PRIVATE gc)
432   add_test(NAME smashtest COMMAND smashtest)
433
434   if (NOT (BUILD_SHARED_LIBS AND WIN32))
435     add_library(staticrootslib_test tests/staticrootslib.c)
436     target_link_libraries(staticrootslib_test PRIVATE gc)
437     add_library(staticrootslib2_test tests/staticrootslib.c)
438     target_compile_options(staticrootslib2_test PRIVATE "-DSTATICROOTSLIB2")
439     target_link_libraries(staticrootslib2_test PRIVATE gc)
440     add_executable(staticrootstest tests/staticrootstest.c)
441     target_compile_options(staticrootstest PRIVATE "-DSTATICROOTSLIB2")
442     target_link_libraries(staticrootstest PRIVATE
443                           gc staticrootslib_test staticrootslib2_test)
444     add_test(NAME staticrootstest COMMAND staticrootstest)
445   endif()
446
447   if (enable_gc_debug)
448     add_executable(tracetest tests/trace_test.c)
449     target_link_libraries(tracetest PRIVATE gc)
450     add_test(NAME tracetest COMMAND tracetest)
451   endif()
452
453   if (enable_threads)
454     add_executable(test_atomic_ops tests/test_atomic_ops.c)
455     target_link_libraries(test_atomic_ops PRIVATE gc)
456     add_test(NAME test_atomic_ops COMMAND test_atomic_ops)
457
458     add_executable(threadleaktest tests/thread_leak_test.c)
459     target_link_libraries(threadleaktest PRIVATE gc ${THREADDLLIBS})
460     add_test(NAME threadleaktest COMMAND threadleaktest)
461
462     if (NOT WIN32)
463       add_executable(threadkey_test tests/threadkey_test.c)
464       target_link_libraries(threadkey_test PRIVATE gc ${THREADDLLIBS})
465       add_test(NAME threadkey_test COMMAND threadkey_test)
466     endif()
467
468     add_executable(subthreadcreate_test tests/subthread_create.c)
469     target_link_libraries(subthreadcreate_test PRIVATE gc ${THREADDLLIBS})
470     add_test(NAME subthreadcreate_test COMMAND subthreadcreate_test)
471
472     add_executable(initsecondarythread_test tests/initsecondarythread.c)
473     target_link_libraries(initsecondarythread_test PRIVATE gc ${THREADDLLIBS})
474     add_test(NAME initsecondarythread_test COMMAND initsecondarythread_test)
475   endif(enable_threads)
476
477   if (enable_cplusplus)
478     add_executable(test_cpp WIN32 tests/test_cpp.cc)
479     target_link_libraries(test_cpp PRIVATE gc gccpp)
480     add_test(NAME test_cpp COMMAND test_cpp)
481   endif()
482
483   if (enable_disclaim)
484     add_executable(disclaim_bench tests/disclaim_bench.c)
485     target_link_libraries(disclaim_bench PRIVATE gc)
486     add_test(NAME disclaim_bench COMMAND disclaim_bench)
487
488     add_executable(disclaim_test tests/disclaim_test.c)
489     target_link_libraries(disclaim_test PRIVATE gc ${THREADDLLIBS})
490     add_test(NAME disclaim_test COMMAND disclaim_test)
491
492     add_executable(disclaim_weakmap_test tests/disclaim_weakmap_test.c)
493     target_link_libraries(disclaim_weakmap_test PRIVATE gc ${THREADDLLIBS})
494     add_test(NAME disclaim_weakmap_test COMMAND disclaim_weakmap_test)
495   endif()
496 endif(build_tests)