条件判断
条件判断通常用于处理一些特殊的编译配置。
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
被引入的版本 2.0.1
参考
is_host, is_os, is_plat, is_subarch
editis_host
bool is_host(string host, ...)
是否为当前的编译主机系统
如果匹配当前的主机系统,那么返回 true,否则返回 false。
一些目标编译平台能够在不同的操作系统上进行构建,例如:Android NDK 可以在 linux, macOS 和 windows 上编译。
因此,我们能够使用这个 API 去决定当前的操作系统。
if is_host("windows") then
add_includedirs("C:\\includes")
else
add_includedirs("/usr/includess")
end
支持的主机系统有:
- "windows"
- "linux"
- "macosx"
被引入的版本 2.1.4
参考
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
被引入的版本 2.0.1
参考
is_config, var_mode, os.is_mode
is_os
bool is_os(string os, ...)
Is the current compilation target system
Returns true if the target compilation os is the one specified with os. Returns false otherwise.
if is_os("ios") then
add_files("src/xxx/*.m")
end
Valid input values are:
- "windows"
- "linux"
- "android"
- "macosx"
- "ios"
被引入的版本 2.0.1
参考
$(os), is_arch, is_host, is_mode, is_plat
editis_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"
参考
is_arch, is_host, is_mode, is_os
editis_subarch
bool is_arch(string arch, ...)
Determine the architecture of the current host subsystem environment
At present, it is mainly used for the detection of the architecture under the subsystem environment such as cygwin and msys2 on the windows system. The msvc tool chain is usually used on the windows compilation platform, and the architecture is x64, x86. In the msys/cygwin subsystem environment, the compiler architecture defaults to x86_64/i386, which is different.
We can also quickly view the current subsystem architecture by executing xmake l os.subarch
.
被引入的版本 2.0.1
参考
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.
被引入的版本 2.3.1
参考
is_host, var_subhost, os.is_subhost