Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

asn1c 内置规则有误 #4439

Closed
guyutongxue opened this issue Nov 28, 2023 · 3 comments
Closed

asn1c 内置规则有误 #4439

guyutongxue opened this issue Nov 28, 2023 · 3 comments
Labels
Milestone

Comments

@guyutongxue
Copy link
Contributor

Xmake 版本

2.8.5

操作系统版本和架构

Ubuntu 23.10

描述问题

tests/projects/c/asn1c 的样例项目无法正确构建。执行 xmake 后,报错:

error: /usr/bin/ld: build/.objs/test/linux/x86_64/release/src/main.c.o: warning: relocation against `asn_DEF_Rectangle' in read-only section `.text.startup'
/usr/bin/ld: build/.objs/test/linux/x86_64/release/src/main.c.o: in function `main':
main.c:(.text.startup+0x77): undefined reference to `asn_DEF_Rectangle'
/usr/bin/ld: main.c:(.text.startup+0x8f): undefined reference to `der_encode'
/usr/bin/ld: main.c:(.text.startup+0xd2): undefined reference to `xer_fprint'
/usr/bin/ld: main.c:(.text.startup+0x110): undefined reference to `asn_DEF_Rectangle'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status

期待的结果

正确编译并链接

工程配置

tests/projects/c/asn1c 相同。

附加信息和错误日志

通过 -vD 参数可发现,在链接程序时未找到 asn1c 生成的 .c 的编译对象文件。


[ 60%]: linking.release test
/usr/bin/g++ -o build/linux/x86_64/release/test build/.objs/test/linux/x86_64/release/src/main.c.o -m64 -s
/usr/bin/ld: build/.objs/test/linux/x86_64/release/src/main.c.o: warning: relocation against `asn_DEF_Rectangle' in read-only section `.text.startup'
/usr/bin/ld: build/.objs/test/linux/x86_64/release/src/main.c.o: in function `main':
main.c:(.text.startup+0x77): undefined reference to `asn_DEF_Rectangle'
/usr/bin/ld: main.c:(.text.startup+0x8f): undefined reference to `der_encode'
/usr/bin/ld: main.c:(.text.startup+0xd2): undefined reference to `xer_fprint'
/usr/bin/ld: main.c:(.text.startup+0x110): undefined reference to `asn_DEF_Rectangle'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status
error: @programdir/core/main.lua:309: @programdir/actions/build/main.lua:148: @programdir/modules/async/runjobs.lua:320: @programdir/actions/build/kinds/binary.lua:74: @programdir/core/sandbox/modules/os.lua:378: execv(/usr/bin/g++ -o build/linux/x86_64/release/test build/.objs/test/linux/x86_64/release/src/main.c.o -m64 -s) failed(1)
stack traceback:
    [C]: in function 'error'
    [@programdir/core/base/os.lua:923]:
    [@programdir/core/sandbox/modules/os.lua:378]: in function 'execv'
    [@programdir/modules/core/tools/gcc.lua:526]:
    [C]: in function 'xpcall'
    [@programdir/core/base/utils.lua:280]:
    [@programdir/core/tool/linker.lua:224]: in function 'link'
    [@programdir/actions/build/kinds/binary.lua:74]: in function 'callback'
    [@programdir/modules/core/project/depend.lua:217]: in function 'on_changed'
    [@programdir/actions/build/kinds/binary.lua:55]: in function '_do_link_target'
    [@programdir/actions/build/kinds/binary.lua:105]:
    [@programdir/actions/build/kinds/binary.lua:132]: in function '_link_target'
    [@programdir/actions/build/kinds/binary.lua:160]: in function 'jobfunc'
    [@programdir/modules/async/runjobs.lua:237]:
    [C]: in function 'xpcall'
    [@programdir/core/base/utils.lua:280]: in function 'trycall'
    [@programdir/core/sandbox/modules/try.lua:117]: in function 'try'
    [@programdir/modules/async/runjobs.lua:220]: in function 'cotask'
    [@programdir/core/base/scheduler.lua:404]:

stack traceback:
        [C]: in function 'error'
        @programdir/core/base/os.lua:923: in function 'base/os.raiselevel'
        (...tail calls...)
        @programdir/core/main.lua:309: in upvalue 'cotask'
        @programdir/core/base/scheduler.lua:404: in function <@programdir/core/base/scheduler.lua:397>

进一步跟进 xmake 源码 asn1c 规则的定义,可注意到问题似乎出现在如下代码:

batchcmds:vrunv(asn1c.program, {path(sourcefile_asn1):absolute()}, {curdir = sourcefile_dir})
-- compile *.c
for _, sourcefile in ipairs(os.files(path.join(sourcefile_dir, "*.c|converter-*.c"))) do
local objectfile = target:objectfile(sourcefile)
batchcmds:compile(sourcefile, objectfile, {configs = {includedirs = sourcefile_dir}})
table.insert(target:objectfiles(), objectfile)
end

其中第 33 行向批处理代码添加对 asn1c 的执行命令(以从 ASN.1 定义生成 C 文件),但是该命令在此时刻并未实际执行,目标路径甚至还可能不存在。随后第 36 行直接遍历目标路径下的 .c 文件,这在首次编译以及 xmake clean -a 之后会得到空的文件集合,并未成功修改 target:objectfiles()

一种可能的修改方法:将遍历 .c 文件并添加 objectfiles 的逻辑移动到 on_buildcmd_file,即:

diff --git a/temp/a.lua b/temp/b.lua
index 5682306..0f651b4 100644
--- a/temp/a.lua
+++ b/temp/b.lua
@@ -32,6 +32,12 @@ rule("asn1c")
         batchcmds:mkdir(sourcefile_dir)
         batchcmds:vrunv(asn1c.program, {path(sourcefile_asn1):absolute()}, {curdir = sourcefile_dir})
 
+        -- add includedirs
+        target:add("includedirs", sourcefile_dir)
+    end)
+    on_buildcmd_file(function (target, batchcmds, sourcefile_asn1, opt)
+        local sourcefile_dir = path.join(target:autogendir(), "rules", "asn1c")
+
         -- compile *.c
         for _, sourcefile in ipairs(os.files(path.join(sourcefile_dir, "*.c|converter-*.c"))) do
             local objectfile = target:objectfile(sourcefile)
@@ -39,9 +45,6 @@ rule("asn1c")
             table.insert(target:objectfiles(), objectfile)
         end
 
-        -- add includedirs
-        target:add("includedirs", sourcefile_dir)
-
         -- add deps
         batchcmds:add_depfiles(sourcefile_asn1)
         batchcmds:set_depcache(target:dependfile(sourcefile_asn1))

如果这看上去是合理的,我可以提交一份 PR。

@Issues-translate-bot
Copy link

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


Title: asn1c The built-in rule is wrong

@waruqi
Copy link
Member

waruqi commented Nov 28, 2023

嗯 你可以来个 pr

@Issues-translate-bot
Copy link

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


Well, you can do a PR

@waruqi waruqi closed this as completed Nov 29, 2023
@waruqi waruqi added this to the v2.8.6 milestone Nov 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants