asdf-build-system.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016, 2017 Andy Patterson <ajpatter@uwaterloo.ca>
  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 asdf-build-system)
  19. #:use-module ((guix build gnu-build-system) #:prefix gnu:)
  20. #:use-module (guix build utils)
  21. #:use-module (guix build lisp-utils)
  22. #:use-module (srfi srfi-1)
  23. #:use-module (srfi srfi-11)
  24. #:use-module (srfi srfi-26)
  25. #:use-module (ice-9 rdelim)
  26. #:use-module (ice-9 receive)
  27. #:use-module (ice-9 regex)
  28. #:use-module (ice-9 match)
  29. #:use-module (ice-9 format)
  30. #:use-module (ice-9 ftw)
  31. #:export (%standard-phases
  32. %standard-phases/source
  33. asdf-build
  34. asdf-build/source))
  35. ;; Commentary:
  36. ;;
  37. ;; System for building ASDF packages; creating executable programs and images
  38. ;; from them.
  39. ;;
  40. ;; Code:
  41. (define %object-prefix "/lib")
  42. (define (%lisp-source-install-prefix)
  43. (string-append %source-install-prefix "/" (%lisp-type) "-source"))
  44. (define %system-install-prefix
  45. (string-append %source-install-prefix "/systems"))
  46. (define (lisp-source-directory output name)
  47. (string-append output (%lisp-source-install-prefix) "/" name))
  48. (define (source-directory output name)
  49. (string-append output %source-install-prefix "/source/" name))
  50. (define (library-directory output)
  51. (string-append output %object-prefix
  52. "/" (%lisp-type)))
  53. (define (output-translation source-path
  54. object-output)
  55. "Return a translation for the system's source path
  56. to it's binary output."
  57. `((,source-path
  58. :**/ :*.*.*)
  59. (,(library-directory object-output)
  60. :**/ :*.*.*)))
  61. (define (source-asd-file output name asd-file)
  62. (string-append (lisp-source-directory output name) "/" asd-file))
  63. (define (copy-files-to-output out name)
  64. "Copy all files from the current directory to OUT. Create an extra link to
  65. any system-defining files in the source to a convenient location. This is
  66. done before any compiling so that the compiled source locations will be
  67. valid."
  68. (let ((source (getcwd))
  69. (target (source-directory out name))
  70. (system-path (string-append out %system-install-prefix)))
  71. ;; SBCL keeps the modification time of the source file in the compiled
  72. ;; file, and the source files might just have been patched by a custom
  73. ;; phase. Therefore we reset the modification time of all the source
  74. ;; files before compiling.
  75. (for-each (lambda (file)
  76. (let ((s (lstat file)))
  77. (unless (or (eq? (stat:type s) 'symlink)
  78. (not (access? file W_OK)))
  79. (utime file 0 0 0 0))))
  80. (find-files source #:directories? #t))
  81. (copy-recursively source target #:keep-mtime? #t)
  82. (mkdir-p system-path)
  83. (for-each
  84. (lambda (file)
  85. (symlink file
  86. (string-append system-path "/" (basename file))))
  87. (find-files target "\\.asd$"))
  88. #t))
  89. (define* (install #:key inputs outputs #:allow-other-keys)
  90. "Copy and symlink all the source files.
  91. The source files are taken from the corresponding compile package (e.g. SBCL)
  92. if it's present in the native-inputs."
  93. (define output (assoc-ref outputs "out"))
  94. (define package-name
  95. (package-name->name+version
  96. (strip-store-file-name output)))
  97. (define (no-prefix pkgname)
  98. (if (string-index pkgname #\-)
  99. (string-drop pkgname (1+ (string-index pkgname #\-)))
  100. pkgname))
  101. (define parent
  102. (match (assoc package-name inputs
  103. (lambda (key alist-car)
  104. (let* ((alt-key (no-prefix key))
  105. (alist-car (no-prefix alist-car)))
  106. (or (string=? alist-car key)
  107. (string=? alist-car alt-key)))))
  108. (#f #f)
  109. (p (cdr p))))
  110. (define parent-name
  111. (and parent
  112. (package-name->name+version (strip-store-file-name parent))))
  113. (define parent-source
  114. (and parent
  115. (string-append parent "/share/common-lisp/"
  116. (string-take parent-name
  117. (string-index parent-name #\-))
  118. "-source")))
  119. (define (first-subdirectory directory) ; From gnu-build-system.
  120. "Return the file name of the first sub-directory of DIRECTORY."
  121. (match (scandir directory
  122. (lambda (file)
  123. (and (not (member file '("." "..")))
  124. (file-is-directory? (string-append directory "/"
  125. file)))))
  126. ((first . _) first)))
  127. (define source-directory
  128. (if (and parent-source
  129. (file-exists? parent-source))
  130. (string-append parent-source "/" (first-subdirectory parent-source))
  131. "."))
  132. (with-directory-excursion source-directory
  133. (copy-files-to-output output package-name)))
  134. (define* (copy-source #:key outputs asd-system-name #:allow-other-keys)
  135. "Copy the source to the library output."
  136. (let* ((out (library-output outputs))
  137. (install-path (string-append out %source-install-prefix)))
  138. (copy-files-to-output out asd-system-name)
  139. ;; Hide the files from asdf
  140. (with-directory-excursion install-path
  141. (rename-file "source" (string-append (%lisp-type) "-source"))
  142. (delete-file-recursively "systems")))
  143. #t)
  144. (define* (build #:key outputs inputs asd-file asd-system-name
  145. #:allow-other-keys)
  146. "Compile the system."
  147. (let* ((out (library-output outputs))
  148. (source-path (lisp-source-directory out asd-system-name))
  149. (translations (wrap-output-translations
  150. `(,(output-translation source-path
  151. out))))
  152. (asd-file (source-asd-file out asd-system-name asd-file)))
  153. (setenv "ASDF_OUTPUT_TRANSLATIONS"
  154. (replace-escaped-macros (format #f "~S" translations)))
  155. (setenv "HOME" out) ; ecl's asdf sometimes wants to create $HOME/.cache
  156. (compile-system asd-system-name asd-file)
  157. ;; As above, ecl will sometimes create this even though it doesn't use it
  158. (let ((cache-directory (string-append out "/.cache")))
  159. (when (directory-exists? cache-directory)
  160. (delete-file-recursively cache-directory))))
  161. #t)
  162. (define* (check #:key tests? outputs inputs asd-file asd-system-name
  163. test-asd-file
  164. #:allow-other-keys)
  165. "Test the system."
  166. (let* ((out (library-output outputs))
  167. (asd-file (source-asd-file out asd-system-name asd-file))
  168. (test-asd-file
  169. (and=> test-asd-file
  170. (cut source-asd-file out asd-system-name <>))))
  171. (if tests?
  172. (test-system asd-system-name asd-file test-asd-file)
  173. (format #t "test suite not run~%")))
  174. #t)
  175. (define* (create-asd-file #:key outputs
  176. inputs
  177. asd-file
  178. asd-system-name
  179. #:allow-other-keys)
  180. "Create a system definition file for the built system."
  181. (let*-values (((out) (library-output outputs))
  182. ((_ version) (package-name->name+version
  183. (strip-store-file-name out)))
  184. ((new-asd-file) (string-append
  185. (library-directory out)
  186. "/" (normalize-string asd-system-name)
  187. ".asd")))
  188. (make-asd-file new-asd-file
  189. #:system asd-system-name
  190. #:version version
  191. #:inputs inputs
  192. #:system-asd-file asd-file))
  193. #t)
  194. (define* (symlink-asd-files #:key outputs #:allow-other-keys)
  195. "Create an extra reference to the system in a convenient location."
  196. (let* ((out (library-output outputs)))
  197. (for-each
  198. (lambda (asd-file)
  199. (receive (new-asd-file asd-file-directory)
  200. (bundle-asd-file out asd-file)
  201. (mkdir-p asd-file-directory)
  202. (symlink asd-file new-asd-file)
  203. ;; Update the source registry for future phases which might want to
  204. ;; use the newly compiled system.
  205. (prepend-to-source-registry
  206. (string-append asd-file-directory "/"))))
  207. (find-files (string-append out %object-prefix) "\\.asd$")))
  208. #t)
  209. (define* (cleanup-files #:key outputs
  210. #:allow-other-keys)
  211. "Remove any compiled files which are not a part of the final bundle."
  212. (let ((out (library-output outputs)))
  213. (match (%lisp-type)
  214. ("sbcl"
  215. (for-each
  216. (lambda (file)
  217. (unless (string-suffix? "--system.fasl" file)
  218. (delete-file file)))
  219. (find-files out "\\.fasl$")))
  220. ("ecl"
  221. (for-each delete-file
  222. (append (find-files out "\\.fas$")
  223. (find-files out "\\.o$")))))
  224. (with-directory-excursion (library-directory out)
  225. (for-each
  226. (lambda (file)
  227. (rename-file file
  228. (string-append "./" (basename file))))
  229. (find-files "."))
  230. (for-each delete-file-recursively
  231. (scandir "."
  232. (lambda (file)
  233. (and
  234. (directory-exists? file)
  235. (string<> "." file)
  236. (string<> ".." file)))))))
  237. #t)
  238. (define* (strip #:rest args)
  239. ;; stripping sbcl binaries removes their entry program and extra systems
  240. (or (string=? (%lisp-type) "sbcl")
  241. (apply (assoc-ref gnu:%standard-phases 'strip) args)))
  242. (define %standard-phases/source
  243. (modify-phases gnu:%standard-phases
  244. (delete 'bootstrap)
  245. (delete 'configure)
  246. (delete 'check)
  247. (delete 'build)
  248. (replace 'install install)))
  249. (define %standard-phases
  250. (modify-phases gnu:%standard-phases
  251. (delete 'bootstrap)
  252. (delete 'configure)
  253. (delete 'install)
  254. (replace 'build build)
  255. (add-before 'build 'copy-source copy-source)
  256. (replace 'check check)
  257. (replace 'strip strip)
  258. (add-after 'check 'create-asd-file create-asd-file)
  259. (add-after 'create-asd-file 'cleanup cleanup-files)
  260. (add-after 'cleanup 'create-symlinks symlink-asd-files)))
  261. (define* (asdf-build #:key inputs
  262. (phases %standard-phases)
  263. #:allow-other-keys
  264. #:rest args)
  265. (apply gnu:gnu-build
  266. #:inputs inputs
  267. #:phases phases
  268. args))
  269. (define* (asdf-build/source #:key inputs
  270. (phases %standard-phases/source)
  271. #:allow-other-keys
  272. #:rest args)
  273. (apply gnu:gnu-build
  274. #:inputs inputs
  275. #:phases phases
  276. args))
  277. ;;; asdf-build-system.scm ends here