Generator expression $<INSTALL_PREFIX> doesn't work with env variable DESTDIR

Using the generator expression $<INSTALL_PREFIX> in install(CODE ) should work with CMake 3.27 and later. But the generator expression doesn’t work with the enviroment variable DESTDIR. I don’t know if this is the intended behavior.

Example

cmake_minimum_required(VERSION 3.27)

project(install_code_bug)

install(CODE "file(TOUCH $<INSTALL_PREFIX>/test_file.txt)")

Configure the above project cmake -B ./build -S ./ && mkdir install.

Running unset DESTDIR && cmake --install ./build --prefix "$(pwd)/install" will result in file ‘install/test_file.txt’ been created.

However, if DESTDIRis used like this export DESTDIR="$(pwd)/install" && cmake --install ./build it will result in an error:

CMake Error at build/cmake_install.cmake:46 (file):
  file problem touching file: /usr/local/test_file.txt

cmake version 3.31.6

OS: Ubuntu 25.10

$ENV{DESTDIR}$<INSTALL_PREFIX> is what you should use here.

This only works if `DESTDIR` is set during configuration.

Yes, it’ll need the proper level of escaping to end up in the code as intended.