forked from chemfiles/chemfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
197 lines (163 loc) · 7.28 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
cmake_minimum_required(VERSION 2.8.12)
set(CMAKE_USER_MAKE_RULES_OVERRIDE "${CMAKE_CURRENT_SOURCE_DIR}/cmake/InitRules.cmake")
project(chemfiles C CXX)
set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${PROJECT_SOURCE_DIR}/cmake")
if (POLICY CMP0042)
cmake_policy(SET CMP0042 NEW) # OSX RPATH handling
endif()
if (POLICY CMP0054)
# Only interpret ``if()`` arguments as variables or keywords when unquoted.
cmake_policy(SET CMP0054 NEW)
endif()
if (POLICY CMP0063)
# Use of `<LANG>_VISIBILITY_PRESET` in OBJECT libraries
cmake_policy(SET CMP0063 NEW)
endif()
file(READ ${PROJECT_SOURCE_DIR}/VERSION CHEMFILES_VERSION)
string(STRIP ${CHEMFILES_VERSION} CHEMFILES_VERSION)
string(REGEX REPLACE "^([0-9]+)\\..*" "\\1" CHEMFILES_VERSION_MAJOR "${CHEMFILES_VERSION}")
string(REGEX REPLACE "^[0-9]+\\.([0-9]+).*" "\\1" CHEMFILES_VERSION_MINOR "${CHEMFILES_VERSION}")
string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" CHEMFILES_VERSION_PATCH "${CHEMFILES_VERSION}")
set(CHEMFILES_VERSION_SHORT "${CHEMFILES_VERSION_MAJOR}.${CHEMFILES_VERSION_MINOR}.${CHEMFILES_VERSION_PATCH}")
option(BUILD_SHARED_LIBS "Build shared libraries instead of static ones" OFF)
option(CHFL_BUILD_TESTS "Build unit tests." OFF)
option(CHFL_BUILD_DOCUMENTATION "Build the documentation." OFF)
option(CHFL_USE_WARNINGS "Compile the code with warnings (default in debug mode)" OFF)
option(CHFL_USE_CLANG_TIDY "Compile the code with clang-tidy warnings" OFF)
option(CHFL_USE_INCLUDE_WHAT_YOU_USE "Compile the code with include-what-you-use warnings" OFF)
option(CHFL_SYSTEM_NETCDF "Use the system NetCDF instead of the internal one" OFF)
option(CHFL_SYSTEM_ZLIB "Use the system zlib instead of the internal one" OFF)
option(CHFL_SYSTEM_LZMA "Use the system lzma instead of the internal one" OFF)
option(CHFL_BUILD_DOCTESTS "Build documentation tests as well as unit tests." ON)
mark_as_advanced(CHFL_BUILD_DOCTESTS)
# Set a default build type if none was specified
if (${CMAKE_CURRENT_SOURCE_DIR} STREQUAL ${CMAKE_SOURCE_DIR})
if("${CMAKE_BUILD_TYPE}" STREQUAL "" AND "${CMAKE_CONFIGURATION_TYPES}" STREQUAL "")
message(STATUS "Setting build type to 'release' as none was specified.")
set(CMAKE_BUILD_TYPE "release"
CACHE STRING
"Choose the type of build, options are: none(CMAKE_CXX_FLAGS or CMAKE_C_FLAGS used) debug release relwithdebinfo minsizerel."
FORCE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS release debug relwithdebuginfo minsizerel none)
endif()
endif()
include(CompilerFlags)
include(ClangTidy)
include(IncludeWhatYouUse)
if(${CHFL_USE_WARNINGS})
# Make sure to have asserts compiled
add_definitions("-UNDEBUG")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CHEMFILES_CXX_WARNINGS}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CHEMFILES_C_WARNINGS}")
endif()
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${CHEMFILES_CXX_WARNINGS}")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${CHEMFILES_C_WARNINGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CHEMFILES_SANITIZERS}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CHEMFILES_SANITIZERS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${CHEMFILES_SANITIZERS}")
if(WIN32)
add_definitions("-DCHEMFILES_WINDOWS")
endif()
if(MSVC)
add_definitions("-D_CRT_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS")
endif()
add_subdirectory(external)
# We need to use a separated library for non-dll-exported classes that have an
# unit test. The chemfiles_objects OBJECT library contains the code for files
# implementation and is linked into both the main chemfiles library and the
# unit tests.
file(GLOB_RECURSE chemfiles_objects_sources src/**.cpp)
add_library(chemfiles_objects OBJECT ${chemfiles_objects_sources})
target_include_directories(chemfiles_objects SYSTEM BEFORE PRIVATE ${EXTERNAL_INCLUDES})
target_include_directories(chemfiles_objects SYSTEM BEFORE PRIVATE ${ZLIB_INCLUDE_DIRS} ${LIBLZMA_INCLUDE_DIRS})
target_include_directories(chemfiles_objects PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
)
target_include_directories(chemfiles_objects SYSTEM BEFORE PRIVATE ${EXTERNAL_INCLUDES})
target_compile_definitions(chemfiles_objects PRIVATE chemfiles_EXPORTS)
if(NOT ${CHFL_SYSTEM_LZMA})
target_compile_definitions(chemfiles_objects PRIVATE LZMA_API_STATIC)
endif()
if(${BUILD_SHARED_LIBS})
set_target_properties(chemfiles_objects PROPERTIES POSITION_INDEPENDENT_CODE TRUE)
endif()
target_use_clang_tidy(chemfiles_objects)
target_use_include_what_you_use(chemfiles_objects)
set(CHEMFILES_OBJECTS
$<TARGET_OBJECTS:chemfiles_objects>
$<TARGET_OBJECTS:molfiles>
$<TARGET_OBJECTS:tng_io>
$<TARGET_OBJECTS:fmt>
$<TARGET_OBJECTS:pugixml>
${ZLIB_OBJECTS}
${LZMA_OBJECTS}
${NETCDF_OBJECTS}
)
# Add the main chemfiles library
add_library(chemfiles ${CHEMFILES_OBJECTS})
set_property(TARGET chemfiles PROPERTY VERSION ${CHEMFILES_VERSION_SHORT})
set_property(TARGET chemfiles PROPERTY SOVERSION ${CHEMFILES_VERSION_SHORT})
target_include_directories(chemfiles PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
$<INSTALL_INTERFACE:include>
)
target_link_libraries(chemfiles ${NETCDF_LIBRARIES} ${ZLIB_LIBRARIES} ${LIBLZMA_LIBRARY})
if(WIN32)
target_link_libraries(chemfiles ws2_32)
endif()
configure_file (
"${PROJECT_SOURCE_DIR}/include/chemfiles/config.in.h"
"${PROJECT_BINARY_DIR}/include/chemfiles/config.h"
)
include(GenerateExportHeader)
generate_export_header(chemfiles
BASE_NAME CHFL
EXPORT_FILE_NAME ${PROJECT_BINARY_DIR}/include/chemfiles/exports.h
)
#----------------------------------------------------------------------------------------#
# Installation configuration
#----------------------------------------------------------------------------------------#
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/chemfiles-config-version.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/chemfiles-config-version.cmake"
@ONLY
)
install(TARGETS chemfiles
EXPORT chemfiles-targets
LIBRARY DESTINATION lib COMPONENT Runtime
ARCHIVE DESTINATION lib COMPONENT Development
RUNTIME DESTINATION bin COMPONENT Runtime
PUBLIC_HEADER DESTINATION include COMPONENT Development
BUNDLE DESTINATION bin COMPONENT Runtime
)
include(CMakePackageConfigHelpers)
configure_package_config_file(
"${PROJECT_SOURCE_DIR}/cmake/chemfiles-config.cmake.in"
"${PROJECT_BINARY_DIR}/chemfiles-config.cmake"
INSTALL_DESTINATION lib/cmake/chemfiles
)
install(EXPORT chemfiles-targets DESTINATION lib/cmake/chemfiles)
install(FILES "${PROJECT_BINARY_DIR}/chemfiles-config-version.cmake"
"${PROJECT_BINARY_DIR}/chemfiles-config.cmake"
DESTINATION lib/cmake/chemfiles)
install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/ DESTINATION include)
install(DIRECTORY ${PROJECT_BINARY_DIR}/include/ DESTINATION include)
# uninstall target
configure_file(
"${PROJECT_SOURCE_DIR}/cmake/uninstall.in.cmake"
"${PROJECT_BINARY_DIR}/uninstall.cmake"
@ONLY
)
add_custom_target(uninstall
COMMAND ${CMAKE_COMMAND} -P ${PROJECT_BINARY_DIR}/uninstall.cmake
)
if(${CHFL_BUILD_DOCUMENTATION})
add_subdirectory(doc)
endif()
enable_testing()
if(${CHFL_BUILD_TESTS})
add_subdirectory(tests)
add_subdirectory(examples)
endif()