Skip to content

Commit

Permalink
Cache the FMI configure results
Browse files Browse the repository at this point in the history
This was previously done for Windows and now for Linux/OSX as well.
The "dynamic" and "static" platforms now skip running the configure
script since we know what the results (should) be.
  • Loading branch information
sjoelund authored and OpenModelica-Hudson committed Sep 29, 2017
1 parent cd1bf87 commit b383de7
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 73 deletions.
151 changes: 88 additions & 63 deletions Compiler/Script/CevalScriptBackend.mo
Expand Up @@ -3164,6 +3164,83 @@ algorithm
end match;
end translateModelCPP;*/

protected function configureFMU
input String platform;
input String fmutmp;
input String logfile;
input Boolean isWindows;
protected
String CC, CFLAGS, LDFLAGS, makefileStr,
dir=fmutmp+"/sources/", cmd="",
quote="'",
dquote = if isWindows then "\"" else "'";
algorithm
CC := System.getCCompiler();
CFLAGS := System.stringReplace(System.getCFlags(),"${MODELICAUSERCFLAGS}","");
LDFLAGS := ("-L"+dquote+Settings.getInstallationDirectoryPath()+"/lib/"+System.getTriple()+"/omc"+dquote+" "+
"-Wl,-rpath,"+dquote+Settings.getInstallationDirectoryPath()+"/lib/"+System.getTriple()+"/omc"+dquote+" "+
System.getLDFlags()+" ");
if System.regularFileExists(dir + logfile) then
System.removeFile(dir + logfile);
end if;
_ := match platform
case "dynamic"
algorithm
makefileStr := System.readFile(dir + "Makefile.in");
// replace @XX@ variables in the Makefile
makefileStr := System.stringReplace(makefileStr, "@CC@", CC);
makefileStr := System.stringReplace(makefileStr, "@CFLAGS@", CFLAGS);
makefileStr := System.stringReplace(makefileStr, "@LDFLAGS@", LDFLAGS+System.getRTLibsSim());
makefileStr := System.stringReplace(makefileStr, "@LIBS@", "");
makefileStr := System.stringReplace(makefileStr, "@DLLEXT@", System.getDllExt());
makefileStr := System.stringReplace(makefileStr, "@NEED_RUNTIME@", "");
makefileStr := System.stringReplace(makefileStr, "@NEED_DGESV@", "");
makefileStr := System.stringReplace(makefileStr, "@FMIPLATFORM@", System.modelicaPlatform());
makefileStr := System.stringReplace(makefileStr, "@CPPFLAGS@", "");
makefileStr := System.stringReplace(makefileStr, "@LIBTYPE_DYNAMIC@", "1");
makefileStr := System.stringReplace(makefileStr, "\r\n", "\n");
System.writeFile(dir + "Makefile", makefileStr);
System.writeFile(dir + "config.log", "Using cached values for dynamic platform");
cmd := "cached values";
then ();
case "static"
algorithm
makefileStr := System.readFile(dir + "Makefile.in");
// replace @XX@ variables in the Makefile
makefileStr := System.stringReplace(makefileStr, "@CC@", CC);
makefileStr := System.stringReplace(makefileStr, "@CFLAGS@", CFLAGS);
makefileStr := System.stringReplace(makefileStr, "@LDFLAGS@", LDFLAGS+System.getRTLibsFMU());
makefileStr := System.stringReplace(makefileStr, "@LIBS@", "");
makefileStr := System.stringReplace(makefileStr, "@DLLEXT@", System.getDllExt());
makefileStr := System.stringReplace(makefileStr, "@NEED_RUNTIME@", "1");
makefileStr := System.stringReplace(makefileStr, "@NEED_DGESV@", "");
makefileStr := System.stringReplace(makefileStr, "@FMIPLATFORM@", System.modelicaPlatform());
makefileStr := System.stringReplace(makefileStr, "@CPPFLAGS@", "-DOMC_MINIMAL_RUNTIME=1 -DCMINPACK_NO_DLL=1");
makefileStr := System.stringReplace(makefileStr, "@LIBTYPE_DYNAMIC@", "1");
makefileStr := System.stringReplace(makefileStr, "\r\n", "\n");
System.writeFile(dir + "Makefile", makefileStr);
System.writeFile(dir + "config.log", "Using cached values for static platform");
cmd := "cached values";
then ();
else
algorithm
cmd := "cd \"" + fmutmp + "/sources\" && ./configure --host="+quote+platform+quote+" CFLAGS="+quote+"-Os -flto"+quote+" LDFLAGS=-flto";
if 0 <> System.systemCall(cmd, outFile=logfile) then
Error.addMessage(Error.SIMULATOR_BUILD_ERROR, {System.readFile(logfile)});
System.removeFile(dir + logfile);
fail();
end if;
then ();
end match;
if not isWindows then
if 0 <> System.systemCall("cd " + dir + " && make clean > /dev/null 2>&1") then
Error.addMessage(Error.SIMULATOR_BUILD_ERROR, {"Failed to make clean"});
fail();
end if;
end if;
ExecStat.execStat("buildModelFMU: configured platform " + platform + " using " + cmd);
end configureFMU;

