CMakeLists.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. -Wshadow -Wcast-qual -Wmissing-prototypes -Wstrict-prototypes # -Wconversion
  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. # main sources of libopen62541
  34. include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include")
  35. include_directories("${CMAKE_CURRENT_SOURCE_DIR}/src")
  36. file(GLOB_RECURSE exported_headers "${CMAKE_CURRENT_SOURCE_DIR}/include/*.h")
  37. file(GLOB generated_headers "${PROJECT_BINARY_DIR}/src_generated/*.h")
  38. set(lib_sources src/ua_types.c
  39. src/ua_types_encoding_binary.c
  40. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.c
  41. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.c
  42. ${PROJECT_BINARY_DIR}/src_generated/ua_nodeids.h
  43. src/ua_securechannel.c
  44. src/ua_session.c
  45. src/server/ua_server.c
  46. src/server/ua_server_addressspace.c
  47. src/server/ua_server_binary.c
  48. src/server/ua_nodes.c
  49. src/server/ua_server_worker.c
  50. src/server/ua_securechannel_manager.c
  51. src/server/ua_session_manager.c
  52. src/server/ua_services_discovery.c
  53. src/server/ua_services_securechannel.c
  54. src/server/ua_services_session.c
  55. src/server/ua_services_attribute.c
  56. src/server/ua_services_nodemanagement.c
  57. src/server/ua_services_view.c
  58. ${exported_headers}
  59. ${generated_headers} )
  60. ## multithreading
  61. option(MULTITHREADING "Enable multithreading" OFF)
  62. if(MULTITHREADING)
  63. set(UA_MULTITHREADING ON)
  64. find_package(Threads REQUIRED)
  65. list(APPEND lib_sources src/server/ua_nodestore_concurrent.c)
  66. else()
  67. list(APPEND lib_sources src/server/ua_nodestore.c)
  68. endif()
  69. ## extensions
  70. option(EXTENSION_UDP "Enable udp extension" OFF)
  71. if(EXTENSION_UDP)
  72. set(EXTENSION_STATELESS ON)
  73. message(STATUS "Extensions: enabling udp")
  74. add_definitions(-DEXTENSION_UDP)
  75. endif()
  76. option(EXTENSION_STATELESS "Enable stateless extension" OFF)
  77. if(EXTENSION_STATELESS)
  78. message(STATUS "Extensions: enabling stateless")
  79. add_definitions(-DEXTENSION_STATELESS)
  80. endif()
  81. option(DEMO_NODESET "Create a demo node for every built-in datatype" OFF)
  82. if(DEMO_NODESET)
  83. message(STATUS "Enabling demo nodeset")
  84. add_definitions(-DDEMO_NODESET)
  85. endif()
  86. add_library(open62541-objects OBJECT ${lib_sources})
  87. add_library(open62541 SHARED $<TARGET_OBJECTS:open62541-objects>)
  88. add_library(open62541-static STATIC $<TARGET_OBJECTS:open62541-objects>)
  89. SET_TARGET_PROPERTIES(open62541-static PROPERTIES OUTPUT_NAME open62541 CLEAN_DIRECT_OUTPUT 1) # static version that exports all symbols
  90. target_compile_definitions(open62541-objects PRIVATE UA_DYNAMIC_LINKING)
  91. target_compile_definitions(open62541-static PRIVATE UA_DYNAMIC_LINKING)
  92. ## logging
  93. set(UA_LOGLEVEL 300 CACHE STRING "Level at which logs shall be reported")
  94. ## coverage
  95. option(ENABLE_COVERAGE "Enable gcov coverage" OFF)
  96. if(ENABLE_COVERAGE)
  97. message(STATUS "Enabling gcov support")
  98. set(CMAKE_BUILD_TYPE DEBUG)
  99. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
  100. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
  101. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
  102. endif()
  103. # set the precompiler flags
  104. configure_file("src/ua_config.h.in" "${PROJECT_BINARY_DIR}/src_generated/ua_config.h")
  105. # generate code from xml definitions
  106. file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/src_generated")
  107. include_directories("${PROJECT_BINARY_DIR}/src_generated")
  108. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.c
  109. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.h
  110. PRE_BUILD
  111. 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
  112. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_datatypes.py
  113. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/Opc.Ua.Types.bsd
  114. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/NodeIds.csv)
  115. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.c
  116. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.h
  117. PRE_BUILD
  118. 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
  119. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_datatypes.py
  120. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/Custom.Opc.Ua.Transport.bsd)
  121. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_nodeids.h
  122. PRE_BUILD
  123. COMMAND python ${PROJECT_SOURCE_DIR}/tools/generate_nodeids.py ${PROJECT_SOURCE_DIR}/tools/schema/NodeIds.csv ${PROJECT_BINARY_DIR}/src_generated/ua_nodeids
  124. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_nodeids.py
  125. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/NodeIds.csv)
  126. # build example client
  127. option(CLIENT "Build a test client" OFF)
  128. if(CLIENT)
  129. message(STATUS "Extensions: enabling client")
  130. add_definitions( -DBENCHMARK=1 )
  131. # the client is built directly with the .o files as it currently uses
  132. # internal functions that are not exported to the shared lib.
  133. add_executable(exampleClient $<TARGET_OBJECTS:open62541-objects> examples/client.c)
  134. if(MULTITHREADING)
  135. target_link_libraries(exampleClient urcu-cds urcu urcu-common pthread)
  136. endif()
  137. if ((CMAKE_COMPILER_IS_GNUCC OR "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang") AND (CMAKE_BUILD_TYPE STREQUAL "MinSizeRel" OR CMAKE_BUILD_TYPE STREQUAL "Release"))
  138. add_custom_command(TARGET exampleClient POST_BUILD
  139. COMMAND ${CMAKE_STRIP} $<TARGET_FILE:exampleClient>
  140. )
  141. endif()
  142. if(EXTENSION_STATELESS)
  143. add_executable(statelessClient $<TARGET_OBJECTS:open62541-objects> examples/client_stateless.c)
  144. if(MULTITHREADING)
  145. target_link_libraries(statelessClient urcu-cds urcu urcu-common pthread)
  146. endif()
  147. if ((CMAKE_COMPILER_IS_GNUCC OR "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang") AND (CMAKE_BUILD_TYPE STREQUAL "MinSizeRel" OR CMAKE_BUILD_TYPE STREQUAL "Release"))
  148. add_custom_command(TARGET statelessClient POST_BUILD
  149. COMMAND ${CMAKE_STRIP} $<TARGET_FILE:statelessClient>
  150. )
  151. endif()
  152. endif()
  153. endif()
  154. # build example server
  155. option(EXAMPLESERVER "Build a test server" OFF)
  156. if(EXAMPLESERVER)
  157. add_executable(exampleServer examples/server.c examples/networklayer_tcp.c examples/logger_stdout.c ${exported_headers} ${generated_headers})
  158. add_executable(exampleServer_datasource examples/server_datasource.c examples/networklayer_tcp.c examples/logger_stdout.c ${exported_headers} ${generated_headers})
  159. target_link_libraries(exampleServer open62541-static)
  160. target_link_libraries(exampleServer_datasource open62541-static)
  161. if(WIN32)
  162. target_link_libraries(exampleServer ws2_32)
  163. target_link_libraries(exampleServer_datasource ws2_32)
  164. else()
  165. target_link_libraries(exampleServer rt)
  166. target_link_libraries(exampleServer_datasource rt)
  167. endif()
  168. if(MULTITHREADING)
  169. target_link_libraries(exampleServer urcu-cds urcu urcu-common pthread)
  170. target_link_libraries(exampleServer_datasource urcu-cds urcu urcu-common pthread)
  171. endif()
  172. if ((CMAKE_COMPILER_IS_GNUCC OR "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang") AND (CMAKE_BUILD_TYPE STREQUAL "MinSizeRel" OR CMAKE_BUILD_TYPE STREQUAL "Release"))
  173. add_custom_command(TARGET exampleServer POST_BUILD
  174. COMMAND ${CMAKE_STRIP} $<TARGET_FILE:exampleServer>
  175. )
  176. add_custom_command(TARGET exampleServer_datasource POST_BUILD
  177. COMMAND ${CMAKE_STRIP} $<TARGET_FILE:exampleServer_datasource>
  178. )
  179. endif()
  180. endif()
  181. ## self-signed certificates
  182. option(GENERATE_SELFSIGNED "Generate self-signed certificates" OFF)
  183. if(GENERATE_SELFSIGNED)
  184. message(STATUS "Enabling self-signed certificates")
  185. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/localhost.der
  186. ${PROJECT_BINARY_DIR}/ca.crt
  187. COMMAND python ${PROJECT_SOURCE_DIR}/tools/certs/create_self-signed.py ${PROJECT_BINARY_DIR}
  188. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/certs/create_self-signed.py
  189. ${CMAKE_CURRENT_SOURCE_DIR}/tools/certs/localhost.cnf)
  190. add_custom_target(selfsigned ALL DEPENDS ${PROJECT_BINARY_DIR}/localhost.der ${PROJECT_BINARY_DIR}/ca.crt)
  191. endif()
  192. # build unit tests
  193. option(ENABLE_UNIT_TESTS "Run unit tests after building" OFF)
  194. if(ENABLE_UNIT_TESTS)
  195. enable_testing()
  196. add_subdirectory(tests)
  197. endif()
  198. # build documentation
  199. option(GENERATE_DOCUMENTATION "Generate doxygen documentation" OFF)
  200. if(GENERATE_DOCUMENTATION)
  201. find_package(Doxygen)
  202. if(NOT DOXYGEN_FOUND)
  203. message(FATAL_ERROR "Doxygen is not installed or not properly configured")
  204. endif(NOT DOXYGEN_FOUND)
  205. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/doc/Doxyfile.in ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY)
  206. add_custom_target(doc
  207. ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
  208. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  209. COMMENT "Generating API documentation with Doxygen")
  210. endif()