for a part of my project I use a qnx compiler that needs QNX_HOST and QNX_TARGET environment variables to be set.
To archive that I use cmake --preset for configuration and for building
the project is build for QNX and for Linux therefore I also used the preset to set the toolchainfile and some cached variables
because both variants (QNX and Linux) should be delivered I now wanted to use ExternalProject_Add to build both variants and include the build output of the linux and the qnx version in one package.
with the CMAKE_ARGS parameter of ExternalProject_Add it is possible to specify a preset but it is only used as a configuration preset
the default build commad is make --build and it is run inside the build directory. Changing it to cmake --build ./ --preset=linux does not work because the cmakepreset.json is not in the build directory. I didt find a way to set the directory in which the build command is run
is there a way to use a build preset with ExternalProject_Add?
thank you for your fast reply
but there is no build command for the FetchContents or did I miss something?
In the SubProject I add with ExternalProject_Add I want to use the build preset from the CMakePrests.json of SubProject Project not from the master Project.
At the moment I defined a build preset in the master project but only because I didn’t find a way for the ExternalProject_add command to use the build preset from the CMakePresets.json in the external project.
no they are not submodules, I am already using CMakePresets at SubProject level for the configuration step of the SubProject they already work. But I don’t know how to also use the build preset during build step. I am using the SubProject standalone I can use:
it does not state that the option --preset is excluded
neither does it say in the chapter option that --preset is not allowed in case cmake --build is used
how should cmake --build now where to build
I see you should probably define a “binaryDir” in the CMakePreset.json and I am guessing that this maybe will solve my issues but I get why the command it self is wrong in general.
But I will check if setting the "binaryDir in the CMakePreset.json will solve my issues
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.
ok I think things are getting a bit more clear to me. What I am still missing is:
if I set binaryDir in the configure preset shouldn’t it also be used in the configuration step or at least depend on it?
would id make sense to have for example set "binaryDir": "./build/xyz_b", in the CMakePresets file but configure with cmake -B ./build/xyz_c --preset=b?
for my understanding binaryDir almost always depends on the build path configured with cmake -B so in this example it would depend on ./build/xyz_c
Build presets (and also test, package, and workflow presets) rely on you using the binary directory specified in the configure preset. If you override the binary directory when you configure your project (i.e. you use the -B option), you won’t be able to use build presets with it.
@craig.scott
It should not be allowed to use the -B with –preset or –workflow option together! IMHO: And the configure presetmust have a binaryDir item.
ok now I understand, makes sense thank you, I didn’t used the binaryDir option which led to confusion. I also didn’t know that it’s possible to use multiple BUILD_COMMANDS for the ExternalProject_Add.
The example I gave is only one single command for the build step:
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 just split it across multiple lines to make it easier to read, but CMake will still consider that all a single command line. But if needed, you can specify multiple commands. Put the first one after BUILD_COMMAND, and then subsequent ones immediately following it preceded by COMMAND. For example:
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
COMMAND ${CMAKE_COMMAND} -E echo "This is a second command"
)