CMakeLists.txt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. ${PROJECT_SOURCE_DIR}/examples/networklayer_tcp.h
  49. ${PROJECT_SOURCE_DIR}/examples/logger_stdout.h)
  50. set(internal_headers ${PROJECT_SOURCE_DIR}/src/ua_util.h
  51. ${PROJECT_SOURCE_DIR}/deps/queue.h
  52. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.h
  53. ${PROJECT_SOURCE_DIR}/src/ua_types_encoding_binary.h
  54. ${PROJECT_SOURCE_DIR}/src/ua_securechannel.h
  55. ${PROJECT_SOURCE_DIR}/src/ua_session.h
  56. ${PROJECT_SOURCE_DIR}/src/server/ua_nodes.h
  57. ${PROJECT_SOURCE_DIR}/src/server/ua_nodestore.h
  58. ${PROJECT_SOURCE_DIR}/src/server/ua_session_manager.h
  59. ${PROJECT_SOURCE_DIR}/src/server/ua_securechannel_manager.h
  60. ${PROJECT_SOURCE_DIR}/src/server/ua_server_internal.h
  61. ${PROJECT_SOURCE_DIR}/src/server/ua_services.h)
  62. set(lib_sources ${PROJECT_SOURCE_DIR}/src/ua_types.c
  63. ${PROJECT_SOURCE_DIR}/src/ua_types_encoding_binary.c
  64. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.c
  65. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.c
  66. ${PROJECT_SOURCE_DIR}/src/ua_connection.c
  67. ${PROJECT_SOURCE_DIR}/src/ua_securechannel.c
  68. ${PROJECT_SOURCE_DIR}/src/ua_session.c
  69. ${PROJECT_SOURCE_DIR}/src/server/ua_server.c
  70. ${PROJECT_SOURCE_DIR}/src/server/ua_server_addressspace.c
  71. ${PROJECT_SOURCE_DIR}/src/server/ua_server_binary.c
  72. ${PROJECT_SOURCE_DIR}/src/server/ua_nodes.c
  73. ${PROJECT_SOURCE_DIR}/src/server/ua_server_worker.c
  74. ${PROJECT_SOURCE_DIR}/src/server/ua_securechannel_manager.c
  75. ${PROJECT_SOURCE_DIR}/src/server/ua_session_manager.c
  76. ${PROJECT_SOURCE_DIR}/src/server/ua_services_discovery.c
  77. ${PROJECT_SOURCE_DIR}/src/server/ua_services_securechannel.c
  78. ${PROJECT_SOURCE_DIR}/src/server/ua_services_session.c
  79. ${PROJECT_SOURCE_DIR}/src/server/ua_services_attribute.c
  80. ${PROJECT_SOURCE_DIR}/src/server/ua_services_nodemanagement.c
  81. ${PROJECT_SOURCE_DIR}/src/server/ua_services_view.c
  82. ${PROJECT_SOURCE_DIR}/src/client/ua_client.c
  83. ${PROJECT_SOURCE_DIR}/examples/networklayer_tcp.c
  84. ${PROJECT_SOURCE_DIR}/examples/logger_stdout.c)
  85. ##TODO: make client stuff optional
  86. ## generate code from xml definitions
  87. file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/src_generated")
  88. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.c
  89. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.h
  90. PRE_BUILD
  91. 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
  92. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_datatypes.py
  93. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/Opc.Ua.Types.bsd
  94. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/NodeIds.csv)
  95. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.c
  96. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.h
  97. PRE_BUILD
  98. 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
  99. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_datatypes.py
  100. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/Custom.Opc.Ua.Transport.bsd)
  101. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_nodeids.h
  102. PRE_BUILD
  103. COMMAND python ${PROJECT_SOURCE_DIR}/tools/generate_nodeids.py ${PROJECT_SOURCE_DIR}/tools/schema/NodeIds.csv ${PROJECT_BINARY_DIR}/src_generated/ua_nodeids
  104. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_nodeids.py
  105. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/NodeIds.csv)
  106. ## logging
  107. set(UA_LOGLEVEL 300 CACHE STRING "Level at which logs shall be reported")
  108. ## multithreading
  109. option(ENABLE_MULTITHREADING "Enable multithreading" OFF)
  110. if(ENABLE_MULTITHREADING)
  111. add_definitions(-DUA_MULTITHREADING)
  112. find_package(Threads REQUIRED)
  113. list(APPEND lib_sources ${PROJECT_SOURCE_DIR}/src/server/ua_nodestore_concurrent.c)
  114. else()
  115. list(APPEND lib_sources ${PROJECT_SOURCE_DIR}/src/server/ua_nodestore.c)
  116. endif()
  117. ## set the precompiler flags
  118. configure_file("src/ua_config.h.in" "${PROJECT_BINARY_DIR}/src_generated/ua_config.h")
  119. ## extensions
  120. option(EXTENSION_UDP "Enable udp extension" OFF)
  121. if(EXTENSION_UDP)
  122. add_definitions(-DEXTENSION_STATELESS)
  123. message(STATUS "Extensions: enabling udp")
  124. add_definitions(-DEXTENSION_UDP)
  125. add_executable(exampleServerUDP $<TARGET_OBJECTS:open62541-object> examples/networklayer_udp.c examples/server_udp.c)
  126. target_link_libraries(exampleServerUDP rt)
  127. endif()
  128. option(EXTENSION_STATELESS "Enable stateless extension" OFF)
  129. if(EXTENSION_STATELESS)
  130. message(STATUS "Extensions: enabling stateless")
  131. add_definitions(-DEXTENSION_STATELESS)
  132. endif()
  133. option(BUILD_DEMO_NODESET "Create a demo node for every built-in datatype" OFF)
  134. if(BUILD_DEMO_NODESET)
  135. message(STATUS "Enabling demo nodeset")
  136. add_definitions(-DDEMO_NODESET)
  137. endif()
  138. option(ENABLE_AMALGAMATION "Concatenate the library to a single file open62541.h/.c" OFF)
  139. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/open62541.h ${PROJECT_BINARY_DIR}/open62541.c
  140. PRE_BUILD
  141. COMMAND python ${CMAKE_CURRENT_SOURCE_DIR}/tools/amalgamate.py ${CMAKE_CURRENT_BINARY_DIR}/open62541.h ${exported_headers}
  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. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/amalgamate.py ${exported_headers} ${internal_headers}
  145. ${PROJECT_BINARY_DIR}/src_generated/ua_config.h ${PROJECT_SOURCE_DIR}/src/server/ua_nodestore_hash.inc ${lib_sources})
  146. if(ENABLE_AMALGAMATION)
  147. add_custom_target(amalgamation ALL DEPENDS ${PROJECT_BINARY_DIR}/open62541.h ${PROJECT_BINARY_DIR}/open62541.c)
  148. add_library(open62541-object OBJECT ${PROJECT_BINARY_DIR}/open62541.c)
  149. include_directories(${PROJECT_BINARY_DIR})
  150. else()
  151. add_definitions(-DNOT_AMALGATED)
  152. add_library(open62541-object OBJECT ${lib_sources} ${internal_headers} ${exported_headers})
  153. include_directories(${PROJECT_SOURCE_DIR}/include)
  154. include_directories(${PROJECT_SOURCE_DIR}/deps)
  155. include_directories(${PROJECT_SOURCE_DIR}/src)
  156. include_directories(${PROJECT_BINARY_DIR}/src_generated)
  157. endif()
  158. target_compile_definitions(open62541-object PRIVATE -DUA_DYNAMIC_LINKING)
  159. add_library(open62541 SHARED $<TARGET_OBJECTS:open62541-object>)
  160. target_compile_definitions(open62541 PRIVATE -DUA_DYNAMIC_LINKING)
  161. if(WIN32)
  162. target_link_libraries(open62541 ws2_32) #since networklayer_tcp is linked into the amalgate
  163. endif()
  164. # build language bindings for the library
  165. option(ENABLE_BINDING_LUA "Build Lua bindings" OFF)
  166. option(ENABLE_BINDING_PYTHON "Build Python bindings" OFF)
  167. if(ENABLE_BINDING_LUA OR ENABLE_BINDING_PYTHON)
  168. add_subdirectory(bindings)
  169. endif()
  170. # coverage
  171. option(ENABLE_COVERAGE "Enable gcov coverage" OFF)
  172. if(ENABLE_COVERAGE)
  173. message(STATUS "Enabling gcov support")
  174. set(CMAKE_BUILD_TYPE DEBUG)
  175. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
  176. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
  177. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
  178. endif()
  179. # build example server
  180. option(BUILD_EXAMPLESERVER "Build the example server" OFF)
  181. if(BUILD_EXAMPLESERVER)
  182. set(server_source $<TARGET_OBJECTS:open62541-object>)
  183. add_executable(exampleServer_static examples/server.c ${server_source})
  184. add_executable(exampleServer_simple examples/server_simple.c ${server_source})
  185. add_executable(exampleServer examples/server.c)
  186. target_link_libraries(exampleServer open62541)
  187. if(WIN32)
  188. target_link_libraries(exampleServer_static ws2_32)
  189. target_link_libraries(exampleServer_simple ws2_32)
  190. target_link_libraries(exampleServer ws2_32)
  191. else()
  192. target_link_libraries(exampleServer_static rt)
  193. target_link_libraries(exampleServer_simple rt)
  194. target_link_libraries(exampleServer rt)
  195. endif()
  196. if(ENABLE_MULTITHREADING)
  197. target_link_libraries(exampleServer_static urcu-cds urcu urcu-common pthread)
  198. target_link_libraries(exampleServer_simple urcu-cds urcu urcu-common pthread)
  199. target_link_libraries(exampleServer urcu-cds urcu urcu-common pthread)
  200. endif()
  201. endif()
  202. ## self-signed certificates
  203. option(GENERATE_SELFSIGNED "Generate self-signed certificates" OFF)
  204. if(GENERATE_SELFSIGNED)
  205. message(STATUS "Enabling self-signed certificates")
  206. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/localhost.der
  207. ${PROJECT_BINARY_DIR}/ca.crt
  208. COMMAND python ${PROJECT_SOURCE_DIR}/tools/certs/create_self-signed.py ${PROJECT_BINARY_DIR}
  209. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/certs/create_self-signed.py
  210. ${CMAKE_CURRENT_SOURCE_DIR}/tools/certs/localhost.cnf)
  211. add_custom_target(selfsigned ALL DEPENDS ${PROJECT_BINARY_DIR}/localhost.der ${PROJECT_BINARY_DIR}/ca.crt)
  212. endif()
  213. # build example client
  214. option(BUILD_EXAMPLECLIENT "Build a test client" OFF)
  215. if(BUILD_EXAMPLECLIENT)
  216. add_definitions(-DBENCHMARK)
  217. set(client_source $<TARGET_OBJECTS:open62541-object>)
  218. #if(NOT ENABLE_AMALGAMATION)
  219. # list(APPEND client_source examples/networklayer_tcp.c examples/logger_stdout.c)
  220. #endif()
  221. add_executable(exampleClient_static examples/client.c ${client_source})
  222. add_executable(exampleClient examples/client.c)
  223. target_link_libraries(exampleClient open62541)
  224. if(WIN32)
  225. target_link_libraries(exampleClient_static ws2_32)
  226. target_link_libraries(exampleClient ws2_32)
  227. else()
  228. target_link_libraries(exampleClient_static rt)
  229. target_link_libraries(exampleClient rt)
  230. endif()
  231. if(ENABLE_MULTITHREADING)
  232. target_link_libraries(exampleClient_static urcu-cds urcu urcu-common pthread)
  233. target_link_libraries(exampleClient urcu-cds urcu urcu-common pthread)
  234. endif()
  235. if(EXTENSION_STATELESS)
  236. add_executable(statelessClient examples/client_stateless.c ${client_source})
  237. if(ENABLE_MULTITHREADING)
  238. target_link_libraries(statelessClient urcu-cds urcu urcu-common pthread)
  239. endif()
  240. endif()
  241. endif()
  242. # build unit tests
  243. option(BUILD_UNIT_TESTS "Run unit tests after building" OFF)
  244. if(BUILD_UNIT_TESTS)
  245. enable_testing()
  246. add_subdirectory(tests)
  247. endif()
  248. # build documentation
  249. option(BUILD_DOCUMENTATION "Generate doxygen documentation" OFF)
  250. if(BUILD_DOCUMENTATION)
  251. find_package(Doxygen)
  252. if(NOT DOXYGEN_FOUND)
  253. message(FATAL_ERROR "Doxygen is not installed or not properly configured")
  254. endif(NOT DOXYGEN_FOUND)
  255. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/doc/Doxyfile.in ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY)
  256. add_custom_target(doc
  257. ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
  258. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  259. COMMENT "Generating API documentation with Doxygen")
  260. endif()