emacs-build-system.scm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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$" "^[^/]*tests?\\.el$"))
  53. (define gnu:unpack (assoc-ref gnu:%standard-phases 'unpack))
  54. (define (store-file->elisp-source-file file)
  55. "Convert FILE, a store file name for an Emacs Lisp source file, into a file
  56. name that has been stripped of the hash and version number."
  57. (let ((suffix ".el"))
  58. (let-values (((name version)
  59. (package-name->name+version
  60. (basename
  61. (strip-store-file-name file) suffix))))
  62. (string-append name suffix))))
  63. (define* (unpack #:key source #:allow-other-keys)
  64. "Unpack SOURCE into the build directory. SOURCE may be a compressed
  65. archive, a directory, or an Emacs Lisp file."
  66. (if (string-suffix? ".el" source)
  67. (begin
  68. (mkdir "source")
  69. (chdir "source")
  70. (copy-file source (store-file->elisp-source-file source))
  71. #t)
  72. (gnu:unpack #:source source)))
  73. (define* (expand-load-path #:key (prepend-source? #t) #:allow-other-keys)
  74. "Expand EMACSLOADPATH, so that inputs, whose code resides in subdirectories,
  75. are properly found.
  76. If @var{prepend-source?} is @code{#t} (the default), also add the current
  77. directory to EMACSLOADPATH in front of any other directories."
  78. (let* ((source-directory (getcwd))
  79. (emacs-load-path (string-split (getenv "EMACSLOADPATH") #\:))
  80. (emacs-load-path*
  81. (map
  82. (lambda (dir)
  83. (match (scandir dir (negate (cute member <> '("." ".."))))
  84. ((sub) (string-append dir "/" sub))
  85. (_ dir)))
  86. emacs-load-path))
  87. (emacs-load-path-value (string-append
  88. (string-join
  89. (if prepend-source?
  90. (cons source-directory emacs-load-path*)
  91. emacs-load-path*)
  92. ":")
  93. ":")))
  94. (setenv "EMACSLOADPATH" emacs-load-path-value)
  95. (when prepend-source?
  96. (format #t "source directory ~s prepended to the `EMACSLOADPATH' \
  97. environment variable\n" source-directory))
  98. (let ((diff (lset-difference string=? emacs-load-path* emacs-load-path)))
  99. (unless (null? diff)
  100. (format #t "expanded load paths for ~{~a~^, ~}\n"
  101. (map basename diff))))))
  102. (define* (add-install-to-native-load-path #:key outputs #:allow-other-keys)
  103. "Append the native-site-lisp of OUTPUT to EMACSNATIVELOADPATH."
  104. (let ((native-load-path (or (false-if-exception
  105. (string-split (getenv "EMACSNATIVELOADPATH") #\:))
  106. '()))
  107. (install-directory (string-append (assoc-ref outputs "out")
  108. "/lib/emacs/native-site-lisp")))
  109. (setenv "EMACSNATIVELOADPATH"
  110. ;; Emacs pushes these directories in reverse order, so the
  111. ;; last one will be the first.
  112. (string-join `(,@native-load-path ,install-directory)
  113. ":"))))
  114. (define* (build #:key outputs inputs #:allow-other-keys)
  115. "Compile .el files."
  116. ;; Ensure that already compiled files in the working directory don't shadow
  117. ;; the build. Might happen, because check runs first.
  118. (for-each delete-file (find-files "." "\\.el[cn]$"))
  119. (let* ((emacs (search-input-file inputs "/bin/emacs"))
  120. (out (assoc-ref outputs "out")))
  121. (setenv "SHELL" "sh")
  122. (parameterize ((%emacs emacs))
  123. (emacs-compile-directory (elpa-directory out)))))
  124. (define* (patch-el-files #:key outputs #:allow-other-keys)
  125. "Substitute the absolute \"/bin/\" directory with the right location in the
  126. store in '.el' files."
  127. (let* ((out (assoc-ref outputs "out"))
  128. (elpa-name-ver (store-directory->elpa-name-version out))
  129. (el-dir (string-append out %install-dir "/" elpa-name-ver))
  130. (el-files (find-files (getcwd) "\\.el$")))
  131. (define (substitute-program-names)
  132. (substitute* el-files
  133. (("\"/bin/([^.]\\S*)\"" _ cmd-name)
  134. (let ((cmd (which cmd-name)))
  135. (unless cmd
  136. (error "patch-el-files: unable to locate " cmd-name))
  137. (string-append "\"" cmd "\"")))))
  138. (with-directory-excursion el-dir
  139. ;; Some old '.el' files (e.g., tex-buf.el in AUCTeX) are still
  140. ;; ISO-8859-1-encoded.
  141. (unless (false-if-exception (substitute-program-names))
  142. (with-fluids ((%default-port-encoding "ISO-8859-1"))
  143. (substitute-program-names))))
  144. #t))
  145. (define (find-root-library-file name)
  146. (let loop ((parts (string-split
  147. (package-name-version->elpa-name-version name) #\-))
  148. (candidate ""))
  149. (cond
  150. ;; at least one version part is given, so we don't terminate "early"
  151. ((null? parts) #f)
  152. ((string-null? candidate) (loop (cdr parts) (car parts)))
  153. ((file-exists? (string-append candidate ".el")) candidate)
  154. (else
  155. (loop (cdr parts) (string-append candidate "-" (car parts)))))))
  156. (define* (ensure-package-description #:key outputs #:allow-other-keys)
  157. (define (write-pkg-file name)
  158. (define summary-regexp
  159. "^;;; [^ ]*\\.el ---[ \t]*\\(.*?\\)[ \t]*\\(-\\*-.*-\\*-[ \t]*\\)?$")
  160. (define %write-pkg-file-form
  161. `(progn
  162. (require 'lisp-mnt)
  163. (require 'package)
  164. (defun build-package-desc-from-library (name)
  165. (package-desc-from-define
  166. name
  167. ;; Workaround for malformed version string (for example "24 (beta)"
  168. ;; in paredit.el), try to parse version obtained by lm-version,
  169. ;; before trying to create package-desc. Otherwise the whole process
  170. ;; of generation -pkg.el will fail.
  171. (condition-case
  172. nil
  173. (let ((version (lm-version)))
  174. ;; raises an error if version is invalid
  175. (and (version-to-list version) version))
  176. (error "0.0.0"))
  177. (or (save-excursion
  178. (goto-char (point-min))
  179. (and (re-search-forward ,summary-regexp nil t)
  180. (match-string-no-properties 1)))
  181. package--default-summary)
  182. (let ((require-lines (lm-header-multiline "package-requires")))
  183. (and require-lines
  184. (package--prepare-dependencies
  185. (package-read-from-string
  186. (mapconcat 'identity require-lines " ")))))
  187. :kind 'single
  188. :url (lm-homepage)
  189. :keywords (lm-keywords-list)
  190. :maintainer (lm-maintainer)
  191. :authors (lm-authors)))
  192. (defun generate-package-description-file (name)
  193. (package-generate-description-file
  194. (build-package-desc-from-library name)
  195. (concat name "-pkg.el")))
  196. (condition-case
  197. err
  198. (let ((name (file-name-base (buffer-file-name))))
  199. (generate-package-description-file name)
  200. (message (concat name "-pkg.el file generated.")))
  201. (error
  202. (message "There are some errors during generation of -pkg.el file:")
  203. (message "%s" (error-message-string err))))))
  204. (unless (file-exists? (string-append name "-pkg.el"))
  205. (emacs-batch-edit-file (string-append name ".el")
  206. %write-pkg-file-form)))
  207. (let* ((out (assoc-ref outputs "out"))
  208. (elpa-name-ver (store-directory->elpa-name-version out)))
  209. (with-directory-excursion (elpa-directory out)
  210. (and=> (find-root-library-file elpa-name-ver) write-pkg-file))))
  211. (define* (check #:key tests? (test-command '("make" "check"))
  212. (parallel-tests? #t) #:allow-other-keys)
  213. "Run the tests by invoking TEST-COMMAND.
  214. When TEST-COMMAND uses make and PARALLEL-TESTS is #t, the tests are run in
  215. parallel. PARALLEL-TESTS? is ignored when using a non-make TEST-COMMAND."
  216. (match-let (((test-program . args) test-command))
  217. (let ((using-make? (string=? test-program "make")))
  218. (if tests?
  219. (apply invoke test-program
  220. `(,@args
  221. ,@(if (and using-make? parallel-tests?)
  222. `("-j" ,(number->string (parallel-job-count)))
  223. '())))
  224. (begin
  225. (format #t "test suite not run~%")
  226. #t)))))
  227. (define* (install #:key outputs
  228. (include %default-include)
  229. (exclude %default-exclude)
  230. #:allow-other-keys)
  231. "Install the package contents."
  232. (define source (getcwd))
  233. (define* (install-file? file stat #:key verbose?)
  234. (let* ((stripped-file (string-trim
  235. (string-drop file (string-length source)) #\/)))
  236. (define (match-stripped-file action regex)
  237. (let ((result (string-match regex stripped-file)))
  238. (when (and result verbose?)
  239. (format #t "info: ~A ~A as it matches \"~A\"\n"
  240. stripped-file action regex))
  241. result))
  242. (when verbose?
  243. (format #t "info: considering installing ~A\n" stripped-file))
  244. (and (any (cut match-stripped-file "included" <>) include)
  245. (not (any (cut match-stripped-file "excluded" <>) exclude)))))
  246. (let* ((out (assoc-ref outputs "out"))
  247. (el-dir (elpa-directory out))
  248. (files-to-install (find-files source install-file?)))
  249. (cond
  250. ((not (null? files-to-install))
  251. (for-each
  252. (lambda (file)
  253. (let* ((stripped-file (string-drop file (string-length source)))
  254. (target-file (string-append el-dir stripped-file)))
  255. (format #t "`~a' -> `~a'~%" file target-file)
  256. (install-file file (dirname target-file))))
  257. files-to-install)
  258. #t)
  259. (else
  260. (format #t "error: No files found to install.\n")
  261. (find-files source (lambda (file stat)
  262. (install-file? file stat #:verbose? #t)))
  263. #f))))
  264. (define* (move-doc #:key outputs #:allow-other-keys)
  265. "Move info files from the ELPA package directory to the info directory."
  266. (let* ((out (assoc-ref outputs "out"))
  267. (site-lisp (string-append out %install-dir))
  268. (info-dir (string-append out "/share/info/"))
  269. (info-files (find-files site-lisp "\\.info$")))
  270. (unless (null? info-files)
  271. (mkdir-p info-dir)
  272. (with-directory-excursion site-lisp
  273. (when (file-exists? "dir") (delete-file "dir"))
  274. (for-each (lambda (f)
  275. (copy-file f (string-append info-dir "/" (basename f)))
  276. (delete-file f))
  277. info-files)))
  278. #t))
  279. (define* (make-autoloads #:key outputs inputs #:allow-other-keys)
  280. "Generate the autoloads file."
  281. (let* ((emacs (search-input-file inputs "/bin/emacs"))
  282. (out (assoc-ref outputs "out"))
  283. (elpa-name-ver (store-directory->elpa-name-version out))
  284. (elpa-name (package-name->name+version elpa-name-ver))
  285. (el-dir (elpa-directory out)))
  286. (parameterize ((%emacs emacs))
  287. (emacs-generate-autoloads elpa-name el-dir))))
  288. (define* (enable-autoloads-compilation #:key outputs #:allow-other-keys)
  289. "Remove the NO-BYTE-COMPILATION local variable embedded in the generated
  290. autoload files."
  291. (let* ((out (assoc-ref outputs "out"))
  292. (autoloads (find-files out "-autoloads.el$")))
  293. (substitute* autoloads
  294. ((";; no-byte-compile.*") ""))
  295. #t))
  296. (define* (validate-compiled-autoloads #:key outputs #:allow-other-keys)
  297. "Verify whether the byte compiled autoloads load fine."
  298. (let* ((out (assoc-ref outputs "out"))
  299. (autoloads (find-files out "-autoloads.elc$")))
  300. (emacs-batch-eval (format #f "(mapc #'load '~s)" autoloads))))
  301. (define (emacs-package? name)
  302. "Check if NAME correspond to the name of an Emacs package."
  303. (string-prefix? "emacs-" name))
  304. (define (package-name-version->elpa-name-version name-ver)
  305. "Convert the Guix package NAME-VER to the corresponding ELPA name-version
  306. format. Essentially drop the prefix used in Guix."
  307. (if (emacs-package? name-ver) ; checks for "emacs-" prefix
  308. (string-drop name-ver (string-length "emacs-"))
  309. name-ver))
  310. (define (store-directory->elpa-name-version store-dir)
  311. "Given a store directory STORE-DIR return the part of the basename after the
  312. second hyphen. This corresponds to 'name-version' as used in ELPA packages."
  313. ((compose package-name-version->elpa-name-version
  314. strip-store-file-name)
  315. store-dir))
  316. (define (elpa-directory store-dir)
  317. "Given the store directory STORE-DIR return the absolute install directory
  318. for libraries following the ELPA convention."
  319. (string-append store-dir %install-dir "/"
  320. (store-directory->elpa-name-version store-dir)))
  321. (define %standard-phases
  322. (modify-phases gnu:%standard-phases
  323. (replace 'unpack unpack)
  324. (add-after 'unpack 'expand-load-path expand-load-path)
  325. (add-after 'expand-load-path 'add-install-to-native-load-path
  326. add-install-to-native-load-path)
  327. (delete 'bootstrap)
  328. (delete 'configure)
  329. (delete 'build)
  330. (replace 'check check)
  331. (replace 'install install)
  332. (add-after 'install 'make-autoloads make-autoloads)
  333. (add-after 'make-autoloads 'enable-autoloads-compilation
  334. enable-autoloads-compilation)
  335. (add-after 'enable-autoloads-compilation 'patch-el-files patch-el-files)
  336. (add-after 'patch-el-files 'ensure-package-description
  337. ensure-package-description)
  338. ;; The .el files are byte compiled directly in the store.
  339. (add-after 'ensure-package-description 'build build)
  340. (add-after 'build 'validate-compiled-autoloads validate-compiled-autoloads)
  341. (add-after 'validate-compiled-autoloads 'move-doc move-doc)))
  342. (define* (emacs-build #:key inputs (phases %standard-phases)
  343. #:allow-other-keys #:rest args)
  344. "Build the given Emacs package, applying all of PHASES in order."
  345. (apply gnu:gnu-build
  346. #:inputs inputs #:phases phases
  347. args))
  348. ;;; emacs-build-system.scm ends here