build-self.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (build-self)
  19. #:use-module (gnu)
  20. #:use-module (guix)
  21. #:use-module (guix config)
  22. #:use-module (srfi srfi-1)
  23. #:use-module (srfi srfi-19)
  24. #:use-module (ice-9 match)
  25. #:export (build))
  26. ;;; Commentary:
  27. ;;;
  28. ;;; When loaded, this module returns a monadic procedure of at least one
  29. ;;; argument: the source tree to build. It returns a derivation that
  30. ;;; builds it.
  31. ;;;
  32. ;;; This file uses modules provided by the already-installed Guix. Those
  33. ;;; modules may be arbitrarily old compared to the version we want to
  34. ;;; build. Because of that, it must rely on the smallest set of features
  35. ;;; that are likely to be provided by the (guix) and (gnu) modules, and by
  36. ;;; Guile itself, forever and ever.
  37. ;;;
  38. ;;; Code:
  39. ;; The dependencies. Don't refer explicitly to the variables because they
  40. ;; could be renamed or shuffled around in modules over time. Conversely,
  41. ;; 'find-best-packages-by-name' is expected to always have the same semantics.
  42. (define libgcrypt
  43. (first (find-best-packages-by-name "libgcrypt" #f)))
  44. (define zlib
  45. (first (find-best-packages-by-name "zlib" #f)))
  46. (define gzip
  47. (first (find-best-packages-by-name "gzip" #f)))
  48. (define bzip2
  49. (first (find-best-packages-by-name "bzip2" #f)))
  50. (define xz
  51. (first (find-best-packages-by-name "xz" #f)))
  52. (define (false-if-wrong-guile package)
  53. "Return #f if PACKAGE depends on the \"wrong\" major version of Guile (e.g.,
  54. 2.0 instead of 2.2), otherwise return PACKAGE."
  55. (let ((guile (any (match-lambda
  56. ((label (? package? dep) _ ...)
  57. (and (string=? (package-name dep) "guile")
  58. dep)))
  59. (package-direct-inputs package))))
  60. (and (or (not guile)
  61. (string-prefix? (effective-version)
  62. (package-version guile)))
  63. package)))
  64. (define (package-for-current-guile . names)
  65. "Return the package with one of the given NAMES that depends on the current
  66. Guile major version (2.0 or 2.2), or #f if none of the packages matches."
  67. (let loop ((names names))
  68. (match names
  69. (()
  70. #f)
  71. ((name rest ...)
  72. (match (find-best-packages-by-name name #f)
  73. (()
  74. (loop rest))
  75. ((first _ ...)
  76. (or (false-if-wrong-guile first)
  77. (loop rest))))))))
  78. (define guile-json
  79. (package-for-current-guile "guile-json"
  80. "guile2.2-json"
  81. "guile2.0-json"))
  82. (define guile-ssh
  83. (package-for-current-guile "guile-ssh"
  84. "guile2.2-ssh"
  85. "guile2.0-ssh"))
  86. (define guile-git
  87. (package-for-current-guile "guile-git"
  88. "guile2.0-git"))
  89. (define guile-bytestructures
  90. (package-for-current-guile "guile-bytestructures"
  91. "guile2.0-bytestructures"))
  92. ;; The actual build procedure.
  93. (define (top-source-directory)
  94. "Return the name of the top-level directory of this source tree."
  95. (and=> (assoc-ref (current-source-location) 'filename)
  96. (lambda (file)
  97. (string-append (dirname file) "/.."))))
  98. (define (date-version-string)
  99. "Return the current date and hour in UTC timezone, for use as a poor
  100. person's version identifier."
  101. ;; XXX: Replace with a Git commit id.
  102. (date->string (current-date 0) "~Y~m~d.~H"))
  103. (define (matching-guile-2.2)
  104. "Return a Guile 2.2 with the same version as the current one or immediately
  105. older than then current one. This is so that we do not build ABI-incompatible
  106. objects. See <https://bugs.gnu.org/29570>."
  107. (let loop ((packages (find-packages-by-name "guile" "2.2"))
  108. (best #f))
  109. (match packages
  110. (()
  111. best)
  112. ((head tail ...)
  113. (if (string=? (package-version head) (version))
  114. head
  115. (if best
  116. (if (version>? (package-version head) (version))
  117. (loop tail best)
  118. (loop tail head))
  119. (loop tail head)))))))
  120. (define (guile-for-build)
  121. "Return a derivation for Guile 2.0 or 2.2, whichever matches the currently
  122. running Guile."
  123. (package->derivation (cond-expand
  124. (guile-2.2
  125. (canonical-package (matching-guile-2.2)))
  126. (else
  127. (canonical-package
  128. (specification->package "guile@2.0"))))))
  129. ;; The procedure below is our return value.
  130. (define* (build source
  131. #:key verbose? (version (date-version-string))
  132. #:allow-other-keys
  133. #:rest rest)
  134. "Return a derivation that unpacks SOURCE into STORE and compiles Scheme
  135. files."
  136. ;; The '%xxxdir' variables were added to (guix config) in July 2016 so we
  137. ;; cannot assume that they are defined. Try to guess their value when
  138. ;; they're undefined (XXX: we get an incorrect guess when environment
  139. ;; variables such as 'NIX_STATE_DIR' are defined!).
  140. (define storedir
  141. (if (defined? '%storedir) %storedir %store-directory))
  142. (define localstatedir
  143. (if (defined? '%localstatedir) %localstatedir (dirname %state-directory)))
  144. (define sysconfdir
  145. (if (defined? '%sysconfdir) %sysconfdir (dirname %config-directory)))
  146. (define sbindir
  147. (if (defined? '%sbindir) %sbindir (dirname %guix-register-program)))
  148. (define builder
  149. #~(begin
  150. (use-modules (guix build pull))
  151. (letrec-syntax ((maybe-load-path
  152. (syntax-rules ()
  153. ((_ item rest ...)
  154. (let ((tail (maybe-load-path rest ...)))
  155. (if (string? item)
  156. (cons (string-append item
  157. "/share/guile/site/"
  158. #$(effective-version))
  159. tail)
  160. tail)))
  161. ((_)
  162. '()))))
  163. (set! %load-path
  164. (append
  165. (maybe-load-path #$guile-json #$guile-ssh
  166. #$guile-git #$guile-bytestructures)
  167. %load-path)))
  168. (letrec-syntax ((maybe-load-compiled-path
  169. (syntax-rules ()
  170. ((_ item rest ...)
  171. (let ((tail (maybe-load-compiled-path rest ...)))
  172. (if (string? item)
  173. (cons (string-append item
  174. "/lib/guile/"
  175. #$(effective-version)
  176. "/site-ccache")
  177. tail)
  178. tail)))
  179. ((_)
  180. '()))))
  181. (set! %load-compiled-path
  182. (append
  183. (maybe-load-compiled-path #$guile-json #$guile-ssh
  184. #$guile-git #$guile-bytestructures)
  185. %load-compiled-path)))
  186. ;; XXX: The 'guile-ssh' package prior to Guix commit 92b7258 was
  187. ;; broken: libguile-ssh could not be found. Work around that.
  188. ;; FIXME: We want Guile-SSH 0.10.2 or later anyway.
  189. #$(if (string-prefix? "0.9." (package-version guile-ssh))
  190. #~(setenv "LTDL_LIBRARY_PATH" (string-append #$guile-ssh "/lib"))
  191. #t)
  192. (build-guix #$output #$source
  193. #:system #$%system
  194. #:storedir #$storedir
  195. #:localstatedir #$localstatedir
  196. #:sysconfdir #$sysconfdir
  197. #:sbindir #$sbindir
  198. #:package-name #$%guix-package-name
  199. #:package-version #$version
  200. #:bug-report-address #$%guix-bug-report-address
  201. #:home-page-url #$%guix-home-page-url
  202. #:libgcrypt #$libgcrypt
  203. #:zlib #$zlib
  204. #:gzip #$gzip
  205. #:bzip2 #$bzip2
  206. #:xz #$xz
  207. ;; XXX: This is not perfect, enabling VERBOSE? means
  208. ;; building a different derivation.
  209. #:debug-port (if #$verbose?
  210. (current-error-port)
  211. (%make-void-port "w")))))
  212. (unless guile-git
  213. ;; XXX: Guix before February 2017 lacks a 'guile-git' package altogether.
  214. ;; If we try to upgrade anyway, the logic in (guix scripts pull) will not
  215. ;; build (guix git), which will leave us with an unusable 'guix pull'. To
  216. ;; avoid that, fail early.
  217. (format (current-error-port)
  218. "\
  219. Your installation is too old and lacks a '~a' package.
  220. Please upgrade to an intermediate version first, for instance with:
  221. guix pull --url=https://git.savannah.gnu.org/cgit/guix.git/snapshot/v0.13.0.tar.gz
  222. \n"
  223. (match (effective-version)
  224. ("2.0" "guile2.0-git")
  225. (_ "guile-git")))
  226. (exit 1))
  227. (mlet %store-monad ((guile (guile-for-build)))
  228. (gexp->derivation "guix-latest" builder
  229. #:modules '((guix build pull)
  230. (guix build utils)
  231. (guix build compile)
  232. ;; Closure of (guix modules).
  233. (guix modules)
  234. (guix memoization)
  235. (guix profiling)
  236. (guix sets))
  237. ;; Arrange so that our own (guix build …) modules are
  238. ;; used.
  239. #:module-path (list (top-source-directory))
  240. #:guile-for-build guile)))
  241. ;; This file is loaded by 'guix pull'; return it the build procedure.
  242. build
  243. ;; Local Variables:
  244. ;; eval: (put 'with-load-path 'scheme-indent-function 1)
  245. ;; End:
  246. ;;; build-self.scm ends here