Cmake filesystem

From wikinotes

file

create, append, read, upload, download, match, compute-hashes, ... a file or directory.

file(GLOB_RECURSE   variable   package/*.py)                    # recursively glob-match files from directory containing CMakeLists.txt. Discovered paths saved to 'variable' are all absolute.
file(GLOB           variable   package/*.py)                    # glob-match files from directory containing CMakeLists.txt. Discovered paths saved to 'variable' are absolute.
 
file(MD5            myfile.txt  variable)                       # Calculate MD5-sum of file and save to 'variable'
 
file(STRINGS        myfile.txt  variable)                       # read file into array: variable. (Entries separated by newline)
file(STRINGS        myfile.txt  variable  REGEX "^[a-zA-Z]+$")  # read file into array, returning only lines matching a regex-match
 
file(MAKE_DIRECTORY  /usr/lib  /usr/local/lib )   # create directories
 
file(DOWNLOAD        https://website/file.txt    file.txt  <options>)  # download file
file(UPLOAD          file.txt  https://some/website  <options>)        # upload a file
 
# ... and lots lots more ...

Install

Define what must copied from the build directory to the host-system when cmake install is issued on the commandline. You can tweak file owners, permissions etc. here as well.

There are 4x types of files you can install, characterized by their first keyword argument:

  • DIRECTORY (copy a directory and all of it's contents to host system)
  • PROGRAMS (copy compiled executable files to host system)
  • TARGETS (copy compiled objects to host system)
  • FILES (anything else - configs, databases, etc)

They all have variations on the same parameters. For a much more comprehensive explanation, see: https://cmake.org/cmake/help/v3.11/command/install.html#id1

NOTE:

It is customary to use mostly relative-paths for the DESTINATION argument. That way users can install multiple versions of a program onto the same host machine by altering the CMAKE_INSTALL_PREFIX environment variable. BSD makes heavy use of this, installing all user-installed programs under /usr/local so that they do not interfere with programs/libraries required by the operating system.

Installing Programs/Directories

These are all more or less the same, except that PROGRAMS automatically have executable permissions set for world/group/owner.

install(
    FILES         config.txt db/users.sqlite          # files to be copied (relative path from CMakeLists.txt)
    DESTINATION   lib/myprogram/configs               # absolute-path, or relative path (from ${CMAKE_INSTALL_PREFIX}) for targets to be copied to.
)

Installing Targets (Executables/Libraries/Frameworks)

You'll want to read the official documentation (below). The idea here is that you can specify a cluster of files you want installed, define where to put files of specific types, and everything will automatically be put where it is supposed to be. Pretty cool huh?

install(
    TARGETS   myExecutable mySharedLib myStaticLib
    RUNTIME   DESTINATION   bin
    LIBRARY   DESTINATION   lib
    ARCHIVE   DESTINATION   lib/static
)

https://cmake.org/cmake/help/v3.11/command/install.html#installing-targets