rebar-build-system.scm 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016, 2018 Ricardo Wurmus <rekado@elephly.net>
  3. ;;; Copyright © 2019 Björn Höfling <bjoern.hoefling@bjoernhoefling.de>
  4. ;;; Copyright © 2020, 2022 Hartmut Goebel <h.goebel@crazy-compilers.com>
  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 rebar-build-system)
  21. #:use-module ((guix build gnu-build-system) #:prefix gnu:)
  22. #:use-module ((guix build utils) #:hide (delete))
  23. #:use-module (ice-9 match)
  24. #:use-module (ice-9 ftw)
  25. #:use-module (srfi srfi-1)
  26. #:use-module (srfi srfi-26)
  27. #:export (rebar-build
  28. %standard-phases))
  29. ;;
  30. ;; Builder-side code of the standard build procedure for Erlang packages using
  31. ;; rebar3.
  32. ;;
  33. ;; TODO: Think about whether bindir ("ebin"), libdir ("priv") and includedir
  34. ;; "(include") need to be configurable
  35. (define %erlang-libdir "/lib/erlang/lib")
  36. (define* (erlang-depends #:key inputs #:allow-other-keys)
  37. (define input-directories
  38. (match inputs
  39. (((_ . dir) ...)
  40. dir)))
  41. (mkdir-p "_checkouts")
  42. (for-each
  43. (lambda (input-dir)
  44. (let ((elibdir (string-append input-dir %erlang-libdir)))
  45. (when (directory-exists? elibdir)
  46. (for-each
  47. (lambda (dirname)
  48. (let ((dest (string-append elibdir "/" dirname))
  49. (link (string-append "_checkouts/" dirname)))
  50. (when (not (file-exists? link))
  51. ;; RETHINK: Maybe better copy and make writable to avoid some
  52. ;; error messages e.g. when using with rebar3-git-vsn.
  53. (symlink dest link))))
  54. (list-directories elibdir)))))
  55. input-directories))
  56. (define* (unpack #:key source #:allow-other-keys)
  57. "Unpack SOURCE in the working directory, and change directory within the
  58. source. When SOURCE is a directory, copy it in a sub-directory of the current
  59. working directory."
  60. (let ((gnu-unpack (assoc-ref gnu:%standard-phases 'unpack)))
  61. (gnu-unpack #:source source)
  62. ;; Packages from hex.pm typically have a contents.tar.gz containing the
  63. ;; actual source. If this tar file exists, extract it.
  64. (when (file-exists? "contents.tar.gz")
  65. (invoke "tar" "xvf" "contents.tar.gz"))))
  66. (define* (build #:key (rebar-flags '()) #:allow-other-keys)
  67. (apply invoke `("rebar3" "compile" ,@rebar-flags)))
  68. (define* (check #:key target (rebar-flags '()) (tests? (not target))
  69. (test-target "eunit")
  70. #:allow-other-keys)
  71. (if tests?
  72. (apply invoke `("rebar3" ,test-target ,@rebar-flags))
  73. (format #t "test suite not run~%")))
  74. (define (erlang-package? name)
  75. "Check if NAME correspond to the name of an Erlang package."
  76. (string-prefix? "erlang-" name))
  77. (define (package-name-version->erlang-name name+ver)
  78. "Convert the Guix package NAME-VER to the corresponding Erlang name-version
  79. format. Essentially drop the prefix used in Guix and replace dashes by
  80. underscores."
  81. (let* ((name- (package-name->name+version name+ver)))
  82. (string-join
  83. (string-split
  84. (if (erlang-package? name-) ; checks for "erlang-" prefix
  85. (string-drop name- (string-length "erlang-"))
  86. name-)
  87. #\-)
  88. "_")))
  89. (define (list-directories directory)
  90. "Return file names of the sub-directory of DIRECTORY."
  91. (scandir directory
  92. (lambda (file)
  93. (and (not (member file '("." "..")))
  94. (file-is-directory? (string-append directory "/" file))))))
  95. (define* (install #:key name outputs
  96. (install-name (package-name-version->erlang-name name))
  97. (install-profile "default") ; build profile outputs to install
  98. #:allow-other-keys)
  99. (let* ((out (assoc-ref outputs "out"))
  100. (pkg-dir (string-append out %erlang-libdir "/" install-name)))
  101. (let ((bin-dir (string-append "_build/" install-profile "/bin"))
  102. (lib-dir (string-append "_build/" install-profile "/lib")))
  103. ;; install _build/PROFILE/bin
  104. (when (file-exists? bin-dir)
  105. (copy-recursively bin-dir out #:follow-symlinks? #t))
  106. ;; install _build/PROFILE/lib/*/{ebin,include,priv}
  107. (for-each
  108. (lambda (*)
  109. (for-each
  110. (lambda (dirname)
  111. (let ((src-dir (string-append lib-dir "/" * "/" dirname))
  112. (dst-dir (string-append pkg-dir "/" dirname)))
  113. (when (file-exists? src-dir)
  114. (copy-recursively src-dir dst-dir #:follow-symlinks? #t))
  115. (false-if-exception
  116. (delete-file (string-append dst-dir "/.gitignore")))))
  117. '("ebin" "include" "priv")))
  118. (list-directories lib-dir))
  119. (false-if-exception
  120. (delete-file (string-append pkg-dir "/priv/Run-eunit-loop.expect"))))))
  121. (define %standard-phases
  122. (modify-phases gnu:%standard-phases
  123. (replace 'unpack unpack)
  124. (delete 'bootstrap)
  125. (delete 'configure)
  126. (add-before 'build 'erlang-depends erlang-depends)
  127. (replace 'build build)
  128. (replace 'check check)
  129. (replace 'install install)))
  130. (define* (rebar-build #:key inputs (phases %standard-phases)
  131. #:allow-other-keys #:rest args)
  132. "Build the given Erlang package, applying all of PHASES in order."
  133. (apply gnu:gnu-build #:inputs inputs #:phases phases args))