using build preset with ExternalProject_Add

The build directory shouldn’t be given after --build if you’re also giving the --preset option. I think that’s what @ClausKlein was trying to communicate. Here’s how the documentation of the --preset option explains it:

–preset , --preset=

Use a build preset to specify build options. The project binary directory is inferred from the configurePreset key. The current working directory must contain CMake preset files. See preset for more details.

When you use ExternalProject_Add(), its defaults for the build command come from a time before presets existed. It sets the working directory to the build directory, which isn’t what you need if you want to use a build preset. You can work around that by using the cmake -E chdir command to issue a command with a different working directory. Here’s how you would do that:

ExternalProject_Add(someTarget
    # ... Specify where to get things using GIT_REPOSITORY or whatever ...
    BUILD_COMMAND
        ${CMAKE_COMMAND} -E chdir <SOURCE_DIR>
            ${CMAKE_COMMAND} --build --preset YourPresetName
)

I’ve used indenting to make things a bit clearer to read, but that’s just personal preference. The ExternalProject_Add() command automatically replaces <SOURCE_DIR> with the source directory, so it’s a convenient way to avoid having to work out where that will be.

3 Likes