cpeters
cpeters

Reputation: 117

Change permissions of file in CMake

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?

Upvotes: 2

Views: 11780

Answers (4)

Musholic
Musholic

Reputation: 11

Since cmake version 3.19, it's now possible to use the file(CHMOD ...) command. The syntax is:

file(CHMOD <files>... <directories>... [PERMISSIONS <permissions>...] [FILE_PERMISSIONS <permissions>...] [DIRECTORY_PERMISSIONS <permissions>...])

For example:

file(CHMOD somescript.sh PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ)

See the documentation for more details.

Upvotes: 1

Tim Jen&#223;en
Tim Jen&#223;en

Reputation: 903

In case you want to copy a binary I would use USE_SOURCE_PERMISSIONS

Upvotes: 0

cpeters
cpeters

Reputation: 117

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/)

Upvotes: 4

Robin
Robin

Reputation: 303

You can do that in the istall command. Use this tags:

install(
    FILES somescript.sh
    PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ
    DESTINATION /some/destination
)

Upvotes: 1

Related Questions