123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- file
- ----
- File manipulation command.
- ------------------------------------------------------------------------------
- ::
- file(WRITE <filename> <content>...)
- file(APPEND <filename> <content>...)
- Write ``<content>`` into a file called ``<filename>``. If the file does
- not exist, it will be created. If the file already exists, ``WRITE``
- mode will overwrite it and ``APPEND`` mode will append to the end.
- Any directories in the path specified by ``<filename>`` that do not
- exist will be created.
- If the file is a build input, use the :command:`configure_file` command
- to update the file only when its content changes.
- ------------------------------------------------------------------------------
- ::
- file(READ <filename> <variable>
- [OFFSET <offset>] [LIMIT <max-in>] [HEX])
- Read content from a file called ``<filename>`` and store it in a
- ``<variable>``. Optionally start from the given ``<offset>`` and
- read at most ``<max-in>`` bytes. The ``HEX`` option causes data to
- be converted to a hexadecimal representation (useful for binary data).
- ------------------------------------------------------------------------------
- ::
- file(STRINGS <filename> <variable> [<options>...])
- Parse a list of ASCII strings from ``<filename>`` and store it in
- ``<variable>``. Binary data in the file are ignored. Carriage return
- (``\r``, CR) characters are ignored. The options are:
- ``LENGTH_MAXIMUM <max-len>``
- Consider only strings of at most a given length.
- ``LENGTH_MINIMUM <min-len>``
- Consider only strings of at least a given length.
- ``LIMIT_COUNT <max-num>``
- Limit the number of distinct strings to be extracted.
- ``LIMIT_INPUT <max-in>``
- Limit the number of input bytes to read from the file.
- ``LIMIT_OUTPUT <max-out>``
- Limit the number of total bytes to store in the ``<variable>``.
- ``NEWLINE_CONSUME``
- Treat newline characters (``\n``, LF) as part of string content
- instead of terminating at them.
- ``NO_HEX_CONVERSION``
- Intel Hex and Motorola S-record files are automatically converted to
- binary while reading unless this option is given.
- ``REGEX <regex>``
- Consider only strings that match the given regular expression.
- ``ENCODING <encoding-type>``
- Consider strings of a given encoding. Currently supported encodings are:
- UTF-8, UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE. If the ENCODING option
- is not provided and the file has a Byte Order Mark, the ENCODING option
- will be defaulted to respect the Byte Order Mark.
- For example, the code
- .. code-block:: cmake
- file(STRINGS myfile.txt myfile)
- stores a list in the variable ``myfile`` in which each item is a line
- from the input file.
- ------------------------------------------------------------------------------
- ::
- file(<HASH> <filename> <variable>)
- Compute a cryptographic hash of the content of ``<filename>`` and
- store it in a ``<variable>``. The supported ``<HASH>`` algorithm names
- are those listed by the :ref:`string(\<HASH\>) <Supported Hash Algorithms>`
- command.
- ------------------------------------------------------------------------------
- ::
- file(GLOB <variable>
- [LIST_DIRECTORIES true|false] [RELATIVE <path>]
- [<globbing-expressions>...])
- file(GLOB_RECURSE <variable> [FOLLOW_SYMLINKS]
- [LIST_DIRECTORIES true|false] [RELATIVE <path>]
- [<globbing-expressions>...])
- Generate a list of files that match the ``<globbing-expressions>`` and
- store it into the ``<variable>``. Globbing expressions are similar to
- regular expressions, but much simpler. If ``RELATIVE`` flag is
- specified, the results will be returned as relative paths to the given
- path. The results will be ordered lexicographically.
- By default ``GLOB`` lists directories - directories are omitted in result if
- ``LIST_DIRECTORIES`` is set to false.
- .. note::
- We do not recommend using GLOB to collect a list of source files from
- your source tree. If no CMakeLists.txt file changes when a source is
- added or removed then the generated build system cannot know when to
- ask CMake to regenerate.
- Examples of globbing expressions include::
- *.cxx - match all files with extension cxx
- *.vt? - match all files with extension vta,...,vtz
- f[3-5].txt - match files f3.txt, f4.txt, f5.txt
- The ``GLOB_RECURSE`` mode will traverse all the subdirectories of the
- matched directory and match the files. Subdirectories that are symlinks
- are only traversed if ``FOLLOW_SYMLINKS`` is given or policy
- :policy:`CMP0009` is not set to ``NEW``.
- By default ``GLOB_RECURSE`` omits directories from result list - setting
- ``LIST_DIRECTORIES`` to true adds directories to result list.
- If ``FOLLOW_SYMLINKS`` is given or policy :policy:`CMP0009` is not set to
- ``OLD`` then ``LIST_DIRECTORIES`` treats symlinks as directories.
- Examples of recursive globbing include::
- /dir
|