build.scm 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2013 Mark H Weaver <mhw@netris.org>
  4. ;;; Copyright © 2020 Marius Bakke <mbakke@fastmail.com>
  5. ;;; Copyright © 2020 Ricardo Wurmus <rekado@elephly.net>
  6. ;;;
  7. ;;; This file is part of GNU Guix.
  8. ;;;
  9. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  10. ;;; under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation; either version 3 of the License, or (at
  12. ;;; your option) any later version.
  13. ;;;
  14. ;;; GNU Guix is distributed in the hope that it will be useful, but
  15. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;; GNU General Public License for more details.
  18. ;;;
  19. ;;; You should have received a copy of the GNU General Public License
  20. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  21. (define-module (guix scripts build)
  22. #:use-module (guix ui)
  23. #:use-module (guix scripts)
  24. #:use-module (guix import json)
  25. #:use-module (guix store)
  26. #:use-module (guix derivations)
  27. #:use-module (guix packages)
  28. #:use-module (guix grafts)
  29. #:use-module (guix utils)
  30. ;; Use the procedure that destructures "NAME-VERSION" forms.
  31. #:use-module ((guix build utils)
  32. #:select ((package-name->name+version
  33. . hyphen-package-name->name+version)))
  34. #:use-module (guix monads)
  35. #:use-module (guix gexp)
  36. #:use-module (guix profiles)
  37. #:use-module (guix diagnostics)
  38. #:autoload (guix http-client) (http-fetch http-get-error?)
  39. #:use-module (ice-9 format)
  40. #:use-module (ice-9 match)
  41. #:use-module (ice-9 vlist)
  42. #:use-module (srfi srfi-1)
  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 (gnu packages)
  49. #:autoload (guix download) (download-to-store)
  50. #:autoload (guix git-download) (git-reference? git-reference-url)
  51. #:autoload (guix git) (git-checkout git-checkout? git-checkout-url)
  52. #:use-module ((guix status) #:select (with-status-verbosity))
  53. #:use-module ((guix progress) #:select (current-terminal-columns))
  54. #:use-module ((guix build syscalls) #:select (terminal-columns))
  55. #:export (%standard-build-options
  56. set-build-options-from-command-line
  57. set-build-options-from-command-line*
  58. show-build-options-help
  59. %transformation-options
  60. options->transformation
  61. manifest-entry-with-transformations
  62. show-transformation-options-help
  63. guix-build
  64. register-root
  65. register-root*))
  66. (define %default-log-urls
  67. ;; Default base URLs for build logs.
  68. '("http://ci.guix.gnu.org/log"))
  69. ;; XXX: The following procedure cannot be in (guix store) because of the
  70. ;; dependency on (guix derivations).
  71. (define* (log-url store file #:key (base-urls %default-log-urls))
  72. "Return a URL under one of the BASE-URLS where a build log for FILE can be
  73. found. Return #f if no build log was found."
  74. (define (valid-url? url)
  75. ;; Probe URL and return #t if it is accessible.
  76. (catch #t
  77. (lambda ()
  78. (guard (c ((http-get-error? c) #f))
  79. (close-port (http-fetch url #:buffered? #f))
  80. #t))
  81. (match-lambda*
  82. (('getaddrinfo-error . _)
  83. #f)
  84. (('tls-certificate-error args ...)
  85. (report-error (G_ "cannot access build log at '~a':~%") url)
  86. (print-exception (current-error-port) #f
  87. 'tls-certificate-error args)
  88. (exit 1))
  89. ((key . args)
  90. (apply throw key args)))))
  91. (define (find-url file)
  92. (let ((base (basename file)))
  93. (any (lambda (base-url)
  94. (let ((url (string-append base-url "/" base)))
  95. (and (valid-url? url) url)))
  96. base-urls)))
  97. (cond ((derivation-path? file)
  98. (catch 'system-error
  99. (lambda ()
  100. ;; Usually we'll have more luck with the output file name since
  101. ;; the deriver that was used by the server could be different, so
  102. ;; try one of the output file names.
  103. (let ((drv (read-derivation-from-file file)))
  104. (or (find-url (derivation->output-path drv))
  105. (find-url file))))
  106. (lambda args
  107. ;; As a last resort, try the .drv.
  108. (if (= ENOENT (system-error-errno args))
  109. (find-url file)
  110. (apply throw args)))))
  111. (else
  112. (find-url file))))
  113. (define (register-root store paths root)
  114. "Register ROOT as an indirect GC root for all of PATHS."
  115. (let* ((root (if (string-prefix? "/" root)
  116. root
  117. (string-append (canonicalize-path (dirname root))
  118. "/" (basename root)))))
  119. (catch 'system-error
  120. (lambda ()
  121. (match paths
  122. ((path)
  123. (symlink path root)
  124. (add-indirect-root store root))
  125. ((paths ...)
  126. (fold (lambda (path count)
  127. (let ((root (string-append root
  128. "-"
  129. (number->string count))))
  130. (symlink path root)
  131. (add-indirect-root store root))
  132. (+ 1 count))
  133. 0
  134. paths))))
  135. (lambda args
  136. (leave (G_ "failed to create GC root `~a': ~a~%")
  137. root (strerror (system-error-errno args)))))))
  138. (define register-root*
  139. (store-lift register-root))
  140. (define (numeric-extension? file-name)
  141. "Return true if FILE-NAME ends with digits."
  142. (string-every char-set:hex-digit (file-extension file-name)))
  143. (define (tarball-base-name file-name)
  144. "Return the \"base\" of FILE-NAME, removing '.tar.gz' or similar
  145. extensions."
  146. ;; TODO: Factorize.
  147. (cond ((not (file-extension file-name))
  148. file-name)
  149. ((numeric-extension? file-name)
  150. file-name)
  151. ((string=? (file-extension file-name) "tar")
  152. (file-sans-extension file-name))
  153. ((file-extension file-name)
  154. =>
  155. (match-lambda
  156. ("scm" file-name)
  157. (else (tarball-base-name (file-sans-extension file-name)))))
  158. (else
  159. file-name)))
  160. (define* (package-with-source store p uri #:optional version)
  161. "Return a package based on P but with its source taken from URI. Extract
  162. the new package's version number from URI."
  163. (let ((base (tarball-base-name (basename uri))))
  164. (let-values (((_ version*)
  165. (hyphen-package-name->name+version base)))
  166. (package (inherit p)
  167. (version (or version version*
  168. (package-version p)))
  169. ;; Use #:recursive? #t to allow for directories.
  170. (source (download-to-store store uri
  171. #:recursive? #t))
  172. ;; Override the replacement, otherwise '--with-source' would
  173. ;; have no effect.
  174. (replacement #f)))))
  175. ;;;
  176. ;;; Transformations.
  177. ;;;
  178. (define (transform-package-source sources)
  179. "Return a transformation procedure that replaces package sources with the
  180. matching URIs given in SOURCES."
  181. (define new-sources
  182. (map (lambda (uri)
  183. (match (string-index uri #\=)
  184. (#f
  185. ;; Determine the package name and version from URI.
  186. (call-with-values
  187. (lambda ()
  188. (hyphen-package-name->name+version
  189. (tarball-base-name (basename uri))))
  190. (lambda (name version)
  191. (list name version uri))))
  192. (index
  193. ;; What's before INDEX is a "PKG@VER" or "PKG" spec.
  194. (call-with-values
  195. (lambda ()
  196. (package-name->name+version (string-take uri index)))
  197. (lambda (name version)
  198. (list name version
  199. (string-drop uri (+ 1 index))))))))
  200. sources))
  201. (lambda (store obj)
  202. (let loop ((sources new-sources)
  203. (result '()))
  204. (match obj
  205. ((? package? p)
  206. (match (assoc-ref sources (package-name p))
  207. ((version source)
  208. (package-with-source store p source version))
  209. (#f
  210. p)))
  211. (_
  212. obj)))))
  213. (define (evaluate-replacement-specs specs proc)
  214. "Parse SPECS, a list of strings like \"guile=guile@2.1\" and return a list
  215. of package spec/procedure pairs as expected by 'package-input-rewriting/spec'.
  216. PROC is called with the package to be replaced and its replacement according
  217. to SPECS. Raise an error if an element of SPECS uses invalid syntax, or if a
  218. package it refers to could not be found."
  219. (define not-equal
  220. (char-set-complement (char-set #\=)))
  221. (map (lambda (spec)
  222. (match (string-tokenize spec not-equal)
  223. ((spec new)
  224. (cons spec
  225. (let ((new (specification->package new)))
  226. (lambda (old)
  227. (proc old new)))))
  228. (x
  229. (leave (G_ "invalid replacement specification: ~s~%") spec))))
  230. specs))
  231. (define (transform-package-inputs replacement-specs)
  232. "Return a procedure that, when passed a package, replaces its direct
  233. dependencies according to REPLACEMENT-SPECS. REPLACEMENT-SPECS is a list of
  234. strings like \"guile=guile@2.1\" meaning that, any dependency on a package
  235. called \"guile\" must be replaced with a dependency on a version 2.1 of
  236. \"guile\"."
  237. (let* ((replacements (evaluate-replacement-specs replacement-specs
  238. (lambda (old new)
  239. new)))
  240. (rewrite (package-input-rewriting/spec replacements)))
  241. (lambda (store obj)
  242. (if (package? obj)
  243. (rewrite obj)
  244. obj))))
  245. (define (transform-package-inputs/graft replacement-specs)
  246. "Return a procedure that, when passed a package, replaces its direct
  247. dependencies according to REPLACEMENT-SPECS. REPLACEMENT-SPECS is a list of
  248. strings like \"gnutls=gnutls@3.5.4\" meaning that packages are built using the
  249. current 'gnutls' package, after which version 3.5.4 is grafted onto them."
  250. (define (set-replacement old new)
  251. (package (inherit old) (replacement new)))
  252. (let* ((replacements (evaluate-replacement-specs replacement-specs
  253. set-replacement))
  254. (rewrite (package-input-rewriting/spec replacements)))
  255. (lambda (store obj)
  256. (if (package? obj)
  257. (rewrite obj)
  258. obj))))
  259. (define %not-equal
  260. (char-set-complement (char-set #\=)))
  261. (define (package-git-url package)
  262. "Return the URL of the Git repository for package, or raise an error if
  263. the source of PACKAGE is not fetched from a Git repository."
  264. (let ((source (package-source package)))
  265. (cond ((and (origin? source)
  266. (git-reference? (origin-uri source)))
  267. (git-reference-url (origin-uri source)))
  268. ((git-checkout? source)
  269. (git-checkout-url source))
  270. (else
  271. (leave (G_ "the source of ~a is not a Git reference~%")
  272. (package-full-name package))))))
  273. (define (evaluate-git-replacement-specs specs proc)
  274. "Parse SPECS, a list of strings like \"guile=stable-2.2\", and return a list
  275. of package pairs, where (PROC PACKAGE URL BRANCH-OR-COMMIT) returns the
  276. replacement package. Raise an error if an element of SPECS uses invalid
  277. syntax, or if a package it refers to could not be found."
  278. (map (lambda (spec)
  279. (match (string-tokenize spec %not-equal)
  280. ((spec branch-or-commit)
  281. (define (replace old)
  282. (let* ((source (package-source old))
  283. (url (package-git-url old)))
  284. (proc old url branch-or-commit)))
  285. (cons spec replace))
  286. (x
  287. (leave (G_ "invalid replacement specification: ~s~%") spec))))
  288. specs))
  289. (define (transform-package-source-branch replacement-specs)
  290. "Return a procedure that, when passed a package, replaces its direct
  291. dependencies according to REPLACEMENT-SPECS. REPLACEMENT-SPECS is a list of
  292. strings like \"guile-next=stable-3.0\" meaning that packages are built using
  293. 'guile-next' from the latest commit on its 'stable-3.0' branch."
  294. (define (replace old url branch)
  295. (package
  296. (inherit old)
  297. (version (string-append "git." (string-map (match-lambda
  298. (#\/ #\-)
  299. (chr chr))
  300. branch)))
  301. (source (git-checkout (url url) (branch branch)
  302. (recursive? #t)))))
  303. (let* ((replacements (evaluate-git-replacement-specs replacement-specs
  304. replace))
  305. (rewrite (package-input-rewriting/spec replacements)))
  306. (lambda (store obj)
  307. (if (package? obj)
  308. (rewrite obj)
  309. obj))))
  310. (define (transform-package-source-commit replacement-specs)
  311. "Return a procedure that, when passed a package, replaces its direct
  312. dependencies according to REPLACEMENT-SPECS. REPLACEMENT-SPECS is a list of
  313. strings like \"guile-next=cabba9e\" meaning that packages are built using
  314. 'guile-next' from commit 'cabba9e'."
  315. (define (replace old url commit)
  316. (package
  317. (inherit old)
  318. (version (if (and (> (string-length commit) 1)
  319. (string-prefix? "v" commit)
  320. (char-set-contains? char-set:digit
  321. (string-ref commit 1)))
  322. (string-drop commit 1) ;looks like a tag like "v1.0"
  323. (string-append "git."
  324. (if (< (string-length commit) 7)
  325. commit
  326. (string-take commit 7)))))
  327. (source (git-checkout (url url) (commit commit)
  328. (recursive? #t)))))
  329. (let* ((replacements (evaluate-git-replacement-specs replacement-specs
  330. replace))
  331. (rewrite (package-input-rewriting/spec replacements)))
  332. (lambda (store obj)
  333. (if (package? obj)
  334. (rewrite obj)
  335. obj))))
  336. (define (transform-package-source-git-url replacement-specs)
  337. "Return a procedure that, when passed a package, replaces its dependencies
  338. according to REPLACEMENT-SPECS. REPLACEMENT-SPECS is a list of strings like
  339. \"guile-json=https://gitthing.com/…\" meaning that packages are built using
  340. a checkout of the Git repository at the given URL."
  341. (define replacements
  342. (map (lambda (spec)
  343. (match (string-tokenize spec %not-equal)
  344. ((spec url)
  345. (cons spec
  346. (lambda (old)
  347. (package
  348. (inherit old)
  349. (source (git-checkout (url url)
  350. (recursive? #t)))))))
  351. (_
  352. (leave (G_ "~a: invalid Git URL replacement specification~%")
  353. spec))))
  354. replacement-specs))
  355. (define rewrite
  356. (package-input-rewriting/spec replacements))
  357. (lambda (store obj)
  358. (if (package? obj)
  359. (rewrite obj)
  360. obj)))
  361. (define (transform-package-tests specs)
  362. "Return a procedure that, when passed a package, sets #:tests? #f in its
  363. 'arguments' field."
  364. (define (package-without-tests p)
  365. (package/inherit p
  366. (arguments
  367. (substitute-keyword-arguments (package-arguments p)
  368. ((#:tests? _ #f) #f)))))
  369. (define rewrite
  370. (package-input-rewriting/spec (map (lambda (spec)
  371. (cons spec package-without-tests))
  372. specs)))
  373. (lambda (store obj)
  374. (if (package? obj)
  375. (rewrite obj)
  376. obj)))
  377. (define %transformations
  378. ;; Transformations that can be applied to things to build. The car is the
  379. ;; key used in the option alist, and the cdr is the transformation
  380. ;; procedure; it is called with two arguments: the store, and a list of
  381. ;; things to build.
  382. `((with-source . ,transform-package-source)
  383. (with-input . ,transform-package-inputs)
  384. (with-graft . ,transform-package-inputs/graft)
  385. (with-branch . ,transform-package-source-branch)
  386. (with-commit . ,transform-package-source-commit)
  387. (with-git-url . ,transform-package-source-git-url)
  388. (without-tests . ,transform-package-tests)))
  389. (define (transformation-procedure key)
  390. "Return the transformation procedure associated with KEY, a symbol such as
  391. 'with-source', or #f if there is none."
  392. (any (match-lambda
  393. ((k . proc)
  394. (and (eq? k key) proc)))
  395. %transformations))
  396. (define %transformation-options
  397. ;; The command-line interface to the above transformations.
  398. (let ((parser (lambda (symbol)
  399. (lambda (opt name arg result . rest)
  400. (apply values
  401. (alist-cons symbol arg result)
  402. rest)))))
  403. (list (option '("with-source") #t #f
  404. (parser 'with-source))
  405. (option '("with-input") #t #f
  406. (parser 'with-input))
  407. (option '("with-graft") #t #f
  408. (parser 'with-graft))
  409. (option '("with-branch") #t #f
  410. (parser 'with-branch))
  411. (option '("with-commit") #t #f
  412. (parser 'with-commit))
  413. (option '("with-git-url") #t #f
  414. (parser 'with-git-url))
  415. (option '("without-tests") #t #f
  416. (parser 'without-tests)))))
  417. (define (show-transformation-options-help)
  418. (display (G_ "
  419. --with-source=[PACKAGE=]SOURCE
  420. use SOURCE when building the corresponding package"))
  421. (display (G_ "
  422. --with-input=PACKAGE=REPLACEMENT
  423. replace dependency PACKAGE by REPLACEMENT"))
  424. (display (G_ "
  425. --with-graft=PACKAGE=REPLACEMENT
  426. graft REPLACEMENT on packages that refer to PACKAGE"))
  427. (display (G_ "
  428. --with-branch=PACKAGE=BRANCH
  429. build PACKAGE from the latest commit of BRANCH"))
  430. (display (G_ "
  431. --with-commit=PACKAGE=COMMIT
  432. build PACKAGE from COMMIT"))
  433. (display (G_ "
  434. --with-git-url=PACKAGE=URL
  435. build PACKAGE from the repository at URL"))
  436. (display (G_ "
  437. --without-tests=PACKAGE
  438. build PACKAGE without running its tests")))
  439. (define (options->transformation opts)
  440. "Return a procedure that, when passed an object to build (package,
  441. derivation, etc.), applies the transformations specified by OPTS."
  442. (define applicable
  443. ;; List of applicable transformations as symbol/procedure pairs in the
  444. ;; order in which they appear on the command line.
  445. (filter-map (match-lambda
  446. ((key . value)
  447. (match (transformation-procedure key)
  448. (#f
  449. #f)
  450. (transform
  451. ;; XXX: We used to pass TRANSFORM a list of several
  452. ;; arguments, but we now pass only one, assuming that
  453. ;; transform composes well.
  454. (list key value (transform (list value)))))))
  455. (reverse opts)))
  456. (define (package-with-transformation-properties p)
  457. (package/inherit p
  458. (properties `((transformations
  459. . ,(map (match-lambda
  460. ((key value _)
  461. (cons key value)))
  462. applicable))
  463. ,@(package-properties p)))))
  464. (lambda (store obj)
  465. (define (tagged-object new)
  466. (if (and (not (eq? obj new))
  467. (package? new) (not (null? applicable)))
  468. (package-with-transformation-properties new)
  469. new))
  470. (tagged-object
  471. (fold (match-lambda*
  472. (((name value transform) obj)
  473. (let ((new (transform store obj)))
  474. (when (eq? new obj)
  475. (warning (G_ "transformation '~a' had no effect on ~a~%")
  476. name
  477. (if (package? obj)
  478. (package-full-name obj)
  479. obj)))
  480. new)))
  481. obj
  482. applicable))))
  483. (define (package-transformations package)
  484. "Return the transformations applied to PACKAGE according to its properties."
  485. (match (assq-ref (package-properties package) 'transformations)
  486. (#f '())
  487. (transformations transformations)))
  488. (define (manifest-entry-with-transformations entry)
  489. "Return ENTRY with an additional 'transformations' property if it's not
  490. already there."
  491. (let ((properties (manifest-entry-properties entry)))
  492. (if (assq 'transformations properties)
  493. entry
  494. (let ((item (manifest-entry-item entry)))
  495. (manifest-entry
  496. (inherit entry)
  497. (properties
  498. (match (and (package? item)
  499. (package-transformations item))
  500. ((or #f '())
  501. properties)
  502. (transformations
  503. `((transformations . ,transformations)
  504. ,@properties)))))))))
  505. ;;;
  506. ;;; Standard command-line build options.
  507. ;;;
  508. (define (show-build-options-help)
  509. "Display on the current output port help about the standard command-line
  510. options handled by 'set-build-options-from-command-line', and listed in
  511. '%standard-build-options'."
  512. (display (G_ "
  513. -L, --load-path=DIR prepend DIR to the package module search path"))
  514. (display (G_ "
  515. -K, --keep-failed keep build tree of failed builds"))
  516. (display (G_ "
  517. -k, --keep-going keep going when some of the derivations fail"))
  518. (display (G_ "
  519. -n, --dry-run do not build the derivations"))
  520. (display (G_ "
  521. --fallback fall back to building when the substituter fails"))
  522. (display (G_ "
  523. --no-substitutes build instead of resorting to pre-built substitutes"))
  524. (display (G_ "
  525. --substitute-urls=URLS
  526. fetch substitute from URLS if they are authorized"))
  527. (display (G_ "
  528. --no-grafts do not graft packages"))
  529. (display (G_ "
  530. --no-offload do not attempt to offload builds"))
  531. (display (G_ "
  532. --max-silent-time=SECONDS
  533. mark the build as failed after SECONDS of silence"))
  534. (display (G_ "
  535. --timeout=SECONDS mark the build as failed after SECONDS of activity"))
  536. (display (G_ "
  537. --rounds=N build N times in a row to detect non-determinism"))
  538. (display (G_ "
  539. -c, --cores=N allow the use of up to N CPU cores for the build"))
  540. (display (G_ "
  541. -M, --max-jobs=N allow at most N build jobs"))
  542. (display (G_ "
  543. --debug=LEVEL produce debugging output at LEVEL")))
  544. (define (set-build-options-from-command-line store opts)
  545. "Given OPTS, an alist as returned by 'args-fold' given
  546. '%standard-build-options', set the corresponding build options on STORE."
  547. ;; '--keep-failed' has no effect when talking to a remote daemon. Catch the
  548. ;; case where GUIX_DAEMON_SOCKET=guix://….
  549. (when (and (assoc-ref opts 'keep-failed?)
  550. (let* ((socket (store-connection-socket store))
  551. (peer (catch 'system-error
  552. (lambda ()
  553. (and (file-port? socket)
  554. (getpeername socket)))
  555. (const #f))))
  556. (and peer (not (= AF_UNIX (sockaddr:fam peer))))))
  557. (warning (G_ "'--keep-failed' ignored since you are \
  558. talking to a remote daemon\n")))
  559. (set-build-options store
  560. #:keep-failed? (assoc-ref opts 'keep-failed?)
  561. #:keep-going? (assoc-ref opts 'keep-going?)
  562. #:rounds (assoc-ref opts 'rounds)
  563. #:build-cores (assoc-ref opts 'cores)
  564. #:max-build-jobs (assoc-ref opts 'max-jobs)
  565. #:fallback? (assoc-ref opts 'fallback?)
  566. #:use-substitutes? (assoc-ref opts 'substitutes?)
  567. #:substitute-urls (assoc-ref opts 'substitute-urls)
  568. #:offload? (and (assoc-ref opts 'offload?)
  569. (not (assoc-ref opts 'keep-failed?)))
  570. #:max-silent-time (assoc-ref opts 'max-silent-time)
  571. #:timeout (assoc-ref opts 'timeout)
  572. #:print-build-trace (assoc-ref opts 'print-build-trace?)
  573. #:print-extended-build-trace?
  574. (assoc-ref opts 'print-extended-build-trace?)
  575. #:multiplexed-build-output?
  576. (assoc-ref opts 'multiplexed-build-output?)
  577. #:verbosity (assoc-ref opts 'debug)))
  578. (define set-build-options-from-command-line*
  579. (store-lift set-build-options-from-command-line))
  580. (define %standard-build-options
  581. ;; List of standard command-line options for tools that build something.
  582. (list (option '(#\L "load-path") #t #f
  583. (lambda (opt name arg result . rest)
  584. ;; XXX: Imperatively modify the search paths.
  585. (%package-module-path (cons arg (%package-module-path)))
  586. (%patch-path (cons arg (%patch-path)))
  587. (set! %load-path (cons arg %load-path))
  588. (set! %load-compiled-path (cons arg %load-compiled-path))
  589. (apply values (cons result rest))))
  590. (option '(#\K "keep-failed") #f #f
  591. (lambda (opt name arg result . rest)
  592. (apply values
  593. (alist-cons 'keep-failed? #t result)
  594. rest)))
  595. (option '(#\k "keep-going") #f #f
  596. (lambda (opt name arg result . rest)
  597. (apply values
  598. (alist-cons 'keep-going? #t result)
  599. rest)))
  600. (option '("rounds") #t #f
  601. (lambda (opt name arg result . rest)
  602. (apply values
  603. (alist-cons 'rounds (string->number* arg)
  604. result)
  605. rest)))
  606. (option '("fallback") #f #f
  607. (lambda (opt name arg result . rest)
  608. (apply values
  609. (alist-cons 'fallback? #t
  610. (alist-delete 'fallback? result))
  611. rest)))
  612. (option '("no-substitutes") #f #f
  613. (lambda (opt name arg result . rest)
  614. (apply values
  615. (alist-cons 'substitutes? #f
  616. (alist-delete 'substitutes? result))
  617. rest)))
  618. (option '("substitute-urls") #t #f
  619. (lambda (opt name arg result . rest)
  620. (apply values
  621. (alist-cons 'substitute-urls
  622. (string-tokenize arg)
  623. (alist-delete 'substitute-urls result))
  624. rest)))
  625. (option '("no-grafts") #f #f
  626. (lambda (opt name arg result . rest)
  627. (apply values
  628. (alist-cons 'graft? #f
  629. (alist-delete 'graft? result eq?))
  630. rest)))
  631. (option '("no-offload" "no-build-hook") #f #f
  632. (lambda (opt name arg result . rest)
  633. (when (string=? name "no-build-hook")
  634. (warning (G_ "'--no-build-hook' is deprecated; \
  635. use '--no-offload' instead~%")))
  636. (apply values
  637. (alist-cons 'offload? #f
  638. (alist-delete 'offload? result))
  639. rest)))
  640. (option '("max-silent-time") #t #f
  641. (lambda (opt name arg result . rest)
  642. (apply values
  643. (alist-cons 'max-silent-time (string->number* arg)
  644. result)
  645. rest)))
  646. (option '("timeout") #t #f
  647. (lambda (opt name arg result . rest)
  648. (apply values
  649. (alist-cons 'timeout (string->number* arg) result)
  650. rest)))
  651. (option '("debug") #t #f
  652. (lambda (opt name arg result . rest)
  653. (let ((level (string->number* arg)))
  654. (apply values
  655. (alist-cons 'debug level
  656. (alist-delete 'debug result))
  657. rest))))
  658. (option '(#\c "cores") #t #f
  659. (lambda (opt name arg result . rest)
  660. (let ((c (false-if-exception (string->number arg))))
  661. (if c
  662. (apply values (alist-cons 'cores c result) rest)
  663. (leave (G_ "not a number: '~a' option argument: ~a~%")
  664. name arg)))))
  665. (option '(#\M "max-jobs") #t #f
  666. (lambda (opt name arg result . rest)
  667. (let ((c (false-if-exception (string->number arg))))
  668. (if c
  669. (apply values (alist-cons 'max-jobs c result) rest)
  670. (leave (G_ "not a number: '~a' option argument: ~a~%")
  671. name arg)))))))
  672. ;;;
  673. ;;; Command-line options.
  674. ;;;
  675. (define %default-options
  676. ;; Alist of default option values.
  677. `((build-mode . ,(build-mode normal))
  678. (graft? . #t)
  679. (substitutes? . #t)
  680. (offload? . #t)
  681. (print-build-trace? . #t)
  682. (print-extended-build-trace? . #t)
  683. (multiplexed-build-output? . #t)
  684. (verbosity . 2)
  685. (debug . 0)))
  686. (define (show-help)
  687. (display (G_ "Usage: guix build [OPTION]... PACKAGE-OR-DERIVATION...
  688. Build the given PACKAGE-OR-DERIVATION and return their output paths.\n"))
  689. (display (G_ "
  690. -e, --expression=EXPR build the package or derivation EXPR evaluates to"))
  691. (display (G_ "
  692. -f, --file=FILE build the package or derivation that the code within
  693. FILE evaluates to"))
  694. (display (G_ "
  695. -m, --manifest=FILE build the packages that the manifest given in FILE
  696. evaluates to"))
  697. (display (G_ "
  698. -S, --source build the packages' source derivations"))
  699. (display (G_ "
  700. --sources[=TYPE] build source derivations; TYPE may optionally be one
  701. of \"package\", \"all\" (default), or \"transitive\""))
  702. (display (G_ "
  703. -s, --system=SYSTEM attempt to build for SYSTEM--e.g., \"i686-linux\""))
  704. (display (G_ "
  705. --target=TRIPLET cross-build for TRIPLET--e.g., \"armel-linux-gnu\""))
  706. (display (G_ "
  707. -d, --derivations return the derivation paths of the given packages"))
  708. (display (G_ "
  709. --check rebuild items to check for non-determinism issues"))
  710. (display (G_ "
  711. --repair repair the specified items"))
  712. (display (G_ "
  713. -r, --root=FILE make FILE a symlink to the result, and register it
  714. as a garbage collector root"))
  715. (display (G_ "
  716. -v, --verbosity=LEVEL use the given verbosity LEVEL"))
  717. (display (G_ "
  718. -q, --quiet do not show the build log"))
  719. (display (G_ "
  720. --log-file return the log file names for the given derivations"))
  721. (newline)
  722. (show-build-options-help)
  723. (newline)
  724. (show-transformation-options-help)
  725. (newline)
  726. (display (G_ "
  727. -h, --help display this help and exit"))
  728. (display (G_ "
  729. -V, --version display version information and exit"))
  730. (newline)
  731. (show-bug-report-information))
  732. (define %options
  733. ;; Specifications of the command-line options.
  734. (cons* (option '(#\h "help") #f #f
  735. (lambda args
  736. (show-help)
  737. (exit 0)))
  738. (option '(#\V "version") #f #f
  739. (lambda args
  740. (show-version-and-exit "guix build")))
  741. (option '(#\S "source") #f #f
  742. (lambda (opt name arg result)
  743. (alist-cons 'source #t result)))
  744. (option '("sources") #f #t
  745. (lambda (opt name arg result)
  746. (match arg
  747. ("package"
  748. (alist-cons 'source #t result))
  749. ((or "all" #f)
  750. (alist-cons 'source package-direct-sources result))
  751. ("transitive"
  752. (alist-cons 'source package-transitive-sources result))
  753. (else
  754. (leave (G_ "invalid argument: '~a' option argument: ~a, ~
  755. must be one of 'package', 'all', or 'transitive'~%")
  756. name arg)))))
  757. (option '("check") #f #f
  758. (lambda (opt name arg result . rest)
  759. (apply values
  760. (alist-cons 'build-mode (build-mode check)
  761. result)
  762. rest)))
  763. (option '("repair") #f #f
  764. (lambda (opt name arg result . rest)
  765. (apply values
  766. (alist-cons 'build-mode (build-mode repair)
  767. result)
  768. rest)))
  769. (option '(#\s "system") #t #f
  770. (lambda (opt name arg result)
  771. (alist-cons 'system arg result)))
  772. (option '("target") #t #f
  773. (lambda (opt name arg result)
  774. (alist-cons 'target arg
  775. (alist-delete 'target result eq?))))
  776. (option '(#\d "derivations") #f #f
  777. (lambda (opt name arg result)
  778. (alist-cons 'derivations-only? #t result)))
  779. (option '(#\e "expression") #t #f
  780. (lambda (opt name arg result)
  781. (alist-cons 'expression arg result)))
  782. (option '(#\f "file") #t #f
  783. (lambda (opt name arg result)
  784. (alist-cons 'file arg result)))
  785. (option '(#\m "manifest") #t #f
  786. (lambda (opt name arg result)
  787. (alist-cons 'manifest arg result)))
  788. (option '(#\n "dry-run") #f #f
  789. (lambda (opt name arg result)
  790. (alist-cons 'dry-run? #t result)))
  791. (option '(#\r "root") #t #f
  792. (lambda (opt name arg result)
  793. (alist-cons 'gc-root arg result)))
  794. (option '(#\v "verbosity") #t #f
  795. (lambda (opt name arg result)
  796. (let ((level (string->number* arg)))
  797. (alist-cons 'verbosity level
  798. (alist-delete 'verbosity result)))))
  799. (option '(#\q "quiet") #f #f
  800. (lambda (opt name arg result)
  801. (alist-cons 'verbosity 0
  802. (alist-delete 'verbosity result))))
  803. (option '("log-file") #f #f
  804. (lambda (opt name arg result)
  805. (alist-cons 'log-file? #t result)))
  806. (append %transformation-options
  807. %standard-build-options)))
  808. (define (options->things-to-build opts)
  809. "Read the arguments from OPTS and return a list of high-level objects to
  810. build---packages, gexps, derivations, and so on."
  811. (define (validate-type x)
  812. (unless (or (derivation? x) (file-like? x) (gexp? x) (procedure? x))
  813. (raise (make-compound-condition
  814. (formatted-message (G_ "~s: not something we can build~%") x)
  815. (condition
  816. (&fix-hint
  817. (hint
  818. (if (unspecified? x)
  819. (G_ "If you build from a file, make sure the last Scheme
  820. expression returns a package value. @code{define-public} defines a variable,
  821. but returns @code{#<unspecified>}. To fix this, add a Scheme expression at
  822. the end of the file that consists only of the package's variable name you
  823. defined, as in this example:
  824. @example
  825. (define-public my-package
  826. (package
  827. ...))
  828. my-package
  829. @end example")
  830. (G_ "If you build from a file, make sure the last
  831. Scheme expression returns a package, gexp, derivation or a list of such
  832. values.")))))))))
  833. (define (ensure-list x)
  834. (let ((lst (match x
  835. ((x ...) x)
  836. (x (list x)))))
  837. (for-each validate-type lst)
  838. lst))
  839. (append-map (match-lambda
  840. (('argument . (? string? spec))
  841. (cond ((derivation-path? spec)
  842. (catch 'system-error
  843. (lambda ()
  844. ;; Ask for absolute file names so that .drv file
  845. ;; names passed from the user to 'read-derivation'
  846. ;; are absolute when it returns.
  847. (let ((spec (canonicalize-path spec)))
  848. (list (read-derivation-from-file spec))))
  849. (lambda args
  850. ;; Non-existent .drv files can be substituted down
  851. ;; the road, so don't error out.
  852. (if (= ENOENT (system-error-errno args))
  853. '()
  854. (apply throw args)))))
  855. ((store-path? spec)
  856. ;; Nothing to do; maybe for --log-file.
  857. '())
  858. (else
  859. (list (specification->package spec)))))
  860. (('file . file)
  861. (let ((file (or (and (string-suffix? ".json" file)
  862. (json->scheme-file file))
  863. file)))
  864. (ensure-list (load* file (make-user-module '())))))
  865. (('manifest . manifest)
  866. (map manifest-entry-item
  867. (manifest-entries
  868. (load* manifest
  869. (make-user-module '((guix profiles) (gnu)))))))
  870. (('expression . str)
  871. (ensure-list (read/eval str)))
  872. (('argument . (? derivation? drv))
  873. drv)
  874. (_ '()))
  875. opts))
  876. (define (options->derivations store opts)
  877. "Given OPTS, the result of 'args-fold', return a list of derivations to
  878. build."
  879. (define transform
  880. (options->transformation opts))
  881. (define package->derivation
  882. (match (assoc-ref opts 'target)
  883. (#f package-derivation)
  884. (triplet
  885. (cut package-cross-derivation <> <> triplet <>))))
  886. (define src (assoc-ref opts 'source))
  887. (define graft? (assoc-ref opts 'graft?))
  888. (define systems
  889. (match (filter-map (match-lambda
  890. (('system . system) system)
  891. (_ #f))
  892. opts)
  893. (() (list (%current-system)))
  894. (systems systems)))
  895. (define things-to-build
  896. (map (cut transform store <>)
  897. (options->things-to-build opts)))
  898. (define (compute-derivation obj system)
  899. ;; Compute the derivation of OBJ for SYSTEM.
  900. (match obj
  901. ((? package? p)
  902. (let ((p (or (and graft? (package-replacement p)) p)))
  903. (match src
  904. (#f
  905. (list (package->derivation store p system)))
  906. (#t
  907. (match (package-source p)
  908. (#f
  909. (warning (package-location p)
  910. (G_ "package '~a' has no source~%")
  911. (package-name p))
  912. '())
  913. (s
  914. (list (package-source-derivation store s)))))
  915. (proc
  916. (map (cut package-source-derivation store <>)
  917. (proc p))))))
  918. ((? derivation? drv)
  919. (list drv))
  920. ((? procedure? proc)
  921. (list (run-with-store store
  922. (mbegin %store-monad
  923. (set-guile-for-build (default-guile))
  924. (proc))
  925. #:system system)))
  926. ((? file-like? obj)
  927. (list (run-with-store store
  928. (lower-object obj system
  929. #:target (assoc-ref opts 'target))
  930. #:system system)))
  931. ((? gexp? gexp)
  932. (list (run-with-store store
  933. (mbegin %store-monad
  934. (set-guile-for-build (default-guile))
  935. (gexp->derivation "gexp" gexp
  936. #:system system))
  937. #:system system)))))
  938. ;; We may get 'unbound-variable' errors while evaluating the 'inputs' fields
  939. ;; of user packages. Since 'guix build' is the primary tool for people
  940. ;; testing new packages, report such errors gracefully.
  941. (with-unbound-variable-handling
  942. (parameterize ((%graft? graft?))
  943. (append-map (lambda (system)
  944. (concatenate
  945. (map/accumulate-builds store
  946. (cut compute-derivation <> system)
  947. things-to-build)))
  948. systems))))
  949. (define (show-build-log store file urls)
  950. "Show the build log for FILE, falling back to remote logs from URLS if
  951. needed."
  952. (let ((log (or (log-file store file)
  953. (log-url store file #:base-urls urls))))
  954. (if log
  955. (format #t "~a~%" log)
  956. (leave (G_ "no build log for '~a'~%") file))))
  957. ;;;
  958. ;;; Entry point.
  959. ;;;
  960. (define-command (guix-build . args)
  961. (category packaging)
  962. (synopsis "build packages or derivations without installing them")
  963. (define opts
  964. (parse-command-line args %options
  965. (list %default-options)))
  966. (define graft?
  967. (assoc-ref opts 'graft?))
  968. (with-error-handling
  969. (with-status-verbosity (assoc-ref opts 'verbosity)
  970. (with-store store
  971. ;; Set the build options before we do anything else.
  972. (set-build-options-from-command-line store opts)
  973. (with-build-handler (build-notifier #:use-substitutes?
  974. (assoc-ref opts 'substitutes?)
  975. #:verbosity
  976. (assoc-ref opts 'verbosity)
  977. #:dry-run?
  978. (assoc-ref opts 'dry-run?))
  979. (parameterize ((current-terminal-columns (terminal-columns))
  980. ;; Set grafting upfront in case the user's input
  981. ;; depends on it (e.g., a manifest or code snippet that
  982. ;; calls 'gexp->derivation').
  983. (%graft? graft?))
  984. (let* ((mode (assoc-ref opts 'build-mode))
  985. (drv (options->derivations store opts))
  986. (urls (map (cut string-append <> "/log")
  987. (if (assoc-ref opts 'substitutes?)
  988. (or (assoc-ref opts 'substitute-urls)
  989. ;; XXX: This does not necessarily match the
  990. ;; daemon's substitute URLs.
  991. %default-substitute-urls)
  992. '())))
  993. (items (filter-map (match-lambda
  994. (('argument . (? store-path? file))
  995. ;; If FILE is a .drv that's not in
  996. ;; store, keep it so that it can be
  997. ;; substituted.
  998. (and (or (not (derivation-path? file))
  999. (not (file-exists? file)))
  1000. file))
  1001. (_ #f))
  1002. opts))
  1003. (roots (filter-map (match-lambda
  1004. (('gc-root . root) root)
  1005. (_ #f))
  1006. opts)))
  1007. (cond ((assoc-ref opts 'log-file?)
  1008. ;; Pass 'show-build-log' the output file names, not the
  1009. ;; derivation file names, because there can be several
  1010. ;; derivations leading to the same output.
  1011. (for-each (cut show-build-log store <> urls)
  1012. (delete-duplicates
  1013. (append (map derivation->output-path drv)
  1014. items))))
  1015. ((assoc-ref opts 'derivations-only?)
  1016. (format #t "~{~a~%~}" (map derivation-file-name drv))
  1017. (for-each (cut register-root store <> <>)
  1018. (map (compose list derivation-file-name) drv)
  1019. roots))
  1020. (else
  1021. (and (build-derivations store (append drv items)
  1022. mode)
  1023. (for-each show-derivation-outputs drv)
  1024. (for-each (cut register-root store <> <>)
  1025. (map (lambda (drv)
  1026. (map cdr
  1027. (derivation->output-paths drv)))
  1028. drv)
  1029. roots)))))))))))