-
Notifications
You must be signed in to change notification settings - Fork 63
/
CMakeLists.txt
282 lines (225 loc) · 8.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
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
cmake_minimum_required (VERSION 3.17)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
FUNCTION(cat IN_FILE OUT_FILE)
file(READ ${IN_FILE} CONTENTS)
file(APPEND ${OUT_FILE} "${CONTENTS}")
ENDFUNCTION()
project (Reference-FMUs)
set(FMI_VERSION 2 CACHE STRING "FMI Version")
set_property(CACHE FMI_VERSION PROPERTY STRINGS 1 2 3)
set(FMI_TYPE ME CACHE STRING "FMI Type (FMI 1.0 only)")
set_property(CACHE FMI_TYPE PROPERTY STRINGS ME CS)
if (${FMI_VERSION} GREATER 1)
set(FMI_TYPE "")
endif ()
set(FMI_ARCHITECTURE "" CACHE STRING "FMI Architecture")
set_property(CACHE FMI_ARCHITECTURE PROPERTY STRINGS "" "aarch64" "x86" "x86_64")
if (NOT FMI_ARCHITECTURE)
if (${CMAKE_HOST_SYSTEM_PROCESSOR} MATCHES "AMD64|x86_64")
set(FMI_ARCHITECTURE "x86_64")
elseif (${CMAKE_HOST_SYSTEM_PROCESSOR} MATCHES "aarch64")
set(FMI_ARCHITECTURE "aarch64")
else ()
message(FATAL_ERROR "Unknown System Architecture: ${CMAKE_SYSTEM_PROCESSOR}")
endif ()
endif ()
set(WITH_FMUSIM OFF CACHE BOOL "Add fmusim project")
if (MSVC)
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
# Minimum supported MSVC version is 1800 = Visual Studio 12.0/2013
# See also https://cmake.org/cmake/help/latest/variable/MSVC_VERSION.html
if (MSVC_VERSION VERSION_LESS 1800)
message (SEND_ERROR "MSVC_VERSION ${MSVC_VERSION} is not a supported Visual Studio compiler version. Please use Visual Studio 2013 or any later version.")
endif ()
endif ()
# hide internal functions
if (UNIX AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang|AppleClang")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden")
endif()
if (${FMI_VERSION} GREATER 2)
if (WIN32)
set(FMI_PLATFORM "${FMI_ARCHITECTURE}-windows")
elseif (APPLE)
set(FMI_PLATFORM "${FMI_ARCHITECTURE}-darwin")
else ()
set(FMI_PLATFORM "${FMI_ARCHITECTURE}-linux")
endif ()
else ()
if (WIN32)
set(FMI_PLATFORM win)
elseif (APPLE)
set(FMI_PLATFORM darwin)
else ()
set(FMI_PLATFORM linux)
endif ()
if ("${FMI_ARCHITECTURE}" STREQUAL "x86_64")
set (FMI_PLATFORM ${FMI_PLATFORM}64)
elseif ("${FMI_ARCHITECTURE}" STREQUAL "x86")
set (FMI_PLATFORM ${FMI_PLATFORM}32)
else ()
message(FATAL_ERROR "Unsupported architecture for FMI version < 3.0: ${FMI_ARCHITECTURE}")
endif ()
endif ()
if (APPLE)
if ("${FMI_ARCHITECTURE}" STREQUAL "x86_64")
set(CMAKE_OSX_ARCHITECTURES "x86_64")
elseif ("${FMI_ARCHITECTURE}" STREQUAL "aarch64")
set(CMAKE_OSX_ARCHITECTURES "arm64")
else ()
message(FATAL_ERROR "Unsupported architecture darwin: ${FMI_ARCHITECTURE}")
endif ()
endif ()
message("FMI_PLATFORM: " ${FMI_PLATFORM})
if (${FMI_VERSION} LESS 3)
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/all.c" "#define FMI_VERSION ${FMI_VERSION}
#include \"fmi${FMI_VERSION}Functions.c\"
#include \"model.c\"
#include \"cosimulation.c\"
")
endif ()
set (MODEL_NAMES BouncingBall Dahlquist Stair Feedthrough VanDerPol)
if (${FMI_VERSION} GREATER 1 OR "${FMI_TYPE}" STREQUAL "CS")
set (MODEL_NAMES ${MODEL_NAMES} Resource)
endif ()
if (${FMI_VERSION} GREATER 2)
set (MODEL_NAMES ${MODEL_NAMES} StateSpace Clocks)
endif ()
foreach (MODEL_NAME ${MODEL_NAMES})
set(TARGET_NAME ${MODEL_NAME})
SET(HEADERS
${MODEL_NAME}/config.h
include/cosimulation.h
include/model.h
)
if (${FMI_VERSION} EQUAL 2)
SET(HEADERS
${HEADERS}
include/fmi2Functions.h
include/fmi2FunctionTypes.h
include/fmi2TypesPlatform.h
)
elseif (${FMI_VERSION} EQUAL 3)
SET(HEADERS
${HEADERS}
include/fmi3Functions.h
include/fmi3FunctionTypes.h
include/fmi3PlatformTypes.h
)
endif()
SET(SOURCES
${MODEL_NAME}/model.c
src/fmi${FMI_VERSION}Functions.c
src/cosimulation.c
)
add_library(${TARGET_NAME} SHARED
${HEADERS}
${SOURCES}
${MODEL_NAME}/FMI${FMI_VERSION}${FMI_TYPE}.xml
${MODEL_NAME}/buildDescription.xml
)
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/fmus)
target_compile_definitions(${TARGET_NAME} PRIVATE
FMI_VERSION=${FMI_VERSION}
DISABLE_PREFIX
)
if (MSVC)
target_compile_options(${TARGET_NAME} PRIVATE /W4 /WX)
else()
target_compile_options(${TARGET_NAME} PRIVATE -Wall -Wextra -Wpedantic -Werror)
endif()
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
target_link_options(${TARGET_NAME} PRIVATE "-static-libgcc")
endif()
if (CMAKE_C_COMPILER_ID STREQUAL "Intel")
target_link_options(${TARGET_NAME} PRIVATE "-static-intel" "-static-libgcc")
endif()
if (${FMI_VERSION} EQUAL 1 AND "${FMI_TYPE}" STREQUAL CS)
target_compile_definitions(${TARGET_NAME} PRIVATE FMI_COSIMULATION)
endif()
target_include_directories(${TARGET_NAME} PRIVATE
include
${MODEL_NAME}
)
set(FMU_BUILD_DIR temp/${MODEL_NAME})
set_target_properties(${TARGET_NAME} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${FMU_BUILD_DIR}/binaries/${FMI_PLATFORM}"
RUNTIME_OUTPUT_DIRECTORY_DEBUG "${FMU_BUILD_DIR}/binaries/${FMI_PLATFORM}"
RUNTIME_OUTPUT_DIRECTORY_RELEASE "${FMU_BUILD_DIR}/binaries/${FMI_PLATFORM}"
LIBRARY_OUTPUT_DIRECTORY "${FMU_BUILD_DIR}/binaries/${FMI_PLATFORM}"
LIBRARY_OUTPUT_DIRECTORY_DEBUG "${FMU_BUILD_DIR}/binaries/${FMI_PLATFORM}"
LIBRARY_OUTPUT_DIRECTORY_RELEASE "${FMU_BUILD_DIR}/binaries/${FMI_PLATFORM}"
ARCHIVE_OUTPUT_DIRECTORY "${FMU_BUILD_DIR}/binaries/${FMI_PLATFORM}"
ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${FMU_BUILD_DIR}/binaries/${FMI_PLATFORM}"
ARCHIVE_OUTPUT_DIRECTORY_RELEASE "${FMU_BUILD_DIR}/binaries/${FMI_PLATFORM}"
)
set_target_properties(${TARGET_NAME} PROPERTIES PREFIX "")
set_target_properties(${TARGET_NAME} PROPERTIES OUTPUT_NAME ${MODEL_NAME})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/lib)
# modelDescription.xml
add_custom_command(TARGET ${TARGET_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_CURRENT_SOURCE_DIR}/${MODEL_NAME}/FMI${FMI_VERSION}${FMI_TYPE}.xml
"${FMU_BUILD_DIR}/modelDescription.xml"
)
# model specific header and source
foreach (SOURCE_FILE config.h model.c)
add_custom_command(TARGET ${TARGET_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy
"${CMAKE_CURRENT_SOURCE_DIR}/${MODEL_NAME}/${SOURCE_FILE}"
"${FMU_BUILD_DIR}/sources/${SOURCE_FILE}"
)
endforeach(SOURCE_FILE)
# common headers
foreach (SOURCE_FILE model.h cosimulation.h)
add_custom_command(TARGET ${TARGET_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy
"${CMAKE_CURRENT_SOURCE_DIR}/include/${SOURCE_FILE}"
"${FMU_BUILD_DIR}/sources/${SOURCE_FILE}"
)
endforeach(SOURCE_FILE)
# common sources
foreach (SOURCE_FILE fmi${FMI_VERSION}Functions.c cosimulation.c)
add_custom_command(TARGET ${TARGET_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy
"${CMAKE_CURRENT_SOURCE_DIR}/src/${SOURCE_FILE}"
"${FMU_BUILD_DIR}/sources/${SOURCE_FILE}"
)
endforeach(SOURCE_FILE)
# all.c / buildDescription.xml
if (${FMI_VERSION} LESS 3)
add_custom_command(TARGET ${TARGET_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy
"${CMAKE_CURRENT_BINARY_DIR}/all.c"
"${FMU_BUILD_DIR}/sources/all.c"
)
else()
add_custom_command(TARGET ${TARGET_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy
"${CMAKE_CURRENT_SOURCE_DIR}/${MODEL_NAME}/buildDescription.xml"
"${FMU_BUILD_DIR}/sources/buildDescription.xml"
)
endif()
set(ARCHIVE_FILES "modelDescription.xml" "binaries" "sources")
if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${MODEL_NAME}/resources")
add_custom_command(TARGET ${TARGET_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory
"${CMAKE_CURRENT_SOURCE_DIR}/${MODEL_NAME}/resources"
"${FMU_BUILD_DIR}/resources/"
)
set(ARCHIVE_FILES ${ARCHIVE_FILES} "resources")
endif()
# delete unintended files from binaries (Release configuration only)
if (MSVC)
add_custom_command(TARGET ${TARGET_NAME} POST_BUILD COMMAND if $<CONFIG:Release> neq 0 (
${CMAKE_COMMAND} -E rm -f
"${FMU_BUILD_DIR}/binaries/${FMI_PLATFORM}/${MODEL_NAME}.exp"
"${FMU_BUILD_DIR}/binaries/${FMI_PLATFORM}/${MODEL_NAME}.lib"
)
)
endif()
# create ZIP archive
add_custom_command(TARGET ${TARGET_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E tar "cfv" ${CMAKE_CURRENT_BINARY_DIR}/fmus/${MODEL_NAME}.fmu --format=zip
${ARCHIVE_FILES}
WORKING_DIRECTORY ${FMU_BUILD_DIR} COMMENT "Creating ZIP archive"
)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/fmus/${MODEL_NAME}.fmu DESTINATION ${CMAKE_INSTALL_PREFIX})
endforeach(MODEL_NAME)
# Examples
include(examples/Examples.cmake)
if (WITH_FMUSIM)
add_subdirectory(fmusim)
endif ()