protected function buildModelFMU " author: Frenkel TUD
translates a model into cpp code and writes also a makefile"
input FCore.Cache inCache;
Expand All @@ -3180,8 +3257,8 @@ protected function buildModelFMU " author: Frenkel TUD
output GlobalScript.SymbolTable st;
protected
Boolean staticSourceCodeFMU;
String filenameprefix, quote, dquote, fmutmp, thisplatform, cmd, logfile,
makefileStr, CC, CFLAGS, LDFLAGS, LIBS, dir;
String filenameprefix, fmutmp, thisplatform, cmd, logfile,
makefileStr, LIBS, dir;
GlobalScript.SimulationOptions defaulSimOpt;
SimCode.SimulationSettings simSettings;
list<String> libs;
Expand Down Expand Up @@ -3221,9 +3298,6 @@ algorithm
System.realtimeTick(ClockIndexes.RT_CLOCK_BUILD_MODEL);

isWindows := System.os() == "Windows_NT";
// compile
quote := if isWindows then "" else "'";
dquote := if isWindows then "\"" else "'";

if Config.simCodeTarget() == "Cpp" then
System.removeDirectory("binaries");
Expand All @@ -3234,79 +3308,30 @@ algorithm
CevalScript.compileModel(filenameprefix + "_FMU", libs,
makeVars={"TARGET_TRIPLET=" + platform});
end if;
ExecStat.execStat("buildModelFMU: Generate C++ for platform " + platform);
end for;
return;
end if;

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

CC := quote+System.getCCompiler()+quote;
CFLAGS := quote+System.stringReplace(System.getCFlags(),"${MODELICAUSERCFLAGS}","")+quote;
LDFLAGS := quote+("-L"+dquote+Settings.getInstallationDirectoryPath()+"/lib/"+System.getTriple()+"/omc"+dquote+" "+
"-Wl,-rpath,"+dquote+Settings.getInstallationDirectoryPath()+"/lib/"+System.getTriple()+"/omc"+dquote+" "+
System.getLDFlags()+" ");
ExecStat.execStat("buildModelFMU: Generate the FMI files");

if isWindows then
dir := fmutmp + "/sources/";
makefileStr := System.readFile(dir + "Makefile.in");
// replace @XX@ variables in the Makefile
makefileStr := System.stringReplace(makefileStr, "@CC@", CC);
makefileStr := System.stringReplace(makefileStr, "@CFLAGS@", CFLAGS);
makefileStr := System.stringReplace(makefileStr, "@LDFLAGS@", LDFLAGS+System.getRTLibsSim()+quote);
makefileStr := System.stringReplace(makefileStr, "@LIBS@", "");
makefileStr := System.stringReplace(makefileStr, "@DLLEXT@", ".dll");
makefileStr := System.stringReplace(makefileStr, "@NEED_RUNTIME@", "");
makefileStr := System.stringReplace(makefileStr, "@NEED_DGESV@", "");
makefileStr := System.stringReplace(makefileStr, "@FMIPLATFORM@", System.modelicaPlatform());
makefileStr := System.stringReplace(makefileStr, "@CPPFLAGS@", "");
makefileStr := System.stringReplace(makefileStr, "\r\n", "\n");
System.writeFile(dir + "Makefile", makefileStr);
if System.regularFileExists(dir + logfile) then
System.removeFile(dir + logfile);
end if;
try
CevalScript.compileModel(filenameprefix, libs, workingDir=dir, makeVars={});
else
outValue := Values.STRING("");
return;
end try;
System.removeDirectory(fmutmp);
return;
end if;

thisplatform := " CC="+CC+
" CFLAGS="+CFLAGS+
" LDFLAGS="+LDFLAGS;
fmutmp := filenameprefix + ".fmutmp";
logfile := filenameprefix + ".log";
dir := fmutmp+"/sources/";

