copy-build-system.scm 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2019 Julien Lepiller <julien@lepiller.eu>
  3. ;;; Copyright © 2020 Pierre Neidhardt <mail@ambrevar.xyz>
  4. ;;; Copyright © 2021 Efraim Flashner <efraim@flashner.co.il>
  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 copy-build-system)
  21. #:use-module ((guix build gnu-build-system) #:prefix gnu:)
  22. #:use-module (guix build utils)
  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 (%standard-phases
  28. copy-build))
  29. ;; Commentary:
  30. ;;
  31. ;; System for building packages that don't require much compilation, mostly
  32. ;; only to copy files around.
  33. ;;
  34. ;; Code:
  35. (define* (install #:key install-plan outputs #:allow-other-keys)
  36. "Copy files from the \"source\" build input to the \"out\" output according to INSTALL-PLAN.
  37. An install plan is a list of plans in the form:
  38. (SOURCE TARGET [FILTERS])
  39. In the above, FILTERS are optional.
  40. - When SOURCE matches a file or directory without trailing slash, install it to
  41. TARGET.
  42. - If TARGET has a trailing slash, install SOURCE basename beneath TARGET.
  43. - Otherwise install SOURCE as TARGET.
  44. - When SOURCE is a directory with a trailing slash, or when FILTERS are used,
  45. the trailing slash of TARGET is implied.
  46. - Without FILTERS, install the full SOURCE _content_ to TARGET.
  47. The paths relative to SOURCE are preserved within TARGET.
  48. - With FILTERS among `#:include`, `#:include-regexp`, `#:exclude`,
  49. `#:exclude-regexp`:
  50. - With `#:include`, install only the paths which suffix exactly matches
  51. one of the elements in the list.
  52. - With `#:include-regexp`, install subpaths matching the regexps in the list.
  53. - The `#:exclude*` FILTERS work similarly. Without `#:include*` flags,
  54. install every subpath but the files matching the `#:exclude*` filters.
  55. If both `#:include*` and `#:exclude*` are specified, the exclusion is done
  56. on the inclusion list.
  57. Examples:
  58. - `(\"foo/bar\" \"share/my-app/\")`: Install bar to \"share/my-app/bar\".
  59. - `(\"foo/bar\" \"share/my-app/baz\")`: Install bar to \"share/my-app/baz\".
  60. - `(\"foo/\" \"share/my-app\")`: Install the content of foo inside \"share/my-app\",
  61. e.g. install \"foo/sub/file\" to \"share/my-app/sub/file\".
  62. - `(\"foo/\" \"share/my-app\" #:include (\"sub/file\"))`: Install only \"foo/sub/file\" to
  63. \"share/my-app/sub/file\".
  64. - `(\"foo/sub\" \"share/my-app\" #:include (\"file\"))`: Install \"foo/sub/file\" to
  65. \"share/my-app/file\"."
  66. (define (install-simple source target)
  67. "Install SOURCE to TARGET.
  68. TARGET must point to a store location.
  69. SOURCE may be a file or a directory.
  70. If a directory, the directory itself is installed, not its content.
  71. if TARGET ends with a '/', the source is installed underneath."
  72. (let ((target (if (string-suffix? "/" target)
  73. (string-append target (basename source))
  74. target)))
  75. (mkdir-p (dirname target))
  76. (copy-recursively source target)))
  77. (define (install-file file target)
  78. (let ((dest (string-append target
  79. (if (string-suffix? "/" target)
  80. (string-append "/" file)
  81. file))))
  82. (format (current-output-port) "`~a' -> `~a'~%" file dest)
  83. (mkdir-p (dirname dest))
  84. (let ((stat (lstat file)))
  85. (case (stat:type stat)
  86. ((symlink)
  87. (let ((target (readlink file)))
  88. (symlink target dest)))
  89. (else
  90. (copy-file file dest))))))
  91. (define* (make-file-predicate suffixes matches-regexp #:optional (default-value #t))
  92. "Return a predicate that returns #t if its file argument matches the
  93. SUFFIXES or the MATCHES-REGEXP. If neither SUFFIXES nor MATCHES-REGEXP is
  94. given, then the predicate always returns DEFAULT-VALUE."
  95. (if (or suffixes matches-regexp)
  96. (let* ((suffixes (or suffixes '()))
  97. (regexps (map make-regexp (or matches-regexp '())))
  98. (predicates (append
  99. (map (lambda (str)
  100. (cut string-suffix? str <>))
  101. suffixes)
  102. (map (lambda (regexp)
  103. (cut regexp-exec regexp <>))
  104. regexps))))
  105. (lambda (file)
  106. (any (cut <> file) predicates)))
  107. (const default-value)))
  108. (define* (install-file-list source target #:key include exclude include-regexp exclude-regexp)
  109. ;; We must use switch current directory to source so that `find-files'
  110. ;; returns file paths relative to source.
  111. (with-directory-excursion source
  112. (let* ((exclusion-pred (negate (make-file-predicate exclude exclude-regexp #f)))
  113. (inclusion-pred (make-file-predicate include include-regexp))
  114. (file-list
  115. (filter! exclusion-pred
  116. (find-files "." (lambda (file _stat)
  117. (inclusion-pred file))))))
  118. (map (cut install-file <> (if (string-suffix? "/" target)
  119. target
  120. (string-append target "/")))
  121. file-list))))
  122. (define* (install source target #:key include exclude include-regexp exclude-regexp)
  123. (let ((final-target (string-append (assoc-ref outputs "out") "/" target))
  124. (filters? (or include exclude include-regexp exclude-regexp)))
  125. (when (and (not (file-is-directory? source))
  126. filters?)
  127. (error "Cannot use filters when SOURCE is a file."))
  128. (let ((multi-files-in-source?
  129. (or (string-suffix? "/" source)
  130. (and (file-is-directory? source)
  131. filters?))))
  132. (if multi-files-in-source?
  133. (install-file-list source final-target
  134. #:include include
  135. #:exclude exclude
  136. #:include-regexp include-regexp
  137. #:exclude-regexp exclude-regexp)
  138. (install-simple source final-target)))))
  139. (for-each (lambda (plan) (apply install plan)) install-plan)
  140. #t)
  141. (define %standard-phases
  142. ;; Everything is as with the GNU Build System except for the `configure'
  143. ;; , `build', `check' and `install' phases.
  144. (modify-phases gnu:%standard-phases
  145. (delete 'bootstrap)
  146. (delete 'configure)
  147. (delete 'build)
  148. (delete 'check)
  149. (replace 'install install)))
  150. (define* (copy-build #:key inputs (phases %standard-phases)
  151. #:allow-other-keys #:rest args)
  152. "Build the given package, applying all of PHASES in order."
  153. (apply gnu:gnu-build #:inputs inputs #:phases phases args))
  154. ;;; copy-build-system.scm ends here