CMakeLists.txt 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. cmake_minimum_required(VERSION 2.6)
  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. # main sources of libopen62541
  8. include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include")
  9. include_directories("${CMAKE_CURRENT_SOURCE_DIR}/src")
  10. file(GLOB_RECURSE headers "${CMAKE_CURRENT_SOURCE_DIR}/src/*.h")
  11. file(GLOB generated_headers "${PROJECT_BINARY_DIR}/src_generated/*.h")
  12. set(lib_sources src/ua_types.c
  13. src/ua_types_encoding_binary.c
  14. ${PROJECT_BINARY_DIR}/src_generated/ua_types_generated.c
  15. ${PROJECT_BINARY_DIR}/src_generated/ua_namespace_0.c
  16. src/ua_transport.c
  17. ${PROJECT_BINARY_DIR}/src_generated/ua_transport_generated.c
  18. src/ua_connection.c
  19. src/ua_securechannel.c
  20. src/ua_session.c
  21. src/ua_util.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}/tools/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}/tools/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}/tools/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}/tools/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}/tools/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}/tools/schema/Custom.Opc.Ua.Transport.bsd)
  134. # build example client
  135. option(CLIENT "Build a test client" OFF)
  136. if(CLIENT)
  137. message(STATUS "Extensions: enabling client")
  138. add_definitions( -DBENCHMARK=1 )
  139. add_executable(exampleClient examples/opcuaClient.c)
  140. target_link_libraries(exampleClient open62541)
  141. endif()
  142. # build example server
  143. set(server_sources examples/opcuaServer.c
  144. examples/logger_stdout.c)
  145. if(NOT ENABLE_MULTITHREADING)
  146. list(APPEND server_sources examples/networklayer_tcp.c)
  147. else()
  148. list(APPEND server_sources examples/networklayer_tcp_concurrent.c)
  149. endif()
  150. add_executable(exampleServer ${server_sources})
  151. target_link_libraries(exampleServer open62541)
  152. if(WIN32)
  153. target_link_libraries(exampleServer ws2_32)
  154. endif(WIN32)
  155. if(ENABLE_MULTITHREADING)
  156. find_package(LibUV REQUIRED)
  157. target_link_libraries(exampleServer urcu-cds urcu uv)
  158. endif()
  159. # build unit tests
  160. option(ENABLE_UNIT_TESTS "Run unit tests after building" OFF)
  161. if(ENABLE_UNIT_TESTS)
  162. enable_testing()
  163. add_subdirectory(tests)
  164. endif()
  165. # build documentation
  166. option(GENERATE_DOCUMENTATION "Generate doxygen documentation" OFF)
  167. if(GENERATE_DOCUMENTATION)
  168. find_package(Doxygen)
  169. if(NOT DOXYGEN_FOUND)
  170. message(FATAL_ERROR "Doxygen is not installed or not properly configured")
  171. endif(NOT DOXYGEN_FOUND)
  172. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/doc/Doxyfile.in ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY)
  173. add_custom_target(doc
  174. ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
  175. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  176. COMMENT "Generating API documentation with Doxygen")
  177. endif()
  178. # download queue.h if required
  179. if(WIN32)
  180. if(NOT EXISTS "${PROJECT_BINARY_DIR}/src_generated/queue.h")
  181. 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)
  182. list(GET result 0 download_ok)
  183. if(NOT ${download_ok} MATCHES 0)
  184. file(REMOVE "${PROJECT_BINARY_DIR}/src_generated/queue.h") # remove empty file if created
  185. message(FATAL_ERROR "queue.h could not be downloaded")
  186. endif()
  187. endif()
  188. endif(WIN32)
  189. # build api server specificaion
  190. #add_executable(api-design examples/api-design/server.c)
  191. #target_link_libraries(api-design open62541)
  192. #if(WIN32)
  193. # target_link_libraries(api-design ws2_32)
  194. #endif(WIN32)
  195. #if(MULTITHREADING)
  196. # target_link_libraries(api-design urcu-cds urcu)
  197. #endif(MULTITHREADING)