CMakeLists.txt 11 KB

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