CMakeLists.txt 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  1. cmake_minimum_required(VERSION 3.0...3.12)
  2. project(open62541)
  3. # set(CMAKE_VERBOSE_MAKEFILE ON)
  4. if(${CMAKE_VERSION} VERSION_LESS 3.12)
  5. cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
  6. endif()
  7. string(TOLOWER "${CMAKE_BUILD_TYPE}" BUILD_TYPE_LOWER_CASE)
  8. set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/tools/cmake")
  9. find_package(PythonInterp REQUIRED)
  10. find_package(Git)
  11. include(AssignSourceGroup)
  12. include(GNUInstallDirs)
  13. # Set when installed via make install
  14. set(open62541_TOOLS_DIR ${PROJECT_SOURCE_DIR}/tools)
  15. set(open62541_NODESET_DIR ${PROJECT_SOURCE_DIR}/deps/ua-nodeset)
  16. include(macros_internal)
  17. include(macros_public)
  18. #############################
  19. # Compiled binaries folders #
  20. #############################
  21. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  22. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  23. set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  24. ###########
  25. # Version #
  26. ###########
  27. include(SetGitBasedVersion)
  28. # The version string will be automatically set based on the git describe output.
  29. # This will automate the version strings. Just create a new tag, and the version will be set correctly.
  30. set_open62541_version()
  31. MESSAGE(STATUS "open62541 Version: ${OPEN62541_VER_MAJOR}.${OPEN62541_VER_MINOR}.${OPEN62541_VER_PATCH}${OPEN62541_VER_LABEL}")
  32. #################
  33. # Build Options #
  34. #################
  35. # Set default build type.
  36. if(NOT CMAKE_BUILD_TYPE)
  37. message(STATUS "CMAKE_BUILD_TYPE not given; setting to 'Debug'")
  38. set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build" FORCE)
  39. endif()
  40. option(UA_ENABLE_AMALGAMATION "Concatenate the library to a single file open62541.h/.c" OFF)
  41. set(UA_AMALGAMATION_ARCHITECTURES "" CACHE STRING "List of architectures to include in amalgamation")
  42. mark_as_advanced(UA_AMALGAMATION_ARCHITECTURES)
  43. # Platform. This is at the beginning in case the architecture changes some UA options
  44. set(UA_ARCHITECTURE "None" CACHE STRING "Architecture to build open62541 on")
  45. if(UA_ENABLE_AMALGAMATION)
  46. if("${UA_AMALGAMATION_ARCHITECTURES}" STREQUAL "")
  47. if(NOT "${UA_ARCHITECTURE}" STREQUAL "None")
  48. set(UA_AMALGAMATION_ARCHITECTURES "${UA_ARCHITECTURE}")
  49. else()
  50. # select some default architectures which should be included
  51. set(UA_AMALGAMATION_ARCHITECTURES "win32;posix")
  52. endif()
  53. endif()
  54. message(STATUS "Architectures included in amalgamation: ${UA_AMALGAMATION_ARCHITECTURES}")
  55. endif()
  56. if("${UA_ARCHITECTURE}" STREQUAL "None")
  57. if(UNIX)
  58. set(UA_ARCHITECTURE "posix" CACHE STRING "" FORCE)
  59. elseif(WIN32)
  60. set(UA_ARCHITECTURE "win32" CACHE STRING "" FORCE)
  61. endif(UNIX)
  62. endif()
  63. message(STATUS "The selected architecture is: ${UA_ARCHITECTURE}")
  64. string(TOUPPER ${UA_ARCHITECTURE} UA_ARCHITECTURE_UPPER)
  65. add_definitions(-DUA_ARCHITECTURE_${UA_ARCHITECTURE_UPPER})
  66. add_subdirectory(arch)
  67. GET_PROPERTY(architectures GLOBAL PROPERTY UA_ARCHITECTURES)
  68. list(SORT architectures)
  69. set_property(CACHE UA_ARCHITECTURE PROPERTY STRINGS None ${architectures})
  70. GET_PROPERTY(ua_architecture_directories_to_include GLOBAL PROPERTY UA_INCLUDE_DIRECTORIES)
  71. GET_PROPERTY(ua_architecture_headers GLOBAL PROPERTY UA_ARCHITECTURE_HEADERS)
  72. GET_PROPERTY(ua_architecture_headers_beginning GLOBAL PROPERTY UA_ARCHITECTURE_HEADERS_BEGINNING)
  73. GET_PROPERTY(ua_architecture_sources GLOBAL PROPERTY UA_ARCHITECTURE_SOURCES)
  74. set(ua_architecture_sources ${ua_architecture_sources}
  75. ${PROJECT_SOURCE_DIR}/arch/network_tcp.c
  76. )
  77. set(ua_architecture_headers ${ua_architecture_headers}
  78. ${PROJECT_SOURCE_DIR}/include/open62541/network_tcp.h
  79. ${PROJECT_SOURCE_DIR}/include/open62541/architecture_functions.h
  80. )
  81. if(UA_ENABLE_WEBSOCKET_SERVER)
  82. set(ua_architecture_sources ${ua_architecture_sources}
  83. ${PROJECT_SOURCE_DIR}/arch/network_ws.c)
  84. set(ua_architecture_headers ${ua_architecture_headers}
  85. ${PROJECT_SOURCE_DIR}/include/open62541/network_ws.h)
  86. endif()
  87. if(${UA_ARCHITECTURE} STREQUAL "None")
  88. message(FATAL_ERROR "No architecture was selected. Please select the architecture of your target platform")
  89. endif(${UA_ARCHITECTURE} STREQUAL "None")
  90. # Create a list of ifdefs for all the architectures.
  91. # This is needed to enable a default architecture if none is selected through gcc compiler def.
  92. # Especially if someone is using the amalgamated file on Linux/Windows he should not need to define an architecture.
  93. set(UA_ARCHITECTURES_NODEF "1 ") #to make it easier to append later the && symbol
  94. foreach(arch_ ${architectures})
  95. string(TOUPPER ${arch_} UA_ARCHITECTURE_UPPER_)
  96. set(UA_ARCHITECTURES_NODEF "${UA_ARCHITECTURES_NODEF} && !defined(UA_ARCHITECTURE_${UA_ARCHITECTURE_UPPER_})")
  97. endforeach(arch_ ${architectures})
  98. # Options
  99. option(BUILD_SHARED_LIBS "Enable building of shared libraries (dll/so)" OFF)
  100. set(UA_LOGLEVEL 300 CACHE STRING "Level at which logs shall be reported")
  101. set(UA_MULTITHREADING 0 CACHE STRING "Level of multithreading (0-99: No Multithreading, 100-199: Thread-Safe API, >=200: Internal Threads")
  102. option(UA_ENABLE_HISTORIZING "Enable server and client to provide historical access." OFF)
  103. option(UA_ENABLE_METHODCALLS "Enable the Method service set" ON)
  104. option(UA_ENABLE_NODEMANAGEMENT "Enable dynamic addition and removal of nodes at runtime" ON)
  105. option(UA_ENABLE_SUBSCRIPTIONS "Enable subscriptions support." ON)
  106. option(UA_ENABLE_SUBSCRIPTIONS_EVENTS "Enable the use of event subscriptions" OFF)
  107. option(UA_ENABLE_DISCOVERY "Enable Discovery Service (LDS)" ON)
  108. option(UA_ENABLE_DISCOVERY_MULTICAST "Enable Discovery Service with multicast support (LDS-ME)" OFF)
  109. option(UA_ENABLE_ENCRYPTION "Enable encryption support (uses mbedTLS)" OFF)
  110. option(UA_ENABLE_DA "Enable OPC UA DataAccess (Part 8) definitions" ON)
  111. option(UA_ENABLE_MICRO_EMB_DEV_PROFILE "Builds CTT Compliant Micro Embedded Device Server Profile" OFF)
  112. option(UA_ENABLE_WEBSOCKET_SERVER "Enable websocket support (uses libwebsockets)" OFF)
  113. # Namespace Zero
  114. set(UA_NAMESPACE_ZERO "REDUCED" CACHE STRING "Completeness of the generated namespace zero (minimal/reduced/full)")
  115. SET_PROPERTY(CACHE UA_NAMESPACE_ZERO PROPERTY STRINGS "MINIMAL" "REDUCED" "FULL")
  116. if(UA_NAMESPACE_ZERO STREQUAL "MINIMAL")
  117. set(UA_GENERATED_NAMESPACE_ZERO OFF)
  118. else()
  119. set(UA_GENERATED_NAMESPACE_ZERO ON)
  120. endif()
  121. if(MSVC AND UA_NAMESPACE_ZERO STREQUAL "FULL")
  122. # For the full NS0 we need a stack size of 8MB (as it is default on linux)
  123. # See https://github.com/open62541/open62541/issues/1326
  124. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:8000000")
  125. endif()
  126. if(UA_ENABLE_MICRO_EMB_DEV_PROFILE)
  127. if(NOT (UA_NAMESPACE_ZERO STREQUAL "FULL"))
  128. message(FATAL_ERROR "CTT Compliant Micro Embedded Device Server Profile needs full namespace zero")
  129. endif()
  130. endif()
  131. if(UA_BUILD_FUZZING OR UA_BUILD_OSS_FUZZ OR UA_BUILD_FUZZING_CORPUS)
  132. # Force enable options not passed in the build script, to also fuzzy-test this code
  133. set(UA_ENABLE_DISCOVERY ON CACHE STRING "" FORCE)
  134. set(UA_ENABLE_DISCOVERY_MULTICAST ON CACHE STRING "" FORCE)
  135. set(UA_ENABLE_ENCRYPTION ON CACHE STRING "" FORCE)
  136. set(UA_ENABLE_HISTORIZING ON CACHE STRING "" FORCE)
  137. set(UA_ENABLE_JSON_ENCODING ON CACHE STRING "" FORCE)
  138. set(UA_ENABLE_SUBSCRIPTIONS ON CACHE STRING "" FORCE)
  139. set(UA_ENABLE_SUBSCRIPTIONS_EVENTS ON CACHE STRING "" FORCE)
  140. endif()
  141. # It should not be possible to enable events without enabling subscriptions and full ns0
  142. #if((UA_ENABLE_SUBSCRIPTIONS_EVENTS) AND (NOT (UA_ENABLE_SUBSCRIPTIONS AND UA_NAMESPACE_ZERO STREQUAL "FULL")))
  143. # message(FATAL_ERROR "Unable to enable events without UA_ENABLE_SUBSCRIPTIONS and full namespace 0")
  144. #endif()
  145. if(UA_ENABLE_COVERAGE)
  146. set(CMAKE_BUILD_TYPE DEBUG)
  147. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -fprofile-arcs -ftest-coverage")
  148. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage -lgcov")
  149. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
  150. endif()
  151. if(UA_ENABLE_DISCOVERY_MULTICAST AND NOT UA_ENABLE_DISCOVERY)
  152. MESSAGE(WARNING "UA_ENABLE_DISCOVERY_MULTICAST is enabled, but not UA_ENABLE_DISCOVERY. UA_ENABLE_DISCOVERY_MULTICAST will be set to OFF")
  153. SET(UA_ENABLE_DISCOVERY_MULTICAST OFF CACHE BOOL "Enable Discovery Service with multicast support (LDS-ME)" FORCE)
  154. endif()
  155. # Advanced options
  156. option(UA_ENABLE_COVERAGE "Enable gcov coverage" OFF)
  157. mark_as_advanced(UA_ENABLE_COVERAGE)
  158. option(UA_ENABLE_QUERY "Enable query support in the client (most servers don't support it)" OFF)
  159. mark_as_advanced(UA_ENABLE_QUERY)
  160. option(UA_ENABLE_IMMUTABLE_NODES "Nodes in the information model are not edited but copied and replaced" OFF)
  161. mark_as_advanced(UA_ENABLE_IMMUTABLE_NODES)
  162. if(UA_MULTITHREADING GREATER 100)
  163. set(UA_ENABLE_IMMUTABLE_NODES ON)
  164. endif()
  165. option(UA_ENABLE_EXPERIMENTAL_HISTORIZING "Enable client experimental historical access features" OFF)
  166. mark_as_advanced(UA_ENABLE_EXPERIMENTAL_HISTORIZING)
  167. option(UA_ENABLE_PUBSUB "Enable publish/subscribe" OFF)
  168. mark_as_advanced(UA_ENABLE_PUBSUB)
  169. option(UA_ENABLE_PUBSUB_ETH_UADP "Enable publish/subscribe UADP over Ethernet" OFF)
  170. mark_as_advanced(UA_ENABLE_PUBSUB_ETH_UADP)
  171. if(UA_ENABLE_PUBSUB_ETH_UADP)
  172. if (NOT CMAKE_SYSTEM MATCHES "Linux")
  173. message(FATAL_ERROR "UADP over Ethernet is only available on Linux.")
  174. endif()
  175. endif()
  176. option(UA_ENABLE_PUBSUB_DELTAFRAMES "Enable sending of delta frames with only the changes" OFF)
  177. mark_as_advanced(UA_ENABLE_PUBSUB_DELTAFRAMES)
  178. option(UA_ENABLE_PUBSUB_INFORMATIONMODEL "Enable PubSub information model twin" OFF)
  179. mark_as_advanced(UA_ENABLE_PUBSUB_INFORMATIONMODEL)
  180. option(UA_ENABLE_PUBSUB_INFORMATIONMODEL_METHODS "Enable PubSub informationmodel methods" OFF)
  181. mark_as_advanced(UA_ENABLE_PUBSUB_INFORMATIONMODEL_METHODS)
  182. if(UA_ENABLE_PUBSUB_INFORMATIONMODEL_METHODS)
  183. if(NOT UA_ENABLE_PUBSUB_INFORMATIONMODEL)
  184. message(FATAL_ERROR "PubSub information model methods cannot be used with disabled PubSub information model.")
  185. endif()
  186. endif()
  187. if(UA_ENABLE_PUBSUB_INFORMATIONMODEL)
  188. if(NOT UA_ENABLE_PUBSUB)
  189. message(FATAL_ERROR "PubSub information model representation cannot be used with disabled PubSub function.")
  190. endif()
  191. if(UA_NAMESPACE_ZERO STREQUAL "MINIMAL")
  192. message(FATAL_ERROR "PubSub information model representation cannot be used with MINIMAL namespace zero.")
  193. endif()
  194. endif()
  195. option(UA_ENABLE_PUBSUB_CUSTOM_PUBLISH_HANDLING
  196. "Use a custom implementation for the publish callback handling" OFF)
  197. mark_as_advanced(UA_ENABLE_PUBSUB_CUSTOM_PUBLISH_HANDLING)
  198. if(UA_ENABLE_PUBSUB_CUSTOM_PUBLISH_INTERRUPT)
  199. if(NOT UA_ENABLE_PUBSUB)
  200. message(FATAL_ERROR "Custom publish callback handling cannot be used with PubSub function disabled")
  201. endif()
  202. endif()
  203. option(UA_ENABLE_JSON_ENCODING "Enable Json encoding (EXPERIMENTAL)" OFF)
  204. mark_as_advanced(UA_ENABLE_JSON_ENCODING)
  205. option(UA_ENABLE_PUBSUB_MQTT "Enable publish/subscribe with mqtt (experimental)" OFF)
  206. mark_as_advanced(UA_ENABLE_PUBSUB_MQTT)
  207. if(UA_ENABLE_PUBSUB_MQTT)
  208. # Warn if run in Windows
  209. if(WIN32)
  210. MESSAGE(WARNING "Multithreading is enabled in MQTT plugin. This feature is under development and marked as EXPERIMENTAL")
  211. endif()
  212. if(NOT UA_ENABLE_PUBSUB)
  213. message(FATAL_ERROR "Mqtt cannot be used with disabled PubSub function.")
  214. endif()
  215. endif()
  216. option(UA_ENABLE_STATUSCODE_DESCRIPTIONS "Enable conversion of StatusCode to human-readable error message" ON)
  217. mark_as_advanced(UA_ENABLE_STATUSCODE_DESCRIPTIONS)
  218. option(UA_ENABLE_TYPEDESCRIPTION "Add the type and member names to the UA_DataType structure" ON)
  219. mark_as_advanced(UA_ENABLE_TYPEDESCRIPTION)
  220. option(UA_ENABLE_NODESET_COMPILER_DESCRIPTIONS "Set node description attribute for nodeset compiler generated nodes" ON)
  221. mark_as_advanced(UA_ENABLE_NODESET_COMPILER_DESCRIPTIONS)
  222. option(UA_ENABLE_DETERMINISTIC_RNG "Do not seed the random number generator (e.g. for unit tests)." OFF)
  223. mark_as_advanced(UA_ENABLE_DETERMINISTIC_RNG)
  224. option(UA_ENABLE_MALLOC_SINGLETON
  225. "Use a global variable pointer for malloc (and free, ...) that can be switched at runtime" OFF)
  226. mark_as_advanced(UA_ENABLE_MALLOC_SINGLETON)
  227. if(UA_ENABLE_MALLOC_SINGLETON)
  228. if(NOT "${UA_ARCHITECTURE}" MATCHES "posix")
  229. message(FATAL_ERROR "The singleton malloc is available only for the posix architecture")
  230. endif()
  231. endif()
  232. option(UA_ENABLE_VALGRIND_INTERACTIVE "Enable dumping valgrind every iteration. CAUTION! SLOWDOWN!" OFF)
  233. mark_as_advanced(UA_ENABLE_VALGRIND_INTERACTIVE)
  234. option(UA_MSVC_FORCE_STATIC_CRT "Force linking with the static C-runtime library when compiling to static library with MSVC" ON)
  235. mark_as_advanced(UA_MSVC_FORCE_STATIC_CRT)
  236. option(UA_FILE_NS0 "Override the NodeSet xml file used to generate namespace zero")
  237. mark_as_advanced(UA_FILE_NS0)
  238. # Semaphores/file system may not be available on embedded devices. It can be
  239. # disabled with the following option
  240. option(UA_ENABLE_DISCOVERY_SEMAPHORE "Enable Discovery Semaphore support" ON)
  241. mark_as_advanced(UA_ENABLE_DISCOVERY_SEMAPHORE)
  242. option(UA_ENABLE_UNIT_TESTS_MEMCHECK "Use Valgrind (Linux) or DrMemory (Windows) to detect memory leaks when running the unit tests" OFF)
  243. mark_as_advanced(UA_ENABLE_UNIT_TESTS_MEMCHECK)
  244. option(UA_ENABLE_UNIT_TEST_FAILURE_HOOKS
  245. "Add hooks to force failure modes for additional unit tests. Not for production use!" OFF)
  246. mark_as_advanced(UA_ENABLE_UNIT_TEST_FAILURE_HOOKS)
  247. set(UA_VALGRIND_INTERACTIVE_INTERVAL 1000 CACHE STRING "The number of iterations to wait before creating the next dump")
  248. mark_as_advanced(UA_VALGRIND_INTERACTIVE_INTERVAL)
  249. # Build options for debugging
  250. option(UA_DEBUG "Enable assertions and additional functionality that should not be included in release builds" OFF)
  251. mark_as_advanced(UA_DEBUG)
  252. if(CMAKE_BUILD_TYPE STREQUAL "Debug")
  253. set(UA_DEBUG ON)
  254. endif()
  255. option(UA_DEBUG_DUMP_PKGS "Dump every package received by the server as hexdump format" OFF)
  256. mark_as_advanced(UA_DEBUG_DUMP_PKGS)
  257. option(UA_ENABLE_HARDENING "Enable Hardening measures (e.g. Stack-Protectors and Fortify)" ON)
  258. mark_as_advanced(UA_ENABLE_HARDENING)
  259. if(CMAKE_VERSION VERSION_GREATER 3.6)
  260. set(UA_ENABLE_STATIC_ANALYZER "OFF" CACHE STRING "Enable installed static analyzer during build process (off/minimal/reduced/full)")
  261. mark_as_advanced(UA_ENABLE_STATIC_ANALYZER)
  262. SET_PROPERTY(CACHE UA_ENABLE_STATIC_ANALYZER PROPERTY STRINGS "OFF" "MINIMAL" "REDUCED" "FULL")
  263. endif()
  264. # Build Targets
  265. option(UA_BUILD_EXAMPLES "Build example servers and clients" OFF)
  266. option(UA_BUILD_TOOLS "Build OPC UA shell tools" OFF)
  267. option(UA_BUILD_UNIT_TESTS "Build the unit tests" OFF)
  268. option(UA_BUILD_FUZZING "Build the fuzzing executables" OFF)
  269. mark_as_advanced(UA_BUILD_FUZZING)
  270. if(UA_BUILD_FUZZING)
  271. # oss-fuzz already defines this by default
  272. add_definitions(-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
  273. endif()
  274. if(UA_ENABLE_EXPERIMENTAL_HISTORIZING)
  275. if(NOT UA_ENABLE_HISTORIZING)
  276. message(FATAL_ERROR "UA_ENABLE_EXPERIMENTAL_HISTORIZING cannot be used with disabled UA_ENABLE_HISTORIZING.")
  277. endif()
  278. endif()
  279. option(UA_BUILD_FUZZING_CORPUS "Build the fuzzing corpus" OFF)
  280. mark_as_advanced(UA_BUILD_FUZZING_CORPUS)
  281. if(UA_BUILD_FUZZING_CORPUS)
  282. add_definitions(-DUA_DEBUG_DUMP_PKGS_FILE)
  283. set(UA_ENABLE_TYPEDESCRIPTION ON CACHE STRING "" FORCE)
  284. #set(UA_DEBUG_DUMP_PKGS ON CACHE STRING "" FORCE)
  285. endif()
  286. option(UA_BUILD_OSS_FUZZ "Special build switch used in oss-fuzz" OFF)
  287. mark_as_advanced(UA_BUILD_OSS_FUZZ)
  288. # Advanced Build Targets
  289. option(UA_PACK_DEBIAN "Special build switch used in .deb packaging" OFF)
  290. mark_as_advanced(UA_PACK_DEBIAN)
  291. # Building shared libs (dll, so). This option is written into ua_config.h.
  292. set(UA_DYNAMIC_LINKING OFF)
  293. if(BUILD_SHARED_LIBS)
  294. set(UA_DYNAMIC_LINKING ON)
  295. if (UA_ENABLE_DISCOVERY_MULTICAST)
  296. set(MDNSD_DYNAMIC_LINKING ON)
  297. endif()
  298. endif()
  299. # Warn if experimental features are enabled
  300. if(UA_MULTITHREADING GREATER 199)
  301. MESSAGE(WARNING "UA_MULTITHREADING >= 200. Internal threads of the server are an EXPERIMENTAL feature. Expect bugs")
  302. endif()
  303. ########################
  304. # Linting during build #
  305. ########################
  306. set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
  307. include(linting_build)
  308. ######################
  309. # External Libraries #
  310. ######################
  311. list(APPEND open62541_LIBRARIES "")
  312. # Force compilation with as C++
  313. option(UA_COMPILE_AS_CXX "Force compilation with a C++ compiler" OFF)
  314. mark_as_advanced(UA_COMPILE_AS_CXX)
  315. if (UA_COMPILE_AS_CXX)
  316. # We need the UINT32_C define
  317. add_definitions(-D__STDC_CONSTANT_MACROS)
  318. endif()
  319. if(UA_ENABLE_ENCRYPTION)
  320. # The recommended way is to install mbedtls via the OS package manager. If
  321. # that is not possible, manually compile mbedTLS and set the cmake variables
  322. # defined in /tools/cmake/FindMbedTLS.cmake.
  323. find_package(MbedTLS REQUIRED)
  324. list(APPEND open62541_LIBRARIES ${MBEDTLS_LIBRARIES})
  325. endif()
  326. if(UA_ENABLE_WEBSOCKET_SERVER)
  327. # The recommended way is to install libwebsockets via the OS package manager. If
  328. # that is not possible, manually compile libwebsockets and set the cmake variables
  329. # defined in /tools/cmake/Findlibwebsockets.cmake
  330. find_package(libwebsockets REQUIRED)
  331. list(APPEND open62541_LIBRARIES ${LIBWEBSOCKETS_LIBRARIES})
  332. endif()
  333. #####################
  334. # Compiler Settings #
  335. #####################
  336. # Check if a C compiler flag is supported and add it (if supported)
  337. # Taken from https://stackoverflow.com/a/33266748
  338. include(CheckCCompilerFlag)
  339. function(check_add_cc_flag CC_FLAG)
  340. string(FIND "${CMAKE_C_FLAGS}" "${CC_FLAG}" flag_already_set)
  341. if(flag_already_set EQUAL -1)
  342. message(STATUS "Test CC flag ${CC_FLAG}")
  343. check_c_compiler_flag("${CC_FLAG}" flag_supported)
  344. if(flag_supported)
  345. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CC_FLAG}" PARENT_SCOPE)
  346. endif()
  347. unset(flag_supported CACHE)
  348. endif()
  349. endfunction()
  350. if(NOT UA_COMPILE_AS_CXX AND (CMAKE_COMPILER_IS_GNUCC OR "x${CMAKE_C_COMPILER_ID}" STREQUAL "xClang"))
  351. check_add_cc_flag("-std=c99") # C99 mode
  352. check_add_cc_flag("-pipe") # Avoid writing temporary files (for compiler speed)
  353. check_add_cc_flag("-Wall") # Warnings
  354. check_add_cc_flag("-Wextra") # More warnings
  355. check_add_cc_flag("-Wpedantic") # Standard compliance
  356. check_add_cc_flag("-Werror") # All warnings are errors
  357. check_add_cc_flag("-Wno-static-in-inline") # Clang doesn't like the use of static inline methods inside static inline methods
  358. check_add_cc_flag("-Wno-overlength-strings") # May happen in the nodeset compiler when complex values are directly encoded
  359. check_add_cc_flag("-Wno-unused-parameter") # some methods may require unused arguments to cast to a method pointer
  360. # Use a strict subset of the C and C++ languages
  361. check_add_cc_flag("-Wc++-compat")
  362. # Check that format strings (printf/scanf) are sane
  363. check_add_cc_flag("-Wformat")
  364. check_add_cc_flag("-Wformat-security")
  365. check_add_cc_flag("-Wformat-nonliteral")
  366. # Check prototype definitions
  367. check_add_cc_flag("-Wmissing-prototypes")
  368. check_add_cc_flag("-Wstrict-prototypes")
  369. check_add_cc_flag("-Wredundant-decls")
  370. check_add_cc_flag("-Wuninitialized")
  371. check_add_cc_flag("-Winit-self")
  372. check_add_cc_flag("-Wcast-qual")
  373. check_add_cc_flag("-Wstrict-overflow")
  374. check_add_cc_flag("-Wnested-externs")
  375. check_add_cc_flag("-Wmultichar")
  376. check_add_cc_flag("-Wundef")
  377. check_add_cc_flag("-fno-strict-aliasing") # fewer compiler assumptions about pointer types
  378. check_add_cc_flag("-fexceptions") # recommended for multi-threaded C code, also in combination with C++ code
  379. if (NOT MINGW)
  380. if(UA_ENABLE_HARDENING)
  381. check_add_cc_flag("-fstack-protector-strong") # more performant stack protector, available since gcc 4.9
  382. check_add_cc_flag("-fstack-clash-protection") # increased reliability of stack overflow detection, available since gcc 8
  383. # future use (control flow integrity protection)
  384. check_add_cc_flag("-mcet")
  385. check_add_cc_flag("-fcf-protection")
  386. endif()
  387. # IPO requires too much memory for unit tests
  388. # GCC docu recommends to compile all files with the same options, therefore ignore it completely
  389. if(NOT UA_BUILD_UNIT_TESTS)
  390. # needed to check if IPO is supported (check needs cmake > 3.9)
  391. if("${CMAKE_VERSION}" VERSION_GREATER 3.9)
  392. cmake_policy(SET CMP0069 NEW) # needed as long as required cmake < 3.9
  393. include(CheckIPOSupported)
  394. check_ipo_supported(RESULT CC_HAS_IPO) # Inter Procedural Optimization / Link Time Optimization (should be same as -flto)
  395. if(CC_HAS_IPO)
  396. set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON)
  397. endif()
  398. endif()
  399. endif()
  400. endif()
  401. if(UA_ENABLE_AMALGAMATION)
  402. add_definitions(-Wno-unused-function)
  403. endif()
  404. if(UA_PACK_DEBIAN)
  405. remove_definitions(-Wno-static-in-inline)
  406. endif()
  407. # Linker
  408. set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "") # cmake sets -rdynamic by default
  409. # Debug
  410. if(BUILD_TYPE_LOWER_CASE STREQUAL "debug" AND UNIX)
  411. if("x${CMAKE_C_COMPILER_ID}" STREQUAL "xClang" AND NOT UA_ENABLE_UNIT_TESTS_MEMCHECK)
  412. # Add default sanitizer settings when using clang and Debug build.
  413. # This allows e.g. CLion to find memory locations for SegFaults
  414. message(STATUS "Sanitizer enabled")
  415. set(SANITIZER_FLAGS "-g -fno-omit-frame-pointer -gline-tables-only -fsanitize=address -fsanitize-address-use-after-scope -fsanitize-coverage=trace-pc-guard,trace-cmp -fsanitize=leak -fsanitize=undefined")
  416. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SANITIZER_FLAGS}")
  417. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SANITIZER_FLAGS}")
  418. endif()
  419. endif()
  420. if(UA_ENABLE_HARDENING AND ((CMAKE_BUILD_TYPE STREQUAL "Release") OR (CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")))
  421. check_add_cc_flag("-D_FORTIFY_SOURCE=2") # run-time buffer overflow detection (needs at least -O1)
  422. endif()
  423. # Strip release builds
  424. if(CMAKE_BUILD_TYPE STREQUAL "MinSizeRel" OR CMAKE_BUILD_TYPE STREQUAL "Release")
  425. check_add_cc_flag("-ffunction-sections")
  426. check_add_cc_flag("-fdata-sections")
  427. check_add_cc_flag("-fno-unwind-tables")
  428. check_add_cc_flag("-fno-asynchronous-unwind-tables")
  429. check_add_cc_flag("-fno-math-errno")
  430. check_add_cc_flag("-fno-ident")
  431. # remove stack-protector with MinSizeRel
  432. if(CMAKE_BUILD_TYPE STREQUAL "MinSizeRel")
  433. check_add_cc_flag("-fno-stack-protector")
  434. endif()
  435. if(NOT OS9)
  436. set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -s")
  437. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -s")
  438. endif()
  439. if(APPLE)
  440. set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,-dead_strip")
  441. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-dead_strip")
  442. else()
  443. set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,--gc-sections")
  444. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--gc-sections")
  445. endif()
  446. if(NOT WIN32 AND NOT CYGWIN AND NOT APPLE)
  447. # these settings reduce the binary size by ~2kb
  448. set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,-z,norelro -Wl,--hash-style=gnu -Wl,--build-id=none")
  449. endif()
  450. endif()
  451. endif()
  452. if(APPLE)
  453. set(CMAKE_MACOSX_RPATH 1)
  454. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_DARWIN_C_SOURCE=1")
  455. endif()
  456. if(MSVC)
  457. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W3 /WX /w44996") # Compiler warnings, error on warning
  458. if(UA_MSVC_FORCE_STATIC_CRT AND NOT BUILD_SHARED_LIBS)
  459. set(CompilerFlags CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE CMAKE_C_FLAGS
  460. CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE)
  461. foreach(CompilerFlag ${CompilerFlags})
  462. string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}")
  463. endforeach()
  464. endif()
  465. endif()
  466. if(UA_BUILD_FUZZING OR UA_BUILD_OSS_FUZZ)
  467. add_definitions(-DUA_malloc=UA_memoryManager_malloc)
  468. add_definitions(-DUA_free=UA_memoryManager_free)
  469. add_definitions(-DUA_calloc=UA_memoryManager_calloc)
  470. add_definitions(-DUA_realloc=UA_memoryManager_realloc)
  471. endif()
  472. #########################
  473. # Generate Main Library #
  474. #########################
  475. file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/src_generated")
  476. configure_file(include/open62541/config.h.in ${PROJECT_BINARY_DIR}/src_generated/open62541/config.h)
  477. configure_file(open62541.pc.in ${PROJECT_BINARY_DIR}/src_generated/open62541.pc @ONLY)
  478. if(UA_ENABLE_DISCOVERY_MULTICAST)
  479. set(MDNSD_LOGLEVEL 300 CACHE STRING "Level at which logs shall be reported" FORCE)
  480. configure_file("deps/mdnsd/libmdnsd/mdnsd_config.h.in" "${PROJECT_BINARY_DIR}/src_generated/mdnsd_config.h")
  481. endif()
  482. set(exported_headers ${exported_headers}
  483. ${PROJECT_BINARY_DIR}/src_generated/open62541/config.h
  484. ${PROJECT_SOURCE_DIR}/include/open62541/architecture_base.h
  485. ${ua_architecture_headers_beginning}
  486. )
  487. if(NOT "${UA_AMALGAMATION_ARCHITECTURES}" STREQUAL "")
  488. foreach(arch ${UA_AMALGAMATION_ARCHITECTURES})
  489. list(APPEND exported_headers ${PROJECT_SOURCE_DIR}/arch/${arch}/ua_architecture.h)
  490. endforeach()
  491. else()
  492. list(APPEND exported_headers ${PROJECT_SOURCE_DIR}/arch/${UA_ARCHITECTURE}/ua_architecture.h)
  493. endif()
  494. if(UA_ENABLE_HISTORIZING)
  495. # Historizing needs to be included before server_config.h
  496. set(historizing_exported_headers
  497. ${PROJECT_SOURCE_DIR}/include/open62541/plugin/historydatabase.h)
  498. endif()
  499. set(exported_headers ${exported_headers}
  500. ${PROJECT_SOURCE_DIR}/deps/ms_stdint.h
  501. ${PROJECT_SOURCE_DIR}/include/open62541/architecture_definitions.h
  502. ${PROJECT_BINARY_DIR}/src_generated/open62541/statuscodes.h
  503. ${PROJECT_BINARY_DIR}/src_generated/open62541/nodeids.h
  504. ${PROJECT_SOURCE_DIR}/include/open62541/constants.h
  505. ${PROJECT_SOURCE_DIR}/include/open62541/types.h
  506. ${PROJECT_BINARY_DIR}/src_generated/open62541/types_generated.h
  507. ${PROJECT_BINARY_DIR}/src_generated/open62541/types_generated_handling.h
  508. ${PROJECT_SOURCE_DIR}/include/open62541/util.h
  509. ${PROJECT_SOURCE_DIR}/include/open62541/server.h
  510. ${PROJECT_SOURCE_DIR}/include/open62541/plugin/log.h
  511. ${PROJECT_SOURCE_DIR}/include/open62541/plugin/network.h
  512. ${PROJECT_SOURCE_DIR}/include/open62541/plugin/accesscontrol.h
  513. ${PROJECT_SOURCE_DIR}/include/open62541/plugin/pki.h
  514. ${PROJECT_SOURCE_DIR}/include/open62541/plugin/securitypolicy.h
  515. ${PROJECT_SOURCE_DIR}/include/open62541/server_pubsub.h
  516. ${PROJECT_SOURCE_DIR}/include/open62541/plugin/pubsub.h
  517. ${PROJECT_SOURCE_DIR}/deps/ziptree.h
  518. ${PROJECT_SOURCE_DIR}/include/open62541/plugin/nodestore.h
  519. ${historizing_exported_headers}
  520. ${PROJECT_SOURCE_DIR}/include/open62541/server_config.h
  521. ${PROJECT_SOURCE_DIR}/include/open62541/client_config.h
  522. ${PROJECT_SOURCE_DIR}/include/open62541/client.h
  523. ${PROJECT_SOURCE_DIR}/include/open62541/client_highlevel.h
  524. ${PROJECT_SOURCE_DIR}/include/open62541/client_subscriptions.h
  525. ${PROJECT_SOURCE_DIR}/include/open62541/client_highlevel_async.h)
  526. set(internal_headers ${PROJECT_SOURCE_DIR}/deps/open62541_queue.h
  527. ${PROJECT_SOURCE_DIR}/deps/pcg_basic.h
  528. ${PROJECT_SOURCE_DIR}/deps/libc_time.h
  529. ${PROJECT_SOURCE_DIR}/deps/base64.h
  530. ${PROJECT_SOURCE_DIR}/src/ua_util_internal.h
  531. ${PROJECT_SOURCE_DIR}/src/ua_types_encoding_binary.h
  532. ${PROJECT_BINARY_DIR}/src_generated/open62541/types_generated_encoding_binary.h
  533. ${PROJECT_BINARY_DIR}/src_generated/open62541/transport_generated.h
  534. ${PROJECT_BINARY_DIR}/src_generated/open62541/transport_generated_handling.h
  535. ${PROJECT_BINARY_DIR}/src_generated/open62541/transport_generated_encoding_binary.h
  536. ${PROJECT_SOURCE_DIR}/src/ua_connection_internal.h
  537. ${PROJECT_SOURCE_DIR}/src/ua_securechannel.h
  538. ${PROJECT_SOURCE_DIR}/src/ua_workqueue.h
  539. ${PROJECT_SOURCE_DIR}/src/ua_timer.h
  540. ${PROJECT_SOURCE_DIR}/src/server/ua_session.h
  541. ${PROJECT_SOURCE_DIR}/src/server/ua_subscription.h
  542. ${PROJECT_SOURCE_DIR}/src/server/ua_session_manager.h
  543. ${PROJECT_SOURCE_DIR}/src/server/ua_securechannel_manager.h
  544. ${PROJECT_SOURCE_DIR}/src/pubsub/ua_pubsub_networkmessage.h
  545. ${PROJECT_SOURCE_DIR}/src/pubsub/ua_pubsub.h
  546. ${PROJECT_SOURCE_DIR}/src/pubsub/ua_pubsub_manager.h
  547. ${PROJECT_SOURCE_DIR}/src/pubsub/ua_pubsub_ns0.h
  548. ${PROJECT_SOURCE_DIR}/src/server/ua_server_internal.h
  549. ${PROJECT_SOURCE_DIR}/src/server/ua_services.h
  550. ${PROJECT_SOURCE_DIR}/src/client/ua_client_internal.h
  551. ${PROJECT_SOURCE_DIR}/src/server/ua_server_async.h)
  552. # TODO: make client optional
  553. set(lib_sources ${PROJECT_SOURCE_DIR}/src/ua_types.c
  554. ${PROJECT_SOURCE_DIR}/src/ua_types_encoding_binary.c
  555. ${PROJECT_BINARY_DIR}/src_generated/open62541/types_generated.c
  556. ${PROJECT_BINARY_DIR}/src_generated/open62541/transport_generated.c
  557. ${PROJECT_BINARY_DIR}/src_generated/open62541/statuscodes.c
  558. ${PROJECT_SOURCE_DIR}/src/ua_util.c
  559. ${PROJECT_SOURCE_DIR}/src/ua_workqueue.c
  560. ${PROJECT_SOURCE_DIR}/src/ua_timer.c
  561. ${PROJECT_SOURCE_DIR}/src/ua_connection.c
  562. ${PROJECT_SOURCE_DIR}/src/ua_securechannel.c
  563. ${PROJECT_SOURCE_DIR}/src/server/ua_session.c
  564. ${PROJECT_SOURCE_DIR}/src/server/ua_nodes.c
  565. ${PROJECT_SOURCE_DIR}/src/server/ua_server.c
  566. ${PROJECT_SOURCE_DIR}/src/server/ua_server_ns0.c
  567. ${PROJECT_SOURCE_DIR}/src/server/ua_server_config.c
  568. ${PROJECT_SOURCE_DIR}/src/server/ua_server_binary.c
  569. ${PROJECT_SOURCE_DIR}/src/server/ua_server_utils.c
  570. ${PROJECT_SOURCE_DIR}/src/server/ua_server_discovery.c
  571. ${PROJECT_SOURCE_DIR}/src/server/ua_server_async.c
  572. ${PROJECT_SOURCE_DIR}/src/server/ua_securechannel_manager.c
  573. ${PROJECT_SOURCE_DIR}/src/server/ua_session_manager.c
  574. ${PROJECT_SOURCE_DIR}/src/pubsub/ua_pubsub_networkmessage.c
  575. ${PROJECT_SOURCE_DIR}/src/pubsub/ua_pubsub_writer.c
  576. ${PROJECT_SOURCE_DIR}/src/pubsub/ua_pubsub_reader.c
  577. ${PROJECT_SOURCE_DIR}/src/pubsub/ua_pubsub_manager.c
  578. ${PROJECT_SOURCE_DIR}/src/pubsub/ua_pubsub_ns0.c
  579. # services
  580. ${PROJECT_SOURCE_DIR}/src/server/ua_services_view.c
  581. ${PROJECT_SOURCE_DIR}/src/server/ua_services_method.c
  582. ${PROJECT_SOURCE_DIR}/src/server/ua_services_session.c
  583. ${PROJECT_SOURCE_DIR}/src/server/ua_services_attribute.c
  584. ${PROJECT_SOURCE_DIR}/src/server/ua_services_discovery.c
  585. ${PROJECT_SOURCE_DIR}/src/server/ua_services_subscription.c
  586. ${PROJECT_SOURCE_DIR}/src/server/ua_services_monitoreditem.c
  587. ${PROJECT_SOURCE_DIR}/src/server/ua_services_securechannel.c
  588. ${PROJECT_SOURCE_DIR}/src/server/ua_services_nodemanagement.c
  589. ${PROJECT_SOURCE_DIR}/src/server/ua_services_discovery_multicast.c
  590. # client
  591. ${PROJECT_SOURCE_DIR}/src/client/ua_client.c
  592. ${PROJECT_SOURCE_DIR}/src/client/ua_client_connect.c
  593. ${PROJECT_SOURCE_DIR}/src/client/ua_client_connect_async.c
  594. ${PROJECT_SOURCE_DIR}/src/client/ua_client_discovery.c
  595. ${PROJECT_SOURCE_DIR}/src/client/ua_client_highlevel.c
  596. ${PROJECT_SOURCE_DIR}/src/client/ua_client_subscriptions.c
  597. ${PROJECT_SOURCE_DIR}/src/client/ua_client_worker.c
  598. # dependencies
  599. ${PROJECT_SOURCE_DIR}/deps/libc_time.c
  600. ${PROJECT_SOURCE_DIR}/deps/pcg_basic.c
  601. ${PROJECT_SOURCE_DIR}/deps/base64.c)
  602. set(default_plugin_headers ${PROJECT_SOURCE_DIR}/plugins/include/open62541/plugin/accesscontrol_default.h
  603. ${PROJECT_SOURCE_DIR}/plugins/include/open62541/plugin/pki_default.h
  604. ${PROJECT_SOURCE_DIR}/plugins/include/open62541/plugin/log_stdout.h
  605. ${PROJECT_SOURCE_DIR}/plugins/include/open62541/plugin/nodestore_default.h
  606. ${PROJECT_SOURCE_DIR}/plugins/include/open62541/server_config_default.h
  607. ${PROJECT_SOURCE_DIR}/plugins/include/open62541/client_config_default.h
  608. ${PROJECT_SOURCE_DIR}/plugins/include/open62541/plugin/securitypolicy_default.h
  609. )
  610. set(default_plugin_sources ${PROJECT_SOURCE_DIR}/plugins/ua_log_stdout.c
  611. ${PROJECT_SOURCE_DIR}/plugins/ua_accesscontrol_default.c
  612. ${PROJECT_SOURCE_DIR}/plugins/ua_pki_default.c
  613. ${PROJECT_SOURCE_DIR}/plugins/ua_nodestore_ziptree.c
  614. ${PROJECT_SOURCE_DIR}/plugins/ua_nodestore_hashmap.c
  615. ${PROJECT_SOURCE_DIR}/plugins/ua_config_default.c
  616. ${PROJECT_SOURCE_DIR}/plugins/securityPolicies/ua_securitypolicy_none.c
  617. )
  618. if(UA_GENERATED_NAMESPACE_ZERO)
  619. list(APPEND internal_headers ${PROJECT_BINARY_DIR}/src_generated/open62541/namespace0_generated.h)
  620. list(APPEND lib_sources ${PROJECT_BINARY_DIR}/src_generated/open62541/namespace0_generated.c)
  621. endif()
  622. list(APPEND default_plugin_headers
  623. ${PROJECT_SOURCE_DIR}/plugins/include/open62541/plugin/securitypolicy_mbedtls_common.h)
  624. list(APPEND default_plugin_sources
  625. ${PROJECT_SOURCE_DIR}/plugins/securityPolicies/securitypolicy_mbedtls_common.c
  626. ${PROJECT_SOURCE_DIR}/plugins/securityPolicies/ua_securitypolicy_basic128rsa15.c
  627. ${PROJECT_SOURCE_DIR}/plugins/securityPolicies/ua_securitypolicy_basic256.c
  628. ${PROJECT_SOURCE_DIR}/plugins/securityPolicies/ua_securitypolicy_basic256sha256.c)
  629. if(UA_ENABLE_HISTORIZING)
  630. list(APPEND default_plugin_headers
  631. ${PROJECT_SOURCE_DIR}/plugins/include/open62541/plugin/historydata/history_data_backend.h
  632. ${PROJECT_SOURCE_DIR}/plugins/include/open62541/plugin/historydata/history_data_gathering.h
  633. ${PROJECT_SOURCE_DIR}/plugins/include/open62541/plugin/historydata/history_database_default.h
  634. ${PROJECT_SOURCE_DIR}/plugins/include/open62541/plugin/historydata/history_data_gathering_default.h
  635. ${PROJECT_SOURCE_DIR}/plugins/include/open62541/plugin/historydata/history_data_backend_memory.h
  636. )
  637. list(APPEND default_plugin_sources
  638. ${PROJECT_SOURCE_DIR}/plugins/historydata/ua_history_data_backend_memory.c
  639. ${PROJECT_SOURCE_DIR}/plugins/historydata/ua_history_data_gathering_default.c
  640. ${PROJECT_SOURCE_DIR}/plugins/historydata/ua_history_database_default.c
  641. )
  642. endif()
  643. if(UA_ENABLE_DISCOVERY)
  644. list(INSERT internal_headers 13 ${PROJECT_SOURCE_DIR}/src/server/ua_discovery_manager.h)
  645. list(APPEND lib_sources ${PROJECT_SOURCE_DIR}/src/server/ua_discovery_manager.c)
  646. endif()
  647. if(UA_ENABLE_PUBSUB)
  648. list(APPEND default_plugin_headers ${PROJECT_SOURCE_DIR}/plugins/include/open62541/plugin/pubsub_udp.h)
  649. list(APPEND default_plugin_sources ${PROJECT_SOURCE_DIR}/plugins/ua_pubsub_udp.c)
  650. if(UA_ENABLE_PUBSUB_ETH_UADP)
  651. list(APPEND default_plugin_headers ${PROJECT_SOURCE_DIR}/plugins/include/open62541/plugin/pubsub_ethernet.h)
  652. list(APPEND default_plugin_sources ${PROJECT_SOURCE_DIR}/plugins/ua_pubsub_ethernet.c)
  653. endif()
  654. if(UA_ENABLE_PUBSUB_MQTT)
  655. if(WIN32)
  656. MESSAGE(WARNING "Multithreading is enabled in MQTT plugin. This feature is under development and marked as EXPERIMENTAL")
  657. else()
  658. list(APPEND default_plugin_headers ${PROJECT_SOURCE_DIR}/deps/mqtt-c/mqtt_pal.h)
  659. list(APPEND default_plugin_sources ${PROJECT_SOURCE_DIR}/plugins/mqtt/ua_mqtt_pal.c)
  660. list(APPEND default_plugin_headers ${PROJECT_SOURCE_DIR}/deps/mqtt-c/mqtt.h)
  661. list(APPEND default_plugin_sources ${PROJECT_SOURCE_DIR}/deps/mqtt-c/mqtt.c)
  662. list(APPEND default_plugin_headers ${PROJECT_SOURCE_DIR}/plugins/ua_network_pubsub_mqtt.h)
  663. list(APPEND default_plugin_sources ${PROJECT_SOURCE_DIR}/plugins/ua_network_pubsub_mqtt.c)
  664. list(APPEND default_plugin_headers ${PROJECT_SOURCE_DIR}/plugins/mqtt/ua_mqtt_adapter.h)
  665. list(APPEND default_plugin_sources ${PROJECT_SOURCE_DIR}/plugins/mqtt/ua_mqtt_adapter.c)
  666. endif()
  667. endif()
  668. endif()
  669. if(UA_ENABLE_JSON_ENCODING)
  670. if(UA_ENABLE_PUBSUB)
  671. list(APPEND lib_sources ${PROJECT_SOURCE_DIR}/src/pubsub/ua_pubsub_networkmessage_json.c)
  672. endif()
  673. list(APPEND internal_headers ${PROJECT_SOURCE_DIR}/deps/jsmn/jsmn.h
  674. ${PROJECT_SOURCE_DIR}/deps/string_escape.h
  675. ${PROJECT_SOURCE_DIR}/deps/itoa.h
  676. ${PROJECT_SOURCE_DIR}/deps/atoi.h
  677. ${PROJECT_SOURCE_DIR}/src/ua_types_encoding_json.h)
  678. list(APPEND lib_sources ${PROJECT_SOURCE_DIR}/deps/jsmn/jsmn.c
  679. ${PROJECT_SOURCE_DIR}/deps/string_escape.c
  680. ${PROJECT_SOURCE_DIR}/deps/itoa.c
  681. ${PROJECT_SOURCE_DIR}/deps/atoi.c
  682. ${PROJECT_SOURCE_DIR}/src/ua_types_encoding_json.c)
  683. endif()
  684. if(UA_ENABLE_CUSTOM_LIBC)
  685. list(APPEND lib_sources ${PROJECT_SOURCE_DIR}/deps/musl/floatscan.c
  686. ${PROJECT_SOURCE_DIR}/deps/musl/vfprintf.c)
  687. endif()
  688. if(UA_ENABLE_SUBSCRIPTIONS)
  689. list(APPEND lib_sources ${PROJECT_SOURCE_DIR}/src/server/ua_subscription.c
  690. ${PROJECT_SOURCE_DIR}/src/server/ua_subscription_monitoreditem.c
  691. ${PROJECT_SOURCE_DIR}/src/server/ua_subscription_datachange.c)
  692. if(UA_ENABLE_SUBSCRIPTIONS_EVENTS)
  693. list(APPEND lib_sources ${PROJECT_SOURCE_DIR}/src/server/ua_subscription_events.c)
  694. endif()
  695. endif()
  696. if(UA_DEBUG_DUMP_PKGS)
  697. list(APPEND lib_sources ${PROJECT_SOURCE_DIR}/plugins/ua_debug_dump_pkgs.c)
  698. endif()
  699. if(UA_ENABLE_DISCOVERY_MULTICAST)
  700. # prepend in list, otherwise it complains that winsock2.h has to be included before windows.h
  701. set(internal_headers ${PROJECT_BINARY_DIR}/src_generated/mdnsd_config.h
  702. ${PROJECT_SOURCE_DIR}/deps/mdnsd/libmdnsd/1035.h
  703. ${PROJECT_SOURCE_DIR}/deps/mdnsd/libmdnsd/xht.h
  704. ${PROJECT_SOURCE_DIR}/deps/mdnsd/libmdnsd/sdtxt.h
  705. ${PROJECT_SOURCE_DIR}/deps/mdnsd/libmdnsd/mdnsd.h
  706. ${internal_headers} )
  707. set(lib_sources ${PROJECT_SOURCE_DIR}/src/server/ua_server_discovery_mdns.c
  708. ${PROJECT_SOURCE_DIR}/deps/mdnsd/libmdnsd/1035.c
  709. ${PROJECT_SOURCE_DIR}/deps/mdnsd/libmdnsd/xht.c
  710. ${PROJECT_SOURCE_DIR}/deps/mdnsd/libmdnsd/sdtxt.c
  711. ${PROJECT_SOURCE_DIR}/deps/mdnsd/libmdnsd/mdnsd.c
  712. ${lib_sources})
  713. endif()
  714. if(UA_BUILD_FUZZING OR UA_BUILD_OSS_FUZZ)
  715. set(lib_sources
  716. ${lib_sources}
  717. ${PROJECT_SOURCE_DIR}/tests/fuzz/custom_memory_manager.c)
  718. endif()
  719. #########################
  720. # Generate source files #
  721. #########################
  722. # List of nodeset files combined into NS0 generated file
  723. set(UA_FILE_NODESETS)
  724. if(UA_NAMESPACE_ZERO STREQUAL "FULL")
  725. if(NOT UA_FILE_NS0)
  726. set(UA_FILE_NS0 ${PROJECT_SOURCE_DIR}/deps/ua-nodeset/Schema/Opc.Ua.NodeSet2.xml)
  727. endif()
  728. set(UA_FILE_NODESETS "${UA_FILE_NS0}")
  729. if(NOT EXISTS "${UA_FILE_NS0}")
  730. message(FATAL_ERROR "File ${UA_FILE_NS0} not found. You probably need to initialize the git submodule for deps/ua-nodeset.")
  731. endif()
  732. set(UA_FILE_NODEIDS ${PROJECT_SOURCE_DIR}/deps/ua-nodeset/Schema/NodeIds.csv)
  733. set(UA_FILE_STATUSCODES ${PROJECT_SOURCE_DIR}/deps/ua-nodeset/Schema/StatusCode.csv)
  734. set(UA_FILE_TYPES_BSD ${PROJECT_SOURCE_DIR}/deps/ua-nodeset/Schema/Opc.Ua.Types.bsd)
  735. else()
  736. if(NOT UA_FILE_NS0)
  737. set(UA_FILE_NS0 ${PROJECT_SOURCE_DIR}/tools/schema/Opc.Ua.NodeSet2.Reduced.xml)
  738. endif()
  739. set(UA_FILE_NODESETS "${UA_FILE_NS0}")
  740. set(UA_FILE_DATATYPES ${PROJECT_SOURCE_DIR}/tools/schema/datatypes_minimal.txt)
  741. set(UA_FILE_NODEIDS ${PROJECT_SOURCE_DIR}/tools/schema/NodeIds.csv)
  742. set(UA_FILE_STATUSCODES ${PROJECT_SOURCE_DIR}/tools/schema/StatusCode.csv)
  743. set(UA_FILE_TYPES_BSD ${PROJECT_SOURCE_DIR}/tools/schema/Opc.Ua.Types.bsd)
  744. if(UA_ENABLE_METHODCALLS)
  745. list(APPEND UA_FILE_DATATYPES ${PROJECT_SOURCE_DIR}/tools/schema/datatypes_method.txt)
  746. endif()
  747. if(UA_ENABLE_SUBSCRIPTIONS)
  748. list(APPEND UA_FILE_DATATYPES ${PROJECT_SOURCE_DIR}/tools/schema/datatypes_subscriptions.txt)
  749. endif()
  750. if(UA_ENABLE_SUBSCRIPTIONS_EVENTS)
  751. list(APPEND UA_FILE_DATATYPES ${PROJECT_SOURCE_DIR}/tools/schema/datatypes_events.txt)
  752. list(APPEND UA_FILE_NODESETS ${PROJECT_SOURCE_DIR}/tools/schema/Opc.Ua.NodeSet2.EventsMinimal.xml)
  753. endif()
  754. if(UA_ENABLE_HISTORIZING)
  755. list(APPEND UA_FILE_DATATYPES ${PROJECT_SOURCE_DIR}/tools/schema/datatypes_historizing.txt)
  756. list(APPEND UA_FILE_NODESETS ${PROJECT_SOURCE_DIR}/tools/schema/Opc.Ua.NodeSet2.HistorizingMinimal.xml)
  757. endif()
  758. if(UA_ENABLE_DISCOVERY)
  759. list(APPEND UA_FILE_DATATYPES ${PROJECT_SOURCE_DIR}/tools/schema/datatypes_discovery.txt)
  760. endif()
  761. if(UA_ENABLE_QUERY)
  762. list(APPEND UA_FILE_DATATYPES ${PROJECT_SOURCE_DIR}/tools/schema/datatypes_query.txt)
  763. endif()
  764. if(UA_ENABLE_PUBSUB)
  765. list(APPEND UA_FILE_DATATYPES ${PROJECT_SOURCE_DIR}/tools/schema/datatypes_pubsub.txt)
  766. if(UA_ENABLE_PUBSUB_INFORMATIONMODEL)
  767. list(APPEND UA_FILE_NODESETS ${PROJECT_SOURCE_DIR}/tools/schema/Opc.Ua.NodeSet2.PubSubMinimal.xml)
  768. endif()
  769. endif()
  770. if(UA_ENABLE_DA)
  771. list(APPEND UA_FILE_DATATYPES ${PROJECT_SOURCE_DIR}/tools/schema/datatypes_dataaccess.txt)
  772. set(UA_NODESET_FILE_DA ${PROJECT_SOURCE_DIR}/tools/schema/Opc.Ua.NodeSet2.Part8_Subset.xml)
  773. endif()
  774. if(UA_ENABLE_TYPEDESCRIPTION)
  775. list(APPEND UA_FILE_DATATYPES ${PROJECT_SOURCE_DIR}/tools/schema/datatypes_typedescription.txt)
  776. endif()
  777. endif()
  778. # standard-defined data types
  779. ua_generate_datatypes(
  780. BUILTIN
  781. NAME "types"
  782. TARGET_SUFFIX "types"
  783. NAMESPACE_IDX 0
  784. FILE_CSV "${UA_FILE_NODEIDS}"
  785. FILES_BSD "${UA_FILE_TYPES_BSD}"
  786. FILES_SELECTED ${UA_FILE_DATATYPES}
  787. )
  788. # transport data types
  789. ua_generate_datatypes(
  790. INTERNAL
  791. NAME "transport"
  792. TARGET_SUFFIX "transport"
  793. NAMESPACE_IDX 1
  794. FILE_CSV "${UA_FILE_NODEIDS}"
  795. FILES_BSD "${UA_FILE_TYPES_BSD}" "${PROJECT_SOURCE_DIR}/tools/schema/Custom.Opc.Ua.Transport.bsd"
  796. FILES_SELECTED "${PROJECT_SOURCE_DIR}/tools/schema/datatypes_transport.txt"
  797. )
  798. # statuscode explanation
  799. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/open62541/statuscodes.h
  800. ${PROJECT_BINARY_DIR}/src_generated/open62541/statuscodes.c
  801. PRE_BUILD
  802. COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/tools/generate_statuscode_descriptions.py
  803. ${UA_FILE_STATUSCODES} ${PROJECT_BINARY_DIR}/src_generated/open62541/statuscodes
  804. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_statuscode_descriptions.py
  805. ${UA_FILE_STATUSCODES})
  806. # Header containing defines for all NodeIds
  807. ua_generate_nodeid_header(
  808. NAME "nodeids"
  809. ID_PREFIX "NS0"
  810. TARGET_SUFFIX "ids-ns0"
  811. FILE_CSV "${UA_FILE_NODEIDS}"
  812. )
  813. # we need a custom target to avoid that the generator is called concurrently and
  814. # thus overwriting files while the other thread is compiling
  815. add_custom_target(open62541-generator-statuscode DEPENDS
  816. ${PROJECT_BINARY_DIR}/src_generated/open62541/nodeids.h
  817. ${PROJECT_BINARY_DIR}/src_generated/open62541/statuscodes.h
  818. ${PROJECT_BINARY_DIR}/src_generated/open62541/statuscodes.c)
  819. if (UA_ENABLE_AMALGAMATION)
  820. # single-file release
  821. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/open62541.h
  822. PRE_BUILD
  823. COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tools/amalgamate.py
  824. ${OPEN62541_VER_COMMIT} ${CMAKE_CURRENT_BINARY_DIR}/open62541.h
  825. ${exported_headers} ${default_plugin_headers} ${ua_architecture_headers}
  826. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/amalgamate.py
  827. ${exported_headers} ${default_plugin_headers} ${ua_architecture_headers})
  828. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/open62541.c
  829. PRE_BUILD
  830. COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tools/amalgamate.py
  831. ${OPEN62541_VER_COMMIT} ${CMAKE_CURRENT_BINARY_DIR}/open62541.c
  832. ${internal_headers} ${lib_sources} ${default_plugin_sources} ${ua_architecture_sources}
  833. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/amalgamate.py ${internal_headers}
  834. ${lib_sources} ${default_plugin_sources} ${ua_architecture_sources} )
  835. add_custom_target(open62541-amalgamation-source DEPENDS ${PROJECT_BINARY_DIR}/open62541.c)
  836. add_custom_target(open62541-amalgamation-header DEPENDS ${PROJECT_BINARY_DIR}/open62541.h)
  837. add_dependencies(open62541-amalgamation-header open62541-generator-types)
  838. add_dependencies(open62541-amalgamation-source open62541-generator-types
  839. open62541-generator-transport open62541-generator-statuscode)
  840. endif()
  841. ua_generate_nodeset(
  842. NAME "ns0"
  843. FILE ${UA_FILE_NODESETS} ${UA_NODESET_FILE_DA}
  844. INTERNAL
  845. IGNORE "${PROJECT_SOURCE_DIR}/tools/nodeset_compiler/NodeID_NS0_Base.txt"
  846. DEPENDS_TARGET "open62541-generator-types"
  847. )
  848. # stack protector and optimization needs to be disabled for the huge ns0 file, otherwise debian packaging fails due to long build times.
  849. # We also disable optimization on Appveyor builds, since they take almost an hour otherwise
  850. if(UA_PACK_DEBIAN OR (NOT "$ENV{APPVEYOR}" STREQUAL "") OR (
  851. (CMAKE_BUILD_TYPE STREQUAL "MinSizeRel" OR CMAKE_BUILD_TYPE STREQUAL "Release" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo") AND (
  852. # List of compilers which have problems with the huge ns0 optimization
  853. (("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") AND (CMAKE_C_COMPILER_VERSION VERSION_LESS 7.0))
  854. )
  855. ))
  856. set_source_files_properties(${PROJECT_BINARY_DIR}/src_generated/open62541/namespace0_generated.c PROPERTIES COMPILE_FLAGS "-fno-stack-protector -O0")
  857. endif()
  858. #####################
  859. # Build the Library #
  860. #####################
  861. assign_source_group(${lib_sources})
  862. assign_source_group(${internal_headers})
  863. assign_source_group(${exported_headers})
  864. assign_source_group(${default_plugin_sources})
  865. assign_source_group(${ua_architecture_sources})
  866. if(UA_ENABLE_AMALGAMATION)
  867. add_library(open62541-object OBJECT ${PROJECT_BINARY_DIR}/open62541.c ${PROJECT_BINARY_DIR}/open62541.h)
  868. target_include_directories(open62541-object PRIVATE ${PROJECT_BINARY_DIR})
  869. target_include_directories(open62541-object PRIVATE "${ua_architecture_directories_to_include}")
  870. if(UA_ENABLE_ENCRYPTION)
  871. target_include_directories(open62541-object PRIVATE ${MBEDTLS_INCLUDE_DIRS})
  872. endif()
  873. # make sure the open62541_amalgamation target builds before so that amalgamation is finished and it is not executed again for open62541-object
  874. # and thus may overwrite the amalgamation result during multiprocessor compilation
  875. # the header is already a dependency of open62541 target itself
  876. add_custom_target(open62541-code-generation DEPENDS
  877. open62541-amalgamation-header
  878. open62541-generator-types
  879. open62541-generator-transport
  880. open62541-generator-statuscode
  881. open62541-amalgamation-source
  882. )
  883. add_library(open62541 $<TARGET_OBJECTS:open62541-object>)
  884. # the only directory that needs to be included if open62541 (amalgameted) target from
  885. # the build directory is ${PROJECT_BINARY_DIR}, that contains the generated open62541.h
  886. target_include_directories(open62541 PUBLIC $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>)
  887. if(UA_COMPILE_AS_CXX)
  888. set_source_files_properties(${PROJECT_BINARY_DIR}/open62541.c PROPERTIES LANGUAGE CXX)
  889. endif()
  890. add_dependencies(open62541-amalgamation-source open62541-generator-namespace)
  891. add_dependencies(open62541-amalgamation-header open62541-generator-namespace)
  892. else()
  893. add_library(open62541-object OBJECT ${lib_sources} ${internal_headers} ${exported_headers})
  894. add_custom_target(open62541-code-generation DEPENDS
  895. open62541-generator-types
  896. open62541-generator-transport
  897. open62541-generator-statuscode
  898. open62541-generator-namespace
  899. )
  900. target_include_directories(open62541-object PRIVATE ${PROJECT_SOURCE_DIR}/src)
  901. add_library(open62541-plugins OBJECT ${default_plugin_sources} ${ua_architecture_sources} ${exported_headers})
  902. add_dependencies(open62541-plugins open62541-generator-types open62541-generator-transport open62541-generator-namespace)
  903. target_include_directories(open62541-plugins PRIVATE ${PROJECT_SOURCE_DIR}/plugins)
  904. target_include_directories(open62541-plugins PRIVATE ${PROJECT_BINARY_DIR}/src_generated)
  905. target_compile_definitions(open62541-plugins PRIVATE -DUA_DYNAMIC_LINKING_EXPORT)
  906. set_target_properties(open62541-plugins PROPERTIES FOLDER "open62541/lib")
  907. add_library(open62541 $<TARGET_OBJECTS:open62541-object> $<TARGET_OBJECTS:open62541-plugins>)
  908. if(UA_COMPILE_AS_CXX)
  909. set_source_files_properties(${lib_sources} PROPERTIES LANGUAGE CXX)
  910. set_source_files_properties(${default_plugin_sources} ${ua_architecture_sources} PROPERTIES LANGUAGE CXX)
  911. endif()
  912. # Declare include directories
  913. function(include_directories_private)
  914. foreach(_include_dir IN ITEMS ${ARGN})
  915. target_include_directories(open62541-object PRIVATE ${_include_dir})
  916. target_include_directories(open62541-plugins PRIVATE ${_include_dir})
  917. endforeach()
  918. endfunction()
  919. function(include_directories_public)
  920. include_directories_private(${ARGN})
  921. foreach(_include_dir IN ITEMS ${ARGN})
  922. target_include_directories(open62541 PUBLIC $<BUILD_INTERFACE:${_include_dir}>)
  923. endforeach()
  924. endfunction()
  925. # Public includes
  926. include_directories_public(${ua_architecture_directories_to_include}
  927. "${PROJECT_SOURCE_DIR}/include"
  928. "${PROJECT_SOURCE_DIR}/plugins/include"
  929. "${PROJECT_SOURCE_DIR}/deps"
  930. "${PROJECT_SOURCE_DIR}/src/pubsub"
  931. "${PROJECT_BINARY_DIR}/src_generated")
  932. # Private includes
  933. include_directories_private("${PROJECT_BINARY_DIR}")
  934. if(UA_ENABLE_ENCRYPTION)
  935. include_directories_private(${MBEDTLS_INCLUDE_DIRS})
  936. endif()
  937. # Option-specific includes
  938. if(UA_ENABLE_DISCOVERY)
  939. include_directories_private("${PROJECT_SOURCE_DIR}/src/client")
  940. endif()
  941. endif()
  942. add_dependencies(open62541-object open62541-code-generation)
  943. # Ensure that the open62541::open62541 alias can be used inside open62541's build
  944. add_library(open62541::open62541 ALIAS open62541)
  945. # Export Symbols
  946. target_compile_definitions(open62541-object PRIVATE -DUA_DYNAMIC_LINKING_EXPORT)
  947. target_compile_definitions(open62541 PRIVATE -DUA_DYNAMIC_LINKING_EXPORT)
  948. if (UA_ENABLE_DISCOVERY_MULTICAST)
  949. target_compile_definitions(open62541-object PRIVATE -DMDNSD_DYNAMIC_LINKING_EXPORT)
  950. target_compile_definitions(open62541 PRIVATE -DMDNSD_DYNAMIC_LINKING_EXPORT)
  951. endif()
  952. # Generate properly versioned shared library links on Linux
  953. SET_TARGET_PROPERTIES(open62541 PROPERTIES SOVERSION "${OPEN62541_VER_MAJOR}" VERSION "${OPEN62541_VER_MAJOR}.${OPEN62541_VER_MINOR}.${OPEN62541_VER_PATCH}")
  954. ##################################
  955. # Architectures changes #
  956. ##################################
  957. GET_PROPERTY(ua_architecture_add_definitions GLOBAL PROPERTY UA_ARCHITECTURE_ADD_DEFINITIONS)
  958. add_definitions(${ua_architecture_add_definitions})
  959. GET_PROPERTY(ua_architecture_remove_definitions GLOBAL PROPERTY UA_ARCHITECTURE_REMOVE_DEFINITIONS)
  960. if (NOT "${ua_architecture_remove_definitions}" STREQUAL "")
  961. string(REPLACE " " ";" ua_architecture_remove_definitions_list ${ua_architecture_remove_definitions})
  962. remove_definitions(${ua_architecture_remove_definitions_list})
  963. foreach ( flag ${ua_architecture_remove_definitions} )
  964. string(REPLACE "${flag}" "" CMAKE_C_FLAGS ${CMAKE_C_FLAGS})
  965. endforeach()
  966. endif(NOT "${ua_architecture_remove_definitions}" STREQUAL "")
  967. GET_PROPERTY(ua_architecture_append_to_library GLOBAL PROPERTY UA_ARCHITECTURE_APPEND_TO_LIBRARY)
  968. list(APPEND open62541_LIBRARIES ${ua_architecture_append_to_library})
  969. target_compile_definitions(open62541 PUBLIC UA_ARCHITECTURE_${UA_ARCHITECTURE_UPPER})
  970. # DLL requires linking to dependencies
  971. target_link_libraries(open62541 ${open62541_LIBRARIES})
  972. ##########################
  973. # Build Selected Targets #
  974. ##########################
  975. # always include, builds with make doc
  976. add_subdirectory(doc)
  977. if(UA_BUILD_EXAMPLES)
  978. if(UA_ENABLE_AMALGAMATION)
  979. # Cannot compile tests with amalgamation. Not prepared for single header include
  980. message(FATAL_ERROR "Examples cannot be built with source amalgamation enabled")
  981. endif()
  982. add_subdirectory(examples)
  983. endif()
  984. if(UA_BUILD_UNIT_TESTS)
  985. if(UA_ENABLE_AMALGAMATION)
  986. # Cannot compile tests with amalgamation. Amalgamation uses the default plugins, not the testing plugins
  987. message(FATAL_ERROR "Unit tests cannot be generated with source amalgamation enabled")
  988. endif()
  989. enable_testing()
  990. add_subdirectory(tests)
  991. endif()
  992. if(UA_BUILD_FUZZING OR UA_BUILD_OSS_FUZZ OR UA_BUILD_FUZZING_CORPUS)
  993. add_subdirectory(tests/fuzz)
  994. endif()
  995. if(UA_BUILD_TOOLS)
  996. if(UA_ENABLE_JSON_ENCODING)
  997. add_subdirectory(tools/ua2json)
  998. endif()
  999. endif()
  1000. ########################
  1001. # Linting as target #
  1002. ########################
  1003. include(linting_target)
  1004. ##########################
  1005. # Installation #
  1006. ##########################
  1007. # invoke via `make install`
  1008. # specify install location with `-DCMAKE_INSTALL_PREFIX=xyz`
  1009. # Enable shared library with `-DBUILD_SHARED_LIBS=ON`
  1010. set(cmake_configfile_install ${CMAKE_INSTALL_LIBDIR}/cmake/open62541)
  1011. set(open62541_install_tools_dir share/open62541/tools)
  1012. set(open62541_install_nodeset_dir share/open62541/tools/ua-nodeset)
  1013. # This list of components allows to define a find_package requirement.
  1014. # E.g.:
  1015. # find_package(open62541 1.0.0 REQUIRED COMPONENTS Events Methods FullNamespace)
  1016. set(open62541_enabled_components "")
  1017. if(UA_NAMESPACE_ZERO STREQUAL "FULL")
  1018. list(APPEND open62541_enabled_components "FullNamespace")
  1019. endif()
  1020. if(UA_ENABLE_METHODCALLS)
  1021. list(APPEND open62541_enabled_components "Methods")
  1022. endif()
  1023. if(UA_ENABLE_SUBSCRIPTIONS)
  1024. list(APPEND open62541_enabled_components "Subscriptions")
  1025. endif()
  1026. if(UA_ENABLE_PUBSUB)
  1027. list(APPEND open62541_enabled_components "PubSub")
  1028. endif()
  1029. if(UA_ENABLE_ENCRYPTION)
  1030. list(APPEND open62541_enabled_components "Encryption")
  1031. endif()
  1032. if(UA_ENABLE_AMALGAMATION)
  1033. list(APPEND open62541_enabled_components "Amalgamation")
  1034. endif()
  1035. if(UA_ENABLE_HISTORIZING)
  1036. list(APPEND open62541_enabled_components "Historizing")
  1037. endif()
  1038. if(UA_ENABLE_EXPERIMENTAL_HISTORIZING)
  1039. list(APPEND open62541_enabled_components "ExperimentalHistorizing")
  1040. endif()
  1041. if(UA_ENABLE_SUBSCRIPTIONS_EVENTS)
  1042. list(APPEND open62541_enabled_components "Events")
  1043. endif()
  1044. if(UA_MULTITHREADING)
  1045. list(APPEND open62541_enabled_components "Multithreading")
  1046. endif()
  1047. if(UA_ENABLE_DISCOVERY)
  1048. list(APPEND open62541_enabled_components "Discovery")
  1049. endif()
  1050. if(UA_ENABLE_DISCOVERY_MULTICAST)
  1051. list(APPEND open62541_enabled_components "DiscoveryMulticast")
  1052. endif()
  1053. # export library (either static or shared depending on BUILD_SHARED_LIBS)
  1054. install(TARGETS open62541
  1055. EXPORT open62541Targets
  1056. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  1057. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  1058. RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}
  1059. INCLUDES DESTINATION include)
  1060. if(UA_ENABLE_AMALGAMATION)
  1061. # Our default way of installation is the non-amalgamated version.
  1062. # See also https://github.com/open62541/open62541/pull/2292#discussion_r241106424
  1063. install(CODE "MESSAGE(WARNING \"Installation with UA_ENABLE_AMALGAMATION=ON is not recommended.\")")
  1064. endif()
  1065. include(CMakePackageConfigHelpers)
  1066. configure_package_config_file("${CMAKE_CURRENT_SOURCE_DIR}/tools/cmake/open62541Config.cmake.in"
  1067. "${CMAKE_CURRENT_BINARY_DIR}/open62541Config.cmake"
  1068. INSTALL_DESTINATION "${cmake_configfile_install}"
  1069. PATH_VARS open62541_install_tools_dir
  1070. open62541_install_nodeset_dir
  1071. open62541_enabled_components
  1072. )
  1073. set(open62541_VERSION)
  1074. get_target_property(open62541_VERSION open62541 VERSION)
  1075. write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/open62541ConfigVersion.cmake"
  1076. VERSION ${open62541_VERSION}
  1077. COMPATIBILITY AnyNewerVersion)
  1078. install(EXPORT open62541Targets
  1079. FILE open62541Targets.cmake
  1080. DESTINATION "${cmake_configfile_install}"
  1081. NAMESPACE open62541::)
  1082. export(
  1083. TARGETS open62541
  1084. NAMESPACE open62541::
  1085. FILE ${CMAKE_CURRENT_BINARY_DIR}/open62541Targets.cmake
  1086. )
  1087. configure_file("${CMAKE_CURRENT_SOURCE_DIR}/tools/cmake/macros_public.cmake" "${CMAKE_CURRENT_BINARY_DIR}/open62541Macros.cmake" COPYONLY)
  1088. install(FILES "${CMAKE_CURRENT_BINARY_DIR}/open62541Config.cmake"
  1089. "${CMAKE_CURRENT_BINARY_DIR}/open62541ConfigVersion.cmake"
  1090. "${CMAKE_CURRENT_BINARY_DIR}/open62541Macros.cmake"
  1091. DESTINATION "${cmake_configfile_install}")
  1092. if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
  1093. install(FILES "${PROJECT_BINARY_DIR}/src_generated/open62541.pc"
  1094. DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
  1095. endif()
  1096. set(UA_install_tools_dirs "tools/certs"
  1097. "tools/nodeset_compiler"
  1098. "tools/schema"
  1099. "deps/ua-nodeset")
  1100. set(UA_install_tools_files "tools/generate_datatypes.py"
  1101. "tools/generate_nodeid_header.py"
  1102. "tools/generate_statuscode_descriptions.py")
  1103. install(DIRECTORY ${UA_install_tools_dirs}
  1104. DESTINATION ${open62541_install_tools_dir}
  1105. USE_SOURCE_PERMISSIONS
  1106. FILES_MATCHING
  1107. PATTERN "*"
  1108. PATTERN "*.pyc" EXCLUDE
  1109. )
  1110. install(FILES ${UA_install_tools_files} DESTINATION ${open62541_install_tools_dir})
  1111. if(NOT UA_ENABLE_AMALGAMATION)
  1112. # Recreate the include folder structure from the source also in /usr/lib/include/open62541
  1113. set(FILES_TO_INSTALL
  1114. ${exported_headers}
  1115. ${default_plugin_headers}
  1116. ${ua_architecture_headers})
  1117. set(BASE_PATH_MAIN "${PROJECT_SOURCE_DIR}/include/open62541")
  1118. set(BASE_PATH_PLUGINS "${PROJECT_SOURCE_DIR}/plugins/include/open62541")
  1119. set(BASE_PATH_ARCH "${PROJECT_SOURCE_DIR}/arch")
  1120. set(BASE_PATH_GENERATED "${PROJECT_BINARY_DIR}/src_generated/open62541")
  1121. set(BASE_PATH_DEPS "${PROJECT_SOURCE_DIR}/deps")
  1122. foreach ( file ${FILES_TO_INSTALL} )
  1123. # Construct a relative path by replacing any occurence of the absolute path
  1124. set(full_path ${file})
  1125. string(REPLACE ${BASE_PATH_MAIN} "" file ${file})
  1126. string(REPLACE ${BASE_PATH_PLUGINS} "" file ${file})
  1127. string(REPLACE ${BASE_PATH_ARCH} "" file ${file})
  1128. string(REPLACE ${BASE_PATH_GENERATED} "" file ${file})
  1129. string(REPLACE ${BASE_PATH_DEPS} "" file ${file})
  1130. get_filename_component( dir ${file} DIRECTORY )
  1131. if(${full_path} MATCHES "deps")
  1132. install( FILES ${full_path} DESTINATION include${dir} )
  1133. else()
  1134. install( FILES ${full_path} DESTINATION include/open62541${dir} )
  1135. endif()
  1136. endforeach()
  1137. else()
  1138. # Export amalgamated header open62541.h which is generated due to build of
  1139. # open62541-object
  1140. install(FILES ${PROJECT_BINARY_DIR}/open62541.h DESTINATION include)
  1141. endif()
  1142. add_subdirectory(tools/packaging)
  1143. ##################################
  1144. # Visual studio solution folders #
  1145. ##################################
  1146. set_property(GLOBAL PROPERTY USE_FOLDERS ON)
  1147. set_property(GLOBAL PROPERTY PREDEFINED_TARGETS_FOLDER "_CmakePredifinedTargets")
  1148. set_target_properties(open62541 PROPERTIES FOLDER "open62541/lib")
  1149. set_target_properties(open62541-object PROPERTIES FOLDER "open62541/lib")
  1150. if (UA_ENABLE_AMALGAMATION)
  1151. set_target_properties(open62541-amalgamation-header PROPERTIES FOLDER "open62541/lib")
  1152. set_target_properties(open62541-amalgamation-source PROPERTIES FOLDER "open62541/lib")
  1153. endif()
  1154. set_target_properties(open62541-generator-namespace PROPERTIES FOLDER "open62541/generators")
  1155. set_target_properties(open62541-generator-statuscode PROPERTIES FOLDER "open62541/generators")
  1156. set_target_properties(open62541-generator-transport PROPERTIES FOLDER "open62541/generators")
  1157. set_target_properties(open62541-generator-types PROPERTIES FOLDER "open62541/generators")