CMakeLists.txt 9.4 KB

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