quirks.scm 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2020 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 (guix quirks)
  19. #:use-module ((guix build utils) #:select (substitute*))
  20. #:use-module (srfi srfi-9)
  21. #:use-module (ice-9 match)
  22. #:use-module (ice-9 rdelim)
  23. #:export (%quirks
  24. patch?
  25. applicable-patch?
  26. apply-patch
  27. %patches))
  28. ;;; Commentary:
  29. ;;;
  30. ;;; Time traveling is a challenge! Sometimes, going back to the past requires
  31. ;;; adjusting the old source code so it can be evaluated with our modern day
  32. ;;; Guile and against our modern Guix APIs. This file describes quirks found
  33. ;;; in old Guix revisions, along with ways to address them or patch them.
  34. ;;;
  35. ;;; Code:
  36. (define (syscalls-reexports-local-variables? source)
  37. "Return true if (guix build syscalls) contains the bug described at
  38. <https://bugs.gnu.org/36723>."
  39. (catch 'system-error
  40. (lambda ()
  41. (define content
  42. (call-with-input-file (string-append source
  43. "/guix/build/syscalls.scm")
  44. read-string))
  45. ;; The faulty code would use the 're-export' macro, causing the
  46. ;; 'AT_SYMLINK_NOFOLLOW' local variable to be re-exported when using
  47. ;; Guile > 2.2.4.
  48. (string-contains content "(re-export variable)"))
  49. (lambda args
  50. (if (= ENOENT (system-error-errno args))
  51. #f
  52. (apply throw args)))))
  53. (define (requires-guile-2.2? source)
  54. "Return true if SOURCE uses Guile 2.2 for the shebang of
  55. 'compute-guix-derivation'."
  56. (define content
  57. (call-with-input-file (string-append source "/" %self-build-file)
  58. read-string))
  59. ;; The 'find-best-packages-by-name' call is inserted by %BUG-41214-PATCH.
  60. (string-contains content
  61. (object->string
  62. '(find-best-packages-by-name "guile" "2.2"))))
  63. (define (guile-2.2.4)
  64. (module-ref (resolve-interface '(gnu packages guile))
  65. 'guile-2.2.4))
  66. (define %quirks
  67. ;; List of predicate/package pairs. This allows us to provide information
  68. ;; about specific Guile versions that old Guix revisions might need to use
  69. ;; just to be able to build and run the trampoline in %SELF-BUILD-FILE. See
  70. ;; <https://bugs.gnu.org/37506>
  71. `((,syscalls-reexports-local-variables? . ,guile-2.2.4)
  72. (,requires-guile-2.2? . ,guile-2.2.4)))
  73. ;;;
  74. ;;; Patches.
  75. ;;;
  76. ;; Patch to apply to a source tree.
  77. (define-record-type <patch>
  78. (patch predicate application)
  79. patch?
  80. (predicate patch-predicate) ;procedure
  81. (application patch-application)) ;procedure
  82. (define (applicable-patch? patch source commit)
  83. "Return true if PATCH is applicable to SOURCE, a directory, which
  84. corresponds to the given Guix COMMIT, a SHA1 hexadecimal string."
  85. ;; The predicate is passed COMMIT so that it can choose to only apply to
  86. ;; ancestors.
  87. ((patch-predicate patch) source commit))
  88. (define (apply-patch patch source)
  89. "Apply PATCH onto SOURCE, directly modifying files beneath it."
  90. ((patch-application patch) source))
  91. (define %self-build-file
  92. ;; The file containing code to build Guix.
  93. "build-aux/build-self.scm")
  94. (define %bug-41028-patch
  95. ;; Patch for <https://bugs.gnu.org/41028>. The faulty code is the
  96. ;; 'compute-guix-derivation' body, which uses 'call-with-new-thread' without
  97. ;; importing (ice-9 threads). However, the 'call-with-new-thread' binding
  98. ;; is no longer available in the default name space on Guile 3.0.
  99. (let ()
  100. (define (missing-ice-9-threads-import? source commit)
  101. ;; Return true if %SELF-BUILD-FILE is missing an (ice-9 threads) import.
  102. (define content
  103. (call-with-input-file (string-append source "/" %self-build-file)
  104. read-string))
  105. (and (string-contains content "(call-with-new-thread")
  106. (not (string-contains content "(ice-9 threads)"))))
  107. (define (add-missing-ice-9-threads-import source)
  108. ;; Add (ice-9 threads) import in the gexp of 'compute-guix-derivation'.
  109. (substitute* (string-append source "/" %self-build-file)
  110. (("^ +\\(use-modules \\(ice-9 match\\)\\)")
  111. (object->string '(use-modules (ice-9 match) (ice-9 threads))))))
  112. (patch missing-ice-9-threads-import? add-missing-ice-9-threads-import)))
  113. (define %bug-41214-patch
  114. ;; Patch for <https://bugs.gnu.org/41214>. Around v1.0.0, (guix build
  115. ;; compile) would use Guile 2.2 procedures to access the set of available
  116. ;; compilation options. These procedures no longer exist in 3.0.
  117. (let ()
  118. (define (accesses-guile-2.2-optimization-options? source commit)
  119. (catch 'system-error
  120. (lambda ()
  121. (call-with-input-file (string-append source
  122. "/guix/build/compile.scm")
  123. (lambda (port)
  124. (match (read port)
  125. (('define-module ('guix 'build 'compile)
  126. _ ...
  127. #:use-module ('language 'tree-il 'optimize)
  128. #:use-module ('language 'cps 'optimize)
  129. #:export ('%default-optimizations
  130. '%lightweight-optimizations
  131. 'compile-files))
  132. #t)
  133. (_
  134. ;; Before v1.0.0 (ca. Dec. 2018), the 'use-modules' form
  135. ;; would show up in a subsequent 'cond-expand' clause.
  136. ;; See <https://bugs.gnu.org/42519>.
  137. (match (read port)
  138. (('cond-expand
  139. ('guile-2.2 ('use-modules ('language 'tree-il 'optimize)
  140. _ ...))
  141. _ ...)
  142. #t)
  143. (_
  144. #f)))))))
  145. (const #f)))
  146. (define (build-with-guile-2.2 source)
  147. (substitute* (string-append source "/" %self-build-file)
  148. (("\\(default-guile\\)")
  149. ;; Note: This goes hand in hand with the 'requires-guile-2.2?' quirk.
  150. (object->string '(car (find-best-packages-by-name "guile" "2.2"))))
  151. (("\\(find-best-packages-by-name \"guile-gcrypt\" #f\\)")
  152. (object->string '(find-best-packages-by-name "guile2.2-gcrypt" #f))))
  153. #t)
  154. (patch accesses-guile-2.2-optimization-options?
  155. build-with-guile-2.2)))
  156. (define %patches
  157. ;; Bits of past Guix revisions can become incompatible with newer Guix and
  158. ;; Guile. This variable lists <patch> records for the Guix source tree that
  159. ;; apply to the Guix source.
  160. (list %bug-41028-patch
  161. %bug-41214-patch))