cmake : How to change file permissions when installing?
There is no FILE_PERMISSIONS
argument in install(FILES ...)
. Use PERMISSIONS
instead:
install(
FILES common.sh
PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ
DESTINATION /rootfs/usr/bin
)
Change permissions of file in CMake
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:
- Create the file as shown in the question but in a temporary folder
${CMAKE_BINARY_DIR}/tmp
- Use
file (COPY ${CMAKE_BINARY_DIR}/tmp/somescript.sh DESTINATION ${CMAKE_BINARY_DIR} FILE_PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ)
- Remove the old file and the temporary directory:
file(REMOVE_RECURSIVE ${CMAKE_BINARY_DIR}/tmp/)
How add execution permissions to file generated by CMake?
Copying file "at place" is bad idea in most cases.
Instead, configure file into one (intermediate) place, and copy it (with changed permissions) into the final location:
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/wrapper.sh.in
${CMAKE_CURRENT_SOURCE_DIR}/tmp/wrapper.sh
@ONLY
)
file(
COPY ${CMAKE_CURRENT_SOURCE_DIR}/tmp/wrapper.sh
DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}
FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
)
Set permissions of the installation directory
to do so, you may use install
command w/ the following signature:
install([[SCRIPT <file>] [CODE <code>]] [...])`
and write a code to change permissions at install time. I.e. smth like this:
execute_process(COMMAND /bin/chmod ARGS 0700 "${CMAKE_INSTALL_PREFIX}")
put this to a chmod.cmake
or to a string and then call install
above.
Change an existing file's permissions using cmake
Write a shell script that changes permissions and run it during installation using install(CODE "execute_process(COMMAND \"yourscript.sh\")"
.
CMake - setting file permissions after installation (java)
The install_jar()
definition is, basically, just install()
call:
function(INSTALL_JAR _TARGET_NAME _DESTINATION)
get_property(__FILES
TARGET ${_TARGET_NAME}
PROPERTY INSTALL_FILES
)
if (__FILES)
install(FILES ${__FILES}
DESTINATION ${_DESTINATION}
)
else (__FILES)
message(SEND_ERROR "The target ${_TARGET_NAME} is not known in this scope.")
endif (__FILES)
endfunction(INSTALL_JAR _TARGET_NAME _DESTINATION)
So you can just write your own install_jar_with_args()
and add PERMISSIONS
keyword to the install()
call.
Probably, this problem needs to be reported to CMake devs.
Keep a single file's permissions when using install() in CMake
You can install files with +x
permission using
install(PROGRAMS ...
command.
Alternatively, you can install whole directory preserving file permissions:
install(DIRECTORIES ... USE_SOURCE_PERMISSIONS)
See documentation for install command for more info.
Related Topics
How to Tail -F the Latest Log File with a Given Pattern
How to Implement a Practical Fiber Scheduler
Custom Git Command Autocompletion
Critical Timing in an Arm Linux Kernel Driver
Accessing Linux /Dev/Usb as Standard Files to Communicate with Usb Device
Simulate Network Latency on Specific Port Using Tc
Retrieve Plain Text Script from Compiled Bash Script
One Liner to Rename Bunch of Files
Systemd: Start Service at Boot Time After Network Is Really Up (For Wol Purpose)
Finding Original MAC Address from Hardware Itself
Making Gcc Prefer Static Libs to Shared Objects When Linking
How to Gzip Standard in to a File and Also Print Standard in to Standard Out
Linux Join Utility Complains About Input File Not Being Sorted
Shell Script Calls Sudo; How to Suppress the Password Prompt