chicken-build-system.scm 6.2 KB

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