xmake0
xmake -f /tmp/xxx.lua
xmake --file=xxx.lua
最简单:
-- 添加名为`demo`的目标到工程
target("demo")
-- 设置目标程序类型为二进制,一般为`控制台`的终端`命令行`程序
set_kind("binary")
-- 添加`src`目录下的所有c文件
add_files("src/*.c")
更复杂点:
-- 如果当前编译的是debug模式
if is_mode("debug") then
-- 启用调试符号
set_symbols("debug")
-- 禁用优化
set_optimize("none")
end
-- 如果当前编译的是release模式
if is_mode("release") then
-- 设置符号可见性为不可见
set_symbols("hidden")
-- 启用最快优化模式
set_optimize("fastest")
-- 去除所有符号信息,包括调试符号
set_strip("all")
end
-- 添加名为test的目标
target("test")
-- `test`编译为`静态库`类型
set_kind("static")
-- 添加所有`c++`文件,包括`子目录`(注:**表明多级递归匹配模式)
add_files("src/**.cpp")
add_files不仅可加源代码,还可加目标/库文件.
add_links只能加链接,而add_files是解包,再打包至目标文件.
target("test")
-- 生成静态库:libtest.a
set_kind("static")
//也可
set_kind("shared")
-- 添加对象文件
add_files("obj/*.o")
-- 添加静态库,重新打包`目标文件`到`libtest.a`中,生成新静态库
add_files("lib/*.a")
重组目标或库文件了.
add_files支持.c/.cpp/.s/.S/.m/.mm/.o/.obj/.a/.lib等.可用|来排除(过滤).
工程多目标,默认编译所有目标.默认编译发布版.
xmake -r
xmake --rebuild
上面为强制重建.
xmake f -a armv7
xmake
-a指定架构.默认架构macosx下默认是x86_64,iphoneos下是armv7.交叉编译:
xmake f -p iphoneos
xmake
//
xmake f -p android --ndk=xxxx
xmake
//安卓
为方便不同平台间来回切换编译:
-- 设置`ndk`到`全局配置`中
xmake g --ndk=xxx
-- 切换到`android`编译平台,不需要每次设置`ndk`了
xmake f -p android
xmake -r
-- 切换到`ios`编译平台
xmake f -p iphoneos
xmake -r
编译时,会缓存设置,每次配置时只需修改要改的部分参数
xmake f -c
xmake f --clean
xmake
//清除缓存
xmake f -c -v
xmake f --clean --verbose
xmake
-v显示细节.
xmake f --help查看帮助.
-q,安静.-D诊断.
-F FILE读入lua配置,
-P PROJECT切换到项目目录.
XMAKE_PROJECT_DIR的工程目录.搜索优先级参数/前面工程目录/当前目录.
--export=EXPORT导出当前配置.
--import=IMPORT,从给定文件导入配置
--menu用菜单来配置.
-p PLAT为平台编译,-a ARCH为架构编译
平台有:
- android - appletvos - bsd - cross - cygwin - iphoneos - linux - macosx - mingw - msys - wasm - watchos - windows
架构有:
- android: armeabi armeabi-v7a arm64-v8a x86 x86_64 mips mip64
- appletvos: arm64 armv7 armv7s i386 x86_64
- bsd: i386 x86_64
- cross: i386 x86_64 arm arm64 mips mips64 riscv riscv64 s390x ppc ppc64 sh4
- cygwin: i386 x86_64
- iphoneos: arm64 armv7 armv7s i386 x86_64
- linux: i386 x86_64 armv7 armv7s arm64-v8a mips mips64 mipsel mips64el
- macosx: i386 x86_64 arm64
- mingw: i386 x86_64 arm arm64
- msys: i386 x86_64
- wasm: wasm32
- watchos: armv7k i386
- windows: x86 x64
-m MODE模式,-k KIND类型.
--require=REQUIRE依赖包,
--pkg_searchdirs远程包搜索目录.
--cross=CROSS,设置交叉工具链前缀
--target_os=,目标os.
--bin=工具链bin目录,
--sdk=SDK,设置交叉SDK目录
--toolchain=工具链.
--mm=MM,Objc编译器
--mxx=MXX,Objc++编译器
--ld=LD,链接器
--ar=AR,静态库链接器
--sh=SH,共享库链接器
--mflags=MFLAGS,Objc编译器标志
--mxflags=MXFLAGS,Objc/c++编译器标志
--mxxflags=MXXFLAGS,Objc++编译器标志
--ldflags=LDFLAGS,二进制链接器标志
--arflags=ARFLAGS,静态库链接器标志
--shflags=SHFLAGS,共享库链接器标志
--links=LINKS,链接库
--syslinks=SYSLINKS,系统链接库
--linkdirs=LINKDIRS,链接搜索目录
--includedirs=INCLUDEDIRS,包含搜索目录
--frameworks=FRAMEWORKS,框架
--frameworkdirs=FRAMEWORKDIRS,框架搜索目录
--cu=CU,Cuda编译器
--cu-ccbin=CU-CCBIN,CudaHostC++编译器
--culd=CULD,Cuda链接器
--cuflags=CUFLAGS,Cuda编译器标志
--culdflags=CULDFLAGS,Cuda链接器标志
--cc=CC,C编译器
--cxx=CXX,C++编译器
--cpp=CPP,C预处理器
--ranlib=RANLIB,静态库索引生成器
--cflags=CFLAGS,C编译器标志
--cxflags=CXFLAGS,C/C++编译器标志
--cxxflags=CXXFLAGS,C++编译器标志
--zc=ZC,Zig编译器
--zcld=ZCLD,Zig链接器
--zcar=ZCAR,Zig静态库归档器
--zcsh=ZCSH,Zig共享库链接器
--nc=NC,Nim编译器
--ncld=NCLD,Nim链接器
--ncar=NCAR,Nim静态库归档器
--ncsh=NCSH,Nim共享库链接器
--mrc=MRC,微软资源编译器
--mrcflags=MRCFLAGS,微软资源标志
--fc=FC,Fortran编译器
--fcld=FCLD,Fortran链接器
--fcsh=FCSH,Fortran共享库链接器
--sc=SC,Swift编译器
--scld=SCLD,Swift链接器
--scsh=SCSH,Swift共享库链接器
--rc=RC,Rust编译器
--rcld=RCLD,Rust链接器
--rcar=RCAR,Rust静态库归档器
--rcsh=RCSH,Rust共享库链接器
--pc=PC,Pascal编译器
--pcld=PCLD,Pascal链接器
--pcsh=PCSH,Pascal共享库链接器
--go=GO,Golang编译器
--gcld=GCLD,Golang链接器
--go-ar=GO-AR,Golang静态库链接器
--as=AS,汇编器
--asflags=ASFLAGS,汇编器标志
--dc=DC,Dlang编译器
--dcld=DCLD,Dlang链接器
--dcar=DCAR,Dlang静态库归档器
--dcsh=DCSH,Dlang共享库链接器
--ndk=NDK,
--android_sdk=,设置目录
还有cuda/qt/vcpkg/mingw/vs/vs_toolset/vs_sdkver/vs_runtime/wdk/wdk_sdkver/wdk_winver/debugger等选项.
ccache/trybuild(第3方工具)/tryconfigs,
-o构建目录,
-MT,-MTd,-MD,-MDd选项
-auto,-make,-autotools,-cmake,-scons,-meson,-bazel,-ninja,-msbuild,-xcodebuild,-ndkbuild为第3方编译工具.
导入类库
import导入扩展类库/自定义类库.
用于自定义脚本(on_build,on_run..),插件开发,模板开发,平台扩展,自定义任务等
导入机制:优先从当前脚本目录下导入,再从扩展类库中导入.
导入core核心扩展模块
import("core.base.option")
import("core.project")
import("core.project.task")
import("core")
function main()
-- 取参数选项
print(option.get("version"))
-- 运行任务和插件
task.run("hello")
project.task.run("hello")
core.project.task.run("hello")
end
//
import("modules.hello1")
import("modules.hello2")
//导入当前目录,_为私有不能访问,其余均可访问
指定目录或别名:
import("hello3", {rootdir = "/home/xxx/modules"})
import("core.platform.platform", {alias = "p"})
function main()
-- 可用p来调用`platform`模块的`plats`接口,取所有`xmake`支持平台列表
table.dump(p.plats())
end
//导入并继承
import("xxx.xxx", {inherit = true})
类似D的公 导入.外部可用.
自定义脚本
自动编译,安装,运行安卓.
-- 定义`androidapp`的测试`demo`
target("demo")
-- 生成动态库:libdemo.so
set_kind("shared")
-- 设置对象`输出目录`,可选
set_objectdir("$(buildir)/.objs")
-- 编译完`libdemo.so`的生成目录,设置为`app/libs/armeabi`
set_targetdir("libs/armeabi")
-- 添加`jni`的代码文件
add_files("jni/*.c")
-- 设置自定义`打包`脚本,使用`xmake`编译完`libdemo.so`后,执行`xmake p`来打包
-- 会自动使用`ant`来编译`app`为`apk`文件
on_package(function (target)
-- trace
print("buiding app")
-- 使用`ant`编译`app`成`apk`文件,输出信息重定向到日志文件
os.run("ant debug")
end)
-- 设置自定义安装脚本,自动安装`apk`文件
on_install(function (target)
-- 跟踪
print("installing app")
-- 使用`adb`安装打包生成的`apk`文件
os.run("adb install -r ./bin/Demo-debug.apk")
end)
-- 设置自定义运行脚本,自动运行安装好的app程序,并且自动获取设备输出信息
on_run(function (target)
-- run it
os.run("adb shell am start -n com.demo/com.demo.DemoTest")
os.run("adb logcat")
end)
运行:
#重新`编译`工程,在`app/libs/armeabi`生成`libdemo.so`
xmake -r
# 打包
xmake p
# 安装到设备上
xmake i
# 运行app,并取日志信息
xmake r demo
//也可一步
xmake -r; xmake i; xmake r demo
//增量编译
xmake i; xmake r demo
构建,打包,清除,安装,卸载,运行,都有入口.
on_build,on_clean,on_package,on_install,on_uninstall,on_run,before_build,before_clean,before_package,before_install,before_uninstall,before_run,after_build,after_clean,after_package,after_install,after_uninstall,after_run接口.
参数为目标.有:name()/targetfile()/get("kind")/get("defines")/等方法.脚本内可导入各种类库.
os:系统类库
string:串类库
path:路径类库
table:table和array处理
io:文件io处理
coroutine:协程类库.
自定义任务
任务可写插件/任务.简单任务:
-- 定义`hello`的任务
task("hello")
-- 任务入口
on_run(function ()
-- 显示你好
print("hello xmake!")
end)
无set_menu,只能在自定义脚本中用.使用:
target("demo")
-- 自定义清理动作
on_clean(function(target)
-- 导入任务模块
import("core.project.task")
-- 运行你好
task.run("hello")
end)
要传递参数,用set_menu或脚本传参.
task.run("hello", {}, "arg1", "arg2")
//{}用于`置菜单`方式
如何取两个参数呢?
-- 定义任务
task("hello")
-- 入口有两个参数
on_run(function (arg1, arg2)
-- 显示你好
print("hello xmake: %s %s!", arg1, arg2)
end)
不能用命令行外部传参.一般在任务间调用.
配置参数
-- 定义任务
task("hello")
-- 设置类型为插件
set_category("plugin")
-- 插件入口,这里指定`main`,说明从当前`插件目录`的`main.lua`脚本中加载`插件`入口
on_run("main")
-- 设置插件`命令行`选项,这里无参数选项,仅显示`插件描述`
set_menu({
-- 用法
usage = "xmake hello [options]"
-- 描述
, description = "Hello xmake!"
-- 定义两个参数选项
-- xmake hello --output="xxx" 指定输出的内容
-- xmake hello -v 显示插件版本
, options =
{
-- 第一个值设置简写:xmake hello -o xxx
-- 第二个值设置全称:xmake hello --output=xxx
-- 第三个值设置类型:`kv`是键值对,`k`是仅有`key`没有值`(-v--version)`,`v`是值类型无`key`
-- 第四个值指定参数描述信息
{'o',"output","kv",nil,"置输出"},
{'v',"version","k","1.0","版本."}
}
})
//目录结构:
hello
- xmake.lua
//描述
- main.lua
//入口
内容:
-- 导入选项模块
import("core.base.option")
-- main.lua入口函数
function main()
-- 显示版本?
if option.get("version") then
print("version: %s", option.get("version"))
else
-- 显示内容
print("hello %s!", option.get("output") or "xmake")
end
end
//运行
xmake hello --version
xmake hello -v
xmake hello -o xxx
xmake hello --output=xxx
//上面自定义的.
xmake hello -h
xmake hello --help
//内定
_g是全局私有变量,有_为私有.
xmake -h得到帮助:动作有:
单命令 | 意思 |
|---|---|
i | 安装包 |
f | 配置 |
g | 全局 |
c | 清理 |
p | 包 |
r | 运行 |
q | 安装并更新要求包 |
空 | 创建(create) |
b | build,构建 |
空 | update,更新 |
u | uninstall,卸载. |
插件有:
命令 | 意思 |
|---|---|
repo | 仓库 |
l,lua | 跑脚本 |
show | 显示 |
project | 生成项目(工程)文件 |
m,macro | 跑给定宏,录制宏并回放 |
doxygen | 生成doxygen文档 |
plugin | 管理插件. |
service | 服务 |
写完插件后,放xmake/plugins(内置)或~/.xmake/plugins用户全局,或用
add_plugindirs("./hello")
加插件目录,但仅对当前工程有效.目标中可导入再运行.
批量打包
xmake macro package
//打包指定平台所有架构
xmake macro package -p iphoneos
//ios
xmake macro package -p iphoneos -f "-m debug" -o /tmp/output
//模式为调试,o为输出目录
xmake macro package --help
//帮助
宏脚本
很实用,
xmake macro --help
-b,--begin开始记录宏
//用名字记录宏.
test
xmake macro --begin
xmake config --plat=macosx
xmake clean
xmake -r
xmake package
xmake macro --end
test
-e停止
--end
--show,显示给定宏
-l,列举宏
--list
-d,删宏
--delete
-c,清理所有宏
--clear
--import=IMPORT,导入给定宏`文件/目录`
xmake macro --import=/xxx/macro.lua test
xmake macro --import=/xxx/macrodir
--export=EXPORT 导出.
xmake macro --export=/xxx/macro.lua test
xmake macro --export=/xxx/macrodir
name,置宏名
//跑指定宏
xmake macro test
//跑匿名
macro:xmake macro .
//跑上个
command:xmake macro ..
一次编译并记录,别人就可使用.
# 开始记录宏
xmake macro --begin
# 执行`xmake`命令
xmake f -p android --ndk=/xxx/ndk -a armv7-a
xmake p
xmake f -p mingw --sdk=/mingwsdk
xmake p
xmake f -p linux --sdk=/toolsdk --toolchains=/xxxx/bin
xmake p
xmake f -p iphoneos -a armv7
xmake p
xmake f -p iphoneos -a arm64
xmake p
xmake f -p iphoneos -a armv7s
xmake p
xmake f -p iphoneos -a i386
xmake p
xmake f -p iphoneos -a x86_64
xmake p
# 结束宏记录,这里不设置宏名,记录的是匿名宏
xmake macro --end
好了,可回放执行这个宏了..
# 之前最近记录的一次匿名宏
xmake macro .
//取名字
# 结束记录,并且命名为test宏
xmake macro --end test
# 回放这个test宏
xmake macro test
//管理
xmake macro --show test
//显示
自定义脚本
写脚本时,xmake就是个lua加载器.
function main()
print("hello xmake!")
end
//hello.lua
xmake l /tmp/hello.lua
//运行
main为入口,
function main(argv)
-- 打印所有参数值
for _, value in ipairs(argv) do
print(value)
end
-- 或者可以直接dump所有
table.dump(argv)
end
//
xmake lua /tmp/hello.lua hello xmake
//传递参数
都在scripts里面,用xmake lua -l查看,有:
scripts: cat cp echo lipo mkdir mv rm rmdir time脚本.
错误立即中断.os/path/io/常用动作.
还有string,table,debug,coroutine,pairs,ipairs,tostring,tonumber模块.
检测头文件依赖
系统头文件过滤,一般不变化.缓存头文件检测结果.二次$xmake变化不大.
cprint终端高亮.
cprint('${bright}hello xmake')
cprint('${red}hello xmake')
cprint('${bright green}hello ${clear}xmake')
cprint('${blue onyellow underline}hello xmake${clear}')
cprint('${red}hello ${magenta}xmake')
cprint('${cyan}hello ${dim yellow}xmake')
属性:
${bright red underline onyellow}
影响一行,上面为红色+黄色背景+下划线,${clear}来清理前面.颜色细节
快速构建
xmake create -P ./hello
//空工程.
xmake -P .
//也可以
xmake run -d hello
//调试.
$ xmake f -p android --ndk=~/files/android-ndk-r10e/
//安卓.
$ xmake f -p iphoneos
//ios
$ xmake f -p mingw --sdk=/usr/local/i386-mingw32-4.3.0/
//为mingw平台交叉编译.
cmd下自动编译vs.自动多任务构建.
用$(varname)取内置变量,
add_cxflags("-I$(buildir)")
//
target("test")
after_build(target)
print("build ok for $(plat)!")
end
//脚本中支持.
print("$(os)")
print("$(host)")
print("$(tmpdir)")
print("$(curdir)")
//用原生壳
target("test")
set_kind("binary")
if is_plat("linux") then
add_ldflags("$(shell pkg-config --libs sqlite3)")
end
//来取链接库名
即使设置了
set_symbols("debug")
,默认也不生成pdb文件,要生成,要这样改:
-- 先禁用内置调试符号开关
--set_symbols("debug")
-- 静态库目标
target("test")
set_kind("static")
-- 仅针对`windows`平台
if is_plat("windows") then
-- 生成`pdb`
add_cxflags("-ZI", "-Fd$(buildir)\\test.pdb")
add_ldflags("-pdb:$(buildir)\\test.pdb")
add_arflags("-pdb:$(buildir)\\test.pdb")
end
-- 可执行
target("demo")
set_kind("binary")
add_deps("test")
add_links("test")
-- 仅针对windows平台
if is_plat("windows") then
-- 生成pdb
add_cxflags("-ZI", "-Fd$(buildir)\\demo.pdb")
add_ldflags("-pdb:$(buildir)\\demo.pdb")
end
//
$ xmake f -h
$ xmake g -h
//配置的详细参数
浙公网安备 33010602011771号