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

ldWrapper tries to call non-existent binary when cross compiling #11834

Closed
expipiplus1 opened this issue Dec 19, 2015 · 8 comments
Closed

ldWrapper tries to call non-existent binary when cross compiling #11834

expipiplus1 opened this issue Dec 19, 2015 · 8 comments
Labels
6.topic: cross-compilation Building packages on a different platform than they will be used on

Comments

@expipiplus1
Copy link
Contributor

In an environment with clang.override {binutils = binutils.override{cross.target = "arm-none-eabi";} clang-wrapper/bin/ld tries to call binutils-arm-none-eabi/bin/ld when the file is actually located at binutils-arm-none-eabi/bin/arm-none-eabi-ld.

I'm not entirely sure that I'm going about this the right way, please let me know if there's a better way to cross compile with clang.

To reproduce:

default.nix

with import <nixpkgs> { };

let
  crossSystem = {
    config = "arm-none-eabi";
  };
  binutils-cross = (binutils.override {cross = crossSystem;});
in

stdenv.mkDerivation rec {
  name = "bug";
  version = "0.0.1";

  buildInputs = [
    (clang.override {binutils = binutils-cross;})
    binutils-cross
    ];
}

main.c

int main(){}

Run

nix-shell --pure
clang main.c -target arm-none-eabi -march=armv6

Output:

/nix/store/4kgv92cb4p1q6lpf1q4cvhkk507n2253-clang-wrapper-3.6.2/bin/ld: line 166: /nix/store/6p37d9411wd09yba0ffw0qwxcbvgk0yx-binutils-2.23.1-arm-none-eabi/bin/ld: No such file or directory
clang-3.6: error: linker command failed with exit code 1 (use -v to see invocation)
@expipiplus1
Copy link
Contributor Author

I'm not convinced I'm doing things correctly, as cc-wrapper is still injecting references to glibc to the clang arguments.

@expipiplus1
Copy link
Contributor Author

@garbas
Copy link
Member

garbas commented May 18, 2016

i'm pinging ppl who i think might have some knowledge about cross-compiling: @viric @bjornfor

@bjornfor
Copy link
Contributor

Sorry, I have no useful input. (I don't use Nix for cross-compilation, but I want to.)

@dvc94ch
Copy link
Contributor

dvc94ch commented Jun 15, 2016

Here's an example clang cross toolchain I built:

with import ../../../nixpkgs {};

let
  pkgsCross = import ../../../nixpkgs {
    inherit system;
    crossSystem = rec {
      config = "armv7-unknown-linux-musleabihf";
      arch = "arm";
      libc = "musl";
      gcc = {
        arch = "armv7-a";
        fpu = "neon";
        float = "hard";
      };
    };
  };

  libgcc = system.pkgsCross.libgcc.crossDrv;

  clang = callPackage ../../../nixpkgs/pkgs/build-support/cc-wrapper {
    cc = llvmPackages.clang-unwrapped;
    isClang = true;
    stdenv = llvmPackages.stdenv;
    libc = pkgsCross.muslCross;
    extraPackages = [ libgcc libcxx libcxxabi ];
    nativeTools = false;
    nativeLibc = false;
    binutils = pkgsCross.binutilsCross;
  };
  libcxxStdenv = overrideCC stdenv clang;
in

libcxxStdenv.mkDerivation rec {
  name = "bug";
  version = "0.0.1";

  src = ./.;

  CFLAGS = "-B${libgcc}/lib -target armv7-unknown-linux-musl-eabihf";

  phases = ["unpackPhase" "buildPhase" "installPhase"];

  installPhase = ''
    mkdir -p $out/bin
    cp hello $out/bin
  '';
}

You'll also need #16217, #15867, gcc6, this binutils patch (clang is surprisingly unflexible in specifying the linker to use, this commit hasn't landed yet llvm-mirror/clang@635bc7f) and my libgcc hack

diff --git a/pkgs/development/tools/misc/binutils/default.nix b/pkgs/development/tools/misc/binutils/default.nix
index 80b5643..b969ab4 100644
--- a/pkgs/development/tools/misc/binutils/default.nix
+++ b/pkgs/development/tools/misc/binutils/default.nix
@@ -75,7 +75,11 @@ stdenv.mkDerivation rec {

   enableParallelBuilding = true;

-  postFixup = optionalString (cross == null) "ln -s $out/bin $dev/bin"; # tools needed for development
+  postFixup =
+    if cross == null then
+      "ln -s $out/bin $dev/bin" # tools needed for development
+    else
+      "ln -s $out/bin/${cross.config}-ld $out/bin/ld";

   meta = with stdenv.lib; {
     description = "Tools for manipulating binaries (linker, assembler, etc.)";
{ stdenv, gcc, glibc, cross ? null, gccCrossStageFinal ? null }:

let
  arch = "x86_64-unknown-linux-gnu";

  path =
    if cross == null
    then "${gcc.cc}/lib/gcc/${arch}/${gcc.cc.version}"
    else "${gccCrossStageFinal.gcc}/lib/gcc/${cross.config}/${gccCrossStageFinal.gcc.version}";

  path-libgcc_s =
    if cross == null
    then "${glibc.out}"
    else "${gccCrossStageFinal.gcc}/${cross.config}";
in

stdenv.mkDerivation rec {
  name = "libgcc";

  buildCommand = ''
    mkdir -p "$out/lib"

    cp ${path}/libgcc.a $out/lib
    cp ${path}/crtbegin.o $out/lib
    cp ${path}/crtbeginS.o $out/lib
    cp ${path}/crtbeginT.o $out/lib
    cp ${path}/crtendS.o $out/lib
    cp ${path}/crtend.o $out/lib
    cp ${path}/crtfastmath.o $out/lib
    cp ${path}/libgcc_eh.a $out/lib

    cp ${path-libgcc_s}/lib/libgcc_s.so $out/lib/libgcc_s.so
    cp ${path-libgcc_s}/lib/libgcc_s.so.1 $out/lib/libgcc_s.so.1
  '';
}

@copumpkin
Copy link
Member

copumpkin commented Jan 18, 2017

@dvc94ch did you look at what we do on darwin to use clang/llvm systemwide? I think we have quite a bit of badly conditioned logic in individual derivations that asks for stdenv.isDarwin when it really should be stdenv.isLLVM but that should be a fairly easy fix.

@Ericson2314 Ericson2314 added the 6.topic: cross-compilation Building packages on a different platform than they will be used on label May 18, 2017
@Ericson2314
Copy link
Member

Ericson2314 commented May 18, 2017

#25232 should have fixed this #25047 will fix the wrapper scripts

@Ericson2314
Copy link
Member

Actually #26007 fixed this

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

No branches or pull requests

6 participants