xmake-io / xmake

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

XMake链接参数生成错误 #5524

Open heckerpowered opened 3 months ago

heckerpowered commented 3 months ago

Xmake 版本

xmake v2.9.4+20240729

操作系统版本和架构

macOS Sequoia 15.0

描述问题

XMake生成了错误的链接参数

add_ldflags("$(shell " .. llvm_config .. " --ldflags)")

命令llvm-config生成如下:

-L/opt/homebrew/Cellar/llvm/18.1.8/lib -Wl,-search_paths_first -Wl,-headerpad_max_install_name

xmake生成的链接命令如下:

/opt/homebrew/Cellar/llvm/18.1.8/bin/clang++ -o build/macosx/arm64/release/BugTest build/.objs/BugTest/macosx/arm64/release/src/main.cpp.o -isysroot /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.0.sdk -Wl,-x -Wl,-dead_strip -l -L/opt/homebrew/Cellar/llvm/18.1.8/lib -Wl,-search_paths_first -Wl,-headerpad_max_install_names -lLLVM-18

错误信息:

ld: library '-L/opt/homebrew/Cellar/llvm/18.1.8/lib' not found
clang++: error: linker command failed with exit code 1 (use -v to see invocation)

我单方面认为是因为XMake链接参数生成错误

期待的结果

生成正确的链接选项,如

/opt/homebrew/Cellar/llvm/18.1.8/bin/clang++ -o build/macosx/arm64/release/BugTest build/.objs/BugTest/macosx/arm64/release/src/main.cpp.o -isysroot /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.0.sdk -Wl,-x -Wl,-dead_strip -L/opt/homebrew/Cellar/llvm/18.1.8/lib -Wl,-search_paths_first -Wl,-headerpad_max_install_names -lLLVM-18

工程配置

add_rules("mode.debug", "mode.release") add_rules("plugin.compile_commands.autoupdate")

local llvm_config = "/opt/homebrew/opt/llvm/bin/llvm-config"

target("BugTest") set_kind("binary") add_files("src/*.cpp") set_toolchains("llvm")

add_ldflags("$(shell " .. llvm_config .. " --ldflags)")
add_ldflags("$(shell " .. llvm_config .. " --libs all)", {force = true})
add_syslinks("$(shell " .. llvm_config .. " --system-libs)")

附加信息和错误日志

hecker@hecker BugTest % xmake -vD
[ 75%]: linking.release BugTest
/opt/homebrew/Cellar/llvm/18.1.8/bin/clang++ -o build/macosx/arm64/release/BugTest build/.objs/BugTest/macosx/arm64/release/src/main.cpp.o -isysroot /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.0.sdk -Wl,-x -Wl,-dead_strip -l -L/opt/homebrew/Cellar/llvm/18.1.8/lib -Wl,-search_paths_first -Wl,-headerpad_max_install_names -lLLVM-18
ld: library '-L/opt/homebrew/Cellar/llvm/18.1.8/lib' not found
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
error: @programdir/core/main.lua:329: @programdir/actions/build/main.lua:148: @programdir/modules/async/runjobs.lua:322: @programdir/actions/build/kinds/binary.lua:53: @programdir/core/sandbox/modules/os.lua:378: execv(/opt/homebrew/Cellar/llvm/18.1.8/bin/clang++ -o build/macosx/arm64/release/BugTest build/.objs/BugTest/macosx/arm64/release/src/main.cpp.o -isysroot /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.0.sdk -Wl,-x -Wl,-dead_strip -l -L/opt/homebrew/Cellar/llvm/18.1.8/lib -Wl,-search_paths_first -Wl,-headerpad_max_install_names -lLLVM-18) failed(1)
stack traceback:
    [C]: in function 'error'
    [@programdir/core/base/os.lua:973]:
    [@programdir/core/sandbox/modules/os.lua:378]: in function 'execv'
    [@programdir/modules/core/tools/gcc.lua:580]:
    [C]: in function 'xpcall'
    [@programdir/core/base/utils.lua:275]:
    [@programdir/core/tool/linker.lua:221]: in function 'link'
    [@programdir/actions/build/kinds/binary.lua:53]: in function 'callback'
    [@programdir/modules/core/project/depend.lua:217]: in function 'on_changed'
    [@programdir/actions/build/kinds/binary.lua:41]: in function '_do_link_target'
    [@programdir/actions/build/kinds/binary.lua:83]:
    [@programdir/actions/build/kinds/binary.lua:110]: in function '_link_target'
    [@programdir/actions/build/kinds/binary.lua:138]: in function 'jobfunc'
    [@programdir/modules/async/runjobs.lua:238]:
    [C]: in function 'xpcall'
    [@programdir/core/base/utils.lua:275]: 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:406]:

