2013-01-05 16:08:07 +01:00
|
|
|
|
;;; GNU Guix --- Functional package management for GNU
|
2013-01-10 00:08:40 +01:00
|
|
|
|
;;; Copyright © 2012, 2013 Ludovic Courtès <ludo@gnu.org>
|
2012-06-13 17:03:34 +02:00
|
|
|
|
;;;
|
2013-01-05 16:08:07 +01:00
|
|
|
|
;;; This file is part of GNU Guix.
|
2012-06-13 17:03:34 +02:00
|
|
|
|
;;;
|
2013-01-05 16:08:07 +01:00
|
|
|
|
;;; GNU Guix is free software; you can redistribute it and/or modify it
|
2012-06-13 17:03:34 +02:00
|
|
|
|
;;; 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.
|
|
|
|
|
;;;
|
2013-01-05 16:08:07 +01:00
|
|
|
|
;;; GNU Guix is distributed in the hope that it will be useful, but
|
2012-06-13 17:03:34 +02:00
|
|
|
|
;;; 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
|
2013-01-05 16:08:07 +01:00
|
|
|
|
;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
|
2012-06-13 17:03:34 +02:00
|
|
|
|
|
|
|
|
|
(define-module (guix build gnu-build-system)
|
|
|
|
|
#:use-module (guix build utils)
|
|
|
|
|
#:use-module (ice-9 ftw)
|
2012-06-16 16:56:47 +02:00
|
|
|
|
#:use-module (ice-9 match)
|
2012-12-20 22:31:08 +01:00
|
|
|
|
#:use-module (ice-9 format)
|
2012-06-16 16:56:47 +02:00
|
|
|
|
#:use-module (srfi srfi-1)
|
2012-08-19 17:54:54 +02:00
|
|
|
|
#:use-module (srfi srfi-26)
|
2012-06-16 16:56:47 +02:00
|
|
|
|
#:export (%standard-phases
|
|
|
|
|
gnu-build))
|
2012-06-13 17:03:34 +02:00
|
|
|
|
|
|
|
|
|
;; Commentary:
|
|
|
|
|
;;
|
|
|
|
|
;; Standard build procedure for packages using the GNU Build System or
|
|
|
|
|
;; something compatible ("./configure && make && make install"). This is the
|
|
|
|
|
;; builder-side code.
|
|
|
|
|
;;
|
|
|
|
|
;; Code:
|
|
|
|
|
|
|
|
|
|
(define (first-subdirectory dir)
|
|
|
|
|
"Return the path of the first sub-directory of DIR."
|
|
|
|
|
(file-system-fold (lambda (path stat result)
|
|
|
|
|
(string=? path dir))
|
|
|
|
|
(lambda (path stat result) result) ; leaf
|
|
|
|
|
(lambda (path stat result) result) ; down
|
|
|
|
|
(lambda (path stat result) result) ; up
|
|
|
|
|
(lambda (path stat result) ; skip
|
|
|
|
|
(or result path))
|
|
|
|
|
(lambda (path stat errno result) ; error
|
|
|
|
|
(error "first-subdirectory" (strerror errno)))
|
|
|
|
|
#f
|
|
|
|
|
dir))
|
|
|
|
|
|
2012-09-01 01:14:31 +02:00
|
|
|
|
(define* (set-paths #:key inputs (path-exclusions '())
|
|
|
|
|
#:allow-other-keys)
|
2012-09-06 22:58:43 +02:00
|
|
|
|
(define (relevant-input-directories env-var)
|
|
|
|
|
;; Return the subset of INPUTS that should be considered when setting
|
|
|
|
|
;; ENV-VAR.
|
|
|
|
|
(match (assoc-ref path-exclusions env-var)
|
|
|
|
|
(#f
|
|
|
|
|
(map cdr inputs))
|
|
|
|
|
((excluded ...)
|
|
|
|
|
(filter-map (match-lambda
|
|
|
|
|
((name . dir)
|
|
|
|
|
(and (not (member name excluded))
|
|
|
|
|
dir)))
|
|
|
|
|
inputs))))
|
2012-07-06 00:50:07 +02:00
|
|
|
|
|
2012-09-06 22:58:43 +02:00
|
|
|
|
(set-path-environment-variable "PATH" '("bin")
|
|
|
|
|
(relevant-input-directories "PATH"))
|
|
|
|
|
(set-path-environment-variable "CPATH" '("include")
|
|
|
|
|
(relevant-input-directories "CPATH"))
|
|
|
|
|
(set-path-environment-variable "LIBRARY_PATH" '("lib" "lib64")
|
|
|
|
|
(relevant-input-directories "LIBRARY_PATH"))
|
2012-07-07 18:40:39 +02:00
|
|
|
|
|
2012-09-06 22:58:43 +02:00
|
|
|
|
;; FIXME: Eventually move this to the `search-paths' field of the
|
|
|
|
|
;; `pkg-config' package.
|
|
|
|
|
(set-path-environment-variable "PKG_CONFIG_PATH"
|
2013-03-04 10:14:34 +01:00
|
|
|
|
'("lib/pkgconfig" "lib64/pkgconfig"
|
|
|
|
|
"share/pkgconfig")
|
2012-09-06 22:58:43 +02:00
|
|
|
|
(relevant-input-directories "PKG_CONFIG_PATH"))
|
|
|
|
|
|
|
|
|
|
;; Dump the environment variables as a shell script, for handy debugging.
|
|
|
|
|
(system "export > environment-variables"))
|
2012-06-13 17:03:34 +02:00
|
|
|
|
|
2012-06-16 16:56:47 +02:00
|
|
|
|
(define* (unpack #:key source #:allow-other-keys)
|
|
|
|
|
(and (zero? (system* "tar" "xvf" source))
|
|
|
|
|
(chdir (first-subdirectory "."))))
|
|
|
|
|
|
2012-12-15 16:35:26 +01:00
|
|
|
|
(define* (patch-source-shebangs #:key source #:allow-other-keys)
|
2012-12-21 22:31:25 +01:00
|
|
|
|
"Patch shebangs in all source files; this includes non-executable
|
|
|
|
|
files such as `.in' templates. Most scripts honor $SHELL and
|
|
|
|
|
$CONFIG_SHELL, but some don't, such as `mkinstalldirs' or Automake's
|
|
|
|
|
`missing' script."
|
|
|
|
|
(for-each patch-shebang
|
|
|
|
|
(remove file-is-directory? (find-files "." ".*"))))
|
|
|
|
|
|
|
|
|
|
(define (patch-generated-file-shebangs . rest)
|
|
|
|
|
"Patch shebangs in generated files, including `SHELL' variables in
|
|
|
|
|
makefiles."
|
|
|
|
|
;; Patch executable files, some of which might have been generated by
|
|
|
|
|
;; `configure'.
|
2012-12-15 16:35:26 +01:00
|
|
|
|
(for-each patch-shebang
|
|
|
|
|
(filter (lambda (file)
|
|
|
|
|
(and (executable-file? file)
|
|
|
|
|
(not (file-is-directory? file))))
|
|
|
|
|
(find-files "." ".*")))
|
|
|
|
|
|
2012-12-21 22:31:25 +01:00
|
|
|
|
;; Patch `SHELL' in generated makefiles.
|
|
|
|
|
(for-each patch-makefile-SHELL (find-files "." "^(GNU)?[mM]akefile$")))
|
2012-12-20 23:06:34 +01:00
|
|
|
|
|
2012-07-01 17:32:03 +02:00
|
|
|
|
(define* (patch #:key (patches '()) (patch-flags '("--batch" "-p1"))
|
|
|
|
|
#:allow-other-keys)
|
|
|
|
|
(every (lambda (p)
|
|
|
|
|
(format #t "applying patch `~a'~%" p)
|
|
|
|
|
(zero? (apply system* "patch"
|
2012-09-01 15:43:46 +02:00
|
|
|
|
(append patch-flags (list "--input" p)))))
|
2012-07-01 17:32:03 +02:00
|
|
|
|
patches))
|
|
|
|
|
|
2012-12-13 23:38:32 +01:00
|
|
|
|
(define* (configure #:key inputs outputs (configure-flags '()) out-of-source?
|
2012-08-23 23:13:41 +02:00
|
|
|
|
#:allow-other-keys)
|
2013-01-10 00:08:40 +01:00
|
|
|
|
(define (package-name)
|
|
|
|
|
(let* ((out (assoc-ref outputs "out"))
|
|
|
|
|
(base (basename out))
|
|
|
|
|
(dash (string-rindex base #\-)))
|
|
|
|
|
;; XXX: We'd rather use `package-name->name+version' or similar.
|
|
|
|
|
(if dash
|
|
|
|
|
(substring base 0 dash)
|
|
|
|
|
base)))
|
|
|
|
|
|
2012-07-01 17:32:03 +02:00
|
|
|
|
(let* ((prefix (assoc-ref outputs "out"))
|
2013-01-01 16:52:27 +01:00
|
|
|
|
(bindir (assoc-ref outputs "bin"))
|
2012-07-01 17:32:03 +02:00
|
|
|
|
(libdir (assoc-ref outputs "lib"))
|
|
|
|
|
(includedir (assoc-ref outputs "include"))
|
2013-01-10 00:08:40 +01:00
|
|
|
|
(docdir (assoc-ref outputs "doc"))
|
2012-12-13 23:38:32 +01:00
|
|
|
|
(bash (or (and=> (assoc-ref inputs "bash")
|
|
|
|
|
(cut string-append <> "/bin/bash"))
|
|
|
|
|
"/bin/sh"))
|
|
|
|
|
(flags `(,(string-append "CONFIG_SHELL=" bash)
|
|
|
|
|
,(string-append "SHELL=" bash)
|
|
|
|
|
,(string-append "--prefix=" prefix)
|
2012-07-01 17:32:03 +02:00
|
|
|
|
"--enable-fast-install" ; when using Libtool
|
|
|
|
|
|
|
|
|
|
;; Produce multiple outputs when specific output names
|
|
|
|
|
;; are recognized.
|
2013-01-01 16:52:27 +01:00
|
|
|
|
,@(if bindir
|
|
|
|
|
(list (string-append "--bindir=" bindir "/bin"))
|
|
|
|
|
'())
|
2012-07-01 17:32:03 +02:00
|
|
|
|
,@(if libdir
|
2013-01-10 00:08:40 +01:00
|
|
|
|
(cons (string-append "--libdir=" libdir "/lib")
|
|
|
|
|
(if includedir
|
|
|
|
|
'()
|
|
|
|
|
(list
|
|
|
|
|
(string-append "--includedir="
|
|
|
|
|
libdir "/include"))))
|
2012-07-01 17:32:03 +02:00
|
|
|
|
'())
|
|
|
|
|
,@(if includedir
|
|
|
|
|
(list (string-append "--includedir="
|
|
|
|
|
includedir "/include"))
|
|
|
|
|
'())
|
2013-01-10 00:08:40 +01:00
|
|
|
|
,@(if docdir
|
|
|
|
|
(list (string-append "--docdir=" docdir
|
|
|
|
|
"/doc/" (package-name)))
|
|
|
|
|
'())
|
2012-08-23 23:13:41 +02:00
|
|
|
|
,@configure-flags))
|
2012-08-30 23:30:42 +02:00
|
|
|
|
(abs-srcdir (getcwd))
|
|
|
|
|
(srcdir (if out-of-source?
|
|
|
|
|
(string-append "../" (basename abs-srcdir))
|
|
|
|
|
".")))
|
|
|
|
|
(format #t "source directory: ~s (relative from build: ~s)~%"
|
|
|
|
|
abs-srcdir srcdir)
|
2012-08-23 23:13:41 +02:00
|
|
|
|
(if out-of-source?
|
|
|
|
|
(begin
|
|
|
|
|
(mkdir "../build")
|
|
|
|
|
(chdir "../build")))
|
|
|
|
|
(format #t "build directory: ~s~%" (getcwd))
|
2012-07-01 17:32:03 +02:00
|
|
|
|
(format #t "configure flags: ~s~%" flags)
|
2012-08-30 23:30:42 +02:00
|
|
|
|
|
2012-12-13 23:38:32 +01:00
|
|
|
|
;; Use BASH to reduce reliance on /bin/sh since it may not always be
|
|
|
|
|
;; reliable (see
|
|
|
|
|
;; <http://thread.gmane.org/gmane.linux.distributions.nixos/9748>
|
|
|
|
|
;; for a summary of the situation.)
|
|
|
|
|
;;
|
2012-08-30 23:30:42 +02:00
|
|
|
|
;; Call `configure' with a relative path. Otherwise, GCC's build system
|
|
|
|
|
;; (for instance) records absolute source file names, which typically
|
|
|
|
|
;; contain the hash part of the `.drv' file, leading to a reference leak.
|
2012-12-13 23:38:32 +01:00
|
|
|
|
(zero? (apply system* bash
|
2012-08-30 23:30:42 +02:00
|
|
|
|
(string-append srcdir "/configure")
|
2012-08-23 23:13:41 +02:00
|
|
|
|
flags))))
|
2012-06-13 17:03:34 +02:00
|
|
|
|
|
2012-10-05 23:21:09 +02:00
|
|
|
|
(define %parallel-job-count
|
|
|
|
|
;; String to be passed next to GNU Make's `-j' argument.
|
|
|
|
|
(match (getenv "NIX_BUILD_CORES")
|
|
|
|
|
(#f "1")
|
|
|
|
|
("0" (number->string (current-processor-count)))
|
|
|
|
|
(x x)))
|
|
|
|
|
|
2012-07-07 16:49:23 +02:00
|
|
|
|
(define* (build #:key (make-flags '()) (parallel-build? #t)
|
|
|
|
|
#:allow-other-keys)
|
|
|
|
|
(zero? (apply system* "make"
|
|
|
|
|
`(,@(if parallel-build?
|
2012-10-05 23:21:09 +02:00
|
|
|
|
`("-j" ,%parallel-job-count)
|
2012-07-07 16:49:23 +02:00
|
|
|
|
'())
|
|
|
|
|
,@make-flags))))
|
2012-06-16 16:56:47 +02:00
|
|
|
|
|
2012-07-01 17:32:03 +02:00
|
|
|
|
(define* (check #:key (make-flags '()) (tests? #t) (test-target "check")
|
2012-07-07 16:49:23 +02:00
|
|
|
|
(parallel-tests? #t)
|
2012-07-01 17:32:03 +02:00
|
|
|
|
#:allow-other-keys)
|
|
|
|
|
(if tests?
|
2012-07-07 16:49:23 +02:00
|
|
|
|
(zero? (apply system* "make" test-target
|
|
|
|
|
`(,@(if parallel-tests?
|
2012-10-05 23:21:09 +02:00
|
|
|
|
`("-j" ,%parallel-job-count)
|
2012-07-07 16:49:23 +02:00
|
|
|
|
'())
|
|
|
|
|
,@make-flags)))
|
2012-07-01 17:32:03 +02:00
|
|
|
|
(begin
|
|
|
|
|
(format #t "test suite not run~%")
|
|
|
|
|
#t)))
|
2012-06-16 16:56:47 +02:00
|
|
|
|
|
|
|
|
|
(define* (install #:key (make-flags '()) #:allow-other-keys)
|
|
|
|
|
(zero? (apply system* "make" "install" make-flags)))
|
|
|
|
|
|
2012-08-19 17:54:54 +02:00
|
|
|
|
(define* (patch-shebangs #:key outputs (patch-shebangs? #t)
|
|
|
|
|
#:allow-other-keys)
|
|
|
|
|
(define (list-of-files dir)
|
|
|
|
|
(map (cut string-append dir "/" <>)
|
|
|
|
|
(or (scandir dir (lambda (f)
|
|
|
|
|
(let ((s (stat (string-append dir "/" f))))
|
|
|
|
|
(eq? 'regular (stat:type s)))))
|
|
|
|
|
'())))
|
|
|
|
|
|
|
|
|
|
(define bindirs
|
|
|
|
|
(append-map (match-lambda
|
|
|
|
|
((_ . dir)
|
|
|
|
|
(list (string-append dir "/bin")
|
|
|
|
|
(string-append dir "/sbin"))))
|
|
|
|
|
outputs))
|
|
|
|
|
|
2012-08-31 23:58:21 +02:00
|
|
|
|
(when patch-shebangs?
|
|
|
|
|
(let ((path (append bindirs
|
|
|
|
|
(search-path-as-string->list (getenv "PATH")))))
|
|
|
|
|
(for-each (lambda (dir)
|
|
|
|
|
(let ((files (list-of-files dir)))
|
|
|
|
|
(for-each (cut patch-shebang <> path) files)))
|
|
|
|
|
bindirs)))
|
|
|
|
|
#t)
|
2012-08-19 17:54:54 +02:00
|
|
|
|
|
2012-08-31 17:04:53 +02:00
|
|
|
|
(define* (strip #:key outputs (strip-binaries? #t)
|
|
|
|
|
(strip-flags '("--strip-debug"))
|
|
|
|
|
(strip-directories '("lib" "lib64" "libexec"
|
|
|
|
|
"bin" "sbin"))
|
|
|
|
|
#:allow-other-keys)
|
|
|
|
|
(define (strip-dir dir)
|
2012-08-31 23:58:21 +02:00
|
|
|
|
(format #t "stripping binaries in ~s with flags ~s~%"
|
|
|
|
|
dir strip-flags)
|
2012-08-31 17:04:53 +02:00
|
|
|
|
(file-system-fold (const #t)
|
|
|
|
|
(lambda (path stat result) ; leaf
|
|
|
|
|
(zero? (apply system* "strip"
|
|
|
|
|
(append strip-flags (list path)))))
|
|
|
|
|
(const #t) ; down
|
|
|
|
|
(const #t) ; up
|
|
|
|
|
(const #t) ; skip
|
|
|
|
|
(lambda (path stat errno result)
|
|
|
|
|
(format (current-error-port)
|
|
|
|
|
"strip: failed to access `~a': ~a~%"
|
|
|
|
|
path (strerror errno))
|
|
|
|
|
#f)
|
|
|
|
|
#t
|
|
|
|
|
dir))
|
|
|
|
|
|
2012-08-31 23:58:21 +02:00
|
|
|
|
(or (not strip-binaries?)
|
|
|
|
|
(every strip-dir
|
|
|
|
|
(append-map (match-lambda
|
|
|
|
|
((_ . dir)
|
|
|
|
|
(filter-map (lambda (d)
|
|
|
|
|
(let ((sub (string-append dir "/" d)))
|
|
|
|
|
(and (directory-exists? sub) sub)))
|
|
|
|
|
strip-directories)))
|
|
|
|
|
outputs))))
|
2012-08-31 17:04:53 +02:00
|
|
|
|
|
2012-06-16 16:56:47 +02:00
|
|
|
|
(define %standard-phases
|
|
|
|
|
;; Standard build phases, as a list of symbol/procedure pairs.
|
|
|
|
|
(let-syntax ((phases (syntax-rules ()
|
|
|
|
|
((_ p ...) `((p . ,p) ...)))))
|
2012-12-20 23:06:34 +01:00
|
|
|
|
(phases set-paths unpack patch
|
2012-12-21 22:31:25 +01:00
|
|
|
|
patch-source-shebangs configure patch-generated-file-shebangs
|
2012-12-15 16:35:26 +01:00
|
|
|
|
build check install
|
2012-08-31 17:04:53 +02:00
|
|
|
|
patch-shebangs strip)))
|
2012-06-16 16:56:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define* (gnu-build #:key (source #f) (outputs #f) (inputs #f)
|
|
|
|
|
(phases %standard-phases)
|
|
|
|
|
#:allow-other-keys
|
|
|
|
|
#:rest args)
|
|
|
|
|
"Build from SOURCE to OUTPUTS, using INPUTS, and by running all of PHASES
|
|
|
|
|
in order. Return #t if all the PHASES succeeded, #f otherwise."
|
|
|
|
|
(setvbuf (current-output-port) _IOLBF)
|
2012-12-15 16:01:52 +01:00
|
|
|
|
(setvbuf (current-error-port) _IOLBF)
|
2012-06-16 16:56:47 +02:00
|
|
|
|
|
|
|
|
|
;; The trick is to #:allow-other-keys everywhere, so that each procedure in
|
|
|
|
|
;; PHASES can pick the keyword arguments it's interested in.
|
|
|
|
|
(every (match-lambda
|
|
|
|
|
((name . proc)
|
2012-12-20 22:31:08 +01:00
|
|
|
|
(let ((start (gettimeofday)))
|
|
|
|
|
(format #t "starting phase `~a'~%" name)
|
|
|
|
|
(let ((result (apply proc args))
|
|
|
|
|
(end (gettimeofday)))
|
|
|
|
|
(format #t "phase `~a' ~:[failed~;succeeded~] after ~a seconds~%"
|
|
|
|
|
name result (- (car end) (car start)))
|
|
|
|
|
result))))
|
2012-06-16 16:56:47 +02:00
|
|
|
|
phases))
|