rust.scm 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016 David Craven <david@craven.ch>
  3. ;;; Copyright © 2016 Eric Le Bihan <eric.le.bihan.dev@free.fr>
  4. ;;; Copyright © 2016 Nikita <nikita@n0.is>
  5. ;;; Copyright © 2017 Ben Woodcroft <donttrustben@gmail.com>
  6. ;;; Copyright © 2017, 2018 Nikolai Merinov <nikolai.merinov@member.fsf.org>
  7. ;;; Copyright © 2017, 2019, 2020, 2021 Efraim Flashner <efraim@flashner.co.il>
  8. ;;; Copyright © 2018, 2019 Tobias Geerinckx-Rice <me@tobias.gr>
  9. ;;; Copyright © 2018 Danny Milosavljevic <dannym+a@scratchpost.org>
  10. ;;; Copyright © 2019 Ivan Petkov <ivanppetkov@gmail.com>
  11. ;;; Copyright © 2020, 2021 Jakub Kądziołka <kuba@kadziolka.net>
  12. ;;; Copyright © 2020 Pierre Langlois <pierre.langlois@gmx.com>
  13. ;;; Copyright © 2020 Matthew James Kraai <kraai@ftbfs.org>
  14. ;;; Copyright © 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com>
  15. ;;;
  16. ;;; This file is part of GNU Guix.
  17. ;;;
  18. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  19. ;;; under the terms of the GNU General Public License as published by
  20. ;;; the Free Software Foundation; either version 3 of the License, or (at
  21. ;;; your option) any later version.
  22. ;;;
  23. ;;; GNU Guix is distributed in the hope that it will be useful, but
  24. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  25. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. ;;; GNU General Public License for more details.
  27. ;;;
  28. ;;; You should have received a copy of the GNU General Public License
  29. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  30. (define-module (gnu packages rust)
  31. #:use-module (gnu packages base)
  32. #:use-module (gnu packages bison)
  33. #:use-module (gnu packages bootstrap)
  34. #:use-module (gnu packages cmake)
  35. #:use-module (gnu packages compression)
  36. #:use-module (gnu packages curl)
  37. #:use-module (gnu packages elf)
  38. #:use-module (gnu packages flex)
  39. #:use-module (gnu packages gcc)
  40. #:use-module (gnu packages gdb)
  41. #:use-module (gnu packages jemalloc)
  42. #:use-module (gnu packages linux)
  43. #:use-module (gnu packages llvm)
  44. #:use-module (gnu packages pkg-config)
  45. #:use-module (gnu packages python)
  46. #:use-module (gnu packages ssh)
  47. #:use-module (gnu packages tls)
  48. #:use-module (gnu packages)
  49. #:use-module (guix build-system cargo)
  50. #:use-module (guix build-system gnu)
  51. #:use-module (guix build-system trivial)
  52. #:use-module (guix download)
  53. #:use-module (guix git-download)
  54. #:use-module ((guix licenses) #:prefix license:)
  55. #:use-module (guix packages)
  56. #:use-module ((guix build utils) #:select (alist-replace))
  57. #:use-module (guix utils)
  58. #:use-module (ice-9 match)
  59. #:use-module (srfi srfi-26))
  60. ;; This is the hash for the empty file, and the reason it's relevant is not
  61. ;; the most obvious.
  62. ;;
  63. ;; The root of the problem is that Cargo keeps track of a file called
  64. ;; Cargo.lock, that contains the hash of the tarball source of each dependency.
  65. ;;
  66. ;; However, tarball sources aren't handled well by Guix because of the need to
  67. ;; patch shebangs in any helper scripts. This is why we use Cargo's vendoring
  68. ;; capabilities, where instead of the tarball, a directory is provided in its
  69. ;; place. (In the case of rustc, the source code already ships with vendored
  70. ;; dependencies, but crates built with cargo-build-system undergo vendoring
  71. ;; during the build.)
  72. ;;
  73. ;; To preserve the advantages of checksumming, vendored dependencies contain
  74. ;; a file called .cargo-checksum.json, which contains the hash of the tarball,
  75. ;; as well as the list of files in it, with the hash of each file.
  76. ;;
  77. ;; The patch-cargo-checksums phase of cargo-build-system runs after
  78. ;; any Guix-specific patches to the vendored dependencies and regenerates the
  79. ;; .cargo-checksum.json files, but it's hard to know the tarball checksum that
  80. ;; should be written to the file - and taking care of any unhandled edge case
  81. ;; would require rebuilding everything that depends on rust. This is why we lie,
  82. ;; and say that the tarball has the hash of an empty file. It's not a problem
  83. ;; because cargo-build-system removes the Cargo.lock file. We can't do that
  84. ;; for rustc because of a quirk of its build system, so we modify the lock file
  85. ;; to substitute the hash.
  86. (define %cargo-reference-hash
  87. "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
  88. (define* (nix-system->gnu-triplet-for-rust
  89. #:optional (system (%current-system)))
  90. (match system
  91. ("x86_64-linux" "x86_64-unknown-linux-gnu")
  92. ("i686-linux" "i686-unknown-linux-gnu")
  93. ("armhf-linux" "armv7-unknown-linux-gnueabihf")
  94. ("aarch64-linux" "aarch64-unknown-linux-gnu")
  95. ("mips64el-linux" "mips64el-unknown-linux-gnuabi64")
  96. (_ (nix-system->gnu-triplet system))))
  97. (define* (rust-uri version #:key (dist "static"))
  98. (string-append "https://" dist ".rust-lang.org/dist/"
  99. "rustc-" version "-src.tar.gz"))
  100. (define* (rust-bootstrapped-package base-rust version checksum)
  101. "Bootstrap rust VERSION with source checksum CHECKSUM using BASE-RUST."
  102. (package
  103. (inherit base-rust)
  104. (version version)
  105. (source
  106. (origin
  107. (inherit (package-source base-rust))
  108. (uri (rust-uri version))
  109. (sha256 (base32 checksum))))
  110. (native-inputs
  111. (alist-replace "cargo-bootstrap" (list base-rust "cargo")
  112. (alist-replace "rustc-bootstrap" (list base-rust)
  113. (package-native-inputs base-rust))))))
  114. ;;; Note: mrustc's only purpose is to be able to bootstap Rust; it's designed
  115. ;;; to be used in source form.
  116. (define %mrustc-source
  117. (let ((name "mrustc")
  118. (version "0.9"))
  119. (origin
  120. (method git-fetch)
  121. (uri (git-reference
  122. (url "https://github.com/thepowersgang/mrustc")
  123. (commit (string-append "v" version))))
  124. (file-name (git-file-name name version))
  125. (sha256
  126. (base32
  127. "194ny7vsks5ygiw7d8yxjmp1qwigd71ilchis6xjl6bb2sj97rd2")))))
  128. ;;; Rust 1.29 is special in that it is built with mrustc, which shortens the
  129. ;;; bootstrap path. Note: the build is non-deterministic.
  130. (define-public rust-1.29
  131. (package
  132. (name "rust")
  133. (version "1.29.2")
  134. (source
  135. (origin
  136. (method url-fetch)
  137. (uri (rust-uri version))
  138. (sha256 (base32 "1jb787080z754caa2w3w1amsygs4qlzj9rs1vy64firfmabfg22h"))
  139. (modules '((guix build utils)))
  140. (snippet '(for-each delete-file-recursively
  141. '("src/jemalloc"
  142. "src/llvm"
  143. "src/llvm-emscripten")))
  144. (patches (search-patches "rust-reproducible-builds.patch"))))
  145. (outputs '("out" "cargo"))
  146. (properties '((timeout . 72000) ;20 hours
  147. (max-silent-time . 18000))) ;5 hours (for armel)
  148. (build-system gnu-build-system)
  149. (inputs
  150. `(("libcurl" ,curl)
  151. ("libssh2" ,libssh2)
  152. ;; Use llvm-7, which enables rust to be built reproducibly.
  153. ;; Versions newer than 7 fail to compile.
  154. ("llvm" ,llvm-7)
  155. ("openssl" ,openssl)
  156. ("zlib" ,zlib)))
  157. (native-inputs
  158. `(("bison" ,bison)
  159. ("flex" ,flex)
  160. ("pkg-config" ,pkg-config)
  161. ;; Required for the libstd sources.
  162. ("mrustc-source" ,%mrustc-source)))
  163. (arguments
  164. `(#:imported-modules ,%cargo-utils-modules ;for `generate-all-checksums'
  165. #:modules ((guix build cargo-utils)
  166. (guix build utils)
  167. (guix build gnu-build-system))
  168. #:test-target "test"
  169. ;; Rust's own .so library files are not found in any RUNPATH, but
  170. ;; that doesn't seem to cause issues.
  171. #:validate-runpath? #f
  172. #:make-flags
  173. (list ,(string-append "RUSTC_TARGET="
  174. (or (%current-target-system)
  175. (nix-system->gnu-triplet-for-rust)))
  176. ,(string-append "RUSTCSRC=../"))
  177. #:phases
  178. (modify-phases %standard-phases
  179. (add-after 'unpack 'patch-reference-to-cc
  180. ;; This prevents errors like 'error: linker `cc` not found' when
  181. ;; "cc" is not found on PATH.
  182. (lambda* (#:key inputs #:allow-other-keys)
  183. (let ((gcc (assoc-ref inputs "gcc")))
  184. (substitute* (find-files "." "^link.rs$")
  185. (("\"cc\".as_ref")
  186. (format #f "~s.as_ref" (string-append gcc "/bin/gcc")))))))
  187. (add-after 'unpack 'copy-mrustc-and-patch
  188. (lambda* (#:key inputs #:allow-other-keys)
  189. (copy-recursively (assoc-ref inputs "mrustc-source") "mrustc")
  190. (invoke "patch" "-p0" "-i" "mrustc/rustc-1.29.0-src.patch")))
  191. (add-after 'copy-mrustc-and-patch 'patch-makefiles
  192. ;; This disables building the (unbundled) LLVM.
  193. (lambda* (#:key inputs parallel-build? #:allow-other-keys)
  194. (let ((llvm (assoc-ref inputs "llvm"))
  195. (job-spec (format #f "-j~a"
  196. (if parallel-build?
  197. (number->string (parallel-job-count))
  198. "1"))))
  199. (with-directory-excursion "mrustc"
  200. (substitute* '("minicargo.mk"
  201. "run_rustc/Makefile")
  202. ;; Use the system-provided LLVM.
  203. (("LLVM_CONFIG := .*")
  204. (string-append "LLVM_CONFIG := " llvm "/bin/llvm-config\n"))
  205. (("\\$\\(LLVM_CONFIG\\): .*")
  206. "$(LLVM_CONFIG):\n")
  207. (("\\$Vcd \\$\\(RUSTCSRC\\)build && \\$\\(MAKE\\).*")
  208. "true\n"))
  209. ;; Patch date.
  210. (substitute* "Makefile"
  211. (("shell date")
  212. "shell date -d @1"))
  213. (substitute* "run_rustc/Makefile"
  214. (("[$]Vtime ")
  215. "$V ")
  216. ;; Unlock the number of parallel jobs for cargo.
  217. (("-j [[:digit:]]+ ")
  218. "")
  219. ;; Patch the shebang of a generated wrapper for rustc, and
  220. ;; make sure that \n newline escapes get interpreted
  221. ;; correctly, specifying the '-e' option of echo.
  222. (("echo '#!/bin/sh")
  223. (string-append "echo -e '#!" (which "sh"))))))))
  224. (add-after 'patch-source-shebangs 'patch-cargo-checksums
  225. (lambda* _
  226. (substitute* "src/Cargo.lock"
  227. (("(\"checksum .* = )\".*\"" all name)
  228. (string-append name "\"" ,%cargo-reference-hash "\"")))
  229. (generate-all-checksums "src/vendor")))
  230. (replace 'configure
  231. (lambda _
  232. (setenv "CC" "gcc")
  233. (setenv "CXX" "g++")
  234. ;; The Guix LLVM package installs only shared libraries.
  235. (setenv "LLVM_LINK_SHARED" "1")
  236. ;; This is a workaround for
  237. ;; https://github.com/thepowersgang/mrustc/issues/138.
  238. (setenv "LIBSSH2_SYS_USE_PKG_CONFIG" "yes")))
  239. (delete 'patch-generated-file-shebangs)
  240. (replace 'build
  241. (lambda* (#:key make-flags parallel-build? #:allow-other-keys)
  242. (let* ((job-count (if parallel-build?
  243. (parallel-job-count)
  244. 1))
  245. (job-spec (string-append "-j" (number->string job-count)))
  246. (make-flags* (cons job-spec make-flags)))
  247. ;; Adapted from:
  248. ;; https://github.com/dtolnay/bootstrap/blob/master/build.sh.
  249. (chdir "mrustc")
  250. (setenv "MINICARGO_FLAGS" job-spec)
  251. (setenv "CARGO_BUILD_JOBS" (number->string job-count))
  252. (display "Building rustc...\n")
  253. (apply invoke "make" "-f" "minicargo.mk" "output/rustc"
  254. make-flags*)
  255. (display "Building cargo...\n")
  256. (apply invoke "make" "-f" "minicargo.mk" "output/cargo"
  257. make-flags*)
  258. (display "Rebuilding stdlib with rustc...\n")
  259. (with-directory-excursion "run_rustc"
  260. (apply invoke "make" "RUST_SRC=../../src/" make-flags*)))))
  261. (replace 'install
  262. (lambda* (#:key inputs outputs #:allow-other-keys)
  263. (let* ((out (assoc-ref outputs "out"))
  264. (cargo (assoc-ref outputs "cargo"))
  265. (bin (string-append out "/bin"))
  266. (rustc (string-append bin "/rustc"))
  267. (cargo-bin (string-append cargo "/bin"))
  268. (lib (string-append out "/lib"))
  269. (gnu-triplet ,(or (%current-target-system)
  270. (nix-system->gnu-triplet-for-rust)))
  271. (system-lib-prefix (string-append lib "/rustlib/"
  272. gnu-triplet "/lib")))
  273. (mkdir-p (dirname rustc))
  274. (copy-file "run_rustc/output/prefix/bin/rustc_binary" rustc)
  275. (wrap-program rustc
  276. `("LD_LIBRARY_PATH" = (,system-lib-prefix)))
  277. (mkdir-p lib)
  278. (copy-recursively "run_rustc/output/prefix/lib" lib)
  279. (install-file "run_rustc/output/prefix/bin/cargo" cargo-bin)))))))
  280. (synopsis "Compiler for the Rust programming language")
  281. (description "Rust is a systems programming language that provides memory
  282. safety and thread safety guarantees.")
  283. (home-page "https://github.com/thepowersgang/mrustc")
  284. ;; Dual licensed.
  285. (license (list license:asl2.0 license:expat))))
  286. (define-public rust-1.30
  287. (package
  288. (name "rust")
  289. (version "1.30.1")
  290. (source (origin
  291. (inherit (package-source rust-1.29))
  292. (uri (rust-uri version))
  293. (sha256
  294. (base32 "0aavdc1lqv0cjzbqwl5n59yd0bqdlhn0zas61ljf38yrvc18k8rn"))
  295. (snippet '(for-each delete-file-recursively
  296. '("src/jemalloc"
  297. "src/llvm"
  298. "src/llvm-emscripten"
  299. "src/tools/clang"
  300. "src/tools/lldb")))))
  301. (outputs '("out" "cargo"))
  302. (properties '((timeout . 72000) ;20 hours
  303. (max-silent-time . 18000))) ;5 hours (for armel)
  304. (build-system gnu-build-system)
  305. (arguments
  306. ;; Only the final Rust is tested, not the intermediate bootstrap ones,
  307. ;; for performance and simplicity.
  308. `(#:tests? #f
  309. #:imported-modules ,%cargo-utils-modules ;for `generate-all-checksums'
  310. #:modules ((guix build utils)
  311. (guix build gnu-build-system)
  312. (ice-9 match)
  313. (srfi srfi-1))
  314. #:phases
  315. (modify-phases %standard-phases
  316. (add-after 'unpack 'set-env
  317. (lambda* (#:key inputs #:allow-other-keys)
  318. (setenv "SHELL" (which "sh"))
  319. (setenv "CONFIG_SHELL" (which "sh"))
  320. (setenv "CC" (search-input-file inputs "/bin/gcc"))
  321. ;; The Guix LLVM package installs only shared libraries.
  322. (setenv "LLVM_LINK_SHARED" "1")))
  323. (add-after 'unpack 'neuter-tidy
  324. ;; We often need to patch tests with various Guix-specific paths.
  325. ;; This often increases the line length and makes tidy, rustc's
  326. ;; style checker, complain. We could insert additional newlines or
  327. ;; add an "// ignore-tidy-linelength" comment, but as an ignore
  328. ;; comment must be used, both approaches are fragile due to
  329. ;; upstream formatting changes. As such, disable running the
  330. ;; linter during tests, since it's intended for rustc developers
  331. ;; anyway.
  332. (lambda _
  333. (substitute* "src/bootstrap/builder.rs"
  334. ((".*::Tidy,.*")
  335. ""))))
  336. (add-after 'patch-generated-file-shebangs 'patch-cargo-checksums
  337. (lambda* _
  338. (use-modules (guix build cargo-utils))
  339. (substitute* "src/Cargo.lock"
  340. (("(\"checksum .* = )\".*\"" all name)
  341. (string-append name "\"" ,%cargo-reference-hash "\"")))
  342. (generate-all-checksums "src/vendor")))
  343. (replace 'configure
  344. (lambda* (#:key inputs outputs #:allow-other-keys)
  345. (let* ((out (assoc-ref outputs "out"))
  346. (gcc (assoc-ref inputs "gcc"))
  347. (python (assoc-ref inputs "python"))
  348. (binutils (assoc-ref inputs "binutils"))
  349. (rustc (assoc-ref inputs "rustc-bootstrap"))
  350. (cargo (assoc-ref inputs "cargo-bootstrap"))
  351. (llvm (assoc-ref inputs "llvm"))
  352. (jemalloc (assoc-ref inputs "jemalloc")))
  353. (call-with-output-file "config.toml"
  354. (lambda (port)
  355. (display (string-append "
  356. [llvm]
  357. [build]
  358. cargo = \"" cargo "/bin/cargo" "\"
  359. rustc = \"" rustc "/bin/rustc" "\"
  360. docs = false
  361. python = \"" python "/bin/python" "\"
  362. vendor = true
  363. submodules = false
  364. [install]
  365. prefix = \"" out "\"
  366. sysconfdir = \"etc\"
  367. [rust]
  368. default-linker = \"" gcc "/bin/gcc" "\"
  369. channel = \"stable\"
  370. rpath = true
  371. [target." ,(nix-system->gnu-triplet-for-rust) "]
  372. llvm-config = \"" llvm "/bin/llvm-config" "\"
  373. cc = \"" gcc "/bin/gcc" "\"
  374. cxx = \"" gcc "/bin/g++" "\"
  375. ar = \"" binutils "/bin/ar" "\"
  376. jemalloc = \"" jemalloc "/lib/libjemalloc_pic.a" "\"
  377. [dist]
  378. ") port))))))
  379. (replace 'build
  380. (lambda* (#:key parallel-build? #:allow-other-keys)
  381. (let ((job-spec (string-append
  382. "-j" (if parallel-build?
  383. (number->string (parallel-job-count))
  384. "1"))))
  385. (invoke "./x.py" job-spec "build")
  386. (invoke "./x.py" job-spec "build" "src/tools/cargo"))))
  387. (replace 'install
  388. (lambda* (#:key outputs #:allow-other-keys)
  389. (let ((out (assoc-ref outputs "out"))
  390. (cargo-out (assoc-ref outputs "cargo")))
  391. (mkdir-p out)
  392. (invoke "./x.py" "install")
  393. (substitute* "config.toml"
  394. ;; Adjust the prefix to the 'cargo' output.
  395. (("prefix = \"[^\"]*\"")
  396. (format #f "prefix = ~s" cargo-out)))
  397. (mkdir-p cargo-out)
  398. (invoke "./x.py" "install" "cargo"))))
  399. (add-after 'install 'delete-install-logs
  400. (lambda* (#:key outputs #:allow-other-keys)
  401. (for-each (lambda (f)
  402. (false-if-exception (delete-file f)))
  403. (append-map (lambda (output)
  404. (find-files (string-append
  405. output "/lib/rustlib")
  406. "(^install.log$|^manifest-)"))
  407. (map cdr outputs)))))
  408. (add-after 'install 'wrap-rustc
  409. (lambda* (#:key inputs outputs #:allow-other-keys)
  410. (let ((out (assoc-ref outputs "out"))
  411. (libc (assoc-ref inputs "libc"))
  412. (ld-wrapper (assoc-ref inputs "ld-wrapper")))
  413. ;; Let gcc find ld and libc startup files.
  414. (wrap-program (string-append out "/bin/rustc")
  415. `("PATH" ":" prefix (,(string-append ld-wrapper "/bin")))
  416. `("LIBRARY_PATH" ":"
  417. suffix (,(string-append libc "/lib"))))))))))
  418. (native-inputs
  419. `(("cmake" ,cmake-minimal)
  420. ("pkg-config" ,pkg-config) ; For "cargo"
  421. ("python" ,python-wrapper)
  422. ("rustc-bootstrap" ,rust-1.29)
  423. ("cargo-bootstrap" ,rust-1.29 "cargo")
  424. ("which" ,which)))
  425. (inputs
  426. `(("jemalloc" ,jemalloc-4.5.0)
  427. ("llvm" ,llvm-7)
  428. ("openssl" ,openssl)
  429. ("libssh2" ,libssh2) ; For "cargo"
  430. ("libcurl" ,curl))) ; For "cargo"
  431. ;; rustc invokes gcc, so we need to set its search paths accordingly.
  432. ;; Note: duplicate its value here to cope with circular dependencies among
  433. ;; modules (see <https://bugs.gnu.org/31392>).
  434. (native-search-paths
  435. (list (search-path-specification
  436. (variable "C_INCLUDE_PATH")
  437. (files '("include")))
  438. (search-path-specification
  439. (variable "CPLUS_INCLUDE_PATH")
  440. (files '("include/c++" "include")))
  441. (search-path-specification
  442. (variable "LIBRARY_PATH")
  443. (files '("lib" "lib64")))))
  444. (supported-systems
  445. (delete "i686-linux" ; fails to build, see bug #35519
  446. %supported-systems))
  447. (synopsis "Compiler for the Rust progamming language")
  448. (description "Rust is a systems programming language that provides memory
  449. safety and thread safety guarantees.")
  450. (home-page "https://www.rust-lang.org")
  451. ;; Dual licensed.
  452. (license (list license:asl2.0 license:expat))))
  453. (define-public rust-1.31
  454. (rust-bootstrapped-package
  455. rust-1.30 "1.31.1" "0sk84ff0cklybcp0jbbxcw7lk7mrm6kb6km5nzd6m64dy0igrlli"))
  456. (define-public rust-1.32
  457. (let ((base-rust (rust-bootstrapped-package
  458. rust-1.31 "1.32.0"
  459. "0ji2l9xv53y27xy72qagggvq47gayr5lcv2jwvmfirx029vlqnac")))
  460. (package
  461. (inherit base-rust)
  462. (source
  463. (origin
  464. (inherit (package-source base-rust))
  465. (snippet '(for-each delete-file-recursively
  466. '("src/llvm"
  467. "src/llvm-emscripten"
  468. "src/tools/clang"
  469. "src/tools/lldb"
  470. "vendor/jemalloc-sys/jemalloc")))
  471. ;; the vendor directory has moved to the root of
  472. ;; the tarball, so we have to strip an extra prefix
  473. (patch-flags '("-p2"))))
  474. (arguments
  475. (substitute-keyword-arguments (package-arguments base-rust)
  476. ;; The test suite fails due to LLVM 7, required for the build to be
  477. ;; reproducible.
  478. ((#:tests? _ #t)
  479. #f)
  480. ((#:phases phases)
  481. `(modify-phases ,phases
  482. ;; Cargo.lock and the vendor/ directory have been moved to the
  483. ;; root of the rust tarball
  484. (replace 'patch-cargo-checksums
  485. (lambda* _
  486. (use-modules (guix build cargo-utils))
  487. (substitute* "Cargo.lock"
  488. (("(\"checksum .* = )\".*\"" all name)
  489. (string-append name "\"" ,%cargo-reference-hash "\"")))
  490. (generate-all-checksums "vendor")))
  491. (add-after 'configure 'override-jemalloc
  492. (lambda* (#:key inputs #:allow-other-keys)
  493. ;; The compiler is no longer directly built against jemalloc,
  494. ;; but rather via the jemalloc-sys crate (which vendors the
  495. ;; jemalloc source). To use jemalloc we must enable linking to
  496. ;; it (otherwise it would use the system allocator), and set
  497. ;; an environment variable pointing to the compiled jemalloc.
  498. (substitute* "config.toml"
  499. (("^jemalloc =.*$") "")
  500. (("[[]rust[]]") "\n[rust]\njemalloc=true\n"))
  501. (setenv "JEMALLOC_OVERRIDE"
  502. (search-input-file inputs
  503. "/lib/libjemalloc_pic.a")))))))))))
  504. (define-public rust-1.33
  505. (let ((base-rust (rust-bootstrapped-package
  506. rust-1.32 "1.33.0"
  507. "152x91mg7bz4ygligwjb05fgm1blwy2i70s2j03zc9jiwvbsh0as")))
  508. (package
  509. (inherit base-rust)
  510. (source
  511. (origin
  512. (inherit (package-source base-rust))
  513. (patches '())
  514. (patch-flags '("-p1"))))
  515. (inputs
  516. ;; Upgrade jemalloc.
  517. (alist-replace "jemalloc" (list jemalloc) (package-inputs base-rust))))))
  518. (define-public rust-1.34
  519. (let ((base-rust (rust-bootstrapped-package
  520. rust-1.33 "1.34.1"
  521. "19s09k7y5j6g3y4d2rk6kg9pvq6ml94c49w6b72dmq8p9lk8bixh")))
  522. (package
  523. (inherit base-rust)
  524. (source
  525. (origin
  526. (inherit (package-source base-rust))
  527. (snippet '(for-each delete-file-recursively
  528. '("src/llvm-emscripten"
  529. "src/llvm-project"
  530. "vendor/jemalloc-sys/jemalloc"))))))))
  531. (define-public rust-1.35
  532. (let ((base-rust (rust-bootstrapped-package
  533. rust-1.34 "1.35.0"
  534. "0bbizy6b7002v1rdhrxrf5gijclbyizdhkglhp81ib3bf5x66kas")))
  535. (package
  536. (inherit base-rust)
  537. (arguments
  538. (substitute-keyword-arguments (package-arguments base-rust)
  539. ((#:phases phases)
  540. `(modify-phases ,phases
  541. (delete 'disable-codegen-tests))))))))
  542. (define-public rust-1.36
  543. (rust-bootstrapped-package
  544. rust-1.35 "1.36.0" "06xv2p6zq03lidr0yaf029ii8wnjjqa894nkmrm6s0rx47by9i04"))
  545. (define-public rust-1.37
  546. (let ((base-rust (rust-bootstrapped-package
  547. rust-1.36 "1.37.0"
  548. "1hrqprybhkhs6d9b5pjskfnc5z9v2l2gync7nb39qjb5s0h703hj")))
  549. (package
  550. (inherit base-rust)
  551. (arguments
  552. (substitute-keyword-arguments (package-arguments base-rust)
  553. ((#:phases phases)
  554. `(modify-phases ,phases
  555. (add-before 'configure 'configure-cargo-home
  556. (lambda _
  557. (let ((cargo-home (string-append (getcwd) "/.cargo")))
  558. (mkdir-p cargo-home)
  559. (setenv "CARGO_HOME" cargo-home)))))))))))
  560. (define-public rust-1.38
  561. (rust-bootstrapped-package
  562. rust-1.37 "1.38.0" "101dlpsfkq67p0hbwx4acqq6n90dj4bbprndizpgh1kigk566hk4"))
  563. (define-public rust-1.39
  564. (let ((base-rust (rust-bootstrapped-package
  565. rust-1.38 "1.39.0"
  566. "0mwkc1bnil2cfyf6nglpvbn2y0zfbv44zfhsd5qg4c9rm6vgd8dl")))
  567. (package
  568. (inherit base-rust)
  569. (arguments
  570. (substitute-keyword-arguments (package-arguments base-rust)
  571. ((#:phases phases)
  572. `(modify-phases ,phases
  573. (replace 'patch-cargo-checksums
  574. ;; The Cargo.lock format changed.
  575. (lambda* _
  576. (use-modules (guix build cargo-utils))
  577. (substitute* "Cargo.lock"
  578. (("(checksum = )\".*\"" all name)
  579. (string-append name "\"" ,%cargo-reference-hash "\"")))
  580. (generate-all-checksums "vendor"))))))))))
  581. (define-public rust-1.40
  582. (let ((base-rust (rust-bootstrapped-package
  583. rust-1.39 "1.40.0"
  584. "1ba9llwhqm49w7sz3z0gqscj039m53ky9wxzhaj11z6yg1ah15yx")))
  585. (package
  586. (inherit base-rust)
  587. (source
  588. (origin
  589. (inherit (package-source base-rust))
  590. ;; llvm-emscripten is no longer bundled, as that codegen backend
  591. ;; got removed.
  592. (snippet '(for-each delete-file-recursively
  593. '("src/llvm-project"
  594. "vendor/jemalloc-sys/jemalloc")))))
  595. ;; Rust 1.40 does not ship rustc-internal libraries by default (see
  596. ;; rustc-dev-split). This means that librustc_driver.so is no longer
  597. ;; available in lib/rustlib/$target/lib, which is the directory
  598. ;; included in the runpath of librustc_codegen_llvm-llvm.so. This is
  599. ;; detected by our validate-runpath phase as an error, but it is
  600. ;; harmless as the codegen backend is loaded by librustc_driver.so
  601. ;; itself, which must at that point have been already loaded. As such,
  602. ;; we skip validating the runpath for Rust 1.40. Rust 1.41 stopped
  603. ;; putting the codegen backend in a separate library, which makes this
  604. ;; workaround only necessary for this release.
  605. (arguments (substitute-keyword-arguments (package-arguments base-rust)
  606. ((#:validate-runpath? _ #f)
  607. #f))))))
  608. (define-public rust-1.41
  609. (let ((base-rust (rust-bootstrapped-package
  610. rust-1.40 "1.41.1"
  611. "0ws5x0fxv57fyllsa6025h3q6j9v3m8nb3syl4x0hgkddq0kvj9q")))
  612. (package
  613. (inherit base-rust)
  614. (arguments
  615. (substitute-keyword-arguments (package-arguments base-rust)
  616. ((#:validate-runpath? _ #t)
  617. #t))))))
  618. (define-public rust-1.42
  619. (rust-bootstrapped-package
  620. rust-1.41 "1.42.0" "0x9lxs82may6c0iln0b908cxyn1cv7h03n5cmbx3j1bas4qzks6j"))
  621. (define-public rust-1.43
  622. (rust-bootstrapped-package
  623. rust-1.42 "1.43.0" "18akhk0wz1my6y9vhardriy2ysc482z0fnjdcgs9gy59kmnarxkm"))
  624. ;; This version needs llvm >= 8.0 and NOT 11
  625. (define-public rust-1.44
  626. (let ((base-rust (rust-bootstrapped-package
  627. rust-1.43 "1.44.1"
  628. "0ww4z2v3gxgn3zddqzwqya1gln04p91ykbrflnpdbmcd575n8bky")))
  629. (package
  630. (inherit base-rust)
  631. (inputs
  632. (alist-replace "llvm" (list llvm-10) (package-inputs base-rust))))))
  633. (define-public rust-1.45
  634. (let ((base-rust (rust-bootstrapped-package
  635. rust-1.44 "1.45.2"
  636. "0273a1g3f59plyi1n0azf21qjzwml1yqdnj5z472crz37qggr8xp")))
  637. (package
  638. (inherit base-rust)
  639. (arguments
  640. (substitute-keyword-arguments (package-arguments base-rust)
  641. ((#:phases phases)
  642. `(modify-phases ,phases
  643. (add-after 'unpack 'set-linker-locale-to-utf8
  644. (lambda _
  645. (substitute* (find-files "." "^linker.rs$")
  646. (("linker.env\\(\"LC_ALL\", \"C\"\\);")
  647. "linker.env(\"LC_ALL\", \"en_US.UTF-8\");")))))))))))
  648. (define-public rust-1.46
  649. (let ((base-rust (rust-bootstrapped-package
  650. rust-1.45 "1.46.0"
  651. "0a17jby2pd050s24cy4dfc0gzvgcl585v3vvyfilniyvjrqknsid")))
  652. (package
  653. (inherit base-rust)
  654. (outputs (cons "rustfmt" (package-outputs base-rust)))
  655. (arguments
  656. (substitute-keyword-arguments (package-arguments base-rust)
  657. ((#:phases phases)
  658. `(modify-phases ,phases
  659. (replace 'build
  660. ;; Phase overridden to also build rustfmt.
  661. (lambda* (#:key parallel-build? #:allow-other-keys)
  662. (let ((job-spec (string-append
  663. "-j" (if parallel-build?
  664. (number->string (parallel-job-count))
  665. "1"))))
  666. (invoke "./x.py" job-spec "build")
  667. (invoke "./x.py" job-spec "build" "src/tools/cargo")
  668. (invoke "./x.py" job-spec "build" "src/tools/rustfmt"))))
  669. (replace 'check
  670. ;; Phase overridden to also test rustfmt.
  671. (lambda* (#:key tests? parallel-build? #:allow-other-keys)
  672. (when tests?
  673. (let ((job-spec (string-append
  674. "-j" (if parallel-build?
  675. (number->string (parallel-job-count))
  676. "1"))))
  677. (invoke "./x.py" job-spec "test" "-vv")
  678. (invoke "./x.py" job-spec "test" "src/tools/cargo")
  679. (invoke "./x.py" job-spec "test" "src/tools/rustfmt")))))
  680. (replace 'install
  681. ;; Phase overridden to also install rustfmt.
  682. (lambda* (#:key outputs #:allow-other-keys)
  683. (invoke "./x.py" "install")
  684. (substitute* "config.toml"
  685. ;; Adjust the prefix to the 'cargo' output.
  686. (("prefix = \"[^\"]*\"")
  687. (format #f "prefix = ~s" (assoc-ref outputs "cargo"))))
  688. (invoke "./x.py" "install" "cargo")
  689. (substitute* "config.toml"
  690. ;; Adjust the prefix to the 'rustfmt' output.
  691. (("prefix = \"[^\"]*\"")
  692. (format #f "prefix = ~s" (assoc-ref outputs "rustfmt"))))
  693. (invoke "./x.py" "install" "rustfmt"))))))))))
  694. (define-public rust-1.47
  695. (rust-bootstrapped-package
  696. rust-1.46 "1.47.0" "07fqd2vp7cf1ka3hr207dnnz93ymxml4935vp74g4is79h3dz19i"))
  697. (define-public rust-1.48
  698. (rust-bootstrapped-package
  699. rust-1.47 "1.48.0" "0fz4gbb5hp5qalrl9lcl8yw4kk7ai7wx511jb28nypbxninkwxhf"))
  700. (define-public rust-1.49
  701. (let ((base-rust (rust-bootstrapped-package
  702. rust-1.48 "1.49.0"
  703. "0yf7kll517398dgqsr7m3gldzj0iwsp3ggzxrayckpqzvylfy2mm")))
  704. (package
  705. (inherit base-rust)
  706. (outputs (cons "doc" (package-outputs base-rust)))
  707. (arguments
  708. (substitute-keyword-arguments (package-arguments base-rust)
  709. ((#:tests? _ #f)
  710. #t)
  711. ((#:phases phases)
  712. `(modify-phases ,phases
  713. (add-after 'unpack 'relax-gdb-auto-load-safe-path
  714. ;; Allow GDB to load binaries from any location, otherwise the
  715. ;; gdbinfo tests fail. This is only useful when testing with a
  716. ;; GDB version newer than 8.2.
  717. (lambda _
  718. (setenv "HOME" (getcwd))
  719. (with-output-to-file (string-append (getenv "HOME") "/.gdbinit")
  720. (lambda _
  721. (format #t "set auto-load safe-path /~%")))
  722. ;; Do not launch gdb with '-nx' which causes it to not execute
  723. ;; any init file.
  724. (substitute* "src/tools/compiletest/src/runtest.rs"
  725. (("\"-nx\".as_ref\\(\\), ")
  726. ""))))
  727. (add-after 'unpack 'patch-cargo-env-shebang
  728. (lambda _
  729. (substitute* '("src/tools/cargo/tests/testsuite/build.rs"
  730. "src/tools/cargo/tests/testsuite/fix.rs")
  731. ;; The cargo *_wrapper tests set RUSTC.*WRAPPER environment
  732. ;; variable which points to /usr/bin/env. Since it's not a
  733. ;; shebang, it needs to be manually patched.
  734. (("/usr/bin/env")
  735. (which "env")))))
  736. (add-after 'unpack 'disable-tests-requiring-git
  737. (lambda _
  738. (substitute* "src/tools/cargo/tests/testsuite/new.rs"
  739. (("fn author_prefers_cargo")
  740. "#[ignore]\nfn author_prefers_cargo")
  741. (("fn finds_author_git")
  742. "#[ignore]\nfn finds_author_git")
  743. (("fn finds_local_author_git")
  744. "#[ignore]\nfn finds_local_author_git"))))
  745. (add-after 'unpack 'patch-command-exec-tests
  746. ;; This test suite includes some tests that the stdlib's
  747. ;; `Command` execution properly handles in situations where
  748. ;; the environment or PATH variable are empty, but this fails
  749. ;; since we don't have `echo` available at its usual FHS
  750. ;; location.
  751. (lambda _
  752. (substitute* (match (find-files "." "^command-exec.rs$")
  753. ((file) file))
  754. (("Command::new\\(\"echo\"\\)")
  755. (format #f "Command::new(~s)" (which "echo"))))))
  756. (add-after 'unpack 'patch-command-uid-gid-test
  757. (lambda _
  758. (substitute* (match (find-files "." "^command-uid-gid.rs$")
  759. ((file) file))
  760. (("/bin/sh")
  761. (which "sh")))))
  762. (add-after 'unpack 'skip-shebang-tests
  763. ;; This test make sure that the parser behaves properly when a
  764. ;; source file starts with a shebang. Unfortunately, the
  765. ;; patch-shebangs phase changes the meaning of these edge-cases.
  766. ;; We skip the test since it's drastically unlikely Guix's
  767. ;; packaging will introduce a bug here.
  768. (lambda _
  769. (delete-file "src/test/ui/parser/shebang/sneaky-attrib.rs")))
  770. (add-after 'unpack 'patch-process-tests
  771. (lambda* (#:key inputs #:allow-other-keys)
  772. (let ((bash (assoc-ref inputs "bash")))
  773. (substitute* "library/std/src/process/tests.rs"
  774. (("\"/bin/sh\"")
  775. (string-append "\"" bash "/bin/sh\"")))
  776. (substitute* "library/std/src/sys/unix/process/process_common/tests.rs"
  777. (("fn test_process_mask")
  778. "#[allow(unused_attributes)]
  779. #[ignore]
  780. fn test_process_mask")))))
  781. (add-after 'unpack 'disable-interrupt-tests
  782. (lambda _
  783. ;; This test hangs in the build container; disable it.
  784. (substitute* (match (find-files "." "^freshness.rs$")
  785. ((file) file))
  786. (("fn linking_interrupted")
  787. "#[ignore]\nfn linking_interrupted"))
  788. ;; Likewise for the ctrl_c_kills_everyone test.
  789. (substitute* (match (find-files "." "^death.rs$")
  790. ((file) file))
  791. (("fn ctrl_c_kills_everyone")
  792. "#[ignore]\nfn ctrl_c_kills_everyone"))))
  793. (add-after 'configure 'enable-docs
  794. (lambda _
  795. (substitute* "config.toml"
  796. (("docs = false")
  797. "docs = true"))))
  798. (add-after 'configure 'add-gdb-to-config
  799. (lambda* (#:key inputs #:allow-other-keys)
  800. (let ((gdb (assoc-ref inputs "gdb")))
  801. (substitute* "config.toml"
  802. (("^python =.*" all)
  803. (string-append all
  804. "gdb = \"" gdb "/bin/gdb\"\n"))))))))))
  805. ;; Add test inputs.
  806. (native-inputs (cons*
  807. ;; The tests fail when using GDB 10 (see:
  808. ;; https://github.com/rust-lang/rust/issues/79009).
  809. `("gdb" ,gdb-9.2)
  810. `("procps" ,procps)
  811. (package-native-inputs base-rust))))))
  812. (define-public rust-1.50
  813. (rust-bootstrapped-package rust-1.49 "1.50.0"
  814. "0pjs7j62maiyvkmhp9zrxl528g2n0fphp4rq6ap7aqdv0a6qz5wm"))
  815. (define-public rust-1.51
  816. (rust-bootstrapped-package rust-1.50 "1.51.0"
  817. "0ixqkqglv3isxbvl4ldr4byrkx692wghsz3fasy1pn5kr2prnsvs"))
  818. (define-public rust-1.52
  819. (let ((base-rust
  820. (rust-bootstrapped-package rust-1.51 "1.52.1"
  821. "165zs3xzp9dravybwslqs1qhn35agp6wacmzpymqg3qfdni26vrs")))
  822. (package
  823. (inherit base-rust)
  824. (inputs
  825. (alist-replace "llvm" (list llvm-12)
  826. (package-inputs base-rust))))))
  827. ;;; Note: Only the latest versions of Rust are supported and tested. The
  828. ;;; intermediate rusts are built for bootstrapping purposes and should not
  829. ;;; be relied upon. This is to ease maintenance and reduce the time
  830. ;;; required to build the full Rust bootstrap chain.
  831. (define-public rust rust-1.49)