当前位置: 首页 > 工具软件 > xmake-idea > 使用案例 >

xmake更新2

谷隐水
2023-12-01

v2.5.4

支持Apt,Portage

add_requires("apt::zlib1g-dev", {alias = "zlib"}) 
target("test") 
    set_kind("binary") 
    add_files("src/*.c") 
    add_packages("zlib")

导出导入

$ xrepo export -o /tmp/output zlib
$ xrepo import -i /xxx/packagedir zlib
$ xrepo env luajit
xrepo env shell
xrepo env quit
//内置命令,退出
//加载包环境

代理

function mirror(url)
     return url:gsub("github.com", "hub.fastgit.org")
end
//设置
$ xmake g --proxy_pac=/tmp/pac.lua

XMAKE_PKG_INSTALLDIR设置安装包路径.
XMAKE_PKG_CACHEDIR缓存包目录.

v2.5.5

github action云端预编译,
根据plat/arch/MT/MD/static/shared来配置.

$ xrepo install --build openssl
//--构建,强制源码构建
add_requires("openssl", {build = true})
$ xmake repo --add local-repo git@github.com:xmake-mirror/myrepo-artifacts.git
//加预编译仓库
add_repositories("local-repo git@github.com:xmake-mirror/myrepo-artifacts.git")
//也可以

新版打包

$ xmake package
package("foo")
//用包来描述,
    set_description("The foo package")
    set_license("Apache-2.0")
    add_deps("add", "sub")

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

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

package()来定义描述本地|远程
add_requires/add_repositories来使用它们

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

add_repositories("local-repo build")
//加仓库,指定仓库根目录
add_requires("foo")
//加依赖,引用包
target("bar")
    set_kind("binary")
    add_files("src/*.cpp")
    add_packages("foo")

target/add_deps本地包,自动关联依赖.

$ xmake package -f remote
//远程包
package("foo")
    set_description("The foo package")
    set_license("Apache-2.0")
    add_deps("add", "sub")

    add_urls("https://github.com/myrepo/foo.git")
    add_versions("1.0", "<shasum256 or gitcommit>")
