transformations.scm 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016, 2017, 2018, 2019, 2020, 2021 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 transformations)
  19. #:use-module (guix i18n)
  20. #:use-module (guix store)
  21. #:use-module (guix packages)
  22. #:use-module (guix profiles)
  23. #:use-module (guix diagnostics)
  24. #:autoload (guix download) (download-to-store)
  25. #:autoload (guix git-download) (git-reference? git-reference-url)
  26. #:autoload (guix git) (git-checkout git-checkout? git-checkout-url)
  27. #:autoload (guix upstream) (package-latest-release
  28. upstream-source-version
  29. upstream-source-signature-urls)
  30. #:use-module (guix utils)
  31. #:use-module (guix memoization)
  32. #:use-module (guix gexp)
  33. ;; Use the procedure that destructures "NAME-VERSION" forms.
  34. #:use-module ((guix build utils)
  35. #:select ((package-name->name+version
  36. . hyphen-package-name->name+version)))
  37. #:use-module (srfi srfi-1)
  38. #:use-module (srfi srfi-9)
  39. #:use-module (srfi srfi-11)
  40. #:use-module (srfi srfi-26)
  41. #:use-module (srfi srfi-34)
  42. #:use-module (srfi srfi-37)
  43. #:use-module (ice-9 match)
  44. #:use-module (ice-9 vlist)
  45. #:export (options->transformation
  46. manifest-entry-with-transformations
  47. show-transformation-options-help
  48. %transformation-options))
  49. ;;; Commentary:
  50. ;;;
  51. ;;; This module implements "package transformation options"---tools for
  52. ;;; package graph rewriting. It contains the graph rewriting logic, but also
  53. ;;; the tip of its user interface: command-line option handling.
  54. ;;;
  55. ;;; Code:
  56. (module-autoload! (current-module) '(gnu packages)
  57. '(specification->package))
  58. (define (numeric-extension? file-name)
  59. "Return true if FILE-NAME ends with digits."
  60. (string-every char-set:hex-digit (file-extension file-name)))
  61. (define (tarball-base-name file-name)
  62. "Return the \"base\" of FILE-NAME, removing '.tar.gz' or similar
  63. extensions."
  64. ;; TODO: Factorize.
  65. (cond ((not (file-extension file-name))
  66. file-name)
  67. ((numeric-extension? file-name)
  68. file-name)
  69. ((string=? (file-extension file-name) "tar")
  70. (file-sans-extension file-name))
  71. ((file-extension file-name)
  72. =>
  73. (match-lambda
  74. ("scm" file-name)
  75. (_ (tarball-base-name (file-sans-extension file-name)))))
  76. (else
  77. file-name)))
  78. ;; Files to be downloaded.
  79. (define-record-type <downloaded-file>
  80. (downloaded-file uri recursive?)
  81. downloaded-file?
  82. (uri downloaded-file-uri)
  83. (recursive? downloaded-file-recursive?))
  84. (define download-to-store*
  85. (store-lift download-to-store))
  86. (define-gexp-compiler (compile-downloaded-file (file <downloaded-file>)
  87. system target)
  88. "Download FILE and return the result as a store item."
  89. (match file
  90. (($ <downloaded-file> uri recursive?)
  91. (download-to-store* uri #:recursive? recursive?))))
  92. (define* (package-with-source p uri #:optional version)
  93. "Return a package based on P but with its source taken from URI. Extract
  94. the new package's version number from URI."
  95. (let ((base (tarball-base-name (basename uri))))
  96. (let-values (((_ version*)
  97. (hyphen-package-name->name+version base)))
  98. (package (inherit p)
  99. (version (or version version*
  100. (package-version p)))
  101. ;; Use #:recursive? #t to allow for directories.
  102. (source (downloaded-file uri #t))))))
  103. ;;;
  104. ;;; Transformations.
  105. ;;;
  106. (define (transform-package-source sources)
  107. "Return a transformation procedure that replaces package sources with the
  108. matching URIs given in SOURCES."
  109. (define new-sources
  110. (map (lambda (uri)
  111. (match (string-index uri #\=)
  112. (#f
  113. ;; Determine the package name and version from URI.
  114. (call-with-values
  115. (lambda ()
  116. (hyphen-package-name->name+version
  117. (tarball-base-name (basename uri))))
  118. (lambda (name version)
  119. (list name version uri))))
  120. (index
  121. ;; What's before INDEX is a "PKG@VER" or "PKG" spec.
  122. (call-with-values
  123. (lambda ()
  124. (package-name->name+version (string-take uri index)))
  125. (lambda (name version)
  126. (list name version
  127. (string-drop uri (+ 1 index))))))))
  128. sources))
  129. (lambda (obj)
  130. (let loop ((sources new-sources)
  131. (result '()))
  132. (match obj
  133. ((? package? p)
  134. (match (assoc-ref sources (package-name p))
  135. ((version source)
  136. (package-with-source p source version))
  137. (#f
  138. p)))
  139. (_
  140. obj)))))
  141. (define (evaluate-replacement-specs specs proc)
  142. "Parse SPECS, a list of strings like \"guile=guile@2.1\" and return a list
  143. of package spec/procedure pairs as expected by 'package-input-rewriting/spec'.
  144. PROC is called with the package to be replaced and its replacement according
  145. to SPECS. Raise an error if an element of SPECS uses invalid syntax, or if a
  146. package it refers to could not be found."
  147. (define not-equal
  148. (char-set-complement (char-set #\=)))
  149. (map (lambda (spec)
  150. (match (string-tokenize spec not-equal)
  151. ((spec new)
  152. (cons spec
  153. (let ((new (specification->package new)))
  154. (lambda (old)
  155. (proc old new)))))
  156. (x
  157. (raise (formatted-message
  158. (G_ "invalid replacement specification: ~s")
  159. spec)))))
  160. specs))
  161. (define (transform-package-inputs replacement-specs)
  162. "Return a procedure that, when passed a package, replaces its direct
  163. dependencies according to REPLACEMENT-SPECS. REPLACEMENT-SPECS is a list of
  164. strings like \"guile=guile@2.1\" meaning that, any dependency on a package
  165. called \"guile\" must be replaced with a dependency on a version 2.1 of
  166. \"guile\"."
  167. (let* ((replacements (evaluate-replacement-specs replacement-specs
  168. (lambda (old new)
  169. new)))
  170. (rewrite (package-input-rewriting/spec replacements)))
  171. (lambda (obj)
  172. (if (package? obj)
  173. (rewrite obj)
  174. obj))))
  175. (define (transform-package-inputs/graft replacement-specs)
  176. "Return a procedure that, when passed a package, replaces its direct
  177. dependencies according to REPLACEMENT-SPECS. REPLACEMENT-SPECS is a list of
  178. strings like \"gnutls=gnutls@3.5.4\" meaning that packages are built using the
  179. current 'gnutls' package, after which version 3.5.4 is grafted onto them."
  180. (define (set-replacement old new)
  181. (package (inherit old) (replacement new)))
  182. (let* ((replacements (evaluate-replacement-specs replacement-specs
  183. set-replacement))
  184. (rewrite (package-input-rewriting/spec replacements)))
  185. (lambda (obj)
  186. (if (package? obj)
  187. (rewrite obj)
  188. obj))))
  189. (define %not-equal
  190. (char-set-complement (char-set #\=)))
  191. (define (package-git-url package)
  192. "Return the URL of the Git repository for package, or raise an error if
  193. the source of PACKAGE is not fetched from a Git repository."
  194. (let ((source (package-source package)))
  195. (cond ((and (origin? source)
  196. (git-reference? (origin-uri source)))
  197. (git-reference-url (origin-uri source)))
  198. ((git-checkout? source)
  199. (git-checkout-url source))
  200. (else
  201. (raise
  202. (formatted-message (G_ "the source of ~a is not a Git reference")
  203. (package-full-name package)))))))
  204. (define (evaluate-git-replacement-specs specs proc)
  205. "Parse SPECS, a list of strings like \"guile=stable-2.2\", and return a list
  206. of package pairs, where (PROC PACKAGE URL BRANCH-OR-COMMIT) returns the
  207. replacement package. Raise an error if an element of SPECS uses invalid
  208. syntax, or if a package it refers to could not be found."
  209. (map (lambda (spec)
  210. (match (string-tokenize spec %not-equal)
  211. ((spec branch-or-commit)
  212. (define (replace old)
  213. (let* ((source (package-source old))
  214. (url (package-git-url old)))
  215. (proc old url branch-or-commit)))
  216. (cons spec replace))
  217. (_
  218. (raise
  219. (formatted-message (G_ "invalid replacement specification: ~s")
  220. spec)))))
  221. specs))
  222. (define (transform-package-source-branch replacement-specs)
  223. "Return a procedure that, when passed a package, replaces its direct
  224. dependencies according to REPLACEMENT-SPECS. REPLACEMENT-SPECS is a list of
  225. strings like \"guile-next=stable-3.0\" meaning that packages are built using
  226. 'guile-next' from the latest commit on its 'stable-3.0' branch."
  227. (define (replace old url branch)
  228. (package
  229. (inherit old)
  230. (version (string-append "git." (string-map (match-lambda
  231. (#\/ #\-)
  232. (chr chr))
  233. branch)))
  234. (source (git-checkout (url url) (branch branch)
  235. (recursive? #t)))))
  236. (let* ((replacements (evaluate-git-replacement-specs replacement-specs
  237. replace))
  238. (rewrite (package-input-rewriting/spec replacements)))
  239. (lambda (obj)
  240. (if (package? obj)
  241. (rewrite obj)
  242. obj))))
  243. (define (transform-package-source-commit replacement-specs)
  244. "Return a procedure that, when passed a package, replaces its direct
  245. dependencies according to REPLACEMENT-SPECS. REPLACEMENT-SPECS is a list of
  246. strings like \"guile-next=cabba9e\" meaning that packages are built using
  247. 'guile-next' from commit 'cabba9e'."
  248. (define (replace old url commit)
  249. (package
  250. (inherit old)
  251. (version (if (and (> (string-length commit) 1)
  252. (string-prefix? "v" commit)
  253. (char-set-contains? char-set:digit
  254. (string-ref commit 1)))
  255. (string-drop commit 1) ;looks like a tag like "v1.0"
  256. (string-append "git."
  257. (if (< (string-length commit) 7)
  258. commit
  259. (string-take commit 7)))))
  260. (source (git-checkout (url url) (commit commit)
  261. (recursive? #t)))))
  262. (let* ((replacements (evaluate-git-replacement-specs replacement-specs
  263. replace))
  264. (rewrite (package-input-rewriting/spec replacements)))
  265. (lambda (obj)
  266. (if (package? obj)
  267. (rewrite obj)
  268. obj))))
  269. (define (transform-package-source-git-url replacement-specs)
  270. "Return a procedure that, when passed a package, replaces its dependencies
  271. according to REPLACEMENT-SPECS. REPLACEMENT-SPECS is a list of strings like
  272. \"guile-json=https://gitthing.com/…\" meaning that packages are built using
  273. a checkout of the Git repository at the given URL."
  274. (define replacements
  275. (map (lambda (spec)
  276. (match (string-tokenize spec %not-equal)
  277. ((spec url)
  278. (cons spec
  279. (lambda (old)
  280. (package
  281. (inherit old)
  282. (source (git-checkout (url url)
  283. (recursive? #t)))))))
  284. (_
  285. (raise
  286. (formatted-message
  287. (G_ "~a: invalid Git URL replacement specification")
  288. spec)))))
  289. replacement-specs))
  290. (define rewrite
  291. (package-input-rewriting/spec replacements))
  292. (lambda (obj)
  293. (if (package? obj)
  294. (rewrite obj)
  295. obj)))
  296. (define (package-dependents/spec top bottom)
  297. "Return the list of dependents of BOTTOM, a spec string, that are also
  298. dependencies of TOP, a package."
  299. (define-values (name version)
  300. (package-name->name+version bottom))
  301. (define dependent?
  302. (mlambda (p)
  303. (and (package? p)
  304. (or (and (string=? name (package-name p))
  305. (or (not version)
  306. (version-prefix? version (package-version p))))
  307. (match (bag-direct-inputs (package->bag p))
  308. (((labels dependencies . _) ...)
  309. (any dependent? dependencies)))))))
  310. (filter dependent? (package-closure (list top))))
  311. (define (package-toolchain-rewriting p bottom toolchain)
  312. "Return a procedure that, when passed a package that's either BOTTOM or one
  313. of its dependents up to P so, changes it so it is built with TOOLCHAIN.
  314. TOOLCHAIN must be an input list."
  315. (define rewriting-property
  316. (gensym " package-toolchain-rewriting"))
  317. (match (package-dependents/spec p bottom)
  318. (() ;P does not depend on BOTTOM
  319. identity)
  320. (set
  321. ;; SET is the list of packages "between" P and BOTTOM (included) whose
  322. ;; toolchain needs to be changed.
  323. (package-mapping (lambda (p)
  324. (if (or (assq rewriting-property
  325. (package-properties p))
  326. (not (memq p set)))
  327. p
  328. (let ((p (package-with-c-toolchain p toolchain)))
  329. (package/inherit p
  330. (properties `((,rewriting-property . #t)
  331. ,@(package-properties p)))))))
  332. (lambda (p)
  333. (or (assq rewriting-property (package-properties p))
  334. (not (memq p set))))
  335. #:deep? #t))))
  336. (define (transform-package-toolchain replacement-specs)
  337. "Return a procedure that, when passed a package, changes its toolchain or
  338. that of its dependencies according to REPLACEMENT-SPECS. REPLACEMENT-SPECS is
  339. a list of strings like \"fftw=gcc-toolchain@10\" meaning that the package to
  340. the left of the equal sign must be built with the toolchain to the right of
  341. the equal sign."
  342. (define split-on-commas
  343. (cute string-tokenize <> (char-set-complement (char-set #\,))))
  344. (define (specification->input spec)
  345. (let ((package (specification->package spec)))
  346. (list (package-name package) package)))
  347. (define replacements
  348. (map (lambda (spec)
  349. (match (string-tokenize spec %not-equal)
  350. ((spec (= split-on-commas toolchain))
  351. (cons spec (map specification->input toolchain)))
  352. (_
  353. (raise
  354. (formatted-message
  355. (G_ "~a: invalid toolchain replacement specification")
  356. spec)))))
  357. replacement-specs))
  358. (lambda (obj)
  359. (if (package? obj)
  360. (or (any (match-lambda
  361. ((bottom . toolchain)
  362. ((package-toolchain-rewriting obj bottom toolchain) obj)))
  363. replacements)
  364. obj)
  365. obj)))
  366. (define (transform-package-with-debug-info specs)
  367. "Return a procedure that, when passed a package, set its 'replacement' field
  368. to the same package but with #:strip-binaries? #f in its 'arguments' field."
  369. (define (non-stripped p)
  370. (package
  371. (inherit p)
  372. (arguments
  373. (substitute-keyword-arguments (package-arguments p)
  374. ((#:strip-binaries? _ #f) #f)))))
  375. (define (package-with-debug-info p)
  376. (if (member "debug" (package-outputs p))
  377. p
  378. (let loop ((p p))
  379. (match (package-replacement p)
  380. (#f
  381. (package
  382. (inherit p)
  383. (replacement (non-stripped p))))
  384. (next
  385. (package
  386. (inherit p)
  387. (replacement (loop next))))))))
  388. (define rewrite
  389. (package-input-rewriting/spec (map (lambda (spec)
  390. (cons spec package-with-debug-info))
  391. specs)))
  392. (lambda (obj)
  393. (if (package? obj)
  394. (rewrite obj)
  395. obj)))
  396. (define (transform-package-tests specs)
  397. "Return a procedure that, when passed a package, sets #:tests? #f in its
  398. 'arguments' field."
  399. (define (package-without-tests p)
  400. (package/inherit p
  401. (arguments
  402. (substitute-keyword-arguments (package-arguments p)
  403. ((#:tests? _ #f) #f)))))
  404. (define rewrite
  405. (package-input-rewriting/spec (map (lambda (spec)
  406. (cons spec package-without-tests))
  407. specs)))
  408. (lambda (obj)
  409. (if (package? obj)
  410. (rewrite obj)
  411. obj)))
  412. (define (transform-package-patches specs)
  413. "Return a procedure that, when passed a package, returns a package with
  414. additional patches."
  415. (define (package-with-extra-patches p patches)
  416. (if (origin? (package-source p))
  417. (package/inherit p
  418. (source (origin
  419. (inherit (package-source p))
  420. (patches (append (map (lambda (file)
  421. (local-file file))
  422. patches)
  423. (origin-patches (package-source p)))))))
  424. p))
  425. (define (coalesce-alist alist)
  426. ;; Coalesce multiple occurrences of the same key in ALIST.
  427. (let loop ((alist alist)
  428. (keys '())
  429. (mapping vlist-null))
  430. (match alist
  431. (()
  432. (map (lambda (key)
  433. (cons key (vhash-fold* cons '() key mapping)))
  434. (delete-duplicates (reverse keys))))
  435. (((key . value) . rest)
  436. (loop rest
  437. (cons key keys)
  438. (vhash-cons key value mapping))))))
  439. (define patches
  440. ;; Spec/patch alist.
  441. (coalesce-alist
  442. (map (lambda (spec)
  443. (match (string-tokenize spec %not-equal)
  444. ((spec patch)
  445. (cons spec (canonicalize-path patch)))
  446. (_
  447. (raise (formatted-message
  448. (G_ "~a: invalid package patch specification")
  449. spec)))))
  450. specs)))
  451. (define rewrite
  452. (package-input-rewriting/spec
  453. (map (match-lambda
  454. ((spec . patches)
  455. (cons spec (cut package-with-extra-patches <> patches))))
  456. patches)))
  457. (lambda (obj)
  458. (if (package? obj)
  459. (rewrite obj)
  460. obj)))
  461. (define (transform-package-latest specs)
  462. "Return a procedure that rewrites package graphs such that those in SPECS
  463. are replaced by their latest upstream version."
  464. (define (package-with-latest-upstream p)
  465. (let ((source (package-latest-release p)))
  466. (cond ((not source)
  467. (warning
  468. (G_ "could not determine latest upstream release of '~a'~%")
  469. (package-name p))
  470. p)
  471. ((string=? (upstream-source-version source)
  472. (package-version p))
  473. p)
  474. (else
  475. (unless (pair? (upstream-source-signature-urls source))
  476. (warning (G_ "cannot authenticate source of '~a', version ~a~%")
  477. (package-name p)
  478. (upstream-source-version source)))
  479. ;; TODO: Take 'upstream-source-input-changes' into account.
  480. (package
  481. (inherit p)
  482. (version (upstream-source-version source))
  483. (source source))))))
  484. (define rewrite
  485. (package-input-rewriting/spec
  486. (map (lambda (spec)
  487. (cons spec package-with-latest-upstream))
  488. specs)))
  489. (lambda (obj)
  490. (if (package? obj)
  491. (rewrite obj)
  492. obj)))
  493. (define %transformations
  494. ;; Transformations that can be applied to things to build. The car is the
  495. ;; key used in the option alist, and the cdr is the transformation
  496. ;; procedure; it is called with two arguments: the store, and a list of
  497. ;; things to build.
  498. `((with-source . ,transform-package-source)
  499. (with-input . ,transform-package-inputs)
  500. (with-graft . ,transform-package-inputs/graft)
  501. (with-branch . ,transform-package-source-branch)
  502. (with-commit . ,transform-package-source-commit)
  503. (with-git-url . ,transform-package-source-git-url)
  504. (with-c-toolchain . ,transform-package-toolchain)
  505. (with-debug-info . ,transform-package-with-debug-info)
  506. (without-tests . ,transform-package-tests)
  507. (with-patch . ,transform-package-patches)
  508. (with-latest . ,transform-package-latest)))
  509. (define (transformation-procedure key)
  510. "Return the transformation procedure associated with KEY, a symbol such as
  511. 'with-source', or #f if there is none."
  512. (any (match-lambda
  513. ((k . proc)
  514. (and (eq? k key) proc)))
  515. %transformations))
  516. ;;;
  517. ;;; Command-line handling.
  518. ;;;
  519. (define %transformation-options
  520. ;; The command-line interface to the above transformations.
  521. (let ((parser (lambda (symbol)
  522. (lambda (opt name arg result . rest)
  523. (apply values
  524. (alist-cons symbol arg result)
  525. rest)))))
  526. (list (option '("with-source") #t #f
  527. (parser 'with-source))
  528. (option '("with-input") #t #f
  529. (parser 'with-input))
  530. (option '("with-graft") #t #f
  531. (parser 'with-graft))
  532. (option '("with-branch") #t #f
  533. (parser 'with-branch))
  534. (option '("with-commit") #t #f
  535. (parser 'with-commit))
  536. (option '("with-git-url") #t #f
  537. (parser 'with-git-url))
  538. (option '("with-c-toolchain") #t #f
  539. (parser 'with-c-toolchain))
  540. (option '("with-debug-info") #t #f
  541. (parser 'with-debug-info))
  542. (option '("without-tests") #t #f
  543. (parser 'without-tests))
  544. (option '("with-patch") #t #f
  545. (parser 'with-patch))
  546. (option '("with-latest") #t #f
  547. (parser 'with-latest))
  548. (option '("help-transform") #f #f
  549. (lambda _
  550. (format #t
  551. (G_ "Available package transformation options:~%"))
  552. (show-transformation-options-help/detailed)
  553. (newline)
  554. (exit 0))))))
  555. (define (show-transformation-options-help/detailed)
  556. (display (G_ "
  557. --with-source=[PACKAGE=]SOURCE
  558. use SOURCE when building the corresponding package"))
  559. (display (G_ "
  560. --with-input=PACKAGE=REPLACEMENT
  561. replace dependency PACKAGE by REPLACEMENT"))
  562. (display (G_ "
  563. --with-graft=PACKAGE=REPLACEMENT
  564. graft REPLACEMENT on packages that refer to PACKAGE"))
  565. (display (G_ "
  566. --with-branch=PACKAGE=BRANCH
  567. build PACKAGE from the latest commit of BRANCH"))
  568. (display (G_ "
  569. --with-commit=PACKAGE=COMMIT
  570. build PACKAGE from COMMIT"))
  571. (display (G_ "
  572. --with-git-url=PACKAGE=URL
  573. build PACKAGE from the repository at URL"))
  574. (display (G_ "
  575. --with-patch=PACKAGE=FILE
  576. add FILE to the list of patches of PACKAGE"))
  577. (display (G_ "
  578. --with-latest=PACKAGE
  579. use the latest upstream release of PACKAGE"))
  580. (display (G_ "
  581. --with-c-toolchain=PACKAGE=TOOLCHAIN
  582. build PACKAGE and its dependents with TOOLCHAIN"))
  583. (display (G_ "
  584. --with-debug-info=PACKAGE
  585. build PACKAGE and preserve its debug info"))
  586. (display (G_ "
  587. --without-tests=PACKAGE
  588. build PACKAGE without running its tests")))
  589. (define (show-transformation-options-help)
  590. "Show basic help for package transformation options."
  591. (display (G_ "
  592. --help-transform list package transformation options not shown here")))
  593. (define (options->transformation opts)
  594. "Return a procedure that, when passed an object to build (package,
  595. derivation, etc.), applies the transformations specified by OPTS and returns
  596. the resulting objects. OPTS must be a list of symbol/string pairs such as:
  597. ((with-branch . \"guile-gcrypt=master\")
  598. (without-tests . \"libgcrypt\"))
  599. Each symbol names a transformation and the corresponding string is an argument
  600. to that transformation."
  601. (define applicable
  602. ;; List of applicable transformations as symbol/procedure pairs in the
  603. ;; order in which they appear on the command line.
  604. (filter-map (match-lambda
  605. ((key . value)
  606. (match (transformation-procedure key)
  607. (#f
  608. #f)
  609. (transform
  610. ;; XXX: We used to pass TRANSFORM a list of several
  611. ;; arguments, but we now pass only one, assuming that
  612. ;; transform composes well.
  613. (list key value (transform (list value)))))))
  614. (reverse opts)))
  615. (define (package-with-transformation-properties p)
  616. (package/inherit p
  617. (properties `((transformations
  618. . ,(map (match-lambda
  619. ((key value _)
  620. (cons key value)))
  621. applicable))
  622. ,@(package-properties p)))))
  623. (lambda (obj)
  624. (define (tagged-object new)
  625. (if (and (not (eq? obj new))
  626. (package? new) (not (null? applicable)))
  627. (package-with-transformation-properties new)
  628. new))
  629. (tagged-object
  630. (fold (match-lambda*
  631. (((name value transform) obj)
  632. (let ((new (transform obj)))
  633. (when (eq? new obj)
  634. (warning (G_ "transformation '~a' had no effect on ~a~%")
  635. name
  636. (if (package? obj)
  637. (package-full-name obj)
  638. obj)))
  639. new)))
  640. obj
  641. applicable))))
  642. (define (package-transformations package)
  643. "Return the transformations applied to PACKAGE according to its properties."
  644. (match (assq-ref (package-properties package) 'transformations)
  645. (#f '())
  646. (transformations transformations)))
  647. (define (manifest-entry-with-transformations entry)
  648. "Return ENTRY with an additional 'transformations' property if it's not
  649. already there."
  650. (let ((properties (manifest-entry-properties entry)))
  651. (if (assq 'transformations properties)
  652. entry
  653. (let ((item (manifest-entry-item entry)))
  654. (manifest-entry
  655. (inherit entry)
  656. (properties
  657. (match (and (package? item)
  658. (package-transformations item))
  659. ((or #f '())
  660. properties)
  661. (transformations
  662. `((transformations . ,transformations)
  663. ,@properties)))))))))