-
Notifications
You must be signed in to change notification settings - Fork 11
/
CMakeLists.txt
209 lines (186 loc) · 7.99 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
cmake_minimum_required(VERSION 3.19)
file(STRINGS "${CMAKE_SOURCE_DIR}/version.txt" projectVersion)
project("libcosim" VERSION ${projectVersion})
message("Current libcosim version: ${CMAKE_PROJECT_VERSION}\n")
# ==============================================================================
# Build settings
# ==============================================================================
option(BUILD_SHARED_LIBS "Build shared libraries instead of static libraries" ON)
option(LIBCOSIM_TREAT_WARNINGS_AS_ERRORS "Treat compiler warnings as errors" ON)
option(LIBCOSIM_BUILD_TESTS "Build test suite" ON)
option(LIBCOSIM_BUILD_APIDOC "Build API documentation (requires Doxygen)" ON)
option(LIBCOSIM_BUILD_PRIVATE_APIDOC "Build private API documentation (only used if LIBCOSIM_BUILD_APIDOC=ON)" OFF)
option(LIBCOSIM_STANDALONE_INSTALLATION "Whether to build for a standalone installation (Linux only; sets a relative RPATH)" OFF)
option(LIBCOSIM_WITH_PROXYFMU "Whether or not to build with proxy-fmu integration" OFF)
option(LIBCOSIM_NO_FMI_LOGGING "Disable FMI logging during simulation" OFF)
# ==============================================================================
# Global internal configuration
# ==============================================================================
# Configure logging in the fmi callback function
if(LIBCOSIM_NO_FMI_LOGGING)
add_compile_definitions(LIBCOSIM_NO_FMI_LOGGING=1)
endif()
# Our custom CMake scripts go in the cmake/ subdirectory.
list(INSERT CMAKE_MODULE_PATH 0 "${CMAKE_SOURCE_DIR}/cmake")
# Use a common output directory for all targets.
# The main purpose of this is to ensure that the DLLs and test executables
# end up in the same directory on Windows, so that the OS finds the former
# when running the latter.
if(CMAKE_CONFIGURATION_TYPES)
set(configTypes ${CMAKE_CONFIGURATION_TYPES})
else()
set(configTypes Debug Release RelWithDebInfo MinSizeRel)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release")
endif()
endif()
foreach(c IN LISTS configTypes)
string(TOLOWER "${c}" configL)
string(TOUPPER "${c}" configU)
set(outputDir "${CMAKE_BINARY_DIR}/output/${configL}")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${configU} "${outputDir}/lib")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_${configU} "${outputDir}/lib")
set(CMAKE_PDB_OUTPUT_DIRECTORY_${configU} "${outputDir}/lib")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${configU} "${outputDir}/bin")
endforeach()
# Use the highest warning levels and treat all warnings as errors,
# but ignore a few selected warnings.
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
add_compile_options("-Wall" "-Wextra" "-Wpedantic")
add_compile_options("-Wno-parentheses")
if(LIBCOSIM_TREAT_WARNINGS_AS_ERRORS)
add_compile_options("-Werror")
endif()
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
add_compile_options("/W4")
if(CMAKE_GENERATOR MATCHES "NMake Makefiles.*")
string(REPLACE "/W3" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
endif()
add_compile_options("/wd4251" "/wd4996")
if(LIBCOSIM_TREAT_WARNINGS_AS_ERRORS)
add_compile_options("/WX")
endif()
add_definitions("-D_SCL_SECURE_NO_WARNINGS" "-D_CRT_SECURE_NO_WARNINGS")
endif()
# Automatically export all symbols in Windows DLLs.
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
# Organise projects in folders in IDEs that support it
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
# Prepare for export and installation.
set(LIBCOSIM_HEADER_INSTALL_DIR "include")
if(WIN32)
set(LIBCOSIM_CMAKE_INSTALL_DIR "cmake")
set(LIBCOSIM_DOC_INSTALL_DIR "doc")
else()
set(LIBCOSIM_CMAKE_INSTALL_DIR "share/${PROJECT_NAME}/cmake")
set(LIBCOSIM_DOC_INSTALL_DIR "share/doc/${PROJECT_NAME}")
endif()
set(LIBCOSIM_INSTALL_DESTINATIONS
ARCHIVE DESTINATION "lib"
LIBRARY DESTINATION "lib"
RUNTIME DESTINATION "bin"
INCLUDES DESTINATION "${LIBCOSIM_HEADER_INSTALL_DIR}")
set(LIBCOSIM_EXPORT_TARGET "${PROJECT_NAME}-targets")
# ==============================================================================
# Dependencies
# ==============================================================================
set(Boost_components date_time log)
if (LIBCOSIM_BUILD_TESTS)
list(APPEND Boost_components timer unit_test_framework)
endif()
if(NOT BUILD_SHARED_LIBS AND NOT DEFINED Boost_USE_STATIC_LIBS)
set(Boost_USE_STATIC_LIBS ON)
endif()
find_package(Boost REQUIRED COMPONENTS ${Boost_components})
if(BUILD_SHARED_LIBS AND NOT DEFINED FMILibrary_USE_SHARED_LIB)
set(FMILibrary_USE_SHARED_LIB TRUE)
endif()
find_package(FMILibrary MODULE REQUIRED)
find_package(libzip REQUIRED)
find_package(Microsoft.GSL REQUIRED)
find_package(yaml-cpp REQUIRED)
find_package(XercesC MODULE REQUIRED)
if(LIBCOSIM_WITH_PROXYFMU)
find_package(PROXYFMU CONFIG REQUIRED)
endif()
# ==============================================================================
# Targets
# ==============================================================================
add_subdirectory("src")
if(LIBCOSIM_BUILD_TESTS)
enable_testing()
add_subdirectory("tests")
endif()
# ==============================================================================
# API documentation
# ==============================================================================
if(LIBCOSIM_BUILD_APIDOC)
find_package(Doxygen REQUIRED)
set(DOXYGEN_PROJECT_NAME "libcosim")
set(DOXYGEN_PROJECT_BRIEF "C++ library for distributed co-simulation")
set(DOXYGEN_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/output/doc")
set(DOXYGEN_GENERATE_LATEX "NO")
set(DOXYGEN_RECURSIVE "YES")
set(DOXYGEN_SORT_BY_SCOPE_NAME "YES")
set(DOXYGEN_STRIP_FROM_INC_PATH "${CMAKE_SOURCE_DIR}/include")
set(DOXYGEN_STRIP_FROM_PATH "${CMAKE_SOURCE_DIR}/include")
set(DOXYGEN_TAGFILES "${CMAKE_SOURCE_DIR}/cmake/cppreference-doxygen-web.tag.xml=http://en.cppreference.com/w/")
set(DOXYGEN_JAVADOC_AUTOBRIEF "YES")
set(DOXYGEN_QT_AUTOBRIEF "YES")
set(DOXYGEN_MULTILINE_CPP_IS_BRIEF "YES")
set(DOXYGEN_EXCLUDE_PATTERNS "*.cpp" "*.c")
set(doxygenInputs "${CMAKE_SOURCE_DIR}/docs" "${CMAKE_SOURCE_DIR}/include")
if(LIBCOSIM_BUILD_PRIVATE_APIDOC)
list(APPEND doxygenInputs
"${CMAKE_SOURCE_DIR}/src/cpp"
)
list(APPEND DOXYGEN_STRIP_FROM_INC_PATH
"${CMAKE_SOURCE_DIR}/src/cpp"
)
list(APPEND DOXYGEN_STRIP_FROM_PATH "${CMAKE_SOURCE_DIR}")
endif()
doxygen_add_docs(doc ${doxygenInputs})
add_custom_target(
install-doc
"${CMAKE_COMMAND}" "-E" "copy_directory"
"${DOXYGEN_OUTPUT_DIRECTORY}"
"${CMAKE_INSTALL_PREFIX}/${LIBCOSIM_DOC_INSTALL_DIR}/"
DEPENDS doc
)
endif()
# ==============================================================================
# Exports and remaining installation
# ==============================================================================
install(
FILES "README.md" "LICENSE"
DESTINATION "${LIBCOSIM_DOC_INSTALL_DIR}"
)
install(
EXPORT "${LIBCOSIM_EXPORT_TARGET}"
DESTINATION "${LIBCOSIM_CMAKE_INSTALL_DIR}"
NAMESPACE "${PROJECT_NAME}::"
)
include(CMakePackageConfigHelpers)
# Generate and install package-config file.
set(configFile "${CMAKE_BINARY_DIR}/${PROJECT_NAME}-config.cmake")
set(targetsFile "${LIBCOSIM_CMAKE_INSTALL_DIR}/${LIBCOSIM_EXPORT_TARGET}.cmake")
configure_package_config_file(
"${CMAKE_SOURCE_DIR}/cmake/project-config.cmake.in"
"${configFile}"
INSTALL_DESTINATION "${LIBCOSIM_CMAKE_INSTALL_DIR}"
PATH_VARS targetsFile
)
install(FILES "${configFile}" DESTINATION "${LIBCOSIM_CMAKE_INSTALL_DIR}")
# Generate and install package-version file
set(versionFile "${CMAKE_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake")
write_basic_package_version_file(
"${versionFile}"
VERSION "${PROJECT_VERSION}"
COMPATIBILITY "SameMajorVersion")
install(FILES "${versionFile}" DESTINATION "${LIBCOSIM_CMAKE_INSTALL_DIR}")
# Install custom find modules
install(FILES
"${CMAKE_SOURCE_DIR}/cmake/FindFMILibrary.cmake"
DESTINATION
"${LIBCOSIM_CMAKE_INSTALL_DIR}"
)