Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve handling of CLI and annotation flags/options. #10418

Merged
merged 4 commits into from
Mar 25, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 13 additions & 33 deletions OMCompiler/Compiler/Main/Main.mo
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ import ClockIndexes;
import Config;
import Corba;
import DAE;
import DAEDump;
import DAEUtil;
import Debug;
import Dump;
Expand Down Expand Up @@ -399,15 +398,16 @@ algorithm
local
Absyn.Program p, pLibs;
DAE.DAElist d;
String s,str,f;
String flatString,str,f;
list<String> libs;
Absyn.Path cname;
Boolean silent,notsilent;
Boolean runBackend, runSilent;
GlobalScript.Statements stmts;
FCore.Cache cache;
FCore.Graph env;
DAE.FunctionTree funcs;
list<Absyn.Class> cls;
String cls, fileNamePrefix;
SimCode.SimulationSettings sim_settings;

// A .mo-file, followed by an optional list of extra .mo-files and libraries.
// The last class in the first file will be instantiated.
Expand Down Expand Up @@ -438,37 +438,17 @@ algorithm

execStat("Parsed file");

// Instantiate the program.
(cache, env, d, cname, s) := instantiate();
p := SymbolTable.getAbsyn();

d := if Flags.isSet(Flags.TRANSFORMS_BEFORE_DUMP) then DAEUtil.transformationsBeforeBackend(cache,env,d) else d;

funcs := FCore.getFunctionTree(cache);
cls := Config.classToInstantiate();
// If no class was explicitly specified, instantiate the last class in the
// program. Otherwise, instantiate the given class name.
cname := if stringEmpty(cls) then AbsynUtil.lastClassname(SymbolTable.getAbsyn()) else AbsynUtil.stringPath(cls);
fileNamePrefix := Util.stringReplaceChar(AbsynUtil.pathString(cname), ".", "_");

Print.clearBuf();
execStat("Transformations before Dump");
if stringEmpty(s) and not Config.silent() then
s := DAEDump.dumpStr(d, funcs);
execStat("DAEDump done");
end if;
Print.printBuf(s);
if Flags.isSet(Flags.DAE_DUMP_GRAPHV) then
DAEDump.dumpGraphviz(d);
end if;
execStat("Misc Dump");

// Do any transformations required before going into code generation, e.g. if-equations to expressions.
d := if boolNot(Flags.isSet(Flags.TRANSFORMS_BEFORE_DUMP)) then DAEUtil.transformationsBeforeBackend(cache,env,d) else d;

if not Config.silent() then
print(Print.getString());
end if;
execStat("Transformations before backend");
runBackend := Config.simulationCg() or Config.simulation();
runSilent := Config.silent();

// Run the backend.
optimizeDae(cache, env, d, p, cname);
// Show any errors or warnings if there are any!
(_ , _, _, _, _) := CevalScriptBackend.translateModel(FCore.emptyCache(), FGraph.empty(), cname,
fileNamePrefix, runBackend, runSilent, NONE());
showErrors(Print.getErrorString(), ErrorExt.printMessagesStr(false));
then ();

Expand Down
112 changes: 31 additions & 81 deletions OMCompiler/Compiler/Script/CevalScriptBackend.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1233,7 +1233,7 @@ algorithm
case ("translateModel",vals as {Values.CODE(Absyn.C_TYPENAME(className)),_,_,_,_,_,Values.STRING(filenameprefix),_,_,_,_,_})
equation
(outCache,simSettings) = calculateSimulationSettings(outCache, vals);
(b,outCache) = translateModel(outCache, inEnv, className, filenameprefix, true, SOME(simSettings));
(b,outCache) = translateModel(outCache, inEnv, className, filenameprefix, true, true, SOME(simSettings));
then
Values.BOOL(b);

Expand Down Expand Up @@ -3183,7 +3183,7 @@ algorithm
end getAdjacencyMatrix;

/* -------------------------------------------------------------------
RUN OLD FRONTEND
RUN FRONTEND
------------------------------------------------------------------- */
public function runFrontEnd
input output FCore.Cache cache;
Expand Down Expand Up @@ -3382,22 +3382,22 @@ algorithm
end if;
end runFrontEndWorkNF;