//多了路径,版本及下面的安装
    on_install(function (package)
        local configs = {}
        if package:config("shared") then
            configs.kind = "shared"
        end
        import("package.tools.xmake").install(package, configs)
    end)

    on_test(function (package)
        -- 检查包含与接口
        -- assert(package:has_cfuncs("foo", {includes = "foo.h"})
    end)
//
$ xmake package -f remote --url=https://xxxx/xxx.tar.gz --shasum=xxxxx --homepage=xxxxx`

可用set_licenseset_version取信息.

$ xrepo search vcpkg::pcre
//搜索第3方.

set_basename/set_filename修改目标名.set_prefixname,set_suffixname和set_extension更多了.
set_kind未置,默认为exe.

target("test")
    add_files("src/*.c")
//更简单了.
$ xmake f -p appletvos
$ xmake
//苹果tvos

导入导出

$ xmake f --export=/tmp/config.txt 
$ xmake f -m debug --xxx=y --export=/tmp/config.txt 
//导入配置
$ xmake f --import=/tmp/config.txt 
$ xmake f -m debug --xxx=y --import=/tmp/config.txt

可用--menu加上菜单.

v2.5.7

utils.bin2c内嵌资源进代码,
支持vala,用add_rules("vala").

add_values("vala.packages")
//添加依赖包,
add_requires("lua")
//下载,集成
add_rules("mode.release", "mode.debug")

add_requires("lua", "glib")

target("test")
    set_kind("binary")
    add_rules("vala")
    add_files("src/*.vala")
    add_packages("lua", "glib")
//加包
    add_values("vala.packages", "lua")

锁定包依赖:

set_policy("package.requires_lock", true)

全局设置,用来固定版本.生成xmake-requires.lock配置.

$ xmake require --upgrade
//强制升级到最新包

add_csnippets/add_cxxsnippets来运行时检查代码片段.{tryrun=true}和{output=true}设置参数.

option("test")
    add_cxxsnippets("HAS_INT_4", "return (sizeof(int) == 4)? 0 : -1;", {tryrun = true})
//运行时检测.

通过,则启用测试选项.

option("test")
    add_cxxsnippets("INT_SIZE", 'printf("%d", sizeof(int)); return 0;', {output = true, number = true})

输出,作为测试选项的.

if is_config("test", "8") tben
    -- xxx
end

来取测试选项的值.

检查代码片段

includes("check_csnippets.lua")

target("test")
    set_kind("binary")
    add_files("*.c")
    add_configfiles("config.h.in")

    check_csnippets("HAS_INT_4", "return (sizeof(int) == 4)? 0 : -1;", {tryrun = true})
    check_csnippets("INT_SIZE", 'printf("%d", sizeof(int)); return 0;', {output = true, number = true})
    configvar_check_csnippets("HAS_LONG_8", "return (sizeof(long) == 8)? 0 : -1;", {tryrun = true})
    configvar_check_csnippets("PTR_SIZE", 'printf("%d", sizeof(void*)); return 0;', {output = true, number = true})
//数字为真,而不是串.

启用捕捉输出,
config.h.in${define PTR_SIZE}会自动生成#define PTR_SIZE 4.

target("console")
    set_kind("binart")
    add_rules("utils.bin2c", {extensions = {".png", ".jpg"}})//扩展名.
//内嵌二进制
    add_files("src/*.c") 
    add_files("res/*.png", "res/*.jpg")

就可

#include "filename.png.h"

来引入.

add_rules("utils.bin2c", {extensions = ".metal"})
add_files("Renderer/*.metal")
//内嵌metal文件

加仓库,指定根目录,

add_repositories("myrepo repodir", {rootdir = os.scriptdir()})

更方便找.

os.cp("/xxx/symlink", "/xxx/dstlink", {symlink = true})
//复制后,保留符号链接

add_files加上{always_added = true}配置,即使不存在,也加上,因为构建过程中可能生成它们.但不能用模式匹配,因为当前不存在.

v2.5.8

add_rules("mode.debug", "mode.release")
target("test")
    set_kind("binary")
    add_files("src/*.pas")

构建pascal.add_fcflags()来加Pascal选项.

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

target("example")
    add_rules("swig.c", {moduletype = "lua"})
//用swig生成c/c++模块接口代码
    add_files("src/example.i", {swigflags = "-no-old-metatable-bindings"})
//swigflags选项.
    add_files("src/example.c")
    add_packages("lua")

python与c++

add_rules("mode.release", "mode.debug")
add_requires("python 3.x")

target("example")
    add_rules("swig.cpp", {moduletype = "python"})
    add_files("src/example.i", {scriptdir = "share"})
    add_files("src/example.cpp")
    add_packages("python")
//加包.

可在LoongArch架构上运行.根目录为xmake.lua,而foo目录为cmake:

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

package("foo")
    add_deps("cmake")
    set_sourcedir(path.join(os.scriptdir(), "foo"))
//置源码目录,
    on_install(function (package)
        local configs = {}
        table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:debug() and "Debug" or "Release"))
        table.insert(configs, "-DBUILD_SHARED_LIBS=" .. (package:config("shared") and "ON" or "OFF"))
        import("package.tools.cmake").install(package, configs)
//调用cmake构建代码
    end)
    on_test(function (package)
        assert(package:has_cfuncs("add", {includes = "foo.h"}))
    end)//作为测试
package_end()

add_requires("foo")
//集成使用,类似远程包.

target("demo")
    set_kind("binary")
    add_files("src/main.c")
    add_packages("foo")

add_requires("foo")add_packages("foo")集成使用.
package.tools.autoconf和package.tools.cmake模块支持mingw/cross/iphoneos/android等交叉编译.

configvar_check_features("HAS_CXX_STD_20", "cxx_std_20", {languages = "c++20"})
//检测特征
configvar_check_macros("HAS_CXX20", "__cplusplus >= 202002L", {languages = "c++20"})
//检测宏

set_symbols("debug")生成pdb.
set_languages新增c++latest.

v2.5.9

支持nim

xmake create -l nim -t console test 
xmake create -l nim -t static test 
xmake create -l nim -t shared test
//
add_rules("mode.debug", "mode.release")

add_requires("nimble::zip >0.3")

target("test")
    set_kind("binary")
    add_files("src/main.nim")
    add_packages("nimble::zip")
//nimble包支持.

unity构建,搞成个大目标来编译.

add_rules("c.unity_build") 
add_rules("c++.unity_build")
//默认合并所有文件为一个.
//
target("test")
    set_kind("binary")
    add_includedirs("src")
    add_rules("c++.unity_build", {batchsize = 2})
//批量大小,每2个一批,
    add_files("src/*.c", "src/*.cpp")
//
target("test")
    set_kind("binary")
    add_rules("c++.unity_build", {batchsize = 0}) -- 禁止批模式,0,禁用分组
    add_files("src/*.c", "src/*.cpp")
    add_files("src/foo/*.c", {unity_group = "foo"})
    add_files("src/bar/*.c", {unity_group = "bar"})
