一、介绍
CMake的文件操作命令
Reading
file(READ <filename> <out-var> [...])
file(STRINGS <filename> <out-var> [...])
file(<HASH> <filename> <out-var>)
file(TIMESTAMP <filename> <out-var> [...])
Writing
file({WRITE | APPEND} <filename> <content>...)
file({TOUCH | TOUCH_NOCREATE} [<file>...])
file(GENERATE OUTPUT <output-file> [...])
Filesystem
file({GLOB | GLOB_RECURSE} <out-var> [...] [<globbing-expr>...])
file(RENAME <oldname> <newname>)
file({REMOVE | REMOVE_RECURSE } [<files>...])
file(MAKE_DIRECTORY [<dir>...])
file({COPY | INSTALL} <file>... DESTINATION <dir> [...])
file(SIZE <filename> <out-var>)
file(READ_SYMLINK <linkname> <out-var>)
file(CREATE_LINK <original> <linkname> [...])
Path Conversion
file(RELATIVE_PATH <out-var> <directory> <file>)
file({TO_CMAKE_PATH | TO_NATIVE_PATH} <path> <out-var>)
Transfer
file(DOWNLOAD <url> <file> [...])
file(UPLOAD <file> <url> [...])
Locking
file(LOCK <path> [...])
二、实例
file(READ <filename> <out-var> [...])
READ 会读取文件的内容并将其存入到变量中?
file(STRINGS <filename> <out-var> [...])
STRINGS 从文件中解析出ASCII字符串列表并存储在变量中?
file(WRITE filename "message towrite"... )
WRITE 将一则信息写入文件’filename’中,如果该文件存在,它会覆盖它,如果不存在,它会创建该文件。
file(APPEND filename "message to write"... )
APPEND 如同WRITE,区别在于它将信息内容追加到文件末尾。
file(MAKE_DIRECTORY [directory1 directory2 ...])
MAKE_DIRECTORY在指定目录处创建子目录,如果它们的父目录不存在,也会创建它们的父目录。
file(RELATIVE_PATH variable directory file)
RELATIVE_PAT推断出指定文件相对于特定目录的路径。?
file(TO_CMAKE_PATH path result)
TO_CMAKE_PATH会将路径转换成cmake风格的路径表达形式。
file(GLOB variable [RELATIVE path] [globbingexpressions]...)
GLOB 会产生一个由所有匹配globbing表达式的文件组成的列表,并将其保存到变量中。Globbing 表达式与正则表达式类似,但更简单。
测试的例子:
file(WRITE test1.txt "Some messages to Write\n" )
file( TO_CMAKE_PATH "$ENV{PROGRAMFILES}" ANDROID_NDK_SEARCH_PATHS )
file(GLOB SOURCES “*.cu” “*.cpp” “*.c” “*.h”)
file(APPEND test1.txt "Another message to write\n")
file(STRINGS test1.txt PARSED_STRINGS)
message(STATUS "\n strings of test1.txt is: \n ${PARSED_STRINGS}")
file(GLOB files RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*.*")
message(STATUS "files: ${files}")
file(MAKE_DIRECTORY dir1 dir2)
file(RENAME dir2 dir3)
file(REMOVE dir3)
file(REMOVE_RECURSE dir3)
file(RELATIVE_PATH relative_path ${PROJECT_SOURCE_DIR}
file(COPY test1.txt DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/dir1)
file(INSTALL test1.txt DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/dir1)
参考:
file — CMake 3.14.7 Documentation
CMakeFile命令之file_P.TAN的博客-CSDN博客_cmake file
|