I have a subproject in my project for generating code used in the project, however i want to include it using ExternalProject so it can be built and ran regardless of the toolchain i use for the main project.
It mostly works except i can't figure out how to use it in add_custom_command, since i want it to use the target rule specified in the docs:
If COMMAND specifies an executable target name (created by the add_executable() command), it will automatically be replaced by the location of the executable created at build time [...]
Simple example of my project setup that replicates the issue: https://godbolt.org/z/of8G4c4Gf
Replacing the ExternalProject_Add with a simple add_subdirectory makes it work, but wouldn't do what i want in the case of using a different toolchain.
ExternalProject_Addwill add thecodegentarget. However, CMake has no idea what that target is doing and what output it will provide, as that info is now hidden away in the external CMake run. So the outer CMake run has no idea about thecodegenbinary produced by that step and where it will be located. You need to provide the full path to the executable toadd_custom_commandmanually.ExternalProject_Addwill buildcodegenand place the resulting binary in a subdirectory inside your build directory. The exact location is toolchain dependent, so it can be tricky to reconstruct the full target path. The most robust way to solve this is:codegenproject. This will allow you to portably specify which path the installed binary will end up in relative to the install root directory.INSTALL_DIRoption onExternalProject_Addto a subdirectory inside yourPROJECT_BINARY_DIR.codegenbinary in the custom command of the outer CMake project. To work on multiple platforms, you may need to make use of theCMAKE_EXECUTABLE_SUFFIXvariable.