for platform in platforms loop
cmd := "cd \"" + fmutmp + "/sources\" && ./configure "+
(if platform == "dynamic" then
" --with-dynamic-om-runtime"+thisplatform+System.getRTLibsSim()+quote
elseif platform == "static" then
thisplatform+quote
else
(
" --host="+quote+platform+quote+
" CFLAGS="+quote+"-Os -flto"+quote+" LDFLAGS=-flto"
)
) + " && make clean";
if System.regularFileExists(logfile) then
System.removeFile(logfile);
end if;
if 0 <> System.systemCall(cmd, outFile=logfile) then
outValue := Values.STRING("");
Error.addMessage(Error.SIMULATOR_BUILD_ERROR, {System.readFile(logfile)});
return;
end if;
configureFMU(platform, fmutmp, logfile, isWindows);
try
CevalScript.compileModel(filenameprefix, libs, workingDir=fmutmp+"/sources", makeVars={});
CevalScript.compileModel(filenameprefix, libs, workingDir=dir, makeVars={});
else
outValue := Values.STRING("");
Error.addMessage(Error.SIMULATOR_BUILD_ERROR, {System.readFile(logfile)});
ExecStat.execStat("buildModelFMU failed for platform " + platform);
return;
end try;
ExecStat.execStat("buildModelFMU: Generate platform " + platform);
end for;

