CMakeLists.txt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. ##TODO: make client stuff optional
  82. ## generate code from xml definitions
  83. file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/src_generated")
  84. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.c
  85. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.h
  86. PRE_BUILD
  87. 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
  88. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_datatypes.py
  89. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/Opc.Ua.Types.bsd
  90. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/NodeIds.csv)
  91. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.c
  92. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.h
  93. PRE_BUILD
  94. 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
  95. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_datatypes.py
  96. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/Custom.Opc.Ua.Transport.bsd)
  97. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_nodeids.h
  98. PRE_BUILD
  99. COMMAND python ${PROJECT_SOURCE_DIR}/tools/generate_nodeids.py ${PROJECT_SOURCE_DIR}/tools/schema/NodeIds.csv ${PROJECT_BINARY_DIR}/src_generated/ua_nodeids
  100. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_nodeids.py
  101. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/NodeIds.csv)
  102. ## logging
  103. set(UA_LOGLEVEL 300 CACHE STRING "Level at which logs shall be reported")
  104. ## multithreading
  105. option(ENABLE_MULTITHREADING "Enable multithreading" OFF)
  106. if(ENABLE_MULTITHREADING)
  107. add_definitions(-DUA_MULTITHREADING)
  108. find_package(Threads REQUIRED)
  109. list(APPEND lib_sources ${PROJECT_SOURCE_DIR}/src/server/ua_nodestore_concurrent.c)
  110. else()
  111. list(APPEND lib_sources ${PROJECT_SOURCE_DIR}/src/server/ua_nodestore.c)
  112. endif()
  113. ## set the precompiler flags
  114. configure_file("src/ua_config.h.in" "${PROJECT_BINARY_DIR}/src_generated/ua_config.h")
  115. ## extensions
  116. option(EXTENSION_UDP "Enable udp extension" OFF)
  117. if(EXTENSION_UDP)
  118. add_definitions(-DEXTENSION_STATELESS)
  119. message(STATUS "Extensions: enabling udp")
  120. add_definitions(-DEXTENSION_UDP)
  121. add_executable(exampleServerUDP $<TARGET_OBJECTS:open62541-object> examples/networklayer_udp.c examples/server_udp.c)
  122. target_link_libraries(exampleServerUDP rt)
  123. endif()
  124. option(EXTENSION_STATELESS "Enable stateless extension" OFF)
  125. if(EXTENSION_STATELESS)
  126. message(STATUS "Extensions: enabling stateless")
  127. add_definitions(-DEXTENSION_STATELESS)
  128. endif()
  129. option(BUILD_DEMO_NODESET "Create a demo node for every built-in datatype" OFF)
  130. if(BUILD_DEMO_NODESET)
  131. message(STATUS "Enabling demo nodeset")
  132. add_definitions(-DDEMO_NODESET)
  133. endif()
  134. option(ENABLE_AMALGAMATION "Concatenate the library to a single file open62541.h/.c" OFF)
  135. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/open62541.h ${PROJECT_BINARY_DIR}/open62541.c
  136. PRE_BUILD
  137. COMMAND python ${CMAKE_CURRENT_SOURCE_DIR}/tools/amalgamate.py ${CMAKE_CURRENT_BINARY_DIR}/open62541.h ${exported_headers}
  138. ${PROJECT_SOURCE_DIR}/examples/networklayer_tcp.h
  139. ${PROJECT_SOURCE_DIR}/examples/logger_stdout.h
  140. COMMAND python ${CMAKE_CURRENT_SOURCE_DIR}/tools/amalgamate.py ${CMAKE_CURRENT_BINARY_DIR}/open62541.c
  141. ${PROJECT_BINARY_DIR}/src_generated/ua_config.h ${internal_headers} ${PROJECT_SOURCE_DIR}/src/server/ua_nodestore_hash.inc ${lib_sources}
  142. ${PROJECT_SOURCE_DIR}/examples/networklayer_tcp.c
  143. ${PROJECT_SOURCE_DIR}/examples/logger_stdout.c
  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. ${PROJECT_SOURCE_DIR}/examples/networklayer_tcp.h ${PROJECT_SOURCE_DIR}/examples/logger_stdout.h
  147. ${PROJECT_SOURCE_DIR}/examples/networklayer_tcp.c ${PROJECT_SOURCE_DIR}/examples/logger_stdout.c)
  148. if(ENABLE_AMALGAMATION)
  149. add_custom_target(amalgamation ALL DEPENDS ${PROJECT_BINARY_DIR}/open62541.h ${PROJECT_BINARY_DIR}/open62541.c)
  150. add_library(open62541-object OBJECT ${PROJECT_BINARY_DIR}/open62541.c)
  151. include_directories(${PROJECT_BINARY_DIR})
  152. else()
  153. add_definitions(-DNOT_AMALGATED)
  154. add_library(open62541-object OBJECT ${lib_sources} ${internal_headers} ${exported_headers})
  155. include_directories(${PROJECT_SOURCE_DIR}/include)
  156. include_directories(${PROJECT_SOURCE_DIR}/deps)
  157. include_directories(${PROJECT_SOURCE_DIR}/src)
  158. include_directories(${PROJECT_BINARY_DIR}/src_generated)
  159. endif()
  160. add_library(open62541 SHARED $<TARGET_OBJECTS:open62541-object>)
  161. if(WIN32 AND ENABLE_AMALGAMATION)
  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. if(NOT ENABLE_AMALGAMATION)
  184. list(APPEND server_source examples/networklayer_tcp.c examples/logger_stdout.c)
  185. endif()
  186. add_executable(exampleServer examples/server.c ${server_source})
  187. add_executable(exampleServer_simple examples/server_simple.c ${server_source})
  188. if(WIN32)
  189. target_link_libraries(exampleServer ws2_32)
  190. target_link_libraries(exampleServer_simple ws2_32)
  191. else()
  192. target_link_libraries(exampleServer rt)
  193. target_link_libraries(exampleServer_simple rt)
  194. endif()
  195. if(ENABLE_MULTITHREADING)
  196. target_link_libraries(exampleServer urcu-cds urcu urcu-common pthread)
  197. target_link_libraries(exampleServer_simple urcu-cds urcu urcu-common pthread)
  198. endif()
  199. endif()
  200. ## self-signed certificates
  201. option(GENERATE_SELFSIGNED "Generate self-signed certificates" OFF)
  202. if(GENERATE_SELFSIGNED)
  203. message(STATUS "Enabling self-signed certificates")
  204. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/localhost.der
  205. ${PROJECT_BINARY_DIR}/ca.crt
  206. COMMAND python ${PROJECT_SOURCE_DIR}/tools/certs/create_self-signed.py ${PROJECT_BINARY_DIR}
  207. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/certs/create_self-signed.py
  208. ${CMAKE_CURRENT_SOURCE_DIR}/tools/certs/localhost.cnf)
  209. add_custom_target(selfsigned ALL DEPENDS ${PROJECT_BINARY_DIR}/localhost.der ${PROJECT_BINARY_DIR}/ca.crt)
  210. endif()
  211. # build example client
  212. option(BUILD_EXAMPLECLIENT "Build a test client" OFF)
  213. if(BUILD_EXAMPLECLIENT)
  214. add_definitions(-DBENCHMARK)
  215. set(client_source $<TARGET_OBJECTS:open62541-object>)
  216. if(NOT ENABLE_AMALGAMATION)
  217. list(APPEND client_source examples/networklayer_tcp.c examples/logger_stdout.c)
  218. endif()
  219. add_executable(exampleClient examples/client.c ${client_source})
  220. if(WIN32)
  221. target_link_libraries(exampleClient ws2_32)
  222. else()
  223. target_link_libraries(exampleClient rt)
  224. endif()
  225. if(ENABLE_MULTITHREADING)
  226. target_link_libraries(exampleClient urcu-cds urcu urcu-common pthread)
  227. endif()
  228. if(EXTENSION_STATELESS)
  229. add_executable(statelessClient examples/client_stateless.c ${client_source})
  230. if(ENABLE_MULTITHREADING)
  231. target_link_libraries(statelessClient urcu-cds urcu urcu-common pthread)
  232. endif()
  233. endif()
  234. endif()
  235. # build unit tests
  236. option(BUILD_UNIT_TESTS "Run unit tests after building" OFF)
  237. if(BUILD_UNIT_TESTS)
  238. enable_testing()
  239. add_subdirectory(tests)
  240. endif()
  241. # build documentation
  242. option(BUILD_DOCUMENTATION "Generate doxygen documentation" OFF)
  243. if(BUILD_DOCUMENTATION)
  244. find_package(Doxygen)
  245. if(NOT DOXYGEN_FOUND)
  246. message(FATAL_ERROR "Doxygen is not installed or not properly configured")
  247. endif(NOT DOXYGEN_FOUND)
  248. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/doc/Doxyfile.in ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY)
  249. add_custom_target(doc
  250. ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
  251. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  252. COMMENT "Generating API documentation with Doxygen")
  253. endif()