unofficial mirror of bug-guix@gnu.org 
 help / color / mirror / code / Atom feed
blob 2846926298f3da5dae4894939d6a6a5c0c7caa39 14147 bytes (raw)
name: guix/build/emacs-build-system.scm 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
 
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2015 Federico Beffa <beffa@fbengineering.ch>
;;; Copyright © 2016 David Thompson <davet@gnu.org>
;;; Copyright © 2016 Alex Kost <alezost@gmail.com>
;;; Copyright © 2018, 2019, 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (guix build emacs-build-system)
  #:use-module ((guix build gnu-build-system) #:prefix gnu:)
  #:use-module ((guix build utils) #:hide (delete))
  #:use-module (guix build emacs-utils)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-11)
  #:use-module (srfi srfi-26)
  #:use-module (ice-9 rdelim)
  #:use-module (ice-9 regex)
  #:use-module (ice-9 match)
  #:export (%standard-phases
            %default-include
            %default-exclude
            emacs-build
            outputs->elpa-install-dir))

;; Commentary:
;;
;; Builder-side code of the build procedure for ELPA Emacs packages.
;;
;; Code:

;;; All the packages are installed under their own directory under
;;; site-lisp/guix, to prevent file name collisions in profiles.  This means
;;; that some machinery is needed to activate the packages, since they aren't
;;; directly on the load path.  This is taken care of by the
;;; 'guix-package-initialize' procedure called from the site-start.el file
;;; shipped with Emacs from Guix.
(define %install-prefix "/share/emacs/site-lisp/guix")

;; These are the default inclusion/exclusion regexps for the install phase.
(define %default-include '("^[^/]*\\.el$" "^[^/]*\\.info$" "^doc/.*\\.info$"))
(define %default-exclude '("^\\.dir-locals\\.el$" "-pkg\\.el$"
                           "^[^/]*tests?\\.el$"))

