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