1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016 Taylan Ulrich Bayırlı/Kammer <taylanbayirli@gmail.com>
3 ;;; Copyright © 2016, 2017, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (use-modules (ice-9 format)
27 (define host (getenv "host"))
28 (define srcdir (getenv "srcdir"))
30 (define (relative-file file)
31 (if (string-prefix? (string-append srcdir "/") file)
32 (string-drop file (+ 1 (string-length srcdir)))
35 (define (file-mtime<? f1 f2)
36 (< (stat:mtime (stat f1))
37 (stat:mtime (stat f2))))
39 (define (scm->go file)
40 (let* ((relative (relative-file file))
41 (without-extension (string-drop-right relative 4)))
42 (string-append without-extension ".go")))
44 (define (file-needs-compilation? file)
45 (let ((go (scm->go file)))
46 (or (not (file-exists? go))
47 (file-mtime<? go file))))
49 (define* (parallel-job-count #:optional (flags (getenv "MAKEFLAGS")))
50 "Return the number of parallel jobs as determined by FLAGS, the flags passed
53 (#f (current-processor-count))
55 (let ((initial-flags (string-tokenize flags)))
56 (let loop ((flags initial-flags))
59 ;; Note: GNU make prior to version 4.2 would hide "-j" flags from
60 ;; $MAKEFLAGS. Thus, check for a "--jobserver" flag here and
61 ;; assume we're using all cores if specified.
62 (if (any (lambda (flag)
63 (string-prefix? "--jobserver" flag))
65 (current-processor-count) ;GNU make < 4.2
67 (("-j" (= string->number count) _ ...)
70 (current-processor-count)))
72 (if (string-prefix? "-j" head)
73 (match (string-drop head 2)
75 (current-processor-count))
76 ((= string->number count)
79 (current-processor-count))))
82 (define (parallel-job-count*)
83 ;; XXX: Work around memory requirements not sustainable on i686 above '-j4'
84 ;; or so: <https://bugs.gnu.org/40522>.
85 (let ((count (parallel-job-count)))
86 (if (string-prefix? "i686" %host-type)
90 (define (% completed total)
91 "Return the completion percentage of COMPLETED over TOTAL as an integer."
92 (inexact->exact (round (* 100. (/ completed total)))))
94 ;; Install a SIGINT handler to give unwind handlers in 'compile-file' an
95 ;; opportunity to run upon SIGINT and to remove temporary output files.
100 (match (command-line)
101 ((_ "--total" (= string->number grand-total)
102 "--completed" (= string->number processed)
104 ;; GRAND-TOTAL is the total number of .scm files in the project; PROCESSED
105 ;; is the total number of .scm files already compiled in previous
106 ;; invocations of this script.
109 (let* ((to-build (filter file-needs-compilation? files))
110 (processed (+ processed
111 (- (length files) (length to-build)))))
112 (compile-files srcdir (getcwd) to-build
113 #:workers (parallel-job-count*)
115 #:report-load (lambda (file total completed)
117 (format #t "[~3d%] LOAD ~a~%"
123 #:report-compilation (lambda (file total completed)
125 (format #t "[~3d%] GUILEC ~a~%"
126 (% (+ total completed 1
134 ;; Try to report the error in an intelligible way.
135 (let* ((stack (make-stack #t))
136 (frame (if (> (stack-length stack) 1)
137 (stack-ref stack 1) ;skip the 'throw' frame
138 (stack-ref stack 0)))
139 (ui (false-if-exception
140 (resolve-module '(guix ui))))
143 (module-ref ui 'report-load-error)))))
145 (report (or (and=> (current-load-port) port-filename) "?.scm")
148 (print-exception (current-error-port) frame
149 (car args) (cdr args))
150 (display-backtrace stack (current-error-port)))))))))