CMakeLists.txt 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. cmake_minimum_required(VERSION 2.8.8)
  2. # set(CMAKE_VERBOSE_MAKEFILE ON)
  3. project(open62541 C)
  4. FIND_PACKAGE(PythonInterp REQUIRED)
  5. # Find Python-lxml
  6. execute_process ( COMMAND ${PYTHON_EXECUTABLE} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())" OUTPUT_VARIABLE PYTHON_SITE_PACKAGES OUTPUT_STRIP_TRAILING_WHITESPACE)
  7. if(NOT EXISTS "${PYTHON_SITE_PACKAGES}/lxml")
  8. execute_process ( COMMAND ${PYTHON_EXECUTABLE} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(True))" OUTPUT_VARIABLE PYTHON_SITE_PACKAGES OUTPUT_STRIP_TRAILING_WHITESPACE)
  9. if(NOT EXISTS "${PYTHON_SITE_PACKAGES}/lxml")
  10. message( FATAL_ERROR "Python-lxml is not installed.")
  11. endif()
  12. endif()
  13. find_package(Git)
  14. if(GIT_FOUND)
  15. execute_process(COMMAND ${GIT_EXECUTABLE} describe --abbrev=7 --dirty --always --tags RESULT_VARIABLE res_var OUTPUT_VARIABLE GIT_COM_ID )
  16. if( NOT ${res_var} EQUAL 0 )
  17. set( GIT_COMMIT_ID "unknown--git-commit-id-unknown")
  18. message( STATUS "Git failed (not a repo, or no tags). Build will not contain git revision info." )
  19. else()
  20. string( REPLACE "\n" "" GIT_COMMIT_ID ${GIT_COM_ID} )
  21. endif()
  22. else()
  23. set( GIT_COMMIT_ID "unknown--no-git-found")
  24. message( STATUS "Git not found. Build will not contain git revision info." )
  25. endif()
  26. add_definitions("-DVERSION=${GIT_COMMIT_ID}")
  27. MESSAGE (STATUS "Git version: " ${GIT_COMMIT_ID})
  28. set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
  29. # Set default build type.
  30. if(NOT CMAKE_BUILD_TYPE)
  31. message(STATUS "CMAKE_BUILD_TYPE not given; setting to 'RelWithDebInfo'.")
  32. set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "Choose the type of build." FORCE)
  33. endif()
  34. # compiler flags
  35. if(CMAKE_COMPILER_IS_GNUCC OR "x${CMAKE_C_COMPILER_ID}" STREQUAL "xClang")
  36. add_definitions(-std=c99 -pipe -Wall -Wextra -Werror -Wformat -Wno-unused-parameter
  37. -Wno-unused-function -Wno-unused-label -Wpointer-arith -Wreturn-type -Wsign-compare -Wmultichar
  38. -Wcast-qual -Wmissing-prototypes -Wstrict-prototypes #-Wshadow #-Wconversion
  39. -Winit-self -Wuninitialized -Wformat-security -Wformat-nonliteral)
  40. # binary size reduction settings
  41. add_definitions(-ffunction-sections -fdata-sections -fno-stack-protector -fno-unwind-tables
  42. -fno-asynchronous-unwind-tables -fno-math-errno -fmerge-all-constants -fno-ident)
  43. if(APPLE)
  44. set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,-dead_strip")
  45. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-dead_strip")
  46. else()
  47. set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,--gc-sections")
  48. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--gc-sections")
  49. endif()
  50. set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "") # cmake sets -rdynamic by default
  51. if(NOT WIN32 AND NOT CYGWIN)
  52. add_definitions(-fvisibility=hidden -fPIC)
  53. if(NOT APPLE)
  54. # these settings reduce the binary size by ~2kb
  55. set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,-z,norelro -Wl,--hash-style=gnu -Wl,--build-id=none")
  56. endif()
  57. endif()
  58. if(CMAKE_BUILD_TYPE STREQUAL "MinSizeRel" OR CMAKE_BUILD_TYPE STREQUAL "Release")
  59. set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -s")
  60. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -s")
  61. endif()
  62. if(APPLE)
  63. set(CMAKE_MACOSX_RPATH 1)
  64. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_DARWIN_C_SOURCE=1")
  65. endif()
  66. elseif(MSVC)
  67. set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MT")
  68. set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MTd")
  69. endif()
  70. # build the main library
  71. set(exported_headers ${PROJECT_BINARY_DIR}/src_generated/ua_config.h
  72. ${PROJECT_SOURCE_DIR}/include/ua_statuscodes.h
  73. ${PROJECT_SOURCE_DIR}/include/ua_types.h
  74. ${PROJECT_BINARY_DIR}/src_generated/ua_nodeids.h
  75. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.h
  76. ${PROJECT_SOURCE_DIR}/include/ua_job.h
  77. ${PROJECT_SOURCE_DIR}/include/ua_connection.h
  78. ${PROJECT_SOURCE_DIR}/include/ua_log.h
  79. ${PROJECT_SOURCE_DIR}/include/ua_server.h
  80. ${PROJECT_SOURCE_DIR}/include/ua_client.h
  81. ${PROJECT_SOURCE_DIR}/examples/networklayer_tcp.h
  82. ${PROJECT_SOURCE_DIR}/examples/logger_stdout.h)
  83. set(internal_headers ${PROJECT_SOURCE_DIR}/src/ua_util.h
  84. ${PROJECT_SOURCE_DIR}/deps/queue.h
  85. ${PROJECT_SOURCE_DIR}/deps/pcg_basic.h
  86. ${PROJECT_SOURCE_DIR}/src/ua_types_encoding_binary.h
  87. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated_encoding_binary.h
  88. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.h
  89. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated_encoding_binary.h
  90. ${PROJECT_SOURCE_DIR}/src/ua_securechannel.h
  91. ${PROJECT_SOURCE_DIR}/src/server/ua_nodes.h
  92. ${PROJECT_SOURCE_DIR}/src/ua_session.h
  93. ${PROJECT_SOURCE_DIR}/src/server/ua_nodestore.h
  94. ${PROJECT_SOURCE_DIR}/src/server/ua_session_manager.h
  95. ${PROJECT_SOURCE_DIR}/src/server/ua_securechannel_manager.h
  96. ${PROJECT_SOURCE_DIR}/src/server/ua_server_internal.h
  97. ${PROJECT_SOURCE_DIR}/src/server/ua_services.h
  98. ${PROJECT_SOURCE_DIR}/src/client/ua_client_internal.h)
  99. set(lib_sources ${PROJECT_SOURCE_DIR}/src/ua_types.c
  100. ${PROJECT_SOURCE_DIR}/src/ua_types_encoding_binary.c
  101. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.c
  102. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.c
  103. ${PROJECT_SOURCE_DIR}/src/ua_connection.c
  104. ${PROJECT_SOURCE_DIR}/src/ua_securechannel.c
  105. ${PROJECT_SOURCE_DIR}/src/ua_session.c
  106. ${PROJECT_SOURCE_DIR}/src/server/ua_server.c
  107. ${PROJECT_SOURCE_DIR}/src/server/ua_server_binary.c
  108. ${PROJECT_SOURCE_DIR}/src/server/ua_nodes.c
  109. ${PROJECT_SOURCE_DIR}/src/server/ua_server_worker.c
  110. ${PROJECT_SOURCE_DIR}/src/server/ua_securechannel_manager.c
  111. ${PROJECT_SOURCE_DIR}/src/server/ua_session_manager.c
  112. ${PROJECT_SOURCE_DIR}/src/server/ua_services_discovery.c
  113. ${PROJECT_SOURCE_DIR}/src/server/ua_services_securechannel.c
  114. ${PROJECT_SOURCE_DIR}/src/server/ua_services_session.c
  115. ${PROJECT_SOURCE_DIR}/src/server/ua_services_attribute.c
  116. ${PROJECT_SOURCE_DIR}/src/server/ua_services_nodemanagement.c
  117. ${PROJECT_SOURCE_DIR}/src/server/ua_services_view.c
  118. ${PROJECT_SOURCE_DIR}/src/client/ua_client.c
  119. ${PROJECT_SOURCE_DIR}/examples/networklayer_tcp.c
  120. ${PROJECT_SOURCE_DIR}/examples/logger_stdout.c
  121. ${PROJECT_SOURCE_DIR}/deps/pcg_basic.c)
  122. ##TODO: make client stuff optional
  123. ## generate code from xml definitions
  124. file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/src_generated")
  125. option(ENABLE_TYPEINTROSPECTION "Add the type and member names to the internal structures" OFF)
  126. mark_as_advanced(ENABLE_TYPEINTROSPECTION)
  127. if(ENABLE_TYPEINTROSPECTION)
  128. set(generate_typeintrospection "--typeintrospection")
  129. else()
  130. set(generate_typeintrospection "")
  131. endif()
  132. option(ENABLE_SUBSCRIPTIONS "Enable compilation of subscription and monitoring support." OFF)
  133. if(ENABLE_SUBSCRIPTIONS)
  134. set(ENABLE_SUBSCRIPTIONS ON) #to propagate it to the config file
  135. list(APPEND lib_sources ${PROJECT_SOURCE_DIR}/src/server/ua_services_subscription.c
  136. ${PROJECT_SOURCE_DIR}/src/server/ua_subscription.c
  137. ${PROJECT_SOURCE_DIR}/src/server/ua_subscription_manager.c)
  138. ##append subscription headers at before ua_session.
  139. list(FIND internal_headers "${PROJECT_SOURCE_DIR}/src/ua_session.h" UaSessionPos)
  140. list(INSERT internal_headers ${UaSessionPos} ${PROJECT_SOURCE_DIR}/src/server/ua_subscription.h
  141. ${PROJECT_SOURCE_DIR}/src/server/ua_subscription_manager.h)
  142. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.c
  143. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.h
  144. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated_encoding_binary.h
  145. PRE_BUILD
  146. COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/tools/generate_datatypes.py --enable-subscription-types=1 ${generate_typeintrospection} --typedescriptions ${PROJECT_SOURCE_DIR}/tools/schema/NodeIds.csv 0 ${PROJECT_SOURCE_DIR}/tools/schema/Opc.Ua.Types.bsd ${PROJECT_BINARY_DIR}/src_generated/ua_types
  147. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_datatypes.py
  148. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/Opc.Ua.Types.bsd
  149. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/NodeIds.csv)
  150. else()
  151. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.c
  152. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.h
  153. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated_encoding_binary.h
  154. PRE_BUILD
  155. COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/tools/generate_datatypes.py ${generate_typeintrospection} --typedescriptions ${PROJECT_SOURCE_DIR}/tools/schema/NodeIds.csv 0 ${PROJECT_SOURCE_DIR}/tools/schema/Opc.Ua.Types.bsd ${PROJECT_BINARY_DIR}/src_generated/ua_types
  156. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_datatypes.py
  157. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/Opc.Ua.Types.bsd
  158. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/NodeIds.csv)
  159. endif()
  160. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.c
  161. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.h
  162. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated_encoding_binary.h
  163. PRE_BUILD
  164. COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/tools/generate_datatypes.py ${generate_typeintrospection} --ns0-types-xml ${PROJECT_SOURCE_DIR}/tools/schema/Opc.Ua.Types.bsd 1 ${PROJECT_SOURCE_DIR}/tools/schema/Custom.Opc.Ua.Transport.bsd ${PROJECT_BINARY_DIR}/src_generated/ua_transport
  165. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_datatypes.py
  166. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/Custom.Opc.Ua.Transport.bsd)
  167. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_nodeids.h
  168. PRE_BUILD
  169. COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/tools/generate_nodeids.py ${PROJECT_SOURCE_DIR}/tools/schema/NodeIds.csv ${PROJECT_BINARY_DIR}/src_generated/ua_nodeids
  170. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_nodeids.py
  171. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/NodeIds.csv)
  172. option(ENABLE_GENERATE_NAMESPACE0 "Generate and load UA XML Namespace 0 definition" OFF)
  173. if(ENABLE_GENERATE_NAMESPACE0)
  174. add_definitions(-DENABLE_GENERATE_NAMESPACE0)
  175. set(GENERATE_NAMESPACE0_FILE "Opc.Ua.NodeSet2.xml" CACHE STRING "Namespace definition XML file")
  176. set_property(CACHE GENERATE_NAMESPACE0_FILE PROPERTY STRINGS Opc.Ua.NodeSet2.xml Opc.Ua.NodeSet2.Minimal.xml)
  177. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_namespaceinit_generated.c
  178. ${PROJECT_BINARY_DIR}/src_generated/ua_namespaceinit_generated.h
  179. PRE_BUILD
  180. COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/generate_open62541CCode.py -i ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/NodeID_AssumeExternal.txt -s description -b ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/NodeID_Blacklist.txt ${PROJECT_SOURCE_DIR}/tools/schema/namespace0/${GENERATE_NAMESPACE0_FILE} ${PROJECT_BINARY_DIR}/src_generated/ua_namespaceinit_generated
  181. DEPENDS ${PROJECT_SOURCE_DIR}/tools/schema/namespace0/${GENERATE_NAMESPACE0_FILE}
  182. ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/generate_open62541CCode.py
  183. ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/logger.py
  184. ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/open62541_MacroHelper.py
  185. ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/ua_builtin_types.py
  186. ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/ua_constants.py
  187. ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/ua_namespace.py
  188. ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/ua_node_types.py)
  189. list(APPEND internal_headers ${PROJECT_BINARY_DIR}/src_generated/ua_namespaceinit_generated.h)
  190. list(APPEND lib_sources ${PROJECT_BINARY_DIR}/src_generated/ua_namespaceinit_generated.c)
  191. endif()
  192. ## logging
  193. set(UA_LOGLEVEL 300 CACHE STRING "Level at which logs shall be reported")
  194. # Enable Methodcall service
  195. option(ENABLE_METHODCALLS "Enable CallMethod/MethodCall service set" OFF)
  196. if(ENABLE_METHODCALLS)
  197. set(ENABLE_METHODCALLS ON) #to propagate it to the config file
  198. list(APPEND lib_sources ${PROJECT_SOURCE_DIR}/src/server/ua_services_call.c)
  199. endif()
  200. ## multithreading
  201. option(ENABLE_MULTITHREADING "Enable multithreading (experimental)" OFF)
  202. if(ENABLE_MULTITHREADING)
  203. set(UA_MULTITHREADING ON) #to propagate it to the config file
  204. find_package(Threads REQUIRED)
  205. list(APPEND lib_sources ${PROJECT_SOURCE_DIR}/src/server/ua_nodestore_concurrent.c)
  206. else()
  207. list(APPEND lib_sources ${PROJECT_SOURCE_DIR}/src/server/ua_nodestore.c)
  208. endif()
  209. option(ENABLE_EXTERNAL_NAMESPACES "Enable namespace handling by an external component (experimental)" OFF)
  210. if(ENABLE_EXTERNAL_NAMESPACES)
  211. add_definitions(-DUA_EXTERNAL_NAMESPACES)
  212. endif()
  213. ## enable dynamic nodeset
  214. option(ENABLE_NODEMANAGEMENT "Enable dynamic addition and removal of nodes" ON)
  215. if(ENABLE_NODEMANAGEMENT)
  216. add_definitions(-DENABLE_NODEMANAGEMENT)
  217. endif()
  218. ## set the precompiler flags
  219. configure_file("include/ua_config.h.in" "${PROJECT_BINARY_DIR}/src_generated/ua_config.h")
  220. ## extensions
  221. option(EXTENSION_UDP "Enable udp extension" OFF)
  222. if(EXTENSION_UDP)
  223. add_definitions(-DEXTENSION_STATELESS)
  224. message(STATUS "Extensions: enabling udp")
  225. add_definitions(-DEXTENSION_UDP)
  226. add_executable(exampleServerUDP $<TARGET_OBJECTS:open62541-object> examples/networklayer_udp.c examples/server_udp.c)
  227. if(ENABLE_MULTITHREADING)
  228. target_link_libraries(exampleServerUDP urcu-cds urcu urcu-common)
  229. endif()
  230. if (NOT APPLE)
  231. target_link_libraries(exampleServerUDP rt)
  232. endif()
  233. endif()
  234. option(ENABLE_LEGACY "Enable LEGACY code" OFF)
  235. mark_as_advanced(ENABLE_LEGACY)
  236. option(EXTENSION_STATELESS "Enable stateless extension" OFF)
  237. if(EXTENSION_STATELESS)
  238. message(STATUS "Extensions: enabling stateless")
  239. add_definitions(-DEXTENSION_STATELESS)
  240. endif()
  241. option(ENABLE_AMALGAMATION "Concatenate the library to a single file open62541.h/.c" OFF)
  242. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/open62541.h
  243. PRE_BUILD
  244. COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tools/amalgamate.py ${GIT_COMMIT_ID} ${CMAKE_CURRENT_BINARY_DIR}/open62541.h ${exported_headers}
  245. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/amalgamate.py ${exported_headers} ${internal_headers})
  246. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/open62541.c
  247. PRE_BUILD
  248. COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tools/amalgamate.py ${GIT_COMMIT_ID} ${CMAKE_CURRENT_BINARY_DIR}/open62541.c
  249. ${internal_headers} ${PROJECT_SOURCE_DIR}/src/server/ua_nodestore_hash.inc ${lib_sources}
  250. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/amalgamate.py ${internal_headers} ${PROJECT_SOURCE_DIR}/src/server/ua_nodestore_hash.inc ${lib_sources})
  251. if(ENABLE_AMALGAMATION)
  252. add_custom_target(amalgamation ALL DEPENDS ${PROJECT_BINARY_DIR}/open62541.h ${PROJECT_BINARY_DIR}/open62541.c)
  253. add_library(open62541-object OBJECT ${PROJECT_BINARY_DIR}/open62541.c ${PROJECT_BINARY_DIR}/open62541.h)
  254. include_directories(${PROJECT_BINARY_DIR})
  255. else()
  256. add_definitions(-DUA_NO_AMALGAMATION)
  257. add_library(open62541-object OBJECT ${lib_sources} ${internal_headers} ${exported_headers})
  258. include_directories(${PROJECT_SOURCE_DIR}/include)
  259. include_directories(${PROJECT_SOURCE_DIR}/deps)
  260. include_directories(${PROJECT_SOURCE_DIR}/src)
  261. endif()
  262. include_directories(${PROJECT_BINARY_DIR}/src_generated) #needed to locate an include of examples/server_nodeset.c
  263. target_compile_definitions(open62541-object PRIVATE -DUA_DYNAMIC_LINKING)
  264. add_library(open62541 SHARED $<TARGET_OBJECTS:open62541-object>)
  265. add_library(open62541-static STATIC $<TARGET_OBJECTS:open62541-object>)
  266. target_compile_definitions(open62541 PRIVATE -DUA_DYNAMIC_LINKING)
  267. target_compile_definitions(open62541-static PRIVATE -DUA_DYNAMIC_LINKING)
  268. if(WIN32)
  269. # since networklayer_tcp is linked into the amalgate
  270. target_link_libraries(open62541 ws2_32)
  271. target_link_libraries(open62541-static ws2_32)
  272. endif()
  273. if(ENABLE_MULTITHREADING)
  274. target_link_libraries(open62541 urcu-cds urcu urcu-common pthread)
  275. target_link_libraries(open62541-static urcu-cds urcu urcu-common pthread)
  276. endif()
  277. # coverage
  278. option(ENABLE_COVERAGE "Enable gcov coverage" OFF)
  279. if(ENABLE_COVERAGE)
  280. message(STATUS "Enabling gcov support")
  281. set(CMAKE_BUILD_TYPE DEBUG)
  282. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
  283. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
  284. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
  285. endif()
  286. # build example server
  287. option(BUILD_EXAMPLESERVER "Build the example server" OFF)
  288. if(BUILD_EXAMPLESERVER)
  289. set(server_source $<TARGET_OBJECTS:open62541-object>)
  290. add_executable(server_static examples/server.c ${server_source})
  291. add_executable(server examples/server.c)
  292. if(ENABLE_AMALGAMATION)
  293. ADD_DEPENDENCIES(server amalgamation)
  294. endif()
  295. target_link_libraries(server open62541)
  296. if(WIN32)
  297. target_link_libraries(server_static ws2_32)
  298. target_link_libraries(server ws2_32)
  299. elseif(NOT APPLE)
  300. target_link_libraries(server_static rt)
  301. target_link_libraries(server rt)
  302. endif()
  303. if(ENABLE_MULTITHREADING)
  304. target_link_libraries(server_static urcu-cds urcu urcu-common pthread)
  305. target_link_libraries(server urcu-cds urcu urcu-common pthread)
  306. endif()
  307. endif()
  308. ## self-signed certificates
  309. option(GENERATE_SELFSIGNED "Generate self-signed certificates" OFF)
  310. if(GENERATE_SELFSIGNED)
  311. message(STATUS "Enabling self-signed certificates")
  312. find_package(OpenSSL REQUIRED)
  313. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/server_cert.der
  314. ${PROJECT_BINARY_DIR}/ca.crt
  315. COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/tools/certs/create_self-signed.py ${PROJECT_BINARY_DIR}
  316. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/certs/create_self-signed.py
  317. ${CMAKE_CURRENT_SOURCE_DIR}/tools/certs/localhost.cnf)
  318. add_custom_target(selfsigned ALL DEPENDS ${PROJECT_BINARY_DIR}/server_cert.der ${PROJECT_BINARY_DIR}/ca.crt)
  319. endif()
  320. # build example client
  321. option(BUILD_EXAMPLECLIENT "Build a test client" OFF)
  322. if(BUILD_EXAMPLECLIENT)
  323. add_definitions(-DBENCHMARK)
  324. set(client_source $<TARGET_OBJECTS:open62541-object>)
  325. add_executable(client_static examples/client.c ${client_source})
  326. add_executable(client examples/client.c)
  327. if(ENABLE_AMALGAMATION)
  328. ADD_DEPENDENCIES(client amalgamation)
  329. endif()
  330. target_link_libraries(client open62541)
  331. if(WIN32)
  332. target_link_libraries(client_static ws2_32)
  333. target_link_libraries(client ws2_32)
  334. elseif(NOT APPLE)
  335. target_link_libraries(client_static rt)
  336. target_link_libraries(client rt)
  337. endif()
  338. if(ENABLE_MULTITHREADING)
  339. target_link_libraries(client_static urcu-cds urcu urcu-common pthread)
  340. target_link_libraries(client urcu-cds urcu urcu-common pthread)
  341. endif()
  342. if(EXTENSION_STATELESS AND NOT ENABLE_AMALGAMATION)
  343. add_executable(client_stateless examples/client_stateless.c ${client_source})
  344. if(ENABLE_MULTITHREADING)
  345. target_link_libraries(client_stateless urcu-cds urcu urcu-common pthread)
  346. endif()
  347. endif()
  348. if(ENABLE_LEGACY AND NOT ENABLE_AMALGAMATION)
  349. add_executable(client_legacy ${PROJECT_SOURCE_DIR}/examples/client_legacy.c $<TARGET_OBJECTS:open62541-object>)
  350. target_link_libraries(client_legacy ${LIBS})
  351. endif()
  352. endif()
  353. # build unit tests
  354. option(BUILD_UNIT_TESTS "Run unit tests after building" OFF)
  355. if(BUILD_UNIT_TESTS)
  356. add_definitions(-DBUILD_UNIT_TESTS)
  357. enable_testing()
  358. add_subdirectory(tests)
  359. endif()
  360. option(BUILD_EXAMPLES "Build example servers and clients" OFF)
  361. if(BUILD_EXAMPLES)
  362. #add_subdirectory(examples)
  363. #FIXME: we had problem with static linking for msvs, here a quick and dirty workaround
  364. #http://stackoverflow.com/questions/3704374/linking-error-lnk2019-in-msvc-unresolved-symbols-with-imp-prefix-but-shoul
  365. #http://stackoverflow.com/questions/1089828/same-header-file-for-both-dll-and-static-library
  366. if(NOT WIN32)
  367. list(APPEND LIBS pthread)
  368. if (NOT APPLE)
  369. list(APPEND LIBS rt)
  370. endif()
  371. else()
  372. list(APPEND LIBS ws2_32)
  373. endif()
  374. if(ENABLE_MULTITHREADING)
  375. list(APPEND LIBS urcu-cds urcu urcu-common)
  376. endif(ENABLE_MULTITHREADING)
  377. add_executable(server_variable ${PROJECT_SOURCE_DIR}/examples/server_variable.c $<TARGET_OBJECTS:open62541-object>)
  378. target_link_libraries(server_variable ${LIBS})
  379. add_executable(server_datasource ${PROJECT_SOURCE_DIR}/examples/server_datasource.c $<TARGET_OBJECTS:open62541-object>)
  380. target_link_libraries(server_datasource ${LIBS})
  381. add_executable(server_firstSteps ${PROJECT_SOURCE_DIR}/examples/server_firstSteps.c $<TARGET_OBJECTS:open62541-object>)
  382. target_link_libraries(server_firstSteps ${LIBS})
  383. add_executable(client_firstSteps ${PROJECT_SOURCE_DIR}/examples/client_firstSteps.c $<TARGET_OBJECTS:open62541-object>)
  384. target_link_libraries(client_firstSteps ${LIBS})
  385. add_executable(server_repeated_job ${PROJECT_SOURCE_DIR}/examples/server_repeated_job.c $<TARGET_OBJECTS:open62541-object>)
  386. target_link_libraries(server_repeated_job ${LIBS})
  387. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/nodeset.h ${PROJECT_BINARY_DIR}/src_generated/nodeset.c
  388. PRE_BUILD
  389. COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/generate_open62541CCode.py -i ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/NodeID_Blacklist_FullNS0.txt ${PROJECT_SOURCE_DIR}/tools/schema/namespace0/Opc.Ua.NodeSet2.xml ${PROJECT_SOURCE_DIR}/examples/server_nodeset.xml ${PROJECT_BINARY_DIR}/src_generated/nodeset
  390. DEPENDS ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/generate_open62541CCode.py
  391. ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/logger.py
  392. ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/open62541_MacroHelper.py
  393. ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/ua_builtin_types.py
  394. ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/ua_constants.py
  395. ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/ua_namespace.py
  396. ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/ua_node_types.py
  397. ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/NodeID_Blacklist_FullNS0.txt
  398. ${PROJECT_SOURCE_DIR}/examples/server_nodeset.xml)
  399. add_executable(server_nodeset ${PROJECT_SOURCE_DIR}/examples/server_nodeset.c ${PROJECT_BINARY_DIR}/src_generated/nodeset.c $<TARGET_OBJECTS:open62541-object>)
  400. target_link_libraries(server_nodeset ${LIBS})
  401. if(ENABLE_METHODCALLS)
  402. add_executable(server_method ${PROJECT_SOURCE_DIR}/examples/server_method.c $<TARGET_OBJECTS:open62541-object>)
  403. target_link_libraries(server_method ${LIBS})
  404. endif()
  405. endif()
  406. # build documentation
  407. option(BUILD_DOCUMENTATION "Generate doxygen/sphinx documentation" OFF)
  408. if(BUILD_DOCUMENTATION)
  409. find_package(Doxygen REQUIRED)
  410. find_package(Sphinx REQUIRED)
  411. configure_file(${PROJECT_SOURCE_DIR}/doc/Doxyfile.in ${PROJECT_BINARY_DIR}/Doxyfile @ONLY)
  412. configure_file(${PROJECT_SOURCE_DIR}/doc/conf.py ${PROJECT_BINARY_DIR}/conf.py @ONLY)
  413. add_custom_target(doxygen
  414. ${DOXYGEN_EXECUTABLE} ${PROJECT_BINARY_DIR}/Doxyfile
  415. WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
  416. COMMENT "Generating API documentation with Doxygen")
  417. add_custom_target(latex ${SPHINX_EXECUTABLE}
  418. -b latex -c "${PROJECT_BINARY_DIR}"
  419. "${PROJECT_SOURCE_DIR}/doc" "${PROJECT_BINARY_DIR}/doc/sphinx"
  420. DEPENDS doxygen
  421. COMMENT "Building LaTeX sources for documentation with Sphinx")
  422. add_custom_target(doc ${SPHINX_EXECUTABLE}
  423. -b html -c "${PROJECT_BINARY_DIR}"
  424. "${PROJECT_SOURCE_DIR}/doc" "${PROJECT_BINARY_DIR}/doc/sphinx"
  425. COMMAND ${CMAKE_COMMAND} -E copy "${PROJECT_SOURCE_DIR}/doc/open62541_html.png" "${PROJECT_BINARY_DIR}/doc/sphinx/_static/"
  426. DEPENDS doxygen
  427. COMMENT "Building HTML documentation with Sphinx")
  428. endif()