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

toolchain.cmake: properly set toolchain for crossbuilding on Alpine Linux #11608

Merged
merged 3 commits into from
Apr 27, 2023

Conversation

ayakael
Copy link
Contributor

@ayakael ayakael commented Nov 13, 2022

Port changes from dotnet/runtime#76500

Changes allow for proper toolchain detection on Alpine Linux.

/cc: @am11

@am11
Copy link
Member

am11 commented Nov 13, 2022

Sync changes from dotnet/runtime#76500

It was reverted dotnet/runtime#76500 (comment)?

@ayakael
Copy link
Contributor Author

ayakael commented Nov 13, 2022

Sync changes from dotnet/runtime#76500

It was reverted dotnet/runtime#76500 (comment)?

Right, it was. Wrong word used.

@ayakael
Copy link
Contributor Author

ayakael commented Nov 13, 2022

@am11 You had also mentionned that built-rootfs.sh should need to be tested for Alpine. Are there any other tests I need to do to confirm that these are fine? Given all of these being if-statements depending on existence of Alpine toolchain on crossrootfs, it's pretty low risk. The other part is having the x86 crosscompile settings move away from -m32 mode as Alpine doesn't support that very well.

@am11
Copy link
Member

am11 commented Nov 13, 2022

Yes, to keep things in sync, you can add support in build-rootfs.sh at the same time when adding support for new platform in toolchain.cmake.

For testing, I use this sort of script:

cd runtime
# make changes in build-rootfs.sh, toolchain.cmake and tryrun.cmake

# cleanup
rm -rf artifacts .tools

# replace <arch> with the arch to test
eng/common/cross/build-rootfs.sh <arch> alpine
ROOTFS_DIR=$(pwd)/.tools/rootfs/<arch> ./build.sh -cross -a <arch> -c Debug
ROOTFS_DIR=$(pwd)/.tools/rootfs/<arch> ./build.sh -cross -a <arch> -c Release

for each architecture.

Optional: if you want to add support in runtime's extra-platforms CI pipeline (which is for community supported platforms), you can send a PR to https:/dotnet/dotnet-buildtools-prereqs-docker/ (similar to dotnet/dotnet-buildtools-prereqs-docker@5291f63).

@akoeplinger
Copy link
Member

Please ping me once this is ready and I can merge it

@ayakael
Copy link
Contributor Author

ayakael commented Feb 25, 2023

@am11 Did you test these changes on Ubuntu? For all intents and purposes, this works for Alpine. I unfortunately don't have a Ubuntu crossbuilding environment.

@am11
Copy link
Member

am11 commented Feb 25, 2023

You can use a prebuilt image from https:/dotnet/versions/blob/main/build-info/docker/image-info.dotnet-dotnet-buildtools-prereqs-docker-production.json that has all the prereqs installed:

$ git clone https:/dotnet/runtime --single-branch --depth 1
$ cd runtime
# apply these changes in runtime repo
$ curl -sSL https://raw.githubusercontent.com/dotnet/arcade/b17639c6b999611cf92c01f0153b3fb3d9340e6e/eng/common/cross/toolchain.cmake -o eng/common/cross/toolchain.cmake

$ docker run -v$(pwd):/runtime --rm -it \
    mcr.microsoft.com/dotnet-buildtools/prereqs:ubuntu-22.04-cross-arm64-alpine \
    /bin/bash

# inside the container; use steps from https:/dotnet/arcade/pull/11608#issuecomment-1312763905

@am11
Copy link
Member

am11 commented Mar 15, 2023

I ran the builds locally and changes seems to be working. Lets run a detailed test once dotnet/dotnet-buildtools-prereqs-docker#823 is in and we get the container images out of common infra (linux-musl-riscv64 image was built earlier).

@ayakael, could you please open a downstream PR in dotnet/runtime with these changes, if CI is happy we can merge these changes with confidence.

@@ -291,6 +310,9 @@ if(TARGET_ARCH_NAME MATCHES "^(arm|armel)$")
add_compile_options(-mfloat-abi=softfp)
endif()
elseif(TARGET_ARCH_NAME STREQUAL "x86")
if(EXISTS ${CROSS_ROOTFS}/usr/lib/gcc/i586-alpine-linux-musl)
add_compile_options(--target=${TOOLCHAIN})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why x86 alpine needs this --target option while other architectures don't

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question, and only used for x86 architecture. 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this, crossbuilding to x86 omits setting a target option. I presume it's because x86 is usually crosscompiled using -m32, thus doesn't require an alternate toolchain. With Alpine, I hadn't had success crossbuilding in that mode due to, I assume, not all libraries having a 32-bit version. I havn't found the particular if-statement that's to blame for omitting it

Copy link
Member

@am11 am11 Mar 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, you are right. @janvorli, without this target, build fails with linux-musl-x86 prereq image as well:

$ cd runtime
# apply patch: https:/dotnet/arcade/pull/11608 (and remove `--target` lines)

$ docker run -v$(pwd):/runtime -e ROOTFS_DIR=/crossrootfs/x86 --rm \
    mcr.microsoft.com/dotnet-buildtools/prereqs:ubuntu-22.04-cross-x86-alpine \
    /runtime/build.sh clr+host+libs -a x86 -cross

