CMakeLists.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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
  19. -Wno-unused-parameter -Wno-unused-function -Wno-unused-label -Wpointer-arith -Wreturn-type -Wsign-compare -Wmultichar
  20. -Wcast-qual -Wmissing-prototypes -Wstrict-prototypes # -Wconversion -Wshadow
  21. -Winit-self -Wuninitialized -Wno-deprecated -Wformat-security -ffunction-sections -fdata-sections)
  22. if(NOT "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
  23. add_definitions(-Wformat-nonliteral)
  24. set (CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,--gc-sections")
  25. set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--gc-sections")
  26. else()
  27. add_definitions(-Wno-gnu-statement-expression)
  28. endif()
  29. if(NOT WIN32)
  30. add_definitions(-fstack-protector -fPIC -fvisibility=hidden)
  31. endif()
  32. endif()
  33. # build the main library
  34. set(exported_headers ${PROJECT_BINARY_DIR}/src_generated/ua_config.h
  35. ${PROJECT_SOURCE_DIR}/include/ua_statuscodes.h
  36. ${PROJECT_SOURCE_DIR}/include/ua_types.h
  37. ${PROJECT_BINARY_DIR}/src_generated/ua_nodeids.h
  38. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.h
  39. ${PROJECT_SOURCE_DIR}/include/ua_connection.h
  40. ${PROJECT_SOURCE_DIR}/include/ua_log.h
  41. ${PROJECT_SOURCE_DIR}/include/ua_server.h)
  42. set(internal_headers ${PROJECT_SOURCE_DIR}/src/ua_util.h
  43. ${PROJECT_SOURCE_DIR}/deps/queue.h
  44. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.h
  45. ${PROJECT_SOURCE_DIR}/src/ua_securechannel.h
  46. ${PROJECT_SOURCE_DIR}/src/ua_session.h
  47. ${PROJECT_SOURCE_DIR}/src/ua_types_encoding_binary.h
  48. ${PROJECT_SOURCE_DIR}/src/server/ua_nodes.h
  49. ${PROJECT_SOURCE_DIR}/src/server/ua_nodestore.h
  50. ${PROJECT_SOURCE_DIR}/src/server/ua_session_manager.h
  51. ${PROJECT_SOURCE_DIR}/src/server/ua_securechannel_manager.h
  52. ${PROJECT_SOURCE_DIR}/src/server/ua_server_internal.h
  53. ${PROJECT_SOURCE_DIR}/src/server/ua_services.h)
  54. set(lib_sources ${PROJECT_SOURCE_DIR}/src/ua_types.c
  55. ${PROJECT_SOURCE_DIR}/src/ua_types_encoding_binary.c
  56. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.c
  57. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.c
  58. ${PROJECT_SOURCE_DIR}/src/ua_securechannel.c
  59. ${PROJECT_SOURCE_DIR}/src/ua_session.c
  60. ${PROJECT_SOURCE_DIR}/src/server/ua_server.c
  61. ${PROJECT_SOURCE_DIR}/src/server/ua_server_addressspace.c
  62. ${PROJECT_SOURCE_DIR}/src/server/ua_server_binary.c
  63. ${PROJECT_SOURCE_DIR}/src/server/ua_nodes.c
  64. ${PROJECT_SOURCE_DIR}/src/server/ua_server_worker.c
  65. ${PROJECT_SOURCE_DIR}/src/server/ua_securechannel_manager.c
  66. ${PROJECT_SOURCE_DIR}/src/server/ua_session_manager.c
  67. ${PROJECT_SOURCE_DIR}/src/server/ua_services_discovery.c
  68. ${PROJECT_SOURCE_DIR}/src/server/ua_services_securechannel.c
  69. ${PROJECT_SOURCE_DIR}/src/server/ua_services_session.c
  70. ${PROJECT_SOURCE_DIR}/src/server/ua_services_attribute.c
  71. ${PROJECT_SOURCE_DIR}/src/server/ua_services_nodemanagement.c
  72. ${PROJECT_SOURCE_DIR}/src/server/ua_services_view.c)
  73. ## generate code from xml definitions
  74. file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/src_generated")
  75. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.c
  76. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.h
  77. PRE_BUILD
  78. 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
  79. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_datatypes.py
  80. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/Opc.Ua.Types.bsd
  81. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/NodeIds.csv)
  82. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.c
  83. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.h
  84. PRE_BUILD
  85. 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
  86. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_datatypes.py
  87. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/Custom.Opc.Ua.Transport.bsd)
  88. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_nodeids.h
  89. PRE_BUILD
  90. COMMAND python ${PROJECT_SOURCE_DIR}/tools/generate_nodeids.py ${PROJECT_SOURCE_DIR}/tools/schema/NodeIds.csv ${PROJECT_BINARY_DIR}/src_generated/ua_nodeids
  91. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_nodeids.py
  92. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/NodeIds.csv)
  93. ## logging
  94. set(UA_LOGLEVEL 300 CACHE STRING "Level at which logs shall be reported")
  95. ## multithreading
  96. option(ENABLE_MULTITHREADING "Enable multithreading" OFF)
  97. if(ENABLE_MULTITHREADING)
  98. set(UA_MULTITHREADING ON)
  99. find_package(Threads REQUIRED)
  100. list(APPEND lib_sources ${PROJECT_SOURCE_DIR}/src/server/ua_nodestore_concurrent.c)
  101. else()
  102. list(APPEND lib_sources ${PROJECT_SOURCE_DIR}/src/server/ua_nodestore.c)
  103. endif()
  104. ## set the precompiler flags
  105. configure_file("src/ua_config.h.in" "${PROJECT_BINARY_DIR}/src_generated/ua_config.h")
  106. ## extensions
  107. option(EXTENSION_UDP "Enable udp extension" OFF)
  108. if(EXTENSION_UDP)
  109. set(EXTENSION_STATELESS ON)
  110. message(STATUS "Extensions: enabling udp")
  111. add_definitions(-DEXTENSION_UDP)
  112. endif()
  113. option(EXTENSION_STATELESS "Enable stateless extension" OFF)
  114. if(EXTENSION_STATELESS)
  115. message(STATUS "Extensions: enabling stateless")
  116. add_definitions(-DEXTENSION_STATELESS)
  117. endif()
  118. option(BUILD_DEMO_NODESET "Create a demo node for every built-in datatype" OFF)
  119. if(BUILD_DEMO_NODESET)
  120. message(STATUS "Enabling demo nodeset")
  121. add_definitions(-DDEMO_NODESET)
  122. endif()
  123. if(CMAKE_BUILD_TYPE STREQUAL "MinSizeRel" OR CMAKE_BUILD_TYPE STREQUAL "Release")
  124. ## build amalgamated source files
  125. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/open62541.h ${PROJECT_BINARY_DIR}/open62541.c
  126. PRE_BUILD
  127. COMMAND python ${CMAKE_CURRENT_SOURCE_DIR}/tools/amalgamate.py ${CMAKE_CURRENT_BINARY_DIR}/open62541.h ${exported_headers}
  128. COMMAND python ${CMAKE_CURRENT_SOURCE_DIR}/tools/amalgamate.py ${CMAKE_CURRENT_BINARY_DIR}/open62541.c
  129. ${PROJECT_BINARY_DIR}/src_generated/ua_config.h ${internal_headers} ${PROJECT_SOURCE_DIR}/src/server/ua_nodestore_hash.inc ${lib_sources}
  130. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/amalgamate.py ${exported_headers} ${internal_headers}
  131. ${PROJECT_BINARY_DIR}/src_generated/ua_config.h ${PROJECT_SOURCE_DIR}/src/server/ua_nodestore_hash.inc ${lib_sources})
  132. add_library(open62541-object OBJECT ${PROJECT_BINARY_DIR}/open62541.c)
  133. add_library(open62541 SHARED $<TARGET_OBJECTS:open62541-object>)
  134. else()
  135. ##compile from individual c files
  136. add_library(open62541-object OBJECT ${lib_sources})
  137. include_directories(${PROJECT_SOURCE_DIR}/include)
  138. include_directories(${PROJECT_SOURCE_DIR}/deps)
  139. include_directories(${PROJECT_SOURCE_DIR}/src)
  140. include_directories(${PROJECT_BINARY_DIR}/src_generated)
  141. endif()
  142. # build language bindings for the library
  143. option(ENABLE_BINDING_LUA "Build Lua bindings" OFF)
  144. option(ENABLE_BINDING_PYTHON "Build Python bindings" OFF)
  145. if(ENABLE_BINDING_LUA OR ENABLE_BINDING_PYTHON)
  146. add_subdirectory(bindings)
  147. endif()
  148. # coverage
  149. option(ENABLE_COVERAGE "Enable gcov coverage" OFF)
  150. if(ENABLE_COVERAGE)
  151. message(STATUS "Enabling gcov support")
  152. set(CMAKE_BUILD_TYPE DEBUG)
  153. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
  154. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
  155. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
  156. endif()
  157. # build example server
  158. option(BUILD_EXAMPLESERVER "Build the example server" OFF)
  159. if(BUILD_EXAMPLESERVER)
  160. add_executable(exampleServer examples/server.c $<TARGET_OBJECTS:open62541-object> examples/networklayer_tcp.c examples/logger_stdout.c)
  161. add_executable(exampleServer_datasource examples/server_datasource.c $<TARGET_OBJECTS:open62541-object> examples/networklayer_tcp.c examples/logger_stdout.c)
  162. target_include_directories(exampleServer PRIVATE ${PROJECT_BINARY_DIR})
  163. target_include_directories(exampleServer_datasource PRIVATE ${PROJECT_BINARY_DIR})
  164. if(WIN32)
  165. target_link_libraries(exampleServer ws2_32)
  166. target_link_libraries(exampleServer_datasource ws2_32)
  167. else()
  168. target_link_libraries(exampleServer rt)
  169. target_link_libraries(exampleServer_datasource rt)
  170. endif()
  171. if(ENABLE_MULTITHREADING)
  172. target_link_libraries(exampleServer urcu-cds urcu urcu-common pthread)
  173. target_link_libraries(exampleServer_datasource urcu-cds urcu urcu-common pthread)
  174. endif()
  175. if((CMAKE_COMPILER_IS_GNUCC OR "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang") AND (CMAKE_BUILD_TYPE STREQUAL "MinSizeRel" OR CMAKE_BUILD_TYPE STREQUAL "Release"))
  176. add_custom_command(TARGET exampleServer POST_BUILD COMMAND ${CMAKE_STRIP} $<TARGET_FILE:exampleServer>)
  177. add_custom_command(TARGET exampleServer_datasource POST_BUILD COMMAND ${CMAKE_STRIP} $<TARGET_FILE:exampleServer_datasource>)
  178. endif()
  179. endif()
  180. ## self-signed certificates
  181. option(GENERATE_SELFSIGNED "Generate self-signed certificates" OFF)
  182. if(GENERATE_SELFSIGNED)
  183. message(STATUS "Enabling self-signed certificates")
  184. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/localhost.der
  185. ${PROJECT_BINARY_DIR}/ca.crt
  186. COMMAND python ${PROJECT_SOURCE_DIR}/tools/certs/create_self-signed.py ${PROJECT_BINARY_DIR}
  187. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/certs/create_self-signed.py
  188. ${CMAKE_CURRENT_SOURCE_DIR}/tools/certs/localhost.cnf)
  189. add_custom_target(selfsigned ALL DEPENDS ${PROJECT_BINARY_DIR}/localhost.der ${PROJECT_BINARY_DIR}/ca.crt)
  190. endif()
  191. # build example client
  192. option(BUILD_EXAMPLECLIENT "Build a test client" OFF)
  193. if(BUILD_EXAMPLECLIENT)
  194. message(STATUS "Extensions: enabling client")
  195. add_definitions( -DBENCHMARK=1 )
  196. add_executable(exampleClient ${PROJECT_BINARY_DIR}/open62541.c examples/client.c)
  197. target_include_directories(exampleClient PRIVATE ${PROJECT_BINARY_DIR})
  198. if(ENABLE_MULTITHREADING)
  199. target_link_libraries(exampleClient urcu-cds urcu urcu-common pthread)
  200. endif()
  201. if ((CMAKE_COMPILER_IS_GNUCC OR "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang") AND (CMAKE_BUILD_TYPE STREQUAL "MinSizeRel" OR CMAKE_BUILD_TYPE STREQUAL "Release"))
  202. add_custom_command(TARGET exampleClient POST_BUILD COMMAND ${CMAKE_STRIP} $<TARGET_FILE:exampleClient>)
  203. endif()
  204. if(EXTENSION_STATELESS)
  205. add_executable(statelessClient ${PROJECT_BINARY_DIR}/open62541.c examples/client_stateless.c)
  206. target_include_directories(statelessClient PRIVATE ${PROJECT_BINARY_DIR})
  207. if(ENABLE_MULTITHREADING)
  208. target_link_libraries(statelessClient urcu-cds urcu urcu-common pthread)
  209. endif()
  210. if ((CMAKE_COMPILER_IS_GNUCC OR "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang") AND (CMAKE_BUILD_TYPE STREQUAL "MinSizeRel" OR CMAKE_BUILD_TYPE STREQUAL "Release"))
  211. add_custom_command(TARGET statelessClient POST_BUILD COMMAND ${CMAKE_STRIP} $<TARGET_FILE:statelessClient>)
  212. endif()
  213. endif()
  214. endif()
  215. # build unit tests
  216. option(BUILD_UNIT_TESTS "Run unit tests after building" OFF)
  217. if(BUILD_UNIT_TESTS)
  218. enable_testing()
  219. add_subdirectory(tests)
  220. endif()
  221. # build documentation
  222. option(BUILD_DOCUMENTATION "Generate doxygen documentation" OFF)
  223. if(BUILD_DOCUMENTATION)
  224. find_package(Doxygen)
  225. if(NOT DOXYGEN_FOUND)
  226. message(FATAL_ERROR "Doxygen is not installed or not properly configured")
  227. endif(NOT DOXYGEN_FOUND)
  228. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/doc/Doxyfile.in ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY)
  229. add_custom_target(doc
  230. ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
  231. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  232. COMMENT "Generating API documentation with Doxygen")
  233. endif()