emacs-build-system.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015 Federico Beffa <beffa@fbengineering.ch>
  3. ;;; Copyright © 2016 David Thompson <davet@gnu.org>
  4. ;;; Copyright © 2016 Alex Kost <alezost@gmail.com>
  5. ;;; Copyright © 2018, 2019 Maxim Cournoyer <maxim.cournoyer@gmail.com>
  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 emacs-build-system)
  22. #:use-module ((guix build gnu-build-system) #:prefix gnu:)
  23. #:use-module ((guix build utils) #:hide (delete))
  24. #:use-module (guix build emacs-utils)
  25. #:use-module (srfi srfi-1)
  26. #:use-module (srfi srfi-11)
  27. #:use-module (srfi srfi-26)
  28. #:use-module (ice-9 rdelim)
  29. #:use-module (ice-9 regex)
  30. #:use-module (ice-9 match)
  31. #:export (%standard-phases
  32. %default-include
  33. %default-exclude
  34. emacs-build))
  35. ;; Commentary:
  36. ;;
  37. ;; Builder-side code of the build procedure for ELPA Emacs packages.
  38. ;;
  39. ;; Code:
  40. ;;; All the packages are installed directly under site-lisp, which means that
  41. ;;; having that directory in the EMACSLOADPATH is enough to have them found by
  42. ;;; Emacs.
  43. (define %install-dir "/share/emacs/site-lisp")
  44. ;; These are the default inclusion/exclusion regexps for the install phase.
  45. (define %default-include '("^[^/]*\\.el$" "^[^/]*\\.info$" "^doc/.*\\.info$"))
  46. (define %default-exclude '("^\\.dir-locals\\.el$" "-pkg\\.el$"
  47. "^[^/]*tests?\\.el$"))
  48. (define gnu:unpack (assoc-ref gnu:%standard-phases 'unpack))
  49. (define (store-file->elisp-source-file file)
  50. "Convert FILE, a store file name for an Emacs Lisp source file, into a file
  51. name that has been stripped of the hash and version number."
  52. (let ((suffix ".el"))
  53. (let-values (((name version)
  54. (package-name->name+version
  55. (basename
  56. (strip-store-file-name file) suffix))))
  57. (string-append name suffix))))
  58. (define* (unpack #:key source #:allow-other-keys)
  59. "Unpack SOURCE into the build directory. SOURCE may be a compressed
  60. archive, a directory, or an Emacs Lisp file."
  61. (if (string-suffix? ".el" source)
  62. (begin
  63. (mkdir "source")
  64. (chdir "source")
  65. (copy-file source (store-file->elisp-source-file source))
  66. #t)
  67. (gnu:unpack #:source source)))
  68. (define* (add-source-to-load-path #:key dummy #:allow-other-keys)
  69. "Augment the EMACSLOADPATH environment variable with the source directory."
  70. (let* ((source-directory (getcwd))
  71. (emacs-load-path (string-split (getenv "EMACSLOADPATH") #\:))
  72. ;; XXX: Make sure the Emacs core libraries appear at the end of
  73. ;; EMACSLOADPATH, to avoid shadowing any other libraries depended
  74. ;; upon.
  75. (emacs-load-path-non-core (filter (cut string-contains <>
  76. "/share/emacs/site-lisp")
  77. emacs-load-path))
  78. (emacs-load-path-value (string-append
  79. (string-join (cons source-directory
  80. emacs-load-path-non-core)
  81. ":")
  82. ":")))
  83. (setenv "EMACSLOADPATH" emacs-load-path-value)
  84. (format #t "source directory ~s prepended to the `EMACSLOADPATH' \
  85. environment variable\n" source-directory)))
  86. (define* (build #:key outputs inputs #:allow-other-keys)
  87. "Compile .el files."
  88. (let* ((emacs (string-append (assoc-ref inputs "emacs") "/bin/emacs"))
  89. (out (assoc-ref outputs "out"))
  90. (site-lisp (string-append out %install-dir)))
  91. (setenv "SHELL" "sh")
  92. (parameterize ((%emacs emacs))
  93. (emacs-byte-compile-directory site-lisp))))
  94. (define* (patch-el-files #:key outputs #:allow-other-keys)
  95. "Substitute the absolute \"/bin/\" directory with the right location in the
  96. store in '.el' files."
  97. (define (file-contains-nul-char? file)
  98. (call-with-input-file file
  99. (lambda (in)
  100. (let loop ((line (read-line in 'concat)))
  101. (cond
  102. ((eof-object? line) #f)
  103. ((string-index line #\nul) #t)
  104. (else (loop (read-line in 'concat))))))
  105. #:binary #t))
  106. (let* ((out (assoc-ref outputs "out"))
  107. (site-lisp (string-append out %install-dir))
  108. ;; (ice-9 regex) uses libc's regexp routines, which cannot deal with
  109. ;; strings containing NULs. Filter out such files. TODO: Remove
  110. ;; this workaround when <https://bugs.gnu.org/30116> is fixed.
  111. (el-files (remove file-contains-nul-char?
  112. (find-files (getcwd) "\\.el$"))))
  113. (define (substitute-program-names)
  114. (substitute* el-files
  115. (("\"/bin/([^.]\\S*)\"" _ cmd-name)
  116. (let ((cmd (which cmd-name)))
  117. (unless cmd
  118. (error "patch-el-files: unable to locate " cmd-name))
  119. (string-append "\"" cmd "\"")))))
  120. (with-directory-excursion site-lisp
  121. ;; Some old '.el' files (e.g., tex-buf.el in AUCTeX) are still
  122. ;; ISO-8859-1-encoded.
  123. (unless (false-if-exception (substitute-program-names))
  124. (with-fluids ((%default-port-encoding "ISO-8859-1"))
  125. (substitute-program-names))))
  126. #t))
  127. (define* (check #:key tests? (test-command '("make" "check"))
  128. (parallel-tests? #t) #:allow-other-keys)
  129. "Run the tests by invoking TEST-COMMAND.
  130. When TEST-COMMAND uses make and PARALLEL-TESTS is #t, the tests are run in
  131. parallel. PARALLEL-TESTS? is ignored when using a non-make TEST-COMMAND."
  132. (match-let (((test-program . args) test-command))
  133. (let ((using-make? (string=? test-program "make")))
  134. (if tests?
  135. (apply invoke test-program
  136. `(,@args
  137. ,@(if (and using-make? parallel-tests?)
  138. `("-j" ,(number->string (parallel-job-count)))
  139. '())))
  140. (begin
  141. (format #t "test suite not run~%")
  142. #t)))))
  143. (define* (install #:key outputs
  144. (include %default-include)
  145. (exclude %default-exclude)
  146. #:allow-other-keys)
  147. "Install the package contents."
  148. (define source (getcwd))
  149. (define* (install-file? file stat #:key verbose?)
  150. (let* ((stripped-file (string-trim
  151. (string-drop file (string-length source)) #\/)))
  152. (define (match-stripped-file action regex)
  153. (let ((result (string-match regex stripped-file)))
  154. (when (and result verbose?)
  155. (format #t "info: ~A ~A as it matches \"~A\"\n"
  156. stripped-file action regex))
  157. result))
  158. (when verbose?
  159. (format #t "info: considering installing ~A\n" stripped-file))
  160. (and (any (cut match-stripped-file "included" <>) include)
  161. (not (any (cut match-stripped-file "excluded" <>) exclude)))))
  162. (let* ((out (assoc-ref outputs "out"))
  163. (site-lisp (string-append out %install-dir))
  164. (files-to-install (find-files source install-file?)))
  165. (cond
  166. ((not (null? files-to-install))
  167. (for-each
  168. (lambda (file)
  169. (let* ((stripped-file (string-drop file (string-length source)))
  170. (target-file (string-append site-lisp stripped-file)))
  171. (format #t "`~a' -> `~a'~%" file target-file)
  172. (install-file file (dirname target-file))))
  173. files-to-install)
  174. #t)
  175. (else
  176. (format #t "error: No files found to install.\n")
  177. (find-files source (lambda (file stat)
  178. (install-file? file stat #:verbose? #t)))
  179. #f))))
  180. (define* (move-doc #:key outputs #:allow-other-keys)
  181. "Move info files from the ELPA package directory to the info directory."
  182. (let* ((out (assoc-ref outputs "out"))
  183. (site-lisp (string-append out %install-dir))
  184. (info-dir (string-append out "/share/info/"))
  185. (info-files (find-files site-lisp "\\.info$")))
  186. (unless (null? info-files)
  187. (mkdir-p info-dir)
  188. (with-directory-excursion site-lisp
  189. (when (file-exists? "dir") (delete-file "dir"))
  190. (for-each (lambda (f)
  191. (copy-file f (string-append info-dir "/" (basename f)))
  192. (delete-file f))
  193. info-files)))
  194. #t))
  195. (define* (make-autoloads #:key outputs inputs #:allow-other-keys)
  196. "Generate the autoloads file."
  197. (let* ((emacs (string-append (assoc-ref inputs "emacs") "/bin/emacs"))
  198. (out (assoc-ref outputs "out"))
  199. (site-lisp (string-append out %install-dir))
  200. (elpa-name-ver (store-directory->elpa-name-version out))
  201. (elpa-name (package-name->name+version elpa-name-ver)))
  202. (parameterize ((%emacs emacs))
  203. (emacs-generate-autoloads elpa-name site-lisp))))
  204. (define* (enable-autoloads-compilation #:key outputs #:allow-other-keys)
  205. "Remove the NO-BYTE-COMPILATION local variable embedded in the generated
  206. autoload files."
  207. (let* ((out (assoc-ref outputs "out"))
  208. (autoloads (find-files out "-autoloads.el$")))
  209. (substitute* autoloads
  210. ((";; no-byte-compile.*") ""))
  211. #t))
  212. (define* (validate-compiled-autoloads #:key outputs #:allow-other-keys)
  213. "Verify whether the byte compiled autoloads load fine."
  214. (let* ((out (assoc-ref outputs "out"))
  215. (autoloads (find-files out "-autoloads.elc$")))
  216. (emacs-batch-eval (format #f "(mapc #'load '~s)" autoloads))))
  217. (define (emacs-package? name)
  218. "Check if NAME correspond to the name of an Emacs package."
  219. (string-prefix? "emacs-" name))
  220. (define (package-name-version->elpa-name-version name-ver)
  221. "Convert the Guix package NAME-VER to the corresponding ELPA name-version
  222. format. Essentially drop the prefix used in Guix."
  223. (if (emacs-package? name-ver) ; checks for "emacs-" prefix
  224. (string-drop name-ver (string-length "emacs-"))
  225. name-ver))
  226. (define (store-directory->elpa-name-version store-dir)
  227. "Given a store directory STORE-DIR return the part of the basename after the
  228. second hyphen. This corresponds to 'name-version' as used in ELPA packages."
  229. ((compose package-name-version->elpa-name-version
  230. strip-store-file-name)
  231. store-dir))
  232. (define %standard-phases
  233. (modify-phases gnu:%standard-phases
  234. (replace 'unpack unpack)
  235. (add-after 'unpack 'add-source-to-load-path add-source-to-load-path)
  236. (delete 'bootstrap)
  237. (delete 'configure)
  238. (delete 'build)
  239. (replace 'check check)
  240. (replace 'install install)
  241. (add-after 'install 'make-autoloads make-autoloads)
  242. (add-after 'make-autoloads 'enable-autoloads-compilation
  243. enable-autoloads-compilation)
  244. (add-after 'enable-autoloads-compilation 'patch-el-files patch-el-files)
  245. ;; The .el files are byte compiled directly in the store.
  246. (add-after 'patch-el-files 'build build)
  247. (add-after 'build 'validate-compiled-autoloads validate-compiled-autoloads)
  248. (add-after 'validate-compiled-autoloads 'move-doc move-doc)))
  249. (define* (emacs-build #:key inputs (phases %standard-phases)
  250. #:allow-other-keys #:rest args)
  251. "Build the given Emacs package, applying all of PHASES in order."
  252. (apply gnu:gnu-build
  253. #:inputs inputs #:phases phases
  254. args))
  255. ;;; emacs-build-system.scm ends here