CMakeLists.txt 10 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 1)
  6. set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
  7. set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DDEBUG")
  8. # main sources of libopen62541
  9. include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include")
  10. include_directories("${CMAKE_CURRENT_SOURCE_DIR}/src")
  11. file(GLOB_RECURSE headers "${CMAKE_CURRENT_SOURCE_DIR}/src/*.h")
  12. file(GLOB generated_headers "${PROJECT_BINARY_DIR}/src_generated/*.h")
  13. set(lib_sources src/ua_types.c
  14. src/ua_types_encoding_binary.c
  15. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.c
  16. ${PROJECT_BINARY_DIR}/src_generated/ua_namespace_0.c
  17. src/ua_transport.c
  18. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.c
  19. src/ua_connection.c
  20. src/ua_securechannel.c
  21. src/ua_session.c
  22. src/ua_util.c
  23. src/server/ua_server.c
  24. src/server/ua_securechannel_manager.c
  25. src/server/ua_session_manager.c
  26. src/server/ua_namespace_manager.c
  27. src/server/ua_server_binary.c
  28. src/server/ua_services_attribute.c
  29. src/server/ua_services_session.c
  30. src/server/ua_services_discovery.c
  31. src/server/ua_services_securechannel.c
  32. src/server/ua_services_nodemanagement.c
  33. src/server/ua_services_view.c
  34. src/server/ua_services_monitoreditems.c
  35. src/server/nodestore/open62541_nodestore_view.c
  36. src/server/nodestore/open62541_nodestore_attribute.c
  37. src/server/nodestore/open62541_nodestore.c
  38. ${headers}
  39. ${generated_headers})
  40. # compiler flags
  41. if(CMAKE_COMPILER_IS_GNUCC OR "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
  42. add_definitions(-std=c99 -pedantic -pipe -Wall -Wextra -Werror -Wformat
  43. -Wno-unused-parameter -Wno-unused-function -Wno-unused-label -Wpointer-arith -Wreturn-type -Wsign-compare -Wmultichar
  44. -Winit-self -Wuninitialized -Wno-deprecated -Wformat-security -ffunction-sections -fdata-sections)
  45. if(NOT "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
  46. add_definitions(-Wformat-nonliteral -Wl,--gc-sections)
  47. endif()
  48. if(NOT WIN32)
  49. add_definitions(-fstack-protector -fPIC -fvisibility=hidden)
  50. endif()
  51. endif()
  52. add_definitions(-Dopen62541_EXPORTS) # dllexport/dllimport differentiation in ua_config.h when building the lib on windows
  53. # build settings
  54. set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMakeModules")
  55. set(generate_src_options) # the options for the tools that generate code from xml-schema definitions
  56. ## self-signed certificates
  57. option(ENABLE_SELFSIGNED "Enable self-signed certificates" OFF)
  58. if(ENABLE_SELFSIGNED)
  59. message(STATUS "Enabling self-signed certificates")
  60. SET(lib_sources ${lib_sources} ${PROJECT_BINARY_DIR}/localhost.der ${PROJECT_BINARY_DIR}/ca.crt)
  61. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/localhost.der
  62. ${PROJECT_BINARY_DIR}/ca.crt
  63. COMMAND python ${PROJECT_SOURCE_DIR}/tools/certs/create_self-signed.py ${PROJECT_BINARY_DIR}
  64. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/certs/create_self-signed.py
  65. ${CMAKE_CURRENT_SOURCE_DIR}/tools/certs/localhost.cnf)
  66. endif()
  67. ## auto-generate all types or only the relevant subset?
  68. option(TYPES_ONLY_NEEDED "Include only compile-needed types" OFF)
  69. if(TYPES_ONLY_NEEDED)
  70. list(APPEND generate_src_options "--only-needed")
  71. endif()
  72. ## encodings
  73. set(UA_ENCODING_AMOUNT 1) # binary encoding
  74. ### xml
  75. option(ENABLE_XML_ENCODING "Enable XML-encoding of the UA types" OFF)
  76. if(ENABLE_XML_ENCODING)
  77. MATH(EXPR UA_ENCODING_AMOUNT "${UA_ENCODING_AMOUNT}+1")
  78. find_package(EXPAT REQUIRED)
  79. if(EXPAT_FOUND)
  80. include_directories(${EXPAT_INCLUDE_DIRS})
  81. else(EXPAT_FOUND)
  82. message(FATAL_ERROR "Expat library not found.")
  83. endif(EXPAT_FOUND)
  84. include_directories("${CMAKE_CURRENT_SOURCE_DIR}/src/ongoing")
  85. list(APPEND lib_sources src/ongoing/ua_types_encoding_xml.c
  86. src/ongoing/ua_namespace_xml.c
  87. src/ongoing/ua_xml.c)
  88. list(APPEND generate_src_options "--with-xml")
  89. endif()
  90. ### json
  91. option(ENABLE_JSON_ENCODING "Enable JSON-encoding of the UA types" OFF)
  92. if(ENABLE_JSON_ENCODING)
  93. MATH(EXPR UA_ENCODING_AMOUNT "${UA_ENCODING_AMOUNT}+1")
  94. include_directories("${CMAKE_CURRENT_SOURCE_DIR}/src/ongoing")
  95. list(APPEND lib_sources src/ongoing/ua_types_encoding_json.c)
  96. list(APPEND generate_src_options "--with-json")
  97. endif(ENABLE_JSON_ENCODING)
  98. ## multithreading
  99. option(ENABLE_MULTITHREADING "Enable multithreading" OFF)
  100. if(ENABLE_MULTITHREADING)
  101. find_package(Threads REQUIRED)
  102. list(APPEND lib_sources src/server/ua_nodestore_concurrent.c)
  103. else()
  104. list(APPEND lib_sources src/server/nodestore/ua_nodestore.c)
  105. list(APPEND lib_sources src/server/nodestore/ua_nodestoreExample.c)
  106. endif()
  107. add_library(open62541-objects OBJECT ${lib_sources}) # static version that exports all symbols
  108. add_library(open62541 SHARED $<TARGET_OBJECTS:open62541-objects>)
  109. ## logging
  110. set(UA_LOGLEVEL 400 CACHE STRING "Level at which logs shall be reported")
  111. ## coverage
  112. option(ENABLE_COVERAGE "Enable gcov coverage" OFF)
  113. if(ENABLE_COVERAGE)
  114. message(STATUS "Enabling gcov support")
  115. set(CMAKE_BUILD_TYPE DEBUG)
  116. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
  117. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
  118. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
  119. endif()
  120. configure_file("src/ua_config.h.in" "${PROJECT_BINARY_DIR}/src_generated/ua_config.h")
  121. # build generated code
  122. file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/src_generated")
  123. include_directories("${PROJECT_BINARY_DIR}/src_generated")
  124. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.c
  125. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.h
  126. COMMAND python ${PROJECT_SOURCE_DIR}/tools/generate_builtin.py ${generate_src_options} ${PROJECT_SOURCE_DIR}/tools/schema/Opc.Ua.Types.bsd ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated
  127. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_builtin.py
  128. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/Opc.Ua.Types.bsd)
  129. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_namespace_0.c
  130. ${PROJECT_BINARY_DIR}/src_generated/ua_namespace_0.h
  131. 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
  132. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_namespace.py
  133. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/NodeIds.csv)
  134. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.c
  135. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.h
  136. 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_generated
  137. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_namespace.py
  138. ${CMAKE_CURRENT_SOURCE_DIR}/tools/schema/Custom.Opc.Ua.Transport.bsd)
  139. # build example client
  140. option(CLIENT "Build a test client" OFF)
  141. if(CLIENT)
  142. message(STATUS "Extensions: enabling client")
  143. add_definitions( -DBENCHMARK=1 )
  144. # the client is built directly with the .o files as it currently uses
  145. # internal functions that are not exported to the shared lib.
  146. add_executable(exampleClient $<TARGET_OBJECTS:open62541-objects> examples/opcuaClient.c)
  147. endif()
  148. # build example server
  149. option(EXAMPLESERVER "Build a test server" OFF)
  150. if(EXAMPLESERVER)
  151. set(server_sources examples/opcuaServer.c
  152. examples/logger_stdout.c
  153. examples/nodestoreAccessExample.c)
  154. if(NOT ENABLE_MULTITHREADING)
  155. list(APPEND server_sources examples/networklayer_tcp.c)
  156. else()
  157. list(APPEND server_sources examples/networklayer_tcp_concurrent.c)
  158. endif()
  159. add_executable(exampleServer ${server_sources})
  160. target_link_libraries(exampleServer open62541)
  161. if(WIN32)
  162. target_link_libraries(exampleServer ws2_32)
  163. endif(WIN32)
  164. if(ENABLE_MULTITHREADING)
  165. find_package(LibUV REQUIRED)
  166. target_link_libraries(exampleServer urcu-cds urcu uv)
  167. endif()
  168. endif()
  169. # build unit tests
  170. option(ENABLE_UNIT_TESTS "Run unit tests after building" OFF)
  171. if(ENABLE_UNIT_TESTS)
  172. enable_testing()
  173. add_subdirectory(tests)
  174. endif()
  175. # build documentation
  176. option(GENERATE_DOCUMENTATION "Generate doxygen documentation" OFF)
  177. if(GENERATE_DOCUMENTATION)
  178. find_package(Doxygen)
  179. if(NOT DOXYGEN_FOUND)
  180. message(FATAL_ERROR "Doxygen is not installed or not properly configured")
  181. endif(NOT DOXYGEN_FOUND)
  182. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/doc/Doxyfile.in ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY)
  183. add_custom_target(doc
  184. ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
  185. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  186. COMMENT "Generating API documentation with Doxygen")
  187. endif()
  188. # download queue.h if required
  189. if(WIN32)
  190. if(NOT EXISTS "${PROJECT_BINARY_DIR}/src_generated/queue.h")
  191. file(DOWNLOAD "http://openbsd.cs.toronto.edu/cgi-bin/cvsweb/~checkout~/src/sys/sys/queue.h" "${PROJECT_BINARY_DIR}/src_generated/queue.h" STATUS result)
  192. list(GET result 0 download_ok)
  193. if(NOT ${download_ok} MATCHES 0)
  194. file(REMOVE "${PROJECT_BINARY_DIR}/src_generated/queue.h") # remove empty file if created
  195. message(FATAL_ERROR "queue.h could not be downloaded")
  196. endif()
  197. endif()
  198. endif(WIN32)
  199. # build api server specificaion
  200. #add_executable(api-design examples/api-design/server.c)
  201. #target_link_libraries(api-design open62541)
  202. #if(WIN32)
  203. # target_link_libraries(api-design ws2_32)
  204. #endif(WIN32)
  205. #if(MULTITHREADING)
  206. # target_link_libraries(api-design urcu-cds urcu)
  207. #endif(MULTITHREADING)