Wednesday, February 7, 2024

[SOLVED] Change permissions of file in CMake

Issue

I created a shell script using CMake just like this: file(WRITE somescript.sh "echo foo!")

I want the user to be able to execute that script right away. For this, it would be nice to change the permissions of that file from within CMake.

Is there something like chmod u+x somescript.sh in CMake?


Solution

The problem with install is that existing files are not overwritten when the destination directory is equal to the source directory and when the files themselves did not change (CMake seems to compare timestamps). But nevermind, I found a working solution for everyone reading along:

  1. Create the file as shown in the question but in a temporary folder ${CMAKE_BINARY_DIR}/tmp
  2. Use file (COPY ${CMAKE_BINARY_DIR}/tmp/somescript.sh DESTINATION ${CMAKE_BINARY_DIR} FILE_PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ)
  3. Remove the old file and the temporary directory: file(REMOVE_RECURSIVE ${CMAKE_BINARY_DIR}/tmp/)


Answered By - cpeters
Answer Checked By - Dawn Plyler (WPSolving Volunteer)