Advertisement
Guest User

rust-1.74.1-r101.ebuild

a guest
Mar 15th, 2025
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 37.79 KB | Software | 0 0
  1. # Copyright 1999-2025 Gentoo Authors
  2. # Distributed under the terms of the GNU General Public License v2
  3.  
  4. EAPI=8
  5.  
  6. LLVM_COMPAT=( 17 )
  7. PYTHON_COMPAT=( python3_{10..12} )
  8.  
  9. RUST_MAX_VER=${PV}
  10. RUST_MIN_VER="$(ver_cut 1).$(($(ver_cut 2) - 1)).0"
  11. RUST_OPTIONAL=1
  12.  
  13. MRUSTC_VERSION="0.11.2"
  14. MRUSTC_RUST_VERSION="1.74.0"
  15.  
  16. inherit check-reqs cmake edo estack flag-o-matic llvm-r1 multiprocessing multilib multilib-build \
  17. optfeature python-any-r1 rust rust-toolchain toolchain-funcs verify-sig
  18.  
  19. if [[ ${PV} = *beta* ]]; then
  20. betaver=${PV//*beta}
  21. BETA_SNAPSHOT="${betaver:0:4}-${betaver:4:2}-${betaver:6:2}"
  22. MY_P="rustc-beta"
  23. SRC="${BETA_SNAPSHOT}/rustc-beta-src.tar.xz -> rustc-${PV}-src.tar.xz"
  24. else
  25. MY_P="rustc-${PV}"
  26. SRC="${MY_P}-src.tar.xz"
  27. KEYWORDS="amd64 arm arm64 ~loong ppc ppc64 ~riscv sparc x86"
  28. fi
  29.  
  30. DESCRIPTION="Language empowering everyone to build reliable and efficient software"
  31. HOMEPAGE="https://www.rust-lang.org/"
  32.  
  33. SRC_URI="
  34. https://static.rust-lang.org/dist/${SRC}
  35. verify-sig? ( https://static.rust-lang.org/dist/${SRC}.asc )
  36. "
  37.  
  38. S="${WORKDIR}/${MY_P}-src"
  39.  
  40. # keep in sync with llvm ebuild of the same version as bundled one.
  41. ALL_LLVM_TARGETS=( AArch64 AMDGPU ARC ARM AVR BPF CSKY DirectX Hexagon Lanai
  42. LoongArch M68k Mips MSP430 NVPTX PowerPC RISCV Sparc SPIRV SystemZ VE
  43. WebAssembly X86 XCore Xtensa )
  44. ALL_LLVM_TARGETS=( "${ALL_LLVM_TARGETS[@]/#/llvm_targets_}" )
  45. LLVM_TARGET_USEDEPS=${ALL_LLVM_TARGETS[@]/%/(-)?}
  46.  
  47. ALL_LLVM_EXPERIMENTAL_TARGETS=( ARC CSKY DirectX M68k SPIRV Xtensa )
  48.  
  49. LICENSE="|| ( MIT Apache-2.0 ) BSD BSD-1 BSD-2 BSD-4"
  50. SLOT="${PV}"
  51.  
  52. IUSE="big-endian clippy cpu_flags_x86_sse2 debug dist doc llvm-libunwind lto miri mrustc-bootstrap nightly parallel-compiler rustfmt rust-analyzer rust-src system-llvm test wasm ${ALL_LLVM_TARGETS[*]}"
  53.  
  54. LLVM_DEPEND=()
  55. # splitting usedeps needed to avoid CI/pkgcheck's UncheckableDep limitation
  56. for _x in "${ALL_LLVM_TARGETS[@]}"; do
  57. LLVM_DEPEND+=( " ${_x}? ( $(llvm_gen_dep "llvm-core/llvm:\${LLVM_SLOT}[${_x}]") )" )
  58. done
  59. LLVM_DEPEND+=( " wasm? ( $(llvm_gen_dep 'llvm-core/lld:${LLVM_SLOT}') )" )
  60. LLVM_DEPEND+=( " $(llvm_gen_dep 'llvm-core/llvm:${LLVM_SLOT}')" )
  61.  
  62. BDEPEND="${PYTHON_DEPS}
  63. app-eselect/eselect-rust
  64. || (
  65. >=sys-devel/gcc-4.7
  66. >=llvm-core/clang-3.5
  67. )
  68. !system-llvm? (
  69. >=dev-build/cmake-3.13.4
  70. app-alternatives/ninja
  71. )
  72. test? ( dev-debug/gdb )
  73. verify-sig? ( sec-keys/openpgp-keys-rust )
  74. mrustc-bootstrap? (
  75. ~dev-lang/mrustc-${MRUSTC_VERSION}
  76. dev-build/cmake
  77. sys-devel/gcc:*
  78. )
  79. !mrustc-bootstrap? ( ${RUST_DEPEND} )
  80. "
  81.  
  82. DEPEND="
  83. >=app-arch/xz-utils-5.2
  84. net-misc/curl:=[http2,ssl]
  85. sys-libs/zlib:=
  86. dev-libs/openssl:0=
  87. system-llvm? (
  88. ${LLVM_DEPEND[*]}
  89. llvm-libunwind? ( llvm-runtimes/libunwind:= )
  90. )
  91. !system-llvm? (
  92. !llvm-libunwind? (
  93. elibc_musl? ( sys-libs/libunwind:= )
  94. )
  95. )
  96. "
  97.  
  98. RDEPEND="${DEPEND}
  99. app-eselect/eselect-rust
  100. dev-lang/rust-common
  101. sys-apps/lsb-release
  102. !dev-lang/rust:stable
  103. !dev-lang/rust-bin:stable
  104. "
  105.  
  106. REQUIRED_USE="|| ( ${ALL_LLVM_TARGETS[*]} )
  107. miri? ( nightly )
  108. parallel-compiler? ( nightly )
  109. rust-analyzer? ( rust-src )
  110. test? ( ${ALL_LLVM_TARGETS[*]} )
  111. wasm? ( llvm_targets_WebAssembly )
  112. x86? ( cpu_flags_x86_sse2 )
  113. "
  114.  
  115. # we don't use cmake.eclass, but can get a warning
  116. CMAKE_WARN_UNUSED_CLI=no
  117.  
  118. QA_FLAGS_IGNORED="
  119. usr/lib/${PN}/${PV}/bin/.*
  120. usr/lib/${PN}/${PV}/libexec/.*
  121. usr/lib/${PN}/${PV}/lib/lib.*.so
  122. usr/lib/${PN}/${PV}/lib/rustlib/.*/bin/.*
  123. usr/lib/${PN}/${PV}/lib/rustlib/.*/lib/lib.*.so
  124. "
  125.  
  126. QA_SONAME="
  127. usr/lib/${PN}/${PV}/lib/lib.*.so.*
  128. usr/lib/${PN}/${PV}/lib/rustlib/.*/lib/lib.*.so
  129. "
  130.  
  131. QA_PRESTRIPPED="
  132. usr/lib/${PN}/${PV}/lib/rustlib/.*/bin/rust-llvm-dwp
  133. usr/lib/${PN}/${PV}/lib/rustlib/.*/lib/self-contained/crtn.o
  134. "
  135.  
  136. # An rmeta file is custom binary format that contains the metadata for the crate.
  137. # rmeta files do not support linking, since they do not contain compiled object files.
  138. # so we can safely silence the warning for this QA check.
  139. QA_EXECSTACK="usr/lib/${PN}/${PV}/lib/rustlib/*/lib*.rlib:lib.rmeta"
  140.  
  141. # causes double bootstrap
  142. RESTRICT="test"
  143.  
  144. VERIFY_SIG_OPENPGP_KEY_PATH=/usr/share/openpgp-keys/rust.asc
  145.  
  146. PATCHES=(
  147. "${FILESDIR}"/1.74.1-cross-compile-libz.patch
  148. #"${FILESDIR}"/1.72.0-bump-libc-deps-to-0.2.146.patch # pending refresh
  149. "${FILESDIR}"/1.70.0-ignore-broken-and-non-applicable-tests.patch
  150. "${FILESDIR}"/1.62.1-musl-dynamic-linking.patch
  151. "${FILESDIR}"/1.67.0-doc-wasm.patch
  152. )
  153.  
  154. clear_vendor_checksums() {
  155. sed -i 's/\("files":{\)[^}]*/\1/' "vendor/${1}/.cargo-checksum.json" || die
  156. }
  157.  
  158. toml_usex() {
  159. usex "${1}" true false
  160. }
  161.  
  162. pre_build_checks() {
  163. local M=9216
  164. # multiply requirements by 1.3 if we are doing x86-multilib
  165. if use amd64; then
  166. M=$(( $(usex abi_x86_32 13 10) * ${M} / 10 ))
  167. fi
  168. M=$(( $(usex clippy 128 0) + ${M} ))
  169. M=$(( $(usex miri 128 0) + ${M} ))
  170. M=$(( $(usex rustfmt 256 0) + ${M} ))
  171. # add 2G if we compile llvm and 256M per llvm_target
  172. if ! use system-llvm; then
  173. M=$(( 2048 + ${M} ))
  174. local ltarget
  175. for ltarget in ${ALL_LLVM_TARGETS[@]}; do
  176. M=$(( $(usex ${ltarget} 256 0) + ${M} ))
  177. done
  178. fi
  179. M=$(( $(usex wasm 256 0) + ${M} ))
  180. M=$(( $(usex debug 2 1) * ${M} ))
  181. eshopts_push -s extglob
  182. if is-flagq '-g?(gdb)?([1-9])'; then
  183. M=$(( 15 * ${M} / 10 ))
  184. fi
  185. eshopts_pop
  186. M=$(( $(usex doc 256 0) + ${M} ))
  187. if use mrustc-bootstrap; then
  188. M=$(( 2 * ${M} ))
  189. fi
  190. CHECKREQS_DISK_BUILD=${M}M check-reqs_pkg_${EBUILD_PHASE}
  191. }
  192.  
  193. llvm_check_deps() {
  194. has_version -r "llvm-core/llvm:${LLVM_SLOT}[${LLVM_TARGET_USEDEPS// /,}]"
  195. }
  196.  
  197. # Is LLVM being linked against libc++?
  198. is_libcxx_linked() {
  199. local code='#include <ciso646>
  200. #if defined(_LIBCPP_VERSION)
  201. HAVE_LIBCXX
  202. #endif
  203. '
  204. local out=$($(tc-getCXX) ${CXXFLAGS} ${CPPFLAGS} -x c++ -E -P - <<<"${code}") || return 1
  205. [[ ${out} == *HAVE_LIBCXX* ]]
  206. }
  207.  
  208. pkg_pretend() {
  209. pre_build_checks
  210. }
  211.  
  212. pkg_setup() {
  213. if [[ ${MERGE_TYPE} != binary ]]; then
  214. pre_build_checks
  215. python-any-r1_pkg_setup
  216.  
  217. export LIBGIT2_NO_PKG_CONFIG=1 #749381
  218. if tc-is-cross-compiler; then
  219. use system-llvm && die "USE=system-llvm not allowed when cross-compiling"
  220. local cross_llvm_target="$(llvm_tuple_to_target "${CBUILD}")"
  221. use "llvm_targets_${cross_llvm_target}" || \
  222. die "Must enable LLVM_TARGETS=${cross_llvm_target} matching CBUILD=${CBUILD} when cross-compiling"
  223. fi
  224.  
  225. if use mrustc-bootstrap; then
  226. echo "yay no gcc dep"
  227. #if ! tc-is-gcc; then
  228. # die "USE=mrustc-bootstrap reqires that the build environment use GCC"
  229. #fi
  230. else
  231. rust_pkg_setup
  232. fi
  233.  
  234. if use system-llvm; then
  235. llvm-r1_pkg_setup
  236.  
  237. local llvm_config="$(get_llvm_prefix)/bin/llvm-config"
  238. export LLVM_LINK_SHARED=1
  239. export RUSTFLAGS="${RUSTFLAGS} -Lnative=$("${llvm_config}" --libdir)"
  240. fi
  241. fi
  242. }
  243.  
  244. src_prepare() {
  245. default
  246. # We'll need to revert this after the bootstrap.
  247. if use mrustc-bootstrap; then
  248. pushd "${S}" 2>/dev/null || die
  249. patch -p0 < "${BROOT}"/usr/share/mrustc-${MRUSTC_VERSION}/patches/rustc-${MRUSTC_RUST_VERSION}-src.patch ||
  250. die "Failed to patch sources to enable bootstrap with mrustc"
  251. popd 2>/dev/null || die
  252. fi
  253. }
  254.  
  255. src_configure() {
  256. if tc-is-cross-compiler; then
  257. export PKG_CONFIG_ALLOW_CROSS=1
  258. export PKG_CONFIG_PATH="${ESYSROOT}/usr/$(get_libdir)/pkgconfig"
  259. export OPENSSL_INCLUDE_DIR="${ESYSROOT}/usr/include"
  260. export OPENSSL_LIB_DIR="${ESYSROOT}/usr/$(get_libdir)"
  261. fi
  262.  
  263. filter-lto # https://bugs.gentoo.org/862109 https://bugs.gentoo.org/866231
  264.  
  265. local rust_target="" rust_targets="" arch_cflags
  266.  
  267. # Collect rust target names to compile standard libs for all ABIs.
  268. for v in $(multilib_get_enabled_abi_pairs); do
  269. rust_targets+=",\"$(rust_abi $(get_abi_CHOST ${v##*.}))\""
  270. done
  271. if use wasm; then
  272. rust_targets+=",\"wasm32-unknown-unknown\""
  273. if use system-llvm; then
  274. # un-hardcode rust-lld linker for this target
  275. # https://bugs.gentoo.org/715348
  276. sed -i '/linker:/ s/rust-lld/wasm-ld/' compiler/rustc_target/src/spec/wasm_base.rs || die
  277. fi
  278. fi
  279. rust_targets="${rust_targets#,}"
  280.  
  281. # cargo and rustdoc are mandatory and should always be included
  282. local tools='"cargo","rustdoc", "rust-demangler"'
  283. use clippy && tools+=',"clippy"'
  284. use miri && tools+=',"miri"'
  285. use rustfmt && tools+=',"rustfmt"'
  286. use rust-analyzer && tools+=',"rust-analyzer"'
  287. use rust-src && tools+=',"src"'
  288.  
  289. if use mrustc-bootstrap; then
  290. local rust_stage0_root="${WORKDIR}/bootstrap/rust-${PV}"
  291. else
  292. local rust_stage0_root="$(${RUSTC} --print sysroot || die "Can't determine rust's sysroot")"
  293. # in case of prefix it will be already prefixed, as --print sysroot returns full path
  294. [[ -d ${rust_stage0_root} ]] || die "${rust_stage0_root} is not a directory"
  295. fi
  296.  
  297. rust_target="$(rust_abi)"
  298. rust_build="$(rust_abi "${CBUILD}")"
  299. rust_host="$(rust_abi "${CHOST}")"
  300.  
  301. LLVM_EXPERIMENTAL_TARGETS=()
  302. for _x in "${ALL_LLVM_EXPERIMENTAL_TARGETS[@]}"; do
  303. if use llvm_targets_${_x} ; then
  304. LLVM_EXPERIMENTAL_TARGETS+=( ${_x} )
  305. fi
  306. done
  307. LLVM_EXPERIMENTAL_TARGETS=${LLVM_EXPERIMENTAL_TARGETS[@]}
  308.  
  309. local cm_btype="$(usex debug DEBUG RELEASE)"
  310. cat <<- _EOF_ > "${S}"/config.toml
  311. changelog-seen = 2
  312. [llvm]
  313. download-ci-llvm = false
  314. optimize = $(toml_usex !debug)
  315. release-debuginfo = $(toml_usex debug)
  316. assertions = $(toml_usex debug)
  317. ninja = true
  318. targets = "${LLVM_TARGETS// /;}"
  319. experimental-targets = "${LLVM_EXPERIMENTAL_TARGETS// /;}"
  320. link-shared = $(toml_usex system-llvm)
  321. $(if is_libcxx_linked; then
  322. # https://bugs.gentoo.org/732632
  323. echo "use-libcxx = true"
  324. echo "static-libstdcpp = false"
  325. fi)
  326. $(case "${rust_target}" in
  327. i586-*-linux-*)
  328. # https://github.com/rust-lang/rust/issues/93059
  329. echo 'cflags = "-fcf-protection=none"'
  330. echo 'cxxflags = "-fcf-protection=none"'
  331. echo 'ldflags = "-fcf-protection=none"'
  332. ;;
  333. *)
  334. ;;
  335. esac)
  336. enable-warnings = false
  337. [llvm.build-config]
  338. CMAKE_VERBOSE_MAKEFILE = "ON"
  339. $(if ! tc-is-cross-compiler; then
  340. # When cross-compiling, LLVM is compiled twice, once for host and
  341. # once for target. Unfortunately, this build configuration applies
  342. # to both, which means any flags applicable to one target but not
  343. # the other will break. Conditionally disable respecting user
  344. # flags when cross-compiling.
  345. echo "CMAKE_C_FLAGS_${cm_btype} = \"${CFLAGS}\""
  346. echo "CMAKE_CXX_FLAGS_${cm_btype} = \"${CXXFLAGS}\""
  347. echo "CMAKE_EXE_LINKER_FLAGS_${cm_btype} = \"${LDFLAGS}\""
  348. echo "CMAKE_MODULE_LINKER_FLAGS_${cm_btype} = \"${LDFLAGS}\""
  349. echo "CMAKE_SHARED_LINKER_FLAGS_${cm_btype} = \"${LDFLAGS}\""
  350. echo "CMAKE_STATIC_LINKER_FLAGS_${cm_btype} = \"${ARFLAGS}\""
  351. fi)
  352. [build]
  353. build-stage = 2
  354. test-stage = 2
  355. build = "${rust_build}"
  356. host = ["${rust_host}"]
  357. target = [${rust_targets}]
  358. cargo = "${rust_stage0_root}/bin/cargo"
  359. rustc = "${rust_stage0_root}/bin/rustc"
  360. rustfmt = "${rust_stage0_root}/bin/rustfmt"
  361. docs = $(toml_usex doc)
  362. compiler-docs = false
  363. submodules = false
  364. python = "${EPYTHON}"
  365. locked-deps = true
  366. vendor = true
  367. extended = true
  368. tools = [${tools}]
  369. verbose = 2
  370. sanitizers = false
  371. profiler = true
  372. cargo-native-static = false
  373. [install]
  374. prefix = "${EPREFIX}/usr/lib/${PN}/${PV}"
  375. sysconfdir = "etc"
  376. docdir = "share/doc/rust"
  377. bindir = "bin"
  378. libdir = "lib"
  379. mandir = "share/man"
  380. [rust]
  381. # https://github.com/rust-lang/rust/issues/54872
  382. codegen-units-std = 1
  383. optimize = true
  384. debug = $(toml_usex debug)
  385. debug-assertions = $(toml_usex debug)
  386. debug-assertions-std = $(toml_usex debug)
  387. debuginfo-level = $(usex debug 2 0)
  388. debuginfo-level-rustc = $(usex debug 2 0)
  389. debuginfo-level-std = $(usex debug 2 0)
  390. debuginfo-level-tools = $(usex debug 2 0)
  391. debuginfo-level-tests = 0
  392. backtrace = true
  393. incremental = false
  394. $(if ! tc-is-cross-compiler; then
  395. echo "default-linker = \"$(tc-getCC)\""
  396. fi)
  397. parallel-compiler = $(toml_usex parallel-compiler)
  398. channel = "$(usex nightly nightly stable)"
  399. description = "gentoo"
  400. rpath = true
  401. verbose-tests = true
  402. optimize-tests = $(toml_usex !debug)
  403. codegen-tests = true
  404. dist-src = false
  405. remap-debuginfo = true
  406. lld = $(usex system-llvm false $(toml_usex wasm))
  407. # only deny warnings if doc+wasm are NOT requested, documenting stage0 wasm std fails without it
  408. # https://github.com/rust-lang/rust/issues/74976
  409. # https://github.com/rust-lang/rust/issues/76526
  410. deny-warnings = $(usex wasm $(usex doc false true) true)
  411. backtrace-on-ice = true
  412. jemalloc = false
  413. lto = "$(usex lto fat off)"
  414. [dist]
  415. src-tarball = false
  416. compression-formats = ["xz"]
  417. compression-profile = "balanced"
  418. _EOF_
  419.  
  420. for v in $(multilib_get_enabled_abi_pairs); do
  421. rust_target=$(rust_abi $(get_abi_CHOST ${v##*.}))
  422. arch_cflags="$(get_abi_CFLAGS ${v##*.})"
  423.  
  424. export CFLAGS_${rust_target//-/_}="${arch_cflags}"
  425.  
  426. cat <<- _EOF_ >> "${S}"/config.toml
  427. [target.${rust_target}]
  428. ar = "$(tc-getAR)"
  429. cc = "$(tc-getCC)"
  430. cxx = "$(tc-getCXX)"
  431. linker = "$(tc-getCC)"
  432. ranlib = "$(tc-getRANLIB)"
  433. llvm-libunwind = "$(usex llvm-libunwind $(usex system-llvm system in-tree) no)"
  434. _EOF_
  435. if use system-llvm; then
  436. cat <<- _EOF_ >> "${S}"/config.toml
  437. llvm-config = "$(get_llvm_prefix)/bin/llvm-config"
  438. _EOF_
  439. fi
  440. # by default librustc_target/spec/linux_musl_base.rs sets base.crt_static_default = true;
  441. # but we patch it and set to false here as well
  442. if use elibc_musl; then
  443. cat <<- _EOF_ >> "${S}"/config.toml
  444. crt-static = false
  445. _EOF_
  446. fi
  447. done
  448. if use wasm; then
  449. cat <<- _EOF_ >> "${S}"/config.toml
  450. [target.wasm32-unknown-unknown]
  451. linker = "$(usex system-llvm lld rust-lld)"
  452. # wasm target does not have profiler_builtins https://bugs.gentoo.org/848483
  453. profiler = false
  454. _EOF_
  455. fi
  456.  
  457. if [[ -n ${I_KNOW_WHAT_I_AM_DOING_CROSS} ]]; then # whitespace intentionally shifted below
  458. # experimental cross support
  459. # discussion: https://bugs.gentoo.org/679878
  460. # TODO: c*flags, clang, system-llvm, cargo.eclass target support
  461. # it would be much better if we could split out stdlib
  462. # complilation to separate ebuild and abuse CATEGORY to
  463. # just install to /usr/lib/rustlib/<target>
  464.  
  465. # extra targets defined as a bash array
  466. # spec format: <LLVM target>:<rust-target>:<CTARGET>
  467. # best place would be /etc/portage/env/dev-lang/rust
  468. # Example:
  469. # RUST_CROSS_TARGETS=(
  470. # "AArch64:aarch64-unknown-linux-gnu:aarch64-unknown-linux-gnu"
  471. # )
  472. # no extra hand holding is done, no target transformations, all
  473. # values are passed as-is with just basic checks, so it's up to user to supply correct values
  474. # valid rust targets can be obtained with
  475. # rustc --print target-list
  476. # matching cross toolchain has to be installed
  477. # matching LLVM_TARGET has to be enabled for both rust and llvm (if using system one)
  478. # only gcc toolchains installed with crossdev are checked for now.
  479.  
  480. # BUG: we can't pass host flags to cross compiler, so just filter for now
  481. # BUG: this should be more fine-grained.
  482. filter-flags '-mcpu=*' '-march=*' '-mtune=*'
  483.  
  484. local cross_target_spec
  485. for cross_target_spec in "${RUST_CROSS_TARGETS[@]}";do
  486. # extracts first element form <LLVM target>:<rust-target>:<CTARGET>
  487. local cross_llvm_target="${cross_target_spec%%:*}"
  488. # extracts toolchain triples, <rust-target>:<CTARGET>
  489. local cross_triples="${cross_target_spec#*:}"
  490. # extracts first element after before : separator
  491. local cross_rust_target="${cross_triples%%:*}"
  492. # extracts last element after : separator
  493. local cross_toolchain="${cross_triples##*:}"
  494. use llvm_targets_${cross_llvm_target} || die "need llvm_targets_${cross_llvm_target} target enabled"
  495. command -v ${cross_toolchain}-gcc > /dev/null 2>&1 || die "need ${cross_toolchain} cross toolchain"
  496.  
  497. cat <<- _EOF_ >> "${S}"/config.toml
  498. [target.${cross_rust_target}]
  499. ar = "${cross_toolchain}-ar"
  500. cc = "${cross_toolchain}-gcc"
  501. cxx = "${cross_toolchain}-g++"
  502. linker = "${cross_toolchain}-gcc"
  503. ranlib = "${cross_toolchain}-ranlib"
  504. _EOF_
  505. if use system-llvm; then
  506. cat <<- _EOF_ >> "${S}"/config.toml
  507. llvm-config = "$(get_llvm_prefix)/bin/llvm-config"
  508. _EOF_
  509. fi
  510. if [[ "${cross_toolchain}" == *-musl* ]]; then
  511. cat <<- _EOF_ >> "${S}"/config.toml
  512. musl-root = "$(${cross_toolchain}-gcc -print-sysroot)/usr"
  513. _EOF_
  514. fi
  515.  
  516. # append cross target to "normal" target list
  517. # example 'target = ["powerpc64le-unknown-linux-gnu"]'
  518. # becomes 'target = ["powerpc64le-unknown-linux-gnu","aarch64-unknown-linux-gnu"]'
  519.  
  520. rust_targets="${rust_targets},\"${cross_rust_target}\""
  521. sed -i "/^target = \[/ s#\[.*\]#\[${rust_targets}\]#" config.toml || die
  522.  
  523. ewarn
  524. ewarn "Enabled ${cross_rust_target} rust target"
  525. ewarn "Using ${cross_toolchain} cross toolchain"
  526. ewarn
  527. if ! has_version -b 'sys-devel/binutils[multitarget]' ; then
  528. ewarn "'sys-devel/binutils[multitarget]' is not installed"
  529. ewarn "'strip' will be unable to strip cross libraries"
  530. ewarn "cross targets will be installed with full debug information"
  531. ewarn "enable 'multitarget' USE flag for binutils to be able to strip object files"
  532. ewarn
  533. ewarn "Alternatively llvm-strip can be used, it supports stripping any target"
  534. ewarn "define STRIP=\"llvm-strip\" to use it (experimental)"
  535. ewarn
  536. fi
  537. done
  538. fi # I_KNOW_WHAT_I_AM_DOING_CROSS
  539.  
  540. einfo "Rust configured with the following flags:"
  541. echo
  542. echo RUSTFLAGS="\"${RUSTFLAGS}\""
  543. echo RUSTFLAGS_BOOTSTRAP="\"${RUSTFLAGS_BOOTSTRAP}\""
  544. echo RUSTFLAGS_NOT_BOOTSTRAP="\"${RUSTFLAGS_NOT_BOOTSTRAP}\""
  545. echo MAGIC_EXTRA_RUSTFLAGS="\"${MAGIC_EXTRA_RUSTFLAGS}\""
  546. env | grep "CARGO_TARGET_.*_RUSTFLAGS="
  547. env | grep "CFLAGS_.*"
  548. echo
  549. einfo "config.toml contents:"
  550. cat "${S}"/config.toml || die
  551. echo
  552. }
  553.  
  554. # Build a very minimal llvm that we can use for bootstrap rustc codegen
  555. llvm_bootstrap() {
  556. # Reference ${P}/src/bootstrap/native.rs for these values
  557. local llvm_cmake_opts=(
  558. "-G Ninja"
  559. "-DLLVM_TARGET_ARCH=${CFG_COMPILER_HOST_TRIPLE%%-*}"
  560. "-DLLVM_DEFAULT_TARGET_TRIPLE=${CFG_COMPILER_HOST_TRIPLE}"
  561. "-DLLVM_TARGETS_TO_BUILD=${BOOTSTRAP_LLVM_TARGETS:=X86;ARM;AArch64}" #;Mips;PowerPC;SystemZ;JSBackend;MSP430;Sparc;NVPTX
  562. "-DLLVM_ENABLE_ASSERTIONS=OFF"
  563. "-DLLVM_INCLUDE_EXAMPLES=OFF"
  564. "-DLLVM_INCLUDE_TESTS=OFF"
  565. "-DLLVM_INCLUDE_DOCS=OFF"
  566. "-DLLVM_INCLUDE_BENCHMARKS=OFF"
  567. "-DLLVM_ENABLE_ZLIB=OFF"
  568. "-DLLVM_ENABLE_TERMINFO=OFF"
  569. "-DLLVM_ENABLE_LIBEDIT=OFF"
  570. "-DCMAKE_CXX_COMPILER=$(tc-getCXX)"
  571. "-DCMAKE_C_COMPILER=$(tc-getCC)"
  572. "-DCMAKE_BUILD_TYPE=Release"
  573. )
  574.  
  575. if [[ -z "${LLVM_CMAKE_OPTS_EXTRA}" ]]; then
  576. llvm_cmake_opts+=( "${LLVM_CMAKE_OPTS_EXTRA}")
  577. fi
  578.  
  579. elog "Building bootstrap llvm ..."
  580.  
  581. mkdir -p "${WORKDIR}/bootstrap/llvm" || die
  582. pushd "${WORKDIR}/bootstrap/llvm" 2>/dev/null || die
  583. edo cmake ${llvm_cmake_opts[*]} "${S}/src/llvm-project/llvm"
  584. eninja || die "Failed to build bootstrap llvm"
  585. popd 2>/dev/null || die
  586. }
  587.  
  588. # High level steps:
  589. # Our system mrustc package has built stdlib for our current platform.
  590. # - Step 1: Use system-installed mrustc, (m)rust(c) stdlib, and minicargo to
  591. # bootstrap a `cargo` and `rustc` (mrustc-stage0)
  592. # - Step 2: Use minicargo and the built `rustc` to build a working `sysroot`
  593. # (includes `std`, `panic_unwind``, `test`, etc.) (mrustc-stage0)
  594. # - Step 3: Build build libs again (this time using `cargo` and `rustc`) (mrustc-stage1)
  595. # - Step 4: Build a `rustc` using those libs (mrustc-stage1)
  596. # - Done so there's an optimised rustc arollvm_cmake_optsund (mrustc is bad at codegen)
  597. # - Step 5: Build `libstd` with this `rustc` (mrustc-stage2)
  598. # - Needed to match ABIs
  599. # Stages:
  600. # - mrustc-stage0: mrustc-built cargo and rustc
  601. # - mrustc-stage1: rustc and sysroot built with mrustc-stage0
  602. # - mrustc-stage2: rustc from stage1 with sysroot built with stage0
  603. # See:
  604. # - https://github.com/thepowersgang/mrustc/blob/master/run_rustc/Makefile
  605. # - https://github.com/thepowersgang/mrustc/blob/master/TestRustcBootstrap.sh
  606. # - Upstream Windows .cmd files are also a good reference for early bootstrap
  607. mrustc_bootstrap() {
  608. export RUSTC_BOOTSTRAP=1 # Possibly the only intended use of this variable in ::gentoo
  609. # export these variables now and unset them at the end of the function so they don't leak
  610. # into the rest of the build.
  611. export CFG_COMPILER_HOST_TRIPLE="$(rust_abi)"
  612. export CFG_RELEASE="${MRUSTC_RUST_VERSION}" # Let's pretend we're 1.74.0
  613. export CFG_RELEASE_CHANNEL="stable"
  614. export CFG_VERSION="${MRUSTC_RUST_VERSION}-stable-mrustc"
  615. export CFG_PREFIX="mrustc"
  616. export CFG_LIBDIR_RELATIVE="lib"
  617. export RUSTC_INSTALL_BINDIR="bin"
  618. export REAL_LIBRARY_PATH_VAR="LD_LIBRARY_PATH"
  619.  
  620. # These flags are used in every invocation of our bootstrap `cargo`.
  621. local cargo_flags="--target ${CFG_COMPILER_HOST_TRIPLE} -j $(makeopts_jobs) --release --verbose"
  622.  
  623. if use system-llvm; then
  624. export LLVM_CONFIG="$(get_llvm_prefix)/bin/llvm-config"
  625. else
  626. llvm_bootstrap
  627. export LLVM_CONFIG="${WORKDIR}/bootstrap/llvm/bin/llvm-config"
  628. fi
  629.  
  630. # define the mrustc sysroot and common minicargo arguments.
  631. local mrustc_sysroot="${BROOT}/usr/lib/rust/mrustc-${MRUSTC_VERSION}/lib/rustlib/${CFG_COMPILER_HOST_TRIPLE}/lib"
  632. local minicargo_common_args=(
  633. "-L" "${mrustc_sysroot}"
  634. "-j" "$(makeopts_jobs)"
  635. "--vendor-dir" "${S}/vendor"
  636. "--manifest-overrides"
  637. "${BROOT}/usr/share/mrustc-${MRUSTC_VERSION}/patches/rustc-${MRUSTC_RUST_VERSION}-overrides.toml"
  638. )
  639. # There's a very good chance that minicargo and mrustc are not in the PATH.
  640. if ! command -v minicargo &> /dev/null; then
  641. export PATH="${BROOT}/usr/lib/rust/mrustc-${MRUSTC_VERSION}/bin:${PATH}"
  642. fi
  643. # Sanity check our bootstrap compiler & stdlib.
  644. elog "Sanity checking mrustc and stdlib ..."
  645. edo mrustc "${S}/tests/ui/hello_world/main.rs" -L "${mrustc_sysroot}" -o "${T}"/hello -g
  646. "${T}"/hello || die "Failed to run hello_world"
  647. # Seems fine, let's build some tools!
  648.  
  649. # Step 1: Build a `cargo` and `rustc` using system-installed mrustc
  650. # Anything we produce is going to be terribly unoptimised; mrustc does not do fantastic codegen.
  651. # It's good enough to bootstrap the "real" rustc though.
  652. elog "Building bootstrap cargo and rustc using mrustc and minicargo (mrustc-stage0) ..."
  653. local stage0="${WORKDIR}/bootstrap/mrustc-stage0"
  654. mkdir -p "${stage0}" || die
  655. edo minicargo "${S}"/src/tools/cargo --output-dir "${stage0}"/cargo-build ${minicargo_common_args[*]}
  656. "${stage0}"/cargo-build/cargo --version || die "Bootstrap cargo failed basic sanity check"
  657. edo minicargo "${S}"/compiler/rustc --output-dir "${stage0}"/rustc-build ${minicargo_common_args[*]} \
  658. --features llvm
  659. "${stage0}"/rustc-build/rustc_main --version || die "Bootstrap rustc failed basic sanity check"
  660. # minicargo has special-casing for `rustc` so we need to rename it.
  661. mv "${stage0}"/rustc-build/rustc_main "${stage0}"/rustc-build/rustc || die "Failed to rename rustc_main to rustc"
  662. # rustc wants these here
  663. mkdir -p "${stage0}"/codegen-backends || die
  664. mv "${stage0}"/rustc-build/librustc_codegen_llvm.* "${stage0}"/codegen-backends || die
  665.  
  666. # Step 2: use the bootstrapped rustc to build sysroot; we need to use `minicargo` for this -
  667. # mrustc does not accept all of the arguments that rustc does, even with the rustc_proxy wrapper.
  668. # `--script-overrides`: If the overrides are available, build scripts (and build-deps) are not built
  669. # which is good since we don't have a working compiler yet, and can't build them.
  670.  
  671. local stage0_sysroot_lib="${stage0}/lib/rustlib/${CFG_COMPILER_HOST_TRIPLE}/lib"
  672. # minicargo <= 0.11.2 doesn't create this directory and silently fails, besides it's better to be explicit, right?
  673. mkdir -p "${stage0_sysroot_lib}" || die "Failed to create stage0 directory"
  674.  
  675. elog "Building 'sysroot' using bootstrap rustc (mrustc-stage0) ..."
  676. edo env MRUSTC_PATH="${stage0}/rustc-build/rustc" minicargo -j $(makeopts_jobs) --vendor-dir "${S}"/vendor \
  677. --script-overrides "${BROOT}/usr/share/mrustc-0.11.2/script-overrides/stable-${MRUSTC_RUST_VERSION}-linux/" \
  678. --output-dir "${stage0_sysroot_lib}" "${S}"/library/sysroot ||
  679. die "Failed to build sysroot with bootstrap rust (mrustc-stage0)"
  680.  
  681. elog "Sanity checking sysroot and rustc ..."
  682. mkdir -p "${T}"/stage0-hello || die
  683. edo "${stage0}"/rustc-build/rustc -L "${stage0_sysroot_lib}" -g "${S}/tests/ui/hello_world/main.rs" \
  684. -o "${T}"/stage0-hello/hello
  685. "${T}"/stage0-hello/hello || die "Failed to run hello_world built with bootstrap rust stage0"
  686.  
  687. elog "mrustc bootstrap stage0 complete!"
  688.  
  689. # Step 3: Build a "proper" libstd, including dynamic libs using our bootstrap cargo and rustc.
  690. elog "Building 'sysroot' with the stage0 rustc (mrustc-stage1) ..."
  691. local stage1="${WORKDIR}/bootstrap/mrustc-stage1"
  692. local stage1_sysroot_lib="${stage1}/lib/rustlib/${CFG_COMPILER_HOST_TRIPLE}/lib"
  693. mkdir -p "${stage1_sysroot_lib}" || die "Failed to create stage1 directory"
  694. mkdir -p "${stage1}/bin" || die
  695.  
  696. # Simplified to avoid calling rustc_proxy; We don't need stage1 rustc until after this is built...
  697. edo env RUSTFLAGS="-Z force-unstable-if-unmarked" CARGO_TARGET_DIR="${stage1}/sysroot-build" \
  698. RUSTC="${stage0}/rustc-build/rustc" "${stage0}"/cargo-build/cargo build ${cargo_flags} \
  699. --manifest-path "${S}/library/sysroot/Cargo.toml" --features panic-unwind
  700.  
  701. # Move the built libs into the sysroot libdir.
  702. mv "${stage1}/sysroot-build/${CFG_COMPILER_HOST_TRIPLE}/release/deps"/*.{rlib,rmeta,so} \
  703. "${stage1_sysroot_lib}" || die "Failed to move stage1 libs to stage1 sysroot"
  704.  
  705. # We need to copy the stage0 rustc to the stage1 sysroot; this "updates" the sysroot location and enables
  706. # resolution of stage1 libs. (run `rustc --print sysroot` on stage0 and stage1 rustc to verify)
  707. cp "${stage0}/rustc-build/rustc" "${stage1}/bin/rustc" || die "Failed to copy rustc to stage1 sysroot"
  708.  
  709. # Step 4: Build `rustc` with itself, so we have a rustc with the right ABI.
  710. # This will be our final `rustc` for the bootstrap process.
  711. elog "Building rustc with stage1 libs (mrustc-stage1) ..."
  712. mkdir -p "${stage1}/rustc-build" || die
  713. edo env RUSTFLAGS="-Z force-unstable-if-unmarked -C link_args=-Wl,-rpath,\$ORIGIN/../lib" \
  714. LD_LIBRARY_PATH="${stage2_sysroot_lib}" CARGO_TARGET_DIR="${stage1}/rustc-build" \
  715. RUSTC="${stage1}/bin/rustc" TMPDIR="${T}" "${stage0}"/cargo-build/cargo build ${cargo_flags} \
  716. --manifest-path "${S}/compiler/rustc/Cargo.toml" --features llvm
  717.  
  718. # Step 5: Build `sysroot` with this `rustc` - Needed to match ABI
  719. # We need to use the previous sysroot; we could reuse that dir but it's easier to just copy it.
  720. elog "Building final 'sysroot' with the final rustc (mrustc-stage2) ..."
  721. local stage2="${WORKDIR}/bootstrap/mrustc-stage2"
  722. local stage2_sysroot_lib="${stage2}/lib/rustlib/${CFG_COMPILER_HOST_TRIPLE}/lib"
  723. mkdir -p "${stage2_sysroot_lib}" || die "Failed to create stage2 directory"
  724. mkdir -p "${stage2}/bin" || die
  725.  
  726. # Copy required files from stage1 to stage2 sysroot
  727. cp "${stage1}/rustc-build/${CFG_COMPILER_HOST_TRIPLE}"/release/rustc-main "${stage2}/bin/rustc_binary" ||
  728. die "Failed to copy final rustc to stage2 sysroot"
  729. cp "${stage1}/rustc-build/${CFG_COMPILER_HOST_TRIPLE}"/release/librustc_driver.so "${stage2}/lib" ||
  730. die "Failed to copy librustc_driver to sysroot"
  731. cp "${stage1}/rustc-build/${CFG_COMPILER_HOST_TRIPLE}"/release/deps/*.{rlib,so} "${stage2_sysroot_lib}" ||
  732. die "Failed to copy final rustc libs to stage2 sysroot"
  733. cp "${stage1_sysroot_lib}"/* "${stage2_sysroot_lib}" || die "Failed to copy stage1 so files to stage2 sysroot"
  734.  
  735. # There's a magic script used in place of rustc so that libs can be found
  736. cat <<- EOF > "${stage2}/bin/rustc" || die "Failed to create rustc wrapper"
  737. #!/bin/sh
  738. LD_LIBRARY_PATH="${stage2}/lib:${stage2_sysroot_lib}" ${stage2}/bin/rustc_binary "\$@"
  739. EOF
  740. chmod +x "${stage2}/bin/rustc" || die "Failed to make rustc wrapper executable"
  741.  
  742. # Use rustc to build 'sysroot'; this is the final step in the bootstrap process.
  743. # rpath probably isn't needed here, but it doesn't hurt.
  744. edo env RUSTFLAGS="-Z force-unstable-if-unmarked -C link_args=-Wl,-rpath,\$ORIGIN/../lib" \
  745. CARGO_TARGET_DIR="${stage2}/stdlib-build" RUSTC="${stage2}/bin/rustc" \
  746. "${stage0}"/cargo-build/cargo build ${cargo_flags} --manifest-path "${S}/library/sysroot/Cargo.toml" \
  747. --features panic-unwind
  748.  
  749. # Build our final output sysroot
  750. local output="${WORKDIR}/bootstrap/rust-${PV}"
  751. local output_sysroot_lib="${output}/lib/rustlib/${CFG_COMPILER_HOST_TRIPLE}/lib"
  752. mkdir -p "${output_sysroot_lib}" || die "Failed to create output directory"
  753. mkdir -p "${output}/bin" || die "Failed to create output directory"
  754.  
  755. # Copy our various output files into the output sysroot
  756. # rustc
  757. cp "${stage1}/rustc-build/${CFG_COMPILER_HOST_TRIPLE}"/release/rustc-main "${output}/bin/rustc_binary" ||
  758. die "Failed to copy final rustc to output"
  759. cp "${stage1}/rustc-build/${CFG_COMPILER_HOST_TRIPLE}"/release/librustc_driver.so "${output}/lib" ||
  760. die "Failed to copy librustc_driver to output"
  761. cp "${stage1}/rustc-build/${CFG_COMPILER_HOST_TRIPLE}"/release/deps/*.{rlib,so} "${output_sysroot_lib}" ||
  762. die "Failed to copy final rustc libs to output"
  763. # cargo; no need to build an optimised cargo if we're using this to build a complelety new Rust.
  764. cp "${stage0}/cargo-build/cargo" "${output}/bin/cargo" || die "Failed to copy cargo to output"
  765. # libs
  766. mv "${stage2}/stdlib-build/${CFG_COMPILER_HOST_TRIPLE}/release/deps"/*.{rlib,rmeta,so} "${output_sysroot_lib}" ||
  767. die "Failed to copy stage2 libs to output"
  768. # Our trusty rustc wrapper
  769. cat <<- EOF > "${output}/bin/rustc" || die "Failed to create rustc wrapper"
  770. #!/bin/sh
  771. LD_LIBRARY_PATH="${output}/lib:${output_sysroot_lib}" ${output}/bin/rustc_binary "\$@"
  772. EOF
  773. chmod +x "${output}/bin/rustc" || die "Failed to make rustc wrapper executable"
  774.  
  775. # Perform a sanity check on the final Rust.
  776. mkdir -p "${T}"/output-hello || die
  777. edo "${output}/bin/rustc" -L "${output_sysroot_lib}" -g "${S}/tests/ui/hello_world/main.rs" \
  778. -o "${T}"/output-hello/hello
  779. "${T}"/output-hello/hello || die "Failed to run hello_world built with bootstrapped Rust"
  780.  
  781. elog "Successfully bootstrapped Rust using mrustc!"
  782.  
  783. # Note: The Rust sysroot that we've produced is pretty close to what we'd expect from a normal Rust build.
  784. # If someone was so inclined they could build an optimised cargo using the stage2 rustc and sysroot,
  785. # and install the output directly. This is untested, as I'm sure there's more to it than that.
  786. # I'm satisfied with being able to build Rust normally at this point.
  787.  
  788. # Tidy up the Rust sources; revert mrustc changes so Rust can be built normally.
  789. pushd "${S}" 2>/dev/null || die
  790. patch -R -p0 < "${BROOT}"/usr/share/mrustc-${MRUSTC_VERSION}/patches/rustc-${MRUSTC_RUST_VERSION}-src.patch ||
  791. die "Failed to revert mrustc patches"
  792. popd 2>/dev/null || die
  793.  
  794. # Tidy up any environment variables we've set in the bootstrap process.
  795. unset CFG_COMPILER_HOST_TRIPLE CFG_RELEASE CFG_RELEASE_CHANNEL CFG_PREFIX CFG_VERSION
  796. unset CFG_LIBDIR_RELATIVE LLVM_CONFIG REAL_LIBRARY_PATH_VAR RUSTFLAGS RUSTC_BOOTSTRAP RUSTC_INSTALL_BINDIR
  797. }
  798.  
  799. src_compile() {
  800. use mrustc-bootstrap && mrustc_bootstrap
  801. RUST_BACKTRACE=1 "${EPYTHON}" ./x.py build -vvv --config="${S}"/config.toml -j$(makeopts_jobs) || die
  802. }
  803.  
  804. src_test() {
  805. # https://rustc-dev-guide.rust-lang.org/tests/intro.html
  806.  
  807. # those are basic and codegen tests.
  808. local tests=(
  809. codegen
  810. codegen-units
  811. compile-fail
  812. incremental
  813. mir-opt
  814. pretty
  815. run-make
  816. )
  817.  
  818. # fails if llvm is not built with ALL targets.
  819. # and known to fail with system llvm sometimes.
  820. use system-llvm || tests+=( assembly )
  821.  
  822. # fragile/expensive/less important tests
  823. # or tests that require extra builds
  824. # TODO: instead of skipping, just make some nonfatal.
  825. if [[ ${ERUST_RUN_EXTRA_TESTS:-no} != no ]]; then
  826. tests+=(
  827. rustdoc
  828. rustdoc-js
  829. rustdoc-js-std
  830. rustdoc-ui
  831. run-make-fulldeps
  832. ui
  833. ui-fulldeps
  834. )
  835. fi
  836.  
  837. local i failed=()
  838. einfo "rust_src_test: enabled tests ${tests[@]/#/src/test/}"
  839. for i in "${tests[@]}"; do
  840. local t="src/test/${i}"
  841. einfo "rust_src_test: running ${t}"
  842. if ! RUST_BACKTRACE=1 "${EPYTHON}" ./x.py test -vv --config="${S}"/config.toml \
  843. -j$(makeopts_jobs) --no-doc --no-fail-fast "${t}"
  844. then
  845. failed+=( "${t}" )
  846. eerror "rust_src_test: ${t} failed"
  847. fi
  848. done
  849.  
  850. if [[ ${#failed[@]} -ne 0 ]]; then
  851. eerror "rust_src_test: failure summary: ${failed[@]}"
  852. die "aborting due to test failures"
  853. fi
  854. }
  855.  
  856. src_install() {
  857. DESTDIR="${D}" "${EPYTHON}" ./x.py install -vv --config="${S}"/config.toml -j$(makeopts_jobs) || die
  858.  
  859. # bug #689562, #689160
  860. rm -v "${ED}/usr/lib/${PN}/${PV}/etc/bash_completion.d/cargo" || die
  861. rmdir -v "${ED}/usr/lib/${PN}/${PV}"/etc{/bash_completion.d,} || die
  862.  
  863. local symlinks=(
  864. cargo
  865. rustc
  866. rustdoc
  867. rust-gdb
  868. rust-gdbgui
  869. rust-lldb
  870. rust-demangler
  871. )
  872.  
  873. use clippy && symlinks+=( clippy-driver cargo-clippy )
  874. use miri && symlinks+=( miri cargo-miri )
  875. use rustfmt && symlinks+=( rustfmt cargo-fmt )
  876. use rust-analyzer && symlinks+=( rust-analyzer )
  877.  
  878. einfo "installing eselect-rust symlinks and paths: ${symlinks[@]}"
  879. local i
  880. for i in "${symlinks[@]}"; do
  881. # we need realpath on /usr/bin/* symlink return version-appended binary path.
  882. # so /usr/bin/rustc should point to /usr/lib/rust/<ver>/bin/rustc-<ver>
  883. # need to fix eselect-rust to remove this hack.
  884. local ver_i="${i}-${PV}"
  885. if [[ -f "${ED}/usr/lib/${PN}/${PV}/bin/${i}" ]]; then
  886. einfo "Installing ${i} symlink"
  887. ln -v "${ED}/usr/lib/${PN}/${PV}/bin/${i}" "${ED}/usr/lib/${PN}/${PV}/bin/${ver_i}" || die
  888. else
  889. ewarn "${i} symlink requested, but source file not found"
  890. ewarn "please report this"
  891. fi
  892. dosym "../lib/${PN}/${PV}/bin/${ver_i}" "/usr/bin/${ver_i}"
  893. done
  894.  
  895. # symlinks to switch components to active rust in eselect
  896. dosym "${PV}/lib" "/usr/lib/${PN}/lib-${PV}"
  897. use rust-analyzer && dosym "${PV}/libexec" "/usr/lib/${PN}/libexec-${PV}"
  898. dosym "${PV}/share/man" "/usr/lib/${PN}/man-${PV}"
  899. dosym "rust/${PV}/lib/rustlib" "/usr/lib/rustlib-${PV}"
  900. dosym "../../lib/${PN}/${PV}/share/doc/rust" "/usr/share/doc/${P}"
  901.  
  902. newenvd - "50${P}" <<-_EOF_
  903. MANPATH="${EPREFIX}/usr/lib/rust/man-${PV}"
  904. _EOF_
  905.  
  906. rm -rf "${ED}/usr/lib/${PN}/${PV}"/*.old || die
  907. rm -rf "${ED}/usr/lib/${PN}/${PV}/bin"/*.old || die
  908. rm -rf "${ED}/usr/lib/${PN}/${PV}/doc"/*.old || die
  909.  
  910. # note: eselect-rust adds EROOT to all paths below
  911. cat <<-_EOF_ > "${T}/provider-${P}"
  912. /usr/bin/cargo
  913. /usr/bin/rustdoc
  914. /usr/bin/rust-demangler
  915. /usr/bin/rust-gdb
  916. /usr/bin/rust-gdbgui
  917. /usr/bin/rust-lldb
  918. /usr/lib/rustlib
  919. /usr/lib/rust/lib
  920. /usr/lib/rust/man
  921. /usr/share/doc/rust
  922. _EOF_
  923.  
  924. if use clippy; then
  925. echo /usr/bin/clippy-driver >> "${T}/provider-${P}"
  926. echo /usr/bin/cargo-clippy >> "${T}/provider-${P}"
  927. fi
  928. if use miri; then
  929. echo /usr/bin/miri >> "${T}/provider-${P}"
  930. echo /usr/bin/cargo-miri >> "${T}/provider-${P}"
  931. fi
  932. if use rustfmt; then
  933. echo /usr/bin/rustfmt >> "${T}/provider-${P}"
  934. echo /usr/bin/cargo-fmt >> "${T}/provider-${P}"
  935. fi
  936. if use rust-analyzer; then
  937. echo /usr/lib/rust/libexec >> "${T}/provider-${P}"
  938. echo /usr/bin/rust-analyzer >> "${T}/provider-${P}"
  939. fi
  940. insinto /etc/env.d/rust
  941. doins "${T}/provider-${P}"
  942.  
  943. if use dist; then
  944. insinto "/usr/lib/${PN}/${PV}/dist"
  945. doins -r "${S}/build/dist/."
  946. fi
  947. }
  948.  
  949. pkg_preinst() {
  950. # 943308 and friends; basically --keep-going can forget to unmerge old rust
  951. # but the soft blocker allows us to install conflicting files.
  952. # This results in duplicated .{rlib,so} files which confuses rustc and results in
  953. # the need for manual intervention.
  954. if has_version -b "dev-lang/rust:stable/$(ver_cut 1-2)"; then
  955. # we need to find all .{rlib,so} files in the old rust lib directory
  956. # and store them in an array for later use
  957. readarray -d '' old_rust_libs < <(
  958. find "${EROOT}/usr/lib/rust/${PV}/lib/rustlib" \
  959. -type f \( -name '*.rlib' -o -name '*.so' \) -print0)
  960. export old_rust_libs
  961. if [[ ${#old_rust_libs[@]} -gt 0 ]]; then
  962. einfo "Found old .rlib and .so files in the old rust lib directory"
  963. else
  964. die "Found no old .rlib and .so files but old rust version is installed. Bailing!"
  965. fi
  966. fi
  967. }
  968.  
  969. pkg_postinst() {
  970.  
  971. local old_rust="dev-lang/rust:stable/$(ver_cut 1-2)"
  972. if has_version -b ${old_rust}; then
  973. # Be _extra_ careful here as we're removing files from the live filesystem
  974. local f
  975. local only_one_file=()
  976. einfo "Tidying up libraries files from non-slotted \`${old_rust}\`."
  977. for f in "${old_rust_libs[@]}"; do
  978. [[ -f ${f} ]] || die "old_rust_libs array contains non-existent file"
  979. local base_name="${f%-*}"
  980. local ext="${f##*.}"
  981. local matching_files=("${base_name}"-*.${ext})
  982. case ${#matching_files[@]} in
  983. 2)
  984. einfo "Removing old .${ext}: ${f}"
  985. rm "${f}" || die
  986. ;;
  987. 1)
  988. # Turns out fingerprints are not as unique as we'd thought, _sometimes_ they collide,
  989. # so we may have already installed over the old file.
  990. # We'll warn about this just in case, but it's probably fine.
  991. only_one_file+=( "${matching_files[0]}" )
  992. ;;
  993. *)
  994. die "Expected one or two files matching ${base_name}-\*.rlib, but found ${#matching_files[@]}"
  995. ;;
  996. esac
  997. done
  998. if [[ ${#only_one_file} -gt 0 ]]; then
  999. einfo "While tidying up non-slotted rust libraries for \`${old_rust}\`,"
  1000. einfo "the following file(s) did not have a duplicate where one was expected:"
  1001. for f in "${only_one_file[@]}"; do
  1002. einfo " * ${f}"
  1003. done
  1004. einfo ""
  1005. einfo "This is unlikely to cause problems; the fingerprint for the library ended up being the same."
  1006. einfo "However, if you encounter any issues please report them to the Gentoo Rust Team."
  1007. fi
  1008. fi
  1009.  
  1010. eselect rust update
  1011.  
  1012. if has_version dev-debug/gdb || has_version llvm-core/lldb; then
  1013. elog "Rust installs helper scripts for calling GDB and LLDB,"
  1014. elog "for convenience they are installed under /usr/bin/rust-{gdb,lldb}-${PV}."
  1015. fi
  1016.  
  1017. optfeature "Emacs support" "app-emacs/rust-mode"
  1018. optfeature "Vim support" "app-vim/rust-vim"
  1019. }
  1020.  
  1021. pkg_postrm() {
  1022. eselect rust cleanup
  1023. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement