xmake icon indicating copy to clipboard operation
xmake copied to clipboard

Fix Cross Compilation for Windows

Open mccakit opened this issue 8 months ago • 6 comments

Xmake 版本

2.9.9

操作系统版本和架构

Windows 10

描述问题

Cross compilation doesn't works for windows host.

期待的结果

Cross compilation should work for windows

工程配置

package("glfw")
    add_deps("cmake")
    set_sourcedir("glfw-3.4")
    on_install(function (package)
        local configs = {}
        import("package.tools.cmake")
        cmake.install(package, configs)
    end)
package_end()
add_requires("glfw")

target("glad")
    set_kind("static")
    add_files("glad/src/*.c")
    add_headerfiles("glad/include/glad/*.h")
    add_headerfiles("glad/include/KHR/*.h")
    add_includedirs("glad/include",{public=true})

target("hi")
    set_kind("binary")
    add_files("src/*.cpp")
    add_deps("glad")
    add_packages("glfw")
    if is_plat("windows") then
        add_syslinks("opengl32", "gdi32", "user32", "shell32")
    elseif is_plat("linux") then
        add_syslinks("GL", "m", "dl", "X11", "pthread")
    end
xmake config --toolchain=llvm --plat=linux --arch=x86_64 --sdk=C:\devtools\llvm\

This fails, so do this

xmake config --toolchain=llvm --plat=cross --cross=x86_64-pc-linux-gnu

and this

xmake config --toolchain=llvm --plat=cross `
--cross=x86_64-pc-linux-gnu `
--sdk=C:/devtools/llvm `
--cxflags=C:/devtools/sysroots/deb `
--ldflags=C:/devtools/sysroots/deb

and this

xmake config --toolchain=llvm --plat=linux --arch=x86_64 --sdk=C:\devtools\llvm\ --runtimes=stdc++_shared

附加信息和错误日志

I could write a lot, but the main thing is standard library cannot be found anad pacakge install fails due to not finding os specifics stuff like pthread

mccakit avatar May 17 '25 11:05 mccakit

you can add -vD to see verbose logs and debug it.

https://github.com/xmake-io/xmake/blob/2b88ba37fa0a9957b8ab24cf1bf6da59e2c39f21/xmake/toolchains/llvm/xmake.lua#L66

or try and wait this patch. https://github.com/xmake-io/xmake/pull/6380

waruqi avatar May 17 '25 14:05 waruqi

Is there anything I can do to help resolve this issue more quickly? I'd like to move on to new challenges and experiments, but I feel stuck.

mccakit avatar May 17 '25 18:05 mccakit

I have improved it. https://github.com/xmake-io/xmake/pull/6471

xmake f -p cross --cross=x86_64-pc-linux-gnu --toolchain=llvm --sdk=C:\xxx\llvm -cvD

but std header has been not supported yet

waruqi avatar May 21 '25 01:05 waruqi

This example still doesn't compile hi.zip

with this

xmake f -p cross --cross=x86_64-pc-linux-gnu --toolchain=llvm --sdk=C:\dev\sysroots\deb2 -cvD

output.txt

I'm on the dev branch as can be seen here

xmake --version
xmake v3.0.0+dev.bfc0f75db, A cross-platform build utility based on Lua

Also custom toolchains don't work for cmake packages,

hi2.zip

I can cross compile with custom toolchain without third party packages

I assume these:

  • When cross compiling llvm doesn't use lld and -fuse-ld=lld, which are necessary when compiling by hand
  • CMake package install doesn't work with cross compilation. It doesn't use the cross compiler toolchain, it defaults to msvc

What probably needs to be done:

  • Llvm toolchain linker should be changed to lld and -fuse-ld=lld should be default.
  • SDK directory should be passed into cmake package as sysroot
  • We should be able to specify toolchains in packages, so that cmake would use clang

mccakit avatar May 21 '25 22:05 mccakit

I have improved it. #6471

xmake f -p cross --cross=x86_64-pc-linux-gnu --toolchain=llvm --sdk=C:\xxx\llvm -cvD

but std header has been not supported yet

But llvm toolchain with sdk works now in native compilation, so big thanks. Previously I had to use clang without providing sdk path in the past

mccakit avatar May 22 '25 08:05 mccakit

@waruqi full example here

https://github.com/mccakit/Bug-Report

mccakit avatar May 22 '25 19:05 mccakit