-
Notifications
You must be signed in to change notification settings - Fork 0
/
support.cmake
152 lines (127 loc) · 4.96 KB
/
support.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
## This file contains support functions and macros for making use of bricks
## easier with cmake-based projects. Call include(bricks/support.cmake) in your
## toplevel CMakeLists.txt to access those functions.
set( BRICK_USED_LLVM_LIBS )
set( BRICK_LLVM_LIBS LLVMCore LLVMSupport LLVMIRReader LLVMBitReader
LLVMBitWriter LLVMLinker LLVMObject )
function( update_file name content )
if( EXISTS ${name} )
file( READ ${name} old )
endif()
if( NOT "${old}" STREQUAL "${content}" )
file( WRITE ${name} ${content} )
endif()
endfunction()
function( bricks_make_runner name flags )
set( file "${CMAKE_CURRENT_BINARY_DIR}/${name}-runner.cpp" )
set( n 0 )
set( libs "" )
foreach( src ${ARGN} )
file( READ ${src} content )
string( FIND "${content}" "TEST" has_test )
if ( NOT has_test EQUAL -1 )
math( EXPR n "${n} + 1" )
set( fn "${CMAKE_CURRENT_BINARY_DIR}/${name}-runner-${n}.cpp" )
update_file( "${fn}" "#include <${src}>" )
set_source_files_properties( ${fn} PROPERTIES COMPILE_FLAGS ${flags} )
add_library( "${name}-${n}" SHARED EXCLUDE_FROM_ALL ${fn} )
list( APPEND libs ${name}-${n} )
endif()
endforeach( src )
update_file( ${file} "/* nothing */" )
add_executable( ${name} EXCLUDE_FROM_ALL ${file} )
target_link_libraries( ${name} ${libs} )
set_source_files_properties( ${file} PROPERTIES COMPILE_FLAGS ${flags} )
endfunction()
# Create a unit test driver for a bunch of header files. Syntax:
#
# bricks_unittest( driver_name header1 header2 ... )
#
# This will get you an executable target driver_name that you can run
# to run the testsuite.
function( bricks_unittest name )
set( flags "-UNDEBUG -DBRICK_UNITTEST_REG -DBRICK_UNITTEST_MAIN -include brick-unittest" )
bricks_make_runner( ${name} "${flags}" ${ARGN} )
endfunction()
function( bricks_benchmark name )
set( flags "-DBRICK_BENCHMARK_REG -DBRICK_BENCHMARK_MAIN -include brick-benchmark" )
bricks_make_runner( ${name} "${flags}" ${ARGN} )
target_link_libraries( ${name} rt )
endfunction()
# Register a target "test-bricks" that builds and runs all the unit tests
# included with bricks. Use test_bricks( directory_with_bricks ). Also note
# that if you write your own unit tests using brick-unittest, the tests of
# any bricks that you use in the tested units will be automatically included in
# your testsuite.
function( test_bricks dir )
include_directories( ${dir} )
add_definitions( ${ARGN} )
file( GLOB SRC "${dir}/brick-*[a-z0-9]" )
bricks_unittest( test-bricks ${SRC} )
target_link_libraries( test-bricks pthread ${BRICK_USED_LLVM_LIBS} )
endfunction()
function( benchmark_bricks dir )
include_directories( ${dir} )
file( GLOB SRC "${dir}/brick-*[a-z]" )
bricks_benchmark( benchmark-bricks ${SRC} )
target_link_libraries( benchmark-bricks pthread )
endfunction()
# Run feature checks and define -DBRICKS_* feature macros. You can use bricks
# without feature checks, but you may be missing some of the features that
# way. Calling this macro from your toplevel CMakeLists.txt is therefore a good
# idea.
macro( bricks_check_dirent )
include( CheckCXXSourceCompiles )
check_cxx_source_compiles(
"#include <dirent.h>
int main() {
struct dirent *d;
(void)d->d_type;
return 0;
}" BRICKS_HAVE_DIRENT_D_TYPE )
if ( BRICKS_HAVE_DIRENT_D_TYPE )
add_definitions( -DBRICKS_HAVE_DIRENT_D_TYPE )
endif()
endmacro()
macro( bricks_check_llvm )
find_package( LLVM )
if( LLVM_FOUND )
add_definitions( -DBRICKS_HAVE_LLVM -isystem ${LLVM_INCLUDE_DIRS} )
set( BRICK_USED_LLVM_LIBS ${BRICK_LLVM_LIBS} )
endif()
endmacro()
macro( bricks_check_features )
bricks_check_dirent()
bricks_check_llvm()
endmacro()
function( bricks_fetch_tbb )
include( ExternalProject )
ExternalProject_Add(
bricks-tbb-build
URL https://www.threadingbuildingblocks.org/sites/default/files/software_releases/source/tbb42_20140601oss_src.tgz
BUILD_COMMAND make
CONFIGURE_COMMAND :
BUILD_IN_SOURCE 1
INSTALL_COMMAND
sh -c "ln -fs build/*_release _release && ln -fs build/*_debug _debug" ;
&& sh -c "cd _debug && ln -s libtbb_debug.so libtbb.so" ;
&& sh -c "cd _debug && ln -s libtbbmalloc_debug.so libtbbmalloc.so" )
ExternalProject_Get_Property( bricks-tbb-build SOURCE_DIR BINARY_DIR )
if ( "${CMAKE_BUILD_TYPE}" STREQUAL "Debug" )
set( tbb_BINARY_DIR "${BINARY_DIR}/_debug" )
else()
set( tbb_BINARY_DIR "${BINARY_DIR}/_release" )
endif()
set( tbb_SOURCE_DIR ${SOURCE_DIR} PARENT_SCOPE )
set( tbb_BINARY_DIR ${tbb_BINARY_DIR} PARENT_SCOPE )
add_library( bricks-tbb UNKNOWN IMPORTED )
set_property( TARGET bricks-tbb PROPERTY IMPORTED_LOCATION
${tbb_BINARY_DIR}/libtbb.so )
add_dependencies( bricks-tbb bricks-tbb-build )
endfunction()
macro( bricks_use_tbb )
add_definitions( -DBRICKS_HAVE_TBB )
include_directories( ${tbb_SOURCE_DIR}/include )
link_directories( ${tbb_BINARY_DIR} )
link_libraries( bricks-tbb )
endmacro()