CMakeLists.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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(NVIM_VERSION_PATCH 0)
  7. set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
  8. # Set default build type.
  9. if (NOT CMAKE_BUILD_TYPE)
  10. message(STATUS "CMAKE_BUILD_TYPE not given; setting to 'RelWithDebInfo'.")
  11. set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "Choose the type of build." FORCE)
  12. endif()
  13. # main sources of libopen62541
  14. include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include")
  15. include_directories("${CMAKE_CURRENT_SOURCE_DIR}/src")
  16. file(GLOB_RECURSE exported_headers "${CMAKE_CURRENT_SOURCE_DIR}/include/*.h")
  17. file(GLOB_RECURSE headers "${CMAKE_CURRENT_SOURCE_DIR}/src/*.h")
  18. file(GLOB generated_headers "${PROJECT_BINARY_DIR}/src_generated/*.h")
  19. set(lib_sources src/ua_types.c
  20. src/ua_types_encoding_binary.c
  21. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.c
  22. ${PROJECT_BINARY_DIR}/src_generated/ua_namespace_0.c
  23. src/ua_transport.c
  24. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.c
  25. src/ua_securechannel.c
  26. src/ua_session.c
  27. src/server/ua_server.c
  28. src/server/ua_server_addressspace.c
  29. src/server/ua_server_binary.c
  30. src/server/ua_server_worker.c
  31. src/server/ua_securechannel_manager.c
  32. src/server/ua_session_manager.c
  33. src/server/ua_services_attribute.c
  34. src/server/ua_services_session.c
  35. src/server/ua_services_discovery.c
  36. src/server/ua_services_securechannel.c
  37. src/server/ua_services_nodemanagement.c
  38. src/server/ua_services_view.c
  39. ${exported_headers}
  40. ${generated_headers}
  41. ${headers})
  42. # compiler flags
  43. if(CMAKE_COMPILER_IS_GNUCC OR "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
  44. add_definitions(-std=c99 -pedantic -pipe -Wall -Wextra -Werror -Wformat
  45. -Wno-unused-parameter -Wno-unused-function -Wno-unused-label -Wpointer-arith -Wreturn-type -Wsign-compare -Wmultichar
  46. -Wshadow -Wcast-align -Wcast-qual -Wmissing-prototypes -Wstrict-prototypes # -Wconversion
  47. -Winit-self -Wuninitialized -Wno-deprecated -Wformat-security -ffunction-sections -fdata-sections)
  48. if(NOT "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
  49. add_definitions(-Wformat-nonliteral)
  50. set (CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,--gc-sections")
  51. set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--gc-sections")
  52. else()
  53. add_definitions(-Wno-gnu-statement-expression)
  54. endif()
  55. if(NOT WIN32)
  56. add_definitions(-fstack-protector -fPIC -fvisibility=hidden)
  57. endif()
  58. endif()
  59. # build settings
  60. set(generate_src_options) # the options for the tools that generate code from xml-schema definitions
  61. ## set debug flag
  62. if(CMAKE_BUILD_TYPE MATCHES Debug)
  63. set(UA_DEBUG ON)
  64. else()
  65. set(UA_DEBUG OFF)
  66. endif()
  67. ## extensions
  68. option(EXTENSION_STATELESS "Enable stateless extension" OFF)
  69. option(EXTENSION_UDP "Enable udp extension" OFF)
  70. if(EXTENSION_UDP)
  71. set(EXTENSION_STATELESS ON)
  72. message(STATUS "Extensions: enabling udp")
  73. add_definitions(-DEXTENSION_UDP)
  74. endif()
  75. if(EXTENSION_STATELESS)
  76. message(STATUS "Extensions: enabling stateless")
  77. add_definitions(-DEXTENSION_STATELESS)
  78. add_executable(statelessClient $<TARGET_OBJECTS:open62541-objects> examples/statelessClient.c)
  79. if(MULTITHREADING)
  80. target_link_libraries(statelessClient urcu-cds urcu urcu-common pthread)
  81. endif()
  82. endif()
  83. ## self-signed certificates
  84. option(ENABLE_SELFSIGNED "Enable self-signed certificates" OFF)
  85. if(ENABLE_SELFSIGNED)
  86. message(STATUS "Enabling self-signed certificates")
  87. SET(lib_sources ${lib_sources} ${PROJECT_BINARY_DIR}/localhost.der ${PROJECT_BINARY_DIR}/ca.crt)
  88. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/localhost.der
  89. ${PROJECT_BINARY_DIR}/ca.crt
  90. COMMAND python ${PROJECT_SOURCE_DIR}/tools/certs/create_self-signed.py ${PROJECT_BINARY_DIR}
  91. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/certs/create_self-signed.py
  92. ${CMAKE_CURRENT_SOURCE_DIR}/tools/certs/localhost.cnf)
  93. endif()
  94. ## auto-generate all types or only the relevant subset?
  95. option(TYPES_ONLY_NEEDED "Include only compile-needed types" OFF)
  96. if(TYPES_ONLY_NEEDED)
  97. list(APPEND generate_src_options "--only-needed")
  98. endif()
  99. ## encodings
  100. set(UA_ENCODING_AMOUNT 1) # binary encoding
  101. ### xml
  102. option(ENABLE_XML_ENCODING "Enable XML-encoding of the UA types" OFF)
  103. if(ENABLE_XML_ENCODING)
  104. MATH(EXPR UA_ENCODING_AMOUNT "${UA_ENCODING_AMOUNT}+1")
  105. find_package(EXPAT REQUIRED)
  106. if(EXPAT_FOUND)
  107. include_directories(${EXPAT_INCLUDE_DIRS})
  108. else(EXPAT_FOUND)
  109. message(FATAL_ERROR "Expat library not found.")
  110. endif(EXPAT_FOUND)
  111. include_directories("${CMAKE_CURRENT_SOURCE_DIR}/src/ongoing")
  112. list(APPEND lib_sources src/ongoing/ua_types_encoding_xml.c
  113. src/ongoing/ua_namespace_xml.c
  114. src/ongoing/ua_xml.c)
  115. list(APPEND generate_src_options "--with-xml")
  116. endif()
  117. # ### json
  118. # option(ENABLE_JSON_ENCODING "Enable JSON-encoding of the UA types" OFF)
  119. # if(ENABLE_JSON_ENCODING)
  120. # MATH(EXPR UA_ENCODING_AMOUNT "${UA_ENCODING_AMOUNT}+1")
  121. # include_directories("${CMAKE_CURRENT_SOURCE_DIR}/src/ongoing")
  122. # list(APPEND lib_sources src/ongoing/ua_types_encoding_json.c)
  123. # list(APPEND generate_src_options "--with-json")
  124. # endif(ENABLE_JSON_ENCODING)
  125. ## multithreading
  126. option(MULTITHREADING "Enable multithreading" OFF)
  127. if(MULTITHREADING)
  128. set(UA_MULTITHREADING ON)
  129. find_package(Threads REQUIRED)
  130. list(APPEND lib_sources src/server/ua_nodestore_concurrent.c)
  131. else()
  132. list(APPEND lib_sources src/server/ua_nodestore.c)
  133. endif()
  134. add_library(open62541-objects OBJECT ${lib_sources})
  135. add_library(open62541 SHARED $<TARGET_OBJECTS:open62541-objects>)
  136. add_library(open62541-static STATIC $<TARGET_OBJECTS:open62541-objects>)
  137. SET_TARGET_PROPERTIES(open62541-static PROPERTIES OUTPUT_NAME open62541 CLEAN_DIRECT_OUTPUT 1) # static version that exports all symbols
  138. target_compile_definitions(open62541-objects PUBLIC UA_DYNAMIC_LINKING)
  139. ## logging
  140. set(UA_LOGLEVEL 400 CACHE STRING "Level at which logs shall be reported")
  141. ## coverage
  142. option(ENABLE_COVERAGE "Enable gcov coverage" OFF)
  143. if(ENABLE_COVERAGE)
  144. message(STATUS "Enabling gcov support")
  145. set(CMAKE_BUILD_TYPE DEBUG)
  146. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
  147. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
  148. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
  149. endif()
  150. # set the precompiler flags
  151. configure_file("src/ua_config.h.in" "${PROJECT_BINARY_DIR}/src_generated/ua_config.h")
  152. # generate code from xml definitions
  153. file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/src_generated")
  154. include_directories("${PROJECT_BINARY_DIR}/src_generated")
  155. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.c
  156. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.h
  157. PRE_BUILD
  158. COMMAND python ${PROJECT_SOURCE_DIR}/tools/generate_builtin.py --export-prototypes ${generate_src_options} ${PROJECT_SOURCE_DIR}/tools/schema/Opc.Ua.Types.bsd ${PROJECT_BINARY_DIR}/src_generated/ua_types
  159. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_builtin.py
  160. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/Opc.Ua.Types.bsd)
  161. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_namespace_0.c
  162. ${PROJECT_BINARY_DIR}/src_generated/ua_namespace_0.h
  163. PRE_BUILD
  164. COMMAND python ${PROJECT_SOURCE_DIR}/tools/generate_namespace.py ${generate_src_options} ${PROJECT_SOURCE_DIR}/tools/schema/NodeIds.csv ${PROJECT_BINARY_DIR}/src_generated/ua_namespace_0
  165. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_namespace.py
  166. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/NodeIds.csv)
  167. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.c
  168. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.h
  169. PRE_BUILD
  170. COMMAND python ${PROJECT_SOURCE_DIR}/tools/generate_builtin.py --additional-includes=ua_transport.h ${PROJECT_SOURCE_DIR}/tools/schema/Custom.Opc.Ua.Transport.bsd ${PROJECT_BINARY_DIR}/src_generated/ua_transport
  171. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_builtin.py
  172. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/Custom.Opc.Ua.Transport.bsd)
  173. # build example client
  174. option(CLIENT "Build a test client" OFF)
  175. if(CLIENT)
  176. message(STATUS "Extensions: enabling client")
  177. add_definitions( -DBENCHMARK=1 )
  178. # the client is built directly with the .o files as it currently uses
  179. # internal functions that are not exported to the shared lib.
  180. add_executable(exampleClient $<TARGET_OBJECTS:open62541-objects> examples/opcuaClient.c)
  181. if(MULTITHREADING)
  182. target_link_libraries(exampleClient urcu-cds urcu urcu-common pthread)
  183. endif()
  184. endif()
  185. # build example server
  186. option(EXAMPLESERVER "Build a test server" OFF)
  187. if(EXAMPLESERVER)
  188. set(server_sources examples/opcuaServer.c
  189. examples/networklayer_tcp.c
  190. examples/logger_stdout.c)
  191. if(EXTENSION_UDP)
  192. list(APPEND server_sources examples/networklayer_udp.c)
  193. endif()
  194. add_executable(exampleServer ${server_sources} ${exported_headers} ${generated_headers})
  195. target_link_libraries(exampleServer open62541 rt)
  196. if(WIN32)
  197. target_link_libraries(exampleServer ws2_32)
  198. endif(WIN32)
  199. if(MULTITHREADING)
  200. target_link_libraries(exampleServer urcu-cds urcu urcu-common pthread)
  201. endif()
  202. endif()
  203. # build unit tests
  204. option(ENABLE_UNIT_TESTS "Run unit tests after building" OFF)
  205. if(ENABLE_UNIT_TESTS)
  206. enable_testing()
  207. add_subdirectory(tests)
  208. endif()
  209. # build documentation
  210. option(GENERATE_DOCUMENTATION "Generate doxygen documentation" OFF)
  211. if(GENERATE_DOCUMENTATION)
  212. find_package(Doxygen)
  213. if(NOT DOXYGEN_FOUND)
  214. message(FATAL_ERROR "Doxygen is not installed or not properly configured")
  215. endif(NOT DOXYGEN_FOUND)
  216. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/doc/Doxyfile.in ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY)
  217. add_custom_target(doc
  218. ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
  219. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  220. COMMENT "Generating API documentation with Doxygen")
  221. endif()