transformations.scm 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016, 2017, 2019, 2020, 2021 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 (test-transformations)
  20. #:use-module (guix tests)
  21. #:use-module (guix store)
  22. #:use-module ((guix gexp) #:select (lower-object))
  23. #:use-module ((guix profiles)
  24. #:select (package->manifest-entry
  25. manifest-entry-properties))
  26. #:use-module (guix derivations)
  27. #:use-module (guix packages)
  28. #:use-module (guix git-download)
  29. #:use-module (guix build-system)
  30. #:use-module (guix build-system gnu)
  31. #:use-module (guix transformations)
  32. #:use-module ((guix gexp)
  33. #:select (local-file? local-file-file
  34. computed-file? computed-file-gexp
  35. gexp-input-thing))
  36. #:use-module (guix ui)
  37. #:use-module (guix utils)
  38. #:use-module (guix git)
  39. #:use-module (guix upstream)
  40. #:use-module (guix diagnostics)
  41. #:use-module (gnu packages)
  42. #:use-module (gnu packages base)
  43. #:use-module (gnu packages busybox)
  44. #:use-module (ice-9 match)
  45. #:use-module (srfi srfi-1)
  46. #:use-module (srfi srfi-26)
  47. #:use-module (srfi srfi-34)
  48. #:use-module (srfi srfi-64))
  49. (test-begin "transformations")
  50. (test-assert "options->transformation, no transformations"
  51. (let ((p (dummy-package "foo"))
  52. (t (options->transformation '())))
  53. (eq? (t p) p)))
  54. (test-assert "options->transformation, with-source"
  55. ;; Our pseudo-package is called 'guix.scm' so the 'guix.scm' source should
  56. ;; be applicable.
  57. (let* ((p (dummy-package "guix.scm"))
  58. (s (search-path %load-path "guix.scm"))
  59. (t (options->transformation `((with-source . ,s)))))
  60. (with-store store
  61. (let* ((new (t p))
  62. (source (run-with-store store
  63. (lower-object (package-source new)))))
  64. (and (not (eq? new p))
  65. (string=? source
  66. (add-to-store store "guix.scm" #t
  67. "sha256" s)))))))
  68. (test-assert "options->transformation, with-source, replacement"
  69. ;; Same, but this time the original package has a 'replacement' field. We
  70. ;; expect that replacement to be set to #f in the new package.
  71. (let* ((p (dummy-package "guix.scm" (replacement coreutils)))
  72. (s (search-path %load-path "guix.scm"))
  73. (t (options->transformation `((with-source . ,s)))))
  74. (let ((new (t p)))
  75. (and (not (eq? new p))
  76. (not (package-replacement new))))))
  77. (test-assert "options->transformation, with-source, with version"
  78. ;; Our pseudo-package is called 'guix.scm' so the 'guix.scm-2.0' source
  79. ;; should be applicable, and its version should be extracted.
  80. (let ((p (dummy-package "foo"))
  81. (s (search-path %load-path "guix.scm")))
  82. (call-with-temporary-directory
  83. (lambda (directory)
  84. (let* ((f (string-append directory "/foo-42.0.tar.gz"))
  85. (t (options->transformation `((with-source . ,f)))))
  86. (copy-file s f)
  87. (with-store store
  88. (let* ((new (t p))
  89. (source (run-with-store store
  90. (lower-object (package-source new)))))
  91. (and (not (eq? new p))
  92. (string=? (package-name new) (package-name p))
  93. (string=? (package-version new) "42.0")
  94. (string=? source
  95. (add-to-store store (basename f) #t
  96. "sha256" f))))))))))
  97. (test-assert "options->transformation, with-source, no matches"
  98. ;; When a transformation in not applicable, a warning must be raised.
  99. (let* ((p (dummy-package "foobar"))
  100. (s (search-path %load-path "guix.scm"))
  101. (t (options->transformation `((with-source . ,s)))))
  102. (let* ((port (open-output-string))
  103. (new (parameterize ((guix-warning-port port))
  104. (t p))))
  105. (and (eq? new p)
  106. (string-contains (get-output-string port)
  107. "had no effect")))))
  108. (test-assert "options->transformation, with-source, PKG=URI"
  109. (let* ((p (dummy-package "foo"))
  110. (s (search-path %load-path "guix.scm"))
  111. (f (string-append "foo=" s))
  112. (t (options->transformation `((with-source . ,f)))))
  113. (with-store store
  114. (let* ((new (t p))
  115. (source (run-with-store store
  116. (lower-object (package-source new)))))
  117. (and (not (eq? new p))
  118. (string=? (package-name new) (package-name p))
  119. (string=? (package-version new)
  120. (package-version p))
  121. (string=? source
  122. (add-to-store store (basename s) #t
  123. "sha256" s)))))))
  124. (test-assert "options->transformation, with-source, PKG@VER=URI"
  125. (let* ((p (dummy-package "foo"))
  126. (s (search-path %load-path "guix.scm"))
  127. (f (string-append "foo@42.0=" s))
  128. (t (options->transformation `((with-source . ,f)))))
  129. (with-store store
  130. (let* ((new (t p))
  131. (source (run-with-store store
  132. (lower-object (package-source new)))))
  133. (and (not (eq? new p))
  134. (string=? (package-name new) (package-name p))
  135. (string=? (package-version new) "42.0")
  136. (string=? source
  137. (add-to-store store (basename s) #t
  138. "sha256" s)))))))
  139. (test-assert "options->transformation, with-input"
  140. (let* ((p (dummy-package "guix.scm"
  141. (inputs `(("foo" ,(specification->package "coreutils"))
  142. ("bar" ,(specification->package "grep"))
  143. ("baz" ,(dummy-package "chbouib"
  144. (native-inputs `(("x" ,grep)))))))))
  145. (t (options->transformation '((with-input . "coreutils=busybox")
  146. (with-input . "grep=findutils")))))
  147. (let ((new (t p)))
  148. (and (not (eq? new p))
  149. (match (package-inputs new)
  150. ((("foo" dep1) ("bar" dep2) ("baz" dep3))
  151. (and (string=? (package-full-name dep1)
  152. (package-full-name busybox))
  153. (string=? (package-full-name dep2)
  154. (package-full-name findutils))
  155. (string=? (package-name dep3) "chbouib")
  156. (match (package-native-inputs dep3)
  157. ((("x" dep))
  158. (string=? (package-full-name dep)
  159. (package-full-name findutils)))))))))))
  160. (test-assert "options->transformation, with-graft"
  161. (let* ((p (dummy-package "guix.scm"
  162. (inputs `(("foo" ,grep)
  163. ("bar" ,(dummy-package "chbouib"
  164. (native-inputs `(("x" ,grep)))))))))
  165. (t (options->transformation '((with-graft . "grep=findutils")))))
  166. (let ((new (t p)))
  167. (and (not (eq? new p))
  168. (match (package-inputs new)
  169. ((("foo" dep1) ("bar" dep2))
  170. (and (string=? (package-full-name dep1)
  171. (package-full-name grep))
  172. (string=? (package-full-name (package-replacement dep1))
  173. (package-full-name findutils))
  174. (string=? (package-name dep2) "chbouib")
  175. (match (package-native-inputs dep2)
  176. ((("x" dep))
  177. (with-store store
  178. (string=? (derivation-file-name
  179. (package-derivation store findutils))
  180. (derivation-file-name
  181. (package-derivation store dep)))))))))))))
  182. (test-equal "options->transformation, with-branch"
  183. (git-checkout (url "https://example.org")
  184. (branch "devel")
  185. (recursive? #t))
  186. (let* ((p (dummy-package "guix.scm"
  187. (inputs `(("foo" ,grep)
  188. ("bar" ,(dummy-package "chbouib"
  189. (source (origin
  190. (method git-fetch)
  191. (uri (git-reference
  192. (url "https://example.org")
  193. (commit "cabba9e")))
  194. (sha256 #f)))))))))
  195. (t (options->transformation '((with-branch . "chbouib=devel")))))
  196. (let ((new (t p)))
  197. (and (not (eq? new p))
  198. (match (package-inputs new)
  199. ((("foo" dep1) ("bar" dep2))
  200. (and (string=? (package-full-name dep1)
  201. (package-full-name grep))
  202. (string=? (package-name dep2) "chbouib")
  203. (package-source dep2))))))))
  204. (test-equal "options->transformation, with-commit"
  205. (git-checkout (url "https://example.org")
  206. (commit "abcdef")
  207. (recursive? #t))
  208. (let* ((p (dummy-package "guix.scm"
  209. (inputs `(("foo" ,grep)
  210. ("bar" ,(dummy-package "chbouib"
  211. (source (origin
  212. (method git-fetch)
  213. (uri (git-reference
  214. (url "https://example.org")
  215. (commit "cabba9e")))
  216. (sha256 #f)))))))))
  217. (t (options->transformation '((with-commit . "chbouib=abcdef")))))
  218. (let ((new (t p)))
  219. (and (not (eq? new p))
  220. (match (package-inputs new)
  221. ((("foo" dep1) ("bar" dep2))
  222. (and (string=? (package-full-name dep1)
  223. (package-full-name grep))
  224. (string=? (package-name dep2) "chbouib")
  225. (package-source dep2))))))))
  226. (test-equal "options->transformation, with-commit, version transformation"
  227. '("1.0" "1.0-rc1-2-gabc123" "git.abc123")
  228. (map (lambda (commit)
  229. (let* ((p (dummy-package "guix.scm"
  230. (inputs `(("foo" ,(dummy-package "chbouib"
  231. (source (origin
  232. (method git-fetch)
  233. (uri (git-reference
  234. (url "https://example.org")
  235. (commit "cabba9e")))
  236. (sha256 #f)))))))))
  237. (t (options->transformation
  238. `((with-commit . ,(string-append "chbouib=" commit))))))
  239. (let ((new (t p)))
  240. (and (not (eq? new p))
  241. (match (package-inputs new)
  242. ((("foo" dep1))
  243. (package-version dep1)))))))
  244. '("v1.0" "1.0-rc1-2-gabc123" "abc123")))
  245. (test-equal "options->transformation, with-git-url"
  246. (let ((source (git-checkout (url "https://example.org")
  247. (recursive? #t))))
  248. (list source source))
  249. (let* ((p (dummy-package "guix.scm"
  250. (inputs `(("foo" ,grep)
  251. ("bar" ,(dummy-package "chbouib"
  252. (native-inputs `(("x" ,grep)))))))))
  253. (t (options->transformation '((with-git-url . "grep=https://example.org")))))
  254. (let ((new (t p)))
  255. (and (not (eq? new p))
  256. (match (package-inputs new)
  257. ((("foo" dep1) ("bar" dep2))
  258. (and (string=? (package-full-name dep1)
  259. (package-full-name grep))
  260. (string=? (package-name dep2) "chbouib")
  261. (match (package-native-inputs dep2)
  262. ((("x" dep3))
  263. (map package-source (list dep1 dep3)))))))))))
  264. (test-equal "options->transformation, with-git-url + with-branch"
  265. ;; Combine the two options and make sure the 'with-branch' transformation
  266. ;; comes after the 'with-git-url' transformation.
  267. (let ((source (git-checkout (url "https://example.org")
  268. (branch "BRANCH")
  269. (recursive? #t))))
  270. (list source source))
  271. (let* ((p (dummy-package "guix.scm"
  272. (inputs `(("foo" ,grep)
  273. ("bar" ,(dummy-package "chbouib"
  274. (native-inputs `(("x" ,grep)))))))))
  275. (t (options->transformation
  276. (reverse '((with-git-url
  277. . "grep=https://example.org")
  278. (with-branch . "grep=BRANCH"))))))
  279. (let ((new (t p)))
  280. (and (not (eq? new p))
  281. (match (package-inputs new)
  282. ((("foo" dep1) ("bar" dep2))
  283. (and (string=? (package-name dep1) "grep")
  284. (string=? (package-name dep2) "chbouib")
  285. (match (package-native-inputs dep2)
  286. ((("x" dep3))
  287. (map package-source (list dep1 dep3)))))))))))
  288. (define* (depends-on-toolchain? p #:optional (toolchain "gcc-toolchain"))
  289. "Return true if P depends on TOOLCHAIN instead of the default tool chain."
  290. (define toolchain-packages
  291. '("gcc" "binutils" "glibc" "ld-wrapper"))
  292. (define (package-name* obj)
  293. (and (package? obj) (package-name obj)))
  294. (match (bag-build-inputs (package->bag p))
  295. (((_ (= package-name* packages) . _) ...)
  296. (and (not (any (cut member <> packages) toolchain-packages))
  297. (member toolchain packages)))))
  298. (test-assert "options->transformation, with-c-toolchain"
  299. (let* ((dep0 (dummy-package "chbouib"
  300. (build-system gnu-build-system)
  301. (native-inputs `(("y" ,grep)))))
  302. (dep1 (dummy-package "stuff"
  303. (native-inputs `(("x" ,dep0)))))
  304. (p (dummy-package "thingie"
  305. (build-system gnu-build-system)
  306. (inputs `(("foo" ,grep)
  307. ("bar" ,dep1)))))
  308. (t (options->transformation
  309. '((with-c-toolchain . "chbouib=gcc-toolchain")))))
  310. ;; Here we check that the transformation applies to DEP0 and all its
  311. ;; dependents: DEP0 must use GCC-TOOLCHAIN, DEP1 must use GCC-TOOLCHAIN
  312. ;; and the DEP0 that uses GCC-TOOLCHAIN, and so on.
  313. (let ((new (t p)))
  314. (and (depends-on-toolchain? new "gcc-toolchain")
  315. (match (bag-build-inputs (package->bag new))
  316. ((("foo" dep0) ("bar" dep1) _ ...)
  317. (and (depends-on-toolchain? dep1 "gcc-toolchain")
  318. (not (depends-on-toolchain? dep0 "gcc-toolchain"))
  319. (string=? (package-full-name dep0)
  320. (package-full-name grep))
  321. (match (bag-build-inputs (package->bag dep1))
  322. ((("x" dep) _ ...)
  323. (and (depends-on-toolchain? dep "gcc-toolchain")
  324. (match (bag-build-inputs (package->bag dep))
  325. ((("y" dep) _ ...) ;this one is unchanged
  326. (eq? dep grep)))))))))))))
  327. (test-equal "options->transformation, with-c-toolchain twice"
  328. (package-full-name grep)
  329. (let* ((dep0 (dummy-package "chbouib"))
  330. (dep1 (dummy-package "stuff"))
  331. (p (dummy-package "thingie"
  332. (build-system gnu-build-system)
  333. (inputs `(("foo" ,dep0)
  334. ("bar" ,dep1)
  335. ("baz" ,grep)))))
  336. (t (options->transformation
  337. '((with-c-toolchain . "chbouib=clang-toolchain")
  338. (with-c-toolchain . "stuff=clang-toolchain")))))
  339. (let ((new (t p)))
  340. (and (depends-on-toolchain? new "clang-toolchain")
  341. (match (bag-build-inputs (package->bag new))
  342. ((("foo" dep0) ("bar" dep1) ("baz" dep2) _ ...)
  343. (and (depends-on-toolchain? dep0 "clang-toolchain")
  344. (depends-on-toolchain? dep1 "clang-toolchain")
  345. (not (depends-on-toolchain? dep2 "clang-toolchain"))
  346. (package-full-name dep2))))))))
  347. (test-assert "options->transformation, with-c-toolchain, no effect"
  348. (let ((p (dummy-package "thingie"))
  349. (t (options->transformation
  350. '((with-c-toolchain . "does-not-exist=gcc-toolchain")))))
  351. ;; When it has no effect, '--with-c-toolchain' returns P.
  352. (eq? (t p) p)))
  353. (test-equal "options->transformation, with-debug-info"
  354. '(#:strip-binaries? #f)
  355. (let* ((dep (dummy-package "chbouib"))
  356. (p (dummy-package "thingie"
  357. (build-system gnu-build-system)
  358. (inputs `(("foo" ,dep)
  359. ("bar" ,grep)))))
  360. (t (options->transformation
  361. '((with-debug-info . "chbouib")))))
  362. (let ((new (t p)))
  363. (match (package-inputs new)
  364. ((("foo" dep0) ("bar" dep1))
  365. (and (string=? (package-full-name dep1)
  366. (package-full-name grep))
  367. (package-arguments (package-replacement dep0))))))))
  368. (test-assert "options->transformation, without-tests"
  369. (let* ((dep (dummy-package "dep"))
  370. (p (dummy-package "foo"
  371. (inputs `(("dep" ,dep)))))
  372. (t (options->transformation '((without-tests . "dep")
  373. (without-tests . "tar")))))
  374. (let ((new (t p)))
  375. (match (bag-direct-inputs (package->bag new))
  376. ((("dep" dep) ("tar" tar) _ ...)
  377. (and (equal? (package-arguments dep) '(#:tests? #f))
  378. (match (memq #:tests? (package-arguments tar))
  379. ((#:tests? #f _ ...) #t))))))))
  380. (test-equal "options->transformation, with-patch"
  381. (search-patches "glibc-locales.patch" "guile-relocatable.patch")
  382. (let* ((dep (dummy-package "dep"
  383. (source (dummy-origin))))
  384. (p (dummy-package "foo"
  385. (inputs `(("dep" ,dep)))))
  386. (patch1 (search-patch "glibc-locales.patch"))
  387. (patch2 (search-patch "guile-relocatable.patch"))
  388. (t (options->transformation
  389. `((with-patch . ,(string-append "dep=" patch1))
  390. (with-patch . ,(string-append "dep=" patch2))
  391. (with-patch . ,(string-append "tar=" patch1))))))
  392. (let ((new (t p)))
  393. (match (bag-direct-inputs (package->bag new))
  394. ((("dep" dep) ("tar" tar) _ ...)
  395. (and (member patch1
  396. (filter-map (lambda (patch)
  397. (and (local-file? patch)
  398. (local-file-file patch)))
  399. (origin-patches (package-source tar))))
  400. (map local-file-file
  401. (origin-patches (package-source dep)))))))))
  402. (test-equal "options->transformation, with-commit + with-patch"
  403. '(#t #t)
  404. (let* ((patch (search-patch "glibc-locales.patch"))
  405. (commit "f8934ec94df5868ee8baf1fb0f8ed0f24e7e91eb")
  406. (t (options->transformation
  407. ;; Note: options are applied in reverse order, so
  408. ;; 'with-patch' comes on top.
  409. `((with-patch . ,(string-append "guile-gcrypt=" patch))
  410. (with-commit
  411. . ,(string-append "guile-gcrypt=" commit))))))
  412. (let ((new (t (@ (gnu packages gnupg) guile-gcrypt))))
  413. (match (package-source new)
  414. ((? computed-file? source)
  415. (let* ((gexp (computed-file-gexp source))
  416. (inputs (map gexp-input-thing
  417. ((@@ (guix gexp) gexp-inputs) gexp))))
  418. (list (any (lambda (input)
  419. (and (git-checkout? input)
  420. (string=? commit (git-checkout-commit input))))
  421. inputs)
  422. (any (lambda (input)
  423. (and (local-file? input)
  424. (string=? (local-file-file input) patch)))
  425. inputs))))))))
  426. (test-equal "options->transformation, with-latest"
  427. "42.0"
  428. (mock ((guix upstream) %updaters
  429. (delay (list (upstream-updater
  430. (name 'dummy)
  431. (pred (const #t))
  432. (description "")
  433. (latest (const (upstream-source
  434. (package "foo")
  435. (version "42.0")
  436. (urls '("http://example.org")))))))))
  437. (let* ((p (dummy-package "foo" (version "1.0")))
  438. (t (options->transformation
  439. `((with-latest . "foo")))))
  440. (package-version (t p)))))
  441. (test-equal "options->transformation, tune"
  442. '(cpu-tuning . "superfast")
  443. (let* ((p0 (dummy-package "p0"))
  444. (p1 (dummy-package "p1"
  445. (inputs `(("p0" ,p0)))
  446. (properties '((tunable? . #t)))))
  447. (p2 (dummy-package "p2"
  448. (inputs `(("p1" ,p1)))))
  449. (t (options->transformation '((tune . "superfast"))))
  450. (p3 (t p2)))
  451. (and (not (package-replacement p3))
  452. (match (package-inputs p3)
  453. ((("p1" tuned))
  454. (match (package-inputs tuned)
  455. ((("p0" p0))
  456. (and (not (package-replacement p0))
  457. (assq 'cpu-tuning
  458. (package-properties
  459. (package-replacement tuned)))))))))))
  460. (test-assert "options->transformations, tune, wrong micro-architecture"
  461. (let ((p (dummy-package "tunable"
  462. (properties '((tunable? . #t)))))
  463. (t (options->transformation '((tune . "nonexistent-superfast")))))
  464. ;; Because GCC used by P's build system does not support
  465. ;; '-march=nonexistent-superfast', we should see an error when lowering
  466. ;; the tuned package.
  467. (guard (c ((formatted-message? c)
  468. (member "nonexistent-superfast"
  469. (formatted-message-arguments c))))
  470. (package->bag (t p))
  471. #f)))
  472. (test-equal "options->transformation + package->manifest-entry"
  473. '((transformations . ((without-tests . "foo"))))
  474. (let* ((p (dummy-package "foo"))
  475. (t (options->transformation '((without-tests . "foo"))))
  476. (e (package->manifest-entry (t p))))
  477. (manifest-entry-properties e)))
  478. (test-end)
  479. ;;; Local Variables:
  480. ;;; eval: (put 'dummy-package 'scheme-indent-function 1)
  481. ;;; End: