xmake-io / xmake

🔥 A cross-platform build utility based on Lua
https://xmake.io
Apache License 2.0
10.14k stars 782 forks source link

rule on_buildcmd_file 无法指定生成的头文件依赖顺序 #5560

Closed codehz closed 2 months ago

codehz commented 2 months ago

Xmake 版本

v2.9.4+20240729

操作系统版本和架构

Darwin 23.6.0 arm64 arm

描述问题

通过自定义规则生成头文件时,无法通过add_depfiles set_depmtime set_depcache指定头文件生成和后续编译的依赖顺序,从而导致多次构建的问题

期待的结果

先执行生成头文件的任务,再执行后续依赖这个头文件的源文件的编译

工程配置

该配置有意使用sleep来手动引入延迟,消除复现bug的竞争条件


rule("cp")
  set_extensions(".inc")
  on_config(function (target)
    local headersdir = path.join(target:autogendir(), "rules", "cp")
    target:add("includedirs", headersdir)
  end)
  on_buildcmd_file(function (target, batchcmds, sourcefile, opt)
    local headersdir = path.join(target:autogendir(), "rules", "cp")
    local headerfile = path.join(headersdir, path.basename(sourcefile) .. ".h")
    batchcmds:show_progress(opt.progress, "${color.build.object}cp %s", sourcefile)
    batchcmds:mkdir(headersdir)
    batchcmds:vrunv("sleep", {"1"})
    batchcmds:vrunv("cp", {sourcefile, headerfile})

    batchcmds:add_depfiles(sourcefile)
    batchcmds:set_depmtime(os.mtime(headerfile))
    batchcmds:set_depcache(target:dependfile(headerfile))
  end)

target("main")
  add_rules("cp")
  set_kind("binary")
  add_files("main.c", "test.inc")

test.inc : 仅包含注释用于触发变动检测 main.c :

#include "test.h"

int main() {}

附加信息和错误日志

改变test.inc文件后 第一次执行xmake,只执行了cp任务

[ 60%]: cp test.inc
sleep 1
cp test.inc build/.gens/main/macosx/arm64/release/rules/cp/test.h

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 1.014s

第二次执行xmake,才执行编译任务

[ 40%]: cache compiling.release main.c
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -c -Qunused-arguments -target arm64-apple-macos14.5 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk -Ibuild/.gens/main/macosx/arm64/release/rules/cp -o build/.objs/main/macosx/arm64/release/main.c.o main.c
[ 80%]: linking.release main
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -o build/macosx/arm64/release/main build/.objs/main/macosx/arm64/release/main.c.o -target arm64-apple-macos14.5 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk -lz

build cache stats:
cache directory: /Users/codehz/Developer/xmake-bug/build/.build_cache
cache hit rate: 100%
cache hit: 1
cache hit total time: 0.001s
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.056s
Issues-translate-bot commented 2 months ago

Bot detected the issue body's language is not English, translate it automatically.


Title: rule on_buildcmd_file cannot specify the generated header file dependency order

waruqi commented 2 months ago

你这个得用 before_buildcmd_file 而不是 on_buildcmd_file,参考 bin2c rule

https://github.com/xmake-io/xmake/blob/5cdc832913442cedc7625b25eba933b422d4289e/xmake/rules/utils/bin2c/xmake.lua#L30

Issues-translate-bot commented 2 months ago

Bot detected the issue body's language is not English, translate it automatically.


You have to use before_buildcmd_file instead of on_buildcmd_file, refer to bin2c rule

codehz commented 2 months ago

ok,确实是这个问题,没注意到这一点

Issues-translate-bot commented 2 months ago

Bot detected the issue body's language is not English, translate it automatically.


ok, this is indeed the problem, I didn’t notice it