CMakeLists.txt 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. cmake_minimum_required(VERSION 2.8.8)
  2. # set(CMAKE_VERBOSE_MAKEFILE ON)
  3. project(open62541 C)
  4. set(open62541_VERSION_MAJOR 0)
  5. set(open62541_VERSION_MINOR 0)
  6. set(open62541_VERSION_PATCH 0)
  7. add_definitions(-DOPEN62541_VERSION_MAJOR=${open62541_VERSION_MAJOR})
  8. add_definitions(-DOPEN62541_VERSION_MINOR=${open62541_VERSION_MINOR})
  9. add_definitions(-DOPEN62541_VERSION_PATCH=${open62541_VERSION_PATCH})
  10. set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
  11. # Set default build type.
  12. if(NOT CMAKE_BUILD_TYPE)
  13. message(STATUS "CMAKE_BUILD_TYPE not given; setting to 'RelWithDebInfo'.")
  14. set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "Choose the type of build." FORCE)
  15. endif()
  16. # compiler flags
  17. if(CMAKE_COMPILER_IS_GNUCC OR "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
  18. add_definitions(-std=c99 -pipe -Wall -Wextra -Werror -Wformat -Wno-unused-parameter
  19. -Wno-unused-function -Wno-unused-label -Wpointer-arith -Wreturn-type -Wsign-compare -Wmultichar
  20. -Wcast-qual -Wmissing-prototypes -Wstrict-prototypes # -Wshadow -Wconversion
  21. -Winit-self -Wuninitialized -Wformat-security -Wformat-nonliteral)
  22. # binary size reduction settings
  23. add_definitions(-ffunction-sections -fdata-sections -fno-stack-protector -fno-unwind-tables
  24. -fno-asynchronous-unwind-tables -fno-math-errno -fmerge-all-constants -fno-ident)
  25. set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,--gc-sections")
  26. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--gc-sections")
  27. set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "") # cmake sets -rdynamic by default
  28. if(NOT WIN32)
  29. add_definitions(-fvisibility=hidden -fPIC)
  30. set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,-z,norelro -Wl,--hash-style=gnu -Wl,--build-id=none")
  31. endif()
  32. if(CMAKE_BUILD_TYPE STREQUAL "MinSizeRel" OR CMAKE_BUILD_TYPE STREQUAL "Release")
  33. add_definitions(-flto)
  34. set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -flto -s")
  35. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -flto -s")
  36. endif()
  37. endif()
  38. # build the main library
  39. set(exported_headers ${PROJECT_BINARY_DIR}/src_generated/ua_config.h
  40. ${PROJECT_SOURCE_DIR}/include/ua_statuscodes.h
  41. ${PROJECT_SOURCE_DIR}/include/ua_types.h
  42. ${PROJECT_BINARY_DIR}/src_generated/ua_nodeids.h
  43. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.h
  44. ${PROJECT_SOURCE_DIR}/include/ua_connection.h
  45. ${PROJECT_SOURCE_DIR}/include/ua_log.h
  46. ${PROJECT_SOURCE_DIR}/include/ua_server.h
  47. ${PROJECT_SOURCE_DIR}/include/ua_client.h)
  48. set(internal_headers ${PROJECT_SOURCE_DIR}/src/ua_util.h
  49. ${PROJECT_SOURCE_DIR}/deps/queue.h
  50. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.h
  51. ${PROJECT_SOURCE_DIR}/src/ua_types_encoding_binary.h
  52. ${PROJECT_SOURCE_DIR}/src/ua_securechannel.h
  53. ${PROJECT_SOURCE_DIR}/src/ua_session.h
  54. ${PROJECT_SOURCE_DIR}/src/server/ua_nodes.h
  55. ${PROJECT_SOURCE_DIR}/src/server/ua_nodestore.h
  56. ${PROJECT_SOURCE_DIR}/src/server/ua_session_manager.h
  57. ${PROJECT_SOURCE_DIR}/src/server/ua_securechannel_manager.h
  58. ${PROJECT_SOURCE_DIR}/src/server/ua_server_internal.h
  59. ${PROJECT_SOURCE_DIR}/src/server/ua_services.h)
  60. set(lib_sources ${PROJECT_SOURCE_DIR}/src/ua_types.c
  61. ${PROJECT_SOURCE_DIR}/src/ua_types_encoding_binary.c
  62. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.c
  63. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.c
  64. ${PROJECT_SOURCE_DIR}/src/ua_connection.c
  65. ${PROJECT_SOURCE_DIR}/src/ua_securechannel.c
  66. ${PROJECT_SOURCE_DIR}/src/ua_session.c
  67. ${PROJECT_SOURCE_DIR}/src/server/ua_server.c
  68. ${PROJECT_SOURCE_DIR}/src/server/ua_server_addressspace.c
  69. ${PROJECT_SOURCE_DIR}/src/server/ua_server_binary.c
  70. ${PROJECT_SOURCE_DIR}/src/server/ua_nodes.c
  71. ${PROJECT_SOURCE_DIR}/src/server/ua_server_worker.c
  72. ${PROJECT_SOURCE_DIR}/src/server/ua_securechannel_manager.c
  73. ${PROJECT_SOURCE_DIR}/src/server/ua_session_manager.c
  74. ${PROJECT_SOURCE_DIR}/src/server/ua_services_discovery.c
  75. ${PROJECT_SOURCE_DIR}/src/server/ua_services_securechannel.c
  76. ${PROJECT_SOURCE_DIR}/src/server/ua_services_session.c
  77. ${PROJECT_SOURCE_DIR}/src/server/ua_services_attribute.c
  78. ${PROJECT_SOURCE_DIR}/src/server/ua_services_nodemanagement.c
  79. ${PROJECT_SOURCE_DIR}/src/server/ua_services_view.c
  80. ${PROJECT_SOURCE_DIR}/src/client/ua_client.c
  81. ${PROJECT_SOURCE_DIR}/examples/networklayer_tcp.c
  82. ${PROJECT_SOURCE_DIR}/examples/logger_stdout.c)
  83. ##TODO: make client stuff optional
  84. ## generate code from xml definitions
  85. file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/src_generated")
  86. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.c
  87. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.h
  88. PRE_BUILD
  89. COMMAND python ${PROJECT_SOURCE_DIR}/tools/generate_datatypes.py --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
  90. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_datatypes.py
  91. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/Opc.Ua.Types.bsd
  92. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/NodeIds.csv)
  93. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.c
  94. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.h
  95. PRE_BUILD
  96. COMMAND python ${PROJECT_SOURCE_DIR}/tools/generate_datatypes.py --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
  97. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_datatypes.py
  98. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/Custom.Opc.Ua.Transport.bsd)
  99. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_nodeids.h
  100. PRE_BUILD
  101. COMMAND python ${PROJECT_SOURCE_DIR}/tools/generate_nodeids.py ${PROJECT_SOURCE_DIR}/tools/schema/NodeIds.csv ${PROJECT_BINARY_DIR}/src_generated/ua_nodeids
  102. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_nodeids.py
  103. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/NodeIds.csv)
  104. ## logging
  105. set(UA_LOGLEVEL 300 CACHE STRING "Level at which logs shall be reported")
  106. ## multithreading
  107. option(ENABLE_MULTITHREADING "Enable multithreading" OFF)
  108. if(ENABLE_MULTITHREADING)
  109. add_definitions(-DUA_MULTITHREADING)
  110. find_package(Threads REQUIRED)
  111. list(APPEND lib_sources ${PROJECT_SOURCE_DIR}/src/server/ua_nodestore_concurrent.c)
  112. else()
  113. list(APPEND lib_sources ${PROJECT_SOURCE_DIR}/src/server/ua_nodestore.c)
  114. endif()
  115. ## set the precompiler flags
  116. configure_file("src/ua_config.h.in" "${PROJECT_BINARY_DIR}/src_generated/ua_config.h")
  117. ## extensions
  118. option(EXTENSION_UDP "Enable udp extension" OFF)
  119. if(EXTENSION_UDP)
  120. add_definitions(-DEXTENSION_STATELESS)
  121. message(STATUS "Extensions: enabling udp")
  122. add_definitions(-DEXTENSION_UDP)
  123. add_executable(exampleServerUDP $<TARGET_OBJECTS:open62541-object> examples/networklayer_udp.c examples/server_udp.c)
  124. target_link_libraries(exampleServerUDP rt)
  125. endif()
  126. option(EXTENSION_STATELESS "Enable stateless extension" OFF)
  127. if(EXTENSION_STATELESS)
  128. message(STATUS "Extensions: enabling stateless")
  129. add_definitions(-DEXTENSION_STATELESS)
  130. endif()
  131. option(BUILD_DEMO_NODESET "Create a demo node for every built-in datatype" OFF)
  132. if(BUILD_DEMO_NODESET)
  133. message(STATUS "Enabling demo nodeset")
  134. add_definitions(-DDEMO_NODESET)
  135. endif()
  136. option(ENABLE_AMALGAMATION "Concatenate the library to a single file open62541.h/.c" OFF)
  137. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/open62541.h ${PROJECT_BINARY_DIR}/open62541.c
  138. PRE_BUILD
  139. COMMAND python ${CMAKE_CURRENT_SOURCE_DIR}/tools/amalgamate.py ${CMAKE_CURRENT_BINARY_DIR}/open62541.h ${exported_headers}
  140. ${PROJECT_SOURCE_DIR}/examples/networklayer_tcp.h
  141. ${PROJECT_SOURCE_DIR}/examples/logger_stdout.h
  142. COMMAND python ${CMAKE_CURRENT_SOURCE_DIR}/tools/amalgamate.py ${CMAKE_CURRENT_BINARY_DIR}/open62541.c
  143. ${PROJECT_BINARY_DIR}/src_generated/ua_config.h ${internal_headers} ${PROJECT_SOURCE_DIR}/src/server/ua_nodestore_hash.inc ${lib_sources}
  144. ${PROJECT_SOURCE_DIR}/examples/networklayer_tcp.c
  145. ${PROJECT_SOURCE_DIR}/examples/logger_stdout.c
  146. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/amalgamate.py ${exported_headers} ${internal_headers}
  147. ${PROJECT_BINARY_DIR}/src_generated/ua_config.h ${PROJECT_SOURCE_DIR}/src/server/ua_nodestore_hash.inc ${lib_sources}
  148. ${PROJECT_SOURCE_DIR}/examples/networklayer_tcp.h ${PROJECT_SOURCE_DIR}/examples/logger_stdout.h
  149. ${PROJECT_SOURCE_DIR}/examples/networklayer_tcp.c ${PROJECT_SOURCE_DIR}/examples/logger_stdout.c)
  150. if(ENABLE_AMALGAMATION)
  151. add_custom_target(amalgamation ALL DEPENDS ${PROJECT_BINARY_DIR}/open62541.h ${PROJECT_BINARY_DIR}/open62541.c)
  152. add_library(open62541-object OBJECT ${PROJECT_BINARY_DIR}/open62541.c)
  153. include_directories(${PROJECT_BINARY_DIR})
  154. else()
  155. add_definitions(-DNOT_AMALGATED)
  156. add_library(open62541-object OBJECT ${lib_sources} ${internal_headers} ${exported_headers})
  157. include_directories(${PROJECT_SOURCE_DIR}/include)
  158. include_directories(${PROJECT_SOURCE_DIR}/deps)
  159. include_directories(${PROJECT_SOURCE_DIR}/src)
  160. include_directories(${PROJECT_BINARY_DIR}/src_generated)
  161. endif()
  162. add_library(open62541 SHARED $<TARGET_OBJECTS:open62541-object>)
  163. if(WIN32)
  164. target_link_libraries(open62541 ws2_32) #since networklayer_tcp is linked into the amalgate
  165. endif()
  166. # build language bindings for the library
  167. option(ENABLE_BINDING_LUA "Build Lua bindings" OFF)
  168. option(ENABLE_BINDING_PYTHON "Build Python bindings" OFF)
  169. if(ENABLE_BINDING_LUA OR ENABLE_BINDING_PYTHON)
  170. add_subdirectory(bindings)
  171. endif()
  172. # coverage
  173. option(ENABLE_COVERAGE "Enable gcov coverage" OFF)
  174. if(ENABLE_COVERAGE)
  175. message(STATUS "Enabling gcov support")
  176. set(CMAKE_BUILD_TYPE DEBUG)
  177. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
  178. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
  179. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
  180. endif()
  181. # build example server
  182. option(BUILD_EXAMPLESERVER "Build the example server" OFF)
  183. if(BUILD_EXAMPLESERVER)
  184. set(server_source $<TARGET_OBJECTS:open62541-object>)
  185. #if(NOT ENABLE_AMALGAMATION)
  186. # list(APPEND server_source examples/networklayer_tcp.c examples/logger_stdout.c)
  187. #endif()
  188. add_executable(exampleServer examples/server.c ${server_source})
  189. add_executable(exampleServer_simple examples/server_simple.c ${server_source})
  190. add_executable(exampleServer_so examples/server.c)
  191. target_link_libraries(exampleServer_so open62541)
  192. if(WIN32)
  193. target_link_libraries(exampleServer ws2_32)
  194. target_link_libraries(exampleServer_simple ws2_32)
  195. target_link_libraries(exampleServer_so ws2_32)
  196. else()
  197. target_link_libraries(exampleServer rt)
  198. target_link_libraries(exampleServer_simple rt)
  199. target_link_libraries(exampleServer_so rt)
  200. endif()
  201. if(ENABLE_MULTITHREADING)
  202. target_link_libraries(exampleServer urcu-cds urcu urcu-common pthread)
  203. target_link_libraries(exampleServer_simple urcu-cds urcu urcu-common pthread)
  204. target_link_libraries(exampleServer_so urcu-cds urcu urcu-common pthread)
  205. endif()
  206. endif()
  207. ## self-signed certificates
  208. option(GENERATE_SELFSIGNED "Generate self-signed certificates" OFF)
  209. if(GENERATE_SELFSIGNED)
  210. message(STATUS "Enabling self-signed certificates")
  211. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/localhost.der
  212. ${PROJECT_BINARY_DIR}/ca.crt
  213. COMMAND python ${PROJECT_SOURCE_DIR}/tools/certs/create_self-signed.py ${PROJECT_BINARY_DIR}
  214. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/certs/create_self-signed.py
  215. ${CMAKE_CURRENT_SOURCE_DIR}/tools/certs/localhost.cnf)
  216. add_custom_target(selfsigned ALL DEPENDS ${PROJECT_BINARY_DIR}/localhost.der ${PROJECT_BINARY_DIR}/ca.crt)
  217. endif()
  218. # build example client
  219. option(BUILD_EXAMPLECLIENT "Build a test client" OFF)
  220. if(BUILD_EXAMPLECLIENT)
  221. add_definitions(-DBENCHMARK)
  222. set(client_source $<TARGET_OBJECTS:open62541-object>)
  223. #if(NOT ENABLE_AMALGAMATION)
  224. # list(APPEND client_source examples/networklayer_tcp.c examples/logger_stdout.c)
  225. #endif()
  226. add_executable(exampleClient examples/client.c ${client_source})
  227. add_executable(exampleClient_so examples/client.c)
  228. target_link_libraries(exampleClient_so open62541)
  229. if(WIN32)
  230. target_link_libraries(exampleClient ws2_32)
  231. target_link_libraries(exampleClient_so ws2_32)
  232. else()
  233. target_link_libraries(exampleClient rt)
  234. target_link_libraries(exampleClient_so rt)
  235. endif()
  236. if(ENABLE_MULTITHREADING)
  237. target_link_libraries(exampleClient urcu-cds urcu urcu-common pthread)
  238. target_link_libraries(exampleClient_so urcu-cds urcu urcu-common pthread)
  239. endif()
  240. if(EXTENSION_STATELESS)
  241. add_executable(statelessClient examples/client_stateless.c ${client_source})
  242. if(ENABLE_MULTITHREADING)
  243. target_link_libraries(statelessClient urcu-cds urcu urcu-common pthread)
  244. endif()
  245. endif()
  246. endif()
  247. # build unit tests
  248. option(BUILD_UNIT_TESTS "Run unit tests after building" OFF)
  249. if(BUILD_UNIT_TESTS)
  250. enable_testing()
  251. add_subdirectory(tests)
  252. endif()
  253. # build documentation
  254. option(BUILD_DOCUMENTATION "Generate doxygen documentation" OFF)
  255. if(BUILD_DOCUMENTATION)
  256. find_package(Doxygen)
  257. if(NOT DOXYGEN_FOUND)
  258. message(FATAL_ERROR "Doxygen is not installed or not properly configured")
  259. endif(NOT DOXYGEN_FOUND)
  260. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/doc/Doxyfile.in ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY)
  261. add_custom_target(doc
  262. ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
  263. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  264. COMMENT "Generating API documentation with Doxygen")
  265. endif()