Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
toolchain.cmake: properly set toolchain for crossbuilding on Alpine
  • Loading branch information
ayakael committed Nov 13, 2022
commit b17639c6b999611cf92c01f0153b3fb3d9340e6e
38 changes: 31 additions & 7 deletions eng/common/cross/toolchain.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,25 @@ elseif(TARGET_ARCH_NAME STREQUAL "armv6")
endif()
elseif(TARGET_ARCH_NAME STREQUAL "ppc64le")
set(CMAKE_SYSTEM_PROCESSOR ppc64le)
set(TOOLCHAIN "powerpc64le-linux-gnu")
if(EXISTS ${CROSS_ROOTFS}/usr/lib/gcc/powerpc64le-alpine-linux-musl)
set(TOOLCHAIN "powerpc64le-alpine-linux-musl")
else()
set(TOOLCHAIN "powerpc64le-linux-gnu")
endif()
elseif(TARGET_ARCH_NAME STREQUAL "riscv64")
set(CMAKE_SYSTEM_PROCESSOR riscv64)
set(TOOLCHAIN "riscv64-linux-gnu")
if(EXISTS ${CROSS_ROOTFS}/usr/lib/gcc/riscv64-alpine-linux-musl)
set(TOOLCHAIN "riscv64-alpine-linux-musl")
else()
set(TOOLCHAIN "riscv64-linux-gnu")
endif()
elseif(TARGET_ARCH_NAME STREQUAL "s390x")
set(CMAKE_SYSTEM_PROCESSOR s390x)
set(TOOLCHAIN "s390x-linux-gnu")
if(EXISTS ${CROSS_ROOTFS}/usr/lib/gcc/s390x-alpine-linux-musl)
set(TOOLCHAIN "s390x-alpine-linux-musl")
else()
set(TOOLCHAIN "s390x-linux-gnu")
endif()
elseif(TARGET_ARCH_NAME STREQUAL "x64")
set(CMAKE_SYSTEM_PROCESSOR x86_64)
if(LINUX)
Expand All @@ -83,7 +95,11 @@ elseif(TARGET_ARCH_NAME STREQUAL "x64")
endif()
elseif(TARGET_ARCH_NAME STREQUAL "x86")
set(CMAKE_SYSTEM_PROCESSOR i686)
set(TOOLCHAIN "i686-linux-gnu")
if(EXISTS ${CROSS_ROOTFS}/usr/lib/gcc/i586-alpine-linux-musl)
set(TOOLCHAIN "i586-alpine-linux-musl")
else()
set(TOOLCHAIN "i686-linux-gnu")
endif()
if(TIZEN)
set(TIZEN_TOOLCHAIN "i586-tizen-linux-gnu/9.2.0")
endif()
Expand Down Expand Up @@ -253,8 +269,12 @@ elseif(TARGET_ARCH_NAME STREQUAL "arm64")
add_toolchain_linker_flag("-Wl,--rpath-link=${CROSS_ROOTFS}/usr/lib64/gcc/${TIZEN_TOOLCHAIN}")
endif()
elseif(TARGET_ARCH_NAME STREQUAL "x86")
add_toolchain_linker_flag(-m32)

if(EXISTS ${CROSS_ROOTFS}/usr/lib/gcc/i586-alpine-linux-musl)
add_toolchain_linker_flag("--target=${TOOLCHAIN}")
add_toolchain_linker_flag("-Wl,--rpath-link=${CROSS_ROOTFS}/usr/lib/gcc/${TOOLCHAIN}")
else()
add_toolchain_linker_flag(-m32)
endif()
if(TIZEN)
add_toolchain_linker_flag("-B${CROSS_ROOTFS}/usr/lib/gcc/${TIZEN_TOOLCHAIN}")
add_toolchain_linker_flag("-L${CROSS_ROOTFS}/lib")
Expand Down Expand Up @@ -291,7 +311,11 @@ if(TARGET_ARCH_NAME MATCHES "^(arm|armel)$")
add_compile_options(-mfloat-abi=softfp)
endif()
elseif(TARGET_ARCH_NAME STREQUAL "x86")
add_compile_options(-m32)
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://github.com/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.

else()
add_compile_options(-m32)
endif()
add_compile_options(-Wno-error=unused-command-line-argument)
endif()

Expand Down