dub-build-system.scm 4.8 KB

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