//自定义分组.
add_files("src/test/*.c", {unity_ignored = true}) --忽略这些

.mpp,.ixx,.cppm,.mxx支持模块.

set_languages("c++20")
target("test")
    set_kind("binary")
    add_files("src/*.cpp", "src/*.mpp")
//模块文件.

还有:

target("hello")
    add_deps("foo")
    add_rules("mdk.console")
//规则
    add_files("src/*.c", "src/*.s")
    add_defines("__EVAL", "__MICROLIB")
    add_includedirs("src/lib/cmsis")
//
$ xmake f -p cross -a cortex-m3 --toolchain=armcc -c 
$ xmake
//armcc编译
$ xmake f -p cross -a cortex-m3 --toolchain=armclang -c 
$ xmake
//armclang编译
add_rules("mode.debug", "mode.release")

target("foo")
    add_rules("mdk.static")
//mdk静态库
    add_files("src/foo/*.c")
$ xmake f -p wasm --toolchain=wasi 
$ xmake
//wasi工具链
$ xmake f --toolchain=circle 
$ xmake
//circle编译器

有有趣的编译期元编程特性circle.网站.

$ xmake f --toolchain=gcc-11 -c 
$ xmake
//工具链版本快速切换.
includes("check_features.lua")
//检查编译特性.

target("test")
    set_kind("binary")
    add_files("*.c")
    add_configfiles("config.h.in")
    configvar_check_features("HAS_CONSTEXPR", "cxx_constexpr")
    configvar_check_features("HAS_CONSEXPR_AND_STATIC_ASSERT", {"cxx_constexpr", "c_static_assert"}, {languages = "c++11"})

仅头文件

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

target("foo")
    set_kind("headeronly")
//仅头文件.
    add_headerfiles("src/foo.h")
    add_rules("utils.install.cmake_importfiles")
    add_rules("utils.install.pkgconfig_importfiles")

借助cmake来找包.

find_package("cmake::xxx")
$ xmake l find_package cmake::ZLIB
$ xmake l find_package cmake::LibXml2
find_package("cmake::OpenCV", {required_version = "4.1.1"})
//指定版本
find_package("cmake::Boost", {components = {"regex", "system"}})
//指定组件.
find_package("cmake::Boost", {components = {"regex", "system"}, presets = {Boost_USE_STATIC_LIB = true}})
set(Boost_USE_STATIC_LIB ON) -- 在FindBoost.cmake中用
//预设开关
find_package(Boost REQUIRED COMPONENTS regex system)
//设置环境变量
find_package("cmake::OpenCV", {envs = {CMAKE_PREFIX_PATH = "xxx"}})
//自定义FindFoo.cmake脚本目录
//mydir/cmake_modules/FindFoo.cmake
find_package("cmake::Foo", {moduledirs = "mydir/cmake_modules"})

依赖包

package("xxx")
    on_fetch(function (package, opt)
         opt.components = {}
         if package:config("regex") then
             table.insert(opt.components, "regex")
         end//可选组件.
         return package:find_package("cmake::xxx", opt)
    end)
package_end()
//通过`cmake`来找.

add_requires("xxx")
add_requires("boost", {configs = {regex = true}})

v2.6.1

增加utils.glsl2spv工具,支持编译glsl shader.
支持cargo

add_rules("mode.release", "mode.debug")
add_requires("cargo::base64 0.13.0")
//cargo包.
add_requires("cargo::flate2 1.0.17", {configs = {features = "zlib"}})

target("test")
    set_kind("binary")
    add_files("src/main.rs")
    add_packages("cargo::base64", "cargo::flate2")

使用cxxbridgec++中调用rust

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

add_requires("cargo::cxx 1.0")
//C++调用rust
target("foo")
    set_kind("static")
    add_files("src/foo.rs")
    set_values("rust.cratetype", "staticlib")
    add_packages("cargo::cxx")

target("test")
    set_kind("binary")
    add_rules("rust.cxxbridge")
//规则.
    add_deps("foo")
    add_files("src/main.cc")
    add_files("src/bridge.rsx")

foo.rs

#[cxx::bridge]
mod foo {
    extern "Rust" {
        fn add(a: i32, b: i32) -> i32;
    }
}

pub fn add(a: i32, b: i32) -> i32 {
    return a + b;
}
#[cxx::bridge]
//加桥
mod foo {
    extern "Rust" {
        fn add(a: i32, b: i32) -> i32;
    }
}

C++中调用.

http://main.cc

#include <stdio.h>
#include "bridge.rs.h"
//加桥

int main(int argc, char** argv) {
    printf("add(1, 2) == %d\n", add(1, 2));
    return 0;
}

