transformations.scm 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016-2023 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2021 Marius Bakke <marius@gnu.org>
  4. ;;; Copyright © 2023 Sarthak Shah <shahsarthakw@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 transformations)
  21. #:use-module ((guix config) #:select (%system))
  22. #:use-module (guix i18n)
  23. #:use-module (guix store)
  24. #:use-module (guix packages)
  25. #:use-module (guix build-system)
  26. #:use-module (guix profiles)
  27. #:use-module (guix diagnostics)
  28. #:autoload (guix download) (download-to-store)
  29. #:autoload (guix git-download) (git-reference? git-reference-url)
  30. #:autoload (guix git) (git-checkout git-checkout? git-checkout-url)
  31. #:autoload (guix upstream) (package-latest-release
  32. upstream-source-version
  33. upstream-source-signature-urls)
  34. #:autoload (guix cpu) (current-cpu cpu->gcc-architecture)
  35. #:use-module (guix utils)
  36. #:use-module (guix memoization)
  37. #:use-module (guix gexp)
  38. #:autoload (guix parameters) (package-parameter-alist parameterize-package) ; g23
  39. ;; Use the procedure that destructures "NAME-VERSION" forms.
  40. #:use-module ((guix build utils)
  41. #:select ((package-name->name+version
  42. . hyphen-package-name->name+version)))
  43. #:use-module (srfi srfi-1)
  44. #:use-module (srfi srfi-9)
  45. #:use-module (srfi srfi-26)
  46. #:use-module (srfi srfi-34)
  47. #:use-module (srfi srfi-35)
  48. #:use-module (srfi srfi-37)
  49. #:use-module (srfi srfi-71)
  50. #:use-module (ice-9 match)
  51. #:use-module (ice-9 vlist)
  52. #:export (options->transformation
  53. manifest-entry-with-transformations
  54. tunable-package?
  55. tuned-package
  56. show-transformation-options-help
  57. transformation-option-key?
  58. %transformation-options))
  59. ;;; Commentary:
  60. ;;;
  61. ;;; This module implements "package transformation options"---tools for
  62. ;;; package graph rewriting. It contains the graph rewriting logic, but also
  63. ;;; the tip of its user interface: command-line option handling.
  64. ;;;
  65. ;;; Code:
  66. (module-autoload! (current-module) '(gnu packages)
  67. '(specification->package))
  68. (define (numeric-extension? file-name)
  69. "Return true if FILE-NAME ends with digits."
  70. (string-every char-set:hex-digit (file-extension file-name)))
  71. (define (tarball-base-name file-name)
  72. "Return the \"base\" of FILE-NAME, removing '.tar.gz' or similar
  73. extensions."
  74. ;; TODO: Factorize.
  75. (cond ((not (file-extension file-name))
  76. file-name)
  77. ((numeric-extension? file-name)
  78. file-name)
  79. ((string=? (file-extension file-name) "tar")
  80. (file-sans-extension file-name))
  81. ((file-extension file-name)
  82. =>
  83. (match-lambda
  84. ("scm" file-name)
  85. (_ (tarball-base-name (file-sans-extension file-name)))))
  86. (else
  87. file-name)))
  88. ;; Files to be downloaded.
  89. (define-record-type <downloaded-file>
  90. (downloaded-file uri recursive?)
  91. downloaded-file?
  92. (uri downloaded-file-uri)
  93. (recursive? downloaded-file-recursive?))
  94. (define download-to-store*
  95. (store-lift download-to-store))
  96. (define-gexp-compiler (compile-downloaded-file (file <downloaded-file>)
  97. system target)
  98. "Download FILE and return the result as a store item."
  99. (match file
  100. (($ <downloaded-file> uri recursive?)
  101. (download-to-store* uri #:recursive? recursive?))))
  102. (define* (package-with-source p uri #:optional version)
  103. "Return a package based on P but with its source taken from URI. Extract
  104. the new package's version number from URI."
  105. (let ((base (tarball-base-name (basename uri))))
  106. (let ((_ version* (hyphen-package-name->name+version base)))
  107. (package (inherit p)
  108. (version (or version version*
  109. (package-version p)))
  110. ;; Use #:recursive? #t to allow for directories.
  111. (source (downloaded-file uri #t))))))
  112. ;;;
  113. ;;; Transformations.
  114. ;;;
  115. (define (evaluate-source-replacement-specs specs)
  116. "Parse SPECS, a list of strings like \"guile=/tmp/guile-4.2.tar.gz\" or just
  117. \"/tmp/guile-4.2.tar.gz\" and return a list of package spec/procedure pairs as
  118. expected by 'package-input-rewriting/spec'. Raise an error if an element of
  119. SPECS uses invalid syntax."
  120. (define not-equal
  121. (char-set-complement (char-set #\=)))
  122. (map (lambda (spec)
  123. (match (string-tokenize spec not-equal)
  124. ((uri)
  125. (let* ((base (tarball-base-name (basename uri)))
  126. (name (hyphen-package-name->name+version base)))
  127. (cons name
  128. (lambda (old)
  129. (package-with-source old uri)))))
  130. ((spec uri)
  131. (let ((name version (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. ;; {g23
  317. ;; how it works:
  318. ;; pkg = parameter = val
  319. ;; collect all ( parameter = val ) pairs for pkg and then apply them
  320. (define (evaluate-parameter-specs specs)
  321. "Parse SPECS, a list of strings like \"bitlbee=purple=true\", and return a
  322. list of spec/procedure pairs, where (PROC PACKAGE PARAMETER VALUE) is called
  323. to return the replacement package. Raise an error if an element of SPECS uses
  324. invalid syntax, or if a package it refers to could not be found."
  325. (let [(package-assq '())]
  326. (map (lambda (spec)
  327. (match (string-tokenize spec %not-equal)
  328. ((pkg name value)
  329. (set! package-assq
  330. (assq-set! package-assq pkg
  331. (cons (cons (string->symbol name)
  332. (string->symbol value))
  333. (or (assq-ref package-assq pkg)
  334. '())))))
  335. (_
  336. (raise
  337. (formatted-message
  338. (G_ "invalid package parameter specification: ~s")
  339. spec)))))
  340. specs)
  341. (map (lambda (x) ; (<pkg> <plist>)
  342. (let ((package-name (car x))
  343. (parameter-lst (cdr x)))
  344. (cons package-name
  345. (lambda (x)
  346. (let* [(original-lst (map (lambda (x)
  347. (cons (car x) (cdr x)))
  348. (package-parameter-alist x)))
  349. (final-lst
  350. (fold (lambda (z y)
  351. (assq-set! y
  352. (car z)
  353. (cdr z)))
  354. original-lst
  355. parameter-lst))]
  356. (parameterize-package x final-lst))))))
  357. package-assq)))
  358. (define (transform-package-parameters replacement-specs)
  359. "Return a procedure that, when passed a package, replaces its direct
  360. dependencies according to REPLACEMENT-SPECS. REPLACEMENT-SPECS is a list of
  361. strings like \"guile-next=stable-3.0\" meaning that packages are built using
  362. 'guile-next' from the latest commit on its 'stable-3.0' branch."
  363. ;; we'll apply per-package parameterization and then return
  364. (let* ((replacements (evaluate-parameter-specs replacement-specs))
  365. (rewrite (package-input-rewriting/spec replacements)))
  366. (lambda (obj)
  367. (if (package? obj)
  368. (rewrite obj)
  369. obj))))
  370. ;; spec parsing will be the same as ludo's patch but for applying the patches, we'll need the circuitry of package-with-parameters
  371. ;; idea:
  372. ;; make a function based on package-with-parameters, and write both the transform and package-with-parameters to use it
  373. ;; use resolver on the speclist and only act if applicable values have changed
  374. ;; }
  375. (define (package-dependents/spec top bottom)
  376. "Return the list of dependents of BOTTOM, a spec string, that are also
  377. dependencies of TOP, a package."
  378. (define-values (name version)
  379. (package-name->name+version bottom))
  380. (define dependent?
  381. (mlambda (p)
  382. (and (package? p)
  383. (or (and (string=? name (package-name p))
  384. (or (not version)
  385. (version-prefix? version (package-version p))))
  386. (match (bag-direct-inputs (package->bag p))
  387. (((labels dependencies . _) ...)
  388. (any dependent? dependencies)))))))
  389. (filter dependent? (package-closure (list top))))
  390. (define (package-toolchain-rewriting p bottom toolchain)
  391. "Return a procedure that, when passed a package that's either BOTTOM or one
  392. of its dependents up to P so, changes it so it is built with TOOLCHAIN.
  393. TOOLCHAIN must be an input list."
  394. (define rewriting-property
  395. (gensym " package-toolchain-rewriting"))
  396. (match (package-dependents/spec p bottom)
  397. (() ;P does not depend on BOTTOM
  398. identity)
  399. (set
  400. ;; SET is the list of packages "between" P and BOTTOM (included) whose
  401. ;; toolchain needs to be changed.
  402. (package-mapping (lambda (p)
  403. (if (or (assq rewriting-property
  404. (package-properties p))
  405. (not (memq p set)))
  406. p
  407. (let ((p (package-with-c-toolchain p toolchain)))
  408. (package/inherit p
  409. (properties `((,rewriting-property . #t)
  410. ,@(package-properties p)))))))
  411. (lambda (p)
  412. (or (assq rewriting-property (package-properties p))
  413. (not (memq p set))))
  414. #:deep? #t))))
  415. (define (transform-package-toolchain replacement-specs)
  416. "Return a procedure that, when passed a package, changes its toolchain or
  417. that of its dependencies according to REPLACEMENT-SPECS. REPLACEMENT-SPECS is
  418. a list of strings like \"fftw=gcc-toolchain@10\" meaning that the package to
  419. the left of the equal sign must be built with the toolchain to the right of
  420. the equal sign."
  421. (define split-on-commas
  422. (cute string-tokenize <> (char-set-complement (char-set #\,))))
  423. (define (specification->input spec)
  424. (let ((package (specification->package spec)))
  425. (list (package-name package) package)))
  426. (define replacements
  427. (map (lambda (spec)
  428. (match (string-tokenize spec %not-equal)
  429. ((spec (= split-on-commas toolchain))
  430. (cons spec (map specification->input toolchain)))
  431. (_
  432. (raise
  433. (formatted-message
  434. (G_ "~a: invalid toolchain replacement specification")
  435. spec)))))
  436. replacement-specs))
  437. (lambda (obj)
  438. (if (package? obj)
  439. (or (any (match-lambda
  440. ((bottom . toolchain)
  441. ((package-toolchain-rewriting obj bottom toolchain) obj)))
  442. replacements)
  443. obj)
  444. obj)))
  445. (define tuning-compiler
  446. (mlambda (micro-architecture)
  447. "Return a compiler wrapper that passes '-march=MICRO-ARCHITECTURE' to the
  448. actual compiler."
  449. (define wrapper
  450. #~(begin
  451. (use-modules (ice-9 match))
  452. (define* (search-next command
  453. #:optional
  454. (path (string-split (getenv "PATH")
  455. #\:)))
  456. ;; Search the next COMMAND on PATH, a list of
  457. ;; directories representing the executable search path.
  458. (define this
  459. (stat (car (command-line))))
  460. (let loop ((path path))
  461. (match path
  462. (()
  463. (match command
  464. ("cc" (search-next "gcc"))
  465. (_ #f)))
  466. ((directory rest ...)
  467. (let* ((file (string-append
  468. directory "/" command))
  469. (st (stat file #f)))
  470. (if (and st (not (equal? this st)))
  471. file
  472. (loop rest)))))))
  473. (match (command-line)
  474. ((command arguments ...)
  475. (match (search-next (basename command))
  476. (#f (exit 127))
  477. (next
  478. (apply execl next
  479. (append (cons next arguments)
  480. (list (string-append "-march="
  481. #$micro-architecture))))))))))
  482. (define program
  483. (program-file (string-append "tuning-compiler-wrapper-" micro-architecture)
  484. wrapper))
  485. (computed-file (string-append "tuning-compiler-" micro-architecture)
  486. (with-imported-modules '((guix build utils))
  487. #~(begin
  488. (use-modules (guix build utils))
  489. (define bin (string-append #$output "/bin"))
  490. (mkdir-p bin)
  491. (for-each (lambda (program)
  492. (symlink #$program
  493. (string-append bin "/" program)))
  494. '("cc" "gcc" "clang" "g++" "c++" "clang++")))))))
  495. (define (build-system-with-tuning-compiler bs micro-architecture)
  496. "Return a variant of BS, a build system, that ensures that the compiler that
  497. BS uses (usually an implicit input) can generate code for MICRO-ARCHITECTURE,
  498. which names a specific CPU of the target architecture--e.g., when targeting
  499. 86_64 MICRO-ARCHITECTURE might be \"skylake\". If it does, return a build
  500. system that builds code for MICRO-ARCHITECTURE; otherwise raise an error."
  501. (define %not-hyphen
  502. (char-set-complement (char-set #\-)))
  503. (define lower
  504. (build-system-lower bs))
  505. (define (lower* . args)
  506. ;; The list of CPU names supported by the '-march' option of C/C++
  507. ;; compilers is specific to each compiler and version thereof. Rather
  508. ;; than pass '-march=MICRO-ARCHITECTURE' as is to the compiler, possibly
  509. ;; leading to an obscure build error, check whether the compiler is known
  510. ;; to support MICRO-ARCHITECTURE. If not, bail out.
  511. (let* ((lowered (apply lower args))
  512. (architecture (match (string-tokenize (bag-system lowered)
  513. %not-hyphen)
  514. ((arch _ ...) arch)))
  515. (compiler (any (match-lambda
  516. ((label (? package? p) . _)
  517. (and (assoc-ref (package-properties p)
  518. 'compiler-cpu-architectures)
  519. p))
  520. (_ #f))
  521. (bag-build-inputs lowered))))
  522. (unless compiler
  523. (raise (formatted-message
  524. (G_ "failed to determine which compiler is used"))))
  525. (let ((lst (assoc-ref (package-properties compiler)
  526. 'compiler-cpu-architectures)))
  527. (unless lst
  528. (raise (formatted-message
  529. (G_ "failed to determine whether ~a supports ~a")
  530. (package-full-name compiler)
  531. micro-architecture)))
  532. (unless (member micro-architecture
  533. (or (assoc-ref lst architecture) '()))
  534. (raise
  535. (make-compound-condition
  536. (formatted-message
  537. (G_ "compiler ~a does not support micro-architecture ~a")
  538. (package-full-name compiler)
  539. micro-architecture)
  540. (condition
  541. (&fix-hint
  542. (hint (match (assoc-ref lst architecture)
  543. (#f (format #f (G_ "Compiler ~a does not support
  544. micro-architectures of ~a.")
  545. (package-full-name compiler "@@")
  546. architecture))
  547. (lst
  548. (format #f (G_ "Compiler ~a supports the following ~a
  549. micro-architectures:
  550. @quotation
  551. ~a
  552. @end quotation")
  553. (package-full-name compiler "@@")
  554. architecture
  555. (string-join lst ", ")))))))))))
  556. (bag
  557. (inherit lowered)
  558. (build-inputs
  559. ;; Arrange so that the compiler wrapper comes first in $PATH.
  560. `(("tuning-compiler" ,(tuning-compiler micro-architecture))
  561. ,@(bag-build-inputs lowered))))))
  562. (build-system
  563. (inherit bs)
  564. (lower lower*)))
  565. (define (tuned-package p micro-architecture)
  566. "Return package P tuned for MICRO-ARCHITECTURE."
  567. (package
  568. (inherit p)
  569. (build-system
  570. (build-system-with-tuning-compiler (package-build-system p)
  571. micro-architecture))
  572. (arguments
  573. ;; The machine building this package may or may not be able to run code
  574. ;; for MICRO-ARCHITECTURE. Because of that, skip tests; they are run for
  575. ;; the "baseline" variant anyway.
  576. (substitute-keyword-arguments (package-arguments p)
  577. ((#:tests? _ #f) #f)))
  578. (properties
  579. `((cpu-tuning . ,micro-architecture)
  580. ;; Remove the 'tunable?' property so that 'package-tuning' does not
  581. ;; call 'tuned-package' again on this one.
  582. ,@(alist-delete 'tunable? (package-properties p))))))
  583. (define (tunable-package? package)
  584. "Return true if package PACKAGE is \"tunable\"--i.e., if tuning it for the
  585. host CPU is worthwhile."
  586. (assq 'tunable? (package-properties package)))
  587. (define package-tuning
  588. (mlambda (micro-architecture)
  589. "Return a procedure that maps the given package to its counterpart tuned
  590. for MICRO-ARCHITECTURE, a string suitable for GCC's '-march'."
  591. (define rewriting-property
  592. (gensym " package-tuning"))
  593. (package-mapping (lambda (p)
  594. (cond ((assq rewriting-property (package-properties p))
  595. p)
  596. ((assq 'tunable? (package-properties p))
  597. (info (G_ "tuning ~a for CPU ~a~%")
  598. (package-full-name p) micro-architecture)
  599. (package/inherit p
  600. (replacement (tuned-package p micro-architecture))
  601. (properties `((,rewriting-property . #t)
  602. ,@(package-properties p)))))
  603. (else
  604. p)))
  605. (lambda (p)
  606. (assq rewriting-property (package-properties p)))
  607. #:deep? #t)))
  608. (define (transform-package-tuning micro-architectures)
  609. "Return a procedure that, when "
  610. (match micro-architectures
  611. ((micro-architecture _ ...)
  612. (let ((rewrite (package-tuning micro-architecture)))
  613. (lambda (obj)
  614. (if (package? obj)
  615. (rewrite obj)
  616. obj))))))
  617. (define (transform-package-with-debug-info specs)
  618. "Return a procedure that, when passed a package, set its 'replacement' field
  619. to the same package but with #:strip-binaries? #f in its 'arguments' field."
  620. (define (non-stripped p)
  621. (package
  622. (inherit p)
  623. (arguments
  624. (substitute-keyword-arguments (package-arguments p)
  625. ((#:strip-binaries? _ #f) #f)))))
  626. (define (package-with-debug-info p)
  627. (if (member "debug" (package-outputs p))
  628. p
  629. (let loop ((p p))
  630. (match (package-replacement p)
  631. (#f
  632. (package
  633. (inherit p)
  634. (replacement (non-stripped p))))
  635. (next
  636. (package
  637. (inherit p)
  638. (replacement (loop next))))))))
  639. (define rewrite
  640. (package-input-rewriting/spec (map (lambda (spec)
  641. (cons spec package-with-debug-info))
  642. specs)))
  643. (lambda (obj)
  644. (if (package? obj)
  645. (rewrite obj)
  646. obj)))
  647. (define (transform-package-tests specs)
  648. "Return a procedure that, when passed a package, sets #:tests? #f in its
  649. 'arguments' field."
  650. (define (package-without-tests p)
  651. (package/inherit p
  652. (arguments
  653. (substitute-keyword-arguments (package-arguments p)
  654. ((#:tests? _ #f) #f)))))
  655. (define rewrite
  656. (package-input-rewriting/spec (map (lambda (spec)
  657. (cons spec package-without-tests))
  658. specs)))
  659. (lambda (obj)
  660. (if (package? obj)
  661. (rewrite obj)
  662. obj)))
  663. (define (transform-package-configure-flag specs)
  664. "Return a procedure that, when passed a package and a flag, adds the flag to
  665. #:configure-flags in the package's 'arguments' field."
  666. (define (package-with-configure-flag p extra-flag)
  667. (package/inherit p
  668. (arguments
  669. (substitute-keyword-arguments (package-arguments p)
  670. ((#:configure-flags flags #~'())
  671. ;; Add EXTRA-FLAG to the end so it can potentially override FLAGS.
  672. #~(append #$flags '(#$extra-flag)))))))
  673. (define configure-flags
  674. ;; Spec/flag alist.
  675. (map (lambda (spec)
  676. ;; Split SPEC on the first equal sign (the configure flag might
  677. ;; contain equal signs, as in '-DINTSIZE=32').
  678. (let ((equal (string-index spec #\=)))
  679. (match (and equal
  680. (list (string-take spec equal)
  681. (string-drop spec (+ 1 equal))))
  682. ((spec flag)
  683. (cons spec flag))
  684. (_
  685. (raise (formatted-message
  686. (G_ "~a: invalid package configure flag specification")
  687. spec))))))
  688. specs))
  689. (define rewrite
  690. (package-input-rewriting/spec
  691. (map (match-lambda
  692. ((spec . flags)
  693. (cons spec (cut package-with-configure-flag <> flags))))
  694. configure-flags)))
  695. (lambda (obj)
  696. (if (package? obj)
  697. (rewrite obj)
  698. obj)))
  699. (define (patched-source name source patches)
  700. "Return a file-like object with the given NAME that applies PATCHES to
  701. SOURCE. SOURCE must itself be a file-like object of any type, including
  702. <git-checkout>, <local-file>, etc."
  703. (define patch
  704. (module-ref (resolve-interface '(gnu packages base)) 'patch))
  705. (computed-file name
  706. (with-imported-modules '((guix build utils))
  707. #~(begin
  708. (use-modules (guix build utils))
  709. (setenv "PATH" #+(file-append patch "/bin"))
  710. ;; XXX: Assume SOURCE is a directory. This is true in
  711. ;; most practical cases, where it's a <git-checkout>.
  712. (copy-recursively #+source #$output)
  713. (chdir #$output)
  714. (for-each (lambda (patch)
  715. (invoke "patch" "-p1" "--batch"
  716. "-i" patch))
  717. '(#+@patches))))))
  718. (define (transform-package-patches specs)
  719. "Return a procedure that, when passed a package, returns a package with
  720. additional patches."
  721. (define (package-with-extra-patches p patches)
  722. (let ((patches (map (lambda (file)
  723. (local-file file))
  724. patches)))
  725. (if (origin? (package-source p))
  726. (package/inherit p
  727. (source (origin
  728. (inherit (package-source p))
  729. (patches (append patches
  730. (origin-patches (package-source p)))))))
  731. (package/inherit p
  732. (source (patched-source (string-append (package-full-name p "-")
  733. "-source")
  734. (package-source p) patches))))))
  735. (define (coalesce-alist alist)
  736. ;; Coalesce multiple occurrences of the same key in ALIST.
  737. (let loop ((alist alist)
  738. (keys '())
  739. (mapping vlist-null))
  740. (match alist
  741. (()
  742. (map (lambda (key)
  743. (cons key (vhash-fold* cons '() key mapping)))
  744. (delete-duplicates (reverse keys))))
  745. (((key . value) . rest)
  746. (loop rest
  747. (cons key keys)
  748. (vhash-cons key value mapping))))))
  749. (define patches
  750. ;; Spec/patch alist.
  751. (coalesce-alist
  752. (map (lambda (spec)
  753. (match (string-tokenize spec %not-equal)
  754. ((spec patch)
  755. (cons spec (canonicalize-path patch)))
  756. (_
  757. (raise (formatted-message
  758. (G_ "~a: invalid package patch specification")
  759. spec)))))
  760. specs)))
  761. (define rewrite
  762. (package-input-rewriting/spec
  763. (map (match-lambda
  764. ((spec . patches)
  765. (cons spec (cut package-with-extra-patches <> patches))))
  766. patches)))
  767. (lambda (obj)
  768. (if (package? obj)
  769. (rewrite obj)
  770. obj)))
  771. (define* (package-with-upstream-version p #:optional version)
  772. "Return package P changed to use the given upstream VERSION or, if VERSION
  773. is #f, the latest known upstream version."
  774. (let ((source (package-latest-release p #:version version)))
  775. (cond ((not source)
  776. (if version
  777. (warning
  778. (G_ "could not find version ~a of '~a' upstream~%")
  779. version (package-name p))
  780. (warning
  781. (G_ "could not determine latest upstream release of '~a'~%")
  782. (package-name p)))
  783. p)
  784. ((string=? (upstream-source-version source)
  785. (package-version p))
  786. (unless version
  787. (info (G_ "~a is already the latest version of '~a'~%")
  788. (package-version p) (package-name p)))
  789. p)
  790. (else
  791. (when (version>? (package-version p)
  792. (upstream-source-version source))
  793. (warning (G_ "using ~a ~a, which is older than the packaged \
  794. version (~a)~%")
  795. (package-name p)
  796. (upstream-source-version source)
  797. (package-version p)))
  798. (unless (pair? (upstream-source-signature-urls source))
  799. (warning (G_ "cannot authenticate source of '~a', version ~a~%")
  800. (package-name p)
  801. (upstream-source-version source)))
  802. ;; TODO: Take 'upstream-source-input-changes' into account.
  803. (package
  804. (inherit p)
  805. (version (upstream-source-version source))
  806. (source source))))))
  807. (define (transform-package-latest specs)
  808. "Return a procedure that rewrites package graphs such that those in SPECS
  809. are replaced by their latest upstream version."
  810. (define rewrite
  811. (package-input-rewriting/spec
  812. (map (lambda (spec)
  813. (cons spec package-with-upstream-version))
  814. specs)))
  815. (lambda (obj)
  816. (if (package? obj)
  817. (rewrite obj)
  818. obj)))
  819. (define (transform-package-version specs)
  820. "Return a procedure that rewrites package graphs such that those in SPECS
  821. are replaced by the specified upstream version."
  822. (define rewrite
  823. (package-input-rewriting/spec
  824. (map (lambda (spec)
  825. (match (string-tokenize spec %not-equal)
  826. ((spec version)
  827. (cons spec (cut package-with-upstream-version <> version)))
  828. (_
  829. (raise (formatted-message
  830. (G_ "~a: invalid upstream version specification")
  831. spec)))))
  832. specs)))
  833. (lambda (obj)
  834. (if (package? obj)
  835. (rewrite obj)
  836. obj)))
  837. (define %transformations
  838. ;; Transformations that can be applied to things to build. The car is the
  839. ;; key used in the option alist, and the cdr is the transformation
  840. ;; procedure; it is called with two arguments: the store, and a list of
  841. ;; things to build.
  842. `((with-source . ,transform-package-source)
  843. (with-input . ,transform-package-inputs)
  844. (with-graft . ,transform-package-inputs/graft)
  845. (with-branch . ,transform-package-source-branch)
  846. (with-commit . ,transform-package-source-commit)
  847. (with-git-url . ,transform-package-source-git-url)
  848. (with-parameter . ,transform-package-parameters) ; g23
  849. (with-c-toolchain . ,transform-package-toolchain)
  850. (tune . ,transform-package-tuning)
  851. (with-debug-info . ,transform-package-with-debug-info)
  852. (without-tests . ,transform-package-tests)
  853. (with-configure-flag . ,transform-package-configure-flag)
  854. (with-patch . ,transform-package-patches)
  855. (with-latest . ,transform-package-latest)
  856. (with-version . ,transform-package-version)))
  857. (define (transformation-procedure key)
  858. "Return the transformation procedure associated with KEY, a symbol such as
  859. 'with-source', or #f if there is none."
  860. (any (match-lambda
  861. ((k . proc)
  862. (and (eq? k key) proc)))
  863. %transformations))
  864. (define (transformation-option-key? key)
  865. "Return true if KEY is an option key (as returned while parsing options with
  866. %TRANSFORMATION-OPTIONS) corresponding to a package transformation option.
  867. For example, (transformation-option-key? 'with-input) => #t."
  868. (->bool (transformation-procedure key)))
  869. ;;;
  870. ;;; Command-line handling.
  871. ;;;
  872. (define %transformation-options
  873. ;; The command-line interface to the above transformations.
  874. (let ((parser (lambda (symbol)
  875. (lambda (opt name arg result . rest)
  876. (apply values
  877. (alist-cons symbol arg result)
  878. rest)))))
  879. (list (option '("with-source") #t #f
  880. (parser 'with-source))
  881. (option '("with-input") #t #f
  882. (parser 'with-input))
  883. (option '("with-graft") #t #f
  884. (parser 'with-graft))
  885. (option '("with-branch") #t #f
  886. (parser 'with-branch))
  887. (option '("with-commit") #t #f
  888. (parser 'with-commit))
  889. (option '("with-git-url") #t #f
  890. (parser 'with-git-url))
  891. (option '("with-parameter") #t #f ; g23
  892. (parser 'with-parameter)) ; g23
  893. (option '("with-c-toolchain") #t #f
  894. (parser 'with-c-toolchain))
  895. (option '("tune") #f #t
  896. (lambda (opt name arg result . rest)
  897. (define micro-architecture
  898. (match arg
  899. ((or #f "native")
  900. (unless (string=? (or (assoc-ref result 'system)
  901. (%current-system))
  902. %system)
  903. (leave (G_ "\
  904. building for ~a instead of ~a, so tuning cannot be guessed~%")
  905. (assoc-ref result 'system) %system))
  906. (cpu->gcc-architecture (current-cpu)))
  907. ("generic" #f)
  908. (_ arg)))
  909. (apply values
  910. (if micro-architecture
  911. (alist-cons 'tune micro-architecture
  912. result)
  913. (alist-delete 'tune result))
  914. rest)))
  915. (option '("with-debug-info") #t #f
  916. (parser 'with-debug-info))
  917. (option '("without-tests") #t #f
  918. (parser 'without-tests))
  919. (option '("with-configure-flag") #t #f
  920. (parser 'with-configure-flag))
  921. (option '("with-patch") #t #f
  922. (parser 'with-patch))
  923. (option '("with-latest") #t #f
  924. (parser 'with-latest))
  925. (option '("with-version") #t #f
  926. (parser 'with-version))
  927. (option '("help-transform") #f #f
  928. (lambda _
  929. (format #t
  930. (G_ "Available package transformation options:~%"))
  931. (show-transformation-options-help/detailed)
  932. (newline)
  933. (exit 0))))))
  934. (define (show-transformation-options-help/detailed)
  935. (display (G_ "
  936. --with-source=[PACKAGE=]SOURCE
  937. use SOURCE when building the corresponding package"))
  938. (display (G_ "
  939. --with-input=PACKAGE=REPLACEMENT
  940. replace dependency PACKAGE by REPLACEMENT"))
  941. (display (G_ "
  942. --with-graft=PACKAGE=REPLACEMENT
  943. graft REPLACEMENT on packages that refer to PACKAGE"))
  944. (display (G_ "
  945. --with-branch=PACKAGE=BRANCH
  946. build PACKAGE from the latest commit of BRANCH"))
  947. (display (G_ "
  948. --with-commit=PACKAGE=COMMIT
  949. build PACKAGE from COMMIT"))
  950. (display (G_ "
  951. --with-git-url=PACKAGE=URL
  952. build PACKAGE from the repository at URL"))
  953. (display (G_ "
  954. --with-patch=PACKAGE=FILE
  955. add FILE to the list of patches of PACKAGE"))
  956. (display (G_ "
  957. --tune[=CPU] tune relevant packages for CPU--e.g., \"skylake\""))
  958. (display (G_ "
  959. --with-configure-flag=PACKAGE=FLAG
  960. append FLAG to the configure flags of PACKAGE"))
  961. (display (G_ "
  962. --with-latest=PACKAGE
  963. use the latest upstream release of PACKAGE"))
  964. (display (G_ "
  965. --with-version=PACKAGE=VERSION
  966. use the given upstream VERSION of PACKAGE"))
  967. (display (G_ "
  968. --with-c-toolchain=PACKAGE=TOOLCHAIN
  969. build PACKAGE and its dependents with TOOLCHAIN"))
  970. (display (G_ "
  971. --with-debug-info=PACKAGE
  972. build PACKAGE and preserve its debug info"))
  973. (display (G_ "
  974. --without-tests=PACKAGE
  975. build PACKAGE without running its tests")))
  976. (define (show-transformation-options-help)
  977. "Show basic help for package transformation options."
  978. (display (G_ "
  979. --help-transform list package transformation options not shown here")))
  980. (define (options->transformation opts)
  981. "Return a procedure that, when passed an object to build (package,
  982. derivation, etc.), applies the transformations specified by OPTS and returns
  983. the resulting objects. OPTS must be a list of symbol/string pairs such as:
  984. ((with-branch . \"guile-gcrypt=master\")
  985. (without-tests . \"libgcrypt\"))
  986. Each symbol names a transformation and the corresponding string is an argument
  987. to that transformation."
  988. (define applicable
  989. ;; List of applicable transformations as symbol/procedure pairs in the
  990. ;; order in which they appear on the command line.
  991. (filter-map (match-lambda
  992. ((key . value)
  993. (match (transformation-procedure key)
  994. (#f
  995. #f)
  996. (transform
  997. ;; XXX: We used to pass TRANSFORM a list of several
  998. ;; arguments, but we now pass only one, assuming that
  999. ;; transform composes well.
  1000. (list key value (transform (list value)))))))
  1001. (reverse opts)))
  1002. (define (package-with-transformation-properties p)
  1003. (package/inherit p
  1004. (properties `((transformations
  1005. . ,(map (match-lambda
  1006. ((key value _)
  1007. (cons key value)))
  1008. (reverse applicable))) ;preserve order
  1009. ,@(package-properties p)))))
  1010. (lambda (obj)
  1011. (define (tagged-object new)
  1012. (if (and (not (eq? obj new))
  1013. (package? new) (not (null? applicable)))
  1014. (package-with-transformation-properties new)
  1015. new))
  1016. (tagged-object
  1017. (fold (match-lambda*
  1018. (((name value transform) obj)
  1019. (let ((new (transform obj)))
  1020. (when (eq? new obj)
  1021. (warning (G_ "transformation '~a' had no effect on ~a~%")
  1022. name
  1023. (if (package? obj)
  1024. (package-full-name obj)
  1025. obj)))
  1026. new)))
  1027. obj
  1028. applicable))))
  1029. (define (package-transformations package)
  1030. "Return the transformations applied to PACKAGE according to its properties."
  1031. (match (assq-ref (package-properties package) 'transformations)
  1032. (#f '())
  1033. (transformations transformations)))
  1034. (define (manifest-entry-with-transformations entry)
  1035. "Return ENTRY with an additional 'transformations' property if it's not
  1036. already there."
  1037. (let ((properties (manifest-entry-properties entry)))
  1038. (if (assq 'transformations properties)
  1039. entry
  1040. (let ((item (manifest-entry-item entry)))
  1041. (manifest-entry
  1042. (inherit entry)
  1043. (properties
  1044. (match (and (package? item)
  1045. (package-transformations item))
  1046. ((or #f '())
  1047. properties)
  1048. (transformations
  1049. `((transformations . ,transformations)
  1050. ,@properties)))))))))