protected function translateModel " author: x02lucpo
translates a model into cpp code and writes also a makefile"
public function translateModel
input FCore.Cache inCache;
input FCore.Graph inEnv;
input Absyn.Path className "path for the model";
input String inFileNamePrefix;
input Boolean addDummy "if true, add a dummy state";
input Boolean runBackend "if true, run the backend as well. This will run SimCode and Codegen as well.";
input Boolean runSilent "if true, flat modelica code will not be dumped to out stream";
input Option<SimCode.SimulationSettings> inSimSettingsOpt;
output Boolean success;
output FCore.Cache outCache;
output list<String> outStringLst;
output list<String> outLibs;
output String outFileDir;
output list<tuple<String,Values.Value>> resultValues;
algorithm
(outCache,outStringLst,outFileDir,resultValues):=
match (inCache,inEnv,className,inFileNamePrefix,addDummy,inSimSettingsOpt)
(outCache,outLibs,outFileDir,resultValues):=
match (inCache,inEnv,className,inFileNamePrefix,inSimSettingsOpt)
local
FCore.Cache cache;
FCore.Graph env;
Expand All @@ -3408,79 +3408,25 @@ algorithm
String commandLineOptions;
list<String> args;
Boolean haveAnnotation;
SimCode.SimulationSettings simSettings;
GlobalScript.SimulationOptions defaulSimOpt;

case (cache,env,_,fileNamePrefix,_,_)
case (cache,env,_,fileNamePrefix,_)
algorithm
if Config.ignoreCommandLineOptionsAnnotation() then
(success, cache, libs, file_dir, resultValues) :=
callTranslateModel(cache,env,className,fileNamePrefix,inSimSettingsOpt);
if isSome(inSimSettingsOpt) then
SOME(simSettings) := inSimSettingsOpt;
else
// read the __OpenModelica_commandLineOptions
Absyn.STRING(commandLineOptions) := Interactive.getNamedAnnotation(className, SymbolTable.getAbsyn(), Absyn.IDENT("__OpenModelica_commandLineOptions"), SOME(Absyn.STRING("")), Interactive.getAnnotationExp);
haveAnnotation := boolNot(stringEq(commandLineOptions, ""));
// backup the flags.
flags := if haveAnnotation then FlagsUtil.backupFlags() else FlagsUtil.loadFlags();
try
// apply if there are any new flags
if haveAnnotation then
args := System.strtok(commandLineOptions, " ");
FlagsUtil.readArgs(args);
end if;

(success, cache, libs, file_dir, resultValues) :=
callTranslateModel(cache,env,className,fileNamePrefix,inSimSettingsOpt);
// reset to the original flags
FlagsUtil.saveFlags(flags);
else
FlagsUtil.saveFlags(flags);
fail();
end try;
defaulSimOpt := buildSimulationOptionsFromModelExperimentAnnotation(className, fileNamePrefix, SOME(defaultSimulationOptions));
simSettings := convertSimulationOptionsToSimCode(defaulSimOpt);
end if;
then
(cache,libs,file_dir,resultValues);

end match;
end translateModel;

protected function translateLabeledModel " author: Fatima
translates a labeled model into cpp code and writes also a makefile"
input FCore.Cache inCache;
input FCore.Graph inEnv;
input Absyn.Path className "path for the model";
input String inFileNamePrefix;
input Boolean addDummy "if true, add a dummy state";
input Option<SimCode.SimulationSettings> inSimSettingsOpt;
input list<Absyn.NamedArg> inLabelstoCancel;
output FCore.Cache outCache;
output BackendDAE.BackendDAE outBackendDAE;
output list<String> outStringLst;
output String outFileDir;
output list<tuple<String,Values.Value>> resultValues;
algorithm
(outCache,outStringLst,outFileDir,resultValues):=
match (inCache,inEnv,className,inFileNamePrefix,addDummy,inSimSettingsOpt,inLabelstoCancel)
local
FCore.Cache cache;
FCore.Graph env;
BackendDAE.BackendDAE indexed_dlow;
list<String> libs;
String file_dir, fileNamePrefix;
Absyn.Program p;
Flags.Flag flags;
String commandLineOptions;
list<String> args;
Boolean haveAnnotation;
list<Absyn.NamedArg> labelstoCancel;

case (cache,env,_,fileNamePrefix,_,_,labelstoCancel)
algorithm

if Config.ignoreCommandLineOptionsAnnotation() then
(true, cache, libs, file_dir, resultValues) :=
SimCodeMain.translateModel(SimCodeMain.TranslateModelKind.NORMAL(),cache,env,className,fileNamePrefix,addDummy,inSimSettingsOpt,Absyn.FUNCTIONARGS({},argNames =labelstoCancel));
(success, cache, libs, file_dir, resultValues) :=
callTranslateModel(cache, env, className, fileNamePrefix, runBackend, runSilent, SOME(simSettings));
else
// read the __OpenModelica_commandLineOptions
Absyn.STRING(commandLineOptions) := Interactive.getNamedAnnotation(className, SymbolTable.getAbsyn(), Absyn.IDENT("__OpenModelica_commandLineOptions"), SOME(Absyn.STRING("")), Interactive.getAnnotationExp);
Absyn.STRING(commandLineOptions) := Interactive.getNamedAnnotation(className, SymbolTable.getAbsyn(), Absyn.IDENT
("__OpenModelica_commandLineOptions"), SOME(Absyn.STRING("")), Interactive.getAnnotationExp);
haveAnnotation := boolNot(stringEq(commandLineOptions, ""));
// backup the flags.
flags := if haveAnnotation then FlagsUtil.backupFlags() else FlagsUtil.loadFlags();
Expand All @@ -3491,8 +3437,8 @@ algorithm
FlagsUtil.readArgs(args);
end if;

