]> granicus.if.org Git - gc/blob - CMakeLists.txt
Enable CMake-based build for Borland and Watcom compilers
[gc] / 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 option(enable_cplusplus "C++ support" OFF)
25 if (enable_cplusplus)
26   project(gc)
27 else()
28   project(gc C)
29 endif()
30
31 include(CTest)
32
33 cmake_minimum_required(VERSION 3.1)
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 "TODO" OFF) #TODO Support it
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 include_directories(libatomic_ops/src)
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(LIBS)
102
103 if (enable_threads)
104   find_package(Threads REQUIRED)
105   message("Thread Model: ${CMAKE_THREAD_LIBS_INIT}" )
106   include_directories(${Threads_INCLUDE_DIR})
107   set(LIBS ${LIBS} ${Threads_LIBRARIES})
108 endif()
109
110 #IF(Threads_FOUND)
111 #       ADD_DEFINITIONS("")
112 #ELSE
113 #       MESSAGE("Parallel mark requires enable_threads ON" )
114 #ENDIF(Threads_FOUND)
115
116 set(_HOST ${CMAKE_HOST_SYSTEM_PROCESSOR}--${CMAKE_SYSTEM})
117                                 #FIXME missing the vendor field.
118 string(TOLOWER ${_HOST} HOST)
119 message("HOST = ${HOST}")
120
121 # Thread Detection.  Relying on cmake for lib and includes.
122 #TODO check cmake detection
123 if (CMAKE_USE_PTHREADS_INIT)
124   set(SRC ${SRC} pthread_start.c pthread_support.c pthread_stop_world.c)
125   # Common defines for most POSIX platforms.
126   if (HOST MATCHES .*-.*-aix.*|.*-.*-android.*|.*-.*-cygwin.*|.*-.*-darwin.*|.*-.*-.*freebsd.*|.*-.*-haiku.*|.*-.*-gnu.*|.*-.*-hpux11.*|.*-.*-irix.*|.*-.*-.*linux.*|.*-.*-msys.*|.*-.*-nacl.*|.*-.*-netbsd.*|.*-.*-openbsd.*|.*-.*-osf.*|.*-.*-solaris.*)
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   endif()
137   if (HOST MATCHES .*-.*-hpux11.*)
138     message("Only HP/UX 11 POSIX threads are supported.")
139     add_definitions("-D_POSIX_C_SOURCE=199506L")
140         #TODO test -DVAR=value. Alternative is COMPILE_DEFINITIONS property
141   endif()
142   if (HOST MATCHES .*-.*-hpux10.*)
143     message("HP/UX 10 POSIX threads are not supported.")
144   endif()
145   if (HOST MATCHES .*-.*-netbsd.*)
146     message("Only on NetBSD 2.0 or later.")
147     add_definitions("-D_PTHREADS")
148   endif()
149   if (HOST MATCHES .*-.*-android.*)
150     # Android NDK does not provide pthread_atfork.
151   elseif (HOST MATCHES .*-.*-aix.*|.*-.*-cygwin.*|.*-.*-freebsd.*|.*-.*-haiku.*|.*-.*-hpux11.*|.*-.*-irix.*|.*-.*-kfreebsd.*-gnu|.*-.*-.*linux.*|.*-.*-netbsd.*|.*-.*-openbsd.*|.*-.*-osf.*|.*-.*-solaris.*)
152     if (enable_handle_fork)
153       add_definitions("-DHANDLE_FORK")
154     endif(enable_handle_fork)
155   endif()
156   if (HOST MATCHES .*-.*-cygwin.*|.*-.*-msys.*)
157     set(SRC ${SRC} win32_threads.c)
158   endif()
159   if (HOST MATCHES .*-.*-darwin.*)
160     if (enable_handle_fork)
161       # The incremental mode conflicts with fork handling.
162       if (enable_parallel_mark)
163         add_definitions("-DHANDLE_FORK")
164       endif()
165     endif(enable_handle_fork)
166     set(SRC ${SRC} darwin_stop_world.c)
167     #TODO darwin_threads=true
168   endif()
169   if (enable_sigrt_signals)
170     add_definitions("-DGC_USESIGRT_SIGNALS")
171   endif()
172 endif(CMAKE_USE_PTHREADS_INIT)
173
174 if (CMAKE_USE_WIN32_THREADS_INIT)
175   add_definitions("-DGC_THREADS")
176   if (enable_parallel_mark)
177     add_definitions("-DPARALLEL_MARK")
178     if (enable_thread_local_alloc)
179       add_definitions("-DTHREAD_LOCAL_ALLOC")
180       set(SRC ${SRC} thread_local_alloc.c)
181     endif()
182   endif(enable_parallel_mark)
183   add_definitions("-DEMPTY_GETENV_RESULTS")
184   set(SRC ${SRC} win32_threads.c)
185 endif(CMAKE_USE_WIN32_THREADS_INIT)
186
187 if (enable_gcj_support)
188   add_definitions("-DGC_GCJ_SUPPORT")
189   if (enable_threads)
190     add_definitions("-DGC_ENABLE_SUSPEND_THREAD")
191   endif()
192   set(SRC ${SRC} gcj_mlc.c)
193 endif(enable_gcj_support)
194
195 if (enable_disclaim)
196   add_definitions("-DENABLE_DISCLAIM")
197   set(SRC ${SRC} fnlz_mlc.c)
198 endif()
199
200 if (enable_java_finalization)
201   add_definitions("-DJAVA_FINALIZATION")
202 endif()
203
204 if (enable_atomic_uncollectable)
205   add_definitions("-DGC_ATOMIC_UNCOLLECTABLE")
206 endif()
207
208 if (enable_gc_debug)
209   add_definitions("-DDBG_HDRS_ALL -DKEEP_BACK_PTRS")
210   if (HOST MATCHES ia64-.*-linux.*|i586-.*-linux.*|i686-.*-linux.*|x86-.*-linux.*|x86_64-.*-linux.*)
211     add_definitions("-DMAKE_BACK_GRAPH")
212     add_definitions("-DSAVE_CALL_COUNT=8")
213     set(SRC ${SRC} backgraph.c)
214   endif()
215   if (HOST MATCHES i.86-.*-dgux.*)
216     add_definitions("-DMAKE_BACK_GRAPH")
217     set(SRC ${SRC} backgraph.c)
218   endif()
219 endif(enable_gc_debug)
220
221 if (enable_redirect_malloc)
222   if (enable_gc_debug)
223     add_definitions("-DREDIRECT_MALLOC=GC_debug_malloc_replacement")
224     add_definitions("-DREDIRECT_REALLOC=GC_debug_realloc_replacement")
225     add_definitions("-DREDIRECT_FREE=GC_debug_free")
226   else()
227     add_definitions("-DREDIRECT_MALLOC=GC_malloc")
228   endif()
229   add_definitions("-DGC_USE_DLOPEN_WRAP")
230 endif(enable_redirect_malloc)
231
232 if (enable_munmap)
233   add_definitions("-DUSE_MMAP -DUSE_MUNMAP")
234 elseif (enable_mmap)
235   add_definitions("-DUSE_MMAP")
236 endif()
237
238 if (NOT enable_dynamic_loading)
239   add_definitions("-DIGNORE_DYNAMIC_LOADING")
240 endif()
241
242 if (NOT enable_register_main_static_data)
243   add_definitions("-DGC_DONT_REGISTER_MAIN_STATIC_DATA")
244 endif()
245
246 if (enable_large_config)
247   add_definitions("-DLARGE_CONFIG")
248 endif()
249
250 if (enable_gc_assertions)
251   add_definitions("-DGC_ASSERTIONS")
252 endif()
253
254 if (NOT enable_threads_discovery)
255   add_definitions("-DGC_NO_THREADS_DISCOVERY")
256 endif()
257
258 if (enable_checksums)
259   if (enable_munmap OR enable_threads)
260     message("CHECKSUMS not compatible with USE_MUNMAP or threads")
261   endif()
262   add_definitions("-DCHECKSUMS")
263   set(SRC ${SRC} checksums.c)
264 endif(enable_checksums)
265
266 if (enable_werror)
267   if (BORLAND)
268     add_compile_options(/w!)
269   elseif (MSVC)
270     add_compile_options(/WX)
271   elseif (WATCOM)
272     add_compile_options(/we)
273   else()
274     add_compile_options(-Werror)
275     if (APPLE)
276       # _dyld_bind_fully_image_containing_address is deprecated in OS X 10.5+
277       add_compile_options(-Wno-deprecated-declarations)
278     endif()
279   endif()
280 endif(enable_werror)
281
282 if (enable_single_obj_compilation OR BUILD_SHARED_LIBS)
283   set(SRC extra/gc.c) # override SRC
284   if (CMAKE_USE_PTHREADS_INIT)
285     add_definitions("-DGC_PTHREAD_START_STANDALONE")
286     set(SRC ${SRC} pthread_start.c)
287   endif(CMAKE_USE_PTHREADS_INIT)
288 elseif (BORLAND)
289   # Suppress "GC_push_contents_hdr() is declared but never used" warning.
290   add_compile_options(/w-use)
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 add_library(gc ${SRC})
304
305 if (enable_cplusplus)
306   add_library(gccpp gc_cpp.cc)
307   target_link_libraries(gccpp PRIVATE gc)
308 endif()
309
310 if (build_cord)
311   set(CORD_SRC cord/cordbscs.c cord/cordprnt.c cord/cordxtra.c)
312   add_library(cord ${CORD_SRC})
313   target_link_libraries(cord PRIVATE gc)
314   install(TARGETS cord EXPORT cordExports
315           LIBRARY DESTINATION lib
316           ARCHIVE DESTINATION lib
317           RUNTIME DESTINATION bin
318           INCLUDES DESTINATION include)
319 endif()
320
321 install(TARGETS gc EXPORT gcExports
322         LIBRARY DESTINATION lib
323         ARCHIVE DESTINATION lib
324         RUNTIME DESTINATION bin
325         INCLUDES DESTINATION include)
326
327 if (enable_cplusplus)
328   install(TARGETS gccpp EXPORT gccppExports
329           LIBRARY DESTINATION lib
330           ARCHIVE DESTINATION lib
331           RUNTIME DESTINATION bin
332           INCLUDES DESTINATION include)
333 endif()
334
335 if (install_headers)
336   install(FILES include/gc.h
337                 include/gc_backptr.h
338                 include/gc_config_macros.h
339                 include/gc_gcj.h
340                 include/gc_inline.h
341                 include/gc_mark.h
342                 include/gc_pthread_redirects.h
343                 include/gc_tiny_fl.h
344                 include/gc_typed.h
345                 include/gc_version.h
346                 include/javaxfc.h
347                 include/leak_detector.h
348           DESTINATION include/gc)
349   install(FILES include/extra/gc.h DESTINATION include)
350   if (enable_cplusplus)
351     install(FILES include/gc_allocator.h
352                   include/gc_cpp.h
353             DESTINATION include/gc)
354     install(FILES include/extra/gc_cpp.h DESTINATION include)
355   endif()
356   if (enable_disclaim)
357     install(FILES include/gc_disclaim.h DESTINATION include/gc)
358   endif()
359   if (build_cord)
360     install(FILES include/cord.h
361                   include/cord_pos.h
362                   include/ec.h
363             DESTINATION include/gc)
364   endif()
365 endif(install_headers)
366
367 if (build_tests)
368   if (build_cord)
369     add_executable(cordtest cord/tests/cordtest.c)
370     target_link_libraries(cordtest PRIVATE cord gc)
371     add_test(NAME cordtest COMMAND cordtest)
372
373     if (WIN32)
374       add_executable(de cord/tests/de.c cord/tests/de_win.c cord/tests/de_win.rc)
375       set_target_properties(de PROPERTIES WIN32_EXECUTABLE TRUE)
376       target_link_libraries(de PRIVATE cord gc gdi32)
377     endif()
378   endif(build_cord)
379
380   # Compile some tests as C++ to test extern "C" in header files.
381   if (enable_cplusplus)
382     set_source_files_properties(tests/leak_test.c tests/test.c
383                                 PROPERTIES LANGUAGE CXX)
384   endif()
385
386   add_executable(gctest WIN32 tests/test.c)
387   target_link_libraries(gctest PRIVATE gc)
388   add_test(NAME gctest COMMAND gctest)
389   if (WATCOM)
390     # Suppress "conditional expression in if statement is always true/false"
391     # and "unreachable code" warnings in GC_MALLOC_[ATOMIC_]WORDS.
392     target_compile_options(gctest PRIVATE
393                            /wcd=13 /wcd=201 /wcd=367 /wcd=368 /wcd=726)
394   endif()
395
396   add_executable(hugetest tests/huge_test.c)
397   target_link_libraries(hugetest PRIVATE gc)
398   add_test(NAME hugetest COMMAND hugetest)
399
400   add_executable(leaktest tests/leak_test.c)
401   target_link_libraries(leaktest PRIVATE gc)
402   add_test(NAME leaktest COMMAND leaktest)
403
404   add_executable(middletest tests/middle.c)
405   target_link_libraries(middletest PRIVATE gc)
406   add_test(NAME middletest COMMAND middletest)
407
408   add_executable(realloc_test tests/realloc_test.c)
409   target_link_libraries(realloc_test PRIVATE gc)
410   add_test(NAME realloc_test COMMAND realloc_test)
411
412   add_executable(smashtest tests/smash_test.c)
413   target_link_libraries(smashtest PRIVATE gc)
414   add_test(NAME smashtest COMMAND smashtest)
415
416   if (enable_gc_debug)
417     add_executable(tracetest tests/trace_test.c)
418     target_link_libraries(tracetest PRIVATE gc)
419     add_test(NAME tracetest COMMAND tracetest)
420   endif()
421
422   if (enable_cplusplus)
423     add_executable(test_cpp WIN32 tests/test_cpp.cc)
424     target_link_libraries(test_cpp PRIVATE gc gccpp)
425     add_test(NAME test_cpp COMMAND test_cpp)
426   endif()
427
428   if (enable_disclaim)
429     add_executable(disclaim_bench tests/disclaim_bench.c)
430     target_link_libraries(disclaim_bench PRIVATE gc)
431     add_test(NAME disclaim_bench COMMAND disclaim_bench)
432
433     add_executable(disclaim_test tests/disclaim_test.c)
434     target_link_libraries(disclaim_test PRIVATE gc)
435     add_test(NAME disclaim_test COMMAND disclaim_test)
436
437     add_executable(disclaim_weakmap_test tests/disclaim_weakmap_test.c)
438     target_link_libraries(disclaim_weakmap_test PRIVATE gc)
439     add_test(NAME disclaim_weakmap_test COMMAND disclaim_weakmap_test)
440   endif()
441 endif(build_tests)