examples.cmake 2.2 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 "-Dprotobuf_MSVC_STATIC_RUNTIME:BOOL=${protobuf_MSVC_STATIC_RUNTIME}"
  19. "-DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}"
  20. "-Dprotobuf_VERBOSE:BOOL=${protobuf_VERBOSE}"
  21. ${ARGN}
  22. )
  23. set_property(TARGET ${NAME} PROPERTY FOLDER "Examples")
  24. set_property(TARGET ${NAME} PROPERTY EXCLUDE_FROM_ALL TRUE)
  25. endfunction()
  26. # Add examples as an external project.
  27. # sub_directory cannot be used because the find_package(protobuf) call would cause failures with redefined targets.
  28. add_examples_build(examples "-Dprotobuf_DIR:PATH=${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_CMAKEDIR}")
  29. add_dependencies(examples libprotobuf protoc)
  30. option(protobuf_BUILD_EXAMPLES_MULTITEST "Build Examples in multiple configurations. Useful for testing." OFF)
  31. mark_as_advanced(protobuf_BUILD_EXAMPLES_MULTITEST)
  32. if(protobuf_BUILD_EXAMPLES_MULTITEST)
  33. set_property(GLOBAL PROPERTY USE_FOLDERS ON)
  34. #Build using the legacy compatiblity module.
  35. add_examples_build(examples-legacy
  36. "-Dprotobuf_DIR:PATH=${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_CMAKEDIR}"
  37. "-Dprotobuf_MODULE_COMPATIBLE:BOOL=TRUE"
  38. )
  39. add_dependencies(examples-legacy libprotobuf protoc)
  40. #Build using the installed library.
  41. add_examples_build(examples-installed
  42. "-Dprotobuf_DIR:PATH=${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_CMAKEDIR}"
  43. )
  44. #Build using the installed library in legacy compatiblity mode.
  45. add_examples_build(examples-installed-legacy
  46. "-Dprotobuf_DIR:PATH=${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_CMAKEDIR}"
  47. "-Dprotobuf_MODULE_COMPATIBLE:BOOL=TRUE"
  48. )
  49. endif()