build-system/gnu: Add 'bootstrap' phase.
[jackhill/guix/guix.git] / guix / build / dub-build-system.scm
CommitLineData
65e862d1
DM
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2016 David Craven <david@craven.ch>
3;;; Copyright © 2017 Danny Milosavljevic <dannym@scratchpost.org>
980bcecd 4;;; Copyright © 2018 Tobias Geerinckx-Rice <me@tobias.gr>
65e862d1
DM
5;;;
6;;; This file is part of GNU Guix.
7;;;
8;;; GNU Guix is free software; you can redistribute it and/or modify it
9;;; under the terms of the GNU General Public License as published by
10;;; the Free Software Foundation; either version 3 of the License, or (at
11;;; your option) any later version.
12;;;
13;;; GNU Guix is distributed in the hope that it will be useful, but
14;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16;;; GNU General Public License for more details.
17;;;
18;;; You should have received a copy of the GNU General Public License
19;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20
21(define-module (guix build dub-build-system)
22 #:use-module ((guix build gnu-build-system) #:prefix gnu:)
23 #:use-module (guix build syscalls)
24 #:use-module (guix build utils)
25 #:use-module (ice-9 popen)
26 #:use-module (ice-9 rdelim)
27 #:use-module (ice-9 ftw)
28 #:use-module (ice-9 format)
29 #:use-module (ice-9 match)
30 #:use-module (rnrs io ports)
31 #:use-module (srfi srfi-1)
32 #:use-module (srfi srfi-26)
33 #:export (%standard-phases
34 dub-build))
35
36;; Commentary:
37;;
38;; Builder-side code of the DUB (the build tool for D) build system.
39;;
40;; Code:
41
42;; FIXME: Needs to be parsed from url not package name.
43(define (package-name->d-package-name name)
44 "Return the package name of NAME."
45 (match (string-split name #\-)
46 (("d" rest ...)
47 (string-join rest "-"))
48 (_ #f)))
49
50(define* (configure #:key inputs #:allow-other-keys)
51 "Prepare one new directory with all the required dependencies.
52 It's necessary to do this (instead of just using /gnu/store as the
53 directory) because we want to hide the libraries in subdirectories
54 lib/dub/... instead of polluting the user's profile root."
55 (let* ((dir (mkdtemp! "/tmp/dub.XXXXXX"))
56 (vendor-dir (string-append dir "/vendor")))
57 (setenv "HOME" dir)
58 (mkdir vendor-dir)
59 (for-each
60 (match-lambda
61 ((name . path)
62 (let* ((d-package (package-name->d-package-name name))
63 (d-basename (basename path)))
64 (when (and d-package path)
65 (match (string-split (basename path) #\-)
66 ((_ ... version)
67 (symlink (string-append path "/lib/dub/" d-basename)
68 (string-append vendor-dir "/" d-basename))))))))
69 inputs)
70 (zero? (system* "dub" "add-path" vendor-dir))))
71
72(define (grep string file-name)
980bcecd
TGR
73 "Find the first occurrence of STRING in the file named FILE-NAME.
74 Return the position of this occurrence, or #f if none was found."
65e862d1
DM
75 (string-contains (call-with-input-file file-name get-string-all)
76 string))
77
78(define (grep* string file-name)
980bcecd
TGR
79 "Find the first occurrence of STRING in the file named FILE-NAME.
80 Return the position of this occurrence, or #f if none was found.
65e862d1
DM
81 If the file named FILE-NAME doesn't exist, return #f."
82 (catch 'system-error
83 (lambda ()
84 (grep string file-name))
85 (lambda args
86 #f)))
87
88(define* (build #:key (dub-build-flags '())
89 #:allow-other-keys)
90 "Build a given DUB package."
91 (if (or (grep* "sourceLibrary" "package.json")
92 (grep* "sourceLibrary" "dub.sdl") ; note: format is different!
93 (grep* "sourceLibrary" "dub.json"))
94 #t
6a1a6967
DM
95 (let ((status (zero? (apply system* `("dub" "build" ,@dub-build-flags)))))
96 (substitute* ".dub/dub.json"
97 (("\"lastUpgrade\": \"[^\"]*\"")
98 "\"lastUpgrade\": \"1970-01-01T00:00:00.0000000\""))
99 status)))
65e862d1
DM
100
101(define* (check #:key tests? #:allow-other-keys)
102 (if tests?
6a1a6967
DM
103 (let ((status (zero? (system* "dub" "test"))))
104 (substitute* ".dub/dub.json"
105 (("\"lastUpgrade\": \"[^\"]*\"")
106 "\"lastUpgrade\": \"1970-01-01T00:00:00.0000000\""))
107 status)
65e862d1
DM
108 #t))
109
110(define* (install #:key inputs outputs #:allow-other-keys)
111 "Install a given DUB package."
112 (let* ((out (assoc-ref outputs "out"))
113 (outbin (string-append out "/bin"))
114 (outlib (string-append out "/lib/dub/" (basename out))))
115 (mkdir-p outbin)
116 ;; TODO remove "-test-application"
117 (copy-recursively "bin" outbin)
118 (mkdir-p outlib)
119 (copy-recursively "." (string-append outlib))
120 #t))
121
122(define %standard-phases
123 (modify-phases gnu:%standard-phases
189be331 124 (delete 'bootstrap)
65e862d1
DM
125 (replace 'configure configure)
126 (replace 'build build)
127 (replace 'check check)
128 (replace 'install install)))
129
130(define* (dub-build #:key inputs (phases %standard-phases)
131 #:allow-other-keys #:rest args)
132 "Build the given DUB package, applying all of PHASES in order."
133 (apply gnu:gnu-build #:inputs inputs #:phases phases args))