summaryrefslogtreecommitdiff
path: root/guix/build/gnu-cross-build.scm
blob: dab60684ac1b2c1927a47f77ee2aeea6f60b81d8 (plain)
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
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2013 Ludovic Courtès <ludo@gnu.org>
;;;
;;; 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 gnu-cross-build)
  #:use-module (guix build utils)
  #:use-module ((guix build gnu-build-system)
                #:renamer (symbol-prefix-proc 'build:))
  #:use-module (ice-9 ftw)
  #:use-module (ice-9 match)
  #:use-module (srfi srfi-1)
  #:export (%standard-cross-phases
            gnu-cross-build))

;;; Commentary:
;;;
;;; Extension of `gnu-build-system.scm' to support cross-compilation.
;;;
;;; Code:

(define* (set-paths #:key inputs native-inputs
                    (search-paths '()) (native-search-paths '())
                    #:allow-other-keys)
  (define input-directories
    (match inputs
      (((_ . dir) ...)
       dir)))

  (define native-input-directories
    (match native-inputs
      (((_ . dir) ...)
       dir)))

  ;; $PATH must refer only to native (host) inputs since target inputs are not
  ;; executable.
  (set-path-environment-variable "PATH" '("bin" "sbin")
                                 native-input-directories)

  ;; Search paths for target inputs.
  (for-each (match-lambda
             ((env-var (directories ...) separator)
              (set-path-environment-variable env-var directories
                                             input-directories
                                             #:separator separator)))
            search-paths)

  ;; Search paths for native inputs.
  (for-each (match-lambda
             ((env-var (directories ...) separator)
              (set-path-environment-variable env-var directories
                                             native-input-directories
                                             #:separator separator)))
            native-search-paths)

  ;; Dump the environment variables as a shell script, for handy debugging.
  (system "export > environment-variables"))

(define* (configure #:key
                    inputs outputs (configure-flags '()) out-of-source?
                    target native-inputs
                    #:allow-other-keys)
  (format #t "configuring for cross-compilation to `~a'~%" target)
  (apply (assoc-ref build:%standard-phases 'configure)
         #:configure-flags (cons (string-append "--host=" target)
                                 configure-flags)

         ;; XXX: The underlying `configure' phase looks for Bash among
         ;; #:inputs, so fool it this way.
         #:inputs native-inputs

         #:outputs outputs
         #:out-of-source? out-of-source?
         '()))

(define* (strip #:key target outputs (strip-binaries? #t)
                (strip-flags '("--strip-debug"))
                (strip-directories '("lib" "lib64" "libexec"
                                     "bin" "sbin"))
                #:allow-other-keys)
  ;; TODO: The only difference with `strip' in gnu-build-system.scm is the
  ;; name of the strip command; factorize it.

  (define (strip-dir dir)
    (format #t "stripping binaries in ~s with flags ~s~%"
            dir strip-flags)
    (file-system-fold (const #t)
                      (lambda (path stat result)  ; leaf
                        (zero? (apply system*
                                      (string-append target "-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))

  (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))))

(define %standard-cross-phases
  ;; The standard phases when cross-building.
  (let ((replacements `((set-paths ,set-paths)
                        (configure ,configure)
                        (strip ,strip))))
    (fold (lambda (replacement phases)
            (match replacement
              ((name proc)
               (alist-replace name proc phases))))
          (alist-delete 'check build:%standard-phases)
          replacements)))

;;; gnu-cross-build.scm ends here