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 ;;; Copyright © 2018 Tobias Geerinckx-Rice <me@tobias.gr>
6 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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
38 ;; Builder-side code of the DUB (the build tool for D) build system.
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 #\-)
47 (string-join rest "-"))
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")))
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) #\-)
67 (symlink (string-append path "/lib/dub/" d-basename)
68 (string-append vendor-dir "/" d-basename))))))))
70 (invoke "dub" "add-path" vendor-dir)
73 (define (grep string file-name)
74 "Find the first occurrence of STRING in the file named FILE-NAME.
75 Return the position of this occurrence, or #f if none was found."
76 (string-contains (call-with-input-file file-name get-string-all)
79 (define (grep* string file-name)
80 "Find the first occurrence of STRING in the file named FILE-NAME.
81 Return the position of this occurrence, or #f if none was found.
82 If the file named FILE-NAME doesn't exist, return #f."
85 (grep string file-name))
89 (define* (build #:key (dub-build-flags '())
91 "Build a given DUB package."
92 (unless (or (grep* "sourceLibrary" "package.json")
93 (grep* "sourceLibrary" "dub.sdl") ; note: format is different!
94 (grep* "sourceLibrary" "dub.json"))
95 (apply invoke `("dub" "build" ,@dub-build-flags))
96 (substitute* ".dub/dub.json"
97 (("\"lastUpgrade\": \"[^\"]*\"")
98 "\"lastUpgrade\": \"1970-01-01T00:00:00.0000000\"")))
101 (define* (check #:key tests? #:allow-other-keys)
103 (invoke "dub" "test")
104 (substitute* ".dub/dub.json"
105 (("\"lastUpgrade\": \"[^\"]*\"")
106 "\"lastUpgrade\": \"1970-01-01T00:00:00.0000000\"")))
109 (define* (install #:key inputs outputs #:allow-other-keys)
110 "Install a given DUB package."
111 (let* ((out (assoc-ref outputs "out"))
112 (outbin (string-append out "/bin"))
113 (outlib (string-append out "/lib/dub/" (basename out))))
115 ;; TODO remove "-test-application"
116 (copy-recursively "bin" outbin)
118 (copy-recursively "." (string-append outlib))
121 (define %standard-phases
122 (modify-phases gnu:%standard-phases
124 (replace 'configure configure)
125 (replace 'build build)
126 (replace 'check check)
127 (replace 'install install)))
129 (define* (dub-build #:key inputs (phases %standard-phases)
130 #:allow-other-keys #:rest args)
131 "Build the given DUB package, applying all of PHASES in order."
132 (apply gnu:gnu-build #:inputs inputs #:phases phases args))