gnu: libshout: Update to 2.4.2.
[guix.git] / guix / build / dub-build-system.scm
blob3ab50733de910be1e78ac28eb7afb7c45f0f4cab
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>
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/>.
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))
36 ;; Commentary:
38 ;; Builder-side code of the DUB (the build tool for D) build system.
40 ;; Code:
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)))
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     (invoke "dub" "add-path" vendor-dir)
71     #t))
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)
77                    string))
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."
83   (catch 'system-error
84     (lambda ()
85       (grep string file-name))
86     (lambda args
87       #f)))
89 (define* (build #:key (dub-build-flags '())
90                 #:allow-other-keys)
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\"")))
99   #t)
101 (define* (check #:key tests? #:allow-other-keys)
102   (when tests?
103     (invoke "dub" "test")
104     (substitute* ".dub/dub.json"
105       (("\"lastUpgrade\": \"[^\"]*\"")
106        "\"lastUpgrade\": \"1970-01-01T00:00:00.0000000\"")))
107   #t)
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))))
114     (mkdir-p outbin)
115     ;; TODO remove "-test-application"
116     (copy-recursively "bin" outbin)
117     (mkdir-p outlib)
118     (copy-recursively "." (string-append outlib))
119     #t))
121 (define %standard-phases
122   (modify-phases gnu:%standard-phases
123     (delete 'bootstrap)
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))