generate_datatypes.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. #!/usr/bin/env python
  2. # This Source Code Form is subject to the terms of the Mozilla Public
  3. # License, v. 2.0. If a copy of the MPL was not distributed with this
  4. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  5. from __future__ import print_function
  6. import sys
  7. import time
  8. import platform
  9. import getpass
  10. from collections import OrderedDict
  11. import re
  12. import xml.etree.ElementTree as etree
  13. import itertools
  14. import argparse
  15. import csv
  16. import json
  17. from nodeset_compiler.opaque_type_mapping import get_base_type_for_opaque as get_base_type_for_opaque_ns0
  18. types = OrderedDict() # contains types that were already parsed
  19. typedescriptions = {} # contains type nodeids
  20. user_opaque_type_mapping = {} # contains user defined opaque type mapping
  21. excluded_types = ["NodeIdType", "InstanceNode", "TypeNode", "Node", "ObjectNode",
  22. "ObjectTypeNode", "VariableNode", "VariableTypeNode", "ReferenceTypeNode",
  23. "MethodNode", "ViewNode", "DataTypeNode",
  24. "NumericRange", "NumericRangeDimensions",
  25. "UA_ServerDiagnosticsSummaryDataType", "UA_SamplingIntervalDiagnosticsDataType",
  26. "UA_SessionSecurityDiagnosticsDataType", "UA_SubscriptionDiagnosticsDataType",
  27. "UA_SessionDiagnosticsDataType"]
  28. builtin_types = ["Boolean", "SByte", "Byte", "Int16", "UInt16", "Int32", "UInt32",
  29. "Int64", "UInt64", "Float", "Double", "String", "DateTime", "Guid",
  30. "ByteString", "XmlElement", "NodeId", "ExpandedNodeId", "StatusCode",
  31. "QualifiedName", "LocalizedText", "ExtensionObject", "DataValue",
  32. "Variant", "DiagnosticInfo"]
  33. # If set to False, every defined datatype must have a corresponding ID entry in the csv file
  34. isInternalTypes = False
  35. # Some types can be memcpy'd off the binary stream. That's especially important
  36. # for arrays. But we need to check if they contain padding and whether the
  37. # endianness is correct. This dict gives the C-statement that must be true for the
  38. # type to be overlayable. Parsed types are added if they apply.
  39. builtin_overlayable = {"Boolean": "true",
  40. "SByte": "true", "Byte": "true",
  41. "Int16": "UA_BINARY_OVERLAYABLE_INTEGER",
  42. "UInt16": "UA_BINARY_OVERLAYABLE_INTEGER",
  43. "Int32": "UA_BINARY_OVERLAYABLE_INTEGER",
  44. "UInt32": "UA_BINARY_OVERLAYABLE_INTEGER",
  45. "Int64": "UA_BINARY_OVERLAYABLE_INTEGER",
  46. "UInt64": "UA_BINARY_OVERLAYABLE_INTEGER",
  47. "Float": "UA_BINARY_OVERLAYABLE_FLOAT",
  48. "Double": "UA_BINARY_OVERLAYABLE_FLOAT",
  49. "DateTime": "UA_BINARY_OVERLAYABLE_INTEGER",
  50. "StatusCode": "UA_BINARY_OVERLAYABLE_INTEGER",
  51. "Guid": "(UA_BINARY_OVERLAYABLE_INTEGER && " +
  52. "offsetof(UA_Guid, data2) == sizeof(UA_UInt32) && " +
  53. "offsetof(UA_Guid, data3) == (sizeof(UA_UInt16) + sizeof(UA_UInt32)) && " +
  54. "offsetof(UA_Guid, data4) == (2*sizeof(UA_UInt32)))"}
  55. whitelistFuncAttrWarnUnusedResult = [] # for instances [ "String", "ByteString", "LocalizedText" ]
  56. # Type aliases
  57. type_aliases = { "CharArray" : "String" }
  58. def getTypeName(xmlTypeName):
  59. typeName = xmlTypeName[xmlTypeName.find(":")+1:]
  60. return type_aliases.get(typeName, typeName)
  61. # Escape C strings:
  62. def makeCLiteral(value):
  63. return re.sub(r'(?<!\\)"', r'\\"', value.replace('\\', r'\\\\').replace('\n', r'\\n').replace('\r', r''))
  64. # Strip invalid characters to create valid C identifiers (variable names etc):
  65. def makeCIdentifier(value):
  66. return re.sub(r'[^\w]', '', value)
  67. def get_base_type_for_opaque(name):
  68. if name in user_opaque_type_mapping:
  69. return user_opaque_type_mapping[name]
  70. else:
  71. return get_base_type_for_opaque_ns0(name)
  72. ################
  73. # Type Classes #
  74. ################
  75. class StructMember(object):
  76. def __init__(self, name, memberType, isArray):
  77. self.name = name
  78. self.memberType = memberType
  79. self.isArray = isArray
  80. def getNodeidTypeAndId(nodeId):
  81. if '=' not in nodeId:
  82. return "UA_NODEIDTYPE_NUMERIC, {{{0}}}".format(nodeId)
  83. if nodeId.startswith("i="):
  84. return "UA_NODEIDTYPE_NUMERIC, {{{0}}}".format(nodeId[2:])
  85. if nodeId.startswith("s="):
  86. strId = nodeId[2:]
  87. return "UA_NODEIDTYPE_STRING, {{ .string = UA_STRING_STATIC(\"{id}\") }}".format(id=strId.replace("\"", "\\\""))
  88. class Type(object):
  89. def __init__(self, outname, xml, namespace):
  90. self.name = None
  91. if xml is not None:
  92. self.name = xml.get("Name")
  93. self.typeIndex = makeCIdentifier(outname.upper() + "_" + self.name.upper())
  94. else:
  95. self.typeIndex = makeCIdentifier(outname.upper())
  96. self.ns0 = ("true" if namespace == 0 else "false")
  97. self.outname = outname
  98. self.kind = None
  99. self.description = ""
  100. self.pointerfree = "false"
  101. self.overlayable = "false"
  102. self.members = []
  103. if xml is not None:
  104. for child in xml:
  105. if child.tag == "{http://opcfoundation.org/BinarySchema/}Documentation":
  106. self.description = child.text
  107. break
  108. def datatype_c(self):
  109. # xmlEncodingId = "0"
  110. binaryEncodingId = "0"
  111. if self.name in typedescriptions:
  112. description = typedescriptions[self.name]
  113. typeid = "{%s, %s}" % (description.namespaceid, getNodeidTypeAndId(description.nodeid))
  114. # xmlEncodingId = description.xmlEncodingId
  115. binaryEncodingId = description.binaryEncodingId
  116. else:
  117. if not isInternalTypes:
  118. raise RuntimeError("NodeId for " + self.name + " not found in .csv file")
  119. else:
  120. typeid = "{0, UA_NODEIDTYPE_NUMERIC, {0}}"
  121. idName = makeCIdentifier(self.name)
  122. return "{\n UA_TYPENAME(\"%s\") /* .typeName */\n" % idName + \
  123. " " + typeid + ", /* .typeId */\n" + \
  124. " sizeof(UA_" + idName + "), /* .memSize */\n" + \
  125. " " + self.typeIndex + ", /* .typeIndex */\n" + \
  126. " " + self.kind + ", /* .typeKind */\n" + \
  127. " " + self.pointerfree + ", /* .pointerFree */\n" + \
  128. " " + self.overlayable + ", /* .overlayable */\n" + \
  129. " " + str(len(self.members)) + ", /* .membersSize */\n" + \
  130. " " + binaryEncodingId + ", /* .binaryEncodingId */\n" + \
  131. " %s_members" % idName + " /* .members */\n}"
  132. def members_c(self):
  133. idName = makeCIdentifier(self.name)
  134. if len(self.members) == 0:
  135. return "#define %s_members NULL" % (idName)
  136. members = "static UA_DataTypeMember %s_members[%s] = {" % (idName, len(self.members))
  137. before = None
  138. size = len(self.members)
  139. for i, member in enumerate(self.members):
  140. memberName = makeCIdentifier(member.name)
  141. memberNameCapital = memberName
  142. if len(memberName) > 0:
  143. memberNameCapital = memberName[0].upper() + memberName[1:]
  144. m = "\n{\n UA_TYPENAME(\"%s\") /* .memberName */\n" % memberNameCapital
  145. m += " %s_%s, /* .memberTypeIndex */\n" % (member.memberType.outname.upper(), makeCIdentifier(member.memberType.name.upper()))
  146. m += " "
  147. if not before:
  148. m += "0,"
  149. else:
  150. if member.isArray:
  151. m += "offsetof(UA_%s, %sSize)" % (idName, memberName)
  152. else:
  153. m += "offsetof(UA_%s, %s)" % (idName, memberName)
  154. m += " - offsetof(UA_%s, %s)" % (idName, makeCIdentifier(before.name))
  155. if before.isArray:
  156. m += " - sizeof(void*),"
  157. else:
  158. m += " - sizeof(UA_%s)," % makeCIdentifier(before.memberType.name)
  159. m += " /* .padding */\n"
  160. m += " %s, /* .namespaceZero */\n" % member.memberType.ns0
  161. m += (" true" if member.isArray else " false") + " /* .isArray */\n}"
  162. if i != size:
  163. m += ","
  164. members += m
  165. before = member
  166. return members + "};"
  167. def datatype_ptr(self):
  168. return "&" + self.outname.upper() + "[" + makeCIdentifier(self.outname.upper() + "_" + self.name.upper()) + "]"
  169. def functions_c(self):
  170. idName = makeCIdentifier(self.name)
  171. funcs = "static UA_INLINE void\nUA_%s_init(UA_%s *p) {\n memset(p, 0, sizeof(UA_%s));\n}\n\n" % (idName, idName, idName)
  172. funcs += "static UA_INLINE UA_%s *\nUA_%s_new(void) {\n return (UA_%s*)UA_new(%s);\n}\n\n" % (idName, idName, idName, self.datatype_ptr())
  173. if self.pointerfree == "true":
  174. funcs += "static UA_INLINE UA_StatusCode\nUA_%s_copy(const UA_%s *src, UA_%s *dst) {\n *dst = *src;\n return UA_STATUSCODE_GOOD;\n}\n\n" % (idName, idName, idName)
  175. funcs += "static UA_INLINE void\nUA_%s_deleteMembers(UA_%s *p) {\n memset(p, 0, sizeof(UA_%s));\n}\n\n" % (idName, idName, idName)
  176. funcs += "static UA_INLINE void\nUA_%s_clear(UA_%s *p) {\n memset(p, 0, sizeof(UA_%s));\n}\n\n" % (idName, idName, idName)
  177. else:
  178. for entry in whitelistFuncAttrWarnUnusedResult:
  179. if idName == entry:
  180. funcs += "UA_INTERNAL_FUNC_ATTR_WARN_UNUSED_RESULT "
  181. break
  182. funcs += "static UA_INLINE UA_StatusCode\nUA_%s_copy(const UA_%s *src, UA_%s *dst) {\n return UA_copy(src, dst, %s);\n}\n\n" % (idName, idName, idName, self.datatype_ptr())
  183. funcs += "static UA_INLINE void\nUA_%s_deleteMembers(UA_%s *p) {\n UA_clear(p, %s);\n}\n\n" % (idName, idName, self.datatype_ptr())
  184. funcs += "static UA_INLINE void\nUA_%s_clear(UA_%s *p) {\n UA_clear(p, %s);\n}\n\n" % (idName, idName, self.datatype_ptr())
  185. funcs += "static UA_INLINE void\nUA_%s_delete(UA_%s *p) {\n UA_delete(p, %s);\n}" % (idName, idName, self.datatype_ptr())
  186. return funcs
  187. def encoding_h(self):
  188. idName = makeCIdentifier(self.name)
  189. enc = "static UA_INLINE size_t\nUA_%s_calcSizeBinary(const UA_%s *src) {\n return UA_calcSizeBinary(src, %s);\n}\n"
  190. enc += "static UA_INLINE UA_StatusCode\nUA_%s_encodeBinary(const UA_%s *src, UA_Byte **bufPos, const UA_Byte *bufEnd) {\n return UA_encodeBinary(src, %s, bufPos, &bufEnd, NULL, NULL);\n}\n"
  191. enc += "static UA_INLINE UA_StatusCode\nUA_%s_decodeBinary(const UA_ByteString *src, size_t *offset, UA_%s *dst) {\n return UA_decodeBinary(src, offset, dst, %s, NULL);\n}"
  192. return enc % tuple(list(itertools.chain(*itertools.repeat([idName, idName, self.datatype_ptr()], 3))))
  193. class BuiltinType(Type):
  194. def __init__(self, name):
  195. Type.__init__(self, name, None, 0)
  196. self.name = name
  197. self.ns0 = "true"
  198. self.typeIndex = makeCIdentifier("UA_TYPES_" + self.name.upper())
  199. self.outname = "ua_types"
  200. self.kind = "UA_DATATYPEKIND_" + self.name.upper()
  201. self.description = ""
  202. self.pointerfree = "false"
  203. if self.name in builtin_overlayable.keys():
  204. self.pointerfree = "true"
  205. self.overlayable = "false"
  206. if name in builtin_overlayable:
  207. self.overlayable = builtin_overlayable[name]
  208. self.members = []
  209. class EnumerationType(Type):
  210. def __init__(self, outname, xml, namespace):
  211. Type.__init__(self, outname, xml, namespace)
  212. self.pointerfree = "true"
  213. self.overlayable = "UA_BINARY_OVERLAYABLE_INTEGER"
  214. self.members = []
  215. self.kind = "UA_DATATYPEKIND_ENUM"
  216. self.typeIndex = "UA_TYPES_INT32"
  217. self.elements = OrderedDict()
  218. for child in xml:
  219. if child.tag == "{http://opcfoundation.org/BinarySchema/}EnumeratedValue":
  220. self.elements[child.get("Name")] = child.get("Value")
  221. def typedef_h(self):
  222. if sys.version_info[0] < 3:
  223. values = self.elements.iteritems()
  224. else:
  225. values = self.elements.items()
  226. return "typedef enum {\n " + ",\n ".join(map(lambda kv : makeCIdentifier("UA_" + self.name.upper() + "_" + kv[0].upper()) + \
  227. " = " + kv[1], values)) + \
  228. ",\n __UA_{0}_FORCE32BIT = 0x7fffffff\n".format(makeCIdentifier(self.name.upper())) + "} " + \
  229. "UA_{0};\nUA_STATIC_ASSERT(sizeof(UA_{0}) == sizeof(UA_Int32), enum_must_be_32bit);".format(makeCIdentifier(self.name))
  230. class OpaqueType(Type):
  231. def __init__(self, outname, xml, namespace, baseType):
  232. Type.__init__(self, outname, xml, namespace)
  233. self.kind = "UA_DATATYPEKIND_" + baseType.upper()
  234. self.baseType = baseType
  235. self.members = []
  236. def typedef_h(self):
  237. return "typedef UA_" + self.baseType + " UA_%s;" % self.name
  238. class StructType(Type):
  239. def __init__(self, outname, xml, namespace):
  240. Type.__init__(self, outname, xml, namespace)
  241. self.members = []
  242. lengthfields = [] # lengthfields of arrays are not included as members
  243. for child in xml:
  244. if child.get("LengthField"):
  245. lengthfields.append(child.get("LengthField"))
  246. for child in xml:
  247. if not child.tag == "{http://opcfoundation.org/BinarySchema/}Field":
  248. continue
  249. if child.get("Name") in lengthfields:
  250. continue
  251. memberName = child.get("Name")
  252. memberName = memberName[:1].lower() + memberName[1:]
  253. memberTypeName = getTypeName(child.get("TypeName"))
  254. memberType = types[memberTypeName]
  255. isArray = True if child.get("LengthField") else False
  256. self.members.append(StructMember(memberName, memberType, isArray))
  257. self.pointerfree = "true"
  258. self.overlayable = "true"
  259. self.kind = "UA_DATATYPEKIND_STRUCTURE"
  260. before = None
  261. for m in self.members:
  262. if m.isArray or m.memberType.pointerfree != "true":
  263. self.pointerfree = "false"
  264. self.overlayable = "false"
  265. else:
  266. self.overlayable += "\n\t\t && " + m.memberType.overlayable
  267. if before:
  268. self.overlayable += "\n\t\t && offsetof(UA_%s, %s) == (offsetof(UA_%s, %s) + sizeof(UA_%s))" % \
  269. (makeCIdentifier(self.name), makeCIdentifier(m.name), makeCIdentifier(self.name), makeCIdentifier(before.name), makeCIdentifier(before.memberType.name))
  270. if "false" in self.overlayable:
  271. self.overlayable = "false"
  272. before = m
  273. def typedef_h(self):
  274. if len(self.members) == 0:
  275. return "typedef void * UA_%s;" % makeCIdentifier(self.name)
  276. returnstr = "typedef struct {\n"
  277. for member in self.members:
  278. if member.isArray:
  279. returnstr += " size_t %sSize;\n" % makeCIdentifier(member.name)
  280. returnstr += " UA_%s *%s;\n" % (makeCIdentifier(member.memberType.name), makeCIdentifier(member.name))
  281. else:
  282. returnstr += " UA_%s %s;\n" % (makeCIdentifier(member.memberType.name), makeCIdentifier(member.name))
  283. return returnstr + "} UA_%s;" % makeCIdentifier(self.name)
  284. #########################
  285. # Parse Typedefinitions #
  286. #########################
  287. def parseTypeDefinitions(outname, xmlDescription, namespace):
  288. def typeReady(element):
  289. "Are all member types defined?"
  290. for child in element:
  291. if child.tag == "{http://opcfoundation.org/BinarySchema/}Field":
  292. childname = getTypeName(child.get("TypeName"))
  293. if childname not in types:
  294. return False
  295. return True
  296. def unknownTypes(element):
  297. "Return all unknown types"
  298. unknowns = []
  299. for child in element:
  300. if child.tag == "{http://opcfoundation.org/BinarySchema/}Field":
  301. childname = getTypeName(child.get("TypeName"))
  302. if childname not in types:
  303. unknowns.append(childname)
  304. return unknowns
  305. def skipType(name):
  306. if name in excluded_types:
  307. return True
  308. if re.search("NodeId$", name) != None:
  309. return True
  310. return False
  311. snippets = {}
  312. for typeXml in etree.parse(xmlDescription).getroot():
  313. if not typeXml.get("Name"):
  314. continue
  315. name = typeXml.get("Name")
  316. snippets[name] = typeXml
  317. detectLoop = len(snippets)+1
  318. while(len(snippets) > 0):
  319. if detectLoop == len(snippets):
  320. name, typeXml = (snippets.items())[0]
  321. raise RuntimeError("Infinite loop detected trying to processing types " + name + ": unknonwn subtype " + str(unknownTypes(typeXml)))
  322. detectLoop = len(snippets)
  323. for name, typeXml in list(snippets.items()):
  324. if name in types or skipType(name):
  325. del snippets[name]
  326. continue
  327. if not typeReady(typeXml):
  328. continue
  329. if name in builtin_types:
  330. types[name] = BuiltinType(name)
  331. elif typeXml.tag == "{http://opcfoundation.org/BinarySchema/}EnumeratedType":
  332. types[name] = EnumerationType(outname, typeXml, namespace)
  333. elif typeXml.tag == "{http://opcfoundation.org/BinarySchema/}OpaqueType":
  334. types[name] = OpaqueType(outname, typeXml, namespace, get_base_type_for_opaque(name)['name'])
  335. elif typeXml.tag == "{http://opcfoundation.org/BinarySchema/}StructuredType":
  336. types[name] = StructType(outname, typeXml, namespace)
  337. else:
  338. raise Exception("Type not known")
  339. del snippets[name]
  340. ##########################
  341. # Parse TypeDescriptions #
  342. ##########################
  343. class TypeDescription(object):
  344. def __init__(self, name, nodeid, namespaceid):
  345. self.name = name
  346. self.nodeid = nodeid
  347. self.namespaceid = namespaceid
  348. self.xmlEncodingId = "0"
  349. self.binaryEncodingId = "0"
  350. def parseTypeDescriptions(f, namespaceid):
  351. definitions = {}
  352. csvreader = csv.reader(f, delimiter=',')
  353. delay_init = []
  354. for row in csvreader:
  355. if len(row) < 3:
  356. continue
  357. if row[2] == "Object":
  358. # Check if node name ends with _Encoding_(DefaultXml|DefaultBinary) and store the node id in the corresponding DataType
  359. m = re.match('(.*?)_Encoding_Default(Xml|Binary)$',row[0])
  360. if (m):
  361. baseType = m.group(1)
  362. if baseType not in types:
  363. continue
  364. delay_init.append({
  365. "baseType": baseType,
  366. "encoding": m.group(2),
  367. "id": row[1]
  368. })
  369. continue
  370. if row[2] != "DataType":
  371. continue
  372. if row[0] == "BaseDataType":
  373. definitions["Variant"] = TypeDescription(row[0], row[1], namespaceid)
  374. elif row[0] == "Structure":
  375. definitions["ExtensionObject"] = TypeDescription(row[0], row[1], namespaceid)
  376. elif row[0] not in types:
  377. continue
  378. else:
  379. definitions[row[0]] = TypeDescription(row[0], row[1], namespaceid)
  380. for i in delay_init:
  381. if i["baseType"] not in definitions:
  382. raise Exception("Type {} not found in definitions file.".format(i["baseType"]))
  383. if i["encoding"] == "Xml":
  384. definitions[i["baseType"]].xmlEncodingId = i["id"]
  385. else:
  386. definitions[i["baseType"]].binaryEncodingId = i["id"]
  387. return definitions
  388. def merge_dicts(*dict_args):
  389. """
  390. Given any number of dicts, shallow copy and merge into a new dict,
  391. precedence goes to key value pairs in latter dicts.
  392. """
  393. result = {}
  394. for dictionary in dict_args:
  395. result.update(dictionary)
  396. return result
  397. ###############################
  398. # Parse the Command Line Input#
  399. ###############################
  400. parser = argparse.ArgumentParser()
  401. parser.add_argument('-c', '--type-csv',
  402. metavar="<typeDescriptions>",
  403. type=argparse.FileType('r'),
  404. dest="type_csv",
  405. action='append',
  406. default=[],
  407. help='csv file with type descriptions')
  408. parser.add_argument('--namespace',
  409. type=int,
  410. dest="namespace",
  411. default=0,
  412. help='namespace id of the generated type nodeids (defaults to 0)')
  413. parser.add_argument('-s', '--selected-types',
  414. metavar="<selectedTypes>",
  415. type=argparse.FileType('r'),
  416. dest="selected_types",
  417. action='append',
  418. default=[],
  419. help='file with list of types (among those parsed) to be generated. If not given, all types are generated')
  420. parser.add_argument('--no-builtin',
  421. action='store_true',
  422. dest="no_builtin",
  423. help='Do not generate builtin types')
  424. parser.add_argument('--opaque-map',
  425. metavar="<opaqueTypeMap>",
  426. type=argparse.FileType('r'),
  427. dest="opaque_map",
  428. action='append',
  429. default=[],
  430. help='JSON file with opaque type mapping: { \'typename\': { \'ns\': 0, \'id\': 7, \'name\': \'UInt32\' }, ... }')
  431. parser.add_argument('--internal',
  432. action='store_true',
  433. dest="internal",
  434. help='Given bsd are internal types which do not have any .csv file')
  435. parser.add_argument('-t', '--type-bsd',
  436. metavar="<typeBsds>",
  437. type=argparse.FileType('r'),
  438. dest="type_bsd",
  439. action='append',
  440. default=[],
  441. help='bsd file with type definitions')
  442. parser.add_argument('outfile',
  443. metavar='<outputFile>',
  444. help='output file w/o extension')
  445. args = parser.parse_args()
  446. outname = args.outfile.split("/")[-1]
  447. inname = ', '.join(list(map(lambda x:x.name.split("/")[-1], args.type_bsd)))
  448. isInternalTypes = args.internal
  449. ################
  450. # Create Types #
  451. ################
  452. for builtin in builtin_types:
  453. types[builtin] = BuiltinType(builtin)
  454. for f in args.opaque_map:
  455. user_opaque_type_mapping.update(json.load(f))
  456. for f in args.type_bsd:
  457. parseTypeDefinitions(outname, f, args.namespace)
  458. typedescriptions = {}
  459. for f in args.type_csv:
  460. typedescriptions = merge_dicts(typedescriptions, parseTypeDescriptions(f, args.namespace))
  461. # Read the selected data types
  462. selected_types = []
  463. for f in args.selected_types:
  464. selected_types += list(filter(len, [line.strip() for line in f]))
  465. # Use all types if none are selected
  466. if len(selected_types) == 0:
  467. selected_types = types.keys()
  468. #############################
  469. # Write out the Definitions #
  470. #############################
  471. fh = open(args.outfile + "_generated.h", 'w')
  472. ff = open(args.outfile + "_generated_handling.h", 'w')
  473. fe = open(args.outfile + "_generated_encoding_binary.h", 'w')
  474. fc = open(args.outfile + "_generated.c",'w')
  475. def printh(string):
  476. print(string, end='\n', file=fh)
  477. def printf(string):
  478. print(string, end='\n', file=ff)
  479. def printe(string):
  480. print(string, end='\n', file=fe)
  481. def printc(string):
  482. print(string, end='\n', file=fc)
  483. def iter_types(v):
  484. l = None
  485. if sys.version_info[0] < 3:
  486. l = list(v.itervalues())
  487. else:
  488. l = list(v.values())
  489. if len(selected_types) > 0:
  490. l = list(filter(lambda t: t.name in selected_types, l))
  491. if args.no_builtin:
  492. l = list(filter(lambda t: type(t) != BuiltinType, l))
  493. return l
  494. ################
  495. # Print Header #
  496. ################
  497. printh('''/* Generated from ''' + inname + ''' with script ''' + sys.argv[0] + '''
  498. * on host ''' + platform.uname()[1] + ''' by user ''' + getpass.getuser() + \
  499. ''' at ''' + time.strftime("%Y-%m-%d %I:%M:%S") + ''' */
  500. #ifndef ''' + outname.upper() + '''_GENERATED_H_
  501. #define ''' + outname.upper() + '''_GENERATED_H_
  502. #ifdef UA_ENABLE_AMALGAMATION
  503. #include "open62541.h"
  504. #else
  505. #include "ua_types.h"
  506. ''' + ('#include "ua_types_generated.h"\n' if outname != "ua_types" else '') + '''
  507. #endif
  508. _UA_BEGIN_DECLS
  509. ''')
  510. filtered_types = iter_types(types)
  511. printh('''/**
  512. * Every type is assigned an index in an array containing the type descriptions.
  513. * These descriptions are used during type handling (copying, deletion,
  514. * binary encoding, ...). */''')
  515. printh("#define " + outname.upper() + "_COUNT %s" % (str(len(filtered_types))))
  516. printh("extern UA_EXPORT const UA_DataType " + outname.upper() + "[" + outname.upper() + "_COUNT];")
  517. for i, t in enumerate(filtered_types):
  518. printh("\n/**\n * " + t.name)
  519. printh(" * " + "^" * len(t.name))
  520. if t.description == "":
  521. printh(" */")
  522. else:
  523. printh(" * " + t.description + " */")
  524. if type(t) != BuiltinType:
  525. printh(t.typedef_h() + "\n")
  526. printh("#define " + makeCIdentifier(outname.upper() + "_" + t.name.upper()) + " " + str(i))
  527. printh('''
  528. _UA_END_DECLS
  529. #endif /* %s_GENERATED_H_ */''' % outname.upper())
  530. ##################
  531. # Print Handling #
  532. ##################
  533. printf('''/* Generated from ''' + inname + ''' with script ''' + sys.argv[0] + '''
  534. * on host ''' + platform.uname()[1] + ''' by user ''' + getpass.getuser() + \
  535. ''' at ''' + time.strftime("%Y-%m-%d %I:%M:%S") + ''' */
  536. #ifndef ''' + outname.upper() + '''_GENERATED_HANDLING_H_
  537. #define ''' + outname.upper() + '''_GENERATED_HANDLING_H_
  538. #include "''' + outname + '''_generated.h"
  539. _UA_BEGIN_DECLS
  540. #if defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 6
  541. # pragma GCC diagnostic push
  542. # pragma GCC diagnostic ignored "-Wmissing-field-initializers"
  543. # pragma GCC diagnostic ignored "-Wmissing-braces"
  544. #endif
  545. ''')
  546. for t in filtered_types:
  547. printf("\n/* " + t.name + " */")
  548. printf(t.functions_c())
  549. printf('''
  550. #if defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 6
  551. # pragma GCC diagnostic pop
  552. #endif
  553. _UA_END_DECLS
  554. #endif /* %s_GENERATED_HANDLING_H_ */''' % outname.upper())
  555. ###########################
  556. # Print Description Array #
  557. ###########################
  558. printc('''/* Generated from ''' + inname + ''' with script ''' + sys.argv[0] + '''
  559. * on host ''' + platform.uname()[1] + ''' by user ''' + getpass.getuser() + \
  560. ''' at ''' + time.strftime("%Y-%m-%d %I:%M:%S") + ''' */
  561. #include "''' + outname + '''_generated.h"''')
  562. for t in filtered_types:
  563. printc("")
  564. printc("/* " + t.name + " */")
  565. printc(t.members_c())
  566. printc("const UA_DataType %s[%s_COUNT] = {" % (outname.upper(), outname.upper()))
  567. for t in filtered_types:
  568. printc("/* " + t.name + " */")
  569. printc(t.datatype_c() + ",")
  570. printc("};\n")
  571. ##################
  572. # Print Encoding #
  573. ##################
  574. printe('''/* Generated from ''' + inname + ''' with script ''' + sys.argv[0] + '''
  575. * on host ''' + platform.uname()[1] + ''' by user ''' + getpass.getuser() + \
  576. ''' at ''' + time.strftime("%Y-%m-%d %I:%M:%S") + ''' */
  577. #ifdef UA_ENABLE_AMALGAMATION
  578. # include "open62541.h"
  579. #else
  580. # include "ua_types_encoding_binary.h"
  581. # include "''' + outname + '''_generated.h"
  582. #endif
  583. ''')
  584. for t in filtered_types:
  585. printe("\n/* " + t.name + " */")
  586. printe(t.encoding_h())
  587. fh.close()
  588. ff.close()
  589. fc.close()
  590. fe.close()