CMakeLists.txt 6.7 KB

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