-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
323 lines (261 loc) · 12.5 KB
/
CMakeLists.txt
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
cmake_minimum_required (VERSION 3.0)
macro(print_all_variables)
message(STATUS "print_all_variables------------------------------------------{")
get_cmake_property(_variableNames VARIABLES)
foreach (_variableName ${_variableNames})
message(STATUS "${_variableName}=${${_variableName}}")
endforeach()
message(STATUS "print_all_variables------------------------------------------}")
endmacro()
project (fmi2AllocGuard LANGUAGES C)
#########################################
# global options
#########################################
option(FMI2AG_TEST "Build tests for fmi2AllocGuard (default: OFF)" OFF)
option(FMI2AG_START_ID "Start id <numeric>" -1)
option(FMI2AG_END_ID "End id <numeric>" -1)
#########################################
# pre build step
#########################################
set( FMI2AG_SCRIPT_DO_INVOKE OFF )
set( FMI2AG_SCRIPT_START_ID 1 )
set( FMI2AG_SCRIPT_END_ID 10 )
# should be the same defaults as the ones in the script
if ( FMI2AG_START_ID GREATER -1 )
message( STATUS "FMI2AG_START_ID parameter explicitly specified with value '${FMI2AG_START_ID}'" )
set( FMI2AG_SCRIPT_START_ID ${FMI2AG_START_ID} )
set(FMI2AG_SCRIPT_DO_INVOKE ON)
endif( FMI2AG_START_ID GREATER -1 )
if ( FMI2AG_END_ID GREATER -1 )
message( STATUS "FMI2AG_END_ID parameter explicitly specified with value '${FMI2AG_END_ID}'" )
set( FMI2AG_SCRIPT_END_ID ${FMI2AG_END_ID} )
set(FMI2AG_SCRIPT_DO_INVOKE ON)
endif( FMI2AG_END_ID GREATER -1 )
if ( FMI2AG_SCRIPT_DO_INVOKE )
message( STATUS "searching for 'python3' executable" )
find_program(FMI2AG_SCRIPT_PYTHON_EXE "python3")
if ( ${FMI2AG_SCRIPT_PYTHON_EXE} STRGREATER "" )
message( STATUS "found 'python3' executable at '${FMI2AG_SCRIPT_PYTHON_EXE}'")
message( STATUS "executing '${FMI2AG_SCRIPT_PYTHON_EXE} ${PROJECT_SOURCE_DIR}/scr/gen.py --start_id ${FMI2AG_SCRIPT_START_ID} --end_id ${FMI2AG_SCRIPT_END_ID}'" )
execute_process(
COMMAND "${FMI2AG_SCRIPT_PYTHON_EXE}" ${PROJECT_SOURCE_DIR}/scr/gen.py --start_id ${FMI2AG_SCRIPT_START_ID} --end_id ${FMI2AG_SCRIPT_END_ID}
RESULT_VARIABLE FMI2AG_SCRIPT_EXEC_RESULT )
if ( ${FMI2AG_SCRIPT_EXEC_RESULT} EQUAL 0 )
message( STATUS "boilerplate generating script returned '${FMI2AG_SCRIPT_EXEC_RESULT}'" )
else()
message( FATAL_ERROR "boilerplate generating script returned '${FMI2AG_SCRIPT_EXEC_RESULT}'" )
endif()
else( ${FMI2AG_SCRIPT_PYTHON_EXE} STRGREATER "" )
message( FATAL_ERROR "==== python executable was not found" )
endif( ${FMI2AG_SCRIPT_PYTHON_EXE} STRGREATER "" )
endif( FMI2AG_SCRIPT_DO_INVOKE )
#########################################
# global cmake options
#########################################
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_RULE_MESSAGES ON)
#########################################
# compiler options
#########################################
#set(CMAKE_CXX_STANDARD 98)
#set(CMAKE_CXX_STANDARD_REQUIRED ON)
#set(CMAKE_C_COMPILER "/usr/bin/clang")
# setting this, causes cmake to loop indefinitely
# ?? probably trying to find the suggested compiler ??
# to switch between gcc/clang, export env variable CC with compiler full path
if (${CMAKE_C_COMPILER_ID} STREQUAL "Clang")
message( STATUS "compiler id = 'Clang'" )
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -Wall -Wextra -pedantic -Wfloat-conversion" )
# -Wfloat-equal - cannot add this as MinUnit does it
elseif (${CMAKE_C_COMPILER_ID} STREQUAL "GNU")
message( STATUS "compiler id = 'GNU'" )
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -Wall -Wextra -pedantic -Wfloat-conversion" )
# -Wfloat-equal - cannot add this as MinUnit does it
elseif (${CMAKE_C_COMPILER_ID} STREQUAL "Intel")
message( STATUS "compiler id = 'Intel'" )
message( WARNING "Not tested with 'Intel' compiler!")
elseif (${CMAKE_C_COMPILER_ID} STREQUAL "MSVC")
message( STATUS "compiler id = 'MSVC'" )
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /TC /W4" )
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /O2" )
# /TC - treat all files as C
else()
message( STATUS "compiler not supported (not one of 'Clang', 'GNU' or 'MSVC')" )
endif()
#########################################
# library
#########################################
set(FMI2AG_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/inc")
set(FMI2AG_SOURCE_DIR "${PROJECT_SOURCE_DIR}/src")
#set_source_files_properties(${FMI2AG_SOURCE_DIR}/avl.c PROPERTIES LANGUAGE CXX )
#set_source_files_properties(${FMI2AG_SOURCE_DIR}/bookkeeping.c PROPERTIES LANGUAGE CXX )
#set_source_files_properties(${FMI2AG_SOURCE_DIR}/fmi2AllocGuard.c PROPERTIES LANGUAGE CXX )
set(FMI2AG_FILES_HEADERS_PUBLIC
${FMI2AG_INCLUDE_DIR}/fmi2Export.h
${FMI2AG_INCLUDE_DIR}/fmi2Defines.h
${FMI2AG_INCLUDE_DIR}/fmi2AllocGuard.h )
set(FMI2AG_FILES_HEADERS_PRIVATE
${FMI2AG_SOURCE_DIR}/avl.h
${FMI2AG_SOURCE_DIR}/bookkeeping.h )
set(FMI2AG_FILES_CODE
${FMI2AG_SOURCE_DIR}/avl.c
${FMI2AG_SOURCE_DIR}/bookkeeping.c
${FMI2AG_SOURCE_DIR}/fmi2AllocGuard.c )
add_library(fmi2AllocGuard SHARED ${FMI2AG_FILES_CODE} ${FMI2AG_FILES_HEADERS_PUBLIC} ${FMI2AG_FILES_HEADERS_PRIVATE} )
include_directories( ${FMI2AG_INCLUDE_DIR} )
# public includes
#add_subdirectory("${PROJECT_SOURCE_DIR}/src" src )
# needed because both .c and .cpp code files are present
#set_target_properties( fmi2AllocGuard PROPERTIES LINKER_LANGUAGE C)
# change the defined exports symbol - see inc/export.h
set_target_properties( fmi2AllocGuard PROPERTIES DEFINE_SYMBOL "FMI2ALLOCGUARD_DLL_EXPORTS")
get_target_property( fmi2AllocGuard_target_type fmi2AllocGuard TYPE )
if( fmi2AllocGuard_target_type STREQUAL "SHARED_LIBRARY" )
# needed to allow both shared and static lib build
# for now, we're only using shared but this can be ammended above
add_definitions( -DFMI2ALLOCGUARD_DLL )
endif()
# by default, compilation is in Release mode
add_definitions( -DCMAKE_BUILD_TYPE=Debug )
# NOTE: to compile C-only:
# - specify language "C" when declaring project
# - remove compiler arguments that do not make sense for C (for GCC, you
# get a warning, other compilers might not be so tolerant)
# - add property "LINKER_LANGUAGE" with "C"
#########################################
# unit testing
#########################################
if ( FMI2AG_TEST )
set(FMI2AG_TEST_CATCH OFF)
# not using Catch2; it requires C++ and we want to make do with C only
set(FMI2AG_TEST_MINUNIT ON)
# this is C only
endif()
if ( FMI2AG_TEST )
enable_testing()
include(ExternalProject)
# the location of the test sources
set(FMI2AG_TST_DIR "${PROJECT_SOURCE_DIR}/tst")
if ( FMI2AG_TEST_CATCH )
set(FMI2AG_FETCH_CATCH
GIT_REPOSITORY https://github.com/catchorg/Catch2
GIT_TAG c32f275a008a50ae058fcd8a43f0c387d90c929b )
# fetching Catch1.x branch
ExternalProject_Add(
Catch-External
PREFIX ${CMAKE_BINARY_DIR}/external/Catch1.x
${FMI2AG_FETCH_CATCH}
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/external/Catch1.x/src/Catch-External/single_include/catch.hpp
${CMAKE_BINARY_DIR}/external/Catch/include/catch.hpp
)
add_library(Catch INTERFACE)
add_dependencies(Catch Catch-External)
target_include_directories(Catch INTERFACE ${CMAKE_BINARY_DIR}/external/Catch/include)
#
# testCatchFmi2AllocGuard
#
## test code must be able to access implementation detail headers
## .. or it can use relative paths
##include_directories("${PROJECT_SOURCE_DIR}/src")
#set ( FMI2AG_TEST_FILES_CATCH
# ${FMI2AG_TST_DIR}/testMain.cpp
# ${FMI2AG_TST_DIR}/test01_StaticInit.cpp
# ${FMI2AG_TST_DIR}/test03_GuardedBookkeeping.cpp )
# #${FMI2AG_TST_DIR}/test02_AllocGuard.cpp
##file(GLOB_RECURSE unit_test_sources "${FMI2AG_TST_DIR}/*.c" "${FMI2AG_TST_DIR}/*.cpp")
#add_executable(testCatchFmi2AllocGuard ${FMI2AG_TEST_FILES_CATCH})
#target_link_libraries(testCatchFmi2AllocGuard PUBLIC Catch)
#target_link_libraries(testCatchFmi2AllocGuard PUBLIC fmi2AllocGuard)
#target_include_directories(testCatchFmi2AllocGuard PUBLIC ${FMI2AG_TST_DIR})
###add_subdirectory(${PROJECT_SOURCE_DIR}/lib/Catch2)
###find_package(Catch2 REQUIRED)
### needed because both .c and .cpp code files are present
###set_target_properties( testCatchFmi2AllocGuard PROPERTIES LINKER_LANGUAGE C)
###set_source_files_properties("${PROJECT_SOURCE_DIR}/tst/*.cpp" PROPERTIES LANGUAGE C )
#target_compile_options(testCatchFmi2AllocGuard PUBLIC -Wall -Wfloat-conversion -pedantic -Wextra)
#add_test( NAME TestStaticInit COMMAND testCatchFmi2AllocGuard TestStaticInit )
##add_test( NAME TestAllocGuard COMMAND testCatchFmi2AllocGuard TestAllocGuard )
#add_test( NAME TestGuardedBookkeeping COMMAND testCatchFmi2AllocGuard TestGuardedBookkeeping )
endif()
if ( FMI2AG_TEST_MINUNIT )
enable_language( C )
# enable the C compiler for the following project
# not sure whether this option is restricted to this scope or
# whether it propagates further on as well
set(FMI2AG_FETCH_MINUNIT
GIT_REPOSITORY https://github.com/cristiarg/minunit.git
GIT_TAG 7bb647614d0ca1644f2e5a620a6e7583edddb4a8 )
ExternalProject_Add(
MinUnit-External
PREFIX ${CMAKE_BINARY_DIR}/external/MinUnit
${FMI2AG_FETCH_MINUNIT}
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/external/MinUnit/src/MinUnit-External/minunit.h
${CMAKE_BINARY_DIR}/external/MinUnit/include/minunit.h
)
add_library(MinUnit INTERFACE)
add_dependencies(MinUnit MinUnit-External)
target_include_directories(MinUnit INTERFACE ${CMAKE_BINARY_DIR}/external/MinUnit/include)
# this will clone the MinUnit repo and also, sort of, install it in an arbitrary location
# I'm sure this process can be optimized
#
# testAVL
#
#set_source_files_properties("${FMI2AG_TST_DIR}/test11_avl.c" PROPERTIES LANGUAGE C )
set ( FMI2AG_TEST_FILES_AVL
${FMI2AG_SOURCE_DIR}/avl.c
${FMI2AG_TST_DIR}/test11_avl.c)
#add_definitions(-DFMI2AG_COMPILE_FOR_UNITTEST)
# needed to allow 'extern "C"' definition for unit testing
add_executable(testAVL ${FMI2AG_TEST_FILES_AVL})
target_link_libraries(testAVL PRIVATE MinUnit)
#target_link_libraries(testAVL PRIVATE fmi2AllocGuard)
#target_include_directories(testAVL PUBLIC ${FMI2AG_TST_DIR})
#set_target_properties( testAVL PROPERTIES LINKER_LANGUAGE C)
add_test( NAME TestAVL COMMAND testAVL )
#
# testAllocGuard
#
set ( FMI2AG_TEST_FILES_ALLOCGUARD
${FMI2AG_TST_DIR}/test12_AllocGuard.c )
add_executable( testAllocGuard ${FMI2AG_TEST_FILES_ALLOCGUARD} )
target_link_libraries( testAllocGuard PRIVATE MinUnit )
target_link_libraries( testAllocGuard PRIVATE fmi2AllocGuard )
add_test( NAME TestAllocGuard COMMAND testAllocGuard )
#
# testGuardedBookkeeping
#
set ( FMI2AG_TEST_FILES_GUARDEDBOOKKEEPING
${FMI2AG_SOURCE_DIR}/avl.c
${FMI2AG_SOURCE_DIR}/bookkeeping.c
${FMI2AG_TST_DIR}/test03_GuardedBookkeeping.c )
add_executable(testGuardedBookkeeping ${FMI2AG_TEST_FILES_GUARDEDBOOKKEEPING} )
target_link_libraries( testGuardedBookkeeping PRIVATE MinUnit )
#target_link_libraries( testGuardedBookkeeping PRIVATE fmi2AllocGuard )
add_test( NAME TestGuardedBookkeeping COMMAND testGuardedBookkeeping )
#
# testLoadSharedLib
#
set ( FMI2AG_TEST_FILES_LOADSHAREDLIB
${FMI2AG_TST_DIR}/test02_dlopen.c )
add_executable(testLoadSharedLib ${FMI2AG_TEST_FILES_LOADSHAREDLIB} )
target_link_libraries( testLoadSharedLib PRIVATE MinUnit )
if(UNIX)
target_link_libraries( testLoadSharedLib PRIVATE dl )
endif(UNIX)
add_test( NAME TestLoadSharedLib COMMAND testLoadSharedLib )
#
# testUseSharedLib
#
set ( FMI2AG_TEST_FILES_USESHAREDLIB
${FMI2AG_TST_DIR}/test04_UseSharedLib.c )
add_executable(testUseSharedLib ${FMI2AG_TEST_FILES_USESHAREDLIB} )
target_link_libraries( testUseSharedLib PRIVATE MinUnit )
target_link_libraries( testUseSharedLib PRIVATE fmi2AllocGuard )
add_test( NAME TestUseSharedLib COMMAND testUseSharedLib )
endif()
endif()
#print_all_variables()