Packages: add_requires & xrepo
xmake's biggest selling point is that dependency management is built in. Two lines in xmake.lua fetch, build and link a library:
add_requires("zlib 1.3.x", "fmt", {configs = {shared = false}})
target("app")
set_kind("binary")
add_files("src/*.cpp")
add_packages("zlib", "fmt")Behind those lines is xrepo, xmake's package manager:
- package recipes live in a git repository, xmake-repo, with well over a thousand packages (each recipe is an
xmake.luadescribing URLs, versions with SHA-256 hashes, build steps andconfigssuch assharedor feature flags); - xmake clones that repository into
~/.xmake/repositories/, downloads the source, builds it with the same toolchain and mode as your project, and installs it into~/.xmake/packages/<letter>/<name>/<version>/<hash>/, where the hash covers the platform, architecture, toolchain and configs; - it writes
xmake-requires.lockif you enableset_policy("package.requires_lock", true), pinning exact versions; - recipes can also delegate:
add_requires("conan::zlib/1.3.1"),vcpkg::zlib,brew::,apt::,pacman::, so xmake can drive Conan and vcpkg too; xrepois also a standalone command (xrepo install zlib,xrepo info zlib,xrepo env,xrepo search) for using packages without an xmake project. In the bundle binary it is available asxmake l private.xrepo ….
The offline failure
All of that needs the network: to clone the recipe repository and to download sources. In the course's offline sandbox, the first add_requires stops right there:
mkdir -p app && cd app
cat > xmake.lua <<'L'
add_requires("zlib")
target("app")
set_kind("binary")
add_files("main.c")
add_packages("zlib")
L
echo 'int main(void) { return 0; }' > main.c
xmake -y -j1 2>&1 | grep -vE '^$|Ascend'checking for platform ... linux (x86_64)
updating repositories .. error: Cloning into '/home/learner/.xmake/repositories/xmake-repo'...
fatal: unable to access 'https://github.com/xmake-io/xmake-repo.git/': Could not resolve host: github.com
In real life this is solved by a network connection, a mirror of xmake-repo (xmake g --proxy_pac=…, or add_repositories("myrepo https://git.internal/xmake-repo.git")), or by pre-filling ~/.xmake/packages in a CI cache. For this sandbox there are two offline options, and both are useful in their own right.
Option 1: system packages
{system = true} tells xmake to only look on the system (pkg-config, standard paths), never to download. The image has the development files of libxcrypt:
xmake g --network=private > /dev/null # never try to reach the network
mkdir -p app && cd app
cat > xmake.lua <<'L'
add_requires("libxcrypt", {system = true})
target("app")
set_kind("binary")
add_files("main.c")
add_packages("libxcrypt")
L
cat > main.c <<'SRC'
#include <crypt.h>
#include <stdio.h>
int main(void) { printf("%s\n", crypt("secret", "$1$saltsaltquot;)); return 0; }
SRC
xmake -y -j1 2>&1 | grep -E 'error|ok'
xmake run app
xmake require --list 2>&1 | grep 'require('
xmake require --list 2>&1 | grep -E -- '-> (links|version)' | sort # fields come in no fixed order[100%]: build ok, spent 0.086s
$1$saltsalt$9xy1btjgzLYfb7hivXtC//
require(libxcrypt): latest, system
-> links: crypt
-> version: 4.4.36
xmake g --network=private is a global setting (stored in ~/.xmake/xmake.conf) that forbids network access: xmake no longer tries to update the recipe repository before resolving packages. Teams with internal mirrors use it on build agents.
Option 2: a local package defined in the project
A package() block in xmake.lua defines a recipe right in your project. With set_sourcedir() pointing at a directory, xmake builds and installs that package exactly like one from xmake-repo (same configs, same hashed install directory), without any download:
xmake g --network=private > /dev/null
mkdir -p app/third_party/greet && cd app
cat > third_party/greet/xmake.lua <<'L'
target("greet")
set_kind("static")
add_files("greet.c")
add_headerfiles("greet.h")
L
echo 'const char *greet(void);' > third_party/greet/greet.h
echo 'const char *greet(void) { return "hi from a local package"; }' > third_party/greet/greet.c
cat > xmake.lua <<'L'
package("greet")
set_description("A greeting library, built from third_party/greet")
set_sourcedir(path.join(os.scriptdir(), "third_party/greet"))
on_install(function (package)
import("package.tools.xmake").install(package)
end)
package_end()
add_requires("greet")
target("app")
set_kind("binary")
add_files("main.c")
add_packages("greet")
L
echo '#include <stdio.h>
#include <greet.h>
int main(void) { puts(greet()); return 0; }' > main.c
xmake -y -j1 2>&1 | grep -vE '^$|Ascend|checking for platform'
xmake run app
echo "--- where it was installed"
xmake require --list 2>&1 | grep -E 'require|libfiles' | sed 's|/home/learner/app/||' => install greet latest .. ok
[ 23%]: <app> cache compiling.release main.c
[ 47%]: <app> linking.release app
[100%]: build ok, spent 0.093s
hi from a local package
--- where it was installed
require(greet): latest
-> libfiles: build/.packages/g/greet/latest/4e0143c97b65425b855ad5fd03038b6a/lib/libgreet.a
on_install used xmake's own installer (package.tools.xmake) because the library has an xmake.lua; recipes for other libraries call package.tools.cmake, autoconf, meson or make the same way, which is how xmake-repo wraps libraries built with every other build system in this course. Packages defined inside a project install into the project's build/.packages/, while repository packages go to the shared cache in ~/.xmake/packages (see the Field Notes on that cache).
Test yourself
What does xmake do on the first build with add_requires("zlib") and a network connection?
- Links
-lzand hopes - Clones/updates the xmake-repo recipes, downloads zlib's source, builds it with the project's toolchain and mode, and installs it into
~/.xmake/packages - Asks apt to install zlib
- Downloads a prebuilt Windows DLL
Which add_requires form never downloads anything?
add_requires("zlib", {system = true})add_requires("zlib latest")add_requires("zlib", {configs = {shared = true}})add_requires("conan::zlib/1.3.1")
How can a project use its own library as an xmake package without a remote repository?
- It can't
- Define
package("name")inxmake.luawithset_sourcedir()and anon_installscript, thenadd_requires("name") - Copy it into
~/.xmake/repositories - Use
add_fileson the other project'sxmake.lua