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