@@ -47,7 +47,7 @@ endfunction()
4747
4848function (generateFMU modelIdentifier )
4949
50- set (options)
50+ set (options WITH_SOURCES )
5151 set (oneValueArgs RESOURCE_FOLDER DESTINATION)
5252 set (multiValueArgs FMI_VERSIONS SOURCES LINK_TARGETS COMPILE_DEFINITIONS)
5353 cmake_parse_arguments (FMU "${options} " "${oneValueArgs} " "${multiValueArgs} " ${ARGN} )
@@ -122,7 +122,6 @@ function(generateFMU modelIdentifier)
122122 set (modelOutputDir "${fmuOutputDir} /${modelIdentifier} " )
123123 set (binaryOutputDir "$<1:${modelOutputDir} /binaries /${TARGET_PLATFORM} >" )
124124
125-
126125 add_library (${versionTarget} SHARED
127126 ${COMMON_OBJECTS}
128127 "$<TARGET_OBJECTS :${model_objects_target} >"
@@ -172,6 +171,55 @@ function(generateFMU modelIdentifier)
172171 )
173172 endif ()
174173
174+ if (FMU_WITH_SOURCES)
175+ if (fmiVersion STREQUAL "fmi2" )
176+ message (WARNING "[generateFMU-fmi2] FMU_WITH_SOURCES is not supported for fmi2; skipping source inclusion for model '${modelIdentifier} '" )
177+ continue ()
178+ endif ()
179+
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+ )
221+ endif ()
222+
175223 # Generate modelDescription.xml
176224 add_custom_command (TARGET ${versionTarget} POST_BUILD
177225 WORKING_DIRECTORY "${binaryOutputDir} "
@@ -180,6 +228,9 @@ function(generateFMU modelIdentifier)
180228
181229 # Package FMU
182230 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" )
233+ endif ()
183234 if (NOT FMU_RESOURCE_FOLDER STREQUAL "" )
184235 message ("[generateFMU-${fmiVersion} ] Using resourceFolder=${FMU_RESOURCE_FOLDER} for model '${modelIdentifier} '" )
185236 file (COPY "${FMU_RESOURCE_FOLDER} /" DESTINATION "${modelOutputDir} /resources" )
0 commit comments