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

Unable to cross-compile for a Linux platform with the buildbot scripts #78960

Open
xtremekforever opened this issue Jan 27, 2025 · 1 comment
Labels
armv7 Architecture: ARMv7 bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. build-script Area → utils: The build script cross-compilation Area → utils: Cross-compilation of project sources

Comments

@xtremekforever
Copy link
Contributor

xtremekforever commented Jan 27, 2025

Description

In order to build Swift for platforms such as armv6, armv7, or even less-powerful arm64 architectures, the ideal way to build is to cross-compile instead of trying to build the Swift toolchain directly on the target, which is very slow.

However, currently the build scripts in the swiftlang/swift repo do not properly support cross compilation for Linux architectures. For example, when trying to build for armv7 and an external sysroot, the first build steps completely fail to build for the desired architecture. As an example, here is the failure from cmark:

CMake Error at /usr/share/cmake-3.28/Modules/CMakeTestCCompiler.cmake:67 (message):
  The C compiler

    "~/.local/share/swiftly/bin/clang"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: '~/swift-main-build/build/Ninja-DebugAssert/cmark-linux-armv7/CMakeFiles/CMakeScratch/TryCompile-igXlDK'
    
    Run Build Command(s): /usr/bin/ninja -v cmTC_8dc74
    [1/2][ 50%][0.022s] ~/.local/share/swiftly/bin/clang --target=arm-unknown-linux-gnueabihf --sysroot=/usr/arm-linux-gnueabihf   -Wno-unknown-warning-option -Werror=unguarded-availability-new -MD -MT CMakeFiles/cmTC_8dc74.dir/testCCompiler.c.o -MF CMakeFiles/cmTC_8dc74.dir/testCCompiler.c.o.d -o CMakeFiles/cmTC_8dc74.dir/testCCompiler.c.o -c ~/swift-main-build/build/Ninja-DebugAssert/cmark-linux-armv7/CMakeFiles/CMakeScratch/TryCompile-igXlDK/testCCompiler.c
    [2/2][100%][0.039s] : && ~/.local/share/swiftly/bin/clang --target=arm-unknown-linux-gnueabihf --sysroot=/usr/arm-linux-gnueabihf -Wno-unknown-warning-option -Werror=unguarded-availability-new  CMakeFiles/cmTC_8dc74.dir/testCCompiler.c.o -o cmTC_8dc74   && :
    FAILED: cmTC_8dc74 
    : && ~/.local/share/swiftly/bin/clang --target=arm-unknown-linux-gnueabihf --sysroot=/usr/arm-linux-gnueabihf -Wno-unknown-warning-option -Werror=unguarded-availability-new  CMakeFiles/cmTC_8dc74.dir/testCCompiler.c.o -o cmTC_8dc74   && :
    /usr/bin/ld.gold: fatal error: /usr/arm-linux-gnueabihf/lib/../lib/Scrt1.o: unsupported ELF machine number 40
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    ninja: build stopped: subcommand failed.

Reproduction

To reproduce the first issue with cmark, run the following command in Linux/Ubuntu:

./swift/utils/build-script --cross-compile-hosts=linux-armv7 --build-llvm=0

Otherwise, run it without --build-llvm=0 to see other crashes that occur after LLVM is built for the host:

./swift/utils/build-script --cross-compile-hosts=linux-armv7

Basically, every step of compilation will fail without being patched.

Expected behavior

I would expect the above commands to succeed as long as the armv7 (armhf) libraries are installed in the host using something like:

apt install gcc-arm-linux-gnueabihf

Cross compilation should also work if using an external sysroot, like this:

./swift/utils/build-script --cross-compile-hosts=linux-armv7 --cross-compile-deps-path ./sysroot-debian-bookworm --cross-compile-append-host-target-to-destdir=False

Environment

Swift version 6.0.3 (swift-6.0.3-RELEASE)
Target: x86_64-unknown-linux-gnu

Additional information

I intend to look at updating the scripts to properly cross compile to linux- hosts. If this is not the proper path, please let me know, but this seems to be the most logical thing to use since --cross-compile-hosts can already cross-compile to other Apple platforms.

Here are the short terms goals of being able to cross compile using the build-script. Let's get the build scripts updated to make it possible to cross compile the following components to linux-armv7:

  • Swift stdlib (which is libdispatch + stdlib)
  • libdispatch
  • foundation
  • xctest
  • swift-testing

This is the "minimal installation" that is needed for something like a cross-compilation Swift SDK (swift-sdk-generator).

@xtremekforever xtremekforever added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Jan 27, 2025
@MaxDesiatov MaxDesiatov added build-script Area → utils: The build script armhf Architecture: the Debian port for ARM processors (armv7+) that have hardware floating point support cross-compilation Area → utils: Cross-compilation of project sources armv7 Architecture: ARMv7 and removed triage needed This issue needs more specific labels armhf Architecture: the Debian port for ARM processors (armv7+) that have hardware floating point support labels Jan 28, 2025
xtremekforever added a commit to xtremekforever/swift that referenced this issue Jan 28, 2025
@xtremekforever
Copy link
Contributor Author

Example build script for minimal stdlib + core libraries:

./swift/utils/build-script \
    --cross-compile-hosts=linux-armv7 --cross-compile-sysroots=/path/to/sysroot \
   # these are taken from the stdlib_base_standalone preset from build-presets.ini
    --build-llvm=0 --skip-build-cmark --skip-build-benchmarks \
   --skip-test-cmark --build-swift-tools=0 --build-swift-libexec=0 \
   --skip-early-swift-driver --skip-early-swiftsyntax --build-embedded-stdlib=0 \
   --native-swift-tools-path=$SWIFT_NATIVE_PATH --native-llvm-tools-path=$SWIFT_NATIVE_PATH --native-clang-tools-path=$SWIFT_NATIVE_PATH \
   # we want to install swift, libdispatch, foundation, and xctest
   --install-swift --libdispatch --install-libdispatch --foundation --install-foundation --xctest --install-xctest \
   # everything gets install to the destdir of course
   --install-destdir=swift-armv7-install

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
armv7 Architecture: ARMv7 bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. build-script Area → utils: The build script cross-compilation Area → utils: Cross-compilation of project sources
Projects
None yet
Development

No branches or pull requests

2 participants