macros_public.cmake 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. # --------------- Generate NodeIDs header ---------------------
  2. #
  3. # Generates header file from .csv which contains defines for every
  4. # node id to be used instead of numeric node ids.
  5. #
  6. # The resulting files will be put into OUTPUT_DIR with the names:
  7. # - NAME.h
  8. #
  9. #
  10. # The following arguments are accepted:
  11. # Options:
  12. #
  13. # Arguments taking one value:
  14. #
  15. # NAME Full name of the generated files, e.g. di_nodeids
  16. # TARGET_SUFFIX Suffix for the resulting target. e.g. ids-di
  17. # [TARGET_PREFIX] Optional prefix for the resulting target. Default `open62541-generator`
  18. # ID_PREFIX Prefix for the generated node ID defines, e.g. NS_DI
  19. # [OUTPUT_DIR] Optional target directory for the generated files. Default is '${PROJECT_BINARY_DIR}/src_generated'
  20. # FILE_CSV Path to the .csv file containing the node ids, e.g. 'OpcUaDiModel.csv'
  21. #
  22. function(ua_generate_nodeid_header)
  23. set(options )
  24. set(oneValueArgs NAME ID_PREFIX OUTPUT_DIR FILE_CSV TARGET_SUFFIX TARGET_PREFIX)
  25. set(multiValueArgs )
  26. cmake_parse_arguments(UA_GEN_ID "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
  27. if(NOT UA_GEN_ID_TARGET_SUFFIX OR "${UA_GEN_ID_TARGET_SUFFIX}" STREQUAL "")
  28. message(FATAL_ERROR "ua_generate_nodeid_header function requires a value for the TARGET_SUFFIX argument")
  29. endif()
  30. # Set default value for output dir
  31. if(NOT UA_GEN_ID_OUTPUT_DIR OR "${UA_GEN_ID_OUTPUT_DIR}" STREQUAL "")
  32. set(UA_GEN_ID_OUTPUT_DIR ${PROJECT_BINARY_DIR}/src_generated)
  33. endif()
  34. # Set default target prefix
  35. if(NOT UA_GEN_ID_TARGET_PREFIX OR "${UA_GEN_ID_TARGET_PREFIX}" STREQUAL "")
  36. set(UA_GEN_ID_TARGET_PREFIX "open62541-generator")
  37. endif()
  38. add_custom_target(${UA_GEN_ID_TARGET_PREFIX}-${UA_GEN_ID_TARGET_SUFFIX} DEPENDS
  39. ${UA_GEN_ID_OUTPUT_DIR}/${UA_GEN_ID_NAME}.h
  40. )
  41. # Header containing defines for all NodeIds
  42. add_custom_command(OUTPUT ${UA_GEN_ID_OUTPUT_DIR}/${UA_GEN_ID_NAME}.h
  43. PRE_BUILD
  44. COMMAND ${PYTHON_EXECUTABLE} ${open62541_TOOLS_DIR}/generate_nodeid_header.py
  45. ${UA_GEN_ID_FILE_CSV} ${UA_GEN_ID_OUTPUT_DIR}/${UA_GEN_ID_NAME} ${UA_GEN_ID_ID_PREFIX}
  46. DEPENDS ${open62541_TOOLS_DIR}/generate_nodeid_header.py
  47. ${UA_GEN_ID_FILE_CSV})
  48. endfunction()
  49. # --------------- Generate Datatypes ---------------------
  50. #
  51. # Generates Datatype definition based on the .csv and .bsd files of a nodeset.
  52. # The result of the generation will be C Code which can be compiled with the rest of the stack.
  53. # Some nodesets come with custom datatypes. These datatype structures first need to be
  54. # generated so that the nodeset can use these types.
  55. #
  56. # The resulting files will be put into OUTPUT_DIR with the names:
  57. # - NAME_generated.c
  58. # - NAME_generated.h
  59. # - NAME_generated_encoding_binary.h
  60. # - NAME_generated_handling.h
  61. #
  62. # The cmake resulting cmake target will be named like this:
  63. # open62541-generator-${TARGET_SUFFIX}
  64. #
  65. # The following arguments are accepted:
  66. # Options:
  67. #
  68. # [BUILTIN] Optional argument. If given, then builtin types will be generated.
  69. #
  70. # Arguments taking one value:
  71. #
  72. # NAME Full name of the generated files, e.g. ua_types_di
  73. # TARGET_SUFFIX Suffix for the resulting target. e.g. types-di
  74. # [TARGET_PREFIX] Optional prefix for the resulting target. Default `open62541-generator`
  75. # NAMESPACE_IDX Namespace index of the nodeset, when it is loaded into the server. This index
  76. # is used for the node ids withing the types array and is currently not determined automatically.
  77. # Make sure that it matches the namespace index in the server.
  78. # [OUTPUT_DIR] Optional target directory for the generated files. Default is '${PROJECT_BINARY_DIR}/src_generated'
  79. # FILE_CSV Path to the .csv file containing the node ids, e.g. 'OpcUaDiModel.csv'
  80. #
  81. # Arguments taking multiple values:
  82. #
  83. # FILES_BSD Path to the .bsd file containing the type definitions, e.g. 'Opc.Ua.Di.Types.bsd'. Multiple files can be
  84. # passed which will all combined to one resulting code.
  85. # [FILES_SELECTED] Optional path to a simple text file which contains a list of types which should be included in the generation.
  86. # The file should contain one type per line. Multiple files can be passed to this argument.
  87. #
  88. #
  89. function(ua_generate_datatypes)
  90. set(options BUILTIN)
  91. set(oneValueArgs NAME TARGET_SUFFIX TARGET_PREFIX NAMESPACE_IDX OUTPUT_DIR FILE_CSV)
  92. set(multiValueArgs FILES_BSD FILES_SELECTED)
  93. cmake_parse_arguments(UA_GEN_DT "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
  94. if(NOT DEFINED open62541_TOOLS_DIR)
  95. message(FATAL_ERROR "open62541_TOOLS_DIR must point to the open62541 tools directory")
  96. endif()
  97. # ------ Argument checking -----
  98. if(NOT DEFINED UA_GEN_DT_NAMESPACE_IDX AND NOT "${UA_GEN_DT_NAMESPACE_IDX}" STREQUAL "0")
  99. message(FATAL_ERROR "ua_generate_datatype function requires a value for the NAMESPACE_IDX argument")
  100. endif()
  101. if(NOT UA_GEN_DT_NAME OR "${UA_GEN_DT_NAME}" STREQUAL "")
  102. message(FATAL_ERROR "ua_generate_datatype function requires a value for the NAME argument")
  103. endif()
  104. if(NOT UA_GEN_DT_TARGET_SUFFIX OR "${UA_GEN_DT_TARGET_SUFFIX}" STREQUAL "")
  105. message(FATAL_ERROR "ua_generate_datatype function requires a value for the TARGET_SUFFIX argument")
  106. endif()
  107. if(NOT UA_GEN_DT_FILE_CSV OR "${UA_GEN_DT_FILE_CSV}" STREQUAL "")
  108. message(FATAL_ERROR "ua_generate_datatype function requires a value for the FILE_CSV argument")
  109. endif()
  110. if(NOT UA_GEN_DT_FILES_BSD OR "${UA_GEN_DT_FILES_BSD}" STREQUAL "")
  111. message(FATAL_ERROR "ua_generate_datatype function requires a value for the FILES_BSD argument")
  112. endif()
  113. # Set default value for output dir
  114. if(NOT UA_GEN_DT_OUTPUT_DIR OR "${UA_GEN_DT_OUTPUT_DIR}" STREQUAL "")
  115. set(UA_GEN_DT_OUTPUT_DIR ${PROJECT_BINARY_DIR}/src_generated)
  116. endif()
  117. # Set default target prefix
  118. if(NOT UA_GEN_DT_TARGET_PREFIX OR "${UA_GEN_DT_TARGET_PREFIX}" STREQUAL "")
  119. set(UA_GEN_DT_TARGET_PREFIX "open62541-generator")
  120. endif()
  121. # ------ Add custom command and target -----
  122. set(UA_GEN_DT_NO_BUILTIN "--no-builtin")
  123. if (UA_GEN_DT_BUILTIN)
  124. set(UA_GEN_DT_NO_BUILTIN "")
  125. endif()
  126. set(SELECTED_TYPES_TMP "")
  127. foreach(f ${UA_GEN_DT_FILES_SELECTED})
  128. set(SELECTED_TYPES_TMP ${SELECTED_TYPES_TMP} "--selected-types=${f}")
  129. endforeach()
  130. set(BSD_FILES_TMP "")
  131. foreach(f ${UA_GEN_DT_FILES_BSD})
  132. set(BSD_FILES_TMP ${BSD_FILES_TMP} "--type-bsd=${f}")
  133. endforeach()
  134. add_custom_command(OUTPUT ${UA_GEN_DT_OUTPUT_DIR}/${UA_GEN_DT_NAME}_generated.c
  135. ${UA_GEN_DT_OUTPUT_DIR}/${UA_GEN_DT_NAME}_generated.h
  136. ${UA_GEN_DT_OUTPUT_DIR}/${UA_GEN_DT_NAME}_generated_handling.h
  137. ${UA_GEN_DT_OUTPUT_DIR}/${UA_GEN_DT_NAME}_generated_encoding_binary.h
  138. PRE_BUILD
  139. COMMAND ${PYTHON_EXECUTABLE} ${open62541_TOOLS_DIR}/generate_datatypes.py
  140. --namespace=${UA_GEN_DT_NAMESPACE_IDX}
  141. ${SELECTED_TYPES_TMP}
  142. ${BSD_FILES_TMP}
  143. --type-csv=${UA_GEN_DT_FILE_CSV}
  144. ${UA_GEN_DT_NO_BUILTIN}
  145. ${UA_GEN_DT_OUTPUT_DIR}/${UA_GEN_DT_NAME}
  146. DEPENDS ${open62541_TOOLS_DIR}/generate_datatypes.py
  147. ${UA_GEN_DT_FILES_BSD}
  148. ${UA_GEN_DT_FILE_CSV}
  149. ${UA_GEN_DT_FILES_SELECTED})
  150. add_custom_target(${UA_GEN_DT_TARGET_PREFIX}-${UA_GEN_DT_TARGET_SUFFIX} DEPENDS
  151. ${UA_GEN_DT_OUTPUT_DIR}/${UA_GEN_DT_NAME}_generated.c
  152. ${UA_GEN_DT_OUTPUT_DIR}/${UA_GEN_DT_NAME}_generated.h
  153. ${UA_GEN_DT_OUTPUT_DIR}/${UA_GEN_DT_NAME}_generated_handling.h
  154. ${UA_GEN_DT_OUTPUT_DIR}/${UA_GEN_DT_NAME}_generated_encoding_binary.h
  155. )
  156. string(TOUPPER "${UA_GEN_DT_NAME}" GEN_NAME_UPPER)
  157. set(${GEN_NAME_UPPER}_SOURCES "${UA_GEN_DT_OUTPUT_DIR}/${UA_GEN_DT_NAME}_generated.c" CACHE INTERNAL "${UA_GEN_DT_NAME} source files")
  158. set(${GEN_NAME_UPPER}_HEADERS "${UA_GEN_DT_OUTPUT_DIR}/${UA_GEN_DT_NAME}_generated.h;${UA_GEN_DT_OUTPUT_DIR}/${UA_GEN_DT_NAME}_generated_handling.h;${UA_GEN_DT_OUTPUT_DIR}/${UA_GEN_DT_NAME}_generated_encoding_binary.h"
  159. CACHE INTERNAL "${UA_GEN_DT_NAME} header files")
  160. if(UA_COMPILE_AS_CXX)
  161. set_source_files_properties(${UA_GEN_DT_OUTPUT_DIR}/${UA_GEN_DT_NAME}_generated.c PROPERTIES LANGUAGE CXX)
  162. endif()
  163. endfunction()
  164. # --------------- Generate Nodeset ---------------------
  165. #
  166. # Generates C code for the given NodeSet2.xml file.
  167. # This C code can be used to initialize the server.
  168. #
  169. # The resulting files will be put into OUTPUT_DIR with the names:
  170. # - ua_namespace_NAME.c
  171. # - ua_namespace_NAME.h
  172. #
  173. # The resulting cmake target will be named like this:
  174. # open62541-generator-ns-${NAME}
  175. #
  176. # The following arguments are accepted:
  177. # Options:
  178. #
  179. # [INTERNAL] Optional argument. If given, then the generated node set code will use internal headers.
  180. #
  181. # Arguments taking one value:
  182. #
  183. # NAME Name of the nodeset, e.g. 'di'
  184. # [TYPES_ARRAY] Optional name of the types array containing the custom datatypes of this node set.
  185. # [OUTPUT_DIR] Optional target directory for the generated files. Default is '${PROJECT_BINARY_DIR}/src_generated'
  186. # [ENCODE_BINARY_SIZE] Optional array size for binary encoding extension objects.
  187. # [IGNORE] Optional file containing a list of node ids which should be ignored. The file should have one id per line.
  188. # [TARGET_PREFIX] Optional prefix for the resulting target. Default `open62541-generator`
  189. #
  190. # Arguments taking multiple values:
  191. #
  192. # FILE Path to the NodeSet2.xml file. Multiple values can be passed. These nodesets will be combined into one output.
  193. # [DEPENDS_TYPES] Optional list of types array which match with the DEPENDS_NS node sets. e.g. 'UA_TYPES;UA_TYPES_DI'
  194. # [DEPENDS_NS] Optional list of NodeSet2.xml files which are a dependency of this node set.
  195. # [DEPENDS_TARGET] Optional list of CMake targets this nodeset depends on.
  196. #
  197. #
  198. function(ua_generate_nodeset)
  199. set(options INTERNAL )
  200. set(oneValueArgs NAME TYPES_ARRAY OUTPUT_DIR ENCODE_BINARY_SIZE IGNORE TARGET_PREFIX)
  201. set(multiValueArgs FILE DEPENDS_TYPES DEPENDS_NS DEPENDS_TARGET)
  202. cmake_parse_arguments(UA_GEN_NS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
  203. if(NOT DEFINED open62541_TOOLS_DIR)
  204. message(FATAL_ERROR "open62541_TOOLS_DIR must point to the open62541 tools directory")
  205. endif()
  206. # ------ Argument checking -----
  207. if(NOT UA_GEN_NS_NAME OR "${UA_GEN_NS_NAME}" STREQUAL "")
  208. message(FATAL_ERROR "ua_generate_nodeset function requires a value for the NAME argument")
  209. endif()
  210. if(NOT UA_GEN_NS_FILE OR "${UA_GEN_NS_FILE}" STREQUAL "")
  211. message(FATAL_ERROR "ua_generate_nodeset function requires a value for the FILE argument")
  212. endif()
  213. # Set default value for output dir
  214. if(NOT UA_GEN_NS_OUTPUT_DIR OR "${UA_GEN_NS_OUTPUT_DIR}" STREQUAL "")
  215. set(UA_GEN_NS_OUTPUT_DIR ${PROJECT_BINARY_DIR}/src_generated)
  216. endif()
  217. list(LENGTH UA_GEN_NS_DEPENDS_TYPES DEPENDS_TYPES_LEN)
  218. list(LENGTH UA_GEN_NS_DEPENDS_NS DEPENDS_NS_LEN)
  219. if(NOT DEPENDS_TYPES_LEN EQUAL DEPENDS_NS_LEN)
  220. message(FATAL_ERROR "ua_generate_nodeset parameters DEPENDS_NS and DEPENDS_TYPES must have the same number of list elements")
  221. endif()
  222. # Set default target prefix
  223. if(NOT UA_GEN_NS_TARGET_PREFIX OR "${UA_GEN_NS_TARGET_PREFIX}" STREQUAL "")
  224. set(UA_GEN_NS_TARGET_PREFIX "open62541-generator")
  225. endif()
  226. # ------ Add custom command and target -----
  227. set(GEN_INTERNAL_HEADERS "")
  228. if (UA_GEN_NS_INTERNAL)
  229. set(GEN_INTERNAL_HEADERS "--internal-headers")
  230. endif()
  231. set(GEN_NS0 "")
  232. set(TARGET_SUFFIX "ns-${UA_GEN_NS_NAME}")
  233. set(FILE_SUFFIX "_${UA_GEN_NS_NAME}")
  234. string(REPLACE "-" "_" FILE_SUFFIX ${FILE_SUFFIX})
  235. if ("${UA_GEN_NS_NAME}" STREQUAL "ns0")
  236. set(GEN_NS0 "--generate-ns0")
  237. set(TARGET_SUFFIX "namespace")
  238. set(FILE_SUFFIX "0")
  239. endif()
  240. set(GEN_IGNORE "")
  241. if (UA_GEN_NS_IGNORE)
  242. set(GEN_IGNORE "--ignore=${UA_GEN_NS_IGNORE}")
  243. endif()
  244. set(GEN_BIN_SIZE "")
  245. if (UA_GEN_NS_ENCODE_BINARY_SIZE OR "${UA_GEN_NS_ENCODE_BINARY_SIZE}" STREQUAL "0")
  246. set(GEN_BIN_SIZE "--encode-binary-size=${UA_GEN_NS_ENCODE_BINARY_SIZE}")
  247. endif()
  248. set(TYPES_ARRAY_LIST "")
  249. foreach(f ${UA_GEN_NS_DEPENDS_TYPES})
  250. set(TYPES_ARRAY_LIST ${TYPES_ARRAY_LIST} "--types-array=${f}")
  251. endforeach()
  252. if(UA_GEN_NS_TYPES_ARRAY)
  253. set(TYPES_ARRAY_LIST ${TYPES_ARRAY_LIST} "--types-array=${UA_GEN_NS_TYPES_ARRAY}")
  254. endif()
  255. set(DEPENDS_FILE_LIST "")
  256. foreach(f ${UA_GEN_NS_DEPENDS_NS})
  257. set(DEPENDS_FILE_LIST ${DEPENDS_FILE_LIST} "--existing=${f}")
  258. endforeach()
  259. set(FILE_LIST "")
  260. foreach(f ${UA_GEN_NS_FILE})
  261. set(FILE_LIST ${FILE_LIST} "--xml=${f}")
  262. endforeach()
  263. add_custom_command(OUTPUT ${UA_GEN_NS_OUTPUT_DIR}/ua_namespace${FILE_SUFFIX}.c
  264. ${UA_GEN_NS_OUTPUT_DIR}/ua_namespace${FILE_SUFFIX}.h
  265. PRE_BUILD
  266. COMMAND ${PYTHON_EXECUTABLE} ${open62541_TOOLS_DIR}/nodeset_compiler/nodeset_compiler.py
  267. ${GEN_INTERNAL_HEADERS}
  268. ${GEN_NS0}
  269. ${GEN_BIN_SIZE}
  270. ${GEN_IGNORE}
  271. ${TYPES_ARRAY_LIST}
  272. ${DEPENDS_FILE_LIST}
  273. ${FILE_LIST}
  274. ${UA_GEN_NS_OUTPUT_DIR}/ua_namespace${FILE_SUFFIX}
  275. DEPENDS
  276. ${open62541_TOOLS_DIR}/nodeset_compiler/nodeset_compiler.py
  277. ${open62541_TOOLS_DIR}/nodeset_compiler/nodes.py
  278. ${open62541_TOOLS_DIR}/nodeset_compiler/nodeset.py
  279. ${open62541_TOOLS_DIR}/nodeset_compiler/datatypes.py
  280. ${open62541_TOOLS_DIR}/nodeset_compiler/backend_open62541.py
  281. ${open62541_TOOLS_DIR}/nodeset_compiler/backend_open62541_nodes.py
  282. ${open62541_TOOLS_DIR}/nodeset_compiler/backend_open62541_datatypes.py
  283. ${UA_GEN_NS_FILE}
  284. ${UA_GEN_NS_DEPENDS_NS}
  285. )
  286. add_custom_target(${UA_GEN_NS_TARGET_PREFIX}-${TARGET_SUFFIX}
  287. DEPENDS
  288. ${UA_GEN_NS_OUTPUT_DIR}/ua_namespace${FILE_SUFFIX}.c
  289. ${UA_GEN_NS_OUTPUT_DIR}/ua_namespace${FILE_SUFFIX}.h)
  290. if (UA_GEN_NS_DEPENDS_TARGET)
  291. add_dependencies(${UA_GEN_NS_TARGET_PREFIX}-${TARGET_SUFFIX} ${UA_GEN_NS_DEPENDS_TARGET})
  292. endif()
  293. if(UA_COMPILE_AS_CXX)
  294. set_source_files_properties(${UA_GEN_NS_OUTPUT_DIR}/ua_namespace${FILE_SUFFIX}.c PROPERTIES LANGUAGE CXX)
  295. endif()
  296. set_property(GLOBAL PROPERTY "UA_GEN_NS_DEPENDS_FILE_${UA_GEN_NS_NAME}" ${UA_GEN_NS_DEPENDS_NS} ${UA_GEN_NS_FILE})
  297. set_property(GLOBAL PROPERTY "UA_GEN_NS_DEPENDS_TYPES_${UA_GEN_NS_NAME}" ${UA_GEN_NS_DEPENDS_TYPES} ${UA_GEN_NS_TYPES_ARRAY})
  298. string(TOUPPER "${UA_GEN_NS_NAME}" GEN_NAME_UPPER)
  299. set(UA_NODESET_${GEN_NAME_UPPER}_SOURCES "${UA_GEN_NS_OUTPUT_DIR}/ua_namespace${FILE_SUFFIX}.c" CACHE INTERNAL "UA_NODESET_${GEN_NAME_UPPER} source files")
  300. set(UA_NODESET_${GEN_NAME_UPPER}_HEADERS "${UA_GEN_NS_OUTPUT_DIR}/ua_namespace${FILE_SUFFIX}.h" CACHE INTERNAL "UA_NODESET_${GEN_NAME_UPPER} header files")
  301. set(UA_NODESET_${GEN_NAME_UPPER}_TARGET "${UA_GEN_NS_TARGET_PREFIX}-${TARGET_SUFFIX}" CACHE INTERNAL "UA_NODESET_${GEN_NAME_UPPER} target")
  302. endfunction()
  303. # --------------- Generate Nodeset and Datatypes ---------------------
  304. #
  305. # Generates C code for the given NodeSet2.xml and Datatype file.
  306. # This C code can be used to initialize the server.
  307. #
  308. # This is a combination of the ua_generate_datatypes, ua_generate_nodeset, and
  309. # ua_generate_nodeid_header macros.
  310. # This function can also be used to just create a nodeset without datatypes by
  311. # omitting the CSV, BSD, and NAMESPACE_IDX parameter.
  312. # If only one of the previous parameters is given, all of them are required.
  313. #
  314. # It is possible to define dependencies of nodesets by using the DEPENDS argument.
  315. # E.g. the PLCOpen nodeset depends on the 'di' nodeset. Thus it is enough to just
  316. # pass 'DEPENDS di' to the function. The 'di' nodeset then first needs to be generated
  317. # with this function or with the ua_generate_nodeset function.
  318. #
  319. # The resulting cmake target will be named like this:
  320. # open62541-generator-ns-${NAME}
  321. #
  322. # The following arguments are accepted:
  323. #
  324. # Options:
  325. #
  326. # INTERNAL Include internal headers. Required if custom datatypes are added.
  327. #
  328. # Arguments taking one value:
  329. #
  330. # NAME Short name of the nodeset. E.g. 'di'
  331. # FILE_NS Path to the NodeSet2.xml file. Multiple values can be passed. These nodesets will be combined into one output.
  332. #
  333. # [FILE_CSV] Optional path to the .csv file containing the node ids, e.g. 'OpcUaDiModel.csv'
  334. # [FILE_BSD] Optional path to the .bsd file containing the type definitions, e.g. 'Opc.Ua.Di.Types.bsd'. Multiple files can be
  335. # passed which will all combined to one resulting code.
  336. # [NAMESPACE_IDX] Optional namespace index of the nodeset, when it is loaded into the server. This parameter is mandatory if FILE_CSV
  337. # or FILE_BSD is set. See ua_generate_datatypes function.
  338. # [TARGET_PREFIX] Optional prefix for the resulting targets. Default `open62541-generator`
  339. #
  340. # Arguments taking multiple values:
  341. # [DEPENDS] Optional list of nodeset names on which this nodeset depends. These names must match any name from a previous
  342. # call to this funtion. E.g. 'di' if you are generating the 'plcopen' nodeset
  343. #
  344. #
  345. function(ua_generate_nodeset_and_datatypes)
  346. set(options INTERNAL)
  347. set(oneValueArgs NAME FILE_NS FILE_CSV FILE_BSD NAMESPACE_IDX OUTPUT_DIR TARGET_PREFIX)
  348. set(multiValueArgs DEPENDS)
  349. cmake_parse_arguments(UA_GEN "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
  350. if(NOT DEFINED open62541_TOOLS_DIR)
  351. message(FATAL_ERROR "open62541_TOOLS_DIR must point to the open62541 tools directory")
  352. endif()
  353. if(NOT DEFINED open62541_NODESET_DIR)
  354. message(FATAL_ERROR "open62541_NODESET_DIR must point to the open62541/deps/ua-nodeset directory")
  355. endif()
  356. # ------ Argument checking -----
  357. if(NOT UA_GEN_NAME OR "${UA_GEN_NAME}" STREQUAL "")
  358. message(FATAL_ERROR "ua_generate_nodeset_and_datatypes function requires a value for the NAME argument")
  359. endif()
  360. string(TOUPPER "${UA_GEN_NAME}" GEN_NAME_UPPER)
  361. if(NOT UA_GEN_FILE_NS OR "${UA_GEN_FILE_NS}" STREQUAL "")
  362. message(FATAL_ERROR "ua_generate_nodeset_and_datatypes function requires a value for the FILE_NS argument")
  363. endif()
  364. if((NOT UA_GEN_FILE_CSV OR "${UA_GEN_FILE_CSV}" STREQUAL "") AND
  365. (NOT "${UA_GEN_FILE_BSD}" STREQUAL "" OR
  366. NOT "${UA_GEN_NAMESPACE_IDX}" STREQUAL ""))
  367. message(FATAL_ERROR "ua_generate_nodeset_and_datatypes function requires FILE_CSV argument if any of FILE_BSD or NAMESPACE_IDX are set")
  368. endif()
  369. if((NOT UA_GEN_FILE_BSD OR "${UA_GEN_FILE_BSD}" STREQUAL "") AND
  370. (NOT "${UA_GEN_FILE_CSV}" STREQUAL "" OR
  371. NOT "${UA_GEN_NAMESPACE_IDX}" STREQUAL ""))
  372. message(FATAL_ERROR "ua_generate_nodeset_and_datatypes function requires FILE_BSD argument if any of FILE_CSV or NAMESPACE_IDX are set")
  373. endif()
  374. if(NOT UA_GEN_NAMESPACE_IDX OR "${UA_GEN_NAMESPACE_IDX}" STREQUAL "" AND
  375. (NOT "${UA_GEN_FILE_CSV}" STREQUAL "" OR
  376. NOT "${UA_GEN_FILE_BSD}" STREQUAL ""))
  377. message(FATAL_ERROR "ua_generate_nodeset_and_datatypes function requires NAMESPACE_IDX argument if any of FILE_CSV or FILE_BSD are set")
  378. endif()
  379. # Set default value for output dir
  380. if(NOT UA_GEN_OUTPUT_DIR OR "${UA_GEN_OUTPUT_DIR}" STREQUAL "")
  381. set(UA_GEN_OUTPUT_DIR ${PROJECT_BINARY_DIR}/src_generated)
  382. endif()
  383. # Set default target prefix
  384. if(NOT UA_GEN_TARGET_PREFIX OR "${UA_GEN_TARGET_PREFIX}" STREQUAL "")
  385. set(UA_GEN_TARGET_PREFIX "open62541-generator")
  386. endif()
  387. set(NODESET_DEPENDS_TARGET "")
  388. set(NODESET_TYPES_ARRAY "UA_TYPES")
  389. if(NOT "${UA_GEN_FILE_BSD}" STREQUAL "")
  390. # Generate Datatypes for nodeset
  391. ua_generate_datatypes(
  392. NAME "ua_types_${UA_GEN_NAME}"
  393. TARGET_PREFIX "${UA_GEN_TARGET_PREFIX}"
  394. TARGET_SUFFIX "types-${UA_GEN_NAME}"
  395. NAMESPACE_IDX ${UA_GEN_NAMESPACE_IDX}
  396. FILE_CSV "${UA_GEN_FILE_CSV}"
  397. FILES_BSD "${UA_GEN_FILE_BSD}"
  398. OUTPUT_DIR "${UA_GEN_OUTPUT_DIR}"
  399. )
  400. set(NODESET_DEPENDS_TARGET "${UA_GEN_TARGET_PREFIX}-types-${UA_GEN_NAME}")
  401. set(NODESET_TYPES_ARRAY "UA_TYPES_${GEN_NAME_UPPER}")
  402. ua_generate_nodeid_header(
  403. NAME "${UA_GEN_NAME}_nodeids"
  404. ID_PREFIX "${GEN_NAME_UPPER}"
  405. FILE_CSV "${UA_GEN_FILE_CSV}"
  406. OUTPUT_DIR "${UA_GEN_OUTPUT_DIR}"
  407. TARGET_PREFIX "${UA_GEN_TARGET_PREFIX}"
  408. TARGET_SUFFIX "ids-${UA_GEN_NAME}"
  409. )
  410. set(NODESET_DEPENDS_TARGET ${NODESET_DEPENDS_TARGET} "${UA_GEN_TARGET_PREFIX}-ids-${UA_GEN_NAME}")
  411. endif()
  412. # Create a list of nodesets on which this nodeset depends on
  413. if (NOT UA_GEN_DEPENDS OR "${UA_GEN_DEPENDS}" STREQUAL "" )
  414. set(NODESET_DEPENDS "${open62541_NODESET_DIR}/Schema/Opc.Ua.NodeSet2.xml")
  415. set(TYPES_DEPENDS "UA_TYPES")
  416. else()
  417. foreach(f ${UA_GEN_DEPENDS})
  418. get_property(DEPENDS_FILE GLOBAL PROPERTY "UA_GEN_NS_DEPENDS_FILE_${f}")
  419. if(NOT DEPENDS_FILE OR "${DEPENDS_FILE}" STREQUAL "")
  420. message(FATAL_ERROR "Nodeset dependency ${f} needs to be generated before ${UA_GEN_NAME}")
  421. endif()
  422. set(NODESET_DEPENDS ${NODESET_DEPENDS} "${DEPENDS_FILE}")
  423. get_property(DEPENDS_TYPES GLOBAL PROPERTY "UA_GEN_NS_DEPENDS_TYPES_${f}")
  424. set(TYPES_DEPENDS ${TYPES_DEPENDS} "${DEPENDS_TYPES}")
  425. set(NODESET_DEPENDS_TARGET ${NODESET_DEPENDS_TARGET} "${UA_GEN_TARGET_PREFIX}-ns-${f}")
  426. endforeach()
  427. endif()
  428. set(NODESET_INTERNAL "")
  429. if (${UA_GEN_INTERNAL})
  430. set(NODESET_INTERNAL "INTERNAL")
  431. endif()
  432. ua_generate_nodeset(
  433. NAME "${UA_GEN_NAME}"
  434. FILE "${UA_GEN_FILE_NS}"
  435. TYPES_ARRAY "${NODESET_TYPES_ARRAY}"
  436. ${NODESET_INTERNAL}
  437. DEPENDS_TYPES ${TYPES_DEPENDS}
  438. DEPENDS_NS ${NODESET_DEPENDS}
  439. DEPENDS_TARGET ${NODESET_DEPENDS_TARGET}
  440. OUTPUT_DIR "${UA_GEN_OUTPUT_DIR}"
  441. TARGET_PREFIX "${UA_GEN_TARGET_PREFIX}"
  442. )
  443. endfunction()