System.removeDirectory(fmutmp);
Expand Down
1 change: 1 addition & 0 deletions Compiler/SimCode/SimCodeMain.mo
Expand Up @@ -293,6 +293,7 @@ algorithm
if not Config.getRunningTestsuite() then
resstr = System.pwd() + System.pathDelimiter() + resstr;
end if;
ExecStat.execStat("translateModelFMU complete");
then
(cache, Values.STRING(resstr), st, dlow_1, libs, file_dir, resultValues);
else
Expand Down
6 changes: 3 additions & 3 deletions Compiler/Template/CodegenFMU.tpl
Expand Up @@ -1073,17 +1073,17 @@ match platform
else
<<
<%fileNamePrefix%>_FMU:
<%\t%>$(MAKE) $(MAINOBJ) <%fileNamePrefix%>_functions.h <%fileNamePrefix%>_literals.h $(OFILES) $(RUNTIMEFILES) > make.log
<%\t%>$(MAKE) CC="$(CC)" $(MAINOBJ) <%fileNamePrefix%>_functions.h <%fileNamePrefix%>_literals.h $(OFILES) $(RUNTIMEFILES) 2>&1 | tee make.log
<%\t%>mkdir -p ../binaries/$(FMIPLATFORM)
ifeq (@LIBTYPE_DYNAMIC@,1)
<%\t%>$(LD) -o <%modelNamePrefix%>$(DLLEXT) $(MAINOBJ) $(OFILES) $(RUNTIMEFILES) <%dirExtra%> <%libsPos1%> <%libsPos2%> $(LDFLAGS)
<%\t%>$(LD) -o <%modelNamePrefix%>$(DLLEXT) $(MAINOBJ) $(OFILES) $(RUNTIMEFILES) <%dirExtra%> <%libsPos1%> <%libsPos2%> $(LDFLAGS) 2>&1 | tee -a make.log
<%\t%>cp <%fileNamePrefix%>$(DLLEXT) <%fileNamePrefix%>_FMU.libs config.log make.log ../binaries/$(FMIPLATFORM)/
endif
<%\t%>head -n20 Makefile > ../binaries/$(FMIPLATFORM)/config.summary
ifeq (@LIBTYPE_STATIC@,1)
<%\t%>rm -f <%modelNamePrefix%>.a
<%\t%>$(AR) -rsu <%modelNamePrefix%>.a $(MAINOBJ) $(OFILES) $(RUNTIMEFILES)
<%\t%>cp <%fileNamePrefix%>.a <%fileNamePrefix%>_FMU.libs config.log ../binaries/$(FMIPLATFORM)/
<%\t%>cp <%fileNamePrefix%>.a <%fileNamePrefix%>_FMU.libs config.log make.log ../binaries/$(FMIPLATFORM)/
endif
<%\t%>$(MAKE) distclean
<%\t%>cd .. && rm -f ../<%fileNamePrefix%>.fmu && zip -r ../<%fileNamePrefix%>.fmu *
Expand Down
37 changes: 30 additions & 7 deletions SimulationRuntime/fmi/export/fmi1/fmu1_model_interface.c
Expand Up @@ -144,9 +144,9 @@ fmiComponent fmiInstantiateModel(fmiString instanceName, fmiString GUID, fmiCall
comp = (ModelInstance *)functions.allocateMemory(1, sizeof(ModelInstance));
if (comp) {
DATA* fmudata = NULL;
MODEL_DATA* modelData = NULL;
SIMULATION_INFO* simInfo = NULL;
threadData_t *threadData = NULL;
MODEL_DATA* modelData = NULL;
SIMULATION_INFO* simInfo = NULL;
threadData_t *threadData = NULL;

comp->functions = functions;
comp->loggingOn = loggingOn;
Expand Down Expand Up @@ -199,14 +199,21 @@ fmiComponent fmiInstantiateModel(fmiString instanceName, fmiString GUID, fmiCall
/* read input vars */
/* input_function(comp->fmuData);*/
/* allocate memory for non-linear system solvers */
#if !defined(OMC_NUM_NONLINEAR_SYSTEMS) || OMC_NUM_NONLINEAR_SYSTEMS>0
initializeNonlinearSystems(comp->fmuData, comp->threadData);
#endif
/* allocate memory for non-linear system solvers */
#if !defined(OMC_NUM_LINEAR_SYSTEMS) || OMC_NUM_LINEAR_SYSTEMS>0
initializeLinearSystems(comp->fmuData, comp->threadData);
#endif
/* allocate memory for mixed system solvers */
#if !defined(OMC_NUM_MIXED_SYSTEMS) || OMC_NUM_MIXED_SYSTEMS>0
initializeMixedSystems(comp->fmuData, comp->threadData);
#endif
/* allocate memory for state selection */
#if !defined(OMC_NO_STATESELECTION)
initializeStateSetJacobians(comp->fmuData, comp->threadData);

#endif
return comp;
}

Expand Down Expand Up @@ -643,7 +650,9 @@ fmiStatus fmiInitialize(fmiComponent c, fmiBoolean toleranceControlled, fmiReal
/*TODO: Simulation stop time is need to calculate in before hand all sample events
We shouldn't generate them all in beforehand */
initSample(comp->fmuData, comp->threadData, comp->fmuData->localData[0]->timeValue, 100 /*should be stopTime*/);
#if !defined(OMC_NDELAY_EXPRESSIONS) || OMC_NDELAY_EXPRESSIONS>0
initDelay(comp->fmuData, comp->fmuData->localData[0]->timeValue);
#endif

/* due to an event overwrite old values */
overwriteOldSimulationData(comp->fmuData);
Expand Down Expand Up @@ -689,13 +698,15 @@ fmiStatus fmiEventUpdate(fmiComponent c, fmiBoolean intermediateResults, fmiEven
/* try */
MMC_TRY_INTERNAL(simulationJumpBuffer)

#if !defined(OMC_NO_STATESELECTION)
if (stateSelection(comp->fmuData, threadData, 1, 1))
{
if (comp->loggingOn) comp->functions.logger(c, comp->instanceName, fmiOK, "log",
"fmiEventUpdate: Need to iterate state values changed!");
/* if new set is calculated reinit the solver */
eventInfo->stateValuesChanged = fmiTrue;
}
#endif

storePreValues(comp->fmuData);

Expand Down Expand Up @@ -800,13 +811,15 @@ fmiStatus fmiCompletedIntegratorStep(fmiComponent c, fmiBoolean* callEventUpdate
storePreValues(comp->fmuData);
*callEventUpdate = fmiFalse;
/******** check state selection ********/
#if !defined(OMC_NO_STATESELECTION)
if (stateSelection(comp->fmuData, comp->threadData, 1, 0))
{
if (comp->loggingOn) comp->functions.logger(c, comp->instanceName, fmiOK, "log",
"fmiEventUpdate: Need to iterate state values changed!");
/* if new set is calculated reinit the solver */
*callEventUpdate = fmiTrue;
}
#endif
/* TODO: fix the extrapolation in non-linear system
* then we can stop to save all variables in
* in the whole ringbuffer
Expand All @@ -829,17 +842,27 @@ fmiStatus fmiTerminate(fmiComponent c)
"fmiTerminate");

comp->state = modelTerminated;
/* call external objects destructors */
comp->fmuData->callback->callExternalObjectDestructors(comp->fmuData, comp->threadData);

#if !defined(OMC_NUM_NONLINEAR_SYSTEMS) || OMC_NUM_NONLINEAR_SYSTEMS>0
/* free nonlinear system data */
freeNonlinearSystems(comp->fmuData, comp->threadData);
#endif
#if !defined(OMC_NUM_MIXED_SYSTEMS) || OMC_NUM_MIXED_SYSTEMS>0
/* free mixed system data */
freeMixedSystems(comp->fmuData, comp->threadData);
#endif
#if !defined(OMC_NUM_LINEAR_SYSTEMS) || OMC_NUM_LINEAR_SYSTEMS>0
/* free linear system data */
freeLinearSystems(comp->fmuData, comp->threadData);

/* call external objects destructors */
comp->fmuData->callback->callExternalObjectDestructors(comp->fmuData, comp->threadData);
#endif
#if !defined(OMC_NO_STATESELECTION)
/* free stateset data */
freeStateSetData(comp->fmuData);
#endif

/* free stateset data */
deInitializeDataStruc(comp->fmuData);
/* free simuation data */
comp->functions.freeMemory(comp->fmuData->modelData);
Expand Down

0 comments on commit b383de7

Please sign in to comment.