@@ -4,47 +4,6 @@ get_filename_component(_fmu4cpp_root "${_fmu4cpp_cmake_dir}/.." ABSOLUTE)
44set (_fmu4cpp_root "${_fmu4cpp_root} " CACHE INTERNAL "" )
55
66
7- function (_getTargetPlatform fmiVersion )
8- set (_target "" PARENT_SCOPE )
9- if (fmiVersion STREQUAL "fmi2" )
10-
11- if ("${CMAKE_SIZEOF_VOID_P } " STREQUAL "8" )
12- set (BITNESS 64)
13- else ()
14- set (BITNESS 32)
15- endif ()
16-
17- if (WIN32 )
18- set (_target win${BITNESS} )
19- elseif (APPLE )
20- set (_target darwin${BITNESS} )
21- else ()
22- set (_target linux ${BITNESS} )
23- endif ()
24-
25- elseif (fmiVersion STREQUAL "fmi3" )
26-
27- set (_target "x86" )
28- if ("${CMAKE_SIZEOF_VOID_P } " STREQUAL "8" )
29- set (_target "${_target} _64" )
30- endif ()
31-
32- if (WIN32 )
33- set (_target ${_target} -windows)
34- elseif (APPLE )
35- set (_target ${_target} -darwin)
36- else ()
37- set (_target ${_target} -linux )
38- endif ()
39-
40- else ()
41- message (FATAL_ERROR "Unknown FMI version: ${fmiVersion} . Supported versions are 'fmi2' and 'fmi3'." )
42- endif ()
43-
44- set (TARGET_PLATFORM ${_target} PARENT_SCOPE )
45-
46- endfunction ()
47-
487function (generateFMU modelIdentifier )
498
509 set (options WITH_SOURCES)
@@ -77,23 +36,20 @@ function(generateFMU modelIdentifier)
7736 endif ()
7837
7938
80- # common object files from the base target
81- set (COMMON_OBJECTS "$<TARGET_OBJECTS :fmu4cpp_base >" )
82-
83- # create an internal object library from provided sources (if not already created)
39+ # create an internal object library from provided sources
8440 set (model_objects_target "${modelIdentifier} _fmu_objects" )
85- if (NOT TARGET ${model_objects_target} )
86- add_library (${model_objects_target} OBJECT ${FMU_SOURCES} )
87- target_include_directories (${model_objects_target} PUBLIC "${_fmu4cpp_root} /export/include" )
88- if (FMU_LINK_TARGETS)
89- target_link_libraries (${model_objects_target} PRIVATE ${FMU_LINK_TARGETS} )
90- endif ()
91- # apply user-provided compile definitions to the object target
92- if (FMU_COMPILE_DEFINITIONS)
93- target_compile_definitions (${model_objects_target} PRIVATE ${FMU_COMPILE_DEFINITIONS} )
94- endif ()
95- set_target_properties (${model_objects_target} PROPERTIES POSITION_INDEPENDENT_CODE ON )
41+
42+ add_library (${model_objects_target} OBJECT ${FMU_SOURCES} )
43+ target_include_directories (${model_objects_target} PUBLIC "${_fmu4cpp_root} /export/include" )
44+ if (FMU_LINK_TARGETS)
45+ target_link_libraries (${model_objects_target} PRIVATE ${FMU_LINK_TARGETS} )
46+ endif ()
47+ # apply user-provided compile definitions to the object target
48+ if (FMU_COMPILE_DEFINITIONS)
49+ target_compile_definitions (${model_objects_target} PRIVATE ${FMU_COMPILE_DEFINITIONS} )
9650 endif ()
51+ set_target_properties (${model_objects_target} PROPERTIES POSITION_INDEPENDENT_CODE ON )
52+
9753
9854 foreach (fmiVersion IN LISTS FMU_FMI_VERSIONS)
9955
@@ -123,33 +79,16 @@ function(generateFMU modelIdentifier)
12379 set (binaryOutputDir "$<1:${modelOutputDir} /binaries /${TARGET_PLATFORM} >" )
12480
12581 add_library (${versionTarget} SHARED
126- ${COMMON_OBJECTS}
82+ "$< TARGET_OBJECTS : fmu4cpp_base >"
12783 "$<TARGET_OBJECTS :${model_objects_target} >"
128- ${VERSION_OBJECTS}
84+ " ${VERSION_OBJECTS} "
12985 )
13086 target_include_directories (${versionTarget} PRIVATE "${_fmu4cpp_root} /export/include" )
13187 target_compile_definitions (${versionTarget} PRIVATE ${VERSION_DEFS} )
13288
13389 # link user-provided link targets (must be propagated to the final shared lib)
13490 if (FMU_LINK_TARGETS)
135- target_link_libraries (${versionTarget} PRIVATE ${FMU_LINK_TARGETS} )
136-
137- foreach (dep IN LISTS FMU_LINK_TARGETS)
138- if (TARGET ${dep} )
139- get_target_property (target_type ${dep} TYPE)
140- if (NOT "${target_type} " STREQUAL "INTERFACE_LIBRARY" )
141- add_custom_command (TARGET ${versionTarget} POST_BUILD
142- WORKING_DIRECTORY "${modelOutputDir} "
143- COMMAND ${CMAKE_COMMAND } -E echo "[generateFMU-${fmiVersion} ] Copying runtime of ${dep} to ${binaryOutputDir} "
144- COMMAND ${CMAKE_COMMAND } -E make_directory "${binaryOutputDir} "
145- # copy the target's runtime file (dll/so/dylib) into the binaries folder
146- COMMAND ${CMAKE_COMMAND } -E copy_if_different
147- $<TARGET_FILE :${dep} >
148- "${binaryOutputDir} /$<TARGET_FILE_NAME :${dep} >"
149- )
150- endif ()
151- endif ()
152- endforeach ()
91+ _bundle_link_libraries ()
15392 endif ()
15493
15594 # if user provided compile definitions, also ensure final target sees them (optional)
@@ -177,47 +116,7 @@ function(generateFMU modelIdentifier)
177116 continue ()
178117 endif ()
179118
180- message ("[generateFMU-${fmiVersion} ] Including sources in FMU for model '${modelIdentifier} '" )
181-
182- set (VERSION_SOURCES ${VERSION_OBJECTS} )
183- file (MAKE_DIRECTORY "${modelOutputDir} /sources" )
184- file (COPY "${generatedSourcesDir} /fmu4cpp/lib_info.cpp" DESTINATION "${modelOutputDir} /sources/fmu4cpp/" )
185- file (COPY ${_fmu4cpp_root} /export/src/fmu4cpp DESTINATION "${modelOutputDir} /sources/" )
186- file (REMOVE_RECURSE "${modelOutputDir} /sources/fmu4cpp/fmi2" ) # remove fmi2 sources
187- file (COPY ${_fmu4cpp_root} /export/include/fmu4cpp DESTINATION "${modelOutputDir} /sources/" )
188- foreach (s IN LISTS FMU_SOURCES VERSION_SOURCES)
189- if (NOT "${s} " MATCHES "^\\ $<") # skip generator expressions
190- if (IS_ABSOLUTE "${s} ")
191- set (abs "${s} ")
192- else ()
193- get_filename_component (abs "${CMAKE_CURRENT_SOURCE_DIR } /${s} " ABSOLUTE )
194- endif ()
195- file (COPY "${abs} " DESTINATION "${modelOutputDir} /sources /")
196- endif ()
197- endforeach ()
198-
199- #glob .cpp files in ${modelOutputDir} /sources /
200- set (SOURCE_SET )
201- file (GLOB_RECURSE COPIED_CPP_FILES "${modelOutputDir} /sources /*.cpp ")
202- foreach (cpp_file IN LISTS COPIED_CPP_FILES )
203- #get relative path to sources dir
204- file (RELATIVE_PATH rel_path "${modelOutputDir} /sources " "${cpp_file} ")
205- set (SOURCE_SET "${SOURCE_SET} <SourceFile name =\"${rel_path} \"/>\n " )
206- endforeach ()
207-
208- #write buildDescription.xml
209- file (WRITE "${modelOutputDir} /sources/buildDescription.xml"
210- "<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>\n "
211- "<fmiBuildDescription fmiVersion=\" 3.0\" >\n "
212- "\t <BuildConfiguration modelIdentifier=\" ${FMU4CPP_MODEL_IDENTIFIER} \" >\n "
213- "\t\t <SourceFileSet language=\" C++17\" >\n "
214- ${SOURCE_SET}
215- "\t\t\t <PreprocessorDefinition name=\" FMI3\" />\n "
216- "\t\t\t <IncludeDirectory name=\" include/\" />\n "
217- "\t\t </SourceFileSet>\n "
218- "\t </BuildConfiguration>\n "
219- "</fmiBuildDescription>"
220- )
119+ _include_sources_in_fmu ()
221120 endif ()
222121
223122 # Generate modelDescription.xml
@@ -226,30 +125,141 @@ function(generateFMU modelIdentifier)
226125 COMMAND ${CMAKE_COMMAND } -E echo "[generateFMU-${fmiVersion} ] Generating modelDescription.xml for model '${modelIdentifier} '"
227126 COMMAND descriptionGenerator "$<TARGET_FILE_NAME :${versionTarget} >" )
228127
229- # Package FMU
230- set (TAR_INPUTS "${modelOutputDir} /binaries" "${modelOutputDir} /modelDescription.xml" )
231- if (FMU_WITH_SOURCES AND fmiVersion STREQUAL "fmi3" )
232- list (APPEND TAR_INPUTS "${modelOutputDir} /sources" "${modelOutputDir} /sources/buildDescription.xml" )
128+ _package_fmu ()
129+
130+ endforeach ()
131+
132+ endfunction ()
133+
134+ macro (_package_fmu )
135+ set (TAR_INPUTS "${modelOutputDir} /binaries" "${modelOutputDir} /modelDescription.xml" )
136+ if (FMU_WITH_SOURCES AND fmiVersion STREQUAL "fmi3" )
137+ list (APPEND TAR_INPUTS "${modelOutputDir} /sources" "${modelOutputDir} /sources/buildDescription.xml" )
138+ endif ()
139+ if (NOT FMU_RESOURCE_FOLDER STREQUAL "" )
140+ message ("[generateFMU-${fmiVersion} ] Using resourceFolder=${FMU_RESOURCE_FOLDER} for model '${modelIdentifier} '" )
141+ file (COPY "${FMU_RESOURCE_FOLDER} /" DESTINATION "${modelOutputDir} /resources" )
142+ list (PREPEND TAR_INPUTS "resources" )
143+ endif ()
144+
145+ if (FMU_DESTINATION)
146+ set (FMU_DEST_DIR "${FMU_DESTINATION} /${fmiVersion} " )
147+ file (MAKE_DIRECTORY "${FMU_DEST_DIR} " )
148+ set (FMU_DESTINATION_ "${FMU_DEST_DIR} /${modelIdentifier} .fmu" )
149+ else ()
150+ set (FMU_DESTINATION_ "${modelIdentifier} .fmu" )
151+ endif ()
152+ add_custom_command (TARGET ${versionTarget} POST_BUILD
153+ WORKING_DIRECTORY "${modelOutputDir} "
154+ COMMAND ${CMAKE_COMMAND } -E echo "[generateFMU-${fmiVersion} ] Packaging ${modelIdentifier} .fmu in ${FMU_DESTINATION_} "
155+ COMMAND ${CMAKE_COMMAND } -E tar c "${FMU_DESTINATION_} " --format=zip ${TAR_INPUTS}
156+ )
157+ endmacro ()
158+
159+ macro (_bundle_link_libraries )
160+ target_link_libraries (${versionTarget} PRIVATE ${FMU_LINK_TARGETS} )
161+
162+ foreach (dep IN LISTS FMU_LINK_TARGETS)
163+ if (TARGET ${dep} )
164+ get_target_property (target_type ${dep} TYPE)
165+ if (NOT "${target_type} " STREQUAL "INTERFACE_LIBRARY" )
166+ add_custom_command (TARGET ${versionTarget} POST_BUILD
167+ WORKING_DIRECTORY "${modelOutputDir} "
168+ COMMAND ${CMAKE_COMMAND } -E echo "[generateFMU-${fmiVersion} ] Copying runtime of ${dep} to ${binaryOutputDir} "
169+ COMMAND ${CMAKE_COMMAND } -E make_directory "${binaryOutputDir} "
170+ # copy the target's runtime file (dll/so/dylib) into the binaries folder
171+ COMMAND ${CMAKE_COMMAND } -E copy_if_different
172+ $<TARGET_FILE :${dep} >
173+ "${binaryOutputDir} /$<TARGET_FILE_NAME :${dep} >"
174+ )
175+ endif ()
233176 endif ()
234- if (NOT FMU_RESOURCE_FOLDER STREQUAL "" )
235- message ("[generateFMU-${fmiVersion} ] Using resourceFolder=${FMU_RESOURCE_FOLDER} for model '${modelIdentifier} '" )
236- file (COPY "${FMU_RESOURCE_FOLDER} /" DESTINATION "${modelOutputDir} /resources" )
237- list (PREPEND TAR_INPUTS "resources" )
177+ endforeach ()
178+ endmacro ()
179+
180+ macro (_include_sources_in_fmu )
181+
182+ message ("[generateFMU-${fmiVersion} ] Including sources in FMU for model '${modelIdentifier} '" )
183+
184+ set (VERSION_SOURCES ${VERSION_OBJECTS} )
185+ file (MAKE_DIRECTORY "${modelOutputDir} /sources" )
186+ file (COPY "${generatedSourcesDir} /fmu4cpp/lib_info.cpp" DESTINATION "${modelOutputDir} /sources/fmu4cpp/" )
187+ file (COPY ${_fmu4cpp_root} /export/src/fmu4cpp DESTINATION "${modelOutputDir} /sources/" )
188+ file (REMOVE_RECURSE "${modelOutputDir} /sources/fmu4cpp/fmi2" ) # remove fmi2 sources
189+ file (COPY ${_fmu4cpp_root} /export/include/fmu4cpp DESTINATION "${modelOutputDir} /sources/" )
190+
191+ foreach (s IN LISTS FMU_SOURCES VERSION_SOURCES)
192+ if (NOT "${s} " MATCHES "^\\ $<") # skip generator expressions
193+ if (IS_ABSOLUTE "${s} ")
194+ set (abs "${s} ")
195+ else ()
196+ get_filename_component (abs "${CMAKE_CURRENT_SOURCE_DIR } /${s} " ABSOLUTE )
197+ endif ()
198+ file (COPY "${abs} " DESTINATION "${modelOutputDir} /sources /")
238199 endif ()
200+ endforeach ()
239201
240- if (FMU_DESTINATION)
241- set (FMU_DEST_DIR "${FMU_DESTINATION} /${fmiVersion} " )
242- file (MAKE_DIRECTORY "${FMU_DEST_DIR} " )
243- set (FMU_DESTINATION_ "${FMU_DEST_DIR} /${modelIdentifier} .fmu" )
202+ # glob .cpp files in ${modelOutputDir} /sources /
203+ set (SOURCE_SET "")
204+ file (GLOB_RECURSE COPIED_CPP_FILES "${modelOutputDir} /sources /*.cpp ")
205+ foreach (cpp_file IN LISTS COPIED_CPP_FILES )
206+ # get relative path to sources dir
207+ file (RELATIVE_PATH rel_path "${modelOutputDir} /sources " "${cpp_file} ")
208+ set (SOURCE_SET "${SOURCE_SET} \t \t \t <SourceFile name =\"${rel_path} \"/>\n " )
209+ endforeach ()
210+
211+ #write buildDescription.xml
212+ file (WRITE "${modelOutputDir} /sources/buildDescription.xml"
213+ "<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>\n "
214+ "<fmiBuildDescription fmiVersion=\" 3.0\" >\n "
215+ "\t <BuildConfiguration modelIdentifier=\" ${FMU4CPP_MODEL_IDENTIFIER} \" >\n "
216+ "\t\t <SourceFileSet language=\" C++17\" >\n "
217+ ${SOURCE_SET}
218+ "\t\t\t <PreprocessorDefinition name=\" FMI3\" />\n "
219+ "\t\t\t <IncludeDirectory name=\" include/\" />\n "
220+ "\t\t </SourceFileSet>\n "
221+ "\t </BuildConfiguration>\n "
222+ "</fmiBuildDescription>\n "
223+ )
224+ endmacro ()
225+
226+ function (_getTargetPlatform fmiVersion )
227+ set (_target "" )
228+ if (fmiVersion STREQUAL "fmi2" )
229+
230+ if ("${CMAKE_SIZEOF_VOID_P } " STREQUAL "8" )
231+ set (BITNESS 64)
244232 else ()
245- set (FMU_DESTINATION_ " ${modelIdentifier} .fmu" )
233+ set (BITNESS 32 )
246234 endif ()
247- add_custom_command (TARGET ${versionTarget} POST_BUILD
248- WORKING_DIRECTORY "${modelOutputDir} "
249- COMMAND ${CMAKE_COMMAND } -E echo "[generateFMU-${fmiVersion} ] Packaging ${modelIdentifier} .fmu in ${FMU_DESTINATION_} "
250- COMMAND ${CMAKE_COMMAND } -E tar c "${FMU_DESTINATION_} " --format=zip ${TAR_INPUTS}
251- )
252235
253- endforeach ()
236+ if (WIN32 )
237+ set (_target win${BITNESS} )
238+ elseif (APPLE )
239+ set (_target darwin${BITNESS} )
240+ else ()
241+ set (_target linux ${BITNESS} )
242+ endif ()
243+
244+ elseif (fmiVersion STREQUAL "fmi3" )
245+
246+ set (_target "x86" )
247+ if ("${CMAKE_SIZEOF_VOID_P } " STREQUAL "8" )
248+ set (_target "${_target} _64" )
249+ endif ()
250+
251+ if (WIN32 )
252+ set (_target ${_target} -windows)
253+ elseif (APPLE )
254+ set (_target ${_target} -darwin)
255+ else ()
256+ set (_target ${_target} -linux )
257+ endif ()
258+
259+ else ()
260+ message (FATAL_ERROR "Unknown FMI version: ${fmiVersion} . Supported versions are 'fmi2' and 'fmi3'." )
261+ endif ()
262+
263+ set (TARGET_PLATFORM ${_target} PARENT_SCOPE )
254264
255265endfunction ()
0 commit comments