chicken-build-system.scm 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2020 raingloom <raingloom@riseup.net>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (guix build chicken-build-system)
  19. #:use-module ((guix build gnu-build-system) #:prefix gnu:)
  20. #:use-module (guix build union)
  21. #:use-module (guix build utils)
  22. #:use-module (ice-9 match)
  23. #:use-module (ice-9 ftw)
  24. #:use-module (srfi srfi-1)
  25. #:use-module (srfi srfi-26)
  26. #:use-module (rnrs io ports)
  27. #:use-module (rnrs bytevectors)
  28. #:export (%standard-phases
  29. chicken-build))
  30. ;; CHICKEN_EGG_CACHE is where sources are fetched and binaries are built
  31. ;; CHICKEN_INSTALL_REPOSITORY is where dependencies are looked up
  32. ;; its first component is also where new eggs are installed.
  33. ;; TODO: deduplicate with go-build-system.scm ?
  34. ;; TODO: the binary version should be defined in one of the relevant modules
  35. ;; instead of being hardcoded everywhere. Tried to do that but got undefined
  36. ;; variable errors.
  37. (define (chicken-package? name)
  38. (string-prefix? "chicken-" name))
  39. (define* (setup-chicken-environment #:key inputs outputs #:allow-other-keys)
  40. (setenv "CHICKEN_INSTALL_REPOSITORY"
  41. (string-concatenate
  42. ;; see TODO item about binary version above
  43. (append (list (assoc-ref outputs "out") "/var/lib/chicken/11/")
  44. (let ((oldenv (getenv "CHICKEN_INSTALL_REPOSITORY")))
  45. (if oldenv
  46. (list ":" oldenv)
  47. '())))))
  48. (setenv "CHICKEN_EGG_CACHE" (getcwd))
  49. #t)
  50. ;; This is copied from go-build-system.scm so it could probably be simplified.
  51. ;; I used it because the source of the egg needs to be unpacked into a directory
  52. ;; that is named after the egg and I knew that the go build system does that.
  53. (define* (unpack #:key source egg-name unpack-path #:allow-other-keys)
  54. "Relative to $CHICKEN_EGG_CACHE, unpack SOURCE in UNPACK-PATH, or EGG-NAME
  55. when UNPACK-PATH is unset. If the SOURCE archive has a single top level
  56. directory, it is stripped so that the sources appear directly under UNPACK-PATH.
  57. When SOURCE is a directory, copy its content into UNPACK-PATH instead of
  58. unpacking."
  59. (define (unpack-maybe-strip source dest)
  60. (let* ((scratch-dir (string-append (or (getenv "TMPDIR") "/tmp")
  61. "/scratch-dir"))
  62. (out (mkdir-p scratch-dir)))
  63. (with-directory-excursion scratch-dir
  64. (if (string-suffix? ".zip" source)
  65. (invoke "unzip" source)
  66. (invoke "tar" "-xvf" source))
  67. (let ((top-level-files (remove (lambda (x)
  68. (member x '("." "..")))
  69. (scandir "."))))
  70. (match top-level-files
  71. ((top-level-file)
  72. (when (file-is-directory? top-level-file)
  73. (copy-recursively top-level-file dest #:keep-mtime? #t)))
  74. (_
  75. (copy-recursively "." dest #:keep-mtime? #t)))))
  76. (delete-file-recursively scratch-dir)))
  77. (when (string-null? egg-name)
  78. (display "WARNING: The egg name is unset.\n"))
  79. (when (string-null? unpack-path)
  80. (set! unpack-path egg-name))
  81. (let ((dest (string-append (getenv "CHICKEN_EGG_CACHE") "/" unpack-path)))
  82. (mkdir-p dest)
  83. (if (file-is-directory? source)
  84. (copy-recursively source dest #:keep-mtime? #t)
  85. (unpack-maybe-strip source dest)))
  86. #t)
  87. (define* (build #:key egg-name #:allow-other-keys)
  88. "Build the Chicken egg named by EGG-NAME"
  89. (invoke "chicken-install" "-cached" "-no-install" egg-name))
  90. (define* (install #:key egg-name #:allow-other-keys)
  91. "Install the already built egg named by EGG-NAME"
  92. (invoke "chicken-install" "-cached" egg-name))
  93. (define* (check #:key egg-name tests? #:allow-other-keys)
  94. "Build and run tests for the Chicken egg EGG-NAME"
  95. ;; there is no "-test-only" option, but we've already run install
  96. ;; so this just runs tests.
  97. ;; i think it's a fair assumption that phases won't be reordered.
  98. (setenv "CHICKEN_REPOSITORY_PATH"
  99. (string-append (getenv "CHICKEN_INSTALL_REPOSITORY")
  100. ":"
  101. (getenv "CHICKEN_REPOSITORY_PATH")))
  102. (when tests?
  103. (invoke "chicken-install" "-cached" "-test" "-no-install" egg-name)))
  104. (define* (stamp-egg-version #:key egg-name name #:allow-other-keys)
  105. "Check if EGG-NAME.egg contains version information and add some if not."
  106. (let* ((filename (string-append egg-name "/" egg-name ".egg"))
  107. (egg-info (call-with-input-file filename read))
  108. (ver? (find (lambda (i) (eqv? (car i) 'version)) egg-info))
  109. (ver (substring name (1+ (string-rindex name #\-)))))
  110. (when (not ver?)
  111. (make-file-writable filename)
  112. (call-with-output-file filename
  113. (lambda (f) (write (cons `(version ,ver) egg-info) f))))))
  114. ;; It doesn't look like Chicken generates any unnecessary references.
  115. ;; So we don't have to remove them either. Nice.
  116. (define %standard-phases
  117. (modify-phases gnu:%standard-phases
  118. (replace 'unpack unpack)
  119. (delete 'bootstrap)
  120. (delete 'configure)
  121. (delete 'patch-generated-file-shebangs)
  122. (add-before 'unpack 'setup-chicken-environment setup-chicken-environment)
  123. (add-before 'build 'stamp-egg-version stamp-egg-version)
  124. (replace 'build build)
  125. (delete 'check)
  126. (replace 'install install)
  127. (add-after 'install 'check check)))
  128. (define* (chicken-build #:key inputs (phases %standard-phases)
  129. #:allow-other-keys #:rest args)
  130. "Build the given Chicken package, applying all of PHASES in order."
  131. (apply gnu:gnu-build #:inputs inputs #:phases phases args))