stack traceback:
        [C]: in function 'error'
        @programdir/core/base/os.lua:973: in function 'base/os.raiselevel'
        (...tail calls...)
        @programdir/core/main.lua:329: in upvalue 'cotask'
        @programdir/core/base/scheduler.lua:406: in function <@programdir/core/base/scheduler.lua:399>
Issues-translate-bot commented 3 months ago

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


Title: XMake link parameter generation error

heckerpowered commented 3 months ago
    add_ldflags("$(shell " .. llvm_config .. " --ldflags)")
    add_ldflags("$(shell " .. llvm_config .. " --libs all)", {force=true})
    -- add_syslinks("$(shell " .. llvm_config .. " --system-libs)")

使用以下配置可以在我的平台上通过编译,测试发现llvm-config --system-libs在我的机器上会生成空串,但我必须要将其注释掉。 其次,llvm-config --libs all生成-lLLVM18,但xmake认为其是无效的,默认将其忽略,我必须使用force = true 做出以上两点修改后可以在我的机器上正常构建,但测试发现Windows机器并不需要做出这两点修改也可以正常构建,我单方面认为这是XMake的问题

Issues-translate-bot commented 3 months ago

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


add_ldflags("$(shell " .. llvm_config .. " --ldflags)")
add_ldflags("$(shell " .. llvm_config .. " --libs all)", {force=true})
-- add_syslinks("$(shell " .. llvm_config .. " --system-libs)")

Using the following configuration can compile on my platform. Testing found that llvm-config --system-libs will generate an empty string on my machine, but I have to comment it out. Secondly, llvm-config --libs all generates -lLLVM18, but xmake thinks it is invalid and ignores it by default, I have to use force=true After making the above two modifications, it can be built normally on my machine. However, the test found that the Windows machine does not need to make these two modifications to build normally. I unilaterally think this is a problem with XMake.

waruqi commented 3 months ago

其次,llvm-config --libs all生成-lLLVM18,但xmake认为其是无效的,默认将其忽略,我必须使用force = true

add_ldflags 默认会自动检测,不保证一定检测成功,根据warnings 提示加 force 就行了。。正常。

waruqi commented 3 months ago

不建议走 shell 这么玩,走 package 集成,也能找系统库。

https://github.com/xmake-io/xmake-repo/blob/dev/packages/l/llvm/fetch.lua

Issues-translate-bot commented 3 months ago

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


It is not recommended to use shell to do this. You can also use package integration to find system libraries.

https://github.com/xmake-io/xmake-repo/blob/dev/packages/l/llvm/fetch.lua

star-hengxing commented 3 months ago

https://discord.com/channels/785173387962089492/1037914914477592716/1278755928589602847

package("libllvm")
do
    on_install(function (package)
        import("lib.detect.find_tool")
        local llvm_config = find_tool("llvm-config")

        local cflags_raw    = os.iorunv(llvm_config.program, { "--cflags" })
        local cppflags_raw  = os.iorunv(llvm_config.program, { "--cppflags" })
        local cxxflags_raw  = os.iorunv(llvm_config.program, { "--cxxflags" })
        local ldflags_raw   = os.iorunv(llvm_config.program, { "--ldflags" })

        local cflags = cflags_raw:split("%s+") or {}
        local cppflags = cppflags_raw:split("%s+") or {}
        local cxxflags = cxxflags_raw:split("%s+") or {}
        local ldflags = ldflags_raw:split("%s+") or {}

        package:add("cflags", cflags)
        package:add("cxflags", cppflags)
        package:add("cxxflags", cxxflags)
        package:add("ldflags", ldflags)
    end)
end
package_end()

add_requires("libllvm")