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

CoreFoundation.tbd hook breaks darwin -> non-darwin cross-compilation #278348

Closed
r-burns opened this issue Jan 2, 2024 · 3 comments
Closed

CoreFoundation.tbd hook breaks darwin -> non-darwin cross-compilation #278348

r-burns opened this issue Jan 2, 2024 · 3 comments
Labels
0.kind: bug 6.topic: cross-compilation Building packages on a different platform than they will be used on 6.topic: darwin Running or building packages on Darwin

Comments

@r-burns
Copy link
Contributor

r-burns commented Jan 2, 2024

The CoreFoundation.tbd setup hook in darwin's CoreFoundation does not take build/host offsets into account, so it unconditionally applies CFLAGS/LDFLAGS which are invalid for non-darwin hosts. This can occur when a package pulls in CoreFoundation via a buildPackage's propagatedBuildInputs. Since CF is propagated by a number of packages, this occurs quite often.

This seems to have technically been an issue for as long as this setup hook has existed, but has only started causing problems now that we are explicitly linking the .tbd file. Previously it was an extraneous directory that could be ignored, but now the explicit .tbd file is seen as an invalid linker script by the target cross linker.

For example, see: nixpkgs:cross-trunk:rpi-musl.nix.x86_64-darwin (https://hydra.nixos.org/build/241448869/nixlog/1):

CMake Error at /nix/store/nsk9a663g0khs9li1hwwqlsxsvqd0x9d-cmake-3.27.7/share/cmake-3.27/Modules/CMakeTestCXXCompiler.cmake:60 (message):
  The C++ compiler

    "/nix/store/7hl3605laf3qi72bgzkxkvpg4ziqzlch-armv6l-unknown-linux-musleabihf-gcc-wrapper-12.3.0/bin/armv6l-unknown-linux-musleabihf-g++"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: '/tmp/nix-build-aws-sdk-cpp-armv6l-unknown-linux-musleabihf-1.11.118.drv-0/source/build/CMakeFiles/CMakeScratch/TryCompile-cfogov'
    
    Run Build Command(s): /nix/store/nsk9a663g0khs9li1hwwqlsxsvqd0x9d-cmake-3.27.7/bin/cmake -E env VERBOSE=1 /nix/store/x5dy4sqg1nxsl7nwibanfrbx95wdb228-gnumake-4.4.1/bin/make -f Makefile cmTC_fd3de/fast
    /nix/store/x5dy4sqg1nxsl7nwibanfrbx95wdb228-gnumake-4.4.1/bin/make  -f CMakeFiles/cmTC_fd3de.dir/build.make CMakeFiles/cmTC_fd3de.dir/build
    make[1]: Entering directory '/private/tmp/nix-build-aws-sdk-cpp-armv6l-unknown-linux-musleabihf-1.11.118.drv-0/source/build/CMakeFiles/CMakeScratch/TryCompile-cfogov'
    Building CXX object CMakeFiles/cmTC_fd3de.dir/testCXXCompiler.cxx.o
    /nix/store/7hl3605laf3qi72bgzkxkvpg4ziqzlch-armv6l-unknown-linux-musleabihf-gcc-wrapper-12.3.0/bin/armv6l-unknown-linux-musleabihf-g++    -o CMakeFiles/cmTC_fd3de.dir/testCXXCompiler.cxx.o -c /tmp/nix-build-aws-sdk-cpp-armv6l-unknown-linux-musleabihf-1.11.118.drv-0/source/build/CMakeFiles/CMakeScratch/TryCompile-cfogov/testCXXCompiler.cxx
    Linking CXX executable cmTC_fd3de
    /nix/store/nsk9a663g0khs9li1hwwqlsxsvqd0x9d-cmake-3.27.7/bin/cmake -E cmake_link_script CMakeFiles/cmTC_fd3de.dir/link.txt --verbose=1
    /nix/store/7hl3605laf3qi72bgzkxkvpg4ziqzlch-armv6l-unknown-linux-musleabihf-gcc-wrapper-12.3.0/bin/armv6l-unknown-linux-musleabihf-g++ CMakeFiles/cmTC_fd3de.dir/testCXXCompiler.cxx.o -o cmTC_fd3de 
    /nix/store/25607ap8xf8n6q29zbz9ikr6dl9mkrs6-armv6l-unknown-linux-musleabihf-binutils-2.40/bin/armv6l-unknown-linux-musleabihf-ld:/nix/store/xk64xciny5qx5g6wjbq7839zxqb53kjq-apple-framework-CoreFoundation/Library/Frameworks/CoreFoundation.framework/CoreFoundation.tbd: file format not recognized; treating as linker script
    /nix/store/25607ap8xf8n6q29zbz9ikr6dl9mkrs6-armv6l-unknown-linux-musleabihf-binutils-2.40/bin/armv6l-unknown-linux-musleabihf-ld:/nix/store/xk64xciny5qx5g6wjbq7839zxqb53kjq-apple-framework-CoreFoundation/Library/Frameworks/CoreFoundation.framework/CoreFoundation.tbd:1: syntax error
    collect2: error: ld returned 1 exit status

(This can be reproduced locally on x86_64-darwin via nix-build -A pkgsCross.muslpi.aws-sdk-cpp)

I suspect this may be fixed by using role_post correctly as in other libraries' setup hooks, e.g.:

getHostRole
export NIX_LDFLAGS${role_post}+=" -liconv"

However, making this change to CF's setup hook would be a a stdenv rebuild on darwin, and I don't have time at the moment to rebuild the world to test this out.

@r-burns r-burns added 0.kind: bug 6.topic: darwin Running or building packages on Darwin 6.topic: cross-compilation Building packages on a different platform than they will be used on labels Jan 2, 2024
@simonzkl
Copy link
Contributor

I had to work around this issue so I quickly hacked together this hook:

makeSetupHook { name = "fixCoreFoundationCrossHook"; }
  (writeScript "fixCoreFoundationCrossHook" ''
    fixCoreFoundationCross() {
      local old_ldflags=($NIX_LDFLAGS)
      local new_ldflags=()

      local old_cflags_compile=($NIX_CFLAGS_COMPILE)
      local new_cflags_compile=()

      for flag in "''${old_cflags_compile[@]}"; do
        if [[ "$flag" != -F*/Library/Frameworks* ]]; then
          new_cflags_compile+=("$flag")
        fi
      done

      for flag in "''${old_ldflags[@]}"; do
        if [[ "$flag" != *CoreFoundation.tbd* ]]; then
          new_ldflags+=("$flag")
        fi
      done

      NIX_LDFLAGS="''${new_ldflags[@]}"
      NIX_CFLAGS_COMPILE="''${new_cflags_compile[@]}"
    }

    postConfigureHooks+=(fixCoreFoundationCross)
  '');

I load this only when cross-compiling from Darwin.

@szlend
Copy link
Member

szlend commented Jan 27, 2024

I'd be happy to fix and test this with some guidance. I attempted the suggested fix using getHostRole and role_post, but it seems $hostOffset is not is scope (for getHostRole). So I'm a bit out of my depth here.

Edit: I assumed this might only be the case while bootstrapping, but it's not available in aws-sdk-cpp-aarch64-unknown-linux-gnu either.

Edit 2: Figured it out. $hostOffset is a local variable that gets lost when using preConfigureHooks.

reckenrode added a commit to reckenrode/nixpkgs that referenced this issue Jan 28, 2024
As of NixOS#265102, x86_64-darwin no
longer uses the open-source CF release. Since there is no longer a need
to switch between implementations, and the hook causes problems with
cross-compilation (see NixOS#278348),
drop the hook and make both darwin.CF an alias for
darwin.apple_sdk.frameworks.CoreFoundation.
reckenrode added a commit to reckenrode/nixpkgs that referenced this issue May 31, 2024
As of NixOS#265102, x86_64-darwin no
longer uses the open-source CF release. Since there is no longer a need
to switch between implementations, and the hook causes problems with
cross-compilation (see NixOS#278348),
drop the hook and make both darwin.CF an alias for
darwin.apple_sdk.frameworks.CoreFoundation.
reckenrode added a commit to reckenrode/nixpkgs that referenced this issue Jun 3, 2024
As of NixOS#265102, x86_64-darwin no
longer uses the open-source CF release. Since there is no longer a need
to switch between implementations, and the hook causes problems with
cross-compilation (see NixOS#278348),
drop the hook and make both darwin.CF an alias for
darwin.apple_sdk.frameworks.CoreFoundation.
reckenrode added a commit to reckenrode/nixpkgs that referenced this issue Jul 16, 2024
As of NixOS#265102, x86_64-darwin no
longer uses the open-source CF release. Since there is no longer a need
to switch between implementations, and the hook causes problems with
cross-compilation (see NixOS#278348),
drop the hook and make both darwin.CF an alias for
darwin.apple_sdk.frameworks.CoreFoundation.
@aviallon
Copy link
Contributor

Closed by #346043

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0.kind: bug 6.topic: cross-compilation Building packages on a different platform than they will be used on 6.topic: darwin Running or building packages on Darwin
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants