ruby-build-system.scm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015 David Thompson <davet@gnu.org>
  3. ;;; Copyright © 2015 Pjotr Prins <pjotr.public01@thebird.nl>
  4. ;;; Copyright © 2015, 2016 Ben Woodcroft <donttrustben@gmail.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 ruby-build-system)
  21. #:use-module ((guix build gnu-build-system) #:prefix gnu:)
  22. #:use-module (guix build utils)
  23. #:use-module (ice-9 ftw)
  24. #:use-module (ice-9 match)
  25. #:use-module (ice-9 popen)
  26. #:use-module (ice-9 rdelim)
  27. #:use-module (ice-9 regex)
  28. #:use-module (srfi srfi-1)
  29. #:use-module (srfi srfi-26)
  30. #:export (%standard-phases
  31. ruby-build))
  32. ;; Commentary:
  33. ;;
  34. ;; Builder-side code of the standard Ruby package build procedure.
  35. ;;
  36. ;; Code:
  37. (define (first-matching-file pattern)
  38. "Return the first file name that matches PATTERN in the current working
  39. directory."
  40. (match (find-files "." pattern)
  41. ((file-name . _) file-name)
  42. (() (error "No files matching pattern: " pattern))))
  43. (define gnu:unpack (assq-ref gnu:%standard-phases 'unpack))
  44. (define (gem-archive? file-name)
  45. (string-match "^.*\\.gem$" file-name))
  46. (define* (unpack #:key source #:allow-other-keys)
  47. "Unpack the gem SOURCE and enter the resulting directory."
  48. (if (gem-archive? source)
  49. (begin
  50. (invoke "gem" "unpack" source)
  51. ;; The unpacked gem directory is named the same as the archive,
  52. ;; sans the ".gem" extension. It is renamed to simply "gem" in an
  53. ;; effort to keep file names shorter to avoid UNIX-domain socket
  54. ;; file names and shebangs that exceed the system's fixed maximum
  55. ;; length when running test suites.
  56. (let ((dir (match:substring (string-match "^(.*)\\.gem$"
  57. (basename source))
  58. 1)))
  59. (rename-file dir "gem")
  60. (chdir "gem"))
  61. #t)
  62. ;; Use GNU unpack strategy for things that aren't gem archives.
  63. (gnu:unpack #:source source)))
  64. (define (first-gemspec)
  65. (first-matching-file "\\.gemspec$"))
  66. (define* (replace-git-ls-files #:key source #:allow-other-keys)
  67. "Many gemspec files downloaded from outside rubygems.org use `git ls-files`
  68. to list of the files to be included in the built gem. However, since this
  69. operation is not deterministic, we replace it with `find`."
  70. (when (not (gem-archive? source))
  71. (let ((gemspec (first-gemspec)))
  72. (substitute* gemspec
  73. (("`git ls-files`") "`find . -type f |sort`")
  74. (("`git ls-files -z`") "`find . -type f -print0 |sort -z`"))))
  75. #t)
  76. (define* (extract-gemspec #:key source #:allow-other-keys)
  77. "Remove the original gemspec, if present, and replace it with a new one.
  78. This avoids issues with upstream gemspecs requiring tools such as git to
  79. generate the files list."
  80. (if (gem-archive? source)
  81. (let ((gemspec (or (false-if-exception (first-gemspec))
  82. ;; Make new gemspec if one wasn't shipped.
  83. ".gemspec")))
  84. (when (file-exists? gemspec) (delete-file gemspec))
  85. ;; Extract gemspec from source gem.
  86. (let ((pipe (open-pipe* OPEN_READ "gem" "spec" "--ruby" source)))
  87. (dynamic-wind
  88. (const #t)
  89. (lambda ()
  90. (call-with-output-file gemspec
  91. (lambda (out)
  92. ;; 'gem spec' writes to stdout, but 'gem build' only reads
  93. ;; gemspecs from a file, so we redirect the output to a file.
  94. (while (not (eof-object? (peek-char pipe)))
  95. (write-char (read-char pipe) out))))
  96. #t)
  97. (lambda ()
  98. (close-pipe pipe)))))
  99. (display "extract-gemspec: skipping as source is not a gem archive\n"))
  100. #t)
  101. (define* (build #:key source #:allow-other-keys)
  102. "Build a new gem using the gemspec from the SOURCE gem."
  103. ;; Build a new gem from the current working directory. This also allows any
  104. ;; dynamic patching done in previous phases to be present in the installed
  105. ;; gem.
  106. (invoke "gem" "build" (first-gemspec)))
  107. (define* (check #:key tests? test-target #:allow-other-keys)
  108. "Run the gem's test suite rake task TEST-TARGET. Skip the tests if TESTS?
  109. is #f."
  110. (if tests?
  111. (invoke "rake" test-target)
  112. #t))
  113. (define* (install #:key inputs outputs (gem-flags '())
  114. #:allow-other-keys)
  115. "Install the gem archive SOURCE to the output store item. Additional
  116. GEM-FLAGS are passed to the 'gem' invocation, if present."
  117. (let* ((ruby-version
  118. (match:substring (string-match "ruby-(.*)\\.[0-9]$"
  119. (assoc-ref inputs "ruby"))
  120. 1))
  121. (out (assoc-ref outputs "out"))
  122. (vendor-dir (string-append out "/lib/ruby/vendor_ruby"))
  123. (gem-file (first-matching-file "\\.gem$"))
  124. (gem-file-basename (basename gem-file))
  125. (gem-name (substring gem-file-basename
  126. 0
  127. (- (string-length gem-file-basename) 4)))
  128. (gem-dir (string-append vendor-dir "/gems/" gem-name)))
  129. (setenv "GEM_VENDOR" vendor-dir)
  130. (or (zero?
  131. ;; 'zero? system*' allows the custom error handling to function as
  132. ;; expected, while 'invoke' raises its own exception.
  133. (apply system* "gem" "install" gem-file
  134. "--verbose"
  135. "--local" "--ignore-dependencies" "--vendor"
  136. ;; Executables should go into /bin, not
  137. ;; /lib/ruby/gems.
  138. "--bindir" (string-append out "/bin")
  139. gem-flags))
  140. (begin
  141. (let ((failed-output-dir (string-append (getcwd) "/out")))
  142. (mkdir failed-output-dir)
  143. (copy-recursively out failed-output-dir))
  144. (error "installation failed")))
  145. ;; Remove the cached gem file as this is unnecessary and contains
  146. ;; timestamped files rendering builds not reproducible.
  147. (let ((cached-gem (string-append vendor-dir "/cache/" gem-file)))
  148. (log-file-deletion cached-gem)
  149. (delete-file cached-gem))
  150. ;; For gems with native extensions, several Makefile-related files
  151. ;; are created that contain timestamps or other elements making
  152. ;; them not reproducible. They are unnecessary so we remove them.
  153. (when (file-exists? (string-append gem-dir "/ext"))
  154. (for-each (lambda (file)
  155. (log-file-deletion file)
  156. (delete-file file))
  157. (append
  158. (find-files (string-append vendor-dir "/doc")
  159. "page-Makefile.ri")
  160. (find-files (string-append vendor-dir "/extensions")
  161. "gem_make.out")
  162. (find-files (string-append gem-dir "/ext")
  163. "Makefile"))))
  164. #t))
  165. (define* (wrap-ruby-program prog #:key (gem-clear-paths #t) #:rest vars)
  166. "Make a wrapper for PROG. VARS should look like this:
  167. '(VARIABLE DELIMITER POSITION LIST-OF-DIRECTORIES)
  168. where DELIMITER is optional. ':' will be used if DELIMITER is not given.
  169. For example, this command:
  170. (wrap-ruby-program \"foo\"
  171. '(\"PATH\" \":\" = (\"/gnu/.../bar/bin\"))
  172. '(\"CERT_PATH\" suffix (\"/gnu/.../baz/certs\"
  173. \"/qux/certs\")))
  174. will copy 'foo' to '.real/fool' and create the file 'foo' with the following
  175. contents:
  176. #!location/of/bin/ruby
  177. ENV['PATH'] = \"/gnu/.../bar/bin\"
  178. ENV['CERT_PATH'] = (ENV.key?('CERT_PATH') ? (ENV['CERT_PATH'] + ':') : '') + '/gnu/.../baz/certs:/qux/certs'
  179. load location/of/.real/foo
  180. This is useful for scripts that expect particular programs to be in $PATH, for
  181. programs that expect particular gems to be in the GEM_PATH.
  182. This is preferable to wrap-program, which uses a bash script, as this prevents
  183. ruby scripts from being executed with @command{ruby -S ...}.
  184. If PROG has previously been wrapped by 'wrap-ruby-program', the wrapper is
  185. extended with definitions for VARS."
  186. (define wrapped-file
  187. (string-append (dirname prog) "/.real/" (basename prog)))
  188. (define already-wrapped?
  189. (file-exists? wrapped-file))
  190. (define (last-line port)
  191. ;; Return the last line read from PORT and leave PORT's cursor right
  192. ;; before it.
  193. (let loop ((previous-line-offset 0)
  194. (previous-line "")
  195. (position (seek port 0 SEEK_CUR)))
  196. (match (read-line port 'concat)
  197. ((? eof-object?)
  198. (seek port previous-line-offset SEEK_SET)
  199. previous-line)
  200. ((? string? line)
  201. (loop position line (+ (string-length line) position))))))
  202. (define (export-variable lst)
  203. ;; Return a string that exports an environment variable.
  204. (match lst
  205. ((var sep '= rest)
  206. (format #f "ENV['~a'] = '~a'"
  207. var (string-join rest sep)))
  208. ((var sep 'prefix rest)
  209. (format #f "ENV['~a'] = '~a' + (ENV.key?('~a') ? ('~a' + ENV['~a']) : '')"
  210. var (string-join rest sep) var sep var))
  211. ((var sep 'suffix rest)
  212. (format #f "ENV['~a'] = (ENV.key?('~a') ? (ENV['~a'] + '~a') : '') + '~a'"
  213. var var var sep (string-join rest sep)))
  214. ((var '= rest)
  215. (format #f "ENV['~a'] = '~a'"
  216. var (string-join rest ":")))
  217. ((var 'prefix rest)
  218. (format #f "ENV['~a'] = '~a' + (ENV.key?('~a') ? (':' + ENV['~a']) : '')"
  219. var (string-join rest ":") var var))
  220. ((var 'suffix rest)
  221. (format #f "ENV['~a'] = (ENV.key?('~a') ? (ENV['~a'] + ':') : '') + '~a'"
  222. var var var (string-join rest ":")))))
  223. (if already-wrapped?
  224. ;; PROG is already a wrapper: add the new "export VAR=VALUE" lines just
  225. ;; before the last line.
  226. (let* ((port (open-file prog "r+"))
  227. (last (last-line port)))
  228. (for-each (lambda (var)
  229. (display (export-variable var) port)
  230. (newline port))
  231. vars)
  232. (display last port)
  233. (close-port port))
  234. ;; PROG is not wrapped yet: create a shell script that sets VARS.
  235. (let ((prog-tmp (string-append wrapped-file "-tmp")))
  236. (mkdir-p (dirname prog-tmp))
  237. (link prog wrapped-file)
  238. (call-with-output-file prog-tmp
  239. (lambda (port)
  240. (format port
  241. "#!~a~%~a~%~a~%load '~a'~%"
  242. (which "ruby")
  243. (string-join (map export-variable vars) "\n")
  244. ;; This ensures that if the GEM_PATH has been changed,
  245. ;; then that change will be noticed.
  246. (if gem-clear-paths "Gem.clear_paths" "")
  247. (canonicalize-path wrapped-file))))
  248. (chmod prog-tmp #o755)
  249. (rename-file prog-tmp prog))))
  250. (define* (wrap #:key inputs outputs #:allow-other-keys)
  251. (define (list-of-files dir)
  252. (map (cut string-append dir "/" <>)
  253. (or (scandir dir (lambda (f)
  254. (let ((s (stat (string-append dir "/" f))))
  255. (eq? 'regular (stat:type s)))))
  256. '())))
  257. (define bindirs
  258. (append-map (match-lambda
  259. ((_ . dir)
  260. (list (string-append dir "/bin")
  261. (string-append dir "/sbin"))))
  262. outputs))
  263. (let* ((out (assoc-ref outputs "out"))
  264. (var `("GEM_PATH" prefix
  265. (,(string-append out "/lib/ruby/vendor_ruby")
  266. ,(getenv "GEM_PATH")))))
  267. (for-each (lambda (dir)
  268. (let ((files (list-of-files dir)))
  269. (for-each (cut wrap-ruby-program <> var)
  270. files)))
  271. bindirs))
  272. #t)
  273. (define (log-file-deletion file)
  274. (display (string-append "deleting '" file "' for reproducibility\n")))
  275. (define %standard-phases
  276. (modify-phases gnu:%standard-phases
  277. (delete 'bootstrap)
  278. (delete 'configure)
  279. (replace 'unpack unpack)
  280. (add-before 'build 'extract-gemspec extract-gemspec)
  281. (add-after 'extract-gemspec 'replace-git-ls-files replace-git-ls-files)
  282. (replace 'build build)
  283. (replace 'check check)
  284. (replace 'install install)
  285. (add-after 'install 'wrap wrap)))
  286. (define* (ruby-build #:key inputs (phases %standard-phases)
  287. #:allow-other-keys #:rest args)
  288. (apply gnu:gnu-build #:inputs inputs #:phases phases args))