libc icon indicating copy to clipboard operation
libc copied to clipboard

libc does not seem to respect the CC environment variable

Open molysgaard opened this issue 1 year ago • 1 comments

To reproduce:

  • Be on a platform that does not have the cc binary, but does have eg. gcc installed
  • Compile using CC=gcc cargo build
  • Observe the following error:
error: linker `cc` not found
  |
  = note: No such file or directory (os error 2)

error: could not compile `libc` (build script) due to 1 previous error

Correct behaviour should be that the build.rs script should honor the CC environment variable and use that to link.

This is explicitly a problem when trying to get reproducible builds in systems like GUIX or (I presume, NIX).

Guix example

; working-developer-shell.scm
(use-modules (gnu)
             (gnu packages commencement)
	     (guix)
	     (guix packages)
	     (guix build-system trivial)
	     (guix licenses)
             )

(define base-manifest (specifications->manifest
  '(
    "[email protected]"
    "[email protected]"
    "gcc-toolchain"
    "coreutils"
    )))

(define-public custom-cc
  (package
    (name "custom-cc")
    (version "1.0")
    (source #f)
    (build-system trivial-build-system)
    (arguments
     `(#:modules ((guix build utils))
       #:builder (begin
                   (use-modules (guix build utils))
                   (let* ((out (assoc-ref %outputs "out"))
                          (bin (string-append out "/bin"))
                          ;; Adjust the path to the gcc binary as necessary.
                          (gcc-path (string-append (assoc-ref %build-inputs "gcc-toolchain") "/bin/gcc")))
                     (mkdir-p bin)
                     (symlink gcc-path (string-append bin "/cc"))))))
    (inputs
     `(("gcc-toolchain", gcc-toolchain)))
    (synopsis "Custom package providing a cc symlink to gcc")
    (description "This package installs a symlink named 'cc' in its bin output, which points to the gcc binary in the gcc-toolchain package.")
    (home-page "https://example.com")
    (license gpl3)))


(define custom-cc-manifest (manifest (list (package->manifest-entry custom-cc))))

(concatenate-manifests (list base-manifest custom-cc-manifest))
; broken-developer-shell.scm
(use-modules (gnu)
             (gnu packages commencement)
	     (guix)
	     (guix packages)
	     (guix build-system trivial)
	     (guix licenses)
             )

(define base-manifest (specifications->manifest
  '(
    "[email protected]"
    "[email protected]"
    "gcc-toolchain"
    "coreutils"
    )))

base-manifest

Broken example using guix

git clone https://github.com/rust-lang/libc.git
cd libc
guix environment -m ../broken-developer-shell.scm --pure
CC=gcc cargo build

Working example using guix

In this example, we add a symlink named cc in our PATH that points to gcc.

git clone https://github.com/rust-lang/libc.git
cd libc
guix environment -m ../working-developer-shell.scm --pure
cargo build

molysgaard avatar Feb 29 '24 13:02 molysgaard

This is not an issue of the libc crate, but of the rust compiler, since rustc invokes the linker. You should be able to use CARGO_TARGET_<triple>_LINKER to set the linker or alternartively open an issue at rust compiler repository.

jschwe avatar Jun 05 '24 12:06 jschwe

The cc crate should actually respect these, but @jschwe is correct that rustc does not use CC as the linker. The linker can be specified in a Cargo config file https://doc.rust-lang.org/cargo/reference/config.html?highlight=linker#targettriplelinker, or by passing -Clinker=... to rustc.

I don't think there is anything to do here so I will close this, feel free to follow up if there is more.

tgross35 avatar Aug 29 '24 09:08 tgross35