os
The system operation module belongs to the built-in module. It can be called directly by the script scope without using import.
This module is also a native module of lua, and xmake has been extended to provide more practical interfaces.
edit⚠ Only some readonly interfaces (for example:
os.getenv
,os.arch
) in the os module can be used in the description scope. Other interfaces can only be used in the script domain, for example:os.cp
,os.rm
etc.
os.addenv
editos.addenvp
editos.addenvs
editos.arch
editos.args
editos.argv
editos.atexit
editos.cd
editos.cp
nil os.cp(string src, string dst, table opt)
Copy files or directories
The behavior is similar to the cp
command in the shell, supporting path wildcard matching (using lua pattern matching), support for multi-file copying, and built-in variable support.
Valid fields for opt
are:
string rootdir
bool symlink
e.g:
os.cp("$(scriptdir)/*.h", "$(buildir)/inc")
os.cp("$(projectdir)/src/test/**.h", "$(buildir)/inc")
The above code will: all the header files in the current xmake.lua
directory, the header files in the project source test directory are all copied to the $(buildir)
output directory.
Among them $(scriptdir)
, $(projectdir)
These variables are built-in variables of xmake. For details, see the related documentation of built-in variables.
The matching patterns in *.h
and **.h
are similar to those in add_files, the former is a single-level directory matching, and the latter is a recursive multi-level directory matching.
This interface also supports `recursive replication' of directories, for example:
-- Recursively copy the current directory to a temporary directory
os.cp("$(curdir)/test/", "$(tmpdir)/test")
The copy at the top will expand and copy all files to the specified directory, and lose the source directory hierarchy. If you want to copy according to the directory structure that maintains it, you can set the rootdir parameter:
os.cp("src/**.h", "/tmp/", {rootdir="src"})
The above script can press the root directory of src
to copy all sub-files under src in the same directory structure.
⚠ Try to use the
os.cp
interface instead ofos.run("cp ..")
, which will ensure platform consistency and cross-platform build description.
Under 2.5.7, the parameter {symlink = true}
is added to keep the symbolic link when copying files.
os.cp("/xxx/foo", "/xxx/bar", {symlink = true})
被引入的版本 2.0.1
参考
editos.cpuinfo
editos.curdir
editos.date
editos.default_njob
editos.dirs
editos.emptydir
editos.exec
editos.execv
editos.exists
editos.exit
editos.features
editos.filedirs
editos.files
editos.filesize
editos.fscase
editos.getenv
editos.getenvs
editos.getpid
editos.getwinsize
editos.host
editos.iorun
editos.iorunv
editos.is_arch
editos.is_host
editos.is_subarch
editos.is_subhost
editos.isdir
editos.isexec
editos.isfile
editos.islink
editos.isroot
editos.joinenvs
editos.ln
editos.match
editos.mclock
editos.meminfo
editos.mkdir
editos.mtime
editos.mv
nil os.mv(string src, string dst, table opt)
Move to rename a file or directory
Similar to the use of os.cp, it also supports multi-file move operations and pattern matching, for example:
-- Move multiple files to a temporary directory
os.mv("$(buildir)/test1", "$(tmpdir)")
-- File movement does not support bulk operations, which is file renaming
os.mv("$(buildir)/libtest.a", "$(buildir)/libdemo.a")
被引入的版本 2.0.1
参考
os.cp, os_trycp, os_rm