macros_public.cmake 23 KB

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