Skip to content

Commit 01e7573

Browse files
committed
Fetch features and license information from Protection annotation
1 parent bfa56aa commit 01e7573

File tree

6 files changed

+274
-2
lines changed

6 files changed

+274
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ OMPython/
1919
OMSetup/
2020
OpenModelicaSetup/
2121
OMEncryption/
22+
OMPublicKey/

OMCompiler/Compiler/FrontEnd/Parser.mo

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,21 @@ algorithm
168168
(success, lveInstance) := ParserExt.startLibraryVendorExecutable(lvePath);
169169
end startLibraryVendorExecutable;
170170

171+
function checkLVEToolLicense
172+
input Option<Integer> lveInstance;
173+
input String packageName;
174+
output Boolean status;
175+
algorithm
176+
status := ParserExt.checkLVEToolLicense(lveInstance, packageName);
177+
end checkLVEToolLicense;
178+
179+
function checkLVEToolFeature
180+
input Option<Integer> lveInstance;
181+
input String feature;
182+
algorithm
183+
ParserExt.checkLVEToolFeature(lveInstance, feature);
184+
end checkLVEToolFeature;
185+
171186
function stopLibraryVendorExecutable
172187
input Option<Integer> lveInstance "Stores a pointer. If it is declared as Integer, it is truncated to 32-bit.";
173188
algorithm

OMCompiler/Compiler/FrontEnd/ParserExt.mo

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,21 @@ public function startLibraryVendorExecutable "Starts the library vendor executab
115115
external "C" success=ParserExt_startLibraryVendorExecutable(lvePath, lveInstance) annotation(Library = {"omparse","omantlr3","omcruntime"});
116116
end startLibraryVendorExecutable;
117117

118+
public function checkLVEToolLicense
119+
input Option<Integer> lveInstance;
120+
input String packageName;
121+
output Boolean status;
122+
123+
external "C" status=ParserExt_checkLVEToolLicense(lveInstance, packageName) annotation(Library = {"omparse","omantlr3","omcruntime"});
124+
end checkLVEToolLicense;
125+
126+
public function checkLVEToolFeature
127+
input Option<Integer> lveInstance;
128+
input String feature;
129+
130+
external "C" ParserExt_checkLVEToolFeature(lveInstance, feature) annotation(Library = {"omparse","omantlr3","omcruntime"});
131+
end checkLVEToolFeature;
132+
118133
public function stopLibraryVendorExecutable
119134
input Option<Integer> lveInstance "Stores a pointer. If it is declared as Integer, it is truncated to 32-bit.";
120135

OMCompiler/Compiler/Script/Interactive.mo

Lines changed: 218 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17345,6 +17345,8 @@ protected
1734517345
String dir,filename;
1734617346
Boolean lveStarted;
1734717347
Option<Integer> lveInstance = NONE();
17348+
list<String> features;
17349+
String libraryKey, licenseFile;
1734817350
algorithm
1734917351
if not System.regularFileExists(fileName) then
1735017352
topClassNamesQualified := {};
@@ -17359,11 +17361,22 @@ algorithm
1735917361
end if;
1736017362
end if;
1736117363
parsed := Parser.parse(fileName,encoding,dir,lveInstance);
17364+
parsed := MetaUtil.createMetaClassesInProgram(parsed);
17365+
topClassNamesQualified := getTopQualifiedClassnames(parsed);
1736217366
if (lveStarted) then
17367+
for topClassNameQualified in topClassNamesQualified loop
17368+
// check license
17369+
//print("Parser.checkLVEToolLicense returned = " + boolString(Parser.checkLVEToolLicense(lveInstance, AbsynUtil.pathString(topClassNameQualified))) + "\n");
17370+
//(libraryKey, licenseFile) := getLicenseAnnotation(topClassNameQualified, parsed);
17371+
//print("Library Key is : " + libraryKey + "\n");
17372+
//print("License File is : " + licenseFile + "\n");
17373+
features := getFeaturesAnnotation(topClassNameQualified, parsed);
17374+
for feature in features loop
17375+
Parser.checkLVEToolFeature(lveInstance, feature);
17376+
end for;
17377+
end for;
1736317378
Parser.stopLibraryVendorExecutable(lveInstance);
1736417379
end if;
17365-
parsed := MetaUtil.createMetaClassesInProgram(parsed);
17366-
topClassNamesQualified := getTopQualifiedClassnames(parsed);
1736717380
if updateProgram then
1736817381
SymbolTable.setAbsyn(.Interactive.updateProgram(parsed, SymbolTable.getAbsyn()));
1736917382
end if;
@@ -18291,6 +18304,209 @@ algorithm
1829118304
end match;
1829218305
end getAccessAnnotationString2;
1829318306

