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.

âš  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.rmetc.

edit

os.arch

string os.arch()

Get current system architecture

That is the default architecture of the current host system, for example, I execute xmake on linux x86_64 to build, then the return value is: x86_64.

Introduced in version 2.0.1

See also

os.is_arch, is_arch, os.subarch

edit

os.cpuinfo

table os.cpuinfo()

Get cpu information

print(os.cpuinfo())
-- got {
--   ncpu = 8,
--   usagerate = 0.0,
--   model_name = "Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz",
--   march = "Kaby Lake",
--   vendor = "GenuineIntel",
--   model = 158,
--   family = 6
-- }
print(os.cpuinfo("march")) -- got "Kaby Lake"

Introduced in version 2.1.5

See also

os.default_njob

edit

os.curdir

string os.curdir()

Get the current directory path

Consistent with the result of $(curdir), it is just a direct return to return a variable that can be maintained with subsequent strings.

Usage reference: os.tmpdir.

Introduced in version 2.0.1

See also

$(curdir), os.tmpdir

edit

os.date

table os.date(string format, numeric datetime)

See Date and Time.

See also

os.time

edit

os.default_njob

numeric os.default_njob()

Get default number of parallel jobs

print(os.default_njob()) -- got 10

Introduced in version 2.5.8

See also

os.cpuinfo

edit

os.dirs

table os.dirs(string path_pattern[, function callback])

Traverse to get all the directories under the specified directory

Supports pattern matching in add_files, supports recursive and non-recursive mode traversal, and returns a table array. If not, returns an empty array, for example:

-- Recursive traversal to get all subdirectories
for _, dir in ipairs(os.dirs("$(buildir)/inc/**")) do
    print(dir)
end

Introduced in version 2.0.1

See also

os.files, os.filedirs, os.isdir, os.match

edit

os.exists

bool os.exists(string path)

Determine if a file or directory exists

Return false if the file or directory does not exist

-- Judging the existence of the directory
if os.exists("$(buildir)") then
    -- ...
end

-- Judging the existence of the file
if os.exists("$(buildir)/libxxx.a") then
    -- ...
end

Introduced in version 2.0.1

See also

os.isfile, os.isdir, os.isexec

edit

os.filedirs

table os.filedirs(string path_pattern[, function callback])

Traverse to get all files and directories under the specified directory

Supports pattern matching in target.add_files, supports recursive and non-recursive mode traversal, and returns a table array. If not, returns an empty array, for example:

-- Recursive traversal to get all child files and directories
for _, filedir in ipairs(os.filedirs("$(buildir)/**")) do
    print(filedir)
end

Introduced in version 2.0.1

See also

os.files, os.dirs, os.match

edit

os.files

table os.files(string path_pattern[, function callback])

Traverse to get all the files in the specified directory

Supports pattern matching in target.add_files, supports recursive and non-recursive mode traversal, and returns a table array. If not, returns an empty array, for example:

-- Non-recursive traversal to get all child files
for _, filepath in ipairs(os.files("$(buildir)/inc/*.h")) do
    print(filepath)
end

Introduced in version 2.0.1

See also

os.isfile, os.dirs, os.filedirs, os.match

edit

os.filesize

numeric os.filesize(string path)

Get file size

print(os.filesize("/tmp/a"))

Introduced in version 2.1.9

edit

os.getenv

string os.getenv(string envname)

Get system environment variables

print(os.getenv("PATH"))

Introduced in version 2.0.1

See also

os.getenvs, $(env), os.addenv, os.setenv

edit

os.host

string os.host()

Get the operating system of the current host

Consistent with the result of $(host), for example, if I execute xmake on linux x86_64 to build, the return value is: linux

Introduced in version 2.0.1

See also

is_host, $(host), os.subhost

edit

os.isdir

bool os.isdir(string path)

Determine if it is a directory

Return false if the directory does not exist

if os.isdir("src") then
    -- ...
end

Introduced in version 2.3.1

See also

os.isfile, os.islink, os.isexec, os.exists

edit

os.isfile

bool os.isfile(string path)

Determine if it is a file

Return false if the file does not exist

if os.isfile("$(buildir)/libxxx.a") then
    -- ...
end

Introduced in version 2.0.1

See also

os.isdir, os.islink, os.isexec, os.exists

edit

os.mclock

numeric os.mclock()

local time = os.mclock()
os.execv(program)
time = os.mclock() - time
print("time %0.3fs", time / 1000.)

See also

os.time, os.mtime, os.sleep

edit

os.mtime

numeric os.mtime(string path)

Get file modification time

local time = os.mtime("$(buildir)/file.o")

See also

os.time, os.sleep

edit

os.programdir

string os.programdir()

Get the xmake installation main program script directory

Consistent with the result of $(programdir), it is just a direct get returned to a variable, which can be maintained with subsequent strings.

Introduced in version 2.1.5

See also

$(programdir)

edit

os.programfile

string os.programfile()

Get the path of the xmake executable

Introduced in version 2.1.5

See also

os.programdir, os.scriptdir

edit

os.projectdir

string os.projectdir()

Get the project home directory

Consistent with the result of $(projectdir), it is just a direct return to return a variable that can be maintained with subsequent strings.

Introduced in version 2.1.5

See also

$(projectdir), os.projectfile

edit

os.projectfile

string os.projectfile()

Get the path to the current project xmake.lua file

See also

os.projectdir

edit

os.scriptdir

string os.scriptdir()

Get the path of the current description script

Consistent with the result of $(scriptdir), it is just a direct return to return a variable that can be maintained with subsequent strings.

Usage reference: os.tmpdir.

Introduced in version 2.0.1

See also

$(scriptdir), os.projectfile, os.projectdir

edit

os.subarch

string os.subarch()

Get Subsystem host architecture

print(os.subarch())
-- got x64

Introduced in version 2.3.1

See also

is_subarch, os.is_subarch, os.arch

edit

os.subhost

string os.subhost()

Get Subsystem host, e.g. msys, cygwin on windows

print(os.subhost())
-- got linux

Introduced in version 2.3.1

See also

os.is_subhost, is_subhost, os.host

edit

os.term

string os.term()

Get current terminal (windows-terminal, vscode, ... )

print(os.term())
-- got xterm

Introduced in version 2.7.3

See also

os.shell

edit

os.time

numeric os.time([table time])

See Date and Time.

See also

os.date

edit

os.tmpdir

string os.tmpdir([table opt])

Get temporary directory

Consistent with the result of $(tmpdir), it is just a direct return to return a variable that can be maintained with subsequent strings.

print(path.join(os.tmpdir(), "file.txt"))

Equivalent to:

print("$(tmpdir)/file.txt")

Valid field for opt is:

Introduced in version 2.0.1

See also

$(tmpdir), os.tmpfile

edit
Interfaces
os.arch
os.cpuinfo
os.curdir
os.date
os.default_njob
os.dirs
os.exists
os.filedirs
os.files
os.filesize
os.getenv
os.host
os.isdir
os.isfile
os.mclock
os.mtime
os.programdir
os.programfile
os.projectdir
os.projectfile
os.scriptdir
os.subarch
os.subhost
os.term
os.time
os.tmpdir