Skip to content

在 rule 的 on_preparecmd_file 正确使用 batchcmds:vlua 的方法 #7415

@Joker-Principal

Description

@Joker-Principal

Xmake 版本

v3.0.7+20260210

操作系统版本和架构

Arch Linux

描述问题

我想用做一些多步骤的代码生成工作,于是参考了 protobuf 的 rule,用了 on_preparecmd_file 接口,因为我要调用根据输出调用工具,batchcmds:runv 并不能满足我的需求,所以使用了 batchcmds:vlua,遇到多个问题:

  1. batchcmds:vlua 没法用 lua module
  2. batchcmds:vlua 脚本中执行 lib.detect.find_tool 会触发调度器的报错
  3. 即便过程出错,最后 build 依旧显示成功

期待的结果

详见下方工程配置

工程配置

工程包括两个文件: xmake.luabuildsystem/some_module.lua

-- xmake.lua:
add_moduledirs("buildsystem", "$(projectdir)")

rule("do_something")
    set_extensions("*.in")
    on_preparecmd_file(function (target, batchcmds, gperf_file, opt)
        -- error: cannot import module: some_module, not found!
        batchcmds:vlua("some_module")

        -- error: cannot import module: buildsystem.some_module, not found!
        batchcmds:vlua("buildsystem.some_module")

        -- error: attempt to call a nil value (global 'add_moduledirs')
        -- stack traceback:
        --     [./xmake.lua:6]: in function 'scriptcmd_file'
        --     [@programdir/modules/private/action/build/target.lua:540]: in function 'job_func'
        --     [@programdir/modules/async/runjobs.lua:441]:
        -- add_moduledirs("buildsystem", "$(projectdir)")

        -- so HOW to use batchcmds:vlua() to run a lua script with module imports?

        -- workaround: use absolute path
        -- see buildsystem/some_module.lua for more
        local some_module = tostring(path("buildsystem/some_module.lua"):absolute())
        batchcmds:vlua(some_module)
    end)
rule_end()

target("main")
    set_kind("binary")
    add_files("*.in", {rule = "do_something"})
    add_files("main.cpp")
-- buildsystem/some_module.lua
function main(...)
    import("lib.detect.find_tool")

    find_tool("clang") -- error: we must call co_lock() in coroutine with scheduler!
    find_tool("gperf")
    find_tool("clang-format")

    -- ...
end

附加信息和错误日志

checking for gcc ... /usr/sbin/gcc
checkinfo: cannot runv(nim --version), No such file or directory
checking for nim ... no
xmake lua some_module
error: cannot import module: some_module, not found!
xmake lua buildsystem.some_module
error: cannot import module: buildsystem.some_module, not found!
xmake lua /test-xmake/buildsystem/some_module.lua
running given lua script file buildsystem/some_module.lua.
error: we must call co_lock() in coroutine with scheduler!
checking for g++ ... /usr/sbin/g++
checking for the c++ compiler (cxx) ... g++
checking for /usr/sbin/g++ ... ok
checking for flags (-fPIC) ... ok
> g++ "-fPIC" "-m64"
checking for g++ ... /usr/sbin/g++
checking for the linker (ld) ... g++
checking for flags (-fPIC) ... ok
> g++ "-fPIC" "-m64" "-m64"

build cache stats:
cache directory: build/.build_cache
cache hit rate: 0%
cache hit: 0
cache hit total time: 0.000s
cache miss: 0
cache miss total time: 0.000s
new cached files: 0
remote cache hit: 0
remote new cached files: 0
preprocess failed: 0
compile fallback count: 0
compile total time: 0.000s

[100%]: build ok, spent 0.201s

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions