Closed fripSide closed 2 years ago
先确认下 dev 版本是否 ok,xmake update -s dev
感谢回复。dev版本还是有这样问题。感觉是每种架构下的链接参数生成的时候,自动添加开始和末尾的的”-Wl“导致的。
目前用一种很不完美的办法解决了:
# 初始化项目不要用默认的 x86_64而是指定为其他架构
xmake f -p cross -a cortex-m4 --toolchain=arm-gcc -c -vD
设置cross架构为 cortex-m4,就不会自动改加载x86_64的链接参数 -Wl,-x了。cortex-m3/4最后面自动加的是 -s:
/Applications/ARM/bin/arm-none-eabi-ld -o build/cross/cortex-m4/release/mini-stm32 build/.objs/mini-stm32/cross/cortex-m4/release/src/main.c.o -Tsrc/link.ld -s
-s参数对arm-none-eabi-ld 来说没意义,但是不会报错。这样就能成功编译。
一直很想找一个这样简单快速的编译系统,命令上能和makefile一样简洁,速度上和ninja一样。后面等我有时间再写个readme到文档,说下怎么交叉编译iot项目。Keil的后面我可以帮你们重写下,之前写过简单的keil项目生产工具。
在更新到 dev 试试
xmake update -s dev,更新无效:仍然是xmake v2.6.3+202203021208。我试试别的安装方法。不过安装路径在哪里?我找了好久。
另外ccache有bug,连续两次编译,只要触发cccahe之后,编译参数就不对(toolchain的flags没有传递到ccahe):
xmake b -vD
checking for arm-none-eabi-gcc ... /Applications/ARM/bin/arm-none-eabi-gcc
checking for the c compiler (cc) ... arm-none-eabi-gcc
checking for the c compiler (cc) ... arm-none-eabi-gcc
checking for /Applications/ARM/bin/arm-none-eabi-gcc ... ok
checking for flags (-fPIC) ... ok
> arm-none-eabi-gcc "-fPIC"
checking for flags (-O3) ... ok
> arm-none-eabi-gcc "-O3"
checking for flags (-DNDEBUG) ... ok
> arm-none-eabi-gcc "-DNDEBUG" "-fno-common" "-ffreestanding" "-O0" "-gdwarf-2" "-g3" "-Wall" "-Werror" "-mcpu=cortex-m3" "-mthumb" "-nostartfiles"
checking for ccache ... /usr/local/bin/ccache
[ 20%]: ccache compiling.release src/main.c
/usr/local/bin/ccache /Applications/ARM/bin/arm-none-eabi-gcc -c -fno-common -ffreestanding -O0 -gdwarf-2 -g3 -Wall -Werror -mcpu=cortex-m3 -mthumb -nostartfiles -fvisibility=hidden -O3 -DNDEBUG -o build/.objs/minimal-proj/cross/cortex-m4/release/src/main.c.o src/main.c
[ 20%]: ccache compiling.release src/startup.c
/usr/local/bin/ccache /Applications/ARM/bin/arm-none-eabi-gcc -c -fno-common -ffreestanding -O0 -gdwarf-2 -g3 -Wall -Werror -mcpu=cortex-m3 -mthumb -nostartfiles -fvisibility=hidden -O3 -DNDEBUG -o build/.objs/minimal-proj/cross/cortex-m4/release/src/startup.c.o src/startup.c
checking for flags (-MMD -MF) ... ok
> arm-none-eabi-gcc "-MMD" "-MF" "/dev/null" "-fno-common" "-ffreestanding" "-O0" "-gdwarf-2" "-g3" "-Wall" "-Werror" "-mcpu=cortex-m3" "-mthumb" "-nostartfiles"
checking for flags (-fdiagnostics-color=always) ... ok
> arm-none-eabi-gcc "-fdiagnostics-color=always" "-fno-common" "-ffreestanding" "-O0" "-gdwarf-2" "-g3" "-Wall" "-Werror" "-mcpu=cortex-m3" "-mthumb" "-nostartfiles"
checking for arm-none-eabi-ld ... /Applications/ARM/bin/arm-none-eabi-ld
checking for the linker (ld) ... arm-none-eabi-ld
[ 60%]: linking.release minimal-proj
/Applications/ARM/bin/arm-none-eabi-ld -o build/cross/cortex-m4/release/minimal-proj build/.objs/minimal-proj/cross/cortex-m4/release/src/main.c.o build/.objs/minimal-proj/cross/cortex-m4/release/src/startup.c.o -s -Tsrc/main.ld
[100%]: build ok!
$ xmake b -vD
checking for flags (-DNDEBUG) ... ok
> arm-none-eabi-gcc "-DNDEBUG"
[ 20%]: ccache compiling.release src/main.c
/usr/local/bin/ccache /Applications/ARM/bin/arm-none-eabi-gcc -c -fvisibility=hidden -O3 -DNDEBUG -o build/.objs/minimal-proj/cross/cortex-m4/release/src/main.c.o src/main.c
[ 20%]: ccache compiling.release src/startup.c
/usr/local/bin/ccache /Applications/ARM/bin/arm-none-eabi-gcc -c -fvisibility=hidden -O3 -DNDEBUG -o build/.objs/minimal-proj/cross/cortex-m4/release/src/startup.c.o src/startup.c
checking for flags (-MMD -MF) ... ok
> arm-none-eabi-gcc "-MMD" "-MF" "/dev/null"
checking for flags (-fdiagnostics-color=always) ... ok
> arm-none-eabi-gcc "-fdiagnostics-color=always"
[ 60%]: linking.release minimal-proj
/Applications/ARM/bin/arm-none-eabi-ld -o build/cross/cortex-m4/release/minimal-proj build/.objs/minimal-proj/cross/cortex-m4/release/src/main.c.o build/.objs/minimal-proj/cross/cortex-m4/release/src/startup.c.o -s -Tsrc/main.ld
error: @programdir/modules/private/async/runjobs.lua:232: @programdir/actions/build/kinds/binary.lua:74: @programdir/core/sandbox/modules/os.lua:249: /Applications/ARM/bin/arm-none-eabi-ld: build/.objs/minimal-proj/cross/cortex-m4/release/src/startup.c.o: in function `reset_handler':
startup.c:(.text+0x174): undefined reference to `memcpy'
/Applications/ARM/bin/arm-none-eabi-ld: startup.c:(.text+0x19c): undefined reference to `memset'
stack traceback:
[C]: in function 'error'
[@programdir/core/base/os.lua:849]:
[@programdir/core/sandbox/modules/os.lua:249]: in function 'runv'
[@programdir/modules/core/tools/ld.lua:81]:
[C]: in function 'xpcall'
[@programdir/core/base/utils.lua:280]:
[@programdir/actions/build/kinds/binary.lua:74]: in function 'callback'
[@programdir/modules/core/project/depend.lua:189]: in function 'on_changed'
[@programdir/actions/build/kinds/binary.lua:55]: in function '_do_link_target'
[@programdir/actions/build/kinds/binary.lua:102]:
[@programdir/actions/build/kinds/binary.lua:129]: in function '_link_target'
[@programdir/actions/build/kinds/binary.lua:157]: in function 'jobfunc'
[@programdir/modules/private/async/runjobs.lua:208]:
[C]: in function 'xpcall'
[@programdir/core/base/utils.lua:280]: in function 'trycall'
[@programdir/core/sandbox/modules/try.lua:121]: in function 'try'
[@programdir/modules/private/async/runjobs.lua:200]: in function 'cotask'
[@programdir/core/base/scheduler.lua:371]:
stack traceback:
[C]: in function 'error'
@programdir/core/base/os.lua:849: in function 'os.raiselevel'
(...tail calls...)
@programdir/modules/private/async/runjobs.lua:232: in field 'catch'
@programdir/core/sandbox/modules/try.lua:127: in global 'try'
@programdir/modules/private/async/runjobs.lua:200: in upvalue 'cotask'
@programdir/core/base/scheduler.lua:371: in function <@programdir/core/base/scheduler.lua:368>
正确参数
/usr/local/bin/ccache /Applications/ARM/bin/arm-none-eabi-gcc -c -fno-common -ffreestanding -O0 -gdwarf-2 -g3 -Wall -Werror -mcpu=cortex-m3 -mthumb -nostartfiles -fvisibility=hidden -O3 -DNDEBUG -o build/.objs/minimal-proj/cross/cortex-m4/release/src/startup.c.o src/startup.c
错误参数:
ccache compiling.release src/startup.c
/usr/local/bin/ccache /Applications/ARM/bin/arm-none-eabi-gcc -c -fvisibility=hidden -O3 -DNDEBUG -o build/.objs/minimal-proj/cross/cortex-m4/release/src/startup.c.o src/startup.c
不要在toolchian添加任何编译参数,直接在target哪里添加,这样就不会触发不正确的ccache:
target("minimal-proj")
set_kind("binary")
add_files("src/*.c")
add_cxflags("-fno-common", "-ffreestanding", "-O0", "-gdwarf-2",
"-g3", "-Wall", "-Werror",
"-mcpu=cortex-m3", "-mthumb", "-nostartfiles")
add_ldflags("-Tsrc/main.ld", {force=true})
另外,cross= cortex-m4,下toolchain的ldflags添加无效,需要去下面src哪里添加:
toolchain("arm-gcc")
local tool_path = "/Applications/ARM/"
set_kind("cross")
set_description("Stm32 Arm Embedded Compiler")
set_sdkdir(tool_path)
set_toolset("cc", "arm-none-eabi-gcc")
-- set_toolset("cxx", "arm-none-eabi-g++")
set_toolset("ld", "arm-none-eabi-ld")
on_check(function (toolchain)
toolchain:add("cxflags", "-fno-common", "-ffreestanding", "-O0", "-gdwarf-2",
"-g3", "-Wall", "-Werror",
"-mcpu=cortex-m3", "-mthumb", "-nostartfiles")
toolchain:add("ldflags", "-Tsrc/main.ld", {force=true})
toolchain:configs_save()
return true
end)
toolchain_end()
set_toolchains("arm-gcc")
target("minimal-proj")
set_kind("binary")
add_files("src/*.c")
add_ldflags("-Tsrc/main.ld", {force=true})
这个是不起作用的:
toolchain:add("ldflags", "-Tsrc/main.ld", {force=true})
需要在下面单独添加:
add_ldflags("-Tsrc/main.ld", {force=true})
只要把flags这一块,不添加任何默认操作,取toolchain和target的并集:脚本flags = toolchain(flag) U target(flag)。并且提供API reset flags,让在target中能够clear所有的flags,应该就能满足几乎所有的需求。我就能去编译大型RTOS。
xmake update -s dev,更新无效:仍然是xmake v2.6.3+202203021208。我试试别的安装方法。不过安装路径在哪里?我找了好久。
-s 仅更新脚本,core 版本不会变。先确认是否生效,不行,就再贴 -vD 的完整输出和错误。
其他问题,单开 issues,不要混着问
使用 xmake update dev, 更新到dev版本:
xmake v2.6.3+dev.bcf29f1
:beers: 新版本没问题,不会再自动增加链接(-Wl, -x)参数(但还是有-s)。另外: toolchain:add("ldflags") 和toolchain:add("cxflags")不生效是我配置写错了,写到on_check里面了,应该放到on_load里面。这是能正确运行的xmake脚本:
add_rules("mode.debug", "mode.release")
set_plat("cross")
toolchain("arm-gcc")
local tool_path = "/Applications/ARM/"
set_kind("cross")
set_description("Stm32 Arm Embedded Compiler")
set_sdkdir(tool_path)
set_toolset("cc", "arm-none-eabi-gcc")
-- set_toolset("cxx", "arm-none-eabi-g++")
set_toolset("ld", "arm-none-eabi-ld")
on_load(function (toolchain)
toolchain:add("cxflags", "-fno-common", "-ffreestanding", "-O0", "-gdwarf-2",
"-g3", "-Wall", "-Werror",
"-mcpu=cortex-m3", "-mthumb", "-nostartfiles")
toolchain:add("ldflags", "-Tsrc/main.ld", {force=true})
end)
toolchain_end()
set_toolchains("arm-gcc")
target("minimal-proj")
set_kind("binary")
add_files("src/*.c")
after_build(function (target)
print("after_build")
local out = target:targetfile() or ""
local bin_out = " build/minimal-proj.bin"
print(string.format("%s => %s", out, bin_out))
os.exec("arm-none-eabi-objcopy -Obinary "..out.." "..bin_out)
os.exec("qemu-system-arm -M stm32-p103 -nographic -kernel"..bin_out)
end)
build项目:
xmake f --toolchain=arm-gcc -c
xmake b -vD
@waruqi 非常感谢你的帮助,这个issue可以关闭了。后面我试试用这个工具完全代替Cmake。
描述问题
我想通过自定义Toolchain来把大型IoT系统的嵌入式Cmake/Keil代码项目(FreeRTOS, Zephyr, LiteOS等)转成xmake。
在这之前,我先用最简单的项目进行测试,但是链接的时候出现了问题,无法正确的传入链接参数。我使用的项目如下: https://github.com/samvrlewis/minimal-stm32/blob/master/Makefile
可以看出来只需要两条命令就能编译成功:
因此我使用自定义的toolchian:
期待的结果
前面的gcc命令执行正常,到了link的时候就多了参数:“-Wl, -x”。希望ldflags能自己clear或者完全自己设置。
正常编译命令:
错误信息
很明显,是ld的时候后面多了“ -Wl,-x“ 参数,这个参数不是我自己配置的。而arm-none-eabi-ld不支持这两个参数。如果没有这两个参数就能编译成功。
相关环境
请提供编译和运行环境信息,下面是一些必须填写的基础信息,便于我们针对性排查问题:
其他信息
如果想支持嵌入式编译,可以把设置cc以及flag的命令都开放给我们就行了。下面的keil有点封装的过度了: https://github.com/xmake-io/xmake/blob/master/xmake/toolchains/armcc/xmake.lua
另外希望cross编译,自定义toolchian的时候,不要进行默认编译器check(让开发者自定义就行),例如:fPIC在arm-none-eabi-gcc 下没意义。这些check可能无法通过编译器检查。