examples.cmake 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. if(protobuf_VERBOSE)
  2. message(STATUS "Protocol Buffers Examples Configuring...")
  3. endif()
  4. get_filename_component(examples_dir "../examples" ABSOLUTE)
  5. if(protobuf_VERBOSE)
  6. message(STATUS "Protocol Buffers Examples Configuring done")
  7. endif()
  8. include(ExternalProject)
  9. # Internal utility function: Create a custom target representing a build of examples with custom options.
  10. function(add_examples_build NAME)
  11. ExternalProject_Add(${NAME}
  12. PREFIX ${NAME}
  13. SOURCE_DIR "${examples_dir}"
  14. BINARY_DIR ${NAME}
  15. STAMP_DIR ${NAME}/logs
  16. INSTALL_COMMAND "" #Skip
  17. LOG_CONFIGURE 1
  18. CMAKE_CACHE_ARGS "-DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}"
  19. "-Dprotobuf_VERBOSE:BOOL=${protobuf_VERBOSE}"
  20. ${ARGN}
  21. )
  22. set_property(TARGET ${NAME} PROPERTY FOLDER "Examples")
  23. set_property(TARGET ${NAME} PROPERTY EXCLUDE_FROM_ALL TRUE)
  24. endfunction()
  25. # Add examples as an external project.
  26. # sub_directory cannot be used because the find_package(protobuf) call would cause failures with redefined targets.
  27. add_examples_build(examples "-Dprotobuf_DIR:PATH=${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_CMAKEDIR}")
  28. add_dependencies(examples libprotobuf protoc)
  29. option(protobuf_BUILD_EXAMPLES_MULTITEST "Build Examples in multiple configurations. Useful for testing." OFF)
  30. mark_as_advanced(protobuf_BUILD_EXAMPLES_MULTITEST)
  31. if(protobuf_BUILD_EXAMPLES_MULTITEST)
  32. set_property(GLOBAL PROPERTY USE_FOLDERS ON)
  33. #Build using the legacy compatibility module.
  34. add_examples_build(examples-legacy
  35. "-Dprotobuf_DIR:PATH=${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_CMAKEDIR}"
  36. "-Dprotobuf_MODULE_COMPATIBLE:BOOL=TRUE"
  37. )
  38. add_dependencies(examples-legacy libprotobuf protoc)
  39. #Build using the installed library.
  40. add_examples_build(examples-installed
  41. "-Dprotobuf_DIR:PATH=${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_CMAKEDIR}"
  42. )
  43. #Build using the installed library in legacy compatibility mode.
  44. add_examples_build(examples-installed-legacy
  45. "-Dprotobuf_DIR:PATH=${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_CMAKEDIR}"
  46. "-Dprotobuf_MODULE_COMPATIBLE:BOOL=TRUE"
  47. )
  48. endif()