CMakeLists.txt 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. cmake_minimum_required(VERSION 2.8.11)
  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_server_external_ns.h
  81. ${PROJECT_SOURCE_DIR}/include/ua_client.h
  82. ${PROJECT_SOURCE_DIR}/include/ua_client_highlevel.h
  83. ${PROJECT_SOURCE_DIR}/examples/networklayer_tcp.h
  84. ${PROJECT_SOURCE_DIR}/examples/logger_stdout.h)
  85. set(internal_headers ${PROJECT_SOURCE_DIR}/src/ua_util.h
  86. ${PROJECT_SOURCE_DIR}/deps/queue.h
  87. ${PROJECT_SOURCE_DIR}/deps/pcg_basic.h
  88. ${PROJECT_SOURCE_DIR}/src/ua_types_encoding_binary.h
  89. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated_encoding_binary.h
  90. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.h
  91. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated_encoding_binary.h
  92. ${PROJECT_SOURCE_DIR}/src/ua_securechannel.h
  93. ${PROJECT_SOURCE_DIR}/src/server/ua_nodes.h
  94. ${PROJECT_SOURCE_DIR}/src/ua_session.h
  95. ${PROJECT_SOURCE_DIR}/src/server/ua_nodestore.h
  96. ${PROJECT_SOURCE_DIR}/src/server/ua_session_manager.h
  97. ${PROJECT_SOURCE_DIR}/src/server/ua_securechannel_manager.h
  98. ${PROJECT_SOURCE_DIR}/src/server/ua_server_internal.h
  99. ${PROJECT_SOURCE_DIR}/src/server/ua_services.h
  100. ${PROJECT_SOURCE_DIR}/src/client/ua_client_internal.h)
  101. set(lib_sources ${PROJECT_SOURCE_DIR}/src/ua_types.c
  102. ${PROJECT_SOURCE_DIR}/src/ua_types_encoding_binary.c
  103. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.c
  104. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.c
  105. ${PROJECT_SOURCE_DIR}/src/ua_connection.c
  106. ${PROJECT_SOURCE_DIR}/src/ua_securechannel.c
  107. ${PROJECT_SOURCE_DIR}/src/ua_session.c
  108. ${PROJECT_SOURCE_DIR}/src/server/ua_server.c
  109. ${PROJECT_SOURCE_DIR}/src/server/ua_server_binary.c
  110. ${PROJECT_SOURCE_DIR}/src/server/ua_nodes.c
  111. ${PROJECT_SOURCE_DIR}/src/server/ua_server_worker.c
  112. ${PROJECT_SOURCE_DIR}/src/server/ua_securechannel_manager.c
  113. ${PROJECT_SOURCE_DIR}/src/server/ua_session_manager.c
  114. ${PROJECT_SOURCE_DIR}/src/server/ua_services_discovery.c
  115. ${PROJECT_SOURCE_DIR}/src/server/ua_services_securechannel.c
  116. ${PROJECT_SOURCE_DIR}/src/server/ua_services_session.c
  117. ${PROJECT_SOURCE_DIR}/src/server/ua_services_attribute.c
  118. ${PROJECT_SOURCE_DIR}/src/server/ua_services_nodemanagement.c
  119. ${PROJECT_SOURCE_DIR}/src/server/ua_services_view.c
  120. ${PROJECT_SOURCE_DIR}/src/client/ua_client.c
  121. ${PROJECT_SOURCE_DIR}/src/client/ua_client_highlevel.c
  122. ${PROJECT_SOURCE_DIR}/examples/networklayer_tcp.c
  123. ${PROJECT_SOURCE_DIR}/examples/logger_stdout.c
  124. ${PROJECT_SOURCE_DIR}/deps/pcg_basic.c)
  125. ##TODO: make client stuff optional
  126. ## generate code from xml definitions
  127. file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/src_generated")
  128. option(ENABLE_TYPEINTROSPECTION "Add the type and member names to the internal structures" OFF)
  129. mark_as_advanced(ENABLE_TYPEINTROSPECTION)
  130. if(ENABLE_TYPEINTROSPECTION)
  131. set(generate_typeintrospection "--typeintrospection")
  132. else()
  133. set(generate_typeintrospection "")
  134. endif()
  135. option(UA_EMBEDDED_LIBC "Target has no libc, use internal definitions" OFF)
  136. if(UA_EMBEDDED_LIBC)
  137. list(APPEND lib_sources ${PROJECT_SOURCE_DIR}/deps/libc_string.c)
  138. endif()
  139. option(ENABLE_SUBSCRIPTIONS "Enable compilation of subscription and monitoring support." OFF)
  140. if(ENABLE_SUBSCRIPTIONS)
  141. set(ENABLE_SUBSCRIPTIONS ON) #to propagate it to the config file
  142. list(APPEND lib_sources ${PROJECT_SOURCE_DIR}/src/server/ua_services_subscription.c
  143. ${PROJECT_SOURCE_DIR}/src/server/ua_subscription.c
  144. ${PROJECT_SOURCE_DIR}/src/server/ua_subscription_manager.c
  145. ${PROJECT_SOURCE_DIR}/src/client/ua_client_highlevel_subscriptions.c)
  146. ##append subscription headers at before ua_session.
  147. list(FIND internal_headers "${PROJECT_SOURCE_DIR}/src/ua_session.h" UaSessionPos)
  148. list(INSERT internal_headers ${UaSessionPos} ${PROJECT_SOURCE_DIR}/src/server/ua_subscription.h
  149. ${PROJECT_SOURCE_DIR}/src/server/ua_subscription_manager.h)
  150. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.c
  151. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.h
  152. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated_encoding_binary.h
  153. PRE_BUILD
  154. 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
  155. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_datatypes.py
  156. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/Opc.Ua.Types.bsd
  157. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/NodeIds.csv)
  158. else()
  159. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.c
  160. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.h
  161. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated_encoding_binary.h
  162. PRE_BUILD
  163. 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
  164. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_datatypes.py
  165. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/Opc.Ua.Types.bsd
  166. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/NodeIds.csv)
  167. endif()
  168. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.c
  169. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.h
  170. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated_encoding_binary.h
  171. PRE_BUILD
  172. 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
  173. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_datatypes.py
  174. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/Custom.Opc.Ua.Transport.bsd)
  175. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_nodeids.h
  176. PRE_BUILD
  177. COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/tools/generate_nodeids.py ${PROJECT_SOURCE_DIR}/tools/schema/NodeIds.csv ${PROJECT_BINARY_DIR}/src_generated/ua_nodeids
  178. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_nodeids.py
  179. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/NodeIds.csv)
  180. option(ENABLE_GENERATE_NAMESPACE0 "Generate and load UA XML Namespace 0 definition" OFF)
  181. if(ENABLE_GENERATE_NAMESPACE0)
  182. add_definitions(-DENABLE_GENERATE_NAMESPACE0)
  183. set(GENERATE_NAMESPACE0_FILE "Opc.Ua.NodeSet2.xml" CACHE STRING "Namespace definition XML file")
  184. set_property(CACHE GENERATE_NAMESPACE0_FILE PROPERTY STRINGS Opc.Ua.NodeSet2.xml Opc.Ua.NodeSet2.Minimal.xml)
  185. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_namespaceinit_generated.c
  186. ${PROJECT_BINARY_DIR}/src_generated/ua_namespaceinit_generated.h
  187. PRE_BUILD
  188. 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
  189. DEPENDS ${PROJECT_SOURCE_DIR}/tools/schema/namespace0/${GENERATE_NAMESPACE0_FILE}
  190. ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/generate_open62541CCode.py
  191. ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/logger.py
  192. ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/open62541_MacroHelper.py
  193. ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/ua_builtin_types.py
  194. ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/ua_constants.py
  195. ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/ua_namespace.py
  196. ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/ua_node_types.py)
  197. list(APPEND internal_headers ${PROJECT_BINARY_DIR}/src_generated/ua_namespaceinit_generated.h)
  198. list(APPEND lib_sources ${PROJECT_BINARY_DIR}/src_generated/ua_namespaceinit_generated.c)
  199. endif()
  200. ## logging
  201. set(UA_LOGLEVEL 300 CACHE STRING "Level at which logs shall be reported")
  202. # Enable Methodcall service
  203. option(ENABLE_METHODCALLS "Enable CallMethod/MethodCall service set" OFF)
  204. if(ENABLE_METHODCALLS)
  205. set(ENABLE_METHODCALLS ON) #to propagate it to the config file
  206. list(APPEND lib_sources ${PROJECT_SOURCE_DIR}/src/server/ua_services_call.c)
  207. endif()
  208. ## multithreading
  209. option(ENABLE_MULTITHREADING "Enable multithreading (experimental)" OFF)
  210. if(ENABLE_MULTITHREADING)
  211. set(UA_MULTITHREADING ON) #to propagate it to the config file
  212. find_package(Threads REQUIRED)
  213. list(APPEND lib_sources ${PROJECT_SOURCE_DIR}/src/server/ua_nodestore_concurrent.c)
  214. else()
  215. list(APPEND lib_sources ${PROJECT_SOURCE_DIR}/src/server/ua_nodestore.c)
  216. endif()
  217. option(ENABLE_EXTERNAL_NAMESPACES "Enable namespace handling by an external component (experimental)" OFF)
  218. if(ENABLE_EXTERNAL_NAMESPACES)
  219. add_definitions(-DUA_EXTERNAL_NAMESPACES)
  220. endif()
  221. ## enable dynamic nodeset
  222. option(ENABLE_NODEMANAGEMENT "Enable dynamic addition and removal of nodes" ON)
  223. if(ENABLE_NODEMANAGEMENT)
  224. add_definitions(-DENABLE_NODEMANAGEMENT)
  225. endif()
  226. ## set the precompiler flags
  227. configure_file("include/ua_config.h.in" "${PROJECT_BINARY_DIR}/src_generated/ua_config.h")
  228. ## extensions
  229. option(EXTENSION_UDP "Enable udp extension" OFF)
  230. if(EXTENSION_UDP)
  231. add_definitions(-DEXTENSION_STATELESS)
  232. message(STATUS "Extensions: enabling udp")
  233. add_definitions(-DEXTENSION_UDP)
  234. add_executable(exampleServerUDP $<TARGET_OBJECTS:open62541-object> examples/networklayer_udp.c examples/server_udp.c)
  235. if(ENABLE_MULTITHREADING)
  236. target_link_libraries(exampleServerUDP urcu-cds urcu urcu-common)
  237. endif()
  238. if (NOT APPLE)
  239. target_link_libraries(exampleServerUDP rt)
  240. endif()
  241. endif()
  242. option(ENABLE_LEGACY "Enable LEGACY code" OFF)
  243. mark_as_advanced(ENABLE_LEGACY)
  244. option(EXTENSION_STATELESS "Enable stateless extension" OFF)
  245. if(EXTENSION_STATELESS)
  246. message(STATUS "Extensions: enabling stateless")
  247. add_definitions(-DEXTENSION_STATELESS)
  248. endif()
  249. option(ENABLE_AMALGAMATION "Concatenate the library to a single file open62541.h/.c" OFF)
  250. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/open62541.h
  251. PRE_BUILD
  252. COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tools/amalgamate.py ${GIT_COMMIT_ID} ${CMAKE_CURRENT_BINARY_DIR}/open62541.h ${exported_headers}
  253. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/amalgamate.py ${exported_headers} ${internal_headers})
  254. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/open62541.c
  255. PRE_BUILD
  256. COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tools/amalgamate.py ${GIT_COMMIT_ID} ${CMAKE_CURRENT_BINARY_DIR}/open62541.c
  257. ${internal_headers} ${PROJECT_SOURCE_DIR}/src/server/ua_nodestore_hash.inc ${lib_sources}
  258. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/amalgamate.py ${internal_headers} ${PROJECT_SOURCE_DIR}/src/server/ua_nodestore_hash.inc ${lib_sources})
  259. if(ENABLE_AMALGAMATION)
  260. add_custom_target(amalgamation ALL DEPENDS ${PROJECT_BINARY_DIR}/open62541.h ${PROJECT_BINARY_DIR}/open62541.c)
  261. add_library(open62541-object OBJECT ${PROJECT_BINARY_DIR}/open62541.c ${PROJECT_BINARY_DIR}/open62541.h)
  262. include_directories(${PROJECT_BINARY_DIR})
  263. else()
  264. add_definitions(-DUA_NO_AMALGAMATION)
  265. add_library(open62541-object OBJECT ${lib_sources} ${internal_headers} ${exported_headers})
  266. include_directories(${PROJECT_SOURCE_DIR}/include)
  267. include_directories(${PROJECT_SOURCE_DIR}/deps)
  268. include_directories(${PROJECT_SOURCE_DIR}/src)
  269. endif()
  270. include_directories(${PROJECT_BINARY_DIR}/src_generated) #needed to locate an include of examples/server_nodeset.c
  271. target_compile_definitions(open62541-object PRIVATE -DUA_DYNAMIC_LINKING)
  272. add_library(open62541 SHARED $<TARGET_OBJECTS:open62541-object>)
  273. add_library(open62541-static STATIC $<TARGET_OBJECTS:open62541-object>)
  274. target_compile_definitions(open62541 PRIVATE -DUA_DYNAMIC_LINKING)
  275. target_compile_definitions(open62541-static PRIVATE -DUA_DYNAMIC_LINKING)
  276. if(WIN32)
  277. # since networklayer_tcp is linked into the amalgate
  278. target_link_libraries(open62541 ws2_32)
  279. target_link_libraries(open62541-static ws2_32)
  280. endif()
  281. if(ENABLE_MULTITHREADING)
  282. target_link_libraries(open62541 urcu-cds urcu urcu-common pthread)
  283. target_link_libraries(open62541-static urcu-cds urcu urcu-common pthread)
  284. endif()
  285. # coverage
  286. option(ENABLE_COVERAGE "Enable gcov coverage" OFF)
  287. if(ENABLE_COVERAGE)
  288. message(STATUS "Enabling gcov support")
  289. set(CMAKE_BUILD_TYPE DEBUG)
  290. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
  291. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
  292. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
  293. endif()
  294. # build example server
  295. option(BUILD_EXAMPLESERVER "Build the example server" OFF)
  296. if(BUILD_EXAMPLESERVER)
  297. set(server_source $<TARGET_OBJECTS:open62541-object>)
  298. add_executable(server_static examples/server.c ${server_source})
  299. add_executable(server examples/server.c)
  300. if(ENABLE_AMALGAMATION)
  301. ADD_DEPENDENCIES(server amalgamation)
  302. endif()
  303. target_link_libraries(server open62541)
  304. if(WIN32)
  305. target_link_libraries(server_static ws2_32)
  306. target_link_libraries(server ws2_32)
  307. elseif(NOT APPLE)
  308. target_link_libraries(server_static rt)
  309. target_link_libraries(server rt)
  310. endif()
  311. if(ENABLE_MULTITHREADING)
  312. target_link_libraries(server_static urcu-cds urcu urcu-common pthread)
  313. target_link_libraries(server urcu-cds urcu urcu-common pthread)
  314. endif()
  315. endif()
  316. ## self-signed certificates
  317. option(GENERATE_SELFSIGNED "Generate self-signed certificates" OFF)
  318. if(GENERATE_SELFSIGNED)
  319. message(STATUS "Enabling self-signed certificates")
  320. find_package(OpenSSL REQUIRED)
  321. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/server_cert.der
  322. ${PROJECT_BINARY_DIR}/ca.crt
  323. COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/tools/certs/create_self-signed.py ${PROJECT_BINARY_DIR}
  324. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/certs/create_self-signed.py
  325. ${CMAKE_CURRENT_SOURCE_DIR}/tools/certs/localhost.cnf)
  326. add_custom_target(selfsigned ALL DEPENDS ${PROJECT_BINARY_DIR}/server_cert.der ${PROJECT_BINARY_DIR}/ca.crt)
  327. endif()
  328. # build example client
  329. option(BUILD_EXAMPLECLIENT "Build a test client" OFF)
  330. if(BUILD_EXAMPLECLIENT)
  331. add_definitions(-DBENCHMARK)
  332. set(client_source $<TARGET_OBJECTS:open62541-object>)
  333. add_executable(client_static examples/client.c ${client_source})
  334. add_executable(client examples/client.c)
  335. if(ENABLE_AMALGAMATION)
  336. ADD_DEPENDENCIES(client amalgamation)
  337. endif()
  338. target_link_libraries(client open62541)
  339. if(WIN32)
  340. target_link_libraries(client_static ws2_32)
  341. target_link_libraries(client ws2_32)
  342. elseif(NOT APPLE)
  343. target_link_libraries(client_static rt)
  344. target_link_libraries(client rt)
  345. endif()
  346. if(ENABLE_MULTITHREADING)
  347. target_link_libraries(client_static urcu-cds urcu urcu-common pthread)
  348. target_link_libraries(client urcu-cds urcu urcu-common pthread)
  349. endif()
  350. if(EXTENSION_STATELESS AND NOT ENABLE_AMALGAMATION)
  351. add_executable(client_stateless examples/client_stateless.c ${client_source})
  352. if(ENABLE_MULTITHREADING)
  353. target_link_libraries(client_stateless urcu-cds urcu urcu-common pthread)
  354. endif()
  355. endif()
  356. if(ENABLE_LEGACY AND NOT ENABLE_AMALGAMATION)
  357. add_executable(client_legacy ${PROJECT_SOURCE_DIR}/examples/client_legacy.c $<TARGET_OBJECTS:open62541-object>)
  358. target_link_libraries(client_legacy ${LIBS})
  359. endif()
  360. endif()
  361. # build unit tests
  362. option(BUILD_UNIT_TESTS "Run unit tests after building" OFF)
  363. if(BUILD_UNIT_TESTS)
  364. add_definitions(-DBUILD_UNIT_TESTS)
  365. enable_testing()
  366. add_subdirectory(tests)
  367. endif()
  368. option(BUILD_EXAMPLES "Build example servers and clients" OFF)
  369. if(BUILD_EXAMPLES)
  370. #add_subdirectory(examples)
  371. #FIXME: we had problem with static linking for msvs, here a quick and dirty workaround
  372. #http://stackoverflow.com/questions/3704374/linking-error-lnk2019-in-msvc-unresolved-symbols-with-imp-prefix-but-shoul
  373. #http://stackoverflow.com/questions/1089828/same-header-file-for-both-dll-and-static-library
  374. if(NOT WIN32)
  375. list(APPEND LIBS pthread)
  376. if (NOT APPLE)
  377. list(APPEND LIBS rt)
  378. endif()
  379. else()
  380. list(APPEND LIBS ws2_32)
  381. endif()
  382. if(ENABLE_MULTITHREADING)
  383. list(APPEND LIBS urcu-cds urcu urcu-common)
  384. endif(ENABLE_MULTITHREADING)
  385. add_executable(server_variable ${PROJECT_SOURCE_DIR}/examples/server_variable.c $<TARGET_OBJECTS:open62541-object>)
  386. target_link_libraries(server_variable ${LIBS})
  387. add_executable(server_datasource ${PROJECT_SOURCE_DIR}/examples/server_datasource.c $<TARGET_OBJECTS:open62541-object>)
  388. target_link_libraries(server_datasource ${LIBS})
  389. # add_executable(server_readspeed ${PROJECT_SOURCE_DIR}/examples/server_readspeed.c $<TARGET_OBJECTS:open62541-object>)
  390. # target_link_libraries(server_readspeed ${LIBS})
  391. add_executable(server_firstSteps ${PROJECT_SOURCE_DIR}/examples/server_firstSteps.c $<TARGET_OBJECTS:open62541-object>)
  392. target_link_libraries(server_firstSteps ${LIBS})
  393. add_executable(client_firstSteps ${PROJECT_SOURCE_DIR}/examples/client_firstSteps.c $<TARGET_OBJECTS:open62541-object>)
  394. target_link_libraries(client_firstSteps ${LIBS})
  395. add_executable(server_repeated_job ${PROJECT_SOURCE_DIR}/examples/server_repeated_job.c $<TARGET_OBJECTS:open62541-object>)
  396. target_link_libraries(server_repeated_job ${LIBS})
  397. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/nodeset.h ${PROJECT_BINARY_DIR}/src_generated/nodeset.c
  398. PRE_BUILD
  399. 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
  400. DEPENDS ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/generate_open62541CCode.py
  401. ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/logger.py
  402. ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/open62541_MacroHelper.py
  403. ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/ua_builtin_types.py
  404. ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/ua_constants.py
  405. ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/ua_namespace.py
  406. ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/ua_node_types.py
  407. ${PROJECT_SOURCE_DIR}/tools/pyUANamespace/NodeID_Blacklist_FullNS0.txt
  408. ${PROJECT_SOURCE_DIR}/examples/server_nodeset.xml)
  409. add_executable(server_nodeset ${PROJECT_SOURCE_DIR}/examples/server_nodeset.c ${PROJECT_BINARY_DIR}/src_generated/nodeset.c $<TARGET_OBJECTS:open62541-object>)
  410. target_link_libraries(server_nodeset ${LIBS})
  411. if(ENABLE_METHODCALLS)
  412. add_executable(server_method ${PROJECT_SOURCE_DIR}/examples/server_method.c $<TARGET_OBJECTS:open62541-object>)
  413. target_link_libraries(server_method ${LIBS})
  414. endif()
  415. endif()
  416. # build documentation
  417. option(BUILD_DOCUMENTATION "Generate documentation" OFF)
  418. if(BUILD_DOCUMENTATION)
  419. find_package(Sphinx REQUIRED)
  420. add_custom_target(doc_latex ${SPHINX_EXECUTABLE}
  421. -b latex -c "${PROJECT_SOURCE_DIR}/doc" "${PROJECT_SOURCE_DIR}/doc" "${PROJECT_BINARY_DIR}/doc_latex"
  422. COMMENT "Building LaTeX sources for documentation with Sphinx")
  423. add_custom_target(doc ${SPHINX_EXECUTABLE}
  424. -b html -c "${PROJECT_SOURCE_DIR}/doc" "${PROJECT_SOURCE_DIR}/doc" "${PROJECT_BINARY_DIR}/doc"
  425. COMMAND ${CMAKE_COMMAND} -E copy "${PROJECT_SOURCE_DIR}/doc/open62541_html.png" "${PROJECT_BINARY_DIR}/doc/_static/"
  426. COMMENT "Building HTML documentation with Sphinx")
  427. endif()