cargo-utils.scm 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016 David Craven <david@craven.ch>
  3. ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
  4. ;;; Copyright © 2019 Ivan Petkov <ivanppetkov@gmail.com>
  5. ;;; Copyright © 2019, 2020 Efraim Flashner <efraim@flashner.co.il>
  6. ;;;
  7. ;;; This file is part of GNU Guix.
  8. ;;;
  9. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  10. ;;; under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation; either version 3 of the License, or (at
  12. ;;; your option) any later version.
  13. ;;;
  14. ;;; GNU Guix is distributed in the hope that it will be useful, but
  15. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;; GNU General Public License for more details.
  18. ;;;
  19. ;;; You should have received a copy of the GNU General Public License
  20. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  21. (define-module (guix build cargo-utils)
  22. #:use-module (guix build utils)
  23. #:use-module (ice-9 popen)
  24. #:use-module (ice-9 rdelim)
  25. #:use-module (ice-9 threads)
  26. #:export (generate-checksums
  27. generate-all-checksums))
  28. ;; Commentary:
  29. ;;
  30. ;; Stand alone utilities for building Rust crates or the compiler itself,
  31. ;; without depending on the entire cargo build-system itself.
  32. ;;
  33. ;; Code:
  34. (define (file-sha256 file-name)
  35. "Calculate the hexdigest of the sha256 checksum of FILE-NAME and return it."
  36. (let ((port (open-pipe* OPEN_READ
  37. "sha256sum"
  38. "--"
  39. file-name)))
  40. (let ((result (read-delimited " " port)))
  41. (close-pipe port)
  42. result)))
  43. (define (generate-checksums dir-name)
  44. "Given DIR-NAME, a store directory, checksum all the files in it one
  45. by one and put the result into the file \".cargo-checksum.json\" in
  46. the same directory."
  47. (let* ((file-names (find-files dir-name "."))
  48. (dir-prefix-name (string-append dir-name "/"))
  49. (dir-prefix-name-len (string-length dir-prefix-name))
  50. (checksums-file-name (string-append dir-name "/.cargo-checksum.json")))
  51. (call-with-output-file checksums-file-name
  52. (lambda (port)
  53. (display "{\"files\":{" port)
  54. (let ((sep ""))
  55. (for-each (lambda (file-name)
  56. (let ((file-relative-name (string-drop file-name dir-prefix-name-len)))
  57. (display sep port)
  58. (set! sep ",")
  59. (write file-relative-name port)
  60. (display ":" port)
  61. (write (file-sha256 file-name) port))) file-names))
  62. ;; NB: cargo requires the "package" field in order to check if the Cargo.lock
  63. ;; file needs to be regenerated when the value changes. However, it doesn't
  64. ;; appear to care what the value is to begin with...
  65. (display "},\"package\":" port)
  66. (write (file-sha256 "/dev/null") port)
  67. (display "}" port)))))
  68. (define (generate-all-checksums dir-name)
  69. (n-par-for-each (parallel-job-count)
  70. (lambda (filename)
  71. (let* ((dir (dirname filename))
  72. (checksum-file (string-append dir "/.cargo-checksum.json")))
  73. (when (file-exists? checksum-file) (delete-file checksum-file))
  74. (display (string-append
  75. "patch-cargo-checksums: generate-checksums for "
  76. dir "\n"))
  77. (generate-checksums dir)))
  78. (find-files dir-name "Cargo.toml$")))