CMakeLists.txt 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. if(NOT UA_NODESET_DIR)
  26. set(UA_NODESET_DIR ${open62541_NODESET_DIR})
  27. endif()
  28. function(assign_source_group)
  29. # define empty function. We don't need it in standalone
  30. endfunction(assign_source_group)
  31. include_directories(${PROJECT_BINARY_DIR}/src_generated)
  32. endif()
  33. # Required for common.h header file used in examples
  34. include_directories(${CMAKE_CURRENT_LIST_DIR})
  35. if(UA_ENABLE_AMALGAMATION)
  36. add_definitions(-DUA_ENABLE_AMALGAMATION)
  37. endif()
  38. #############################
  39. # Compiled binaries folders #
  40. #############################
  41. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/examples)
  42. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/bin/examples)
  43. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/bin/examples)
  44. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO ${CMAKE_BINARY_DIR}/bin/examples)
  45. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL ${CMAKE_BINARY_DIR}/bin/examples)
  46. macro(add_example EXAMPLE_NAME EXAMPLE_SOURCE)
  47. add_executable(${EXAMPLE_NAME} ${STATIC_OBJECTS} ${EXAMPLE_SOURCE} ${ARGN} ${PROJECT_SOURCE_DIR}/common.h)
  48. target_link_libraries(${EXAMPLE_NAME} open62541::open62541)
  49. assign_source_group(${EXAMPLE_SOURCE})
  50. set_target_properties(${EXAMPLE_NAME} PROPERTIES FOLDER "open62541/examples")
  51. set_target_properties(${EXAMPLE_NAME} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
  52. if(UA_COMPILE_AS_CXX)
  53. set_source_files_properties(${EXAMPLE_SOURCE} PROPERTIES LANGUAGE CXX)
  54. endif()
  55. endmacro()
  56. #############
  57. # Tutorials #
  58. #############
  59. add_example(tutorial_datatypes tutorial_datatypes.c)
  60. add_example(tutorial_server_firststeps tutorial_server_firststeps.c)
  61. add_example(tutorial_server_variable tutorial_server_variable.c)
  62. add_example(tutorial_server_datasource tutorial_server_datasource.c)
  63. if(UA_ENABLE_SUBSCRIPTIONS)
  64. add_example(tutorial_server_monitoreditems tutorial_server_monitoreditems.c)
  65. endif()
  66. add_example(tutorial_server_variabletype tutorial_server_variabletype.c)
  67. add_example(tutorial_server_object tutorial_server_object.c)
  68. if(UA_ENABLE_METHODCALLS)
  69. add_example(tutorial_server_method tutorial_server_method.c)
  70. if (UA_MULTITHREADING EQUAL 100)
  71. add_example(tutorial_server_method_async tutorial_server_method_async.c)
  72. endif()
  73. endif()
  74. add_example(tutorial_client_firststeps tutorial_client_firststeps.c)
  75. add_example(tutorial_client_events tutorial_client_events.c)
  76. if(UA_ENABLE_SUBSCRIPTIONS_EVENTS)
  77. add_example(tutorial_server_events tutorial_server_events.c)
  78. endif()
  79. ##################
  80. # Example Server #
  81. ##################
  82. add_example(server_ctt server_ctt.c)
  83. install(PROGRAMS $<TARGET_FILE:server_ctt>
  84. DESTINATION bin
  85. RENAME ua_server_ctt.exe)
  86. ##################
  87. # Example Client #
  88. ##################
  89. add_example(client client.c)
  90. add_example(client_connect client_connect.c)
  91. if(UA_ENABLE_HISTORIZING)
  92. add_example(client_historical client_historical.c)
  93. endif()
  94. install(PROGRAMS $<TARGET_FILE:client>
  95. DESTINATION bin
  96. RENAME ua_client)
  97. add_example(client_async client_async.c)
  98. if(UA_ENABLE_METHODCALLS)
  99. if (UA_MULTITHREADING EQUAL 100)
  100. add_example(client_method_async client_method_async.c)
  101. endif()
  102. endif()
  103. add_example(client_connect_loop client_connect_loop.c)
  104. add_example(client_connectivitycheck_loop client_connectivitycheck_loop.c)
  105. if(UA_ENABLE_SUBSCRIPTIONS)
  106. add_example(client_subscription_loop client_subscription_loop.c)
  107. endif()
  108. ####################
  109. # Feature Examples #
  110. ####################
  111. add_example(server_mainloop server_mainloop.c)
  112. add_example(server_instantiation server_instantiation.c)
  113. add_example(server_repeated_job server_repeated_job.c)
  114. add_example(server_inheritance server_inheritance.c)
  115. if(UA_ENABLE_HISTORIZING)
  116. add_example(tutorial_server_historicaldata tutorial_server_historicaldata.c)
  117. endif()
  118. if(UA_ENABLE_ENCRYPTION)
  119. add_example(server_encryption encryption/server_encryption.c)
  120. add_example(client_encryption encryption/client_encryption.c)
  121. target_include_directories(server_encryption PRIVATE "${PROJECT_SOURCE_DIR}/examples")
  122. target_include_directories(client_encryption PRIVATE "${PROJECT_SOURCE_DIR}/examples")
  123. endif()
  124. add_example(custom_datatype_client custom_datatype/client_types_custom.c)
  125. add_example(custom_datatype_server custom_datatype/server_types_custom.c)
  126. if(UA_ENABLE_NODEMANAGEMENT)
  127. add_example(access_control_server access_control/server_access_control.c)
  128. add_example(access_control_client access_control/client_access_control.c)
  129. if(UA_ENABLE_ENCRYPTION)
  130. add_example(access_control_client_encrypt access_control_encrypt/client_access_control_encrypt.c)
  131. endif()
  132. endif()
  133. if(UA_ENABLE_DISCOVERY_MULTICAST)
  134. add_example(discovery_server_lds discovery/server_lds.c)
  135. add_example(discovery_server_register discovery/server_register.c)
  136. add_example(discovery_client_find_servers discovery/client_find_servers.c)
  137. add_example(discovery_server_multicast discovery/server_multicast.c)
  138. endif()
  139. add_subdirectory(nodeset)
  140. ####################
  141. # Example PubSub #
  142. ####################
  143. if(UA_ENABLE_PUBSUB)
  144. add_example(tutorial_pubsub_connection pubsub/tutorial_pubsub_connection.c)
  145. add_example(tutorial_pubsub_publish pubsub/tutorial_pubsub_publish.c)
  146. add_example(server_pubsub_publisher_iop pubsub/server_pubsub_publisher_iop.c)
  147. add_example(server_pubsub_publish_rt_level pubsub/server_pubsub_publisher_rt_level.c)
  148. if(UA_ENABLE_AMALGAMATION)
  149. message(WARNING "PubSub subscriber tutorial (preview) can not be used with AMALGAMATION. Skipping tutorial_pubsub_subscribe.")
  150. else(NOT UA_ENABLE_AMALGAMATION)
  151. add_example(tutorial_pubsub_subscribe pubsub/tutorial_pubsub_subscribe.c)
  152. add_example(pubsub_subscribe_standalone pubsub/pubsub_subscribe_standalone.c)
  153. endif()
  154. if(UA_ENABLE_PUBSUB_MQTT)
  155. if(NOT WIN32)
  156. include_directories(${PROJECT_SOURCE_DIR}/../plugins)
  157. add_example(tutorial_pubsub_mqtt_publish pubsub/tutorial_pubsub_mqtt_publish.c)
  158. endif()
  159. endif()
  160. endif()