This is a mirror page, please see the original page:

Default packaging format

After version 2.5.5, we have provided a new local package packaging solution that will seamlessly integrate add_requires and add_packages.

We can execute the xmake package command to generate the default new version of the packaging format.

$ xmake package
package(foo): build/packages/f/foo generated

It will generate the file build/packages/f/foo/xmake.lua with the following content:

    set_description("The foo package")
    add_deps("add", "sub")

    on_load(function (package)
        package:set("installdir", path.join(os.scriptdir(), package:plat(), package:arch(), package:mode()))

    on_fetch(function (package)
        local result = {}
        result.links = "foo"
        result.linkdirs = package:installdir("lib")
        result.includedirs = package:installdir("include")
        return result

In fact, it uses package() to define and describe local packages, just like remote packages.

The generated directory structure is as follows:

$ tree build/packages/f/foo/
├── macosx
│   └── x86_64
│       └── release
│           ├── include
│           │   └── foo.h
│           └── lib
│               └── libfoo.a
└── xmake.lua

We can also use the add_requires/add_repositories interface to seamlessly integrate this package.

add_rules("mode.debug", "mode.release")

add_repositories("local-repo build")


Among them, add_repositories configuration specifies the warehouse root directory of the local package, and then this package can be referenced through add_requires.

In addition, the generated local package has another feature, which is to support target/add_deps, which automatically associates the dependencies of multiple packages, and automatically connects all dependency links during integration.

Here is the complete test example.

"/usr/bin/xcrun -sdk macosx clang++" -o build/macosx/x86_64/release/bar build/.objs/bar/macosx/x86_64/release/src/main.cpp.o -arch x86_64 -mmacosx-version -min=10.15 -isysroot
/Applications/ -stdlib=libc++
 -Wl,-x -lfoo -lsub -ladd -lz

Generate remote package

Out of the local package format, xmake package now also supports generating remote packages, so that users can quickly submit them to remote warehouses.

We only need to modify the package format when packaging.

$ xmake package -f remote

He will also generate packages/f/foo/xmake.lua file.

    set_description("The foo package")
    add_deps("add", "sub")

    add_versions("1.0", "")

    on_install(function (package)
        local configs = {}
        if package:config("shared") then
            configs.kind = "shared"
        import("").install(package, configs)

    on_test(function (package)
        - TODO check includes and interfaces
        - assert(package:has_cfuncs("foo", {includes = "foo.h"})

Compared with the local package, the package definition configuration has more actual installation logic, as well as the settings of urls and versions,

We can also modify urls, versions and other configuration values ​​through additional parameters, for example:

$ xmake package -f remote --url=https://xxxx/xxx.tar.gz --shasum=xxxxx --homepage=xxxxx`

xmake will also read the relevant configuration information from the target's set_license and set_version configurations.

Find packages from CMake

Now cmake is the de facto standard, so the find_package provided by CMake can already find a large number of libraries and modules. We fully reuse this part of cmake's ecology to expand xmake's integration of packages.

We can use find_package("cmake::xxx") to find some packages with cmake, xmake will automatically generate a cmake script to call cmake's find_package to find some packages and get the bread information.


$ xmake l find_package cmake::ZLIB
  links = {
  includedirs = {
  linkdirs = {
$ xmake l find_package cmake::LibXml2
  links = {
  includedirs = {
  linkdirs = {

Integrate the package in the project

If we integrate and find cmake dependent packages in the xmake.lua project configuration, we usually don't need to use find_package directly, and we can use a more general and simple package integration method.

add_requires("cmake::ZLIB", {alias = "zlib", system = true})

We specify system = true to tell xmake to force cmake to find the package from the system. If it cannot be found, the installation logic will not be followed, because cmake does not provide the installation function of package managers such as vcpkg/conan.
Only the package search feature is provided.

Specify version

add_requires("cmake::OpenCV 4.1.1", {system = true})

Specified components

add_requires("cmake::Boost", {system = true, configs = {components = {"regex", "system"}}))

Default switch

add_requires("cmake::Boost", {system = true, configs = {components = {"regex", "system"},
                                             presets = {Boost_USE_STATIC_LIB = true}}})

It is equivalent to predefine some configurations in CMakeLists.txt before calling find_package internally to find the package to control the find_package search strategy and status.

set(Boost_USE_STATIC_LIB ON) - will be used in FindBoost.cmake
find_package(Boost REQUIRED COMPONENTS regex system)

Set environment variables

add_requires("cmake::OpenCV", {system = true, configs = {envs = {CMAKE_PREFIX_PATH = "xxx"}}})

Specify custom FindFoo.cmake module script directory


add_requires("cmake::Foo", {system = true, configs = {moduledirs = "mydir/cmake_modules"}})

Related issues: #1632

For cmake packages, we have added the link_libraries configuration option to allow users to customize the configuration of package dependencies and even support for target links when looking to use cmake packages.

add_requires("cmake::xxx", {configs = {link_libraries = {"abc::lib1", "abc::lib2"}}})

xmake automatically appends the following configuration to improve the extraction of links libraries when looking for cmake packages.

target_link_libraries(test PRIVATE ABC::lib1 ABC::lib2)

Specify the search mode

In addition, we add the following search mode configuration.

add_requires("cmake::xxx", {configs = {search_mode = "config"}})
add_requires("cmake::xxx", {configs = {search_mode = "module"}})
add_requires("cmake::xxx") -- both

Specify config search mode, for example, to tell cmake to look for packages from XXXConfig.cmake.

xmake will automatically append the following configuration internally when it looks for cmake packages.