Search code examples
c++xcodecmakeflatbuffers

How to get the output path of a target defined by ExternalProject?


I'm building Google's FlatBuffers as a dependency for my own project and I need to compile a schema at build-time. I don't want to use BuildFlatBuffers.cmake or FindFlatBuffers.cmake because I'm using a specific version and I can't rely on it being locally installed.

This is a simplified version of my CMakeLists.txt:

ExternalProject_Add (
  flatbuf
  URL "https://github.com/google/flatbuffers/archive/v1.8.0.tar.gz"
  CMAKE_ARGS -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
)
add_custom_target (
  flatbuf_schema
  PREFIX ${FLATBUF_PREFIX}
  DEPENDS flatbuf
  COMMAND ${FLATBUF_PREFIX}/src/flatbuf-build/flatc --cpp ${FLATBUF_SCHEMA}
)

It works fine for Make and Ninja but fails in Xcode, which builds flatc in the Debug directory.

I thought about these possible solutions:

  • use add_subdirectory instead of ExternalProject_Add so that I can use ${FLATBUFFERS_FLATC_EXECUTABLE} or $<TARGET_FILE:flatc>;
  • manually assign a RUNTIME_OUTPUT_DIRECTORY for flatbuf;
  • search for flatc in multiple paths (not portable; I also don't know how to make it happen at build-time).

I tried (2) and (3) but without success. As for (1), I'm not sure it's a good idea. How can I build schemas in a portable manner?


Solution

  • Apparently CMake provides a variable to solve this exact problem, i.e. CMAKE_CFG_INTDIR (docs). The path to the flatc executable should then be

    ExternalProject_Get_Property(flatbuf BINARY_DIR)
    set (FLATC "${BINARY_DIR}/${CMAKE_CFG_INTDIR}/flatc")