123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- (define-module (guix build cargo-build-system)
- #:use-module ((guix build gnu-build-system) #:prefix gnu:)
- #:use-module (guix build json)
- #:use-module (guix build utils)
- #:use-module (guix build cargo-utils)
- #:use-module (ice-9 popen)
- #:use-module (ice-9 rdelim)
- #:use-module (ice-9 ftw)
- #:use-module (ice-9 format)
- #:use-module (ice-9 match)
- #:use-module (srfi srfi-1)
- #:use-module (srfi srfi-26)
- #:export (%standard-phases
- cargo-build))
- (define (manifest-targets)
- "Extract all targets from the Cargo.toml manifest"
- (let* ((port (open-input-pipe "cargo read-manifest"))
- (data (read-json port))
- (targets (or (assoc-ref data "targets") '())))
- (close-port port)
- targets))
- (define (has-executable-target?)
- "Check if the current cargo project declares any binary targets."
- (let* ((bin? (lambda (kind) (string=? kind "bin")))
- (get-kinds (lambda (dep) (assoc-ref dep "kind")))
- (bin-dep? (lambda (dep) (find bin? (get-kinds dep)))))
- (find bin-dep? (manifest-targets))))
- (define (crate-src? path)
- "Check if PATH refers to a crate source, namely a gzipped tarball with a
- Cargo.toml file present at its root."
- (and (not (directory-exists? path))
-
-
-
-
-
-
-
- (apply invoke (list "sh" "-c"
- (string-append "tar -tf " path
- " | cut -d/ -f2"
- " | grep -q '^Cargo.toml$'")))))
- (define* (unpack-rust-crates #:key inputs vendor-dir #:allow-other-keys)
- (define (inputs->rust-inputs inputs)
- "Filter using the label part from INPUTS."
- (filter (lambda (input)
- (match input
- ((name . _) (rust-package? name))))
- inputs))
- (define (inputs->directories inputs)
- "Extract the directory part from INPUTS."
- (match inputs
- (((names . directories) ...)
- directories)))
- (let ((rust-inputs (inputs->directories (inputs->rust-inputs inputs))))
- (unless (null? rust-inputs)
- (mkdir-p "target/package")
- (mkdir-p vendor-dir)
-
- (for-each
- (lambda (input-crate)
- (for-each
- (lambda (packaged-crate)
- (unless
- (file-exists?
- (string-append "target/package/" (basename packaged-crate)))
- (install-file packaged-crate "target/package/")))
- (find-files
- (string-append input-crate "/share/cargo/registry") "\\.crate$")))
- (delete-duplicates rust-inputs))
- (for-each (lambda (crate)
- (invoke "tar" "xzf" crate "-C" vendor-dir))
- (find-files "target/package" "\\.crate$"))))
- #t)
- (define (rust-package? name)
- (string-prefix? "rust-" name))
- (define* (configure #:key inputs
- (vendor-dir "guix-vendor")
- #:allow-other-keys)
- "Vendor Cargo.toml dependencies as guix inputs."
- (chmod "." #o755)
-
-
-
-
- (mkdir-p vendor-dir)
- (for-each
- (match-lambda
- ((name . path)
- (let* ((basepath (strip-store-file-name path))
- (crate-dir (string-append vendor-dir "/" basepath)))
- (and (crate-src? path)
-
- (not (file-exists? crate-dir))
- (mkdir-p crate-dir)
-
-
-
-
-
- (invoke "tar" "xvf" path "-C" crate-dir "--strip-components" "1")))))
- inputs)
-
- (setenv "CARGO_HOME" (string-append (getcwd) "/.cargo"))
- (mkdir-p ".cargo")
- (let ((port (open-file ".cargo/config" "w" #:encoding "utf-8")))
- (display "
- [source.crates-io]
- replace-with = 'vendored-sources'
- [source.vendored-sources]
- directory = '" port)
- (display (string-append (getcwd) "/" vendor-dir) port)
- (display "'
- " port)
- (close-port port))
-
-
-
-
- (setenv "RUSTFLAGS" "--cap-lints allow")
- (setenv "CC" (string-append (assoc-ref inputs "gcc") "/bin/gcc"))
- (setenv "LIBGIT2_SYS_USE_PKG_CONFIG" "1")
- (setenv "LIBSSH2_SYS_USE_PKG_CONFIG" "1")
- (when (assoc-ref inputs "openssl")
- (setenv "OPENSSL_DIR" (assoc-ref inputs "openssl")))
- (when (assoc-ref inputs "gettext")
- (setenv "GETTEXT_SYSTEM" (assoc-ref inputs "gettext")))
- (when (assoc-ref inputs "clang")
- (setenv "LIBCLANG_PATH"
- (string-append (assoc-ref inputs "clang") "/lib")))
-
-
-
- (when (file-exists? "Cargo.lock")
- (delete-file "Cargo.lock"))
- #t)
- (define* (patch-cargo-checksums #:key
- (vendor-dir "guix-vendor")
- #:allow-other-keys)
- "Patch the checksums of the vendored crates after patching their shebangs."
- (generate-all-checksums vendor-dir)
- #t)
- (define* (build #:key
- skip-build?
- (features '())
- (cargo-build-flags '("--release"))
- #:allow-other-keys)
- "Build a given Cargo package."
- (or skip-build?
- (apply invoke
- `("cargo" "build"
- ,@(if (null? features)
- '()
- `("--features" ,(string-join features)))
- ,@cargo-build-flags))))
- (define* (check #:key
- tests?
- (cargo-test-flags '("--release"))
- #:allow-other-keys)
- "Run tests for a given Cargo package."
- (if tests?
- (apply invoke "cargo" "test" cargo-test-flags)
- #t))
- (define* (package #:key
- install-source?
- (cargo-package-flags '("--no-metadata" "--no-verify"))
- #:allow-other-keys)
- "Run 'cargo-package' for a given Cargo package."
- (if install-source?
- (apply invoke `("cargo" "package" ,@cargo-package-flags))
- (format #t "Not installing cargo sources, skipping `cargo package`.~%"))
- #t)
- (define* (install #:key
- inputs
- outputs
- skip-build?
- install-source?
- features
- #:allow-other-keys)
- "Install a given Cargo package."
- (let* ((out (assoc-ref outputs "out"))
- (registry (string-append out "/share/cargo/registry"))
- (sources (string-append out "/share/cargo/src")))
- (mkdir-p out)
-
-
- (setenv "CARGO_TARGET_DIR" "./target")
-
-
- (or skip-build?
- (not (has-executable-target?))
- (invoke "cargo" "install" "--no-track" "--path" "." "--root" out
- "--features" (string-join features)))
- (when install-source?
-
-
- (mkdir-p sources)
- (for-each (lambda (crate)
- (install-file crate registry))
- (find-files "target/package" "\\.crate$"))
- (for-each (lambda (crate)
- (invoke "tar" "xzf" crate "-C" sources))
- (find-files registry "\\.crate$")))
- #t))
- (define %standard-phases
- (modify-phases gnu:%standard-phases
- (delete 'bootstrap)
- (replace 'configure configure)
- (replace 'build build)
- (replace 'check check)
- (replace 'install install)
- (add-after 'build 'package package)
- (add-after 'unpack 'unpack-rust-crates unpack-rust-crates)
- (add-after 'patch-generated-file-shebangs 'patch-cargo-checksums patch-cargo-checksums)))
- (define* (cargo-build #:key inputs (phases %standard-phases)
- #:allow-other-keys #:rest args)
- "Build the given Cargo package, applying all of PHASES in order."
- (apply gnu:gnu-build #:inputs inputs #:phases phases args))
|