cargo-utils.scm 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 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. #:export (generate-checksums
  26. generate-all-checksums))
  27. ;; Commentary:
  28. ;;
  29. ;; Stand alone utilities for building Rust crates or the compiler itself,
  30. ;; without depending on the entire cargo build-system itself.
  31. ;;
  32. ;; Code:
  33. (define (file-sha256 file-name)
  34. "Calculate the hexdigest of the sha256 checksum of FILE-NAME and return it."
  35. (let ((port (open-pipe* OPEN_READ
  36. "sha256sum"
  37. "--"
  38. file-name)))
  39. (let ((result (read-delimited " " port)))
  40. (close-pipe port)
  41. result)))
  42. (define (generate-checksums dir-name)
  43. "Given DIR-NAME, a store directory, checksum all the files in it one
  44. by one and put the result into the file \".cargo-checksum.json\" in
  45. the same directory."
  46. (let* ((file-names (find-files dir-name "."))
  47. (dir-prefix-name (string-append dir-name "/"))
  48. (dir-prefix-name-len (string-length dir-prefix-name))
  49. (checksums-file-name (string-append dir-name "/.cargo-checksum.json")))
  50. (call-with-output-file checksums-file-name
  51. (lambda (port)
  52. (display "{\"files\":{" port)
  53. (let ((sep ""))
  54. (for-each (lambda (file-name)
  55. (let ((file-relative-name (string-drop file-name dir-prefix-name-len)))
  56. (display sep port)
  57. (set! sep ",")
  58. (write file-relative-name port)
  59. (display ":" port)
  60. (write (file-sha256 file-name) port))) file-names))
  61. ;; NB: cargo requires the "package" field in order to check if the Cargo.lock
  62. ;; file needs to be regenerated when the value changes. However, it doesn't
  63. ;; appear to care what the value is to begin with...
  64. (display "},\"package\":" port)
  65. (write (file-sha256 "/dev/null") port)
  66. (display "}" port)))))
  67. (define (generate-all-checksums dir-name)
  68. (for-each
  69. (lambda (filename)
  70. (let* ((dir (dirname filename))
  71. (checksum-file (string-append dir "/.cargo-checksum.json")))
  72. (when (file-exists? checksum-file) (delete-file checksum-file))
  73. (display (string-append
  74. "patch-cargo-checksums: generate-checksums for "
  75. dir "\n"))
  76. (generate-checksums dir)))
  77. (find-files dir-name "Cargo.toml$")))