I’m trying to use CPACK_PRE_BUILD_SCRIPTS to filter out certain files from the staging directory before CPack begins packaging. The use case for this is that we call on an external package manager executable to copy binaries into our install directory. That’s done through install(CODE …).

Unfortunately, some of the binaries being copied into the install directory aren’t required or desired in the final package, such as test executables and other CI tools. I was hoping to use CPACK_PRE_BUILD_SCRIPTS to remove any unwanted files from the staging directory before CPack does it’s thing.

This is where my issue lies, the script I have that’s run for CPACK_PRE_BUILD_SCRIPTS doesn’t have it’s CMAKE_ variables initialised to what I’d expect. If I have the following pre-build script:


Then I get the following output during CPack:

CMAKE_INSTALL_PREFIX = C:/Program Files (x86)

At the very least I was hoping that CMAKE_INSTALL_PREFIX would be initialised to the staging directory. Currently it’s not even set to what’s in my CMakeCache.

Is this expected behaviour for CPACK_PRE_BUILD_SCRIPTS?

Currently using CMake 3.19.3
Windows 10
Tried Visual Studio 2019 and Ninja CMake generators
Tried ZIP and NSIS CPack generators

You’re probably looking for $ENV{DESTDIR} which is what CPack uses to “inject” itself into the installation procedure. That said, not all of the variables are available.

I think you probably want to use components for the bits you want and then install only those components rather than trying to undo installation of some bits.

I had a look at the docs for DESTDIR and it doesn’t seem like I would be able to use it in this case as it wouldn’t work on Windows.

Unfortunately I don’t have the granular control to be able to use components.
Effectively what I’m doing is the following

install(CODE "execute_process(COMMAND an-executable-that-copies-binaries-into-install-folder)")

And that executable doesn’t provide the API to be able to filter what’s going to be copied.

The docs for CPACK_INSTALL_SCRIPTS state the following

For every script, the following variables will be set: CMAKE_CURRENT_SOURCE_DIR, CMAKE_CURRENT_BINARY_DIR and CMAKE_INSTALL_PREFIX (which is set to the staging install directory)

I was hoping the same would apply to CPACK_PRE_BUILD_SCRIPTS since they seem to be related, but unfortunately not.

I have an alternative solution which doesn’t make use of CPACK_PRE_BUILD_SCRIPTS and instead just extends the code in the install(CODE …) mentioned above to include the same script as I would assign to CPACK_PRE_BUILD_SCRIPTS. In that case CMAKE_INSTALL_PREFIX will be initialised correctly to the staging directory.

This has already been raised in the CMake issue tracker (at least in part, for setting some of the variables).

Thank you. I did have a quick search for existing bugs, but I obviously didn’t do a very good job of it. I’ll keep an eye on the bug. Thanks again.