[...]
  Commencing build of "install" target in "CoreCLR component" for linux.x86.Debug in /runtime/artifacts/obj/coreclr/linux.x86.Debug
  Invoking "/runtime/eng/native/gen-buildsys.sh" "/runtime/src/coreclr" "/runtime/artifacts/obj/coreclr/linux.x86.Debug" x86 linux clang Debug ""  -DCLR_CMAKE_PGO_INSTRUMENT=0 -DCLR_CMAKE_OPTDATA_PATH= -DCLR_CMAKE_PGO_OPTIMIZE=0 -DCLI_CMAKE_FALLBACK_OS="linux-musl" -DFEATURE_DISTRO_AGNOSTIC_SSL=1 
  /runtime/artifacts/obj/coreclr/linux.x86.Debug /runtime/src/coreclr
  Not searching for unused variables given on the command line.
  loading initial cache file /runtime/eng/native/tryrun.cmake
  -- The C compiler identification is Clang 15.0.7
  -- The CXX compiler identification is Clang 15.0.7
  -- Detecting C compiler ABI info
  -- Detecting C compiler ABI info - failed
  -- Check for working C compiler: /usr/bin/clang-15
  -- Check for working C compiler: /usr/bin/clang-15 - broken
  CMake Error at /usr/share/cmake-3.26/Modules/CMakeTestCCompiler.cmake:67 (message):
    The C compiler
  
      "/usr/bin/clang-15"
  
    is not able to compile a simple test program.
  
    It fails with the following output:
  
      Change Dir: /runtime/artifacts/obj/coreclr/linux.x86.Debug/CMakeFiles/CMakeScratch/TryCompile-qWpnFx
      
      Run Build Command(s):/usr/bin/cmake -E env VERBOSE=1 /usr/bin/gmake -f Makefile cmTC_66ccc/fast && /usr/bin/gmake  -f CMakeFiles/cmTC_66ccc.dir/build.make CMakeFiles/cmTC_66ccc.dir/build
      gmake[1]: Entering directory '/runtime/artifacts/obj/coreclr/linux.x86.Debug/CMakeFiles/CMakeScratch/TryCompile-qWpnFx'
      Building C object CMakeFiles/cmTC_66ccc.dir/testCCompiler.c.o
      /usr/bin/clang-15 --gcc-toolchain=/crossrootfs/x86/usr --sysroot=/crossrootfs/x86   -m32 -Wno-error=unused-command-line-argument -MD -MT CMakeFiles/cmTC_66ccc.dir/testCCompiler.c.o -MF CMakeFiles/cmTC_66ccc.dir/testCCompiler.c.o.d -o CMakeFiles/cmTC_66ccc.dir/testCCompiler.c.o -c /runtime/artifacts/obj/coreclr/linux.x86.Debug/CMakeFiles/CMakeScratch/TryCompile-qWpnFx/testCCompiler.c
      Linking C executable cmTC_66ccc
      /usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_66ccc.dir/link.txt --verbose=1
      /usr/bin/clang-15 --gcc-toolchain=/crossrootfs/x86/usr --sysroot=/crossrootfs/x86 -Wl,--rpath-link=/crossrootfs/x86/lib/i586-alpine-linux-musl -Wl,--rpath-link=/crossrootfs/x86/usr/lib/i586-alpine-linux-musl -Wl,--rpath-link=/crossrootfs/x86/usr/lib/gcc/i586-alpine-linux-musl -m32 -Wl,--rpath-link=/crossrootfs/x86/lib/i586-alpine-linux-musl -Wl,--rpath-link=/crossrootfs/x86/usr/lib/i586-alpine-linux-musl -Wl,--rpath-link=/crossrootfs/x86/usr/lib/gcc/i586-alpine-linux-musl -m32 -fuse-ld=lld  CMakeFiles/cmTC_66ccc.dir/testCCompiler.c.o -o cmTC_66ccc 
      ld.lld: error: cannot open crtbeginS.o: No such file or directory
      ld.lld: error: unable to find library -lgcc
      ld.lld: error: unable to find library -lgcc
      ld.lld: error: cannot open crtendS.o: No such file or directory
      clang: error: linker command failed with exit code 1 (use -v to see invocation)
      gmake[1]: *** [CMakeFiles/cmTC_66ccc.dir/build.make:100: cmTC_66ccc] Error 1
      gmake[1]: Leaving directory '/runtime/artifacts/obj/coreclr/linux.x86.Debug/CMakeFiles/CMakeScratch/TryCompile-qWpnFx'
      gmake: *** [Makefile:127: cmTC_66ccc/fast] Error 2
      
      
  
    
  
    CMake will not be able to correctly generate this project.
  Call Stack (most recent call first):
    CMakeLists.txt:16 (project)
  
  
  -- Configuring incomplete, errors occurred!
  /runtime/src/coreclr
  /runtime/artifacts/obj/coreclr/linux.x86.Debug /runtime/src/coreclr
  Executing make install -j 16
  make: *** No rule to make target 'install'.  Stop.
  /runtime/src/coreclr
  Failed to build "CoreCLR component".
/runtime/src/coreclr/runtime.proj(62,5): error MSB3073: The command ""/runtime/src/coreclr/build-runtime.sh" -x86 -debug -cross -os linux -outputrid linux-musl-x86" exited with code 2.

@am11
Copy link
Member

am11 commented Apr 27, 2023

@janvorli if there is no other feedback, can this be merged?

@janvorli
Copy link
Member

@am11 sure, I am sorry for the delay. It just needs to be updated to the latest main. I'll do it using github.

Copy link
Member

@janvorli janvorli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you!

@akoeplinger akoeplinger enabled auto-merge (squash) April 27, 2023 16:21
@akoeplinger akoeplinger merged commit dd41f27 into dotnet:main Apr 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants