CMakeLists.txt 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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. include(macros)
  9. #############################
  10. # Compiled binaries folders #
  11. #############################
  12. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  13. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  14. set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  15. ###########
  16. # Version #
  17. ###########
  18. set(OPEN62541_VER_MAJOR 0)
  19. set(OPEN62541_VER_MINOR 4)
  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. # If a relative path is specified, it is treated as relative to the $<INSTALL_PREFIX>
  23. set(LIB_INSTALL_DIR lib CACHE PATH "Installation path of libraries")
  24. # Set OPEN62541_VER_COMMIT
  25. if(GIT_FOUND)
  26. execute_process(COMMAND ${GIT_EXECUTABLE} describe --always --tags
  27. RESULT_VARIABLE res_var OUTPUT_VARIABLE GIT_COM_ID WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
  28. if(${res_var} EQUAL 0 AND NOT OPEN62541_VER_COMMIT STREQUAL "")
  29. string(REPLACE "\n" "" OPEN62541_VER_COMMIT ${GIT_COM_ID} )
  30. endif()
  31. endif()
  32. if(NOT OPEN62541_VER_COMMIT OR OPEN62541_VER_COMMIT STREQUAL "")
  33. set(OPEN62541_VER_COMMIT "undefined")
  34. endif()
  35. #################
  36. # Build Options #
  37. #################
  38. # Set default build type.
  39. if(NOT CMAKE_BUILD_TYPE)
  40. message(STATUS "CMAKE_BUILD_TYPE not given; setting to 'Debug'")
  41. set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build" FORCE)
  42. endif()
  43. option(UA_ENABLE_AMALGAMATION "Concatenate the library to a single file open62541.h/.c" OFF)
  44. set(UA_AMALGAMATION_ARCHITECTURES "" CACHE STRING "List of architectures to include in amalgamation")
  45. mark_as_advanced(UA_AMALGAMATION_ARCHITECTURES)
  46. # Platform. This is at the beginning in case the architecture changes some UA options
  47. set(UA_ARCHITECTURE "None" CACHE STRING "Architecture to build open62541 on")
  48. if(UA_ENABLE_AMALGAMATION)
  49. if(NOT "${UA_ARCHITECTURE}" STREQUAL "None")
  50. set(UA_AMALGAMATION_ARCHITECTURES "${UA_ARCHITECTURE}")
  51. else()
  52. if("${UA_AMALGAMATION_ARCHITECTURES}" STREQUAL "")
  53. # select some default architectures which should be included
  54. set(UA_AMALGAMATION_ARCHITECTURES "win32;posix")
  55. endif()
  56. endif()
  57. message(STATUS "Architectures included in amalgamation: ${UA_AMALGAMATION_ARCHITECTURES}")
  58. endif()
  59. if("${UA_ARCHITECTURE}" STREQUAL "None")
  60. if(UNIX)
  61. set(UA_ARCHITECTURE "posix" CACHE STRING "" FORCE)
  62. elseif(WIN32)
  63. set(UA_ARCHITECTURE "win32" CACHE STRING "" FORCE)
  64. endif(UNIX)
  65. endif()
  66. message(STATUS "The selected architecture is: ${UA_ARCHITECTURE}")
  67. string(TOUPPER ${UA_ARCHITECTURE} UA_ARCHITECTURE_UPPER)
  68. add_definitions(-DUA_ARCHITECTURE_${UA_ARCHITECTURE_UPPER})
  69. add_subdirectory(arch)
  70. GET_PROPERTY(architectures GLOBAL PROPERTY UA_ARCHITECTURES)
  71. list(SORT architectures)
  72. set_property(CACHE UA_ARCHITECTURE PROPERTY STRINGS None ${architectures})
  73. GET_PROPERTY(ua_directories_to_include GLOBAL PROPERTY UA_INCLUDE_DIRECTORIES)
  74. include_directories(${ua_directories_to_include})
  75. GET_PROPERTY(ua_architecture_headers GLOBAL PROPERTY UA_ARCHITECTURE_HEADERS)
  76. GET_PROPERTY(ua_architecture_headers_beginning GLOBAL PROPERTY UA_ARCHITECTURE_HEADERS_BEGINNING)
  77. GET_PROPERTY(ua_architecture_sources GLOBAL PROPERTY UA_ARCHITECTURE_SOURCES)
  78. set(ua_architecture_sources ${ua_architecture_sources}
  79. ${PROJECT_SOURCE_DIR}/arch/ua_network_tcp.c
  80. )
  81. set(ua_architecture_headers ${ua_architecture_headers}
  82. ${PROJECT_SOURCE_DIR}/arch/ua_network_tcp.h
  83. ${PROJECT_SOURCE_DIR}/arch/ua_architecture_functions.h
  84. )
  85. if(${UA_ARCHITECTURE} STREQUAL "None")
  86. message(FATAL_ERROR "No architecture was selected. Please select the architecture of your target platform")
  87. endif(${UA_ARCHITECTURE} STREQUAL "None")
  88. # Create a list of ifdefs for all the architectures.
  89. # This is needed to enable a default architecture if none is selected through gcc compiler def.
  90. # Especially if someone is using the amalgamated file on Linux/Windows he should not need to define an architecture.
  91. set(UA_ARCHITECTURES_NODEF "1 ") #to make it easier to append later the && symbol
  92. foreach(arch_ ${architectures})
  93. string(TOUPPER ${arch_} UA_ARCHITECTURE_UPPER_)
  94. set(UA_ARCHITECTURES_NODEF "${UA_ARCHITECTURES_NODEF} && !defined(UA_ARCHITECTURE_${UA_ARCHITECTURE_UPPER_})")
  95. endforeach(arch_ ${architectures})
  96. # Options
  97. set(UA_LOGLEVEL 300 CACHE STRING "Level at which logs shall be reported")
  98. option(UA_ENABLE_HISTORIZING "Enable server to provide historical access." ON)
  99. option(UA_ENABLE_METHODCALLS "Enable the Method service set" ON)
  100. option(UA_ENABLE_NODEMANAGEMENT "Enable dynamic addition and removal of nodes at runtime" ON)
  101. option(UA_ENABLE_SUBSCRIPTIONS "Enable subscriptions support." ON)
  102. option(UA_ENABLE_SUBSCRIPTIONS_EVENTS "Enable the use of events. (EXPERIMENTAL)" OFF)
  103. option(UA_ENABLE_DISCOVERY "Enable Discovery Service (LDS)" ON)
  104. option(UA_ENABLE_DISCOVERY_MULTICAST "Enable Discovery Service with multicast support (LDS-ME)" OFF)
  105. option(UA_ENABLE_QUERY "Enable query support." OFF)
  106. option(UA_ENABLE_COVERAGE "Enable gcov coverage" OFF)
  107. option(UA_ENABLE_ENCRYPTION "Enable encryption support (uses mbedTLS)" OFF)
  108. option(BUILD_SHARED_LIBS "Enable building of shared libraries (dll/so)" OFF)
  109. # Namespace Zero
  110. set(UA_NAMESPACE_ZERO "REDUCED" CACHE STRING "Completeness of the generated namespace zero (minimal/reduced/full)")
  111. SET_PROPERTY(CACHE UA_NAMESPACE_ZERO PROPERTY STRINGS "MINIMAL" "REDUCED" "FULL")
  112. if(UA_NAMESPACE_ZERO STREQUAL "MINIMAL")
  113. set(UA_GENERATED_NAMESPACE_ZERO OFF)
  114. else()
  115. set(UA_GENERATED_NAMESPACE_ZERO ON)
  116. endif()
  117. if(MSVC AND UA_NAMESPACE_ZERO STREQUAL "FULL")
  118. # For the full NS0 we need a stack size of 8MB (as it is default on linux)
  119. # See https://github.com/open62541/open62541/issues/1326
  120. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:8000000")
  121. endif()
  122. # It should not be possible to enable events without enabling subscriptions and full ns0
  123. if((UA_ENABLE_SUBSCRIPTIONS_EVENTS) AND (NOT (UA_ENABLE_SUBSCRIPTIONS AND UA_NAMESPACE_ZERO STREQUAL "FULL")))
  124. message(FATAL_ERROR "Unable to enable events without UA_ENABLE_SUBSCRIPTIONS and full namespace 0")
  125. endif()
  126. if(UA_ENABLE_COVERAGE)
  127. set(CMAKE_BUILD_TYPE DEBUG)
  128. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
  129. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage -lgcov")
  130. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
  131. endif()
  132. if(UA_BUILD_FUZZING OR UA_BUILD_OSS_FUZZ OR UA_BUILD_FUZZING_CORPUS)
  133. # Force enable options not passed in the build script, to also fuzzy-test this code
  134. set(UA_ENABLE_DISCOVERY ON CACHE STRING "" FORCE)
  135. set(UA_ENABLE_DISCOVERY_MULTICAST ON CACHE STRING "" FORCE)
  136. set(UA_ENABLE_ENCRYPTION ON CACHE STRING "" FORCE)
  137. endif()
  138. if(UA_ENABLE_DISCOVERY_MULTICAST AND NOT UA_ENABLE_DISCOVERY)
  139. MESSAGE(WARNING "UA_ENABLE_DISCOVERY_MULTICAST is enabled, but not UA_ENABLE_DISCOVERY. UA_ENABLE_DISCOVERY_MULTICAST will be set to OFF")
  140. SET(UA_ENABLE_DISCOVERY_MULTICAST OFF CACHE BOOL "Enable Discovery Service with multicast support (LDS-ME)" FORCE)
  141. endif()
  142. # Advanced options
  143. option(UA_ENABLE_MULTITHREADING "Enable multithreading (EXPERIMENTAL)" OFF)
  144. mark_as_advanced(UA_ENABLE_MULTITHREADING)
  145. option(UA_ENABLE_IMMUTABLE_NODES "Nodes in the information model are not edited but copied and replaced" OFF)
  146. mark_as_advanced(UA_ENABLE_IMMUTABLE_NODES)
  147. if(UA_ENABLE_MULTITHREADING)
  148. set(UA_ENABLE_IMMUTABLE_NODES ON)
  149. endif()
  150. option(UA_ENABLE_PUBSUB "Enable publish/subscribe" OFF)
  151. mark_as_advanced(UA_ENABLE_PUBSUB)
  152. option(UA_ENABLE_PUBSUB_ETH_UADP "Enable publish/subscribe UADP over Ethernet" OFF)
  153. mark_as_advanced(UA_ENABLE_PUBSUB_ETH_UADP)
  154. if(UA_ENABLE_PUBSUB_ETH_UADP)
  155. if (NOT CMAKE_SYSTEM MATCHES "Linux")
  156. message(FATAL_ERROR "UADP over Ethernet is only available on Linux.")
  157. endif()
  158. endif()
  159. option(UA_ENABLE_PUBSUB_DELTAFRAMES "Enable sending of delta frames with only the changes" OFF)
  160. mark_as_advanced(UA_ENABLE_PUBSUB_DELTAFRAMES)
  161. option(UA_ENABLE_PUBSUB_INFORMATIONMODEL "Enable PubSub information model twin" OFF)
  162. mark_as_advanced(UA_ENABLE_PUBSUB_INFORMATIONMODEL)
  163. option(UA_ENABLE_PUBSUB_INFORMATIONMODEL_METHODS "Enable PubSub informationmodel methods" OFF)
  164. mark_as_advanced(UA_ENABLE_PUBSUB_INFORMATIONMODEL_METHODS)
  165. if(UA_ENABLE_PUBSUB_INFORMATIONMODEL_METHODS)
  166. if(NOT UA_ENABLE_PUBSUB_INFORMATIONMODEL)
  167. message(FATAL_ERROR "PubSub information model methods cannot be used with disabled PubSub information model.")
  168. endif()
  169. endif()
  170. if(UA_ENABLE_PUBSUB_INFORMATIONMODEL)
  171. if(NOT UA_ENABLE_PUBSUB)
  172. message(FATAL_ERROR "PubSub information model representation cannot be used with disabled PubSub function.")
  173. endif()
  174. endif()
  175. option(UA_ENABLE_STATUSCODE_DESCRIPTIONS "Enable conversion of StatusCode to human-readable error message" ON)
  176. mark_as_advanced(UA_ENABLE_STATUSCODE_DESCRIPTIONS)
  177. option(UA_ENABLE_TYPENAMES "Add the type and member names to the UA_DataType structure" ON)
  178. mark_as_advanced(UA_ENABLE_TYPENAMES)
  179. option(UA_ENABLE_NODESET_COMPILER_DESCRIPTIONS "Set node description attribute for nodeset compiler generated nodes" ON)
  180. mark_as_advanced(UA_ENABLE_NODESET_COMPILER_DESCRIPTIONS)
  181. option(UA_ENABLE_DETERMINISTIC_RNG "Do not seed the random number generator (e.g. for unit tests)." OFF)
  182. mark_as_advanced(UA_ENABLE_DETERMINISTIC_RNG)
  183. option(UA_ENABLE_VALGRIND_INTERACTIVE "Enable dumping valgrind every iteration. CAUTION! SLOWDOWN!" OFF)
  184. mark_as_advanced(UA_ENABLE_VALGRIND_INTERACTIVE)
  185. option(UA_MSVC_FORCE_STATIC_CRT "Force linking with the static C-runtime library when compiling to static library with MSVC" ON)
  186. mark_as_advanced(UA_MSVC_FORCE_STATIC_CRT)
  187. option(UA_FILE_NS0 "Override the NodeSet xml file used to generate namespace zero")
  188. mark_as_advanced(UA_FILE_NS0)
  189. # Semaphores/file system may not be available on embedded devices. It can be
  190. # disabled with the following option
  191. option(UA_ENABLE_DISCOVERY_SEMAPHORE "Enable Discovery Semaphore support" ON)
  192. mark_as_advanced(UA_ENABLE_DISCOVERY_SEMAPHORE)
  193. option(UA_ENABLE_UNIT_TESTS_MEMCHECK "Use Valgrind (Linux) or DrMemory (Windows) to detect memory leaks when running the unit tests" OFF)
  194. mark_as_advanced(UA_ENABLE_UNIT_TESTS_MEMCHECK)
  195. option(UA_ENABLE_NONSTANDARD_UDP "Enable udp extension (non-standard)" OFF)
  196. mark_as_advanced(UA_ENABLE_NONSTANDARD_UDP)
  197. option(UA_ENABLE_UNIT_TEST_FAILURE_HOOKS
  198. "Add hooks to force failure modes for additional unit tests. Not for production use!" OFF)
  199. mark_as_advanced(UA_ENABLE_UNIT_TEST_FAILURE_HOOKS)
  200. set(UA_VALGRIND_INTERACTIVE_INTERVAL 1000 CACHE STRING "The number of iterations to wait before creating the next dump")
  201. mark_as_advanced(UA_VALGRIND_INTERACTIVE_INTERVAL)
  202. # Build options for debugging
  203. option(UA_DEBUG "Enable assertions and additional functionality that should not be included in release builds" OFF)
  204. mark_as_advanced(UA_DEBUG)
  205. if(CMAKE_BUILD_TYPE STREQUAL "Debug")
  206. set(UA_DEBUG ON)
  207. endif()
  208. option(UA_DEBUG_DUMP_PKGS "Dump every package received by the server as hexdump format" OFF)
  209. mark_as_advanced(UA_DEBUG_DUMP_PKGS)
  210. # Build Targets
  211. option(UA_BUILD_EXAMPLES "Build example servers and clients" OFF)
  212. option(UA_BUILD_UNIT_TESTS "Build the unit tests" OFF)
  213. option(UA_BUILD_FUZZING "Build the fuzzing executables" OFF)
  214. mark_as_advanced(UA_BUILD_FUZZING)
  215. if(UA_BUILD_FUZZING)
  216. # oss-fuzz already defines this by default
  217. add_definitions(-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
  218. endif()
  219. if(UA_ENABLE_HISTORIZING)
  220. set(historizing_exported_headers
  221. ${PROJECT_SOURCE_DIR}/include/ua_plugin_historydatabase.h
  222. ${PROJECT_SOURCE_DIR}/plugins/historydata/ua_plugin_history_data_backend.h
  223. ${PROJECT_SOURCE_DIR}/plugins/historydata/ua_plugin_history_data_gathering.h
  224. )
  225. set(historizing_default_plugin_headers
  226. ${PROJECT_SOURCE_DIR}/plugins/historydata/ua_historydatabackend_memory.h
  227. ${PROJECT_SOURCE_DIR}/plugins/historydata/ua_historydatagathering_default.h
  228. ${PROJECT_SOURCE_DIR}/plugins/historydata/ua_historydatabase_default.h
  229. )
  230. set(historizing_default_plugin_sources
  231. ${PROJECT_SOURCE_DIR}/plugins/historydata/ua_historydatabackend_memory.c
  232. ${PROJECT_SOURCE_DIR}/plugins/historydata/ua_historydatagathering_default.c
  233. ${PROJECT_SOURCE_DIR}/plugins/historydata/ua_historydatabase_default.c
  234. )
  235. endif()
  236. option(UA_BUILD_FUZZING_CORPUS "Build the fuzzing corpus" OFF)
  237. mark_as_advanced(UA_BUILD_FUZZING_CORPUS)
  238. if(UA_BUILD_FUZZING_CORPUS)
  239. add_definitions(-DUA_DEBUG_DUMP_PKGS_FILE)
  240. set(UA_ENABLE_TYPENAMES ON CACHE STRING "" FORCE)
  241. set(UA_DEBUG_DUMP_PKGS ON CACHE STRING "" FORCE)
  242. endif()
  243. option(UA_BUILD_OSS_FUZZ "Special build switch used in oss-fuzz" OFF)
  244. mark_as_advanced(UA_BUILD_OSS_FUZZ)
  245. # Advanced Build Targets
  246. option(UA_BUILD_SELFSIGNED_CERTIFICATE "Generate self-signed certificate" OFF)
  247. mark_as_advanced(UA_BUILD_SELFSIGNED_CERTIFICATE)
  248. # Building shared libs (dll, so). This option is written into ua_config.h.
  249. set(UA_DYNAMIC_LINKING OFF)
  250. if(BUILD_SHARED_LIBS)
  251. set(UA_DYNAMIC_LINKING ON)
  252. if (UA_ENABLE_DISCOVERY_MULTICAST)
  253. set(MDNSD_DYNAMIC_LINKING ON)
  254. endif()
  255. endif()
  256. # Warn if experimental features are enabled
  257. if(UA_ENABLE_SUBSCRIPTIONS_EVENTS)
  258. MESSAGE(WARNING "UA_ENABLE_SUBSCRIPTIONS_EVENTS is enabled. The feature is under development and marked as EXPERIMENTAL")
  259. endif()
  260. if(UA_ENABLE_MULTITHREADING)
  261. MESSAGE(WARNING "UA_ENABLE_MULTITHREADING is enabled. The feature is under development and marked as EXPERIMENTAL")
  262. endif()
  263. ######################
  264. # External Libraries #
  265. ######################
  266. list(APPEND open62541_LIBRARIES "")
  267. # Force compilation with as C++
  268. option(UA_COMPILE_AS_CXX "Force compilation with a C++ compiler" OFF)
  269. mark_as_advanced(UA_COMPILE_AS_CXX)
  270. if (UA_COMPILE_AS_CXX)
  271. # We need the UINT32_C define
  272. add_definitions(-D__STDC_CONSTANT_MACROS)
  273. endif()
  274. if(UA_ENABLE_ENCRYPTION)
  275. # The recommended way is to install mbedtls via the OS package manager. If
  276. # that is not possible, manually compile mbedTLS and set the cmake variables
  277. # defined in /tools/cmake/FindMbedTLS.cmake.
  278. find_package(MbedTLS REQUIRED)
  279. list(APPEND open62541_LIBRARIES ${MBEDTLS_LIBRARIES})
  280. endif()
  281. #####################
  282. # Compiler Settings #
  283. #####################
  284. if(NOT UA_COMPILE_AS_CXX AND (CMAKE_COMPILER_IS_GNUCC OR "x${CMAKE_C_COMPILER_ID}" STREQUAL "xClang"))
  285. # Compiler
  286. add_definitions(-std=c99 -pipe
  287. -Wall -Wextra -Werror -Wpedantic
  288. -Wno-static-in-inline # clang doesn't like the use of static inline methods inside static inline methods
  289. -Wno-overlength-strings # may happen in the nodeset compiler when complex values are directly encoded
  290. -Wno-unused-parameter # some methods may require unused arguments to cast to a method pointer
  291. -Wmissing-prototypes -Wstrict-prototypes -Wredundant-decls
  292. -Wformat -Wformat-security -Wformat-nonliteral
  293. -Wuninitialized -Winit-self
  294. -Wcast-qual
  295. -Wstrict-overflow
  296. -Wnested-externs
  297. -Wmultichar
  298. -Wundef
  299. -Wc++-compat)
  300. if(UA_ENABLE_AMALGAMATION)
  301. add_definitions(-Wno-unused-function)
  302. endif()
  303. # Linker
  304. set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "") # cmake sets -rdynamic by default
  305. # Debug
  306. if(CMAKE_BUILD_TYPE STREQUAL "Debug")
  307. # add_definitions(-fsanitize=address)
  308. # list(APPEND open62541_LIBRARIES asan)
  309. # add_definitions(-fsanitize=undefined)
  310. # list(APPEND open62541_LIBRARIES ubsan)
  311. endif()
  312. # Strip release builds
  313. if(CMAKE_BUILD_TYPE STREQUAL "MinSizeRel" OR CMAKE_BUILD_TYPE STREQUAL "Release")
  314. add_definitions(-ffunction-sections -fdata-sections -fno-stack-protector -fno-unwind-tables
  315. -fno-asynchronous-unwind-tables -fno-math-errno -fmerge-all-constants -fno-ident)
  316. if(NOT OS9)
  317. set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -s")
  318. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -s")
  319. endif()
  320. if(APPLE)
  321. set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,-dead_strip")
  322. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-dead_strip")
  323. else()
  324. set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,--gc-sections")
  325. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--gc-sections")
  326. endif()
  327. if(NOT WIN32 AND NOT CYGWIN AND NOT APPLE)
  328. # these settings reduce the binary size by ~2kb
  329. set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,-z,norelro -Wl,--hash-style=gnu -Wl,--build-id=none")
  330. endif()
  331. endif()
  332. endif()
  333. if(APPLE)
  334. set(CMAKE_MACOSX_RPATH 1)
  335. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_DARWIN_C_SOURCE=1")
  336. endif()
  337. if(MSVC)
  338. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W3 /WX /w44996") # Compiler warnings, error on warning
  339. if(UA_MSVC_FORCE_STATIC_CRT AND NOT BUILD_SHARED_LIBS)
  340. set(CompilerFlags CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE CMAKE_C_FLAGS
  341. CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE)
  342. foreach(CompilerFlag ${CompilerFlags})
  343. string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}")
  344. endforeach()
  345. endif()
  346. endif()
  347. if(UA_BUILD_FUZZING OR UA_BUILD_OSS_FUZZ)
  348. add_definitions(-DUA_malloc=UA_memoryManager_malloc)
  349. add_definitions(-DUA_free=UA_memoryManager_free)
  350. add_definitions(-DUA_calloc=UA_memoryManager_calloc)
  351. add_definitions(-DUA_realloc=UA_memoryManager_realloc)
  352. endif()
  353. #########################
  354. # Generate Main Library #
  355. #########################
  356. file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/src_generated")
  357. configure_file("include/ua_config.h.in" "${PROJECT_BINARY_DIR}/src_generated/ua_config.h")
  358. configure_file("include/open62541.pc.in" "${PROJECT_BINARY_DIR}/src_generated/open62541.pc" @ONLY)
  359. if(UA_ENABLE_DISCOVERY_MULTICAST)
  360. set(MDNSD_LOGLEVEL 300 CACHE STRING "Level at which logs shall be reported" FORCE)
  361. configure_file("deps/mdnsd/libmdnsd/mdnsd_config.h.in" "${PROJECT_BINARY_DIR}/src_generated/mdnsd_config.h")
  362. endif()
  363. if(UA_ENABLE_DISCOVERY)
  364. include_directories(${PROJECT_SOURCE_DIR}/src/client)
  365. endif()
  366. include_directories(${PROJECT_SOURCE_DIR}/include
  367. ${PROJECT_SOURCE_DIR}/plugins
  368. ${PROJECT_SOURCE_DIR}/deps
  369. ${PROJECT_SOURCE_DIR}/src/pubsub
  370. ${PROJECT_BINARY_DIR}
  371. ${PROJECT_BINARY_DIR}/src_generated
  372. ${MBEDTLS_INCLUDE_DIRS})
  373. set(exported_headers ${exported_headers}
  374. ${PROJECT_BINARY_DIR}/src_generated/ua_config.h
  375. ${PROJECT_SOURCE_DIR}/arch/ua_architecture_base.h
  376. ${ua_architecture_headers_beginning}
  377. )
  378. if(NOT "${UA_AMALGAMATION_ARCHITECTURES}" STREQUAL "")
  379. foreach(arch ${UA_AMALGAMATION_ARCHITECTURES})
  380. list(APPEND exported_headers ${PROJECT_SOURCE_DIR}/arch/${arch}/ua_architecture.h)
  381. endforeach()
  382. else()
  383. list(APPEND exported_headers ${PROJECT_SOURCE_DIR}/arch/${UA_ARCHITECTURE}/ua_architecture.h)
  384. endif()
  385. list(APPEND exported_headers ${PROJECT_SOURCE_DIR}/arch/ua_architecture_definitions.h)
  386. set(exported_headers ${exported_headers}
  387. ${PROJECT_SOURCE_DIR}/deps/ms_stdint.h
  388. ${PROJECT_BINARY_DIR}/src_generated/ua_statuscodes.h
  389. ${PROJECT_BINARY_DIR}/src_generated/ua_nodeids.h
  390. ${PROJECT_SOURCE_DIR}/include/ua_constants.h
  391. ${PROJECT_SOURCE_DIR}/include/ua_types.h
  392. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.h
  393. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated_handling.h
  394. ${PROJECT_SOURCE_DIR}/include/ua_util.h
  395. ${PROJECT_SOURCE_DIR}/include/ua_server.h
  396. ${historizing_exported_headers}
  397. ${PROJECT_SOURCE_DIR}/include/ua_plugin_log.h
  398. ${PROJECT_SOURCE_DIR}/include/ua_plugin_network.h
  399. ${PROJECT_SOURCE_DIR}/include/ua_plugin_access_control.h
  400. ${PROJECT_SOURCE_DIR}/include/ua_plugin_pki.h
  401. ${PROJECT_SOURCE_DIR}/include/ua_plugin_securitypolicy.h
  402. ${PROJECT_SOURCE_DIR}/include/ua_server_pubsub.h
  403. ${PROJECT_SOURCE_DIR}/include/ua_plugin_pubsub.h
  404. ${PROJECT_SOURCE_DIR}/include/ua_plugin_nodestore.h
  405. ${PROJECT_SOURCE_DIR}/include/ua_server_config.h
  406. ${PROJECT_SOURCE_DIR}/include/ua_client_config.h
  407. ${PROJECT_SOURCE_DIR}/include/ua_client.h
  408. ${PROJECT_SOURCE_DIR}/include/ua_client_highlevel.h
  409. ${PROJECT_SOURCE_DIR}/include/ua_client_subscriptions.h
  410. ${PROJECT_SOURCE_DIR}/include/ua_client_highlevel_async.h)
  411. set(internal_headers ${PROJECT_SOURCE_DIR}/deps/queue.h
  412. ${PROJECT_SOURCE_DIR}/deps/pcg_basic.h
  413. ${PROJECT_SOURCE_DIR}/deps/libc_time.h
  414. ${PROJECT_SOURCE_DIR}/deps/base64.h
  415. ${PROJECT_SOURCE_DIR}/src/ua_util_internal.h
  416. ${PROJECT_SOURCE_DIR}/src/ua_types_encoding_binary.h
  417. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated_encoding_binary.h
  418. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.h
  419. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated_handling.h
  420. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated_encoding_binary.h
  421. ${PROJECT_SOURCE_DIR}/src/ua_connection_internal.h
  422. ${PROJECT_SOURCE_DIR}/src/ua_securechannel.h
  423. ${PROJECT_SOURCE_DIR}/src/ua_workqueue.h
  424. ${PROJECT_SOURCE_DIR}/src/ua_timer.h
  425. ${PROJECT_SOURCE_DIR}/src/server/ua_session.h
  426. ${PROJECT_SOURCE_DIR}/src/server/ua_subscription.h
  427. ${PROJECT_SOURCE_DIR}/src/server/ua_session_manager.h
  428. ${PROJECT_SOURCE_DIR}/src/server/ua_securechannel_manager.h
  429. ${PROJECT_SOURCE_DIR}/src/pubsub/ua_pubsub_networkmessage.h
  430. ${PROJECT_SOURCE_DIR}/src/pubsub/ua_pubsub.h
  431. ${PROJECT_SOURCE_DIR}/src/pubsub/ua_pubsub_manager.h
  432. ${PROJECT_SOURCE_DIR}/src/pubsub/ua_pubsub_ns0.h
  433. ${PROJECT_SOURCE_DIR}/src/server/ua_server_internal.h
  434. ${PROJECT_SOURCE_DIR}/src/server/ua_services.h
  435. ${PROJECT_BINARY_DIR}/src_generated/ua_namespace0.h
  436. ${PROJECT_SOURCE_DIR}/src/client/ua_client_internal.h)
  437. # TODO: make client optional
  438. set(lib_sources ${PROJECT_SOURCE_DIR}/src/ua_types.c
  439. ${PROJECT_SOURCE_DIR}/src/ua_types_encoding_binary.c
  440. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.c
  441. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.c
  442. ${PROJECT_BINARY_DIR}/src_generated/ua_statuscodes.c
  443. ${PROJECT_SOURCE_DIR}/src/ua_util.c
  444. ${PROJECT_SOURCE_DIR}/src/ua_workqueue.c
  445. ${PROJECT_SOURCE_DIR}/src/ua_timer.c
  446. ${PROJECT_SOURCE_DIR}/src/ua_connection.c
  447. ${PROJECT_SOURCE_DIR}/src/ua_securechannel.c
  448. ${PROJECT_SOURCE_DIR}/src/server/ua_session.c
  449. ${PROJECT_SOURCE_DIR}/src/server/ua_nodes.c
  450. ${PROJECT_SOURCE_DIR}/src/server/ua_server.c
  451. ${PROJECT_SOURCE_DIR}/src/server/ua_server_ns0.c
  452. ${PROJECT_BINARY_DIR}/src_generated/ua_namespace0.c
  453. ${PROJECT_SOURCE_DIR}/src/server/ua_server_binary.c
  454. ${PROJECT_SOURCE_DIR}/src/server/ua_server_utils.c
  455. ${PROJECT_SOURCE_DIR}/src/server/ua_server_discovery.c
  456. ${PROJECT_SOURCE_DIR}/src/server/ua_securechannel_manager.c
  457. ${PROJECT_SOURCE_DIR}/src/server/ua_session_manager.c
  458. ${PROJECT_SOURCE_DIR}/src/pubsub/ua_pubsub_networkmessage.c
  459. ${PROJECT_SOURCE_DIR}/src/pubsub/ua_pubsub.c
  460. ${PROJECT_SOURCE_DIR}/src/pubsub/ua_pubsub_manager.c
  461. ${PROJECT_SOURCE_DIR}/src/pubsub/ua_pubsub_ns0.c
  462. # services
  463. ${PROJECT_SOURCE_DIR}/src/server/ua_services_view.c
  464. ${PROJECT_SOURCE_DIR}/src/server/ua_services_call.c
  465. ${PROJECT_SOURCE_DIR}/src/server/ua_services_session.c
  466. ${PROJECT_SOURCE_DIR}/src/server/ua_services_attribute.c
  467. ${PROJECT_SOURCE_DIR}/src/server/ua_services_discovery.c
  468. ${PROJECT_SOURCE_DIR}/src/server/ua_services_subscription.c
  469. ${PROJECT_SOURCE_DIR}/src/server/ua_services_securechannel.c
  470. ${PROJECT_SOURCE_DIR}/src/server/ua_services_nodemanagement.c
  471. ${PROJECT_SOURCE_DIR}/src/server/ua_services_discovery_multicast.c
  472. # client
  473. ${PROJECT_SOURCE_DIR}/src/client/ua_client.c
  474. ${PROJECT_SOURCE_DIR}/src/client/ua_client_connect.c
  475. ${PROJECT_SOURCE_DIR}/src/client/ua_client_connect_async.c
  476. ${PROJECT_SOURCE_DIR}/src/client/ua_client_discovery.c
  477. ${PROJECT_SOURCE_DIR}/src/client/ua_client_highlevel.c
  478. ${PROJECT_SOURCE_DIR}/src/client/ua_client_subscriptions.c
  479. ${PROJECT_SOURCE_DIR}/src/client/ua_client_worker.c
  480. # dependencies
  481. ${PROJECT_SOURCE_DIR}/deps/libc_time.c
  482. ${PROJECT_SOURCE_DIR}/deps/pcg_basic.c
  483. ${PROJECT_SOURCE_DIR}/deps/base64.c)
  484. set(default_plugin_headers ${PROJECT_SOURCE_DIR}/plugins/ua_accesscontrol_default.h
  485. ${historizing_default_plugin_headers}
  486. ${PROJECT_SOURCE_DIR}/plugins/ua_pki_certificate.h
  487. ${PROJECT_SOURCE_DIR}/plugins/ua_log_stdout.h
  488. ${PROJECT_SOURCE_DIR}/plugins/ua_nodestore_default.h
  489. ${PROJECT_SOURCE_DIR}/plugins/ua_config_default.h
  490. ${PROJECT_SOURCE_DIR}/plugins/ua_securitypolicies.h
  491. )
  492. set(default_plugin_sources ${PROJECT_SOURCE_DIR}/plugins/ua_log_stdout.c
  493. ${PROJECT_SOURCE_DIR}/plugins/ua_accesscontrol_default.c
  494. ${historizing_default_plugin_sources}
  495. ${PROJECT_SOURCE_DIR}/plugins/ua_pki_certificate.c
  496. ${PROJECT_SOURCE_DIR}/plugins/ua_nodestore_default.c
  497. ${PROJECT_SOURCE_DIR}/plugins/ua_config_default.c
  498. ${PROJECT_SOURCE_DIR}/plugins/ua_securitypolicy_none.c
  499. )
  500. if(UA_ENABLE_ENCRYPTION)
  501. list(APPEND default_plugin_sources ${PROJECT_SOURCE_DIR}/plugins/ua_securitypolicy_basic128rsa15.c
  502. ${PROJECT_SOURCE_DIR}/plugins/ua_securitypolicy_basic256sha256.c)
  503. endif()
  504. if(UA_ENABLE_DISCOVERY)
  505. list(INSERT internal_headers 5 ${PROJECT_SOURCE_DIR}/src/server/ua_discovery_manager.h)
  506. list(APPEND lib_sources ${PROJECT_SOURCE_DIR}/src/server/ua_discovery_manager.c)
  507. endif()
  508. if(UA_ENABLE_PUBSUB)
  509. list(APPEND default_plugin_headers ${PROJECT_SOURCE_DIR}/plugins/ua_network_pubsub_udp.h)
  510. list(APPEND default_plugin_sources ${PROJECT_SOURCE_DIR}/plugins/ua_network_pubsub_udp.c)
  511. if(UA_ENABLE_PUBSUB_ETH_UADP)
  512. list(APPEND default_plugin_headers ${PROJECT_SOURCE_DIR}/plugins/ua_network_pubsub_ethernet.h)
  513. list(APPEND default_plugin_sources ${PROJECT_SOURCE_DIR}/plugins/ua_network_pubsub_ethernet.c)
  514. endif()
  515. endif()
  516. if(UA_ENABLE_SUBSCRIPTIONS)
  517. list(APPEND lib_sources ${PROJECT_SOURCE_DIR}/src/server/ua_subscription.c
  518. ${PROJECT_SOURCE_DIR}/src/server/ua_subscription_monitoreditem.c
  519. ${PROJECT_SOURCE_DIR}/src/server/ua_subscription_datachange.c)
  520. if(UA_ENABLE_SUBSCRIPTIONS_EVENTS)
  521. list(APPEND lib_sources ${PROJECT_SOURCE_DIR}/src/server/ua_subscription_events.c)
  522. endif()
  523. endif()
  524. if(UA_DEBUG_DUMP_PKGS)
  525. list(APPEND lib_sources ${PROJECT_SOURCE_DIR}/plugins/ua_debug_dump_pkgs.c)
  526. endif()
  527. if(UA_ENABLE_NONSTANDARD_UDP)
  528. list(APPEND exported_headers ${PROJECT_SOURCE_DIR}/arch/ua_network_udp.h)
  529. endif()
  530. if(UA_ENABLE_DISCOVERY_MULTICAST)
  531. # prepend in list, otherwise it complains that winsock2.h has to be included before windows.h
  532. set(internal_headers ${PROJECT_BINARY_DIR}/src_generated/mdnsd_config.h
  533. ${PROJECT_SOURCE_DIR}/deps/mdnsd/libmdnsd/1035.h
  534. ${PROJECT_SOURCE_DIR}/deps/mdnsd/libmdnsd/xht.h
  535. ${PROJECT_SOURCE_DIR}/deps/mdnsd/libmdnsd/sdtxt.h
  536. ${PROJECT_SOURCE_DIR}/deps/mdnsd/libmdnsd/mdnsd.h
  537. ${internal_headers} )
  538. set(lib_sources ${PROJECT_SOURCE_DIR}/src/server/ua_server_discovery_mdns.c
  539. ${PROJECT_SOURCE_DIR}/deps/mdnsd/libmdnsd/1035.c
  540. ${PROJECT_SOURCE_DIR}/deps/mdnsd/libmdnsd/xht.c
  541. ${PROJECT_SOURCE_DIR}/deps/mdnsd/libmdnsd/sdtxt.c
  542. ${PROJECT_SOURCE_DIR}/deps/mdnsd/libmdnsd/mdnsd.c
  543. ${lib_sources})
  544. endif()
  545. if(UA_BUILD_FUZZING OR UA_BUILD_OSS_FUZZ)
  546. set(lib_sources
  547. ${lib_sources}
  548. ${PROJECT_SOURCE_DIR}/tests/fuzz/custom_memory_manager.c)
  549. endif()
  550. #########################
  551. # Generate source files #
  552. #########################
  553. if(UA_NAMESPACE_ZERO STREQUAL "FULL")
  554. if(NOT UA_FILE_NS0)
  555. set(UA_FILE_NS0 ${PROJECT_SOURCE_DIR}/deps/ua-nodeset/Schema/Opc.Ua.NodeSet2.xml)
  556. endif()
  557. set(UA_FILE_NODEIDS ${PROJECT_SOURCE_DIR}/deps/ua-nodeset/Schema/NodeIds.csv)
  558. set(UA_FILE_STATUSCODES ${PROJECT_SOURCE_DIR}/deps/ua-nodeset/Schema/StatusCode.csv)
  559. set(UA_FILE_TYPES_BSD ${PROJECT_SOURCE_DIR}/deps/ua-nodeset/Schema/Opc.Ua.Types.bsd)
  560. else()
  561. if(NOT UA_FILE_NS0)
  562. set(UA_FILE_NS0 ${PROJECT_SOURCE_DIR}/tools/schema/Opc.Ua.NodeSet2.Minimal.xml)
  563. endif()
  564. set(UA_FILE_DATATYPES ${PROJECT_SOURCE_DIR}/tools/schema/datatypes_minimal.txt)
  565. set(UA_FILE_NODEIDS ${PROJECT_SOURCE_DIR}/tools/schema/NodeIds.csv)
  566. set(UA_FILE_STATUSCODES ${PROJECT_SOURCE_DIR}/tools/schema/StatusCode.csv)
  567. set(UA_FILE_TYPES_BSD ${PROJECT_SOURCE_DIR}/tools/schema/Opc.Ua.Types.bsd)
  568. if(UA_ENABLE_METHODCALLS)
  569. list(APPEND UA_FILE_DATATYPES ${PROJECT_SOURCE_DIR}/tools/schema/datatypes_method.txt)
  570. endif()
  571. if(UA_ENABLE_SUBSCRIPTIONS)
  572. list(APPEND UA_FILE_DATATYPES ${PROJECT_SOURCE_DIR}/tools/schema/datatypes_subscriptions.txt)
  573. endif()
  574. if(UA_ENABLE_HISTORIZING)
  575. list(APPEND UA_FILE_DATATYPES ${PROJECT_SOURCE_DIR}/tools/schema/datatypes_historizing.txt)
  576. endif()
  577. if(UA_ENABLE_DISCOVERY)
  578. list(APPEND UA_FILE_DATATYPES ${PROJECT_SOURCE_DIR}/tools/schema/datatypes_discovery.txt)
  579. endif()
  580. if(UA_ENABLE_QUERY)
  581. list(APPEND UA_FILE_DATATYPES ${PROJECT_SOURCE_DIR}/tools/schema/datatypes_query.txt)
  582. endif()
  583. if(UA_ENABLE_PUBSUB)
  584. list(APPEND UA_FILE_DATATYPES ${PROJECT_SOURCE_DIR}/tools/schema/datatypes_pubsub.txt)
  585. if(UA_ENABLE_PUBSUB_INFORMATIONMODEL)
  586. set(UA_FILE_NSPUBSUB ${PROJECT_SOURCE_DIR}/tools/schema/Opc.Ua.NodeSet2.PubSubMinimal.xml)
  587. endif()
  588. endif()
  589. endif()
  590. if(NOT EXISTS "${UA_FILE_NS0}")
  591. message(FATAL_ERROR "File ${UA_FILE_NS0} not found. You probably need to initialize the git submodule for deps/ua-nodeset.")
  592. endif()
  593. # standard-defined data types
  594. ua_generate_datatypes(
  595. BUILTIN
  596. NAME "ua_types"
  597. TARGET_SUFFIX "types"
  598. NAMESPACE_IDX 0
  599. FILE_CSV "${UA_FILE_NODEIDS}"
  600. FILES_BSD "${UA_FILE_TYPES_BSD}"
  601. FILES_SELECTED ${UA_FILE_DATATYPES}
  602. )
  603. # transport data types
  604. ua_generate_datatypes(
  605. NAME "ua_transport"
  606. TARGET_SUFFIX "transport"
  607. NAMESPACE_IDX 1
  608. FILE_CSV "${UA_FILE_NODEIDS}"
  609. FILES_BSD "${UA_FILE_TYPES_BSD}" "${PROJECT_SOURCE_DIR}/tools/schema/Custom.Opc.Ua.Transport.bsd"
  610. FILES_SELECTED "${PROJECT_SOURCE_DIR}/tools/schema/datatypes_transport.txt"
  611. )
  612. # statuscode explanation
  613. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_statuscodes.h
  614. ${PROJECT_BINARY_DIR}/src_generated/ua_statuscodes.c
  615. PRE_BUILD
  616. COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/tools/generate_statuscode_descriptions.py
  617. ${UA_FILE_STATUSCODES} ${PROJECT_BINARY_DIR}/src_generated/ua_statuscodes
  618. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_statuscode_descriptions.py
  619. ${UA_FILE_STATUSCODES})
  620. # Header containing defines for all NodeIds
  621. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_nodeids.h
  622. PRE_BUILD
  623. COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/tools/generate_nodeid_header.py
  624. ${UA_FILE_NODEIDS} ${PROJECT_BINARY_DIR}/src_generated/ua_nodeids NS0
  625. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_nodeid_header.py
  626. ${UA_FILE_NODEIDS})
  627. # we need a custom target to avoid that the generator is called concurrently and
  628. # thus overwriting files while the other thread is compiling
  629. add_custom_target(open62541-generator-statuscode DEPENDS
  630. ${PROJECT_BINARY_DIR}/src_generated/ua_nodeids.h
  631. ${PROJECT_BINARY_DIR}/src_generated/ua_statuscodes.h
  632. ${PROJECT_BINARY_DIR}/src_generated/ua_statuscodes.c)
  633. # single-file release
  634. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/open62541.h
  635. PRE_BUILD
  636. COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tools/amalgamate.py
  637. ${OPEN62541_VER_COMMIT} ${CMAKE_CURRENT_BINARY_DIR}/open62541.h
  638. ${exported_headers} ${default_plugin_headers} ${ua_architecture_headers}
  639. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/amalgamate.py
  640. ${exported_headers} ${default_plugin_headers} ${ua_architecture_headers})
  641. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/open62541.c
  642. PRE_BUILD
  643. COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tools/amalgamate.py
  644. ${OPEN62541_VER_COMMIT} ${CMAKE_CURRENT_BINARY_DIR}/open62541.c
  645. ${internal_headers} ${lib_sources} ${default_plugin_sources} ${ua_architecture_sources}
  646. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/amalgamate.py ${internal_headers}
  647. ${lib_sources} ${default_plugin_sources} ${ua_architecture_sources} )
  648. add_custom_target(open62541-amalgamation-source DEPENDS ${PROJECT_BINARY_DIR}/open62541.c)
  649. add_custom_target(open62541-amalgamation-header DEPENDS ${PROJECT_BINARY_DIR}/open62541.h)
  650. add_dependencies(open62541-amalgamation-header open62541-generator-types)
  651. add_dependencies(open62541-amalgamation-source open62541-generator-types
  652. open62541-generator-transport open62541-generator-statuscode)
  653. if(NOT UA_NODESET_ENCODE_BINARY_SIZE)
  654. set(UA_NODESET_ENCODE_BINARY_SIZE 32000)
  655. endif()
  656. ua_generate_nodeset(
  657. NAME "ns0"
  658. FILE "${UA_FILE_NS0}" "${UA_FILE_NSPUBSUB}"
  659. INTERNAL
  660. IGNORE "${PROJECT_SOURCE_DIR}/tools/nodeset_compiler/NodeID_NS0_Base.txt"
  661. ENCODE_BINARY_SIZE ${UA_NODESET_ENCODE_BINARY_SIZE}
  662. DEPENDS_TARGET "open62541-generator-types"
  663. )
  664. # stack protector needs to be disabled for the huge ns0 file, otherwise it may take many minutes to compile the file.
  665. if(NOT MSVC)
  666. set_source_files_properties(${PROJECT_BINARY_DIR}/src_generated/ua_namespace0.c PROPERTIES COMPILE_FLAGS -fno-stack-protector)
  667. endif()
  668. #####################
  669. # Build the Library #
  670. #####################
  671. assign_source_group(${lib_sources})
  672. assign_source_group(${internal_headers})
  673. assign_source_group(${exported_headers})
  674. assign_source_group(${default_plugin_sources})
  675. assign_source_group(${ua_architecture_sources})
  676. if(UA_ENABLE_AMALGAMATION)
  677. add_library(open62541-object OBJECT ${PROJECT_BINARY_DIR}/open62541.c ${PROJECT_BINARY_DIR}/open62541.h)
  678. target_include_directories(open62541-object PRIVATE ${PROJECT_BINARY_DIR})
  679. # make sure the open62541_amalgamation target builds before so that amalgamation is finished and it is not executed again for open62541-object
  680. # and thus may overwrite the amalgamation result during multiprocessor compilation
  681. # the header is already a dependency of open62541 target itself
  682. add_dependencies(open62541-object
  683. open62541-amalgamation-header
  684. open62541-generator-types
  685. open62541-generator-transport
  686. open62541-generator-statuscode
  687. open62541-amalgamation-source)
  688. add_library(open62541 $<TARGET_OBJECTS:open62541-object>)
  689. if(UA_COMPILE_AS_CXX)
  690. set_source_files_properties(${PROJECT_BINARY_DIR}/open62541.c PROPERTIES LANGUAGE CXX)
  691. endif()
  692. else()
  693. add_definitions(-DUA_NO_AMALGAMATION)
  694. add_library(open62541-object OBJECT ${lib_sources} ${internal_headers} ${exported_headers})
  695. add_dependencies(open62541-object
  696. open62541-amalgamation-header
  697. open62541-generator-types
  698. open62541-generator-transport
  699. open62541-generator-statuscode)
  700. target_include_directories(open62541-object PRIVATE ${PROJECT_SOURCE_DIR}/src)
  701. add_library(open62541-plugins OBJECT ${default_plugin_sources} ${ua_architecture_sources} ${exported_headers})
  702. add_dependencies(open62541-plugins open62541-generator-types open62541-generator-transport)
  703. target_include_directories(open62541-plugins PRIVATE ${PROJECT_SOURCE_DIR}/plugins)
  704. target_include_directories(open62541-plugins PRIVATE ${PROJECT_BINARY_DIR}/src_generated)
  705. target_compile_definitions(open62541-plugins PRIVATE -DUA_DYNAMIC_LINKING_EXPORT)
  706. set_target_properties(open62541-plugins PROPERTIES FOLDER "open62541/lib")
  707. add_library(open62541 $<TARGET_OBJECTS:open62541-object> $<TARGET_OBJECTS:open62541-plugins>)
  708. if(UA_COMPILE_AS_CXX)
  709. set_source_files_properties(${lib_sources} PROPERTIES LANGUAGE CXX)
  710. set_source_files_properties(${default_plugin_sources} ${ua_architecture_sources} PROPERTIES LANGUAGE CXX)
  711. endif()
  712. endif()
  713. add_dependencies(open62541-amalgamation-source open62541-generator-namespace)
  714. add_dependencies(open62541-amalgamation-header open62541-generator-namespace)
  715. if(NOT UA_ENABLE_AMALGAMATION)
  716. add_dependencies(open62541-object open62541-generator-namespace)
  717. endif()
  718. # Export Symbols
  719. target_compile_definitions(open62541-object PRIVATE -DUA_DYNAMIC_LINKING_EXPORT)
  720. target_compile_definitions(open62541 PRIVATE -DUA_DYNAMIC_LINKING_EXPORT)
  721. if (UA_ENABLE_DISCOVERY_MULTICAST)
  722. target_compile_definitions(open62541-object PRIVATE -DMDNSD_DYNAMIC_LINKING_EXPORT)
  723. target_compile_definitions(open62541 PRIVATE -DMDNSD_DYNAMIC_LINKING_EXPORT)
  724. endif()
  725. # Generate properly versioned shared library links on Linux
  726. SET_TARGET_PROPERTIES(open62541 PROPERTIES SOVERSION 0 VERSION "${OPEN62541_VER_MAJOR}.${OPEN62541_VER_MINOR}.${OPEN62541_VER_PATCH}")
  727. ##################################
  728. # Architectures changes #
  729. ##################################
  730. GET_PROPERTY(ua_architecture_add_definitions GLOBAL PROPERTY UA_ARCHITECTURE_ADD_DEFINITIONS)
  731. add_definitions(${ua_architecture_add_definitions})
  732. GET_PROPERTY(ua_architecture_remove_definitions GLOBAL PROPERTY UA_ARCHITECTURE_REMOVE_DEFINITIONS)
  733. if (NOT "${ua_architecture_remove_definitions}" STREQUAL "")
  734. string(REPLACE " " ";" ua_architecture_remove_definitions_list ${ua_architecture_remove_definitions})
  735. remove_definitions(${ua_architecture_remove_definitions_list})
  736. endif(NOT "${ua_architecture_remove_definitions}" STREQUAL "")
  737. GET_PROPERTY(ua_architecture_append_to_library GLOBAL PROPERTY UA_ARCHITECTURE_APPEND_TO_LIBRARY)
  738. list(APPEND open62541_LIBRARIES ${ua_architecture_append_to_library})
  739. target_compile_definitions(open62541 PUBLIC UA_ARCHITECTURE_${UA_ARCHITECTURE_UPPER})
  740. # DLL requires linking to dependencies
  741. target_link_libraries(open62541 ${open62541_LIBRARIES})
  742. ##########################
  743. # Build Selected Targets #
  744. ##########################
  745. # always include, builds with make doc
  746. add_subdirectory(doc)
  747. if(UA_BUILD_EXAMPLES)
  748. add_subdirectory(examples)
  749. endif()
  750. if(UA_BUILD_UNIT_TESTS)
  751. if(UA_ENABLE_AMALGAMATION)
  752. # Cannot compile tests with amalgamation. Amalgamation uses the default plugins, not the testing plugins
  753. message(FATAL_ERROR "Unit tests cannot be generated with source amalgamation enabled")
  754. endif()
  755. enable_testing()
  756. add_subdirectory(tests)
  757. endif()
  758. if(UA_BUILD_FUZZING OR UA_BUILD_OSS_FUZZ OR UA_BUILD_FUZZING_CORPUS)
  759. add_subdirectory(tests/fuzz)
  760. endif()
  761. ############################
  762. # Linting run (clang-tidy) #
  763. ############################
  764. find_package(ClangTools)
  765. add_custom_target(lint ${CLANG_TIDY_PROGRAM}
  766. ${lib_sources}
  767. -checks=cert-*,performance-*,readability-*,-readability-braces-around-statements
  768. -warnings-as-errors=cert-*,performance-*,readability-*,-readability-braces-around-statements
  769. --
  770. -std=c99
  771. -I${PROJECT_SOURCE_DIR}/include
  772. -I${PROJECT_SOURCE_DIR}/plugins
  773. -I${PROJECT_SOURCE_DIR}/deps
  774. -I${PROJECT_SOURCE_DIR}/src
  775. -I${PROJECT_SOURCE_DIR}/src/server
  776. -I${PROJECT_SOURCE_DIR}/src/client
  777. -I${PROJECT_BINARY_DIR}/src_generated
  778. -DUA_NO_AMALGAMATION
  779. DEPENDS ${lib_sources}
  780. COMMENT "Run clang-tidy on the library")
  781. add_dependencies(lint open62541)
  782. add_custom_target(cpplint cpplint
  783. ${lib_sources}
  784. ${internal_headers}
  785. ${default_plugin_headers}
  786. ${default_plugin_sources}
  787. ${ua_architecture_headers}
  788. ${ua_architecture_sources}
  789. DEPENDS ${lib_sources}
  790. ${internal_headers}
  791. ${default_plugin_headers}
  792. ${default_plugin_sources}
  793. ${ua_architecture_headers}
  794. ${ua_architecture_sources}
  795. COMMENT "Run cpplint code style checker on the library")
  796. ##########################
  797. # Installation #
  798. ##########################
  799. # invoke via `make install`
  800. # specify install location with `-DCMAKE_INSTALL_PREFIX=xyz`
  801. # Enable shared library with `-DBUILD_SHARED_LIBS=ON`
  802. set(cmake_configfile_install ${LIB_INSTALL_DIR}/cmake/open62541)
  803. set(target_install_dest_name "${cmake_configfile_install}/open62541Targets.cmake")
  804. set(open62541_tools_dir share/open62541/tools)
  805. set(open62541_deps_dir include/open62541/deps)
  806. # export library (either static or shared depending on BUILD_SHARED_LIBS)
  807. install(TARGETS open62541
  808. EXPORT open62541Targets
  809. LIBRARY DESTINATION ${LIB_INSTALL_DIR}
  810. ARCHIVE DESTINATION ${LIB_INSTALL_DIR}
  811. RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}
  812. INCLUDES DESTINATION include/open62541 ${open62541_deps_dir})
  813. include(CMakePackageConfigHelpers)
  814. configure_package_config_file("${CMAKE_CURRENT_SOURCE_DIR}/tools/cmake/open62541-config.cmake.in"
  815. "${CMAKE_CURRENT_BINARY_DIR}/cmake/open62541-config.cmake"
  816. INSTALL_DESTINATION "${cmake_configfile_install}"
  817. PATH_VARS target_install_dest_name open62541_tools_dir)
  818. set(open62541_VERSION)
  819. get_target_property(open62541_VERSION open62541 VERSION)
  820. write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/open62541ConfigVersion.cmake"
  821. VERSION ${open62541_VERSION}
  822. COMPATIBILITY AnyNewerVersion)
  823. install(EXPORT open62541Targets
  824. FILE open62541Targets.cmake
  825. DESTINATION "${cmake_configfile_install}")
  826. install(FILES "${CMAKE_CURRENT_BINARY_DIR}/cmake/open62541-config.cmake"
  827. "${CMAKE_CURRENT_BINARY_DIR}/open62541ConfigVersion.cmake"
  828. DESTINATION "${cmake_configfile_install}")
  829. if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
  830. install(FILES "${PROJECT_BINARY_DIR}/src_generated/open62541.pc"
  831. DESTINATION ${LIB_INSTALL_DIR}/pkgconfig)
  832. endif()
  833. # export amalgamated header open62541.h which is generated due to build of open62541-object
  834. if(UA_ENABLE_AMALGAMATION)
  835. install(FILES ${PROJECT_BINARY_DIR}/open62541.h DESTINATION include/open62541)
  836. endif()
  837. install(DIRECTORY deps/ DESTINATION ${open62541_deps_dir})
  838. install(DIRECTORY tools/ DESTINATION ${open62541_tools_dir} USE_SOURCE_PERMISSIONS)
  839. ##########################
  840. # Packaging (DEB/RPM) #
  841. ##########################
  842. # invoke via `make package`
  843. set(CPACK_GENERATOR "TGZ;DEB;RPM")
  844. set(CPACK_PACKAGE_VENDOR "open62541 team")
  845. set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "OPC UA implementation")
  846. 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.")
  847. set(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/README.md")
  848. set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")
  849. set(CPACK_PACKAGE_VERSION_MAJOR "0")
  850. set(CPACK_PACKAGE_VERSION_MINOR "2")
  851. set(CPACK_PACKAGE_VERSION_PATCH "0")
  852. set(CPACK_DEBIAN_PACKAGE_MAINTAINER "open62541 team") #required
  853. include(CPack)
  854. ##################################
  855. # Visual studio solution folders #
  856. ##################################
  857. set_property(GLOBAL PROPERTY USE_FOLDERS ON)
  858. set_property(GLOBAL PROPERTY PREDEFINED_TARGETS_FOLDER "_CmakePredifinedTargets")
  859. set_target_properties(open62541 PROPERTIES FOLDER "open62541/lib")
  860. set_target_properties(open62541-object PROPERTIES FOLDER "open62541/lib")
  861. set_target_properties(lint PROPERTIES FOLDER "CodeAnalysis")
  862. set_target_properties(open62541-amalgamation-header PROPERTIES FOLDER "open62541/lib")
  863. set_target_properties(open62541-amalgamation-source PROPERTIES FOLDER "open62541/lib")
  864. set_target_properties(open62541-generator-namespace PROPERTIES FOLDER "open62541/generators")
  865. set_target_properties(open62541-generator-statuscode PROPERTIES FOLDER "open62541/generators")
  866. set_target_properties(open62541-generator-transport PROPERTIES FOLDER "open62541/generators")
  867. set_target_properties(open62541-generator-types PROPERTIES FOLDER "open62541/generators")