[CMake] File permissions on CONFIGURE_FILE output

Michael Wild themiwi at gmail.com
Wed Feb 3 04:29:57 EST 2010


On 3. Feb, 2010, at 10:16 , Chris Hillery wrote:

> On Wed, Feb 3, 2010 at 1:09 AM, Michael Wild <themiwi at gmail.com> wrote:
> 
>> 
>> On 2. Feb, 2010, at 22:31 , Aaron_Wright at selinc.com wrote:
>> 
>>> I run CONFIGURE_FILE on a file that is read only. The output file is also
>>> read only, which is a problem because I need to append to it. Is there a
>>> way to change this behavior? Or a work around?
>>> 
>> 
>> Either change the source-file permissions or use file(COPY ...) after
>> configure_file.
>> 
> 
> There doesn't seem to be a way in cmake to change file permissions, which is
> an odd hole (there's lots of stuff about setting permissions at
> install-time, but nothing else). You'd have to do something system-specific
> like execute_process(COMMAND chmod).
> 
> There's also no file(COPY).

You probably didn't grep the man-page thoroughly ;-) It is documented as "file(<COPY|INSTALL>"


> I tried an experiment using cmake -E copy, but
> it also maintains permissions just like configure_file()!
> 
> The only work-around I've found is to copy the file "manually":
> 
> configure_file("${CMAKE_SOURCE_DIR}/readonly.txt"
> "${CMAKE_BINARY_DIR}/readonly.txt")
> file(READ "${CMAKE_BINARY_DIR}/readonly.txt" myfile)
> file(REMOVE "${CMAKE_BINARY_DIR}/readonly.txt")
> file(WRITE "${CMAKE_BINARY_DIR}/readonly.txt" "${myfile}")
> 
> This actually works, because it creates a new file with default permissions.
> Alternately, you could write to a new file with a different name; in that
> case you could skip the file(REMOVE).
> 
> I do agree, though, that ideally you wouldn't want to muck with a configured
> file after configuring it. However, I expect there are some non-ideal
> situations which might require that, so the above work-around should get you
> going.
> 
> Ceej
> aka Chris Hillery


This works for me:

cmake_minimum_required(VERSION 2.6)
project(test None)

# configure into CMakeFiles directory, because it must be in a different directory
# since file(COPY) does rename files
configure_file(hello.sh.in ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/hello.sh)
# now copy the temporary into the final destination, setting the permissions
file(COPY ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/hello.sh
  DESTINATION ${CMAKE_BINARY_DIR}
  FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ
  GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)


HTH

Michael


More information about the CMake mailing list