CMakeLists.txt 14 KB

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