TExcelTAD
Home › Meson, Autotools & xmake › Packages: add_requires & xrepo

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:

lua
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.lua describing URLs, versions with SHA-256 hashes, build steps and configs such as shared or 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.lock if you enable set_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;
  • xrepo is 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 as xmake 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:

bash · try it yourself
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:

bash · try it yourself
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$saltsalt
quot;)); 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:

bash · try it yourself
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?

  1. Links -lz and hopes
  2. 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
  3. Asks apt to install zlib
  4. Downloads a prebuilt Windows DLL

Which add_requires form never downloads anything?

  1. add_requires("zlib", {system = true})
  2. add_requires("zlib latest")
  3. add_requires("zlib", {configs = {shared = true}})
  4. add_requires("conan::zlib/1.3.1")

How can a project use its own library as an xmake package without a remote repository?

  1. It can't
  2. Define package("name") in xmake.lua with set_sourcedir() and an on_install script, then add_requires("name")
  3. Copy it into ~/.xmake/repositories
  4. Use add_files on the other project's xmake.lua