emacs-build-system.scm 12 KB

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