18307+
public function getLicenseAnnotation
18308+
"Returns the Protection(License=) annotation of a class.
18309+
This is annotated with the annotation:
18310+
annotation(Protection(License(libraryKey=\"15783-A39323-498222-444ckk4ll\", licenseFile=\"MyLibraryAuthorization_Tool.mo_lic\"))); in the class definition"
18311+
input Absyn.Path className;
18312+
input Absyn.Program p;
18313+
output tuple<String, String> license;
18314+
algorithm
18315+
license := match(className,p)
18316+
local
18317+
tuple<String, String> licenseAnn;
18318+
case(_,_)
18319+
equation
18320+
licenseAnn = getNamedAnnotation(className, p, Absyn.IDENT("Protection"), SOME(("","")), getLicenseAnnotationWork1);
18321+
then
18322+
licenseAnn;
18323+
else ("", "");
18324+
end match;
18325+
end getLicenseAnnotation;
18326+
18327+
protected function getLicenseAnnotationWork1
18328+
"Extractor function for getLicenseAnnotation"
18329+
input Option<Absyn.Modification> mod;
18330+
output tuple<String, String> license;
18331+
algorithm
18332+
license := match (mod)
18333+
local
18334+
list<Absyn.ElementArg> arglst;
18335+
String libraryKey, licenseFile;
18336+
18337+
case (SOME(Absyn.CLASSMOD(elementArgLst = arglst)))
18338+
equation
18339+
(libraryKey, licenseFile) = getLicenseAnnotationWork2(arglst);
18340+
then (libraryKey, licenseFile);
18341+
end match;
18342+
end getLicenseAnnotationWork1;
18343+
18344+
protected function getLicenseAnnotationWork2
18345+
"Extractor function for getLicenseAnnotation"
18346+
input list<Absyn.ElementArg> eltArgs;
18347+
output tuple<String, String> license;
18348+
algorithm
18349+
license := match eltArgs
18350+
local
18351+
Option<Absyn.Modification> mod;
18352+
list<Absyn.ElementArg> xs;
18353+
String libraryKey, licenseFile;
18354+
18355+
case ({}) then ("", "");
18356+
18357+
case (Absyn.MODIFICATION(path = Absyn.IDENT(name="License"), modification = mod)::_)
18358+
equation
18359+
(libraryKey, licenseFile) = getLicenseAnnotationTuple(mod);
18360+
then (libraryKey, licenseFile);
18361+
18362+
case (_::xs)
18363+
equation
18364+
(libraryKey, licenseFile) = getLicenseAnnotationWork2(xs);
18365+
then (libraryKey, licenseFile);
18366+
18367+
end match;
18368+
end getLicenseAnnotationWork2;
18369+
18370+
protected function getLicenseAnnotationTuple
18371+
"Extractor function for getLicenseAnnotation"
18372+
input Option<Absyn.Modification> mod;
18373+
output tuple<String, String> license;
18374+
algorithm
18375+
license := match (mod)
18376+
local
18377+
list<Absyn.ElementArg> arglst;
18378+
String libraryKey, licenseFile;
18379+
18380+
case (SOME(Absyn.CLASSMOD(elementArgLst = arglst)))
18381+
equation
18382+
libraryKey = getLicenseAnnotationLibraryKey(arglst);
18383+
licenseFile = getLicenseAnnotationLicenseFile(arglst);
18384+
then (libraryKey, licenseFile);
18385+
end match;
18386+
end getLicenseAnnotationTuple;
18387+
18388+
protected function getLicenseAnnotationLibraryKey
18389+
"Extractor function for getLicenseAnnotation"
18390+
input list<Absyn.ElementArg> eltArgs;
18391+
output String libraryKey;
18392+
algorithm
18393+
libraryKey := match eltArgs
18394+
local
18395+
list<Absyn.ElementArg> xs;
18396+
String s;
18397+
18398+
case ({}) then "";
18399+
18400+
case (Absyn.MODIFICATION(path = Absyn.IDENT(name="libraryKey"),
18401+
modification = SOME(Absyn.CLASSMOD(eqMod=Absyn.EQMOD(exp=Absyn.STRING(s)))))::_)
18402+
then s;
18403+
18404+
case (_::xs)
18405+
equation
18406+
s = getLicenseAnnotationLibraryKey(xs);
18407+
then s;
18408+
18409+
end match;
18410+
end getLicenseAnnotationLibraryKey;
18411+
18412+
protected function getLicenseAnnotationLicenseFile
18413+
"Extractor function for getLicenseAnnotation"
18414+
input list<Absyn.ElementArg> eltArgs;
18415+
output String licenseFile;
18416+
algorithm
18417+
licenseFile := match eltArgs
18418+
local
18419+
list<Absyn.ElementArg> xs;
18420+
String s;
18421+
18422+
case ({}) then "";
18423+
18424+
case (Absyn.MODIFICATION(path = Absyn.IDENT(name="licenseFile"),
18425+
modification = SOME(Absyn.CLASSMOD(eqMod=Absyn.EQMOD(exp=Absyn.STRING(s)))))::_)
18426+
then s;
18427+
18428+
case (_::xs)
18429+
equation
18430+
s = getLicenseAnnotationLicenseFile(xs);
18431+
then s;
18432+
18433+
end match;
18434+
end getLicenseAnnotationLicenseFile;
18435+
18436+
public function getFeaturesAnnotation
18437+
"Returns the Protection(features=) annotation of a class.
18438+
This is annotated with the annotation:
18439+
annotation(Protection(features={\"LicenseOption1 LicenseOption2\", \"LicenseOption3\"})); in the class definition"
18440+
input Absyn.Path className;
18441+
input Absyn.Program p;
18442+
output list<String> features;
18443+
algorithm
18444+
features := match(className,p)
18445+
local
18446+
list<String> featuresList;
18447+
case(_,_)
18448+
equation
18449+
featuresList = getNamedAnnotation(className, p, Absyn.IDENT("Protection"), SOME({}), getFeaturesAnnotationList);
18450+
then
18451+
featuresList;
18452+
else {};
18453+
end match;
18454+
end getFeaturesAnnotation;
18455+
18456+
protected function getFeaturesAnnotationList
18457+
"Extractor function for getFeaturesAnnotation"
18458+
input Option<Absyn.Modification> mod;
18459+
output list<String> features;
18460+
algorithm
18461+
features := match (mod)
18462+
local
18463+
list<Absyn.ElementArg> arglst;
18464+
18465+
case (SOME(Absyn.CLASSMOD(elementArgLst = arglst)))
18466+
then getFeaturesAnnotationList2(arglst);
18467+
18468+
end match;
18469+
end getFeaturesAnnotationList;
18470+
18471+
protected function getFeaturesAnnotationList2
18472+
"Extractor function for getFeaturesAnnotation"
18473+
input list<Absyn.ElementArg> eltArgs;
18474+
output list<String> features;
18475+
algorithm
18476+
features := match eltArgs
18477+
local
18478+
list<Absyn.Exp> expList;
18479+
list<Absyn.ElementArg> xs;
18480+
list<String> featuresList;
18481+
18482+
case ({}) then {};
18483+
18484+
case (Absyn.MODIFICATION(path = Absyn.IDENT(name="features"),
18485+
modification = SOME(Absyn.CLASSMOD(eqMod=Absyn.EQMOD(exp=Absyn.ARRAY(expList)))))::_)
18486+
equation
18487+
featuresList = List.map(expList, expToString);
18488+
then featuresList;
18489+
18490+
case (_::xs)
18491+
equation
18492+
featuresList = getFeaturesAnnotationList2(xs);
18493+
then featuresList;
18494+
18495+
end match;
18496+
end getFeaturesAnnotationList2;
18497+
18498+
protected function expToString
18499+
input Absyn.Exp inExp;
18500+
output String outExp;
18501+
algorithm
18502+
outExp := match (inExp)
18503+
local
18504+
String str;
18505+
case (Absyn.STRING(str)) then str;
18506+
case (_) then "";
18507+
end match;
18508+
end expToString;
18509+
1829418510
public function checkAccessAnnotationAndEncryption
1829518511
input Absyn.Path path;
1829618512
input Absyn.Program p;

OMCompiler/Parser/Parser_omc.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,16 @@ int ParserExt_startLibraryVendorExecutable(const char* path, void** lveInstance)
134134
return startLibraryVendorExecutable(path, lveInstance);
135135
}
136136

137+
int ParserExt_checkLVEToolLicense(void** lveInstance, const char* packageName)
138+
{
139+
return checkLVEToolLicense(lveInstance, packageName);
140+
}
141+
142+
void ParserExt_checkLVEToolFeature(void** lveInstance, const char* feature)
143+
{
144+
checkLVEToolFeature(lveInstance, feature);
145+
}
146+
137147
void ParserExt_stopLibraryVendorExecutable(void** lveInstance)
138148
{
139149
stopLibraryVendorExecutable(lveInstance);

OMCompiler/Parser/parse.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,21 @@ int startLibraryVendorExecutable(const char* path, void** lveInstance)
466466
return 0;
467467
}
468468

469+
int checkLVEToolLicense(void** lveInstance, const char* packageName)
470+
{
471+
#ifdef OMENCRYPTION
472+
return checkLVEToolLicenseImpl(lveInstance, packageName);
473+
#endif
474+
return 0;
475+
}
476+
477+
void checkLVEToolFeature(void** lveInstance, const char* feature)
478+
{
479+
#ifdef OMENCRYPTION
480+
checkLVEToolFeatureImpl(lveInstance, feature);
481+
#endif
482+
}
483+
469484
void stopLibraryVendorExecutable(void** lveInstance)
470485
{
471486
#ifdef OMENCRYPTION

0 commit comments

Comments
 (0)