Targets, Modes & Options
This lesson covers the parts of xmake.lua you use in every project: shared libraries with versions, project-wide settings, user options, generated configuration headers and install rules.
Description scope vs script scope
An xmake.lua has two kinds of code:
- description scope:
set_*/add_*/target()/option()calls at the top level and inside targets. They are declarations, evaluated when xmake loads the project; plain Lua logic (if is_mode("debug") then … end,is_plat("linux"), loops) is allowed around them. - script scope: functions passed to
on_build,before_build,after_link,on_install,on_run… They run at build time, can call xmake's modules (os.cp,os.exec,io.writefile,import(...)), and are where custom logic belongs.
Calls placed before the first target() apply to all targets.
A versioned shared library, an option and a config header
mkdir -p calc/src calc/include && cd calc
cat > xmake.lua <<'L'
set_project("calc")
set_version("1.4.0", {soname = true}) -- libcalc.so.1.4.0, soname libcalc.so.1
add_rules("mode.debug", "mode.release")
set_languages("c17")
set_warnings("all", "error")
option("fast_mul")
set_default(false)
set_showmenu(true)
set_description("Use the shift-and-add multiplier")
add_defines("CALC_FAST_MUL")
option_end()
target("calc")
set_kind("shared")
add_files("src/calc.c")
add_includedirs("include", {public = true})
add_headerfiles("include/calc.h")
add_options("fast_mul")
set_configdir("$(builddir)/config")
add_configfiles("src/calc_config.h.in")
add_includedirs("$(builddir)/config")
target("calc-cli")
set_kind("binary")
add_deps("calc")
add_files("src/main.c")
L
cat > src/calc_config.h.in <<'T'
#pragma once
#define CALC_VERSION "${VERSION}"
#define CALC_VERSION_MAJOR ${VERSION_MAJOR}
T
echo 'int calc_mul(int a, int b); const char *calc_version(void);' > include/calc.h
cat > src/calc.c <<'SRC'
#include "calc.h"
#include "calc_config.h"
const char *calc_version(void) { return CALC_VERSION; }
int calc_mul(int a, int b) {
#ifdef CALC_FAST_MUL
int r = 0; while (b) { if (b & 1) r += a; a <<= 1; b >>= 1; } return r;
#else
return a * b;
#endif
}
SRC
cat > src/main.c <<'SRC'
#include <stdio.h>
#include "calc.h"
int main(void) { printf("calc %s: 6*7=%d\n", calc_version(), calc_mul(6, 7)); return 0; }
SRC
xmake f --help | grep -A1 fast_mul
xmake -j1 > /dev/null 2>&1
xmake run calc-cli
ls build/linux/x86_64/release
readelf -d build/linux/x86_64/release/libcalc.so.1.4.0 | grep SONAME
cat build/config/calc_config.h
echo "--- with the option"
xmake f --fast_mul=y
xmake -j1 -v 2>&1 | grep -o '\-DCALC_FAST_MUL' | sort -u
xmake run calc-cli --fast_mul=[y|n] Use the shift-and-add multiplier
calc 1.4.0: 6*7=42
calc-cli
libcalc.so
libcalc.so.1
libcalc.so.1.4.0
0x000000000000000e (SONAME) Library soname: [libcalc.so.1]
#pragma once
#define CALC_VERSION "1.4.0"
#define CALC_VERSION_MAJOR 1
--- with the option
checking for platform ... linux (x86_64)
generating src/calc_config.h.in ... ok
warning: Ascend SDK not found
-DCALC_FAST_MUL
calc 1.4.0: 6*7=42
What happened:
set_version("1.4.0", {soname = true})gave the shared library a versioned file name, a soname and the two symlinks. Withoutsoname = true, xmake builds a plainlibcalc.so.option("fast_mul")declared a user option;set_showmenu(true)lists it inxmake f --help, andxmake f --fast_mul=yturns it on.add_options("fast_mul")attaches the option's settings (here the define) to the target only while it is enabled. Options can also run checks, see below.add_configfiles()filled${VERSION}and friends fromset_version()into a header in$(builddir)/config, the counterpart of Meson'sconfigure_fileand CMake'sconfigure_file.set_warnings("all", "error")andset_languages("c17")are portable spellings that xmake translates for GCC, Clang or MSVC.
Checks in options
An option can test the system and enable itself only when the test passes, which covers what AC_CHECK_HEADERS does for Autotools:
mkdir -p chk && cd chk
cat > xmake.lua <<'L'
option("epoll")
add_cincludes("sys/epoll.h")
add_defines("HAVE_EPOLL")
option_end()
option("kqueue")
add_cincludes("sys/event.h")
add_defines("HAVE_KQUEUE")
option_end()
option("strlcpy")
add_cfuncs("strlcpy")
add_cincludes("string.h")
add_defines("HAVE_STRLCPY")
option_end()
target("app")
set_kind("binary")
add_files("main.c")
add_options("epoll", "kqueue", "strlcpy")
L
cat > main.c <<'SRC'
#include <stdio.h>
int main(void) {
#ifdef HAVE_EPOLL
puts("epoll");
#endif
#ifdef HAVE_KQUEUE
puts("kqueue");
#endif
#ifdef HAVE_STRLCPY
puts("strlcpy");
#endif
return 0;
}
SRC
xmake f -c 2>&1 | grep -E 'checking for (epoll|kqueue|strlcpy)' | sort # xmake checks options in no fixed order
xmake -j1 > /dev/null 2>&1 && xmake run appchecking for epoll ... ok
checking for kqueue ... no
checking for strlcpy ... ok
epoll
strlcpy
Installing
xmake install -o DIR installs every target with its add_headerfiles(); the utils.install.pkgconfig_importfiles and utils.install.cmake_importfiles rules also generate a .pc file and a CMake package for a library, so non-xmake projects can consume it. The Mini Project uses both.
Test yourself
Where do os.cp(...) and other file operations belong in xmake.lua?
- At the top level
- Inside script-scope functions such as
on_installorafter_build - Inside
option() - They are not allowed
How does a user turn on the option fast_mul declared with set_showmenu(true)?
xmake --fast_mulxmake f --fast_mul=y- Edit
xmake.lua xmake option fast_mul on
What does set_version("1.4.0", {soname = true}) do for a shared library target on Linux?
- Nothing, it only sets the project version
- Builds
libcalc.so.1.4.0with sonamelibcalc.so.1and the symlinks - Names the file
libcalc-1.4.0.so - Creates a pkg-config file