xmake: First Project
xmake (by Ruki Wang, first released in 2015) takes a different route from Meson and CMake: it is not a generator. One self-contained binary reads xmake.lua, detects the toolchain, builds the project itself, runs it, installs it, packages it, and manages third-party dependencies through its own package manager, xrepo. The build description is ordinary Lua, in a declarative style.
The course image has xmake 3.1.2 (the official single-file "bundle" binary, which unpacks its Lua scripts into /tmp/.xmake<uid>/ on first use).
The commands
| Task | xmake | CMake equivalent |
|---|---|---|
| configure (optional) | xmake f / xmake config |
cmake -S . -B build |
| build | xmake / xmake build [target] |
cmake --build build |
| run | xmake run [target] [args] |
./build/app |
| clean | xmake clean |
cmake --build build --target clean |
| install | xmake install -o DIR |
cmake --install build --prefix DIR |
| show the configuration | xmake show, xmake f -v |
cmake -L |
| new project from a template | xmake create -l c -t console app |
— |
Configuration is implicit: plain xmake configures on first use (platform, architecture, mode, toolchain) and remembers the result in the project's .xmake/ directory. Build outputs go to build/.
A first project
mkdir -p hx/src hx/include && cd hx
cat > xmake.lua <<'L'
add_rules("mode.debug", "mode.release")
target("greet")
set_kind("static")
add_files("src/greet.c")
add_includedirs("include", {public = true})
target("hello")
set_kind("binary")
add_deps("greet")
add_files("src/main.c")
L
echo 'const char *greet(void);' > include/greet.h
echo '#include "greet.h"
const char *greet(void) { return "hello from xmake"; }' > src/greet.c
echo '#include <stdio.h>
#include "greet.h"
int main(void) { puts(greet()); return 0; }' > src/main.c
xmake -j1
xmake run hello
echo "--- nothing changed"
xmake -j1
echo "--- outputs"
find build -type f -not -path "*/.build_cache/*" | sortchecking for platform ... linux (x86_64)
[ 23%]: <hello> cache compiling.release src/main.c
[ 35%]: <greet> cache compiling.release src/greet.c
[ 47%]: <greet> archiving.release libgreet.a
[ 71%]: <hello> linking.release hello
[100%]: build ok, spent 0.29s
warning: Ascend SDK not found
hello from xmake
--- nothing changed
[100%]: build ok, spent 0.042s
--- outputs
build/.deps/greet/linux/x86_64/release/libgreet.a.d
build/.deps/greet/linux/x86_64/release/src/greet.c.o.d
build/.deps/hello/linux/x86_64/release/hello.d
build/.deps/hello/linux/x86_64/release/src/main.c.o.d
build/.objs/greet/linux/x86_64/release/src/greet.c.o
build/.objs/hello/linux/x86_64/release/src/main.c.o
build/linux/x86_64/release/hello
build/linux/x86_64/release/libgreet.a
Reading xmake.lua:
target("name")starts a target; every call after it, up to the nexttarget()(ortarget_end()), configures that target. Indentation is only for readability.set_kind():binary,static,shared,object,headeronly,phony.add_files()takes patterns:add_files("src/*.c")andadd_files("src/**.c")(recursive) are normal xmake style, unlike Meson. xmake re-evaluates the pattern on every build, so new files are picked up.{public = true}makes the include directory part of the target's interface, like CMake'sPUBLIC:hellogets-Iincludebecause itadd_deps("greet"). Other values are{interface = true}and the default (private).add_rules("mode.debug", "mode.release")defines the two build modes and their flags; without it,-m debugwould just be a name.
The progress lines show the percentage, the target in <…>, and the action. cache compiling means xmake found ccache and compiles through it (as Meson did). The output path build/linux/x86_64/release/ encodes platform, architecture and mode, so different configurations never overwrite each other's files. Objects go to build/.objs/<target>/…, and the .d files in build/.deps/ record each object's header dependencies and command line, which is how xmake decides what to rebuild (there is no Ninja underneath). build/.build_cache/ (not listed) holds xmake's own build cache.
Note: xmake 3.1.2 prints
warning: Ascend SDK not foundat the end of some commands in a fresh environment: an optional toolchain probe for Huawei's Ascend AI accelerators. It is harmless; the sessions in this course leave it in where it appears.
Debug mode and verbose builds
mkdir -p hx && cd hx
cat > xmake.lua <<'L'
add_rules("mode.debug", "mode.release")
target("hello")
set_kind("binary")
add_files("main.c")
set_warnings("all")
L
echo 'int main(void) { return 0; }' > main.c
echo "--- release (the default mode)"
xmake -j1 -v 2>&1 | grep -E '^/usr/bin/(gcc|g\+\+)' | cut -c1-120
echo "--- debug"
xmake f -m debug
xmake -j1 -v 2>&1 | grep -E '^/usr/bin/(gcc|g\+\+)' | cut -c1-120
ls build/linux/x86_64--- release (the default mode)
/usr/bin/gcc -c -m64 -fvisibility=hidden -Wall -O3 -DNDEBUG -o build/.objs/hello/linux/x86_64/release/main.c.o main.c
/usr/bin/g++ -o build/linux/x86_64/release/hello build/.objs/hello/linux/x86_64/release/main.c.o -m64 -s
--- debug
checking for platform ... linux (x86_64)
warning: Ascend SDK not found
/usr/bin/gcc -c -m64 -g -Wall -O0 -o build/.objs/hello/linux/x86_64/debug/main.c.o main.c
/usr/bin/g++ -o build/linux/x86_64/debug/hello build/.objs/hello/linux/x86_64/debug/main.c.o -m64
debug
release
xmake f -m debug (short for xmake config --mode=debug) changes the stored configuration; the next xmake rebuilds with -g -O0 instead of -O3 -DNDEBUG. -v prints each command, -D adds diagnostic detail when something goes wrong. Note that xmake links with g++, even for C code: it uses the C++ driver as the default linker so mixed C/C++ targets link without extra work. The release link also gets -s (strip symbols) and compiles with -fvisibility=hidden, two defaults from the mode.release rule worth knowing when a release-only link error mentions a missing symbol.
Test yourself
What does xmake generate for another build tool to run?
build.ninja- Makefiles
- Nothing: xmake runs the compiler itself (it can export other formats on request)
- A Visual Studio solution
In xmake.lua, how does hello get -Iinclude from the greet library?
add_includedirsis globalgreetdeclaresadd_includedirs("include", {public = true})andhellohasadd_deps("greet")- xmake scans
#includelines - It doesn't; you must repeat it
Where does xmake keep the configuration chosen by xmake f -m debug?
- In
xmake.lua - In the project's
.xmake/directory - In
build/CMakeCache.txt - Nowhere; it must be passed every time