Xmake provides some built-in conditional judgment apis, which are used to obtain some information about project status during selective compilation to adjust compilation logic..
For example: is_os
, is_plat
, is_arch
, is_kind
, is_mode
, is_option
is_mode
Let's take the most commonly used is_mode
Let's talk about how to use it. This api is mainly used to determine the current compilation mode. For example, when compiling and configuring normally, it will execute:
$ xmake f -m debug $ xmake
To compile debug
Version, then the mode is debug
, then release
Version, i.e release
Has
$ xmake f -m release $ xmake
However, if only configured in this way, xmake still does not know how to compile the release version if compiling for debug, because the values of these modes are not built-in
We can freely set, such as profile, checking, etc., to compile the performance mode and detection mode, depending on the actual requirements of our project..
Generally, only debug
and release
That's all right. How to distinguish between them xmake.lua
It is configured. Generally, refer to the following configuration:
--If the current compilation mode is debug if is_mode("debug") then --Add DEBUG compilation macro add_defines("DEBUG") --Enable debug symbols set_symbols("debug") --Disable Optimization set_optimize("none") --In case of release mode elseif is_mode("release") then --Hide Symbol set_symbols("hidden") --Strip all symbols set_strip("all") --Start optimization: the fastest speed mode set_optimize("fastest") --Ignore frame pointer add_cxflags("-fomit-frame-pointer") add_mxflags("-fomit-frame-pointer") end
Enable and disable debug symbol information and determine whether optimization is disabled and enabled by determining whether the debug version is being compiled.
Of course, if our project also sets other modes, such as the performance analysis mode: profile, you can also use this to determine whether you need to add some compilation options for analysis.
is_plat
Now let's talk about the judgment of the compilation platform. This is also very practical. Although our tool is for cross platform development, the usual configuration must be universal
But after all, there are thousands of projects with different requirements, and some projects need special compilation for different platforms
At this time, we need this api, for example:
--If the current platform is Android if is_plat("android") then add_files("src/xxx/*.c") end --If the current platform is mac os x or iphone os if is_plat("macosx", "iphoneos") then add_mxflags("-framework Foundation") add_ldflags("-framework Foundation") end
The compilation of some special codes is added for the Android platform, and the Foundation framework link is added for the mac os x and iphone os platforms.
Here is a practical tip, is_xxx
Series interfaces can transfer multiple parameters at the same time, which is logically or
We can write as above:
if is_plat("macosx", "iphoneos", "android", "linux") then end
Otherwise, if you use the native syntax of Lua, although it is OK, it will be very cumbersome, for example:
if is_plat("macosx") or is_plat("iphoneos") or is_plat("android") or is_plat("linux") then end
except is_xxx
Series, like: add_xxxs
This suffix has s
The complex API of can pass multiple parameters, such as add_files
:
add_files("src/*.c", "test.c", "hello.cpp")
Wait, I won't introduce them here...
is_arch
This one is_plat
Similarly, it is only used to judge the target architecture of the current compilation, namely:
xmake f --arch=x86_64
Then, we judge in the project description:
--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 platform is armv7, arm64, armv7s, armv7-a if is_arch("armv7", "arm64", "armv7s", "armv7-a") then -- ... end
It may be tedious to judge all arm architectures one by one as above. After all, there are many architecture types for each platform. xmake provides similar add_files
The wildcard matches the pattern in to make a more concise judgment:
--If the current platform is arm platform if is_arch("arm*") then -- ... end
Use * to match all..
is_os
This is very simple. It is used to determine the current compilation target, for example:
--If the current operating system is ios if is_os("ios") then add_files("src/xxx/*.m") end
Currently, the supported operating systems are: windows linux、android、macosx、ios
is_kind
It is used to determine whether the current compiled library is a dynamic library or a static library
It is generally used in the following scenarios:
target("test") --Set the kind of target through configuration set_kind("$(kind)") add_files("src/*c") --If the current compilation is a static library, add the specified file if is_kind("static") then add_files("src/xxx.c") end
When compiling configuration, you can manually switch. Compilation type:
--Compile static libraries xmake f -k static xmake --Compile Dynamic Library xmake f -k shared xmake
is_option
If an automatic detection option or manual setting option is enabled, you can use the is_option
Interface, for example:
--If the xmake f -- demo=y option is enabled manually if is_option("demo") then --Compile the code in the demo directory add_subdirs("src/demo") end