diff --git a/Compiler/FrontEnd/ModelicaBuiltin.mo b/Compiler/FrontEnd/ModelicaBuiltin.mo index bcf6638b15e..9a2d17a331a 100644 --- a/Compiler/FrontEnd/ModelicaBuiltin.mo +++ b/Compiler/FrontEnd/ModelicaBuiltin.mo @@ -1492,6 +1492,14 @@ external "builtin"; annotation(preferredView="text"); end setCommandLineOptions; +function getCommandLineOptions + "Returns all command line options who have non-default values as a string. + The format of the string is '--flag=value --flag2=value2'." + output String flags; +external "builtin"; +annotation(preferredView="text"); +end getCommandLineOptions; + function getConfigFlagValidOptions "Returns the list of valid options for a string config flag, and the description strings for these options if available" input String flag; diff --git a/Compiler/Script/CevalScript.mo b/Compiler/Script/CevalScript.mo index 3ca268198f3..14c26d282c9 100644 --- a/Compiler/Script/CevalScript.mo +++ b/Compiler/Script/CevalScript.mo @@ -943,6 +943,12 @@ algorithm case (cache,_,"setCommandLineOptions",_,st,_) then (cache,Values.BOOL(false),st); + case (cache, _, "getCommandLineOptions", {}, st, _) + then (cache, Values.STRING(Flags.unparseFlags()), st); + + case (cache, _, "getCommandLineOptions", _, st, _) + then (cache, Values.BOOL(false), st); + case (cache,_,"clearCommandLineOptions",{},st,_) equation Flags.resetDebugFlags(); diff --git a/Compiler/Util/Flags.mo b/Compiler/Util/Flags.mo index 49bf2919fa5..9eafd0833df 100644 --- a/Compiler/Util/Flags.mo +++ b/Compiler/Util/Flags.mo @@ -1493,32 +1493,23 @@ end createDebugFlags; public function loadFlags "Loads the flags with getGlobalRoot. Creates a new flags structure if it hasn't been created yet." - output Flags outFlags; + input Boolean initialize = true; + output Flags flags; +protected + array debug_flags; + array config_flags; algorithm - outFlags := matchcontinue() - local - array debug_flags; - array config_flags; - Flags flags; - Integer debug_count, config_count; - - case () - equation - outFlags = getGlobalRoot(Global.flagsIndex); - then - outFlags; - + try + flags := getGlobalRoot(Global.flagsIndex); + else + if initialize then + List.fold(allDebugFlags, checkDebugFlag, 1); + flags := FLAGS(createDebugFlags(), createConfigFlags()); + saveFlags(flags); else - equation - List.fold(allDebugFlags, checkDebugFlag, 1); - debug_flags = createDebugFlags(); - config_flags = createConfigFlags(); - flags = FLAGS(debug_flags, config_flags); - saveFlags(flags); - then - flags; - - end matchcontinue; + flags := Flags.NO_FLAGS(); + end if; + end try; end loadFlags; public function backupFlags @@ -2935,5 +2926,91 @@ algorithm end match; end getValidStringOptions; +public +function flagDataEq + input FlagData data1; + input FlagData data2; + output Boolean eq; +algorithm + eq := match (data1, data2) + case (EMPTY_FLAG(), EMPTY_FLAG()) then true; + case (BOOL_FLAG(), BOOL_FLAG()) then data1.data == data2.data; + case (INT_FLAG(), INT_FLAG()) then data1.data == data2.data; + case (INT_LIST_FLAG(), INT_LIST_FLAG()) + then List.isEqualOnTrue(data1.data, data2.data, intEq); + case (REAL_FLAG(), REAL_FLAG()) then data1.data == data2.data; + case (STRING_FLAG(), STRING_FLAG()) then data1.data == data2.data; + case (STRING_LIST_FLAG(), STRING_LIST_FLAG()) + then List.isEqualOnTrue(data1.data, data2.data, stringEq); + case (ENUM_FLAG(), ENUM_FLAG()) + then referenceEq(data1.validValues, data2.validValues) and + data1.data == data2.data; + else false; + end match; +end flagDataEq; + +function flagDataString + input FlagData flagData; + output String str; +algorithm + str := match flagData + case BOOL_FLAG() then boolString(flagData.data); + case INT_FLAG() then intString(flagData.data); + case INT_LIST_FLAG() + then List.toString(flagData.data, intString, "", "", ",", "", false); + + case REAL_FLAG() then realString(flagData.data); + case STRING_FLAG() then flagData.data; + case STRING_LIST_FLAG() then stringDelimitList(flagData.data, ","); + case ENUM_FLAG() then Util.tuple21(listGet(flagData.validValues, flagData.data)); + else ""; + end match; +end flagDataString; + +function unparseFlags + "Goes through all the existing flags, and each flag whose value differs from + the default is added to the output string as flag=value." + output String str = ""; +protected + Flags flags; + array debug_flags; + array config_flags; + list strl = {}; + String name; +algorithm + try + FLAGS(debugFlags = debug_flags, configFlags = config_flags) := loadFlags(false); + else + str := ""; + return; + end try; + + for f in allDebugFlags loop + if f.default <> debug_flags[f.index] then + strl := f.name :: strl; + end if; + end for; + + if not listEmpty(strl) then + str := "-d=" + stringDelimitList(strl, ","); + end if; + + strl := {}; + for f in allConfigFlags loop + if not flagDataEq(f.defaultValue, config_flags[f.index]) then + name := match f.shortname + case SOME(name) then " -" + name; + else " --" + f.name; + end match; + + strl := (name + "=" + flagDataString(config_flags[f.index])) :: strl; + end if; + end for; + + if not listEmpty(strl) then + str := str + stringAppendList(strl); + end if; +end unparseFlags; + annotation(__OpenModelica_Interface="util"); end Flags;