精通xmake2

工具链都有相应include/lib目录,有类似:

    - bin
       - 工具
       - 工具
       - ...
   - lib
       - libxxx.a
   - include
       - xxx.h

结构.则可以用:

$ xmake f -p cross --sdk=/home/toolchains_sdkdir

来编译了.-p cross表明交叉编译,--sdk=工具链根目录.一般xmake自动处理.
手动调整:

$ xmake f -p linux --sdk=/home/toolchains_sdkdir --bin=/usr/opt/bin 
//bin目录
$ xmake

--cross=配置编译工具前缀名.

$ xmake f -p linux --sdk=/usr/toolsdk --bin=/opt/bin --cross=armv7-linux-
$ xmake f -p linux --sdk=/user/toolsdk --cc=armv7-linux-clang --cxx=armv7-linux-clang++
//继续加选项

--ccc.--cxxC++编译器.有CC/CXX环境变量时,环境变量优先.

xmake f --cxx=clang++@/home/xxx/c++mips.exe
//等价
$ xmake f -p linux --sdk=/user/toolsdk --ld=armv7-linux-clang++ --sh=armv7-linux-clang++ --ar=armv7-linux-ar

ld链接器,sh共享,ar静态库,同样LD/SH/AR环境变量.
--includedirs和--linkdirs和--links来追加搜索路径.
:或者;(窗口)来分割.
--cflags,--cxxflags,--ldflags,--shflags--arflags设置选项,as为汇编器.

$ xmake f -p mingw
$ xmake -v
//
$ xmake g --mingw=/home/mingwsdk 
$ xmake f -p mingw 
$ xmake

切换平台.
设置工具链,不必手动指定.

target("test")
    set_kind("binary")
    set_toolchain("cxx", "clang")
    set_toolchain("ld", "clang++")

设置配置参数,等价于xmake f:

set_config("cflags", "-DTEST")
set_config("sdk", "/home/xxx/tooksdk")
set_config("cc", "gcc")
set_config("ld", "g++")

还可用-p/--plat=自定义平台.

$ xmake f -p linux --sdk=/usr/local/arm-xxx-gcc/ 
$ xmake

林操平台.
目标为子工程.

add_links("tbox")
add_linkdirs("lib")
add_includedirs("include")
//外面放通用的
target("test1")
    set_kind("binary")
    add_files("src/test1/*.c")
    
target("test2")
    set_kind("binary")
    add_files("src/test2/*.c")

不同项目,单独处理.用add_deps设置依赖关系.

target("foo")
    set_kind("static")
    add_files("*.c")
    add_defines("FOO", {public = true})
    add_includedirs("foo/inc", {public = true})

target("test1")
    set_kind("binary")
    add_deps("foo")
    add_files("test1/*.c")
    
target("test2")
    set_kind("binary")
    add_deps("foo")
    add_files("test2/*.c")

可继承linkdirs,links,includedirs以及defines等关系,且自动处理编译顺序.依赖还是级联的.

add_deps("dep1", "dep2", {inherit = false})
//不继承的依赖.
add_includedirs("inc1", {public = true})
//公开目录.

private(默认,私)/public(公开)/interface(仅被依赖)三种.
add_includedirs, add_defines, add_cflags等,都支持可见性.

大工程

子工程,用xmake.lua,用includes来导入.

$ xmake build test1
$ xmake run test3
$ xmake install demo1
//不用切换到子目录去操作.

xmake.lua放通用配置.

-- 定义项目
set_project("tbox")
set_xmakever("2.3.2")
set_version("1.6.5", {build = "%Y%m%d%H%M"})

-- 常见标志
set_warnings("all", "error")
set_languages("c99")
add_cxflags("-Wno-error=deprecated-declarations", "-fno-strict-aliasing", "-Wno-error=expansion-to-defined")
add_mxflags("-Wno-error=deprecated-declarations", "-fno-strict-aliasing", "-Wno-error=expansion-to-defined")

-- 构建模式
add_rules("mode.release", "mode.debug")

-- 包含子项目
includes("test", "demo")

子配置只影响自己及下层.
子项目可跨xmake.lua单来依赖.

target("demo1")
    set_kind("binary")
    add_files("demo1/*.c")
    add_deps("test1")
//依赖平级对方.优先编译被依赖,依赖方自动链接相应库等

add_files,add_includedirs都是相对于子项目目录.只需要考虑当前目录.

projectdir
  - test
    - xmake.lua
    - test1/ *.c
    - test2/ *.c
//
target("test1")
    add_files("test1/*.c")
target("test2")
    add_files("test2/*.c")
//这样.

$(projectdir)根目录来定位.
includes是全局接口.

includes("test1", "test2")
//要这样,不能放`目标`内部
target("test")
    set_kind("static")
    add_files("test/*.c")

直接includes("test1")引用目录即可.
批量:

includes("test/*/xmake.lua")
//递归
includes("test/**/xmake.lua")

描述域配置文件.复杂脚本在脚本域中写.
on_xxx,after_xxx,before_xxx都是脚本域.
可用import接口导入xmake内置的各种lua模块,及用户的lua脚本

target("test")
    set_kind("binary")
    add_files("src/*.c")
    on_load("modules.test.load")
    on_install("modules.test.install")
//脚本放单独文件中.

可配置更复杂的.
单独lua脚本文件以main为主入口.

import("core.project.config")
import("mymodule")
//导入内置/自写扩展.

function main(target)
    if is_plat("linux", "macosx") then
        target:add("links", "pthread", "m", "dl")
    end
end
//主为入口

on_load加载目标时启动,只执行一遍.而描述域则可能多次执行.
还有,on/after/before_build/install/package/run阶段.
脚本可用来自定义(on_build,on_run..)脚本,开发插件,开发模板,扩展平台,自定义任务等.

import("core.base.option")
import("core.base.task")
//用.分割路径.
function main()
    
    -- 取参数选项
    print(option.get("version"))

    -- 运行任务和插件
    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`支持平台列表
    print(p.plats())
end

新的

import("xxx.xxx", {try = true, anonymous = true})

try失败,无误返回,匿名不引入当前域.
测试lua.

function main() 
    print("hello xmake!") 
end
//运行.
$ xmake lua /tmp/test.lua
$ xmake lua lib.detect.find_tool gcc
//快速执行.
$ xmake lua
//交互模式

导入扩展模块

> task = import("core.project.task")
//交互模式
> task.run("hello")
hello xmake!

target:set,target:add来添加目标属性.set_,add_可用前者来动态配置.还有许多其他接口.如名字,取,目标文件,目标类型,依赖们,依赖,选项们,选项,包们,包,源列表.
on_link处理链接时动作.on_build构建时,

target("test")
    on_build("windows", function (target)
        print("build for windows")
    end)
//还可针对平台.

on_build_file自定义编译脚本.
on_build_files一批.

target("test")
    set_kind("binary")
    add_files("src/*.c")
    on_build_files(function (target, sourcebatch, opt)
    end)
//sourcebatch来表示.

sourcebatch方法有sourcekind/sourcefiles/objectfiles/dependfiles
on_clean清理动作.
on_package打包,on_install安装,on_uninstall卸载.on_run运行apk
还有target:before_xxx和target:after_xxx系列勾挂口.
xmake内置模块,如os,io操作.后面函数等略了.

posted @   zjh6  阅读(55)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
点击右上角即可分享
微信分享提示