Skip to content

Commit

Permalink
Implement cross compilation for Cpp FMUs (#3760)
Browse files Browse the repository at this point in the history
  • Loading branch information
rfranke committed Mar 18, 2016
1 parent 4bb6e51 commit 95e9b46
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 19 deletions.
13 changes: 11 additions & 2 deletions Compiler/Script/CevalScriptBackend.mo
Expand Up @@ -2754,12 +2754,21 @@ algorithm
// compile
quote := if isWindows then "" else "'";
dquote := if isWindows then "\"" else "'";
CevalScript.compileModel(filenameprefix+"_FMU" , libs);

if Config.simCodeTarget() == "Cpp" then
// Cpp FMUs are not source-code FMUs
System.removeDirectory("binaries");
for platform in platforms loop
if platform == "dynamic" or platform == "static" then
CevalScript.compileModel(filenameprefix + "_FMU", libs);
else
CevalScript.compileModel(filenameprefix + "_FMU", libs,
makeVars={"TARGET_TRIPLET=" + platform});
end if;
end for;
return;
end if;

CevalScript.compileModel(filenameprefix+"_FMU" , libs);
fmutmp := filenameprefix + ".fmutmp";
logfile := filenameprefix + ".log";

Expand Down
60 changes: 43 additions & 17 deletions Compiler/Template/CodegenFMUCpp.tpl
Expand Up @@ -711,28 +711,55 @@ case SIMCODE(modelInfo=MODELINFO(__), makefileParams=MAKEFILE_PARAMS(__), simula
let lapackbins = match platformstr case "win32" then '"<%omhome%>/bin/lapack_win32.DLL" "<%omhome%>/bin/blas_win32.DLL"' else ''
let mkdir = match makefileParams.platform case "win32" then '"mkdir.exe"' else 'mkdir'
<<
# Makefile generated by OpenModelica
# Makefile generated by OpenModelica for native and cross compilation
# How to cross compile:
# - build OpenModelica (omc) from source code (see github.com/OpenModelica)
# - install a cross compiler, e.g. apt-get install g++-mingw-w64-i686
# - fix or work around capitalization of windows.h in MSL, see
# https://trac.modelica.org/Modelica/ticket/1962
# - add the target triplet, e.g. i686-w64-mingw32, to
# OMCompiler/SimulationRuntime/cpp/Makefile
# - provide a link to boost includes outside /usr/include, e.g.
# ln -s /usr/include/boost OMCompiler/SimulationRuntime/cpp/boost
# - rebuild omc to add the new platform
# - link boost includes in the work directory and invoke the omc commands
# setCommandLineOptions("+simCodeTarget=Cpp");
# buildModelFMU(MyModel, platforms={"i686-w64-mingw32"});
# - alternatively call this Makefile with
# make TARGET_TRIPLET=i686-w64-mingw32

#TARGET_TRIPLET=
OMHOME=<%makefileParams.omhome%>
include $(OMHOME)/include/omc/cpp/ModelicaConfig_gcc.inc
include $(OMHOME)/include/omc/cpp/ModelicaLibraryConfig_gcc.inc
# Simulations use -O0 by default; can be changed to e.g. -O2 or -Ofast
SIM_OR_DYNLOAD_OPT_LEVEL=-O0
CC=<%makefileParams.ccompiler%>
CXX=<%makefileParams.cxxcompiler%>
LINK=<%makefileParams.linker%>
EXEEXT=<%makefileParams.exeext%>
DLLEXT=<%makefileParams.dllext%>
CFLAGS_BASED_ON_INIT_FILE=<%extraCflags%>

FMU_CFLAGS=$(SYSTEM_CFLAGS:-O0=$(SIM_OR_DYNLOAD_OPT_LEVEL)) -DFMU_BUILD
CFLAGS=$(CFLAGS_BASED_ON_INIT_FILE) -Winvalid-pch $(FMU_CFLAGS) -DRUNTIME_STATIC_LINKING -I"$(OMHOME)/include/omc/cpp" -I"$(UMFPACK_INCLUDE)" -I"$(SUNDIALS_INCLUDE)" -I"$(BOOST_INCLUDE)" <%makefileParams.includes ; separator=" "%> <%additionalCFlags_GCC%>
# simulations use -O0 by default; can be changed to e.g. -O2 or -Ofast
SIM_OPT_LEVEL=-O0

# native build or cross compilation
ifeq ($(TARGET_TRIPLET),)
CC=<%makefileParams.ccompiler%>
CXX=<%makefileParams.cxxcompiler%>
DLLEXT=<%makefileParams.dllext%>
TRIPLET=<%getTriple()%>
PLATFORM=<%platformstr%>
else
CC=$(TARGET_TRIPLET)-gcc
CXX=$(TARGET_TRIPLET)-g++
DLLEXT=$(if $(findstring mingw,$(TARGET_TRIPLET)),.dll,.so)
TRIPLET=$(TARGET_TRIPLET)
PLATFORM=$(if $(findstring darwin,$(TRIPLET)),darwin,$(if $(findstring mingw,$(TRIPLET)),win,linux))$(if $(findstring x86_64,$(TRIPLET)),64,32)
endif

CFLAGS_BASED_ON_INIT_FILE=<%extraCflags%>
FMU_CFLAGS=$(subst -DUSE_THREAD,,$(subst -O0,$(SIM_OPT_LEVEL),$(SYSTEM_CFLAGS)))
CFLAGS=$(CFLAGS_BASED_ON_INIT_FILE) -Winvalid-pch $(FMU_CFLAGS) -DFMU_BUILD -DRUNTIME_STATIC_LINKING -I"$(OMHOME)/include/omc/cpp" -I"$(UMFPACK_INCLUDE)" -I"$(SUNDIALS_INCLUDE)" -I"$(BOOST_INCLUDE)" <%makefileParams.includes ; separator=" "%> <%additionalCFlags_GCC%>

ifeq ($(USE_LOGGER),ON)
$(eval CFLAGS=$(CFLAGS) -DUSE_LOGGER)
$(eval CFLAGS=$(CFLAGS) -DUSE_LOGGER)
endif

LDFLAGS=-L"$(OMHOME)/lib/<%getTriple()%>/omc/cpp" <%additionalLinkerFlags_GCC%> -Wl,--no-undefined
PLATFORM=<%platformstr%>
LDFLAGS=-L"$(OMHOME)/lib/$(TRIPLET)/omc/cpp" <%additionalLinkerFlags_GCC%> -Wl,--no-undefined

CALCHELPERMAINFILE=OMCpp<%fileNamePrefix%>CalcHelperMain.cpp

Expand Down Expand Up @@ -777,7 +804,6 @@ case SIMCODE(modelInfo=MODELINFO(__), makefileParams=MAKEFILE_PARAMS(__), simula

<%modelName%>.fmu: $(OFILES)
<%\t%>$(CXX) -shared -o <%fileNamePrefix%>$(DLLEXT) $(OFILES) $(LDFLAGS) $(LIBS)
<%\t%>rm -rf binaries
<%\t%><%mkdir%> -p "binaries/$(PLATFORM)"
<%\t%>cp $(BINARIES) "binaries/$(PLATFORM)/"
ifeq ($(USE_FMU_SUNDIALS),ON)
Expand All @@ -793,10 +819,10 @@ case SIMCODE(modelInfo=MODELINFO(__), makefileParams=MAKEFILE_PARAMS(__), simula
else
<%\t%>zip -r "<%modelName%>.fmu" modelDescription.xml binaries
endif
<%\t%>rm -rf binaries

clean:
<%\t%>rm $(SRC) <%fileNamePrefix%>$(DLLEXT)
<%\t%>rm <%fileNamePrefix%>$(DLLEXT)
<%\t%>rm -rf binaries

>>
end fmuMakefile;
Expand Down

0 comments on commit 95e9b46

Please sign in to comment.