CMakeLists.txt 9.4 KB

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