CMakeLists.txt 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. cmake_minimum_required(VERSION 3.0)
  2. project(open62541)
  3. # set(CMAKE_VERBOSE_MAKEFILE ON)
  4. set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/tools/cmake")
  5. find_package(PythonInterp REQUIRED)
  6. find_package(Git)
  7. include(AssignSourceGroup)
  8. #############################
  9. # Compiled binaries folders #
  10. #############################
  11. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  12. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  13. set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  14. set(LIB_INSTALL_DIR /usr/lib CACHE PATH "Installation path of libraries")
  15. ###########
  16. # Version #
  17. ###########
  18. set(OPEN62541_VER_MAJOR 0)
  19. set(OPEN62541_VER_MINOR 3)
  20. set(OPEN62541_VER_PATCH 0)
  21. set(OPEN62541_VER_LABEL "dev") # Appended to the X.Y.Z version format. For example "-rc1" or an empty string
  22. set(LIB_INSTALL_DIR lib CACHE PATH "Installation path of libraries")
  23. # Set OPEN62541_VER_COMMIT
  24. if(GIT_FOUND)
  25. execute_process(COMMAND ${GIT_EXECUTABLE} describe --always --tags
  26. RESULT_VARIABLE res_var OUTPUT_VARIABLE GIT_COM_ID WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
  27. if(${res_var} EQUAL 0)
  28. string(REPLACE "\n" "" OPEN62541_VER_COMMIT ${GIT_COM_ID} )
  29. endif()
  30. endif()
  31. if(NOT OPEN62541_VER_COMMIT OR OPEN62541_VER_COMMIT STREQUAL "")
  32. set(OPEN62541_VER_COMMIT "undefined")
  33. endif()
  34. #################
  35. # Build Options #
  36. #################
  37. # Set default build type.
  38. if(NOT CMAKE_BUILD_TYPE)
  39. message(STATUS "CMAKE_BUILD_TYPE not given; setting to 'Debug'")
  40. set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build" FORCE)
  41. endif()
  42. # Options
  43. set(UA_LOGLEVEL 300 CACHE STRING "Level at which logs shall be reported")
  44. option(UA_ENABLE_METHODCALLS "Enable the Method service set" ON)
  45. option(UA_ENABLE_NODEMANAGEMENT "Enable dynamic addition and removal of nodes at runtime" ON)
  46. option(UA_ENABLE_SUBSCRIPTIONS "Enable subscriptions support." ON)
  47. option(UA_ENABLE_DISCOVERY "Enable Discovery Service (LDS)" ON)
  48. option(UA_ENABLE_DISCOVERY_MULTICAST "Enable Discovery Service with multicast support (LDS-ME)" OFF)
  49. # Semaphores/file system may not be available on embedded devices. It can be disabled with the following option
  50. option(UA_ENABLE_DISCOVERY_SEMAPHORE "Enable Discovery Semaphore support" ON)
  51. mark_as_advanced(UA_ENABLE_DISCOVERY_SEMAPHORE)
  52. option(UA_ENABLE_AMALGAMATION "Concatenate the library to a single file open62541.h/.c" OFF)
  53. option(UA_ENABLE_COVERAGE "Enable gcov coverage" OFF)
  54. option(BUILD_SHARED_LIBS "Enable building of shared libraries (dll/so)" OFF)
  55. set(UA_VXWORKS_WRS_KERNEL OFF CACHE BOOL "Enable if you want to compile for VxWorks as kernel Module")
  56. if(UA_ENABLE_COVERAGE)
  57. set(CMAKE_BUILD_TYPE DEBUG)
  58. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
  59. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage -lgcov")
  60. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
  61. endif()
  62. if(UA_ENABLE_DISCOVERY_MULTICAST AND NOT UA_ENABLE_DISCOVERY)
  63. MESSAGE(WARNING "UA_ENABLE_DISCOVERY_MULTICAST is enabled, but not UA_ENABLE_DISCOVERY. UA_ENABLE_DISCOVERY_MULTICAST will be set to OFF")
  64. SET(UA_ENABLE_DISCOVERY_MULTICAST OFF CACHE BOOL "Enable Discovery Service with multicast support (LDS-ME)" FORCE)
  65. endif()
  66. # Advanced options
  67. option(UA_ENABLE_MULTITHREADING "Enable multithreading (experimental)" OFF)
  68. mark_as_advanced(UA_ENABLE_MULTITHREADING)
  69. option(UA_ENABLE_STATUSCODE_DESCRIPTIONS "Enable conversion of StatusCode to human-readable error message" ON)
  70. mark_as_advanced(UA_ENABLE_STATUSCODE_DESCRIPTIONS)
  71. option(UA_ENABLE_TYPENAMES "Add the type and member names to the UA_DataType structure" ON)
  72. mark_as_advanced(UA_ENABLE_TYPENAMES)
  73. option(UA_ENABLE_EMBEDDED_LIBC "Use a custom implementation of some libc functions that might be missing on embedded targets (e.g. string handling)." OFF)
  74. mark_as_advanced(UA_ENABLE_EMBEDDED_LIBC)
  75. option(UA_ENABLE_DETERMINISTIC_RNG "Do not seed the random number generator (e.g. for unit tests)." OFF)
  76. mark_as_advanced(UA_ENABLE_DETERMINISTIC_RNG)
  77. option(UA_ENABLE_FULL_NS0 "Use the full NS0 instead of a minimal Namespace 0 nodeset" OFF)
  78. option(UA_ENABLE_VALGRIND_UNIT_TESTS "Use Valgrind to detect memory leaks when running the unit tests" OFF)
  79. mark_as_advanced(UA_ENABLE_VALGRIND_UNIT_TESTS)
  80. option(UA_ENABLE_NONSTANDARD_UDP "Enable udp extension (non-standard)" OFF)
  81. mark_as_advanced(UA_ENABLE_NONSTANDARD_UDP)
  82. # Build Targets
  83. option(UA_BUILD_EXAMPLES "Build example servers and clients" OFF)
  84. option(UA_BUILD_UNIT_TESTS "Build the unit tests" OFF)
  85. option(UA_BUILD_FUZZING "Build the fuzzing executables" OFF)
  86. mark_as_advanced(UA_BUILD_FUZZING)
  87. if (UA_BUILD_FUZZING)
  88. # oss-fuzz already defines this by default
  89. add_definitions(-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
  90. endif()
  91. option(UA_BUILD_OSS_FUZZ "Special build switch used in oss-fuzz" OFF)
  92. mark_as_advanced(UA_BUILD_OSS_FUZZ)
  93. option(UA_DEBUG_DUMP_PKGS "Dump every package received by the server as hexdump format" OFF)
  94. mark_as_advanced(UA_DEBUG_DUMP_PKGS)
  95. # Advanced Build Targets
  96. option(UA_BUILD_SELFSIGNED_CERTIFICATE "Generate self-signed certificate" OFF)
  97. mark_as_advanced(UA_BUILD_SELFSIGNED_CERTIFICATE)
  98. # Building shared libs (dll, so). This option is written into ua_config.h.
  99. set(UA_DYNAMIC_LINKING OFF)
  100. if(BUILD_SHARED_LIBS)
  101. set(UA_DYNAMIC_LINKING ON)
  102. if (UA_ENABLE_DISCOVERY_MULTICAST)
  103. set(MDNSD_DYNAMIC_LINKING ON)
  104. endif()
  105. endif()
  106. # Force compilation with as C++
  107. option(UA_COMPILE_AS_CXX "Force compilation with a C++ compiler" OFF)
  108. mark_as_advanced(UA_COMPILE_AS_CXX)
  109. if (UA_COMPILE_AS_CXX)
  110. # We need the UINT32_C define
  111. add_definitions(-D__STDC_CONSTANT_MACROS)
  112. endif()
  113. #####################
  114. # Compiler Settings #
  115. #####################
  116. # Collect libraries
  117. list(APPEND open62541_LIBRARIES "")
  118. if(NOT WIN32)
  119. if(QNXNTO)
  120. list(APPEND open62541_LIBRARIES socket)
  121. list(APPEND open62541_LIBRARIES c)
  122. list(APPEND open62541_LIBRARIES stdc++)
  123. else()
  124. list(APPEND open62541_LIBRARIES pthread m)
  125. if(NOT APPLE AND (NOT ${CMAKE_SYSTEM_NAME} MATCHES "OpenBSD"))
  126. list(APPEND open62541_LIBRARIES rt)
  127. endif()
  128. endif()
  129. else()
  130. list(APPEND open62541_LIBRARIES ws2_32)
  131. if(UA_ENABLE_DISCOVERY_MULTICAST)
  132. list(APPEND open62541_LIBRARIES iphlpapi)
  133. endif()
  134. endif()
  135. if(UA_ENABLE_MULTITHREADING)
  136. list(APPEND open62541_LIBRARIES urcu-cds urcu-bp urcu-common)
  137. endif(UA_ENABLE_MULTITHREADING)
  138. if(NOT UA_COMPILE_AS_CXX AND (CMAKE_COMPILER_IS_GNUCC OR "x${CMAKE_C_COMPILER_ID}" STREQUAL "xClang"))
  139. # Compiler
  140. add_definitions(-std=c99 -pipe
  141. -Wall -Wextra -Werror -Wpedantic
  142. -Wno-static-in-inline # clang doesn't like the use of static inline methods inside static inline methods
  143. -Wno-overlength-strings # may happen in the nodeset compiler when complex values are directly encoded
  144. -Wno-unused-parameter # some methods may require unused arguments to cast to a method pointer
  145. -Wmissing-prototypes -Wstrict-prototypes -Wredundant-decls
  146. -Wformat -Wformat-security -Wformat-nonliteral
  147. -Wuninitialized -Winit-self
  148. -Wcast-qual
  149. -Wstrict-overflow
  150. -Wnested-externs
  151. -Wmultichar
  152. -Wundef
  153. -Wc++-compat)
  154. if(NOT WIN32 AND NOT CYGWIN AND NOT QNXNTO AND (NOT ${CMAKE_SYSTEM_NAME} MATCHES "OpenBSD"))
  155. add_definitions(-Wshadow -Wconversion -fvisibility=hidden -fPIC)
  156. endif()
  157. if(UA_ENABLE_AMALGAMATION)
  158. add_definitions(-Wno-unused-function)
  159. endif()
  160. if(UA_VXWORKS_WRS_KERNEL)
  161. # Disable flags for VXWORKS
  162. remove_definitions(-Werror -Wpedantic -Wno-static-in-inline -fPIC)
  163. add_definitions(-D_WRS_KERNEL)
  164. endif()
  165. # Linker
  166. set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "") # cmake sets -rdynamic by default
  167. # Debug
  168. if(CMAKE_BUILD_TYPE STREQUAL "Debug")
  169. # add_definitions(-fsanitize=address)
  170. # list(APPEND open62541_LIBRARIES asan)
  171. # add_definitions(-fsanitize=undefined)
  172. # list(APPEND open62541_LIBRARIES ubsan)
  173. endif()
  174. # Strip release builds
  175. if(CMAKE_BUILD_TYPE STREQUAL "MinSizeRel" OR CMAKE_BUILD_TYPE STREQUAL "Release")
  176. add_definitions(-ffunction-sections -fdata-sections -fno-stack-protector -fno-unwind-tables
  177. -fno-asynchronous-unwind-tables -fno-math-errno -fmerge-all-constants -fno-ident)
  178. set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -s")
  179. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -s")
  180. if(APPLE)
  181. set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,-dead_strip")
  182. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-dead_strip")
  183. else()
  184. set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,--gc-sections")
  185. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--gc-sections")
  186. endif()
  187. if(NOT WIN32 AND NOT CYGWIN AND NOT APPLE)
  188. # these settings reduce the binary size by ~2kb
  189. set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,-z,norelro -Wl,--hash-style=gnu -Wl,--build-id=none")
  190. endif()
  191. endif()
  192. endif()
  193. if(APPLE)
  194. set(CMAKE_MACOSX_RPATH 1)
  195. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_DARWIN_C_SOURCE=1")
  196. endif()
  197. if(MSVC)
  198. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W3 /WX") # Compiler warnings, error on warning
  199. if(NOT BUILD_SHARED_LIB)
  200. set(CompilerFlags CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE CMAKE_C_FLAGS
  201. CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE)
  202. foreach(CompilerFlag ${CompilerFlags})
  203. string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}")
  204. endforeach()
  205. endif()
  206. endif()
  207. #########################
  208. # Generate Main Library #
  209. #########################
  210. file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/src_generated")
  211. configure_file("include/ua_config.h.in" "${PROJECT_BINARY_DIR}/src_generated/ua_config.h")
  212. configure_file("include/open62541.pc.in" "${PROJECT_BINARY_DIR}/src_generated/open62541.pc" @ONLY)
  213. if(UA_ENABLE_DISCOVERY_MULTICAST)
  214. set(MDNSD_LOGLEVEL 300 CACHE STRING "Level at which logs shall be reported" FORCE)
  215. configure_file("deps/mdnsd/libmdnsd/mdnsd_config.h.in" "${PROJECT_BINARY_DIR}/src_generated/mdnsd_config.h")
  216. endif()
  217. include_directories(${PROJECT_SOURCE_DIR}/include
  218. ${PROJECT_SOURCE_DIR}/plugins # TODO: discovery depends on the default config
  219. ${PROJECT_SOURCE_DIR}/deps
  220. ${PROJECT_BINARY_DIR}
  221. ${PROJECT_BINARY_DIR}/src_generated)
  222. set(exported_headers ${PROJECT_BINARY_DIR}/src_generated/ua_config.h
  223. ${PROJECT_SOURCE_DIR}/deps/ms_stdint.h
  224. ${PROJECT_SOURCE_DIR}/include/ua_constants.h
  225. ${PROJECT_SOURCE_DIR}/include/ua_types.h
  226. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.h
  227. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated_handling.h
  228. ${PROJECT_SOURCE_DIR}/include/ua_server.h
  229. ${PROJECT_SOURCE_DIR}/include/ua_plugin_network.h
  230. ${PROJECT_SOURCE_DIR}/include/ua_plugin_log.h
  231. ${PROJECT_SOURCE_DIR}/include/ua_plugin_access_control.h
  232. ${PROJECT_SOURCE_DIR}/include/ua_plugin_securitypolicy.h
  233. ${PROJECT_SOURCE_DIR}/include/ua_plugin_nodestore.h
  234. ${PROJECT_SOURCE_DIR}/include/ua_server_config.h
  235. ${PROJECT_SOURCE_DIR}/include/ua_client.h
  236. ${PROJECT_SOURCE_DIR}/include/ua_client_highlevel.h
  237. )
  238. set(internal_headers ${PROJECT_SOURCE_DIR}/deps/queue.h
  239. ${PROJECT_SOURCE_DIR}/deps/pcg_basic.h
  240. ${PROJECT_SOURCE_DIR}/deps/libc_time.h
  241. ${PROJECT_SOURCE_DIR}/src/ua_util.h
  242. ${PROJECT_SOURCE_DIR}/src/ua_types_encoding_binary.h
  243. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated_encoding_binary.h
  244. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.h
  245. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated_handling.h
  246. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated_encoding_binary.h
  247. ${PROJECT_SOURCE_DIR}/src/ua_connection_internal.h
  248. ${PROJECT_SOURCE_DIR}/src/ua_securechannel.h
  249. ${PROJECT_SOURCE_DIR}/src/ua_session.h
  250. ${PROJECT_SOURCE_DIR}/src/ua_timer.h
  251. ${PROJECT_SOURCE_DIR}/src/server/ua_subscription.h
  252. ${PROJECT_SOURCE_DIR}/src/server/ua_session_manager.h
  253. ${PROJECT_SOURCE_DIR}/src/server/ua_securechannel_manager.h
  254. ${PROJECT_SOURCE_DIR}/src/server/ua_server_internal.h
  255. ${PROJECT_SOURCE_DIR}/src/server/ua_services.h
  256. ${PROJECT_BINARY_DIR}/src_generated/ua_namespace0.h
  257. ${PROJECT_SOURCE_DIR}/src/client/ua_client_internal.h)
  258. # TODO: make client optional
  259. set(lib_sources ${PROJECT_SOURCE_DIR}/src/ua_types.c
  260. ${PROJECT_SOURCE_DIR}/src/ua_types_encoding_binary.c
  261. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.c
  262. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.c
  263. ${PROJECT_BINARY_DIR}/src_generated/ua_statuscode_descriptions.c
  264. ${PROJECT_SOURCE_DIR}/src/ua_util.c
  265. ${PROJECT_SOURCE_DIR}/src/ua_timer.c
  266. ${PROJECT_SOURCE_DIR}/src/ua_session.c
  267. ${PROJECT_SOURCE_DIR}/src/ua_connection.c
  268. ${PROJECT_SOURCE_DIR}/src/ua_securechannel.c
  269. ${PROJECT_SOURCE_DIR}/src/server/ua_nodes.c
  270. ${PROJECT_SOURCE_DIR}/src/server/ua_server.c
  271. ${PROJECT_SOURCE_DIR}/src/server/ua_server_ns0.c
  272. ${PROJECT_BINARY_DIR}/src_generated/ua_namespace0.c
  273. ${PROJECT_SOURCE_DIR}/src/server/ua_server_binary.c
  274. ${PROJECT_SOURCE_DIR}/src/server/ua_server_utils.c
  275. ${PROJECT_SOURCE_DIR}/src/server/ua_server_worker.c
  276. ${PROJECT_SOURCE_DIR}/src/server/ua_server_discovery.c
  277. ${PROJECT_SOURCE_DIR}/src/server/ua_securechannel_manager.c
  278. ${PROJECT_SOURCE_DIR}/src/server/ua_session_manager.c
  279. ${PROJECT_SOURCE_DIR}/src/server/ua_subscription.c
  280. ${PROJECT_SOURCE_DIR}/src/server/ua_subscription_datachange.c
  281. # services
  282. ${PROJECT_SOURCE_DIR}/src/server/ua_services_view.c
  283. ${PROJECT_SOURCE_DIR}/src/server/ua_services_call.c
  284. ${PROJECT_SOURCE_DIR}/src/server/ua_services_session.c
  285. ${PROJECT_SOURCE_DIR}/src/server/ua_services_attribute.c
  286. ${PROJECT_SOURCE_DIR}/src/server/ua_services_discovery.c
  287. ${PROJECT_SOURCE_DIR}/src/server/ua_services_subscription.c
  288. ${PROJECT_SOURCE_DIR}/src/server/ua_services_securechannel.c
  289. ${PROJECT_SOURCE_DIR}/src/server/ua_services_nodemanagement.c
  290. ${PROJECT_SOURCE_DIR}/src/server/ua_services_discovery_multicast.c
  291. # client
  292. ${PROJECT_SOURCE_DIR}/src/client/ua_client.c
  293. ${PROJECT_SOURCE_DIR}/src/client/ua_client_connect.c
  294. ${PROJECT_SOURCE_DIR}/src/client/ua_client_discovery.c
  295. ${PROJECT_SOURCE_DIR}/src/client/ua_client_highlevel.c
  296. ${PROJECT_SOURCE_DIR}/src/client/ua_client_highlevel_subscriptions.c
  297. # dependencies
  298. ${PROJECT_SOURCE_DIR}/deps/libc_time.c
  299. ${PROJECT_SOURCE_DIR}/deps/pcg_basic.c)
  300. set(default_plugin_headers ${PROJECT_SOURCE_DIR}/plugins/ua_network_tcp.h
  301. ${PROJECT_SOURCE_DIR}/plugins/ua_accesscontrol_default.h
  302. ${PROJECT_SOURCE_DIR}/plugins/ua_log_stdout.h
  303. ${PROJECT_SOURCE_DIR}/plugins/ua_nodestore_default.h
  304. ${PROJECT_SOURCE_DIR}/plugins/ua_config_default.h
  305. ${PROJECT_SOURCE_DIR}/plugins/ua_securitypolicy_none.h)
  306. set(default_plugin_sources ${PROJECT_SOURCE_DIR}/plugins/ua_network_tcp.c
  307. ${PROJECT_SOURCE_DIR}/plugins/ua_clock.c
  308. ${PROJECT_SOURCE_DIR}/plugins/ua_log_stdout.c
  309. ${PROJECT_SOURCE_DIR}/plugins/ua_accesscontrol_default.c
  310. ${PROJECT_SOURCE_DIR}/plugins/ua_nodestore_default.c
  311. ${PROJECT_SOURCE_DIR}/plugins/ua_config_default.c
  312. ${PROJECT_SOURCE_DIR}/plugins/ua_securitypolicy_none.c)
  313. if(UA_DEBUG_DUMP_PKGS)
  314. list(APPEND lib_sources ${PROJECT_SOURCE_DIR}/plugins/ua_debug_dump_pkgs.c)
  315. endif()
  316. if(UA_ENABLE_EMBEDDED_LIBC)
  317. list(APPEND lib_sources ${PROJECT_SOURCE_DIR}/deps/libc_string.c)
  318. endif()
  319. if(UA_ENABLE_NONSTANDARD_UDP)
  320. list(APPEND exported_headers ${PROJECT_SOURCE_DIR}/plugins/ua_network_udp.h)
  321. endif()
  322. if(UA_ENABLE_DISCOVERY_MULTICAST)
  323. # prepend in list, otherwise it complains that winsock2.h has to be included before windows.h
  324. set(internal_headers ${PROJECT_BINARY_DIR}/src_generated/mdnsd_config.h
  325. ${PROJECT_SOURCE_DIR}/deps/mdnsd/libmdnsd/1035.h
  326. ${PROJECT_SOURCE_DIR}/deps/mdnsd/libmdnsd/xht.h
  327. ${PROJECT_SOURCE_DIR}/deps/mdnsd/libmdnsd/sdtxt.h
  328. ${PROJECT_SOURCE_DIR}/deps/mdnsd/libmdnsd/mdnsd.h
  329. ${internal_headers} )
  330. list(APPEND internal_headers ${PROJECT_SOURCE_DIR}/src/server/ua_mdns_internal.h)
  331. set(lib_sources ${PROJECT_SOURCE_DIR}/src/server/ua_mdns.c
  332. ${PROJECT_SOURCE_DIR}/deps/mdnsd/libmdnsd/1035.c
  333. ${PROJECT_SOURCE_DIR}/deps/mdnsd/libmdnsd/xht.c
  334. ${PROJECT_SOURCE_DIR}/deps/mdnsd/libmdnsd/sdtxt.c
  335. ${PROJECT_SOURCE_DIR}/deps/mdnsd/libmdnsd/mdnsd.c
  336. ${lib_sources})
  337. endif()
  338. #########################
  339. # Generate source files #
  340. #########################
  341. if (UA_ENABLE_FULL_NS0)
  342. set(UA_FILE_NS0 ${PROJECT_SOURCE_DIR}/deps/ua-nodeset/Schema/Opc.Ua.NodeSet2.xml)
  343. set(UA_FILE_DATATYPES "")
  344. set(UA_FILE_NODEIDS ${PROJECT_SOURCE_DIR}/deps/ua-nodeset/Schema/NodeIds.csv)
  345. set(UA_FILE_TYPES_BSD ${PROJECT_SOURCE_DIR}/deps/ua-nodeset/Schema/Opc.Ua.Types.bsd)
  346. if(NOT EXISTS "${UA_FILE_NS0}")
  347. MESSAGE(FATAL_ERROR "File ${UA_FILE_NS0} not found. You probably need to initialize the git submodule for deps/ua-nodeset.")
  348. endif()
  349. else()
  350. set(UA_FILE_NS0 ${PROJECT_SOURCE_DIR}/tools/schema/Opc.Ua.NodeSet2.Minimal.xml)
  351. set(UA_FILE_DATATYPES "${PROJECT_SOURCE_DIR}/tools/schema/datatypes_minimal.txt")
  352. set(UA_FILE_NODEIDS ${PROJECT_SOURCE_DIR}/tools/schema/NodeIds.csv)
  353. set(UA_FILE_TYPES_BSD ${PROJECT_SOURCE_DIR}/tools/schema/Opc.Ua.Types.bsd)
  354. endif()
  355. if (UA_FILE_DATATYPES STREQUAL "")
  356. set(SELECTED_TYPES_TMP "")
  357. else()
  358. set(SELECTED_TYPES_TMP "--selected-types=${UA_FILE_DATATYPES}")
  359. endif()
  360. # standard-defined data types
  361. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.c
  362. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.h
  363. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated_handling.h
  364. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated_encoding_binary.h
  365. PRE_BUILD
  366. COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/tools/generate_datatypes.py
  367. --type-csv=${UA_FILE_NODEIDS}
  368. ${SELECTED_TYPES_TMP}
  369. --type-bsd=${UA_FILE_TYPES_BSD}
  370. ${PROJECT_BINARY_DIR}/src_generated/ua_types
  371. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_datatypes.py
  372. ${UA_FILE_NODEIDS}
  373. ${UA_FILE_TYPES_BSD}
  374. ${UA_FILE_DATATYPES})
  375. # we need a custom target to avoid that the generator is called concurrently and thus overwriting files while the other thread is compiling
  376. add_custom_target(open62541-generator-types DEPENDS
  377. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.c
  378. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.h
  379. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated_handling.h
  380. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated_encoding_binary.h)
  381. # transport data types
  382. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.c
  383. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.h
  384. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated_handling.h
  385. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated_encoding_binary.h
  386. PRE_BUILD
  387. COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/tools/generate_datatypes.py
  388. --namespace=1
  389. --selected-types=${PROJECT_SOURCE_DIR}/tools/schema/datatypes_transport.txt
  390. --type-bsd=${UA_FILE_TYPES_BSD}
  391. --type-bsd=${PROJECT_SOURCE_DIR}/tools/schema/Custom.Opc.Ua.Transport.bsd
  392. --no-builtin
  393. ${PROJECT_BINARY_DIR}/src_generated/ua_transport
  394. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_datatypes.py
  395. ${PROJECT_SOURCE_DIR}/tools/schema/datatypes_transport.txt
  396. ${UA_FILE_TYPES_BSD}
  397. ${PROJECT_SOURCE_DIR}/tools/schema/Custom.Opc.Ua.Transport.bsd)
  398. # we need a custom target to avoid that the generator is called concurrently and thus overwriting files while the other thread is compiling
  399. add_custom_target(open62541-generator-transport DEPENDS
  400. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.c
  401. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.h
  402. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated_handling.h
  403. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated_encoding_binary.h)
  404. # statuscode explanation
  405. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_statuscode_descriptions.c
  406. PRE_BUILD
  407. COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/tools/generate_statuscode_descriptions.py
  408. ${PROJECT_SOURCE_DIR}/tools/schema/Opc.Ua.StatusCodes.csv ${PROJECT_BINARY_DIR}/src_generated/ua_statuscode_descriptions
  409. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_statuscode_descriptions.py
  410. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/Opc.Ua.StatusCodes.csv)
  411. # we need a custom target to avoid that the generator is called concurrently and thus overwriting files while the other thread is compiling
  412. add_custom_target(open62541-generator-statuscode DEPENDS
  413. ${PROJECT_BINARY_DIR}/src_generated/ua_statuscode_descriptions.c)
  414. # single-file release
  415. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/open62541.h
  416. PRE_BUILD
  417. COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tools/amalgamate.py
  418. ${OPEN62541_VER_COMMIT} ${CMAKE_CURRENT_BINARY_DIR}/open62541.h
  419. ${exported_headers} ${default_plugin_headers}
  420. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/amalgamate.py
  421. ${exported_headers} ${default_plugin_headers})
  422. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/open62541.c
  423. PRE_BUILD
  424. COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tools/amalgamate.py
  425. ${OPEN62541_VER_COMMIT} ${CMAKE_CURRENT_BINARY_DIR}/open62541.c
  426. ${internal_headers} ${lib_sources} ${default_plugin_sources}
  427. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/amalgamate.py ${internal_headers}
  428. ${lib_sources})
  429. add_custom_target(open62541-amalgamation-source DEPENDS ${PROJECT_BINARY_DIR}/open62541.c)
  430. add_custom_target(open62541-amalgamation-header DEPENDS ${PROJECT_BINARY_DIR}/open62541.h)
  431. add_dependencies(open62541-amalgamation-header open62541-generator-types)
  432. add_dependencies(open62541-amalgamation-source open62541-generator-types
  433. open62541-generator-transport open62541-generator-statuscode)
  434. set(NODESET_MAX_STR_LEN 0)
  435. if(MSVC)
  436. # Visual Studio has a maximum string length of 65535 bytes
  437. set(NODESET_MAX_STR_LEN 65535)
  438. endif()
  439. # generated namespace 0
  440. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_namespace0.c
  441. ${PROJECT_BINARY_DIR}/src_generated/ua_namespace0.h
  442. PRE_BUILD
  443. COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/tools/nodeset_compiler/nodeset_compiler.py
  444. --generate-ns0
  445. --internal-headers
  446. --max-string-length=${NODESET_MAX_STR_LEN}
  447. --ignore ${PROJECT_SOURCE_DIR}/tools/nodeset_compiler/NodeID_NS0_Base.txt
  448. --xml ${UA_FILE_NS0}
  449. ${PROJECT_BINARY_DIR}/src_generated/ua_namespace0
  450. DEPENDS ${UA_FILE_NS0}
  451. ${PROJECT_SOURCE_DIR}/tools/nodeset_compiler/nodeset_compiler.py
  452. ${PROJECT_SOURCE_DIR}/tools/nodeset_compiler/nodes.py
  453. ${PROJECT_SOURCE_DIR}/tools/nodeset_compiler/nodeset.py
  454. ${PROJECT_SOURCE_DIR}/tools/nodeset_compiler/datatypes.py
  455. ${PROJECT_SOURCE_DIR}/tools/nodeset_compiler/backend_open62541.py
  456. ${PROJECT_SOURCE_DIR}/tools/nodeset_compiler/backend_open62541_nodes.py
  457. ${PROJECT_SOURCE_DIR}/tools/nodeset_compiler/backend_open62541_datatypes.py)
  458. # stack protector needs to be disabled for the huge ns0 file, otherwise it may take many minutes to compile the file.
  459. set_source_files_properties(${PROJECT_BINARY_DIR}/src_generated/ua_namespace0.c PROPERTIES COMPILE_FLAGS -fno-stack-protector)
  460. # we need a custom target to avoid that the generator is called concurrently and thus overwriting files while the other thread is compiling
  461. add_custom_target(open62541-generator-namespace DEPENDS
  462. ${PROJECT_BINARY_DIR}/src_generated/ua_namespace0.c
  463. ${PROJECT_BINARY_DIR}/src_generated/ua_namespace0.h)
  464. #####################
  465. # Build the Library #
  466. #####################
  467. assign_source_group(${lib_sources})
  468. assign_source_group(${internal_headers})
  469. assign_source_group(${exported_headers})
  470. assign_source_group(${default_plugin_sources})
  471. if(UA_ENABLE_AMALGAMATION)
  472. add_library(open62541-object OBJECT ${PROJECT_BINARY_DIR}/open62541.c ${PROJECT_BINARY_DIR}/open62541.h)
  473. target_include_directories(open62541-object PRIVATE ${PROJECT_BINARY_DIR})
  474. # make sure the open62541_amalgamation target builds before so that amalgamation is finished and it is not executed again for open62541-object
  475. # and thus may overwrite the amalgamation result during multiprocessor compilation
  476. # the header is already a dependency of open62541 target itself
  477. add_dependencies(open62541-object
  478. open62541-amalgamation-header
  479. open62541-generator-types
  480. open62541-generator-transport
  481. open62541-generator-statuscode
  482. open62541-amalgamation-source)
  483. add_library(open62541 $<TARGET_OBJECTS:open62541-object>)
  484. if(UA_COMPILE_AS_CXX)
  485. set_source_files_properties(${PROJECT_BINARY_DIR}/open62541.c PROPERTIES LANGUAGE CXX)
  486. endif()
  487. else()
  488. add_definitions(-DUA_NO_AMALGAMATION)
  489. add_library(open62541-object OBJECT ${lib_sources} ${internal_headers} ${exported_headers})
  490. add_dependencies(open62541-object
  491. open62541-amalgamation-header
  492. open62541-generator-types
  493. open62541-generator-transport
  494. open62541-generator-statuscode)
  495. target_include_directories(open62541-object PRIVATE ${PROJECT_SOURCE_DIR}/src)
  496. add_library(open62541-plugins OBJECT ${default_plugin_sources} ${exported_headers})
  497. add_dependencies(open62541-plugins open62541-generator-types open62541-generator-transport)
  498. target_include_directories(open62541-plugins PRIVATE ${PROJECT_SOURCE_DIR}/plugins)
  499. target_include_directories(open62541-plugins PRIVATE ${PROJECT_BINARY_DIR}/src_generated)
  500. target_compile_definitions(open62541-plugins PRIVATE -DUA_DYNAMIC_LINKING_EXPORT)
  501. set_target_properties(open62541-plugins PROPERTIES FOLDER "open62541/lib")
  502. add_library(open62541 $<TARGET_OBJECTS:open62541-object> $<TARGET_OBJECTS:open62541-plugins>)
  503. if(UA_COMPILE_AS_CXX)
  504. set_source_files_properties(${lib_sources} PROPERTIES LANGUAGE CXX)
  505. set_source_files_properties(${default_plugin_sources} PROPERTIES LANGUAGE CXX)
  506. endif()
  507. endif()
  508. add_dependencies(open62541-amalgamation-source open62541-generator-namespace)
  509. add_dependencies(open62541-amalgamation-header open62541-generator-namespace)
  510. if(NOT UA_ENABLE_AMALGAMATION)
  511. add_dependencies(open62541-object open62541-generator-namespace)
  512. endif()
  513. # Export Symbols
  514. target_compile_definitions(open62541-object PRIVATE -DUA_DYNAMIC_LINKING_EXPORT)
  515. target_compile_definitions(open62541 PRIVATE -DUA_DYNAMIC_LINKING_EXPORT)
  516. if (UA_ENABLE_DISCOVERY_MULTICAST)
  517. target_compile_definitions(open62541-object PRIVATE -DMDNSD_DYNAMIC_LINKING_EXPORT)
  518. target_compile_definitions(open62541 PRIVATE -DMDNSD_DYNAMIC_LINKING_EXPORT)
  519. endif()
  520. # DLL requires linking to dependencies
  521. target_link_libraries(open62541 ${open62541_LIBRARIES})
  522. # Generate properly versioned shared library links on Linux
  523. SET_TARGET_PROPERTIES(open62541 PROPERTIES SOVERSION 0 VERSION "${OPEN62541_VER_MAJOR}.${OPEN62541_VER_MINOR}.${OPEN62541_VER_PATCH}")
  524. ##########################
  525. # Build Selected Targets #
  526. ##########################
  527. # always include, builds with make doc
  528. add_subdirectory(doc)
  529. if(UA_BUILD_EXAMPLES)
  530. add_subdirectory(examples)
  531. endif()
  532. if(UA_BUILD_UNIT_TESTS)
  533. if(UA_ENABLE_AMALGAMATION)
  534. # Cannot compile tests with amalgamation. Amalgamation uses the default plugins, not the testing plugins
  535. message(FATAL_ERROR "Unit tests cannot be generated with source amalgamation enabled")
  536. endif()
  537. enable_testing()
  538. add_subdirectory(tests)
  539. endif()
  540. if(UA_BUILD_FUZZING OR UA_BUILD_OSS_FUZZ)
  541. # Force enable discovery, to also fuzzy-test this code
  542. set(UA_ENABLE_DISCOVERY ON CACHE STRING "" FORCE)
  543. set(UA_ENABLE_DISCOVERY_MULTICAST ON CACHE STRING "" FORCE)
  544. add_subdirectory(tests/fuzz)
  545. endif()
  546. ############################
  547. # Linting run (clang-tidy) #
  548. ############################
  549. find_package(ClangTools)
  550. add_custom_target(lint ${CLANG_TIDY_PROGRAM}
  551. ${lib_sources}
  552. -checks=cert-*,performance-*,readability-*,-readability-braces-around-statements
  553. -warnings-as-errors=cert-*,performance-*,readability-*,-readability-braces-around-statements
  554. --
  555. -std=c99
  556. -I${PROJECT_SOURCE_DIR}/include
  557. -I${PROJECT_SOURCE_DIR}/plugins
  558. -I${PROJECT_SOURCE_DIR}/deps
  559. -I${PROJECT_SOURCE_DIR}/src
  560. -I${PROJECT_SOURCE_DIR}/src/server
  561. -I${PROJECT_SOURCE_DIR}/src/client
  562. -I${PROJECT_BINARY_DIR}/src_generated
  563. -DUA_NO_AMALGAMATION
  564. DEPENDS ${lib_sources}
  565. COMMENT "Run clang-tidy on the library")
  566. add_dependencies(lint open62541)
  567. ##########################
  568. # Installation #
  569. ##########################
  570. # invoke via `make install`
  571. # specify install location with `-DCMAKE_INSTALL_PREFIX=xyz`
  572. # Enable shared library with `-DBUILD_SHARED_LIBS=ON`
  573. set(cmake_configfile_install ${LIB_INSTALL_DIR}/cmake3)
  574. set(target_install_dest_name "${cmake_configfile_install}/open62541Targets.cmake")
  575. set(open62541_tools_dir share/open62541/tools)
  576. set(open62541_deps_dir include/open62541/deps)
  577. # export library (either static or shared depending on BUILD_SHARED_LIBS)
  578. install(TARGETS open62541
  579. EXPORT open62541Targets
  580. LIBRARY DESTINATION ${LIB_INSTALL_DIR}
  581. ARCHIVE DESTINATION ${LIB_INSTALL_DIR}
  582. RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}
  583. INCLUDES DESTINATION ${INCLUDE_INSTALL_DIR}/open62541 ${open62541_deps_dir})
  584. include(CMakePackageConfigHelpers)
  585. configure_package_config_file("${CMAKE_CURRENT_SOURCE_DIR}/tools/cmake/open62541-config.cmake.in"
  586. "${CMAKE_CURRENT_BINARY_DIR}/cmake/open62541-config.cmake"
  587. INSTALL_DESTINATION "${cmake_configfile_install}"
  588. PATH_VARS target_install_dest_name open62541_tools_dir)
  589. set(open62541_VERSION)
  590. get_target_property(open62541_VERSION open62541 VERSION)
  591. write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/open62541ConfigVersion.cmake"
  592. VERSION ${open62541_VERSION}
  593. COMPATIBILITY AnyNewerVersion)
  594. install(EXPORT open62541Targets
  595. FILE open62541Targets.cmake
  596. DESTINATION "${cmake_configfile_install}")
  597. install(FILES "${CMAKE_CURRENT_BINARY_DIR}/cmake/open62541-config.cmake"
  598. "${CMAKE_CURRENT_BINARY_DIR}/open62541ConfigVersion.cmake"
  599. DESTINATION "${cmake_configfile_install}")
  600. if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
  601. install(FILES "${PROJECT_BINARY_DIR}/src_generated/open62541.pc"
  602. DESTINATION ${LIB_INSTALL_DIR}/pkgconfig)
  603. endif()
  604. # export amalgamated header open62541.h which is generated due to build of open62541-object
  605. if(UA_ENABLE_AMALGAMATION)
  606. install(FILES ${PROJECT_BINARY_DIR}/open62541.h DESTINATION include/open62541)
  607. endif()
  608. install(DIRECTORY deps/ DESTINATION ${open62541_deps_dir})
  609. install(DIRECTORY tools/ DESTINATION ${open62541_tools_dir} USE_SOURCE_PERMISSIONS)
  610. ##########################
  611. # Packaging (DEB/RPM) #
  612. ##########################
  613. # invoke via `make package`
  614. set(CPACK_GENERATOR "TGZ;DEB;RPM")
  615. set(CPACK_PACKAGE_VENDOR "open62541 team")
  616. set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "OPC UA implementation")
  617. set(CPACK_PACKAGE_DESCRIPTION "open62541 is a C-based library (linking with C++ projects is possible) with all necessary tools to implement dedicated OPC UA clients and servers, or to integrate OPC UA-based communication into existing applications.")
  618. set(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/README.md")
  619. set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")
  620. set(CPACK_PACKAGE_VERSION_MAJOR "0")
  621. set(CPACK_PACKAGE_VERSION_MINOR "2")
  622. set(CPACK_PACKAGE_VERSION_PATCH "0")
  623. set(CPACK_DEBIAN_PACKAGE_MAINTAINER "open62541 team") #required
  624. include(CPack)
  625. ##################################
  626. # Visual studio solution folders #
  627. ##################################
  628. set_property(GLOBAL PROPERTY USE_FOLDERS ON)
  629. set_property(GLOBAL PROPERTY PREDEFINED_TARGETS_FOLDER "_CmakePredifinedTargets")
  630. set_target_properties(open62541 PROPERTIES FOLDER "open62541/lib")
  631. set_target_properties(open62541-object PROPERTIES FOLDER "open62541/lib")
  632. set_target_properties(lint PROPERTIES FOLDER "CodeAnalysis")
  633. set_target_properties(open62541-amalgamation-header PROPERTIES FOLDER "open62541/lib")
  634. set_target_properties(open62541-amalgamation-source PROPERTIES FOLDER "open62541/lib")