dub-build-system.scm 4.4 KB

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