-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCMakeLists.txt
243 lines (204 loc) · 9.87 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
cmake_minimum_required( VERSION 3.5 FATAL_ERROR )
######### Meta information about the project
set( META_PROJECT_NAME "ovf" )
set( META_PROJECT_DESCRIPTION "OVF file parser" )
set( META_AUTHOR_DOMAIN "https://github.com/spirit-code/ovf" )
set( META_AUTHOR_MAINTAINER "Gideon Mueller" )
set( META_AUTHOR_EMAIL "[email protected]" )
set( META_VERSION_MAJOR "0" )
set( META_VERSION_MINOR "4" )
set( META_VERSION_PATCH "3" )
set( META_VERSION "${META_VERSION_MAJOR}.${META_VERSION_MINOR}.${META_VERSION_PATCH}" )
#############################################
project( ${META_PROJECT_NAME} VERSION ${META_VERSION} )
set( CMAKE_POSITION_INDEPENDENT_CODE ON )
#############################################
option( OVF_BUILD_PYTHON_BINDINGS "Build the shared library for Python." OFF )
option( OVF_BUILD_FORTRAN_BINDINGS "Build the module file for Fortran." OFF )
option( OVF_BUILD_TEST "Build unit tests for the ovf library." ON )
option( OVF_TEST_COVERAGE "Have unit tests output coverage info." OFF )
#############################################
set( SOURCE_FILES src/ovf.cpp )
set( HEADER_FILES
src/ovf.h
src/detail/helpers.hpp
src/detail/parse.hpp
src/detail/parse_rules.hpp
src/detail/write.hpp
)
if( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" )
add_definitions( -DNOMINMAX )
endif()
### flag for fmt to be header-only
add_definitions( -DFMT_HEADER_ONLY -DFMT_TEST=no )
######## GNU Compiler Collection - gcc ###############################
if( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" )
# Require at least gcc 7
if( OVF_BUILD_FORTRAN_BINDINGS AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7 )
message( FATAL_ERROR "GCC version must be at least 7! Older versions contain incompatible bugs for Fortran..." )
endif()
### Compiler Flags
set( CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g3" )
### Linker Flags
if( APPLE )
set( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-no_compact_unwind" )
endif()
######## Apple Clang #################################################
elseif( "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" )
### Compiler Flags
set( CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g3" )
### Linker Flags
if( APPLE )
set( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-no_compact_unwind" )
endif()
######## Intel Compiler - icc ########################################
elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Intel")
### Compiler Flags
set( CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g3" )
### Linker Flags
if( APPLE )
set( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-no_compact_unwind" )
endif()
endif()
######################################################################
######### Coverage ##########################
if( OVF_BUILD_TEST AND OVF_TEST_COVERAGE )
set( OVF_CXX_FLAGS_COVERAGE
${CMAKE_CXX_FLAGS_DEBUG}
--coverage
-fno-inline
-fno-inline-small-functions
-fno-default-inline )
string( REPLACE ";" " " OVF_CXX_FLAGS_COVERAGE "${OVF_CXX_FLAGS_COVERAGE}" )
set( OVF_LINK_FLAGS_COVERAGE "-lgcov --coverage" )
endif( )
#############################################
### Object library
add_library( ${PROJECT_NAME} OBJECT ${SOURCE_FILES} )
set_property( TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 11 )
set_property( TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD_REQUIRED ON )
set_property( TARGET ${PROJECT_NAME} PROPERTY CXX_EXTENSIONS OFF )
set_property( TARGET ${PROJECT_NAME} PROPERTY COMPILE_FLAGS ${OVF_CXX_FLAGS_COVERAGE} )
set_property( TARGET ${PROJECT_NAME} PROPERTY LINK_FLAGS ${OVF_LINK_FLAGS_COVERAGE} )
target_include_directories( ${PROJECT_NAME} PUBLIC ${PROJECT_SOURCE_DIR}/include )
target_include_directories( ${PROJECT_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/thirdparty/PEGTL/include )
### Static library
add_library( ${PROJECT_NAME}_static $<TARGET_OBJECTS:${PROJECT_NAME}> )
install( TARGETS ${PROJECT_NAME}_static DESTINATION lib )
install( DIRECTORY include/ovf DESTINATION include )
### Build Python bindings
if( OVF_BUILD_PYTHON_BINDINGS )
add_library( ${PROJECT_NAME}_python SHARED $<TARGET_OBJECTS:${PROJECT_NAME}> )
set_property( TARGET ${PROJECT_NAME}_python PROPERTY OUTPUT_NAME "${PROJECT_NAME}" )
if(WIN32)
# On Windows the runtime_output_directory is used https://cmake.org/cmake/help/v3.9/manual/cmake-buildsystem.7.html#output-artifacts
set_property( TARGET ${PROJECT_NAME}_python PROPERTY RUNTIME_OUTPUT_DIRECTORY_DEBUG ${PROJECT_SOURCE_DIR}/python/ovf ) # Use runtime_output_directory_debug to avoid an additional debug directory being created
set_property( TARGET ${PROJECT_NAME}_python PROPERTY RUNTIME_OUTPUT_DIRECTORY_RELEASE ${PROJECT_SOURCE_DIR}/python/ovf )
else()
set_property( TARGET ${PROJECT_NAME}_python PROPERTY LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/python/ovf )
endif()
set_property( TARGET ${PROJECT_NAME}_python PROPERTY LINK_FLAGS ${OVF_LINK_FLAGS_COVERAGE} )
### Utility python files
configure_file( ${PROJECT_SOURCE_DIR}/LICENSE.txt ${PROJECT_SOURCE_DIR}/python/LICENSE.txt COPYONLY )
configure_file( ${PROJECT_SOURCE_DIR}/CMake/__init__.py.in ${PROJECT_SOURCE_DIR}/python/ovf/__init__.py )
endif()
### Build Fortran bindings
if( OVF_BUILD_FORTRAN_BINDINGS )
enable_language( Fortran )
######## GNU Compiler Collection - gcc ###############################
if( "${CMAKE_Fortran_COMPILER_ID}" STREQUAL "GNU" )
set( CMAKE_Fortran_FLAGS_DEBUG "${CMAKE_Fortran_FLAGS_DEBUG} -g3 -fcheck=all" )
######## flang #######################################################
elseif( "${CMAKE_Fortran_COMPILER_ID}" MATCHES "Clang" )
set( CMAKE_Fortran_FLAGS_DEBUG "${CMAKE_Fortran_FLAGS_DEBUG} -g3 -fcheck=all" )
######## Intel Compiler - icc ########################################
elseif ("${CMAKE_Fortran_COMPILER_ID}" MATCHES "Intel")
set( CMAKE_Fortran_FLAGS_DEBUG "${CMAKE_Fortran_FLAGS} -g3 -traceback" )
endif()
# this is where we will place the Fortran module files
set( CMAKE_Fortran_MODULE_DIRECTORY ${PROJECT_SOURCE_DIR}/fortran )
add_library( ${PROJECT_NAME}_fortran STATIC ${PROJECT_SOURCE_DIR}/fortran/ovf.f90 )
target_link_libraries( ${PROJECT_NAME}_fortran ${PROJECT_NAME}_static )
# set_property(TARGET ${PROJECT_NAME}_fortran PROPERTY LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/fortran)
endif()
set( TEST_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR} )
set( TEST_EXECUTABLES )
### CXX test creation macro
macro( add_cxx_test testName testSrc )
# Executable
add_executable( ${testName} test/main.cpp ${PROJECT_SOURCE_DIR}/test/${testSrc} )
# Link Library
target_link_libraries( ${testName} ${PROJECT_NAME}_static )
# Properties
set_property( TARGET ${testName} PROPERTY RUNTIME_OUTPUT_DIRECTORY ${TEST_RUNTIME_OUTPUT_DIRECTORY} )
set_property( TARGET ${testName} PROPERTY CXX_STANDARD 11 )
set_property( TARGET ${testName} PROPERTY CXX_STANDARD_REQUIRED ON )
set_property( TARGET ${testName} PROPERTY CXX_EXTENSIONS OFF )
set_property( TARGET ${testName} PROPERTY COMPILE_FLAGS ${OVF_CXX_FLAGS_COVERAGE} )
set_property( TARGET ${testName} PROPERTY LINK_FLAGS ${OVF_CXX_FLAGS_COVERAGE} )
# Include Directories
target_include_directories( ${testName} PRIVATE ${PROJECT_SOURCE_DIR}/test )
target_include_directories( ${testName} PRIVATE ${PROJECT_SOURCE_DIR}/include )
# Add the test
add_test( NAME ${testName}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMAND ${testName} --use-colour=yes )
# Add to list
set( TEST_EXECUTABLES ${TEST_EXECUTABLES} ${testName} )
endmacro( add_cxx_test testName testSrc )
###
if ( OVF_BUILD_TEST )
MESSAGE( STATUS ">> Building unit tests for Spirit" )
### Enable CTest testing
enable_testing()
### Tests
add_cxx_test( test_cpp_simple simple.cpp )
add_cxx_test( test_cpp_binary binary.cpp )
endif()
### Python test creation macro
set( PYTHON_TEST_EXECUTABLES )
macro( add_python_test test_name src )
# Add the test
add_test(
NAME ${test_name}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMAND ${PYTHON_EXECUTABLE} "${PROJECT_SOURCE_DIR}/python/test/${src}" )
# Properties
set_tests_properties( ${test_name}
PROPERTIES ENVIRONMENT "PYTHONPATH=${SPIRIT_PYDIR}:$PYTHONPATH" )
# Add to list
set( PYTHON_TEST_EXECUTABLES ${PYTHON_TEST_EXECUTABLES} ${test_name} )
endmacro( add_python_test )
###
if( OVF_BUILD_TEST AND OVF_BUILD_PYTHON_BINDINGS )
MESSAGE( STATUS ">> Setting up unit tests for python bindings" )
# find_package( PythonInterp 2.7 REQUIRED )
find_package( PythonInterp REQUIRED )
### Tests
add_python_test( test_python_simple simple.py )
add_python_test( test_python_binary binary.py )
endif()
### Fortran test creation macro
macro( add_fortran_test testName testSrc )
# Executable
add_executable( ${testName} ${PROJECT_SOURCE_DIR}/fortran/test/${testSrc} )
# Link Library
target_link_libraries( ${testName} ${PROJECT_NAME}_fortran )
# Properties
set_property( TARGET ${testName} PROPERTY RUNTIME_OUTPUT_DIRECTORY ${TEST_RUNTIME_OUTPUT_DIRECTORY} )
set_property( TARGET ${testName} PROPERTY LINKER_LANGUAGE Fortran )
# Add the test
add_test( NAME ${testName}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMAND ${testName} --use-colour=yes )
# Add to list
set( TEST_EXECUTABLES ${TEST_EXECUTABLES} ${testName} )
endmacro( add_fortran_test testName testSrc )
###
if( OVF_BUILD_TEST AND OVF_BUILD_FORTRAN_BINDINGS )
MESSAGE( STATUS ">> Setting up unit tests for fortran bindings" )
### Tests
add_fortran_test( test_fortran_simple simple.f90 )
endif()
set( OVF_LIBRARIES ${PROJECT_NAME} PARENT_SCOPE )
set( OVF_LIBRARIES ${PROJECT_NAME}_static PARENT_SCOPE )