(define gnu:unpack (assoc-ref gnu:%standard-phases 'unpack))

(define (store-file->elisp-source-file file)
  "Convert FILE, a store file name for an Emacs Lisp source file, into a file
name that has been stripped of the hash and version number."
  (let ((suffix ".el"))
    (let-values (((name version)
                  (package-name->name+version
                   (basename
                    (strip-store-file-name file) suffix))))
      (string-append name suffix))))

(define (version->elpa-version version)
  "Sanitize VERSION into an ELPA-compatible version.

package.el uses the 'version-to-list' procedure from the 'subr' Emacs library,
which supports a very limited set of version snapshot annotations, defined in
the 'version-regexp-alist' variable of the same library.

Strip the least significant components of a version string, so that a Guix
version such as \"0.4.1-1.a41d5cc\" becomes the valid ELPA version \"0.4.1\".
In case a valid ELPA version cannot be derived from VERSION, \"0.0.0\" is
returned."
  (let ((m (string-match "[0-9]+(\\.[0-9]+)*" version)))
    (if m
        (match:substring m)
        (begin
          (format (current-error-port)
                  "warning: failed producing an ELPA-compatible \
version string from ~s; using \"0.0.0\"~%" version)
          "0.0.0"))))

(define* (unpack #:key source #:allow-other-keys)
  "Unpack SOURCE into the build directory.  SOURCE may be a compressed
archive, a directory, or an Emacs Lisp file."
  (if (string-suffix? ".el" source)
      (begin
        (mkdir "source")
        (chdir "source")
        (copy-file source (store-file->elisp-source-file source))
        #t)
      (gnu:unpack #:source source)))

(define* (add-source-to-load-path #:key dummy #:allow-other-keys)
  "Augment the EMACSLOADPATH environment variable with the source directory."
  (let* ((source-directory (getcwd))
         (emacs-load-path (string-split (getenv "EMACSLOADPATH") #\:))
         ;; XXX: Make sure the Emacs core libraries appear at the end of
         ;; EMACSLOADPATH, to avoid shadowing any other libraries depended
         ;; upon.
         (emacs-load-path-non-core (filter (cut string-contains <>
                                                "/share/emacs/site-lisp")
                                           emacs-load-path))
         (emacs-load-path-value (string-append
                                 (string-join (cons source-directory
                                                    emacs-load-path-non-core)
                                              ":")
                                 ":")))
    (setenv "EMACSLOADPATH" emacs-load-path-value)
    (format #t "source directory ~s prepended to the `EMACSLOADPATH' \
environment variable\n" source-directory)))

(define* (build #:key outputs inputs #:allow-other-keys)
  "Byte compile .el files."
  (let ((emacs (string-append (assoc-ref inputs "emacs") "/bin/emacs"))
        (install-dir (outputs->elpa-install-dir outputs)))
    (setenv "SHELL" "sh")
    (parameterize ((%emacs emacs))
      (emacs-byte-compile-directory install-dir))))

(define* (patch-el-files #:key outputs #:allow-other-keys)
  "Substitute the absolute \"/bin/\" directory with the right location in the
store in '.el' files."

  (define (file-contains-nul-char? file)
    (call-with-input-file file
      (lambda (in)
        (let loop ((line (read-line in 'concat)))
          (cond
           ((eof-object? line) #f)
           ((string-index line #\nul) #t)
           (else (loop (read-line in 'concat))))))
      #:binary #t))

  (let ((install-dir (outputs->elpa-install-dir outputs))
        ;; (ice-9 regex) uses libc's regexp routines, which cannot deal with
        ;; strings containing NULs.  Filter out such files.  TODO: Remove
        ;; this workaround when <https://bugs.gnu.org/30116> is fixed.
        (el-files (remove file-contains-nul-char?
                          (find-files (getcwd) "\\.el$"))))
    (define (substitute-program-names)
      (substitute* el-files
        (("\"/bin/([^.]\\S*)\"" _ cmd-name)
         (let ((cmd (which cmd-name)))
           (unless cmd
             (error "patch-el-files: unable to locate " cmd-name))
           (string-append "\"" cmd "\"")))))

    (with-directory-excursion install-dir
      ;; Some old '.el' files (e.g., tex-buf.el in AUCTeX) are still
      ;; ISO-8859-1-encoded.
      (unless (false-if-exception (substitute-program-names))
        (with-fluids ((%default-port-encoding "ISO-8859-1"))
          (substitute-program-names))))
    #t))

(define* (check #:key tests? (test-command '("make" "check"))
                (parallel-tests? #t) #:allow-other-keys)
  "Run the tests by invoking TEST-COMMAND.

When TEST-COMMAND uses make and PARALLEL-TESTS is #t, the tests are run in
parallel. PARALLEL-TESTS? is ignored when using a non-make TEST-COMMAND."
  (match-let (((test-program . args) test-command))
    (let ((using-make? (string=? test-program "make")))
      (if tests?
          (apply invoke test-program
                 `(,@args
                   ,@(if (and using-make? parallel-tests?)
                         `("-j" ,(number->string (parallel-job-count)))
                         '())))
          (begin
            (format #t "test suite not run~%")
            #t)))))

(define* (install #:key outputs
                  (include %default-include)
                  (exclude %default-exclude)
                  #:allow-other-keys)
  "Install the package contents."

  (define source (getcwd))

  (define* (install-file? file stat #:key verbose?)
    (let* ((stripped-file (string-trim
                           (string-drop file (string-length source)) #\/)))
      (define (match-stripped-file action regex)
        (let ((result (string-match regex stripped-file)))
          (when (and result verbose?)
                (format #t "info: ~A ~A as it matches \"~A\"\n"
                        stripped-file action regex))
          result))

      (when verbose?
            (format #t "info: considering installing ~A\n" stripped-file))

      (and (any (cut match-stripped-file "included" <>) include)
           (not (any (cut match-stripped-file "excluded" <>) exclude)))))

  (let ((install-dir (outputs->elpa-install-dir outputs))
         (files-to-install (find-files source install-file?)))
    (cond
     ((not (null? files-to-install))
      (for-each
       (lambda (file)
         (let* ((stripped-file (string-drop file (string-length source)))
                (target-file (string-append install-dir stripped-file)))
           (format #t "`~a' -> `~a'~%" file target-file)
           (install-file file (dirname target-file))))
       files-to-install)
      #t)
     (else
      (format #t "error: No files found to install.\n")
      (find-files source (lambda (file stat)
                           (install-file? file stat #:verbose? #t)))
      #f))))

(define* (create-pkg.el #:key outputs #:allow-other-keys)
  "Generate the name-pkg.el file required by package.el."
  (let*-values (((install-dir)   (outputs->elpa-install-dir outputs))
                ((elpa-name-ver) (store-directory->elpa-name-version
                                  (assoc-ref outputs "out")))
                ((name version)  (package-name->name+version elpa-name-ver))
                ((elpa-version)  (version->elpa-version version)))
    (call-with-output-file (string-append install-dir "/" name "-pkg.el")
      (lambda (port)
        ;; Note: the file cannot be byte compiled, as the loader expects a
        ;; single form present, which leaves no option to include (require
        ;; 'package).
        (display ";; Generated by Guix -*- no-byte-compile: t -*-\n" port)
        (write `(define-package ,name ,elpa-version) port)
        (display "\n" port)
        #t))))

(define* (move-doc #:key outputs #:allow-other-keys)
  "Move info files from the ELPA package directory to the info directory."
  (let* ((install-dir (outputs->elpa-install-dir outputs))
         (info-dir (string-append (assoc-ref outputs "out") "/share/info/"))
         (info-files (find-files install-dir "\\.info$")))
    (unless (null? info-files)
      (mkdir-p info-dir)
      (with-directory-excursion install-dir
        (when (file-exists? "dir") (delete-file "dir"))
        (for-each (lambda (f)
                    (copy-file f (string-append info-dir "/" (basename f)))
                    (delete-file f))
                  info-files)))
    #t))

(define* (make-autoloads #:key outputs inputs #:allow-other-keys)
  "Generate the autoloads file."
  (let* ((emacs (string-append (assoc-ref inputs "emacs") "/bin/emacs"))
         (install-dir (outputs->elpa-install-dir outputs))
         (elpa-name-ver (store-directory->elpa-name-version
                         (assoc-ref outputs "out")))
         (elpa-name (package-name->name+version elpa-name-ver)))
    (parameterize ((%emacs emacs))
      (emacs-generate-autoloads elpa-name install-dir))))

(define* (enable-autoloads-compilation #:key outputs #:allow-other-keys)
  "Remove the NO-BYTE-COMPILATION local variable embedded in the generated
autoload files."
  (let* ((out (assoc-ref outputs "out"))
         (autoloads (find-files out "-autoloads.el$")))
    (substitute* autoloads
      ((";; no-byte-compile.*") ""))
    #t))

(define* (validate-compiled-autoloads #:key outputs #:allow-other-keys)
  "Verify whether the byte compiled autoloads load fine."
  (let* ((out (assoc-ref outputs "out"))
         (autoloads (find-files out "-autoloads.elc$")))
    (emacs-batch-eval (format #f "(mapc #'load '~s)" autoloads))))

(define (emacs-package? name)
  "Check if NAME correspond to the name of an Emacs package."
  (string-prefix? "emacs-" name))

(define (package-name-version->elpa-name-version name-ver)
  "Convert the Guix package NAME-VER to the corresponding ELPA name-version
format.  Essentially drop the prefix used in Guix."
  (if (emacs-package? name-ver)  ; checks for "emacs-" prefix
      (string-drop name-ver (string-length "emacs-"))
      name-ver))

(define (store-directory->elpa-name-version store-dir)
  "Given a store directory STORE-DIR return the part of the basename after the
second hyphen.  This corresponds to 'name-version' as used in ELPA packages."
  ((compose package-name-version->elpa-name-version
            strip-store-file-name)
   store-dir))

(define (outputs->elpa-install-dir outputs)
  "Given OUTPUTS, a package outputs, return the ELPA-like Guix installation
directory of the Emacs package."
  (let ((out (assoc-ref outputs "out")))
    (string-append out %install-prefix "/"
                   (store-directory->elpa-name-version out))))

(define %standard-phases
  (modify-phases gnu:%standard-phases
    (replace 'unpack unpack)
    (add-after 'unpack 'add-source-to-load-path add-source-to-load-path)
    (delete 'bootstrap)
    (delete 'configure)
    (delete 'build)
    (replace 'check check)
    (replace 'install install)
    (add-after 'install 'create-pkg.el create-pkg.el)
    (add-after 'create-pkg.el 'make-autoloads make-autoloads)
    (add-after 'make-autoloads 'enable-autoloads-compilation
      enable-autoloads-compilation)
    (add-after 'enable-autoloads-compilation 'patch-el-files patch-el-files)
    ;; The .el files are byte compiled directly in the store.
    (add-after 'patch-el-files 'build build)
    (add-after 'build 'validate-compiled-autoloads validate-compiled-autoloads)
    (add-after 'validate-compiled-autoloads 'move-doc move-doc)))

(define* (emacs-build #:key inputs (phases %standard-phases)
                      #:allow-other-keys #:rest args)
  "Build the given Emacs package, applying all of PHASES in order."
  (apply gnu:gnu-build
         #:inputs inputs #:phases phases
         args))

;;; emacs-build-system.scm ends here

debug log:

solving 2846926298 ...
found 2846926298 in https://yhetil.org/guix-bugs/20201218221723.26829-1-maxim.cournoyer@gmail.com/
found 26ea59bc25 in https://git.savannah.gnu.org/cgit/guix.git
preparing index
index prepared:
100644 26ea59bc25f4f238dc8b84df11c3d1ad606425e3	guix/build/emacs-build-system.scm

applying [1/1] https://yhetil.org/guix-bugs/20201218221723.26829-1-maxim.cournoyer@gmail.com/
diff --git a/guix/build/emacs-build-system.scm b/guix/build/emacs-build-system.scm
index 26ea59bc25..2846926298 100644

Checking patch guix/build/emacs-build-system.scm...
Applied patch guix/build/emacs-build-system.scm cleanly.

index at:
100644 2846926298f3da5dae4894939d6a6a5c0c7caa39	guix/build/emacs-build-system.scm

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).