Skip to content

Commit

Permalink
Build FMI 3.0 FMUs
Browse files Browse the repository at this point in the history
  • Loading branch information
t-sommer committed Aug 9, 2024
1 parent 6277061 commit 2822786
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 13 deletions.
2 changes: 2 additions & 0 deletions fmusim-gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ set(PROJECT_SOURCES
${ZLIB_SRC_DIR}/contrib/minizip/iowin32.c
../fmusim/FMIModelDescription.c
../fmusim/FMIModelDescription.h
../fmusim/FMIBuildDescription.c
../fmusim/FMIBuildDescription.h
../src/structured_variable_name.tab.c
../src/structured_variable_name.yy.c
)
Expand Down
79 changes: 76 additions & 3 deletions fmusim-gui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,13 @@ void MainWindow::loadFMU(const QString &filename) {
return;
}

const QString buildDescriptionPath = QDir(unzipdir).filePath("sources/buildDescription.xml");

if (QFileInfo::exists(buildDescriptionPath)) {
QByteArray ba = buildDescriptionPath.toLocal8Bit();
buildDescription = FMIReadBuildDescription(ba.data());
}

// update the GUI
startValues.clear();

Expand Down Expand Up @@ -348,6 +355,17 @@ void MainWindow::loadFMU(const QString &filename) {
// enable widgets
ui->showSideBarAction->setEnabled(true);
ui->showSideBarAction->setChecked(true);

bool hasSourceCode = false;

if ((modelDescription->coSimulation && modelDescription->coSimulation->nSourceFiles > 0) ||
(modelDescription->modelExchange && modelDescription->modelExchange->nSourceFiles > 0) ||
buildDescription) {
hasSourceCode = true;
}

ui->compilePlatformBinaryAction->setEnabled(hasSourceCode);

ui->showInfoAction->setEnabled(true);
ui->showSettingsAction->setEnabled(true);
ui->showFilesAction->setEnabled(true);
Expand Down Expand Up @@ -778,6 +796,11 @@ void MainWindow::unloadFMU() {
modelDescription = nullptr;
}

if (buildDescription) {
FMIFreeBuildDescription(buildDescription);
buildDescription = nullptr;
}

if (!unzipdir.isEmpty()) {
QByteArray bytes = unzipdir.toLocal8Bit();
const char *cstr = bytes.data();
Expand All @@ -799,6 +822,7 @@ void MainWindow::unloadFMU() {

ui->dockWidget->setHidden(true);

ui->compilePlatformBinaryAction->setEnabled(false);
ui->showInfoAction->setEnabled(false);
ui->showSettingsAction->setEnabled(false);
ui->showFilesAction->setEnabled(false);
Expand Down Expand Up @@ -885,9 +909,16 @@ void MainWindow::compilePlatformBinary() {
buildDirectory.setAutoRemove(dialog.removeBuilDirectory());

QFile::copy(":/resources/CMakeLists.txt", buildDirectory.filePath("CMakeLists.txt"));
QFile::copy(":/resources/fmi2Functions.h", buildDirectory.filePath("fmi2Functions.h"));
QFile::copy(":/resources/fmi2FunctionTypes.h", buildDirectory.filePath("fmi2FunctionTypes.h"));
QFile::copy(":/resources/fmi2TypesPlatform.h", buildDirectory.filePath("fmi2TypesPlatform.h"));

if (modelDescription->fmiMajorVersion == 2) {
QFile::copy(":/resources/fmi2Functions.h", buildDirectory.filePath("fmi2Functions.h"));
QFile::copy(":/resources/fmi2FunctionTypes.h", buildDirectory.filePath("fmi2FunctionTypes.h"));
QFile::copy(":/resources/fmi2TypesPlatform.h", buildDirectory.filePath("fmi2TypesPlatform.h"));
} else {
QFile::copy(":/resources/fmi3Functions.h", buildDirectory.filePath("fmi3Functions.h"));
QFile::copy(":/resources/fmi3FunctionTypes.h", buildDirectory.filePath("fmi3FunctionTypes.h"));
QFile::copy(":/resources/fmi3PlatformTypes.h", buildDirectory.filePath("fmi3PlatformTypes.h"));
}

size_t nSourceFiles;
const char** sourceFiles;
Expand All @@ -902,6 +933,46 @@ void MainWindow::compilePlatformBinary() {
sourceFiles = modelDescription->modelExchange->sourceFiles;
}

QStringList definitions;

if (modelDescription->fmiMajorVersion == 3) {
definitions << "FMI3_OVERRIDE_FUNCTION_PREFIX";
}

if (buildDescription) {

if (buildDescription->nBuildConfigurations > 1) {
ui->logPlainTextEdit->appendPlainText("Multiple Build Configurations are not supported.\n");
return;
}

const FMIBuildConfiguration* buildConfiguration = buildDescription->buildConfigurations[0];

if (buildConfiguration->nSourceFileSets > 1) {
ui->logPlainTextEdit->appendPlainText("Multiple Source File Sets are not supported.\n");
return;
}

const FMISourceFileSet* sourceFileSet = buildConfiguration->sourceFileSets[0];

nSourceFiles = sourceFileSet->nSourceFiles;
sourceFiles = sourceFileSet->sourceFiles;

for (size_t i = 0; i < sourceFileSet->nPreprocessorDefinitions; i++) {

FMIPreprocessorDefinition* preprocessorDefinition = sourceFileSet->preprocessorDefinitions[i];

QString definition = preprocessorDefinition->name;

if (preprocessorDefinition->value) {
definition += "=";
definition += preprocessorDefinition->value;
}

definitions << definition;
}
}

QString buildDirPath = wsl ? wslPath(buildDirectory.path()) : buildDirectory.path();
QString unzipdirPath = wsl ? wslPath(unzipdir) : unzipdir;

Expand Down Expand Up @@ -940,8 +1011,10 @@ void MainWindow::compilePlatformBinary() {

arguments << "-S" + buildDirPath;
arguments << "-B" + buildDirPath;
arguments << "-DFMI_MAJOR_VERSION=" + QString::number(modelDescription->fmiMajorVersion);
arguments << "-DMODEL_IDENTIFIER=" + modelIdentifier;
arguments << "-DINCLUDE='" + includeDirectories.join(';') + "'";
arguments << "-DDEFINITIONS='" + definitions.join(';') + "'";
arguments << "-DSOURCES='" + sources.join(';') + "'";
arguments << "-DUNZIPDIR='" + unzipdirPath + "'";

Expand Down
4 changes: 2 additions & 2 deletions fmusim-gui/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ QT_END_NAMESPACE

extern "C" {
#include "FMIModelDescription.h"
struct FMIRecorder;
struct FMISimulationSettings;
#include "FMIBuildDescription.h"
}

class ModelVariablesItemModel;
Expand Down Expand Up @@ -48,6 +47,7 @@ class MainWindow : public QMainWindow
QFileSystemModel filesModel;
QComboBox* interfaceTypeComboBox;
FMIModelDescription* modelDescription = nullptr;
FMIBuildDescription* buildDescription = nullptr;
QString unzipdir;
QMap<const FMIModelVariable*, QString> startValues;
QList<const FMIModelVariable*> plotVariables;
Expand Down
14 changes: 6 additions & 8 deletions fmusim-gui/resources/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,17 @@ cmake_minimum_required (VERSION 3.15)

set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")

set(FMI_VERSION 2 CACHE STRING "FMI Version")
set_property(CACHE FMI_VERSION PROPERTY STRINGS 2 3)
set(FMI_MAJOR_VERSION 2 CACHE STRING "FMI Major Version")
set_property(CACHE FMI_MAJOR_VERSION PROPERTY STRINGS 2 3)

set (MODEL_IDENTIFIER CACHE STRING "Model Identifier")
set (INCLUDE CACHE STRING "Include directories")
set (DEFINITIONS CACHE STRING "Precompiler definitions")
set (SOURCES CACHE STRING "Source files")
set (UNZIPDIR CACHE STRING "Unzip directory")

project (${MODEL_IDENTIFIER})

#message(STATUS "MODEL_IDENTIFIER: ${MODEL_IDENTIFIER}")
#message(STATUS "INCLUDE: ${INCLUDE}")
#message(STATUS "SOURCES: ${SOURCES}")
#message(STATUS "UNZIPDIR: ${UNZIPDIR}")

if (${CMAKE_HOST_SYSTEM_PROCESSOR} MATCHES "AMD64|x86_64")
set(FMI_ARCHITECTURE "x86_64")
elseif (${CMAKE_HOST_SYSTEM_PROCESSOR} MATCHES "aarch64")
Expand All @@ -25,7 +21,7 @@ else ()
message(FATAL_ERROR "Unknown system architecture: ${CMAKE_SYSTEM_PROCESSOR}")
endif ()

if (${FMI_VERSION} GREATER 2)
if (${FMI_MAJOR_VERSION} GREATER 2)

if (WIN32)
set(FMI_PLATFORM "${FMI_ARCHITECTURE}-windows")
Expand Down Expand Up @@ -59,6 +55,8 @@ add_library(${MODEL_IDENTIFIER} SHARED ${SOURCES})

target_include_directories(${MODEL_IDENTIFIER} PUBLIC ${INCLUDE})

target_compile_definitions(${MODEL_IDENTIFIER} PUBLIC ${DEFINITIONS})

set_target_properties(${MODEL_IDENTIFIER} PROPERTIES PREFIX "")

install(TARGETS ${MODEL_IDENTIFIER} DESTINATION "${UNZIPDIR}/binaries/${FMI_PLATFORM}")
Expand Down

0 comments on commit 2822786

Please sign in to comment.