gnu: Add dub-build-system.
[jackhill/guix/guix.git] / guix / build / dub-build-system.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016 David Craven <david@craven.ch>
3 ;;; Copyright © 2017 Danny Milosavljevic <dannym@scratchpost.org>
4 ;;;
5 ;;; This file is part of GNU Guix.
6 ;;;
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.
11 ;;;
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.
16 ;;;
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/>.
19
20 (define-module (guix build dub-build-system)
21 #:use-module ((guix build gnu-build-system) #:prefix gnu:)
22 #:use-module (guix build syscalls)
23 #:use-module (guix build utils)
24 #:use-module (ice-9 popen)
25 #:use-module (ice-9 rdelim)
26 #:use-module (ice-9 ftw)
27 #:use-module (ice-9 format)
28 #:use-module (ice-9 match)
29 #:use-module (rnrs io ports)
30 #:use-module (srfi srfi-1)
31 #:use-module (srfi srfi-26)
32 #:export (%standard-phases
33 dub-build))
34
35 ;; Commentary:
36 ;;
37 ;; Builder-side code of the DUB (the build tool for D) build system.
38 ;;
39 ;; Code:
40
41 ;; FIXME: Needs to be parsed from url not package name.
42 (define (package-name->d-package-name name)
43 "Return the package name of NAME."
44 (match (string-split name #\-)
45 (("d" rest ...)
46 (string-join rest "-"))
47 (_ #f)))
48
49 (define* (configure #:key inputs #:allow-other-keys)
50 "Prepare one new directory with all the required dependencies.
51 It's necessary to do this (instead of just using /gnu/store as the
52 directory) because we want to hide the libraries in subdirectories
53 lib/dub/... instead of polluting the user's profile root."
54 (let* ((dir (mkdtemp! "/tmp/dub.XXXXXX"))
55 (vendor-dir (string-append dir "/vendor")))
56 (setenv "HOME" dir)
57 (mkdir vendor-dir)
58 (for-each
59 (match-lambda
60 ((name . path)
61 (let* ((d-package (package-name->d-package-name name))
62 (d-basename (basename path)))
63 (when (and d-package path)
64 (match (string-split (basename path) #\-)
65 ((_ ... version)
66 (symlink (string-append path "/lib/dub/" d-basename)
67 (string-append vendor-dir "/" d-basename))))))))
68 inputs)
69 (zero? (system* "dub" "add-path" vendor-dir))))
70
71 (define (grep string file-name)
72 "Find the first occurence of STRING in the file named FILE-NAME.
73 Return the position of this occurence, or #f if none was found."
74 (string-contains (call-with-input-file file-name get-string-all)
75 string))
76
77 (define (grep* string file-name)
78 "Find the first occurence of STRING in the file named FILE-NAME.
79 Return the position of this occurence, or #f if none was found.
80 If the file named FILE-NAME doesn't exist, return #f."
81 (catch 'system-error
82 (lambda ()
83 (grep string file-name))
84 (lambda args
85 #f)))
86
87 (define* (build #:key (dub-build-flags '())
88 #:allow-other-keys)
89 "Build a given DUB package."
90 (if (or (grep* "sourceLibrary" "package.json")
91 (grep* "sourceLibrary" "dub.sdl") ; note: format is different!
92 (grep* "sourceLibrary" "dub.json"))
93 #t
94 (let ((status (zero? (apply system* `("dub" "build" ,@dub-build-flags)))))
95 (system* "dub" "run") ; might fail for "targetType": "library"
96 status)))
97
98 (define* (check #:key tests? #:allow-other-keys)
99 (if tests?
100 (zero? (system* "dub" "test"))
101 #t))
102
103 (define* (install #:key inputs outputs #:allow-other-keys)
104 "Install a given DUB package."
105 (let* ((out (assoc-ref outputs "out"))
106 (outbin (string-append out "/bin"))
107 (outlib (string-append out "/lib/dub/" (basename out))))
108 (mkdir-p outbin)
109 ;; TODO remove "-test-application"
110 (copy-recursively "bin" outbin)
111 (mkdir-p outlib)
112 (copy-recursively "." (string-append outlib))
113 #t))
114
115 (define %standard-phases
116 (modify-phases gnu:%standard-phases
117 (replace 'configure configure)
118 (replace 'build build)
119 (replace 'check check)
120 (replace 'install install)))
121
122 (define* (dub-build #:key inputs (phases %standard-phases)
123 #:allow-other-keys #:rest args)
124 "Build the given DUB package, applying all of PHASES in order."
125 (apply gnu:gnu-build #:inputs inputs #:phases phases args))