copy-build-system.scm 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (guix build copy-build-system)
  20. #:use-module ((guix build gnu-build-system) #:prefix gnu:)
  21. #:use-module (guix build utils)
  22. #:use-module (ice-9 match)
  23. #:use-module (ice-9 ftw)
  24. #:use-module (srfi srfi-1)
  25. #:use-module (srfi srfi-26)
  26. #:export (%standard-phases
  27. copy-build))
  28. ;; Commentary:
  29. ;;
  30. ;; System for building packages that don't require much compilation, mostly
  31. ;; only to copy files around.
  32. ;;
  33. ;; Code:
  34. (define* (install #:key install-plan outputs #:allow-other-keys)
  35. "Copy files from the \"source\" build input to the \"out\" output according to INSTALL-PLAN.
  36. An install plan is a list of plans in the form:
  37. (SOURCE TARGET [FILTERS])
  38. In the above, FILTERS are optional.
  39. - When SOURCE matches a file or directory without trailing slash, install it to
  40. TARGET.
  41. - If TARGET has a trailing slash, install SOURCE basename beneath TARGET.
  42. - Otherwise install SOURCE as TARGET.
  43. - When SOURCE is a directory with a trailing slash, or when FILTERS are used,
  44. the trailing slash of TARGET is implied.
  45. - Without FILTERS, install the full SOURCE _content_ to TARGET.
  46. The paths relative to SOURCE are preserved within TARGET.
  47. - With FILTERS among `#:include`, `#:include-regexp`, `#:exclude`,
  48. `#:exclude-regexp`:
  49. - With `#:include`, install only the paths which suffix exactly matches
  50. one of the elements in the list.
  51. - With `#:include-regexp`, install subpaths matching the regexps in the list.
  52. - The `#:exclude*` FILTERS work similarly. Without `#:include*` flags,
  53. install every subpath but the files matching the `#:exclude*` filters.
  54. If both `#:include*` and `#:exclude*` are specified, the exclusion is done
  55. on the inclusion list.
  56. Examples:
  57. - `(\"foo/bar\" \"share/my-app/\")`: Install bar to \"share/my-app/bar\".
  58. - `(\"foo/bar\" \"share/my-app/baz\")`: Install bar to \"share/my-app/baz\".
  59. - `(\"foo/\" \"share/my-app\")`: Install the content of foo inside \"share/my-app\",
  60. e.g. install \"foo/sub/file\" to \"share/my-app/sub/file\".
  61. - `(\"foo/\" \"share/my-app\" #:include (\"sub/file\"))`: Install only \"foo/sub/file\" to
  62. \"share/my-app/sub/file\".
  63. - `(\"foo/sub\" \"share/my-app\" #:include (\"file\"))`: Install \"foo/sub/file\" to
  64. \"share/my-app/file\"."
  65. (define (install-simple source target)
  66. "Install SOURCE to TARGET.
  67. TARGET must point to a store location.
  68. SOURCE may be a file or a directory.
  69. If a directory, the directory itself is installed, not its content.
  70. if TARGET ends with a '/', the source is installed underneath."
  71. (let ((target (if (string-suffix? "/" target)
  72. (string-append target (basename source))
  73. target)))
  74. (mkdir-p (dirname target))
  75. (copy-recursively source target)))
  76. (define (install-file file target)
  77. (let ((dest (string-append target
  78. (if (string-suffix? "/" target)
  79. (string-append "/" file)
  80. file))))
  81. (format (current-output-port) "`~a' -> `~a'~%" file dest)
  82. (mkdir-p (dirname dest))
  83. (let ((stat (lstat file)))
  84. (case (stat:type stat)
  85. ((symlink)
  86. (let ((target (readlink file)))
  87. (symlink target dest)))
  88. (else
  89. (copy-file file dest))))))
  90. (define* (make-file-predicate suffixes matches-regexp #:optional (default-value #t))
  91. "Return a predicate that returns #t if its file argument matches the
  92. SUFFIXES or the MATCHES-REGEXP. If neither SUFFIXES nor MATCHES-REGEXP is
  93. given, then the predicate always returns DEFAULT-VALUE."
  94. (if (or suffixes matches-regexp)
  95. (let* ((suffixes (or suffixes '()))
  96. (regexps (map make-regexp (or matches-regexp '())))
  97. (predicates (append
  98. (map (lambda (str)
  99. (cut string-suffix? str <>))
  100. suffixes)
  101. (map (lambda (regexp)
  102. (cut regexp-exec regexp <>))
  103. regexps))))
  104. (lambda (file)
  105. (any (cut <> file) predicates)))
  106. (const default-value)))
  107. (define* (install-file-list source target #:key include exclude include-regexp exclude-regexp)
  108. ;; We must use switch current directory to source so that `find-files'
  109. ;; returns file paths relative to source.
  110. (with-directory-excursion source
  111. (let* ((exclusion-pred (negate (make-file-predicate exclude exclude-regexp #f)))
  112. (inclusion-pred (make-file-predicate include include-regexp))
  113. (file-list
  114. (filter! exclusion-pred
  115. (find-files "." (lambda (file _stat)
  116. (inclusion-pred file))))))
  117. (map (cut install-file <> (if (string-suffix? "/" target)
  118. target
  119. (string-append target "/")))
  120. file-list))))
  121. (define* (install source target #:key include exclude include-regexp exclude-regexp)
  122. (set! target (string-append (assoc-ref outputs "out") "/" target))
  123. (let ((filters? (or include exclude include-regexp exclude-regexp)))
  124. (when (and (not (file-is-directory? source))
  125. filters?)
  126. (error "Cannot use filters when SOURCE is a file."))
  127. (let ((multi-files-in-source?
  128. (or (string-suffix? "/" source)
  129. (and (file-is-directory? source)
  130. filters?))))
  131. (if multi-files-in-source?
  132. (install-file-list source target
  133. #:include include
  134. #:exclude exclude
  135. #:include-regexp include-regexp
  136. #:exclude-regexp exclude-regexp)
  137. (install-simple source target)))))
  138. (for-each (lambda (plan) (apply install plan)) install-plan)
  139. #t)
  140. (define %standard-phases
  141. ;; Everything is as with the GNU Build System except for the `configure'
  142. ;; , `build', `check' and `install' phases.
  143. (modify-phases gnu:%standard-phases
  144. (delete 'bootstrap)
  145. (delete 'configure)
  146. (delete 'build)
  147. (delete 'check)
  148. (replace 'install install)))
  149. (define* (copy-build #:key inputs (phases %standard-phases)
  150. #:allow-other-keys #:rest args)
  151. "Build the given package, applying all of PHASES in order."
  152. (apply gnu:gnu-build #:inputs inputs #:phases phases args))
  153. ;;; copy-build-system.scm ends here