(true, cache, libs, file_dir, resultValues) :=
SimCodeMain.translateModel(SimCodeMain.TranslateModelKind.NORMAL(),cache,env,className,fileNamePrefix,addDummy,inSimSettingsOpt,Absyn.FUNCTIONARGS({},argNames =labelstoCancel));
(success, cache, libs, file_dir, resultValues) :=
callTranslateModel(cache, env, className, fileNamePrefix, runBackend, runSilent, SOME(simSettings));
// reset to the original flags
FlagsUtil.saveFlags(flags);
else
Expand All @@ -3504,7 +3450,7 @@ algorithm
(cache,libs,file_dir,resultValues);

end match;
end translateLabeledModel;
end translateModel;

protected function callTranslateModel
"Call the main translate function. This function
Expand All @@ -3514,6 +3460,8 @@ protected function callTranslateModel
input FCore.Graph inEnv;
input Absyn.Path className "path for the model";
input String inFileNamePrefix;
input Boolean runBackend "if true, run the backend as well. This will run SimCode and Codegen as well.";
input Boolean runSilent "if true, flat modelica code will not be dumped to out stream";
input Option<SimCode.SimulationSettings> inSimSettingsOpt;
output Boolean success;
output FCore.Cache outCache;
Expand All @@ -3528,8 +3476,8 @@ algorithm
success := true;
else
(success, outCache, outStringLst, outFileDir, resultValues) :=
SimCodeMain.translateModel(SimCodeMain.TranslateModelKind.NORMAL(),inCache,inEnv,
className,inFileNamePrefix,true,inSimSettingsOpt,Absyn.FUNCTIONARGS({},{}));
SimCodeMain.translateModel(SimCodeMain.TranslateModelKind.NORMAL(), inCache, inEnv,
className, inFileNamePrefix, runBackend, runSilent, inSimSettingsOpt, Absyn.FUNCTIONARGS({},{}));
end if;
end callTranslateModel;

Expand Down Expand Up @@ -4033,7 +3981,8 @@ algorithm
FlagsUtil.setConfigBool(Flags.BUILDING_FMU, true);
FlagsUtil.setConfigString(Flags.FMI_VERSION, FMUVersion);
try
(success, cache, libs, _, _) := SimCodeMain.translateModel(SimCodeMain.TranslateModelKind.FMU(FMUType, fmuTargetName), cache, inEnv, className, filenameprefix, addDummy, SOME(simSettings));
(success, cache, libs, _, _) := SimCodeMain.translateModel(SimCodeMain.TranslateModelKind.FMU(FMUType, fmuTargetName),
cache, inEnv, className, filenameprefix, true, true, SOME(simSettings));
true := success;
outValue := Values.STRING((if not Testsuite.isRunning() then System.pwd() + Autoconf.pathDelimiter else "") + fmuTargetName + ".fmu");
else
Expand Down Expand Up @@ -4229,7 +4178,8 @@ protected function translateModelXML " author: Alachew
protected
Boolean success;
algorithm
(success,cache) := SimCodeMain.translateModel(SimCodeMain.TranslateModelKind.XML(),cache,env,className,fileNamePrefix,addDummy,inSimSettingsOpt);
(success,cache) := SimCodeMain.translateModel(SimCodeMain.TranslateModelKind.XML(), cache, env, className,
fileNamePrefix, true, true, inSimSettingsOpt);
outValue := Values.STRING(if success then ((if not Testsuite.isRunning() then System.pwd() + Autoconf.pathDelimiter else "") + fileNamePrefix+".xml") else "");
end translateModelXML;

Expand Down Expand Up @@ -5625,7 +5575,7 @@ algorithm
(cache,simSettings) := calculateSimulationSettings(cache, values);
SimCode.SIMULATION_SETTINGS(method = method_str, outputFormat = outputFormat_str) := simSettings;

(success,cache,libsAndLibDirs,file_dir,resultValues) := translateModel(cache,env, classname, filenameprefix,true, SOME(simSettings));
(success,cache,libsAndLibDirs,file_dir,resultValues) := translateModel(cache,env, classname, filenameprefix, true, true, SOME(simSettings));
//cname_str = AbsynUtil.pathString(classname);
//SimCodeUtil.generateInitData(indexed_dlow_1, classname, filenameprefix, init_filename,
// starttime_r, stoptime_r, interval_r, tolerance_r, method_str,options_str,outputFormat_str);
Expand Down
Loading