1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016 David Craven <david@craven.ch>
3 ;;; Copyright © 2017 Danny Milosavljevic <dannym@scratchpost.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 (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
37 ;; Builder-side code of the DUB (the build tool for D) build system.
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 #\-)
46 (string-join rest "-"))
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")))
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) #\-)
66 (symlink (string-append path "/lib/dub/" d-basename)
67 (string-append vendor-dir "/" d-basename))))))))
69 (zero? (system* "dub" "add-path" vendor-dir))))
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)
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."
83 (grep string file-name))
87 (define* (build #:key (dub-build-flags '())
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"))
94 (let ((status (zero? (apply system* `("dub" "build" ,@dub-build-flags)))))
95 (system* "dub" "run") ; might fail for "targetType": "library"
98 (define* (check #:key tests? #:allow-other-keys)
100 (zero? (system* "dub" "test"))
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))))
109 ;; TODO remove "-test-application"
110 (copy-recursively "bin" outbin)
112 (copy-recursively "." (string-append outlib))
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)))
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))