[CMake] [CMAKE] Checking if a file exist at the installing process

paul pfultz2 at yahoo.com
Wed Oct 18 18:36:55 EDT 2017


You can use install(CODE) or install(SCRIPT) to run custom cmake code at
install time. The only caveat is the install location is actually
`$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}` instead of just
`${CMAKE_INSTALL_PREFIX}`. This can happen when the user calls `make
DESTDIR=<path>` or during cpack.

On Wed, 2017-10-18 at 15:03 -0200, Daniel Yoshizawa wrote:
> Hello everyone,
> 
> I have a problem and couldn't find a solution over the internet, the
> situation is that I must copy some files at make install, the trick is, I
> must check if the files exists at install time if they don't, I must copy
> the files, otherwise do nothing.
> 
> It doesn't feel anywhere near fine to me, but this is what are asking me to
> do, so there is no other way.
> 
> My initial solution was to check if the file don't exist, if so I create a
> INSTALL command for it, the problem is that it runs at the configuration
> phase and I need something at install or post install, this files have
> nothing to do with the build, are just configuration files or other non-
> related stuff, so no target for them.
> 
> One more thing, I know just the cmake basics, so please any help is welcome.
> 
> Below is my original code that create the INSTALL commands at configuration
> phase.
> 
> ----------------------------------------------------------------------------
> ----------------------------------------------------------------------------
> -----
> 
> ############################################################################
> ##############
> #
> #   Desc : Files installation script, checks if the file exist on
> destination folder,
> #          if so, doesn't overwrite the files.
> #
> #   Params : input_file - File path with <source file>:<destination>
> #            must be one entry per line following the model above.
> #
> #   Obs : It is advised create .in files using ${CMAKE_SOURCE_DIR} to
> describe the path,
> #         calling configure_file to generate the correct path.
> #
> #   Ref : https://cmake.org/cmake/help/v3.2/command/configure_file.html
> #
> ############################################################################
> ##############
> 
> function (install_files input_file)
> 
>     MESSAGE("Files to be installed.")
> 
>     file(STRINGS ${input_file} contents)
> 
>     foreach(tuple ${contents})
>         string(REPLACE ":" ";" tuple_list ${tuple})
>         list(GET tuple_list 0 source)
>         list(GET tuple_list 1 destination)
> 
>         if(IS_DIRECTORY ${source})
>             install_directory(${source} ${destination})
>         else(IS_DIRECTORY ${source})
>             install_file(${source} ${destination})
>         endif()
> 
>     endforeach()
> 
> endfunction(install_files input_file)
> 
> function (install_directory path_to_dir path_to_destination)
> 
>     file(GLOB folder_files "${path_to_dir}/*")
> 
>     foreach(filename ${folder_files})
> 
>         if(IS_DIRECTORY ${filename})
>             get_filename_component( dir ${filename} NAME )
>             install_directory(${filename} ${path_to_destination}/${dir})
>         else(IS_DIRECTORY ${filename})
>             install_file(${filename} ${path_to_destination})
>         endif()
> 
>     endforeach(filename)
> 
> endfunction(install_directory path_to_dir path_to_destination)
> 
> function (install_file path_to_file path_to_destination)
> 
>     get_filename_component( filename ${path_to_file} NAME )
>     
>     if (NOT EXISTS ${path_to_destination}/${filename})
>         MESSAGE("source path      : ${path_to_file}")
>         MESSAGE("destination path : ${path_to_destination}")
> 
>         install (FILES "${path_to_file}"
>                     DESTINATION "${path_to_destination}"
>                     PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE
> GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
>                 )
>     endif()
> 
> endfunction (install_file path_to_file path_to_destination)
> 
> ----------------------------------------------------------------------------
> ----------------------------------------------------------------------------
> -----
> 
> Sorry for any mistakes and many thanks in advance,
> Daniel Yoshizawa.
> -- 
> 
> Powered by www.kitware.com
> 
> Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.o
> rg/Wiki/CMake_FAQ
> 
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:
> 
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
> 
> Visit other Kitware open-source projects at http://www.kitware.com/opensourc
> e/opensource.html
> 
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/cmake


More information about the CMake mailing list