transformations.scm 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016-2022 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2021 Marius Bakke <marius@gnu.org>
  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 transformations)
  20. #:use-module ((guix config) #:select (%system))
  21. #:use-module (guix i18n)
  22. #:use-module (guix store)
  23. #:use-module (guix packages)
  24. #:use-module (guix build-system)
  25. #:use-module (guix profiles)
  26. #:use-module (guix diagnostics)
  27. #:autoload (guix download) (download-to-store)
  28. #:autoload (guix git-download) (git-reference? git-reference-url)
  29. #:autoload (guix git) (git-checkout git-checkout? git-checkout-url)
  30. #:autoload (guix upstream) (package-latest-release
  31. upstream-source-version
  32. upstream-source-signature-urls)
  33. #:autoload (guix cpu) (current-cpu cpu->gcc-architecture)
  34. #:use-module (guix utils)
  35. #:use-module (guix memoization)
  36. #:use-module (guix gexp)
  37. ;; Use the procedure that destructures "NAME-VERSION" forms.
  38. #:use-module ((guix build utils)
  39. #:select ((package-name->name+version
  40. . hyphen-package-name->name+version)))
  41. #:use-module (srfi srfi-1)
  42. #:use-module (srfi srfi-9)
  43. #:use-module (srfi srfi-11)
  44. #:use-module (srfi srfi-26)
  45. #:use-module (srfi srfi-34)
  46. #:use-module (srfi srfi-35)
  47. #:use-module (srfi srfi-37)
  48. #:use-module (ice-9 match)
  49. #:use-module (ice-9 vlist)
  50. #:export (options->transformation
  51. manifest-entry-with-transformations
  52. tunable-package?
  53. tuned-package
  54. show-transformation-options-help
  55. transformation-option-key?
  56. %transformation-options))
  57. ;;; Commentary:
  58. ;;;
  59. ;;; This module implements "package transformation options"---tools for
  60. ;;; package graph rewriting. It contains the graph rewriting logic, but also
  61. ;;; the tip of its user interface: command-line option handling.
  62. ;;;
  63. ;;; Code:
  64. (module-autoload! (current-module) '(gnu packages)
  65. '(specification->package))
  66. (define (numeric-extension? file-name)
  67. "Return true if FILE-NAME ends with digits."
  68. (string-every char-set:hex-digit (file-extension file-name)))
  69. (define (tarball-base-name file-name)
  70. "Return the \"base\" of FILE-NAME, removing '.tar.gz' or similar
  71. extensions."
  72. ;; TODO: Factorize.
  73. (cond ((not (file-extension file-name))
  74. file-name)
  75. ((numeric-extension? file-name)
  76. file-name)
  77. ((string=? (file-extension file-name) "tar")
  78. (file-sans-extension file-name))
  79. ((file-extension file-name)
  80. =>
  81. (match-lambda
  82. ("scm" file-name)
  83. (_ (tarball-base-name (file-sans-extension file-name)))))
  84. (else
  85. file-name)))
  86. ;; Files to be downloaded.
  87. (define-record-type <downloaded-file>
  88. (downloaded-file uri recursive?)
  89. downloaded-file?
  90. (uri downloaded-file-uri)
  91. (recursive? downloaded-file-recursive?))
  92. (define download-to-store*
  93. (store-lift download-to-store))
  94. (define-gexp-compiler (compile-downloaded-file (file <downloaded-file>)
  95. system target)
  96. "Download FILE and return the result as a store item."
  97. (match file
  98. (($ <downloaded-file> uri recursive?)
  99. (download-to-store* uri #:recursive? recursive?))))
  100. (define* (package-with-source p uri #:optional version)
  101. "Return a package based on P but with its source taken from URI. Extract
  102. the new package's version number from URI."
  103. (let ((base (tarball-base-name (basename uri))))
  104. (let-values (((_ version*)
  105. (hyphen-package-name->name+version base)))
  106. (package (inherit p)
  107. (version (or version version*
  108. (package-version p)))
  109. ;; Use #:recursive? #t to allow for directories.
  110. (source (downloaded-file uri #t))))))
  111. ;;;
  112. ;;; Transformations.
  113. ;;;
  114. (define (evaluate-source-replacement-specs specs)
  115. "Parse SPECS, a list of strings like \"guile=/tmp/guile-4.2.tar.gz\" or just
  116. \"/tmp/guile-4.2.tar.gz\" and return a list of package spec/procedure pairs as
  117. expected by 'package-input-rewriting/spec'. Raise an error if an element of
  118. SPECS uses invalid syntax."
  119. (define not-equal
  120. (char-set-complement (char-set #\=)))
  121. (map (lambda (spec)
  122. (match (string-tokenize spec not-equal)
  123. ((uri)
  124. (let* ((base (tarball-base-name (basename uri)))
  125. (name (hyphen-package-name->name+version base)))
  126. (cons name
  127. (lambda (old)
  128. (package-with-source old uri)))))
  129. ((spec uri)
  130. (let-values (((name version)
  131. (package-name->name+version spec)))
  132. ;; Note: Here VERSION is used as the version string of the new
  133. ;; package rather than as part of the spec of the package being
  134. ;; targeted.
  135. (cons name
  136. (lambda (old)
  137. (package-with-source old uri version)))))
  138. (_
  139. (raise (formatted-message
  140. (G_ "invalid source replacement specification: ~s")
  141. spec)))))
  142. specs))
  143. (define (transform-package-source replacement-specs)
  144. "Return a transformation procedure that replaces package sources with the
  145. matching URIs given in REPLACEMENT-SPECS."
  146. (let* ((replacements (evaluate-source-replacement-specs replacement-specs))
  147. (rewrite (package-input-rewriting/spec replacements)))
  148. (lambda (obj)
  149. (if (package? obj)
  150. (rewrite obj)
  151. obj))))
  152. (define (evaluate-replacement-specs specs proc)
  153. "Parse SPECS, a list of strings like \"guile=guile@2.1\" and return a list
  154. of package spec/procedure pairs as expected by 'package-input-rewriting/spec'.
  155. PROC is called with the package to be replaced and its replacement according
  156. to SPECS. Raise an error if an element of SPECS uses invalid syntax, or if a
  157. package it refers to could not be found."
  158. (define not-equal
  159. (char-set-complement (char-set #\=)))
  160. (map (lambda (spec)
  161. (match (string-tokenize spec not-equal)
  162. ((spec new)
  163. (cons spec
  164. (let ((new (specification->package new)))
  165. (lambda (old)
  166. (proc old new)))))
  167. (x
  168. (raise (formatted-message
  169. (G_ "invalid replacement specification: ~s")
  170. spec)))))
  171. specs))
  172. (define (transform-package-inputs replacement-specs)
  173. "Return a procedure that, when passed a package, replaces its direct
  174. dependencies according to REPLACEMENT-SPECS. REPLACEMENT-SPECS is a list of
  175. strings like \"guile=guile@2.1\" meaning that, any dependency on a package
  176. called \"guile\" must be replaced with a dependency on a version 2.1 of
  177. \"guile\"."
  178. (let* ((replacements (evaluate-replacement-specs replacement-specs
  179. (lambda (old new)
  180. new)))
  181. (rewrite (package-input-rewriting/spec replacements)))
  182. (lambda (obj)
  183. (if (package? obj)
  184. (rewrite obj)
  185. obj))))
  186. (define (transform-package-inputs/graft replacement-specs)
  187. "Return a procedure that, when passed a package, replaces its direct
  188. dependencies according to REPLACEMENT-SPECS. REPLACEMENT-SPECS is a list of
  189. strings like \"gnutls=gnutls@3.5.4\" meaning that packages are built using the
  190. current 'gnutls' package, after which version 3.5.4 is grafted onto them."
  191. (define (set-replacement old new)
  192. (package (inherit old) (replacement new)))
  193. (let* ((replacements (evaluate-replacement-specs replacement-specs
  194. set-replacement))
  195. (rewrite (package-input-rewriting/spec replacements)))
  196. (lambda (obj)
  197. (if (package? obj)
  198. (rewrite obj)
  199. obj))))
  200. (define %not-equal
  201. (char-set-complement (char-set #\=)))
  202. (define (package-git-url package)
  203. "Return the URL of the Git repository for package, or raise an error if
  204. the source of PACKAGE is not fetched from a Git repository."
  205. (let ((source (package-source package)))
  206. (cond ((and (origin? source)
  207. (git-reference? (origin-uri source)))
  208. (git-reference-url (origin-uri source)))
  209. ((git-checkout? source)
  210. (git-checkout-url source))
  211. (else
  212. (raise
  213. (formatted-message (G_ "the source of ~a is not a Git reference")
  214. (package-full-name package)))))))
  215. (define (evaluate-git-replacement-specs specs proc)
  216. "Parse SPECS, a list of strings like \"guile=stable-2.2\", and return a list
  217. of package pairs, where (PROC PACKAGE URL BRANCH-OR-COMMIT) returns the
  218. replacement package. Raise an error if an element of SPECS uses invalid
  219. syntax, or if a package it refers to could not be found."
  220. (map (lambda (spec)
  221. (match (string-tokenize spec %not-equal)
  222. ((spec branch-or-commit)
  223. (define (replace old)
  224. (let* ((source (package-source old))
  225. (url (package-git-url old)))
  226. (proc old url branch-or-commit)))
  227. (cons spec replace))
  228. (_
  229. (raise
  230. (formatted-message (G_ "invalid replacement specification: ~s")
  231. spec)))))
  232. specs))
  233. (define (transform-package-source-branch replacement-specs)
  234. "Return a procedure that, when passed a package, replaces its direct
  235. dependencies according to REPLACEMENT-SPECS. REPLACEMENT-SPECS is a list of
  236. strings like \"guile-next=stable-3.0\" meaning that packages are built using
  237. 'guile-next' from the latest commit on its 'stable-3.0' branch."
  238. (define (replace old url branch)
  239. (package
  240. (inherit old)
  241. (version (string-append "git." (string-map (match-lambda
  242. (#\/ #\-)
  243. (chr chr))
  244. branch)))
  245. (source (git-checkout (url url) (branch branch)
  246. (recursive? #t)))))
  247. (let* ((replacements (evaluate-git-replacement-specs replacement-specs
  248. replace))
  249. (rewrite (package-input-rewriting/spec replacements)))
  250. (lambda (obj)
  251. (if (package? obj)
  252. (rewrite obj)
  253. obj))))
  254. (define (commit->version-string commit)
  255. "Return a string suitable for use in the 'version' field of a package based
  256. on the given COMMIT."
  257. (cond ((and (> (string-length commit) 1)
  258. (string-prefix? "v" commit)
  259. (char-set-contains? char-set:digit
  260. (string-ref commit 1)))
  261. ;; Probably a tag like "v1.0" or a 'git describe' identifier.
  262. (string-drop commit 1))
  263. ((not (string-every char-set:hex-digit commit))
  264. ;; Pass through tags and 'git describe' style IDs directly.
  265. commit)
  266. (else
  267. (string-append "git."
  268. (if (< (string-length commit) 7)
  269. commit
  270. (string-take commit 7))))))
  271. (define (transform-package-source-commit replacement-specs)
  272. "Return a procedure that, when passed a package, replaces its direct
  273. dependencies according to REPLACEMENT-SPECS. REPLACEMENT-SPECS is a list of
  274. strings like \"guile-next=cabba9e\" meaning that packages are built using
  275. 'guile-next' from commit 'cabba9e'."
  276. (define (replace old url commit)
  277. (package
  278. (inherit old)
  279. (version (commit->version-string commit))
  280. (source (git-checkout (url url) (commit commit)
  281. (recursive? #t)))))
  282. (let* ((replacements (evaluate-git-replacement-specs replacement-specs
  283. replace))
  284. (rewrite (package-input-rewriting/spec replacements)))
  285. (lambda (obj)
  286. (if (package? obj)
  287. (rewrite obj)
  288. obj))))
  289. (define (transform-package-source-git-url replacement-specs)
  290. "Return a procedure that, when passed a package, replaces its dependencies
  291. according to REPLACEMENT-SPECS. REPLACEMENT-SPECS is a list of strings like
  292. \"guile-json=https://gitthing.com/…\" meaning that packages are built using
  293. a checkout of the Git repository at the given URL."
  294. (define replacements
  295. (map (lambda (spec)
  296. (match (string-tokenize spec %not-equal)
  297. ((spec url)
  298. (cons spec
  299. (lambda (old)
  300. (package
  301. (inherit old)
  302. (source (git-checkout (url url)
  303. (recursive? #t)))))))
  304. (_
  305. (raise
  306. (formatted-message
  307. (G_ "~a: invalid Git URL replacement specification")
  308. spec)))))
  309. replacement-specs))
  310. (define rewrite
  311. (package-input-rewriting/spec replacements))
  312. (lambda (obj)
  313. (if (package? obj)
  314. (rewrite obj)
  315. obj)))
  316. (define (package-dependents/spec top bottom)
  317. "Return the list of dependents of BOTTOM, a spec string, that are also
  318. dependencies of TOP, a package."
  319. (define-values (name version)
  320. (package-name->name+version bottom))
  321. (define dependent?
  322. (mlambda (p)
  323. (and (package? p)
  324. (or (and (string=? name (package-name p))
  325. (or (not version)
  326. (version-prefix? version (package-version p))))
  327. (match (bag-direct-inputs (package->bag p))
  328. (((labels dependencies . _) ...)
  329. (any dependent? dependencies)))))))
  330. (filter dependent? (package-closure (list top))))
  331. (define (package-toolchain-rewriting p bottom toolchain)
  332. "Return a procedure that, when passed a package that's either BOTTOM or one
  333. of its dependents up to P so, changes it so it is built with TOOLCHAIN.
  334. TOOLCHAIN must be an input list."
  335. (define rewriting-property
  336. (gensym " package-toolchain-rewriting"))
  337. (match (package-dependents/spec p bottom)
  338. (() ;P does not depend on BOTTOM
  339. identity)
  340. (set
  341. ;; SET is the list of packages "between" P and BOTTOM (included) whose
  342. ;; toolchain needs to be changed.
  343. (package-mapping (lambda (p)
  344. (if (or (assq rewriting-property
  345. (package-properties p))
  346. (not (memq p set)))
  347. p
  348. (let ((p (package-with-c-toolchain p toolchain)))
  349. (package/inherit p
  350. (properties `((,rewriting-property . #t)
  351. ,@(package-properties p)))))))
  352. (lambda (p)
  353. (or (assq rewriting-property (package-properties p))
  354. (not (memq p set))))
  355. #:deep? #t))))
  356. (define (transform-package-toolchain replacement-specs)
  357. "Return a procedure that, when passed a package, changes its toolchain or
  358. that of its dependencies according to REPLACEMENT-SPECS. REPLACEMENT-SPECS is
  359. a list of strings like \"fftw=gcc-toolchain@10\" meaning that the package to
  360. the left of the equal sign must be built with the toolchain to the right of
  361. the equal sign."
  362. (define split-on-commas
  363. (cute string-tokenize <> (char-set-complement (char-set #\,))))
  364. (define (specification->input spec)
  365. (let ((package (specification->package spec)))
  366. (list (package-name package) package)))
  367. (define replacements
  368. (map (lambda (spec)
  369. (match (string-tokenize spec %not-equal)
  370. ((spec (= split-on-commas toolchain))
  371. (cons spec (map specification->input toolchain)))
  372. (_
  373. (raise
  374. (formatted-message
  375. (G_ "~a: invalid toolchain replacement specification")
  376. spec)))))
  377. replacement-specs))
  378. (lambda (obj)
  379. (if (package? obj)
  380. (or (any (match-lambda
  381. ((bottom . toolchain)
  382. ((package-toolchain-rewriting obj bottom toolchain) obj)))
  383. replacements)
  384. obj)
  385. obj)))
  386. (define tuning-compiler
  387. (mlambda (micro-architecture)
  388. "Return a compiler wrapper that passes '-march=MICRO-ARCHITECTURE' to the
  389. actual compiler."
  390. (define wrapper
  391. #~(begin
  392. (use-modules (ice-9 match))
  393. (define* (search-next command
  394. #:optional
  395. (path (string-split (getenv "PATH")
  396. #\:)))
  397. ;; Search the next COMMAND on PATH, a list of
  398. ;; directories representing the executable search path.
  399. (define this
  400. (stat (car (command-line))))
  401. (let loop ((path path))
  402. (match path
  403. (()
  404. (match command
  405. ("cc" (search-next "gcc"))
  406. (_ #f)))
  407. ((directory rest ...)
  408. (let* ((file (string-append
  409. directory "/" command))
  410. (st (stat file #f)))
  411. (if (and st (not (equal? this st)))
  412. file
  413. (loop rest)))))))
  414. (match (command-line)
  415. ((command arguments ...)
  416. (match (search-next (basename command))
  417. (#f (exit 127))
  418. (next
  419. (apply execl next
  420. (append (cons next arguments)
  421. (list (string-append "-march="
  422. #$micro-architecture))))))))))
  423. (define program
  424. (program-file (string-append "tuning-compiler-wrapper-" micro-architecture)
  425. wrapper))
  426. (computed-file (string-append "tuning-compiler-" micro-architecture)
  427. (with-imported-modules '((guix build utils))
  428. #~(begin
  429. (use-modules (guix build utils))
  430. (define bin (string-append #$output "/bin"))
  431. (mkdir-p bin)
  432. (for-each (lambda (program)
  433. (symlink #$program
  434. (string-append bin "/" program)))
  435. '("cc" "gcc" "clang" "g++" "c++" "clang++")))))))
  436. (define (build-system-with-tuning-compiler bs micro-architecture)
  437. "Return a variant of BS, a build system, that ensures that the compiler that
  438. BS uses (usually an implicit input) can generate code for MICRO-ARCHITECTURE,
  439. which names a specific CPU of the target architecture--e.g., when targeting
  440. 86_64 MICRO-ARCHITECTURE might be \"skylake\". If it does, return a build
  441. system that builds code for MICRO-ARCHITECTURE; otherwise raise an error."
  442. (define %not-hyphen
  443. (char-set-complement (char-set #\-)))
  444. (define lower
  445. (build-system-lower bs))
  446. (define (lower* . args)
  447. ;; The list of CPU names supported by the '-march' option of C/C++
  448. ;; compilers is specific to each compiler and version thereof. Rather
  449. ;; than pass '-march=MICRO-ARCHITECTURE' as is to the compiler, possibly
  450. ;; leading to an obscure build error, check whether the compiler is known
  451. ;; to support MICRO-ARCHITECTURE. If not, bail out.
  452. (let* ((lowered (apply lower args))
  453. (architecture (match (string-tokenize (bag-system lowered)
  454. %not-hyphen)
  455. ((arch _ ...) arch)))
  456. (compiler (any (match-lambda
  457. ((label (? package? p) . _)
  458. (and (assoc-ref (package-properties p)
  459. 'compiler-cpu-architectures)
  460. p))
  461. (_ #f))
  462. (bag-build-inputs lowered))))
  463. (unless compiler
  464. (raise (formatted-message
  465. (G_ "failed to determine which compiler is used"))))
  466. (let ((lst (assoc-ref (package-properties compiler)
  467. 'compiler-cpu-architectures)))
  468. (unless lst
  469. (raise (formatted-message
  470. (G_ "failed to determine whether ~a supports ~a")
  471. (package-full-name compiler)
  472. micro-architecture)))
  473. (unless (member micro-architecture
  474. (or (assoc-ref lst architecture) '()))
  475. (raise
  476. (make-compound-condition
  477. (formatted-message
  478. (G_ "compiler ~a does not support micro-architecture ~a")
  479. (package-full-name compiler)
  480. micro-architecture)
  481. (condition
  482. (&fix-hint
  483. (hint (match (assoc-ref lst architecture)
  484. (#f (format #f (G_ "Compiler ~a does not support
  485. micro-architectures of ~a.")
  486. (package-full-name compiler "@@")
  487. architecture))
  488. (lst
  489. (format #f (G_ "Compiler ~a supports the following ~a
  490. micro-architectures:
  491. @quotation
  492. ~a
  493. @end quotation")
  494. (package-full-name compiler "@@")
  495. architecture
  496. (string-join lst ", ")))))))))))
  497. (bag
  498. (inherit lowered)
  499. (build-inputs
  500. ;; Arrange so that the compiler wrapper comes first in $PATH.
  501. `(("tuning-compiler" ,(tuning-compiler micro-architecture))
  502. ,@(bag-build-inputs lowered))))))
  503. (build-system
  504. (inherit bs)
  505. (lower lower*)))
  506. (define (tuned-package p micro-architecture)
  507. "Return package P tuned for MICRO-ARCHITECTURE."
  508. (package
  509. (inherit p)
  510. (build-system
  511. (build-system-with-tuning-compiler (package-build-system p)
  512. micro-architecture))
  513. (arguments
  514. ;; The machine building this package may or may not be able to run code
  515. ;; for MICRO-ARCHITECTURE. Because of that, skip tests; they are run for
  516. ;; the "baseline" variant anyway.
  517. (substitute-keyword-arguments (package-arguments p)
  518. ((#:tests? _ #f) #f)))
  519. (properties
  520. `((cpu-tuning . ,micro-architecture)
  521. ;; Remove the 'tunable?' property so that 'package-tuning' does not
  522. ;; call 'tuned-package' again on this one.
  523. ,@(alist-delete 'tunable? (package-properties p))))))
  524. (define (tunable-package? package)
  525. "Return true if package PACKAGE is \"tunable\"--i.e., if tuning it for the
  526. host CPU is worthwhile."
  527. (assq 'tunable? (package-properties package)))
  528. (define package-tuning
  529. (mlambda (micro-architecture)
  530. "Return a procedure that maps the given package to its counterpart tuned
  531. for MICRO-ARCHITECTURE, a string suitable for GCC's '-march'."
  532. (define rewriting-property
  533. (gensym " package-tuning"))
  534. (package-mapping (lambda (p)
  535. (cond ((assq rewriting-property (package-properties p))
  536. p)
  537. ((assq 'tunable? (package-properties p))
  538. (info (G_ "tuning ~a for CPU ~a~%")
  539. (package-full-name p) micro-architecture)
  540. (package/inherit p
  541. (replacement (tuned-package p micro-architecture))
  542. (properties `((,rewriting-property . #t)
  543. ,@(package-properties p)))))
  544. (else
  545. p)))
  546. (lambda (p)
  547. (assq rewriting-property (package-properties p)))
  548. #:deep? #t)))
  549. (define (transform-package-tuning micro-architectures)
  550. "Return a procedure that, when "
  551. (match micro-architectures
  552. ((micro-architecture _ ...)
  553. (let ((rewrite (package-tuning micro-architecture)))
  554. (lambda (obj)
  555. (if (package? obj)
  556. (rewrite obj)
  557. obj))))))
  558. (define (transform-package-with-debug-info specs)
  559. "Return a procedure that, when passed a package, set its 'replacement' field
  560. to the same package but with #:strip-binaries? #f in its 'arguments' field."
  561. (define (non-stripped p)
  562. (package
  563. (inherit p)
  564. (arguments
  565. (substitute-keyword-arguments (package-arguments p)
  566. ((#:strip-binaries? _ #f) #f)))))
  567. (define (package-with-debug-info p)
  568. (if (member "debug" (package-outputs p))
  569. p
  570. (let loop ((p p))
  571. (match (package-replacement p)
  572. (#f
  573. (package
  574. (inherit p)
  575. (replacement (non-stripped p))))
  576. (next
  577. (package
  578. (inherit p)
  579. (replacement (loop next))))))))
  580. (define rewrite
  581. (package-input-rewriting/spec (map (lambda (spec)
  582. (cons spec package-with-debug-info))
  583. specs)))
  584. (lambda (obj)
  585. (if (package? obj)
  586. (rewrite obj)
  587. obj)))
  588. (define (transform-package-tests specs)
  589. "Return a procedure that, when passed a package, sets #:tests? #f in its
  590. 'arguments' field."
  591. (define (package-without-tests p)
  592. (package/inherit p
  593. (arguments
  594. (substitute-keyword-arguments (package-arguments p)
  595. ((#:tests? _ #f) #f)))))
  596. (define rewrite
  597. (package-input-rewriting/spec (map (lambda (spec)
  598. (cons spec package-without-tests))
  599. specs)))
  600. (lambda (obj)
  601. (if (package? obj)
  602. (rewrite obj)
  603. obj)))
  604. (define (patched-source name source patches)
  605. "Return a file-like object with the given NAME that applies PATCHES to
  606. SOURCE. SOURCE must itself be a file-like object of any type, including
  607. <git-checkout>, <local-file>, etc."
  608. (define patch
  609. (module-ref (resolve-interface '(gnu packages base)) 'patch))
  610. (computed-file name
  611. (with-imported-modules '((guix build utils))
  612. #~(begin
  613. (use-modules (guix build utils))
  614. (setenv "PATH" #+(file-append patch "/bin"))
  615. ;; XXX: Assume SOURCE is a directory. This is true in
  616. ;; most practical cases, where it's a <git-checkout>.
  617. (copy-recursively #+source #$output)
  618. (chdir #$output)
  619. (for-each (lambda (patch)
  620. (invoke "patch" "-p1" "--batch"
  621. "-i" patch))
  622. '(#+@patches))))))
  623. (define (transform-package-patches specs)
  624. "Return a procedure that, when passed a package, returns a package with
  625. additional patches."
  626. (define (package-with-extra-patches p patches)
  627. (let ((patches (map (lambda (file)
  628. (local-file file))
  629. patches)))
  630. (if (origin? (package-source p))
  631. (package/inherit p
  632. (source (origin
  633. (inherit (package-source p))
  634. (patches (append patches
  635. (origin-patches (package-source p)))))))
  636. (package/inherit p
  637. (source (patched-source (string-append (package-full-name p "-")
  638. "-source")
  639. (package-source p) patches))))))
  640. (define (coalesce-alist alist)
  641. ;; Coalesce multiple occurrences of the same key in ALIST.
  642. (let loop ((alist alist)
  643. (keys '())
  644. (mapping vlist-null))
  645. (match alist
  646. (()
  647. (map (lambda (key)
  648. (cons key (vhash-fold* cons '() key mapping)))
  649. (delete-duplicates (reverse keys))))
  650. (((key . value) . rest)
  651. (loop rest
  652. (cons key keys)
  653. (vhash-cons key value mapping))))))
  654. (define patches
  655. ;; Spec/patch alist.
  656. (coalesce-alist
  657. (map (lambda (spec)
  658. (match (string-tokenize spec %not-equal)
  659. ((spec patch)
  660. (cons spec (canonicalize-path patch)))
  661. (_
  662. (raise (formatted-message
  663. (G_ "~a: invalid package patch specification")
  664. spec)))))
  665. specs)))
  666. (define rewrite
  667. (package-input-rewriting/spec
  668. (map (match-lambda
  669. ((spec . patches)
  670. (cons spec (cut package-with-extra-patches <> patches))))
  671. patches)))
  672. (lambda (obj)
  673. (if (package? obj)
  674. (rewrite obj)
  675. obj)))
  676. (define (transform-package-latest specs)
  677. "Return a procedure that rewrites package graphs such that those in SPECS
  678. are replaced by their latest upstream version."
  679. (define (package-with-latest-upstream p)
  680. (let ((source (package-latest-release p)))
  681. (cond ((not source)
  682. (warning
  683. (G_ "could not determine latest upstream release of '~a'~%")
  684. (package-name p))
  685. p)
  686. ((string=? (upstream-source-version source)
  687. (package-version p))
  688. p)
  689. (else
  690. (unless (pair? (upstream-source-signature-urls source))
  691. (warning (G_ "cannot authenticate source of '~a', version ~a~%")
  692. (package-name p)
  693. (upstream-source-version source)))
  694. ;; TODO: Take 'upstream-source-input-changes' into account.
  695. (package
  696. (inherit p)
  697. (version (upstream-source-version source))
  698. (source source))))))
  699. (define rewrite
  700. (package-input-rewriting/spec
  701. (map (lambda (spec)
  702. (cons spec package-with-latest-upstream))
  703. specs)))
  704. (lambda (obj)
  705. (if (package? obj)
  706. (rewrite obj)
  707. obj)))
  708. (define %transformations
  709. ;; Transformations that can be applied to things to build. The car is the
  710. ;; key used in the option alist, and the cdr is the transformation
  711. ;; procedure; it is called with two arguments: the store, and a list of
  712. ;; things to build.
  713. `((with-source . ,transform-package-source)
  714. (with-input . ,transform-package-inputs)
  715. (with-graft . ,transform-package-inputs/graft)
  716. (with-branch . ,transform-package-source-branch)
  717. (with-commit . ,transform-package-source-commit)
  718. (with-git-url . ,transform-package-source-git-url)
  719. (with-c-toolchain . ,transform-package-toolchain)
  720. (tune . ,transform-package-tuning)
  721. (with-debug-info . ,transform-package-with-debug-info)
  722. (without-tests . ,transform-package-tests)
  723. (with-patch . ,transform-package-patches)
  724. (with-latest . ,transform-package-latest)))
  725. (define (transformation-procedure key)
  726. "Return the transformation procedure associated with KEY, a symbol such as
  727. 'with-source', or #f if there is none."
  728. (any (match-lambda
  729. ((k . proc)
  730. (and (eq? k key) proc)))
  731. %transformations))
  732. (define (transformation-option-key? key)
  733. "Return true if KEY is an option key (as returned while parsing options with
  734. %TRANSFORMATION-OPTIONS) corresponding to a package transformation option.
  735. For example, (transformation-option-key? 'with-input) => #t."
  736. (->bool (transformation-procedure key)))
  737. ;;;
  738. ;;; Command-line handling.
  739. ;;;
  740. (define %transformation-options
  741. ;; The command-line interface to the above transformations.
  742. (let ((parser (lambda (symbol)
  743. (lambda (opt name arg result . rest)
  744. (apply values
  745. (alist-cons symbol arg result)
  746. rest)))))
  747. (list (option '("with-source") #t #f
  748. (parser 'with-source))
  749. (option '("with-input") #t #f
  750. (parser 'with-input))
  751. (option '("with-graft") #t #f
  752. (parser 'with-graft))
  753. (option '("with-branch") #t #f
  754. (parser 'with-branch))
  755. (option '("with-commit") #t #f
  756. (parser 'with-commit))
  757. (option '("with-git-url") #t #f
  758. (parser 'with-git-url))
  759. (option '("with-c-toolchain") #t #f
  760. (parser 'with-c-toolchain))
  761. (option '("tune") #f #t
  762. (lambda (opt name arg result . rest)
  763. (define micro-architecture
  764. (match arg
  765. ((or #f "native")
  766. (unless (string=? (or (assoc-ref result 'system)
  767. (%current-system))
  768. %system)
  769. (leave (G_ "\
  770. building for ~a instead of ~a, so tuning cannot be guessed~%")
  771. (assoc-ref result 'system) %system))
  772. (cpu->gcc-architecture (current-cpu)))
  773. ("generic" #f)
  774. (_ arg)))
  775. (apply values
  776. (if micro-architecture
  777. (alist-cons 'tune micro-architecture
  778. result)
  779. (alist-delete 'tune result))
  780. rest)))
  781. (option '("with-debug-info") #t #f
  782. (parser 'with-debug-info))
  783. (option '("without-tests") #t #f
  784. (parser 'without-tests))
  785. (option '("with-patch") #t #f
  786. (parser 'with-patch))
  787. (option '("with-latest") #t #f
  788. (parser 'with-latest))
  789. (option '("help-transform") #f #f
  790. (lambda _
  791. (format #t
  792. (G_ "Available package transformation options:~%"))
  793. (show-transformation-options-help/detailed)
  794. (newline)
  795. (exit 0))))))
  796. (define (show-transformation-options-help/detailed)
  797. (display (G_ "
  798. --with-source=[PACKAGE=]SOURCE
  799. use SOURCE when building the corresponding package"))
  800. (display (G_ "
  801. --with-input=PACKAGE=REPLACEMENT
  802. replace dependency PACKAGE by REPLACEMENT"))
  803. (display (G_ "
  804. --with-graft=PACKAGE=REPLACEMENT
  805. graft REPLACEMENT on packages that refer to PACKAGE"))
  806. (display (G_ "
  807. --with-branch=PACKAGE=BRANCH
  808. build PACKAGE from the latest commit of BRANCH"))
  809. (display (G_ "
  810. --with-commit=PACKAGE=COMMIT
  811. build PACKAGE from COMMIT"))
  812. (display (G_ "
  813. --with-git-url=PACKAGE=URL
  814. build PACKAGE from the repository at URL"))
  815. (display (G_ "
  816. --with-patch=PACKAGE=FILE
  817. add FILE to the list of patches of PACKAGE"))
  818. (display (G_ "
  819. --with-latest=PACKAGE
  820. use the latest upstream release of PACKAGE"))
  821. (display (G_ "
  822. --with-c-toolchain=PACKAGE=TOOLCHAIN
  823. build PACKAGE and its dependents with TOOLCHAIN"))
  824. (display (G_ "
  825. --with-debug-info=PACKAGE
  826. build PACKAGE and preserve its debug info"))
  827. (display (G_ "
  828. --without-tests=PACKAGE
  829. build PACKAGE without running its tests")))
  830. (define (show-transformation-options-help)
  831. "Show basic help for package transformation options."
  832. (display (G_ "
  833. --help-transform list package transformation options not shown here")))
  834. (define (options->transformation opts)
  835. "Return a procedure that, when passed an object to build (package,
  836. derivation, etc.), applies the transformations specified by OPTS and returns
  837. the resulting objects. OPTS must be a list of symbol/string pairs such as:
  838. ((with-branch . \"guile-gcrypt=master\")
  839. (without-tests . \"libgcrypt\"))
  840. Each symbol names a transformation and the corresponding string is an argument
  841. to that transformation."
  842. (define applicable
  843. ;; List of applicable transformations as symbol/procedure pairs in the
  844. ;; order in which they appear on the command line.
  845. (filter-map (match-lambda
  846. ((key . value)
  847. (match (transformation-procedure key)
  848. (#f
  849. #f)
  850. (transform
  851. ;; XXX: We used to pass TRANSFORM a list of several
  852. ;; arguments, but we now pass only one, assuming that
  853. ;; transform composes well.
  854. (list key value (transform (list value)))))))
  855. (reverse opts)))
  856. (define (package-with-transformation-properties p)
  857. (package/inherit p
  858. (properties `((transformations
  859. . ,(map (match-lambda
  860. ((key value _)
  861. (cons key value)))
  862. (reverse applicable))) ;preserve order
  863. ,@(package-properties p)))))
  864. (lambda (obj)
  865. (define (tagged-object new)
  866. (if (and (not (eq? obj new))
  867. (package? new) (not (null? applicable)))
  868. (package-with-transformation-properties new)
  869. new))
  870. (tagged-object
  871. (fold (match-lambda*
  872. (((name value transform) obj)
  873. (let ((new (transform obj)))
  874. (when (eq? new obj)
  875. (warning (G_ "transformation '~a' had no effect on ~a~%")
  876. name
  877. (if (package? obj)
  878. (package-full-name obj)
  879. obj)))
  880. new)))
  881. obj
  882. applicable))))
  883. (define (package-transformations package)
  884. "Return the transformations applied to PACKAGE according to its properties."
  885. (match (assq-ref (package-properties package) 'transformations)
  886. (#f '())
  887. (transformations transformations)))
  888. (define (manifest-entry-with-transformations entry)
  889. "Return ENTRY with an additional 'transformations' property if it's not
  890. already there."
  891. (let ((properties (manifest-entry-properties entry)))
  892. (if (assq 'transformations properties)
  893. entry
  894. (let ((item (manifest-entry-item entry)))
  895. (manifest-entry
  896. (inherit entry)
  897. (properties
  898. (match (and (package? item)
  899. (package-transformations item))
  900. ((or #f '())
  901. properties)
  902. (transformations
  903. `((transformations . ,transformations)
  904. ,@properties)))))))))