Rust中调用C++略了.
utils.glsl2spv规则,引入*.vert/*.frag来自动编译生成*.spv.也可二进制内嵌spv文件.

add_rules("mode.debug", "mode.release")
add_requires("glslang", {configs = {binaryonly = true}})

target("test")
    set_kind("binary")
    add_rules("utils.glsl2spv", {outputdir = "build"})
//输出目录.
add_rules("utils.glsl2spv", {bin2c = true})
//这样用可引入.
    add_files("src/*.c")
    add_files("src/*.vert", "src/*.frag")
//文件.
    add_packages("glslang")
//
set_runtimes("microlib")
//微控器加运行时.
target("hello")
    add_deps("foo")
    add_rules("mdk.console")
//静态库.
add_rules("mdk.static")
    add_files("src/*.c", "src/*.s")
    add_includedirs("src/lib/cmsis")
    set_runtimes("microlib")
//这样.

简化openmp编译:

add_requires("openmp")
target("loop")
    set_kind("binary")
    add_files("src/*.cpp")
    add_packages("openmp")

v2.6.2

支持配置构建linux驱动

add_requires("linux-headers", {configs = {driver_modules = true}})
//linux-headers

target("hello")
    add_rules("platform.linux.driver")
//应用platform.linux.driver规则,就可以了
    add_files("src/*.c")
    add_packages("linux-headers")
    set_license("GPL-2.0")

就生成驱动了.Xmake会自动帮你拉取指定版本依赖内核源码.
用户不需要关心如何搭建内核驱动开发环境.
依赖,通过

add_requires("linux-headers", {configs = {driver_modules = true}})
add_requires("linux-headers 5.9.x", {configs = {driver_modules = true}})
//指定版本

实现.
交叉编译

$ xmake f -p cross -a arm --sdk=/mnt/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf -c
//指定平台,架构,sdk根目录
$ xmake
$ xmake f -p cross -a arm64 --sdk=/mnt/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu -c
$ xmake
//不同平台

分组,Run all tests和Run all benchmarks

target("test1")
    set_kind("binary")
    set_default(false)
    set_group("test")
    add_files("src/*.cpp")
//默认禁止.

target("test2")
    set_kind("binary")
    set_default(false)
    set_group("test")
    add_files("src/*.cpp")

$ xmake -g test
$ xmake --group=test

命令来构建他们.

$ xmake run -g test 
$ xmake run --group=test
//来批量运行`测试`组.

分组支持匹配模式

$ xmake build -g test_* 
$ xmake run -g test/foo_* 
$ xmake build -g bench* 
$ xmake run -g bench*

方便cmake

add_requires("cmake::ZLIB", {alias = "zlib", system = true})
//集成cmake包
target("test")
    set_kind("binary")
    add_files("src/*.c")
    add_packages("zlib")

系统表明强制从系统中调用cmake查找包.

add_requires("cmake::OpenCV 4.1.1", {system = true})
add_requires("cmake::Boost", {system = true, configs = {components = {"regex", "system"}})}
//指定版本,组件
add_requires("cmake::Boost", {system = true, configs = {components = {"regex", "system"}, presets = {Boost_USE_STATIC_LIB = true}}})
//预设置开关

查找包前,预定义配置,来控制查找策略和状态

add_requires("cmake::OpenCV", {system = true, configs = {envs = {CMAKE_PREFIX_PATH = "xxx"}}})
//设置环境变量

v2.6.3

支持vcpkg清单模式,

add_requires("vcpkg::zlib 1.2.11+10")
add_requires("vcpkg::fmt >=8.0.1", {configs = {baseline = "50fd3d9957195575849a49fa591e645f1d8e7156"}})
add_requires("vcpkg::libpng", {configs = {features = {"apng"}}})

target("test")
    set_kind("binary")
    add_files("src/*.cpp")
    add_packages("vcpkg::zlib", "vcpkg::fmt", "vcpkg::libpng")

支持,vcpkg/conan/conda/pacman/homebrew/apt/dub/cargo第3方管理包.
py.

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

target("example")
    add_rules("python.library")
//可下面这样
add_rules("python.library", {soabi = true})
    add_files("src/*.cpp")
    add_packages("pybind11")
    set_languages("c++11")

删除头文件

target("test")
    add_headerfiles("src/*.h")
    remove_headerfiles("src/test.h")

on_config,在配置完后,构建前执行.
on_load在加载目标时执行.

on_load -> after_load -> on_config -> before_build -> on_build -> after_build

内置代理

$ xmake g --proxy_pac=github_mirror.lua

支持riscv32/64.

 类似资料: