Reputation: 95
I have a WiX installer for my library and I want to create the PACKAGE-config-version.cmake
file during the install process, since it uses the ${PROJECT_VERSION}
variable which is contained in my top level CMake file, which also contains all the installation utility.
I know I could just create a file, write the content to it and install it, but how can I do that without explicitly generating the file beforehand and possibly deleting it afterwards? How can I achieve, that the file only exists during the install process, so that I don't have to worry about cleaning stuff up afterwards or actually deleting the file before it can be installed etc. ?
Upvotes: 1
Views: 496
Reputation: 20798
For that specific case I would use the configure_file()
command:
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/PACKAGE-config-version.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/PACKAGE-config-version.cmake
)
That way, changes to the original are taken in account as expected. Then the install()
installs the resulting file to the destination:
install(
FILES
${CMAKE_CURRENT_BINARY_DIR}/PACKAGE-config-version.cmake
DESTINATION
share/cmake/PACKAGE
)
(I assume "PACKAGE" is properly defined for the purpose, i.e. the destination directory is likely CamelCase.)
Otherwise, there is a script option in the install()
command:
Here is the synopsis:
install([[SCRIPT <file>] [CODE <code>]]
[ALL_COMPONENTS | COMPONENT <component>]
[EXCLUDE_FROM_ALL] [...])
The CODE
is like a bash script written inline (inside your CMakeLists.txt
file) so you could create the file using such. Watch out as you need to escape all sorts of characters.
You can also create a separate script (i.e. install(SCRIPT generate-version-file.sh)
). That shell script is in charge of creating the file (making it easier to test the script since you can run it from your console to verify that it works as expected).
Upvotes: 0