CMakeLists.txt 10 KB

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