asdf-build-system.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016, 2017 Andy Patterson <ajpatter@uwaterloo.ca>
  3. ;;; Copyright © 2020 Guillaume Le Vaillant <glv@posteo.net>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (guix build asdf-build-system)
  20. #:use-module ((guix build gnu-build-system) #:prefix gnu:)
  21. #:use-module (guix build utils)
  22. #:use-module (guix build union)
  23. #:use-module (guix build lisp-utils)
  24. #:use-module (srfi srfi-1)
  25. #:use-module (srfi srfi-11)
  26. #:use-module (srfi srfi-26)
  27. #:use-module (ice-9 rdelim)
  28. #:use-module (ice-9 receive)
  29. #:use-module (ice-9 regex)
  30. #:use-module (ice-9 match)
  31. #:use-module (ice-9 format)
  32. #:use-module (ice-9 ftw)
  33. #:export (%standard-phases
  34. %standard-phases/source
  35. asdf-build
  36. asdf-build/source))
  37. ;; Commentary:
  38. ;;
  39. ;; System for building ASDF packages; creating executable programs and images
  40. ;; from them.
  41. ;;
  42. ;; Code:
  43. (define %object-prefix "/lib/common-lisp")
  44. (define (%lisp-source-install-prefix)
  45. (string-append %source-install-prefix "/" (%lisp-type)))
  46. (define %system-install-prefix
  47. (string-append %source-install-prefix "/systems"))
  48. (define (main-system-name output)
  49. (let ((package-name (package-name->name+version
  50. (strip-store-file-name output)))
  51. (lisp-prefix (string-append (%lisp-type) "-")))
  52. (if (string-prefix? lisp-prefix package-name)
  53. (string-drop package-name (string-length lisp-prefix))
  54. package-name)))
  55. (define (lisp-source-directory output name)
  56. (string-append output (%lisp-source-install-prefix) "/" name))
  57. (define (source-directory output name)
  58. (string-append output %source-install-prefix "/source/" name))
  59. (define (library-directory output)
  60. (string-append output %object-prefix
  61. "/" (%lisp-type)))
  62. (define (output-translation source-path
  63. object-output)
  64. "Return a translation for the system's source path
  65. to it's binary output."
  66. `((,source-path
  67. :**/ :*.*.*)
  68. (,(library-directory object-output)
  69. :**/ :*.*.*)))
  70. (define (source-asd-file output name asd-file)
  71. (string-append (lisp-source-directory output name) "/" asd-file))
  72. (define (find-asd-files output name asd-files)
  73. (if (null? asd-files)
  74. (find-files (lisp-source-directory output name) "\\.asd$")
  75. (map (lambda (asd-file)
  76. (source-asd-file output name asd-file))
  77. asd-files)))
  78. (define (copy-files-to-output out name)
  79. "Copy all files from the current directory to OUT. Create an extra link to
  80. any system-defining files in the source to a convenient location. This is
  81. done before any compiling so that the compiled source locations will be
  82. valid."
  83. (let ((source (getcwd))
  84. (target (source-directory out name))
  85. (system-path (string-append out %system-install-prefix)))
  86. ;; SBCL keeps the modification time of the source file in the compiled
  87. ;; file, and the source files might just have been patched by a custom
  88. ;; phase. Therefore we reset the modification time of all the source
  89. ;; files before compiling.
  90. (for-each (lambda (file)
  91. (let ((s (lstat file)))
  92. (unless (or (eq? (stat:type s) 'symlink)
  93. (not (access? file W_OK)))
  94. (utime file 0 0 0 0))))
  95. (find-files source #:directories? #t))
  96. (copy-recursively source target #:keep-mtime? #t)
  97. (mkdir-p system-path)
  98. (for-each
  99. (lambda (file)
  100. (symlink file
  101. (string-append system-path "/" (basename file))))
  102. (find-files target "\\.asd$"))
  103. #t))
  104. (define* (install #:key inputs outputs #:allow-other-keys)
  105. "Copy and symlink all the source files.
  106. The source files are taken from the corresponding compile package (e.g. SBCL)
  107. if it's present in the native-inputs."
  108. (define output (assoc-ref outputs "out"))
  109. (define package-name
  110. (package-name->name+version
  111. (strip-store-file-name output)))
  112. (define (no-prefix pkgname)
  113. (let ((index (string-index pkgname #\-)))
  114. (if index
  115. (string-drop pkgname (1+ index))
  116. pkgname)))
  117. (define parent
  118. (match (assoc package-name inputs
  119. (lambda (key alist-car)
  120. (let* ((alt-key (no-prefix key))
  121. (alist-car (no-prefix alist-car)))
  122. (or (string=? alist-car key)
  123. (string=? alist-car alt-key)))))
  124. (#f #f)
  125. (p (cdr p))))
  126. (define parent-name
  127. (and parent
  128. (package-name->name+version (strip-store-file-name parent))))
  129. (define parent-source
  130. (and parent
  131. (string-append parent "/share/common-lisp/"
  132. (let ((index (string-index parent-name #\-)))
  133. (if index
  134. (string-take parent-name index)
  135. parent-name)))))
  136. (define (first-subdirectory directory) ; From gnu-build-system.
  137. "Return the file name of the first sub-directory of DIRECTORY."
  138. (match (scandir directory
  139. (lambda (file)
  140. (and (not (member file '("." "..")))
  141. (file-is-directory? (string-append directory "/"
  142. file)))))
  143. ((first . _) first)))
  144. (define source-directory
  145. (if (and parent-source
  146. (file-exists? parent-source))
  147. (string-append parent-source "/" (first-subdirectory parent-source))
  148. "."))
  149. (with-directory-excursion source-directory
  150. (copy-files-to-output output package-name)))
  151. (define* (copy-source #:key outputs asd-systems #:allow-other-keys)
  152. "Copy the source to the library output."
  153. (let* ((out (library-output outputs))
  154. (install-path (string-append out %source-install-prefix))
  155. (system-name (main-system-name out)))
  156. (copy-files-to-output out system-name)
  157. ;; Hide the files from asdf
  158. (with-directory-excursion install-path
  159. (rename-file "source" (%lisp-type))
  160. (delete-file-recursively "systems")))
  161. #t)
  162. (define* (configure #:key inputs #:allow-other-keys)
  163. ;; Create a directory having the configuration files for
  164. ;; all the dependencies in 'etc/common-lisp/'.
  165. (let ((out (string-append (getcwd) "/.cl-union")))
  166. (match inputs
  167. (((name . directories) ...)
  168. (union-build out (filter directory-exists? directories)
  169. #:create-all-directories? #t
  170. #:log-port (%make-void-port "w"))))
  171. (setenv "CL_UNION" out)
  172. (setenv "XDG_CONFIG_DIRS" (string-append out "/etc")))
  173. #t)
  174. (define* (build #:key outputs inputs asd-files asd-systems
  175. #:allow-other-keys)
  176. "Compile the system."
  177. (let* ((out (library-output outputs))
  178. (system-name (main-system-name out))
  179. (source-path (string-append out (%lisp-source-install-prefix)))
  180. (translations (wrap-output-translations
  181. `(,(output-translation source-path
  182. out))))
  183. (asd-files (find-asd-files out system-name asd-files)))
  184. (setenv "ASDF_OUTPUT_TRANSLATIONS"
  185. (replace-escaped-macros (format #f "~S" translations)))
  186. (setenv "HOME" out) ; ecl's asdf sometimes wants to create $HOME/.cache
  187. (compile-systems asd-systems asd-files))
  188. #t)
  189. (define* (check #:key tests? outputs inputs asd-files asd-systems
  190. test-asd-file
  191. #:allow-other-keys)
  192. "Test the system."
  193. (let* ((out (library-output outputs))
  194. (system-name (main-system-name out))
  195. (asd-files (find-asd-files out system-name asd-files))
  196. (test-asd-file
  197. (and=> test-asd-file
  198. (cut source-asd-file out system-name <>))))
  199. (if tests?
  200. (test-system (first asd-systems) asd-files test-asd-file)
  201. (format #t "test suite not run~%")))
  202. #t)
  203. (define* (create-asdf-configuration #:key inputs outputs #:allow-other-keys)
  204. "Create the ASDF configuration files for the built systems."
  205. (let* ((system-name (main-system-name (assoc-ref outputs "out")))
  206. (out (library-output outputs))
  207. (conf-dir (string-append out "/etc/common-lisp"))
  208. (deps-conf-dir (string-append (getenv "CL_UNION") "/etc/common-lisp"))
  209. (source-dir (lisp-source-directory out system-name))
  210. (lib-dir (string-append (library-directory out) "/" system-name)))
  211. (make-asdf-configuration system-name conf-dir deps-conf-dir
  212. source-dir lib-dir)
  213. #t))
  214. (define* (cleanup-files #:key outputs
  215. #:allow-other-keys)
  216. "Remove any compiled files which are not a part of the final bundle."
  217. (let* ((out (library-output outputs))
  218. (cache-directory (string-append out "/.cache")))
  219. ;; Remove the cache directory in case the lisp implementation wrote
  220. ;; something in there when compiling or testing a system.
  221. (when (directory-exists? cache-directory)
  222. (delete-file-recursively cache-directory)))
  223. #t)
  224. (define* (strip #:rest args)
  225. ;; stripping sbcl binaries removes their entry program and extra systems
  226. (or (string=? (%lisp-type) "sbcl")
  227. (apply (assoc-ref gnu:%standard-phases 'strip) args)))
  228. (define %standard-phases/source
  229. (modify-phases gnu:%standard-phases
  230. (delete 'bootstrap)
  231. (delete 'configure)
  232. (delete 'check)
  233. (delete 'build)
  234. (replace 'install install)))
  235. (define %standard-phases
  236. (modify-phases gnu:%standard-phases
  237. (delete 'bootstrap)
  238. (replace 'configure configure)
  239. (add-before 'configure 'copy-source copy-source)
  240. (replace 'build build)
  241. (replace 'check check)
  242. (add-after 'check 'create-asdf-configuration create-asdf-configuration)
  243. (add-after 'create-asdf-configuration 'cleanup cleanup-files)
  244. (delete 'install)
  245. (replace 'strip strip)))
  246. (define* (asdf-build #:key inputs
  247. (phases %standard-phases)
  248. #:allow-other-keys
  249. #:rest args)
  250. (apply gnu:gnu-build
  251. #:inputs inputs
  252. #:phases phases
  253. args))
  254. (define* (asdf-build/source #:key inputs
  255. (phases %standard-phases/source)
  256. #:allow-other-keys
  257. #:rest args)
  258. (apply gnu:gnu-build
  259. #:inputs inputs
  260. #:phases phases
  261. args))
  262. ;;; asdf-build-system.scm ends here