CMakeLists.txt 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. 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. # compiler flags
  14. if(CMAKE_COMPILER_IS_GNUCC OR "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
  15. add_definitions(-std=c99 -pipe -Wall -Wextra -Werror -Wformat
  16. -Wno-unused-parameter -Wno-unused-function -Wno-unused-label -Wpointer-arith -Wreturn-type -Wsign-compare -Wmultichar
  17. -Wshadow -Wcast-qual -Wmissing-prototypes -Wstrict-prototypes # -Wconversion
  18. -Winit-self -Wuninitialized -Wno-deprecated -Wformat-security -ffunction-sections -fdata-sections)
  19. if(NOT "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
  20. add_definitions(-Wformat-nonliteral)
  21. set (CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,--gc-sections")
  22. set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--gc-sections")
  23. else()
  24. add_definitions(-Wno-gnu-statement-expression)
  25. endif()
  26. if(NOT WIN32)
  27. add_definitions(-fstack-protector -fPIC -fvisibility=hidden)
  28. endif()
  29. endif()
  30. # main sources of libopen62541
  31. include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include")
  32. include_directories("${CMAKE_CURRENT_SOURCE_DIR}/src")
  33. file(GLOB_RECURSE exported_headers "${CMAKE_CURRENT_SOURCE_DIR}/include/*.h")
  34. file(GLOB generated_headers "${PROJECT_BINARY_DIR}/src_generated/*.h")
  35. set(lib_sources src/ua_types.c
  36. src/ua_types_encoding_binary.c
  37. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.c
  38. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.c
  39. ${PROJECT_BINARY_DIR}/src_generated/ua_nodeids.h
  40. src/ua_securechannel.c
  41. src/ua_session.c
  42. src/server/ua_server.c
  43. src/server/ua_server_addressspace.c
  44. src/server/ua_server_binary.c
  45. src/server/ua_nodes.c
  46. src/server/ua_server_worker.c
  47. src/server/ua_securechannel_manager.c
  48. src/server/ua_session_manager.c
  49. src/server/ua_services_discovery.c
  50. src/server/ua_services_securechannel.c
  51. src/server/ua_services_session.c
  52. src/server/ua_services_attribute.c
  53. src/server/ua_services_nodemanagement.c
  54. src/server/ua_services_view.c
  55. ${exported_headers}
  56. ${generated_headers} )
  57. ## multithreading
  58. option(MULTITHREADING "Enable multithreading" OFF)
  59. if(MULTITHREADING)
  60. set(UA_MULTITHREADING ON)
  61. find_package(Threads REQUIRED)
  62. list(APPEND lib_sources src/server/ua_nodestore_concurrent.c)
  63. else()
  64. list(APPEND lib_sources src/server/ua_nodestore.c)
  65. endif()
  66. ## extensions
  67. option(EXTENSION_UDP "Enable udp extension" OFF)
  68. if(EXTENSION_UDP)
  69. set(EXTENSION_STATELESS ON)
  70. message(STATUS "Extensions: enabling udp")
  71. add_definitions(-DEXTENSION_UDP)
  72. endif()
  73. option(EXTENSION_STATELESS "Enable stateless extension" OFF)
  74. if(EXTENSION_STATELESS)
  75. message(STATUS "Extensions: enabling stateless")
  76. add_definitions(-DEXTENSION_STATELESS)
  77. endif()
  78. add_library(open62541-objects OBJECT ${lib_sources})
  79. add_library(open62541 SHARED $<TARGET_OBJECTS:open62541-objects>)
  80. add_library(open62541-static STATIC $<TARGET_OBJECTS:open62541-objects>)
  81. SET_TARGET_PROPERTIES(open62541-static PROPERTIES OUTPUT_NAME open62541 CLEAN_DIRECT_OUTPUT 1) # static version that exports all symbols
  82. target_compile_definitions(open62541-objects PRIVATE UA_DYNAMIC_LINKING)
  83. target_compile_definitions(open62541-static PRIVATE UA_DYNAMIC_LINKING)
  84. ## logging
  85. set(UA_LOGLEVEL 300 CACHE STRING "Level at which logs shall be reported")
  86. ## coverage
  87. option(ENABLE_COVERAGE "Enable gcov coverage" OFF)
  88. if(ENABLE_COVERAGE)
  89. message(STATUS "Enabling gcov support")
  90. set(CMAKE_BUILD_TYPE DEBUG)
  91. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
  92. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
  93. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
  94. endif()
  95. # set the precompiler flags
  96. configure_file("src/ua_config.h.in" "${PROJECT_BINARY_DIR}/src_generated/ua_config.h")
  97. # generate code from xml definitions
  98. file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/src_generated")
  99. include_directories("${PROJECT_BINARY_DIR}/src_generated")
  100. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.c
  101. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.h
  102. PRE_BUILD
  103. 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
  104. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_datatypes.py
  105. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/Opc.Ua.Types.bsd
  106. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/NodeIds.csv)
  107. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.c
  108. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.h
  109. PRE_BUILD
  110. 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
  111. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_datatypes.py
  112. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/Custom.Opc.Ua.Transport.bsd)
  113. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_nodeids.h
  114. PRE_BUILD
  115. COMMAND python ${PROJECT_SOURCE_DIR}/tools/generate_nodeids.py ${PROJECT_SOURCE_DIR}/tools/schema/NodeIds.csv ${PROJECT_BINARY_DIR}/src_generated/ua_nodeids
  116. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_nodeids.py
  117. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/NodeIds.csv)
  118. # build example client
  119. option(CLIENT "Build a test client" OFF)
  120. if(CLIENT)
  121. message(STATUS "Extensions: enabling client")
  122. add_definitions( -DBENCHMARK=1 )
  123. # the client is built directly with the .o files as it currently uses
  124. # internal functions that are not exported to the shared lib.
  125. add_executable(exampleClient $<TARGET_OBJECTS:open62541-objects> examples/client.c)
  126. if(MULTITHREADING)
  127. target_link_libraries(exampleClient urcu-cds urcu urcu-common pthread)
  128. endif()
  129. if(EXTENSION_STATELESS)
  130. add_executable(statelessClient $<TARGET_OBJECTS:open62541-objects> examples/client_stateless.c)
  131. if(MULTITHREADING)
  132. target_link_libraries(statelessClient urcu-cds urcu urcu-common pthread)
  133. endif()
  134. endif()
  135. endif()
  136. # build example server
  137. option(EXAMPLESERVER "Build a test server" OFF)
  138. if(EXAMPLESERVER)
  139. add_executable(exampleServer examples/server.c examples/networklayer_tcp.c examples/logger_stdout.c ${exported_headers} ${generated_headers})
  140. target_link_libraries(exampleServer open62541-static)
  141. if(WIN32)
  142. target_link_libraries(exampleServer ws2_32)
  143. else()
  144. target_link_libraries(exampleServer rt)
  145. endif()
  146. if(MULTITHREADING)
  147. target_link_libraries(exampleServer urcu-cds urcu urcu-common pthread)
  148. endif()
  149. endif()
  150. ## self-signed certificates
  151. option(GENERATE_SELFSIGNED "Generate self-signed certificates" OFF)
  152. if(GENERATE_SELFSIGNED)
  153. message(STATUS "Enabling self-signed certificates")
  154. SET(lib_sources ${lib_sources} ${PROJECT_BINARY_DIR}/localhost.der ${PROJECT_BINARY_DIR}/ca.crt)
  155. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/localhost.der
  156. ${PROJECT_BINARY_DIR}/ca.crt
  157. COMMAND python ${PROJECT_SOURCE_DIR}/tools/certs/create_self-signed.py ${PROJECT_BINARY_DIR}
  158. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/certs/create_self-signed.py
  159. ${CMAKE_CURRENT_SOURCE_DIR}/tools/certs/localhost.cnf)
  160. endif()
  161. # build unit tests
  162. option(ENABLE_UNIT_TESTS "Run unit tests after building" OFF)
  163. if(ENABLE_UNIT_TESTS)
  164. enable_testing()
  165. add_subdirectory(tests)
  166. endif()
  167. # build documentation
  168. option(GENERATE_DOCUMENTATION "Generate doxygen documentation" OFF)
  169. if(GENERATE_DOCUMENTATION)
  170. find_package(Doxygen)
  171. if(NOT DOXYGEN_FOUND)
  172. message(FATAL_ERROR "Doxygen is not installed or not properly configured")
  173. endif(NOT DOXYGEN_FOUND)
  174. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/doc/Doxyfile.in ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY)
  175. add_custom_target(doc
  176. ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
  177. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  178. COMMENT "Generating API documentation with Doxygen")
  179. endif()