assert
editcatch
editcprint
Wrap color print terminal log
The behavior is similar to print, the difference is that this interface also supports color terminal output, and supports emoji
character output.
E.g:
cprint('${bright}hello xmake')
cprint('${red}hello xmake')
cprint('${bright green}hello ${clear}xmake')
cprint('${blue onyellow underline}hello xmake${clear}')
cprint('${red}hello ${magenta}xmake')
cprint('${cyan}hello ${dim yellow}xmake')
The results are as follows:
The color-related descriptions are placed in ${ }
, and you can set several different properties at the same time, for example:
${bright red underline onyellow}
Indicates: highlighted red, background yellow, and with a down line
All of these descriptions will affect the entire entire line of characters. If you only want to display partial color text, you can insert ${clear}
at the end position to clear the previous color description.
E.g:
${red}hello ${clear}xmake
In this case, only hello is displayed in red, and the others are still normal black display.
Other colors belong to, I will not introduce them here, directly paste the list of attributes in the xmake code:
colors.keys =
{
-- Attributes
reset = 0 -- reset attribute
, clear = 0 -- clear attribute
, default = 0 -- default property
, bright = 1 -- highlight
, dim = 2 -- dark
, underline = 4 -- underline
, blink = 5 -- flashing
, reverse = 7 -- reverse color
, hidden = 8 -- hidden text
-- Foreground
, black = 30
, red = 31
, green = 32
, yellow = 33
, blue = 34
, magenta = 35
, cyan = 36
, white = 37
-- Background color
, onblack = 40
, onred = 41
, ongreen = 42
, onyellow = 43
, onblue = 44
, onmagenta = 45
, oncyan = 46
, onwhite = 47
In addition to color highlighting, if your terminal is under macosx, lion above the system, xmake can also support the display of emoji expressions, for systems that do not support Ignore the display, for example:
cprint("hello xmake${beer}")
cprint("hello${ok_hand} xmake")
The above two lines of code, I printed a classic beer symbol in the homebrew, the following line printed an ok gesture symbol, is not very dazzling.
All emoji emoticons, as well as the corresponding keys in xmake, can be found in emoji.
Version 2.1.7 supports 24-bit true color output, if the terminal supports it:
import("core.base.colors")
if colors.truecolor() then
cprint("${255;0;0}hello")
cprint("${on;255;0;0}hello${clear} xmake")
cprint("${bright 255;0;0 underline}hello")
cprint("${bright on;255;0;0 0;255;0}hello${clear} xmake")
end
Xmake's detection support for truecolor is implemented by the $COLORTERM
environment variable. If your terminal supports truecolor, you can manually set this environment variable to tell xmake to enable truecolor support.
It can be enabled and tested with the following command:
$ export COLORTERM=truecolor
$ xmake --version
The 2.1.7 version can disable color output with COLORTERM=nocolor
.
See also
editcprintf
No line feed color print terminal log
This interface is similar to cprint, the difference is that it does not wrap the output.
See also
editdprint
editdprintf
editfinally
editfind_package
editfind_packages
editformat
string format(string formatstr[, ...])
Formatting a string
If you just want to format the string and don't output it, you can use this interface. This interface is equivalent to the string.format interface, just a simplified version of the interface name.
local s = format("hello %s", xmake)
See also
editget_config
edithas_config
edithas_package
editimport
editinherit
editipairs
for traversing arrays
This is lua's native built-in api. In xmake, it has been extended in its original behavior to simplify some of the daily lua traversal code.
First look at the default native notation:
for idx, val in ipairs({"a", "b", "c", "d", "e", "f"}) do
print("%d %s", idx, val)
end
The extension is written like the pairs interface, for example:
for idx, val in ipairs({"a", "b", "c", "d", "e", "f"}, function (v) return v:upper() end) do
print("%d %s", idx, val)
end
for idx, val in ipairs({"a", "b", "c", "d", "e", "f"}, function (v, a, b) return v:upper() .. a .. b end, "a", "b") do
print("%d %s", idx, val)
end
This simplifies the logic of the for
block code. For example, if I want to traverse the specified directory and get the file name, but not including the path, I can simplify the writing by this extension:
for _, filename in ipairs(os.dirs("*"), path.filename) do
-- ...
end
See also
editirpairs
editis_arch
bool is_arch(string arch, ...)
Is the current compilation architecture
Returns true if the current compilation architecture is the one specified with arch. Returns false otherwise.
You can use this api to check the configuration command: xmake f -a armv7
-- if the current architecture is x86_64 or i386
if is_arch("x86_64", "i386") then
add_files("src/xxx/*.c")
end
-- if the current architecture is armv7 or arm64 or armv7s or armv7-a
if is_arch("armv7", "arm64", "armv7s", "armv7-a") then
-- ...
end
And you can also use the lua regular expression: .*
to check all matched architectures.
-- if the current architecture is arm which contains armv7, arm64, armv7s and armv7-a ...
if is_arch("arm.*") then
-- ...
end
Introduced in version 2.0.1
See also
is_host, is_os, is_plat, is_subarch
editis_config
editis_host
bool is_host(string host, ...)
Is the current compilation host system
Returns true if the current compilation host system is the one specified with host. Returns false otherwise.
Some compilation platforms can be built on multiple different operating systems, for example: android ndk (on linux, macOS and windows).
So, we can use this api to determine the current host operating system.
if is_host("windows") then
add_includedirs("C:\\includes")
else
add_includedirs("/usr/includess")
end
Support hosts:
- "windows"
- "linux"
- "macosx"
We can also get it from $(host) or os.host.
Introduced in version 2.1.4
See also
$(host), is_arch, is_os, is_plat, is_subhost
editis_mode
bool is_mode(string mode, ...)
Is the current compilation mode
You can use this api to check the configuration command: xmake f -m debug
The compilation mode is not builtin mode for xmake, so you can set the mode value by yourself.
We often use these configuration values: debug
, release
, profile
, etc.
-- if the current compilation mode is debug?
if is_mode("debug") then
-- add macro: DEBUG
add_defines("DEBUG")
-- enable debug symbols
set_symbols("debug")
-- disable optimization
set_optimize("none")
end
-- if the current compilation mode is release or profile?
if is_mode("release", "profile") then
if is_mode("release") then
-- mark symbols visibility as hidden
set_symbols("hidden")
-- strip all symbols
set_strip("all")
-- fomit frame pointer
add_cxflags("-fomit-frame-pointer")
add_mxflags("-fomit-frame-pointer")
else
-- enable debug symbols
set_symbols("debug")
end
-- add vectorexts
add_vectorexts("sse2", "sse3", "ssse3", "mmx")
end
Introduced in version 2.0.1
See also
is_config, var_mode, os_is_mode
is_plat
bool is_plat(string plat, ...)
Is the current compilation platform
Returns true if the current compilation platform is the one specified with plat. Returns false otherwise.
You can use this api to check the configuration command: xmake f -p iphoneos
-- if the current platform is android
if is_plat("android") then
add_files("src/xxx/*.c")
end
-- if the current platform is macosx or iphoneos
if is_plat("macosx", "iphoneos") then
add_frameworks("Foundation")
end
Support platforms:
- "windows"
- "linux"
- "macosx"
- "android"
- "iphoneos"
- "watchos"
See also
is_arch, is_host, is_mode, is_os
editis_subhost
bool is_subhost(string subhost, ...)
Determine the subsystem environment of the current host
At present, it is mainly used for detection of cygwin, msys2 and other subsystem environments on windows systems. If you run xmake in the msys2 shell environment, then is_subhost("windows")
will return false, and is_host("windows")
It will still return true.
Currently supported subsystems:
- "msys"
- "cygwin"
Configuration example:
if is_subhost("msys", "cygwin") then
-- Currently in the shell environment of msys2/cygwin
end
We can also quickly check the current subsystem platform by executing xmake l os.subhost
.
âš It may also support other subsystem environments under linux and macos systems later, if they exist.
Introduced in version 2.3.1
See also
is_host, var_subhost, os.is_subhost
pairs
Used to traverse the dictionary
This is lua's native built-in api. In xmake, it has been extended in its original behavior to simplify some of the daily lua traversal code.
First look at the default native notation:
local t = {a = "a", b = "b", c = "c", d = "d", e = "e", f = "f"}
for key, val in pairs(t) do
print("%s: %s", key, val)
end
This is sufficient for normal traversal operations, but if we get the uppercase for each of the elements it traverses, we can write:
for key, val in pairs(t, function (v) return v:upper() end) do
print("%s: %s", key, val)
end
Even pass in some parameters to the second function
, for example:
for key, val in pairs(t, function (v, a, b) return v:upper() .. a .. b end, "a", "b") do
print("%s: %s", key, val)
end
See also
editWrapping print terminal log
This interface is also the native interface of lua. xmake is also extended based on the original behavior, and supports: formatted output, multivariable output.
First look at the way native support:
print("hello xmake!")
print("hello", "xmake!", 123)
And also supports extended formatting:
print("hello %s!", "xmake")
print("hello xmake! %d", 123)
Xmake will support both types of writing at the same time, and the internal will automatically detect and select the output behavior.
See also
printf, vprint, cprint, dprint
editprintf
No line printing terminal log
Like the print interface, the only difference is that it doesn't wrap.
See also
editraise
nil raise(string message)
Throwing an abort program
If you want to interrupt xmake running in custom scripts and plug-in tasks, you can use this interface to throw an exception. If the upper layer does not show the call to exceptions_intro, xmake will be executed. An error message is displayed.
if errors then raise(errors) end
If an exception is thrown in the try block, the error information is captured in catch and finally. See: exceptions_intro
See also
edittodisplay
edittonumber
edittostring
edittry
edittype
editunpack
editval
editvformat
string vformat(string formatstr[, ...])
Formatting strings, support for built-in variable escaping
This interface is followed by the format interface is similar, but adds support for the acquisition and escaping of built-in variables.
local s = vformat("hello %s $(mode) {link builtin_variables_intro $(arch)} $(env PATH)", xmake)
edit