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