CMakeLists.txt 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. cmake_minimum_required(VERSION 3.0)
  2. project(open62541-examples)
  3. # This examples folder can also be built standalone.
  4. # First install open62541 using `make install` then
  5. # copy this folder to any other location and call CMake directly:
  6. #
  7. # cp -r open62541/examples $HOME/open62541_examples
  8. # cd $HOME/open62541_examples
  9. # mkdir build && cd build
  10. # cmake -DUA_NAMESPACE_ZERO=FULL ..
  11. # make -j
  12. if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
  13. # Examples are built standalone.
  14. find_package(open62541 REQUIRED)
  15. if(UA_TOOLS_DIR)
  16. # build with cmake tools from open62541 source_dir
  17. # cmake -DUA_TOOLS_DIR="<path-to-tools-dir>" ..
  18. get_filename_component(ABSOLUTE_PATH ${UA_TOOLS_DIR} ABSOLUTE)
  19. set(CMAKE_MODULE_PATH "${ABSOLUTE_PATH}/cmake")
  20. else()
  21. # build with installed cmake tools, i.e. from /usr/share/open62541/tools/cmake
  22. # cmake ..
  23. set(CMAKE_MODULE_PATH "${open62541_TOOLS_DIR}/cmake")
  24. set(UA_TOOLS_DIR ${open62541_TOOLS_DIR})
  25. endif()
  26. function(assign_source_group)
  27. # define empty function. We don't need it in standalone
  28. endfunction(assign_source_group)
  29. include_directories(${PROJECT_BINARY_DIR}/src_generated)
  30. add_custom_target(open62541-object)
  31. file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/src_generated")
  32. set(examples_headers
  33. ${examples_headers}
  34. ${PROJECT_SOURCE_DIR}/common.h
  35. )
  36. else()
  37. # Examples are built inside the main repo
  38. include_directories(${CMAKE_SOURCE_DIR}/include)
  39. include_directories(${CMAKE_SOURCE_DIR}/plugins)
  40. include_directories(${CMAKE_BINARY_DIR})
  41. include_directories(${CMAKE_SOURCE_DIR}/examples)
  42. include_directories(${CMAKE_SOURCE_DIR}/arch)
  43. include_directories(${CMAKE_SOURCE_DIR}/plugins/historydata)
  44. set(examples_headers
  45. ${examples_headers}
  46. ${PROJECT_SOURCE_DIR}/common.h
  47. )
  48. endif()
  49. if(UA_ENABLE_AMALGAMATION)
  50. add_definitions(-DUA_ENABLE_AMALGAMATION)
  51. endif()
  52. #############################
  53. # Compiled binaries folders #
  54. #############################
  55. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/examples)
  56. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/bin/examples)
  57. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/bin/examples)
  58. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO ${CMAKE_BINARY_DIR}/bin/examples)
  59. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL ${CMAKE_BINARY_DIR}/bin/examples)
  60. macro(add_example EXAMPLE_NAME EXAMPLE_SOURCE)
  61. add_executable(${EXAMPLE_NAME} ${STATIC_OBJECTS} ${EXAMPLE_SOURCE} ${ARGN} ${examples_headers})
  62. target_link_libraries(${EXAMPLE_NAME} open62541 ${open62541_LIBRARIES})
  63. assign_source_group(${EXAMPLE_SOURCE})
  64. add_dependencies(${EXAMPLE_NAME} open62541-object)
  65. set_target_properties(${EXAMPLE_NAME} PROPERTIES FOLDER "open62541/examples")
  66. set_target_properties(${EXAMPLE_NAME} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
  67. if(UA_COMPILE_AS_CXX)
  68. set_source_files_properties(${EXAMPLE_SOURCE} PROPERTIES LANGUAGE CXX)
  69. endif()
  70. endmacro()
  71. #############
  72. # Tutorials #
  73. #############
  74. add_example(tutorial_datatypes tutorial_datatypes.c)
  75. add_example(tutorial_server_firststeps tutorial_server_firststeps.c)
  76. add_example(tutorial_server_variable tutorial_server_variable.c)
  77. add_example(tutorial_server_datasource tutorial_server_datasource.c)
  78. if(UA_ENABLE_SUBSCRIPTIONS)
  79. add_example(tutorial_server_monitoreditems tutorial_server_monitoreditems.c)
  80. endif()
  81. add_example(tutorial_server_variabletype tutorial_server_variabletype.c)
  82. add_example(tutorial_server_object tutorial_server_object.c)
  83. if(UA_ENABLE_METHODCALLS)
  84. add_example(tutorial_server_method tutorial_server_method.c)
  85. endif()
  86. add_example(tutorial_client_firststeps tutorial_client_firststeps.c)
  87. add_example(tutorial_client_events tutorial_client_events.c)
  88. if(UA_ENABLE_SUBSCRIPTIONS_EVENTS)
  89. add_example(tutorial_server_events tutorial_server_events.c)
  90. endif()
  91. ##################
  92. # Example Server #
  93. ##################
  94. add_example(server_ctt server_ctt.c)
  95. install(PROGRAMS $<TARGET_FILE:server_ctt>
  96. DESTINATION bin
  97. RENAME ua_server_ctt.exe)
  98. ##################
  99. # Example Client #
  100. ##################
  101. add_example(client client.c)
  102. if(UA_ENABLE_HISTORIZING)
  103. add_example(client_historical client_historical.c)
  104. endif()
  105. install(PROGRAMS $<TARGET_FILE:client>
  106. DESTINATION bin
  107. RENAME ua_client)
  108. add_example(client_async client_async.c)
  109. add_example(client_connect_loop client_connect_loop.c)
  110. add_example(client_connectivitycheck_loop client_connectivitycheck_loop.c)
  111. if(UA_ENABLE_SUBSCRIPTIONS)
  112. add_example(client_subscription_loop client_subscription_loop.c)
  113. endif()
  114. ####################
  115. # Feature Examples #
  116. ####################
  117. add_example(server_mainloop server_mainloop.c)
  118. add_example(server_instantiation server_instantiation.c)
  119. add_example(server_repeated_job server_repeated_job.c)
  120. add_example(server_inheritance server_inheritance.c)
  121. if(UA_ENABLE_HISTORIZING)
  122. add_example(tutorial_server_historicaldata tutorial_server_historicaldata.c)
  123. endif()
  124. if(UA_ENABLE_ENCRYPTION)
  125. add_example(server_basic128rsa15 encryption/server_basic128rsa15.c)
  126. add_example(server_basic256sha256 encryption/server_basic256sha256.c)
  127. add_example(client_encryption encryption/client_encryption.c)
  128. # common.h
  129. target_include_directories(server_basic128rsa15 PRIVATE "${PROJECT_SOURCE_DIR}/examples")
  130. target_include_directories(server_basic256sha256 PRIVATE "${PROJECT_SOURCE_DIR}/examples")
  131. target_include_directories(client_encryption PRIVATE "${PROJECT_SOURCE_DIR}/examples")
  132. endif()
  133. add_example(custom_datatype_client custom_datatype/client_types_custom.c)
  134. add_example(custom_datatype_server custom_datatype/server_types_custom.c)
  135. if(UA_ENABLE_NODEMANAGEMENT)
  136. add_example(access_control_server access_control/server_access_control.c)
  137. add_example(access_control_client access_control/client_access_control.c)
  138. endif()
  139. if(UA_BUILD_SELFSIGNED_CERTIFICATE)
  140. find_package(OpenSSL REQUIRED)
  141. add_custom_command(OUTPUT server_cert.der
  142. COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_SOURCE_DIR}/tools/certs/create_self-signed.py ${CMAKE_CURRENT_BINARY_DIR}
  143. DEPENDS ${CMAKE_SOURCE_DIR}/tools/certs/create_self-signed.py
  144. ${CMAKE_SOURCE_DIR}/tools/certs/localhost.cnf)
  145. add_custom_target(selfsigned ALL DEPENDS server_cert.der)
  146. add_executable(server_certificate server_certificate.c ${STATIC_OBJECTS} server_cert.der)
  147. target_link_libraries(server_certificate open62541 ${open62541_LIBRARIES})
  148. endif()
  149. if(UA_ENABLE_DISCOVERY_MULTICAST)
  150. add_example(discovery_server_lds discovery/server_lds.c)
  151. add_example(discovery_server_register discovery/server_register.c)
  152. add_example(discovery_client_find_servers discovery/client_find_servers.c)
  153. add_example(discovery_server_multicast discovery/server_multicast.c)
  154. endif()
  155. add_subdirectory(nodeset)
  156. ####################
  157. # Example PubSub #
  158. ####################
  159. if(UA_ENABLE_PUBSUB)
  160. add_example(tutorial_pubsub_connection pubsub/tutorial_pubsub_connection.c)
  161. add_example(tutorial_pubsub_publish pubsub/tutorial_pubsub_publish.c)
  162. if(UA_ENABLE_AMALGAMATION)
  163. message(WARNING "PubSub subscriber tutorial (preview) can not be used with AMALGAMATION. Skipping tutorial_pubsub_subscribe.")
  164. else(NOT UA_ENABLE_AMALGAMATION)
  165. add_example(tutorial_pubsub_subscribe pubsub/tutorial_pubsub_subscribe.c)
  166. add_example(pubsub_subscribe_standalone pubsub/pubsub_subscribe_standalone.c)
  167. endif()
  168. endif()