file.rst 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. file
  2. ----
  3. File manipulation command.
  4. ::
  5. file(WRITE filename "message to write"... )
  6. file(APPEND filename "message to write"... )
  7. file(READ filename variable [LIMIT numBytes] [OFFSET offset] [HEX])
  8. file(<MD5|SHA1|SHA224|SHA256|SHA384|SHA512> filename variable)
  9. file(STRINGS filename variable [LIMIT_COUNT num]
  10. [LIMIT_INPUT numBytes] [LIMIT_OUTPUT numBytes]
  11. [LENGTH_MINIMUM numBytes] [LENGTH_MAXIMUM numBytes]
  12. [NEWLINE_CONSUME] [REGEX regex]
  13. [NO_HEX_CONVERSION])
  14. file(GLOB variable [RELATIVE path] [globbing expressions]...)
  15. file(GLOB_RECURSE variable [RELATIVE path]
  16. [FOLLOW_SYMLINKS] [globbing expressions]...)
  17. file(RENAME <oldname> <newname>)
  18. file(REMOVE [file1 ...])
  19. file(REMOVE_RECURSE [file1 ...])
  20. file(MAKE_DIRECTORY [directory1 directory2 ...])
  21. file(RELATIVE_PATH variable directory file)
  22. file(TO_CMAKE_PATH path result)
  23. file(TO_NATIVE_PATH path result)
  24. file(DOWNLOAD url file [INACTIVITY_TIMEOUT timeout]
  25. [TIMEOUT timeout] [STATUS status] [LOG log] [SHOW_PROGRESS]
  26. [EXPECTED_HASH ALGO=value] [EXPECTED_MD5 sum]
  27. [TLS_VERIFY on|off] [TLS_CAINFO file])
  28. file(UPLOAD filename url [INACTIVITY_TIMEOUT timeout]
  29. [TIMEOUT timeout] [STATUS status] [LOG log] [SHOW_PROGRESS])
  30. file(TIMESTAMP filename variable [<format string>] [UTC])
  31. file(GENERATE OUTPUT output_file
  32. <INPUT input_file|CONTENT input_content>
  33. [CONDITION expression])
  34. WRITE will write a message into a file called 'filename'. It
  35. overwrites the file if it already exists, and creates the file if it
  36. does not exist. (If the file is a build input, use configure_file to
  37. update the file only when its content changes.)
  38. APPEND will write a message into a file same as WRITE, except it will
  39. append it to the end of the file
  40. READ will read the content of a file and store it into the variable.
  41. It will start at the given offset and read up to numBytes. If the
  42. argument HEX is given, the binary data will be converted to
  43. hexadecimal representation and this will be stored in the variable.
  44. MD5, SHA1, SHA224, SHA256, SHA384, and SHA512 will compute a
  45. cryptographic hash of the content of a file.
  46. STRINGS will parse a list of ASCII strings from a file and store it in
  47. a variable. Binary data in the file are ignored. Carriage return
  48. (CR) characters are ignored. It works also for Intel Hex and Motorola
  49. S-record files, which are automatically converted to binary format
  50. when reading them. Disable this using NO_HEX_CONVERSION.
  51. LIMIT_COUNT sets the maximum number of strings to return. LIMIT_INPUT
  52. sets the maximum number of bytes to read from the input file.
  53. LIMIT_OUTPUT sets the maximum number of bytes to store in the output
  54. variable. LENGTH_MINIMUM sets the minimum length of a string to
  55. return. Shorter strings are ignored. LENGTH_MAXIMUM sets the maximum
  56. length of a string to return. Longer strings are split into strings
  57. no longer than the maximum length. NEWLINE_CONSUME allows newlines to
  58. be included in strings instead of terminating them.
  59. REGEX specifies a regular expression that a string must match to be
  60. returned. Typical usage
  61. ::
  62. file(STRINGS myfile.txt myfile)
  63. stores a list in the variable "myfile" in which each item is a line
  64. from the input file.
  65. GLOB will generate a list of all files that match the globbing
  66. expressions and store it into the variable. Globbing expressions are
  67. similar to regular expressions, but much simpler. If RELATIVE flag is
  68. specified for an expression, the results will be returned as a
  69. relative path to the given path. (We do not recommend using GLOB to
  70. collect a list of source files from your source tree. If no
  71. CMakeLists.txt file changes when a source is added or removed then the
  72. generated build system cannot know when to ask CMake to regenerate.)
  73. Examples of globbing expressions include:
  74. ::
  75. *.cxx - match all files with extension cxx
  76. *.vt? - match all files with extension vta,...,vtz
  77. f[3-5].txt - match files f3.txt, f4.txt, f5.txt
  78. GLOB_RECURSE will generate a list similar to the regular GLOB, except
  79. it will traverse all the subdirectories of the matched directory and
  80. match the files. Subdirectories that are symlinks are only traversed
  81. if FOLLOW_SYMLINKS is given or cmake policy CMP0009 is not set to NEW.
  82. See cmake --help-policy CMP0009 for more information.
  83. Examples of recursive globbing include:
  84. ::
  85. /dir/*.py - match all python files in /dir and subdirectories
  86. MAKE_DIRECTORY will create the given directories, also if their parent
  87. directories don't exist yet
  88. RENAME moves a file or directory within a filesystem, replacing the
  89. destination atomically.
  90. REMOVE will remove the given files, also in subdirectories
  91. REMOVE_RECURSE will remove the given files and directories, also
  92. non-empty directories
  93. RELATIVE_PATH will determine relative path from directory to the given
  94. file.
  95. TO_CMAKE_PATH will convert path into a cmake style path with unix /.
  96. The input can be a single path or a system path like "$ENV{PATH}".
  97. Note the double quotes around the ENV call TO_CMAKE_PATH only takes
  98. one argument. This command will also convert the native list
  99. delimiters for a list of paths like the PATH environment variable.
  100. TO_NATIVE_PATH works just like TO_CMAKE_PATH, but will convert from a
  101. cmake style path into the native path style \ for windows and / for
  102. UNIX.
  103. DOWNLOAD will download the given URL to the given file. If LOG var is
  104. specified a log of the download will be put in var. If STATUS var is
  105. specified the status of the operation will be put in var. The status
  106. is returned in a list of length 2. The first element is the numeric
  107. return value for the operation, and the second element is a string
  108. value for the error. A 0 numeric error means no error in the
  109. operation. If TIMEOUT time is specified, the operation will timeout
  110. after time seconds, time should be specified as an integer. The
  111. INACTIVITY_TIMEOUT specifies an integer number of seconds of
  112. inactivity after which the operation should terminate. If
  113. EXPECTED_HASH ALGO=value is specified, the operation will verify that
  114. the downloaded file's actual hash matches the expected value, where
  115. ALGO is one of MD5, SHA1, SHA224, SHA256, SHA384, or SHA512. If it
  116. does not match, the operation fails with an error. ("EXPECTED_MD5
  117. sum" is short-hand for "EXPECTED_HASH MD5=sum".) If SHOW_PROGRESS is
  118. specified, progress information will be printed as status messages
  119. until the operation is complete. For https URLs CMake must be built
  120. with OpenSSL. TLS/SSL certificates are not checked by default. Set
  121. TLS_VERIFY to ON to check certificates and/or use EXPECTED_HASH to
  122. verify downloaded content. Set TLS_CAINFO to specify a custom
  123. Certificate Authority file. If either TLS option is not given CMake
  124. will check variables CMAKE_TLS_VERIFY and CMAKE_TLS_CAINFO,
  125. respectively.
  126. UPLOAD will upload the given file to the given URL. If LOG var is
  127. specified a log of the upload will be put in var. If STATUS var is
  128. specified the status of the operation will be put in var. The status
  129. is returned in a list of length 2. The first element is the numeric
  130. return value for the operation, and the second element is a string
  131. value for the error. A 0 numeric error means no error in the
  132. operation. If TIMEOUT time is specified, the operation will timeout
  133. after time seconds, time should be specified as an integer. The
  134. INACTIVITY_TIMEOUT specifies an integer number of seconds of
  135. inactivity after which the operation should terminate. If
  136. SHOW_PROGRESS is specified, progress information will be printed as
  137. status messages until the operation is complete.
  138. TIMESTAMP will write a string representation of the modification time
  139. of filename to variable.
  140. Should the command be unable to obtain a timestamp variable will be
  141. set to the empty string "".
  142. See documentation of the string TIMESTAMP sub-command for more
  143. details.
  144. The file() command also provides COPY and INSTALL signatures:
  145. ::
  146. file(<COPY|INSTALL> files... DESTINATION <dir>
  147. [FILE_PERMISSIONS permissions...]
  148. [DIRECTORY_PERMISSIONS permissions...]
  149. [NO_SOURCE_PERMISSIONS] [USE_SOURCE_PERMISSIONS]
  150. [FILES_MATCHING]
  151. [[PATTERN <pattern> | REGEX <regex>]
  152. [EXCLUDE] [PERMISSIONS permissions...]] [...])
  153. The COPY signature copies files, directories, and symlinks to a
  154. destination folder. Relative input paths are evaluated with respect
  155. to the current source directory, and a relative destination is
  156. evaluated with respect to the current build directory. Copying
  157. preserves input file timestamps, and optimizes out a file if it exists
  158. at the destination with the same timestamp. Copying preserves input
  159. permissions unless explicit permissions or NO_SOURCE_PERMISSIONS are
  160. given (default is USE_SOURCE_PERMISSIONS). See the install(DIRECTORY)
  161. command for documentation of permissions, PATTERN, REGEX, and EXCLUDE
  162. options.
  163. The INSTALL signature differs slightly from COPY: it prints status
  164. messages, and NO_SOURCE_PERMISSIONS is default. Installation scripts
  165. generated by the install() command use this signature (with some
  166. undocumented options for internal use).
  167. GENERATE will write an <output_file> with content from an
  168. <input_file>, or from <input_content>. The output is generated
  169. conditionally based on the content of the <condition>. The file is
  170. written at CMake generate-time and the input may contain generator
  171. expressions. The <condition>, <output_file> and <input_file> may also
  172. contain generator expressions. The <condition> must evaluate to
  173. either '0' or '1'. The <output_file> must evaluate to a unique name
  174. among all configurations and among all invocations of file(GENERATE).