Diff rust-1.90.0-r1 with a rust-1.92.0_p1-r1

/usr/portage/dev-lang/rust/rust-1.92.0_p1-r1.ebuild 2026-02-19 11:18:07.354647916 +0300
3 3

  
4 4
EAPI=8
5 5

  
6
LLVM_COMPAT=( 20 )
6
# Bump notes: https://wiki.gentoo.org/wiki/Project:Rust/Rust_bump
7

  
8
LLVM_COMPAT=( 21 )
7 9
PYTHON_COMPAT=( python3_{11..14} )
8 10

  
9
MRUSTC_VERSION="0.12.0"
10
MRUSTC_RUST_VERSION="1.90.0"
11
RUST_OPTIONAL=1
12

  
13
# We bumped the rust-patches tag without bumping the revision for
14
# https://bugs.gentoo.org/963657, given that ithe patch affects only a
15
# specific use case (bootstrap with Rust 1.90.0).
16
RUST_PATCH_VER=${PV}_p1
11
# Patches are kept in rust-patches.git, see its README.rst for the versioning
12
# scheme.
13
#
14
# We use _pN from the ebuild version for the patchset but it can be overridden
15
# in the ebuild for changes that don't require a revbump.
16
#
17
# Uncomment this line when the ebuild needs a patchset update but no revbump.
18
#RUST_PATCH_VER=${PV}-1
17 19

  
18 20
RUST_MAX_VER=${PV%%_*}
21
RUST_PV=${PV%%_p*}
22
RUST_P=${PN}-${RUST_PV}
23
[[ -z ${RUST_PATCH_VER} ]] && RUST_PATCH_VER=${PV}
24

  
19 25
if [[ ${PV} == *9999* ]]; then
20
	RUST_MIN_VER="1.88.0" # Update this as new `beta` releases come out.
26
	# Update this as new `beta` releases come out.
27
	RUST_MIN_VER="1.88.0"
21 28
elif [[ ${PV} == *beta* ]]; then
22 29
	RUST_MAX_VER="$(ver_cut 1).$(ver_cut 2).0"
23 30
	RUST_MIN_VER="$(ver_cut 1).$(($(ver_cut 2) - 1)).0"
......
25 32
	RUST_MIN_VER="$(ver_cut 1).$(($(ver_cut 2) - 1)).0"
26 33
fi
27 34

  
28
inherit check-reqs estack edo flag-o-matic llvm-r1 multiprocessing optfeature \
29
	multilib multilib-build python-any-r1 rust rust-toolchain toolchain-funcs verify-sig
35
inherit check-reqs estack flag-o-matic llvm-r1 multiprocessing optfeature
36
inherit multilib multilib-build python-any-r1 rust rust-toolchain toolchain-funcs
37
inherit verify-sig
30 38

  
31 39
if [[ ${PV} = *9999* ]]; then
32 40
	inherit git-r3
......
36 44
	betaver=${PV//*beta}
37 45
	BETA_SNAPSHOT="${betaver:0:4}-${betaver:4:2}-${betaver:6:2}"
38 46
	MY_P="rustc-beta"
39
	SRC_URI="https://static.rust-lang.org/dist/${BETA_SNAPSHOT}/rustc-beta-src.tar.xz -> rustc-${PV}-src.tar.xz
47
	SRC_URI="
48
		https://static.rust-lang.org/dist/${BETA_SNAPSHOT}/rustc-beta-src.tar.xz -> rustc-${RUST_PV}-src.tar.xz
40 49
		https://gitweb.gentoo.org/proj/rust-patches.git/snapshot/rust-patches-${RUST_PATCH_VER}.tar.bz2
41
		verify-sig? ( https://static.rust-lang.org/dist/${BETA_SNAPSHOT}/rustc-beta-src.tar.xz.asc
42
			-> rustc-${PV}-src.tar.xz.asc )
50
		verify-sig? (
51
			https://static.rust-lang.org/dist/${BETA_SNAPSHOT}/rustc-beta-src.tar.xz.asc
52
				-> rustc-${RUST_PV}-src.tar.xz.asc
53
		)
43 54
	"
44 55
	S="${WORKDIR}/${MY_P}-src"
45 56
else
46
	MY_P="rustc-${PV}"
47
	SRC_URI="https://static.rust-lang.org/dist/${MY_P}-src.tar.xz
57
	MY_P="rustc-${RUST_PV}"
58
	SRC_URI="
59
		https://static.rust-lang.org/dist/${MY_P}-src.tar.xz
48 60
		https://gitweb.gentoo.org/proj/rust-patches.git/snapshot/rust-patches-${RUST_PATCH_VER}.tar.bz2
49 61
		verify-sig? ( https://static.rust-lang.org/dist/${MY_P}-src.tar.xz.asc )
50 62
	"
51 63
	S="${WORKDIR}/${MY_P}-src"
64

  
52 65
	KEYWORDS="amd64 arm arm64 ~loong ~mips ppc ppc64 ~riscv ~sparc x86"
53 66
fi
54 67

  
......
56 69
HOMEPAGE="https://www.rust-lang.org/"
57 70

  
58 71
# keep in sync with llvm ebuild of the same version as bundled one.
59
ALL_LLVM_TARGETS=( AArch64 AMDGPU ARC ARM AVR BPF CSKY DirectX Hexagon Lanai
60
	LoongArch M68k Mips MSP430 NVPTX PowerPC RISCV Sparc SPIRV SystemZ VE
61
	WebAssembly X86 XCore Xtensa )
72
ALL_LLVM_TARGETS=( AArch64 AMDGPU ARC ARM AVR BPF CSKY DirectX Hexagon Lanai )
73
ALL_LLVM_TARGETS+=( LoongArch M68k Mips MSP430 NVPTX PowerPC RISCV Sparc SPIRV )
74
ALL_LLVM_TARGETS+=( SystemZ VE WebAssembly X86 XCore Xtensa )
62 75
ALL_LLVM_TARGETS=( "${ALL_LLVM_TARGETS[@]/#/llvm_targets_}" )
63 76
LLVM_TARGET_USEDEPS=${ALL_LLVM_TARGETS[@]/%/(-)?}
64 77

  
......
77 90
LICENSE="|| ( MIT Apache-2.0 ) BSD BSD-1 BSD-2 BSD-4"
78 91
SLOT="${PV%%_*}" # Beta releases get to share the same SLOT as the eventual stable
79 92

  
80
IUSE="big-endian clippy cpu_flags_x86_sse2 debug dist doc llvm-libunwind lto mrustc-bootstrap"
81
IUSE+=" rustfmt rust-analyzer rust-src +system-llvm test ${ALL_LLVM_TARGETS[*]} ${ALL_RUST_SYSROOTS[*]}"
93
IUSE="big-endian clippy cpu_flags_x86_sse2 debug dist doc llvm-libunwind lto"
94
IUSE+=" rustfmt rust-analyzer rust-src +system-llvm test"
95
IUSE+=" ${ALL_LLVM_TARGETS[*]} ${ALL_RUST_SYSROOTS[*]}"
82 96

  
83 97
if [[ ${PV} = *9999* ]]; then
84 98
	# These USE flags require nightly rust
......
97 111
LLVM_DEPEND+=( "	$(llvm_gen_dep 'llvm-core/llvm:${LLVM_SLOT}')" )
98 112

  
99 113
# dev-libs/oniguruma is used for documentation
100
BDEPEND="${PYTHON_DEPS}
114
BDEPEND="
115
	${PYTHON_DEPS}
101 116
	app-eselect/eselect-rust
102 117
	dev-libs/oniguruma
103 118
	|| (
......
110 125
			sys-devel/mold
111 126
		)
112 127
	) )
128
	rust_sysroots_wasm? ( llvm-core/clang )
113 129
	!system-llvm? (
114 130
		>=dev-build/cmake-3.13.4
115 131
		app-alternatives/ninja
116 132
	)
117 133
	test? ( dev-debug/gdb )
118 134
	verify-sig? ( sec-keys/openpgp-keys-rust )
119
	mrustc-bootstrap? (
120
		~dev-lang/mrustc-${MRUSTC_VERSION}
121
		dev-build/cmake
122
		sys-devel/gcc:*
123
	)
124
	!mrustc-bootstrap? ( ${RUST_DEPEND} )
125 135
"
126 136

  
127 137
DEPEND="
......
140 150
	)
141 151
"
142 152

  
143
RDEPEND="${DEPEND}
153
RDEPEND="
154
	${DEPEND}
144 155
	app-eselect/eselect-rust
145 156
	dev-lang/rust-common
146 157
	sys-apps/lsb-release
......
148 159
	!dev-lang/rust-bin:stable
149 160
"
150 161

  
151
REQUIRED_USE="|| ( ${ALL_LLVM_TARGETS[*]} )
162
REQUIRED_USE="
163
	|| ( ${ALL_LLVM_TARGETS[*]} )
152 164
	rust-analyzer? ( rust-src )
153 165
	test? ( ${ALL_LLVM_TARGETS[*]} )
154 166
	rust_sysroots_bpf? ( llvm_targets_BPF )
......
257 269
			die "Must enable LLVM_TARGETS=${cross_llvm_target} matching CBUILD=${CBUILD} when cross-compiling"
258 270
	fi
259 271

  
260
	if use mrustc-bootstrap; then
261
		if ! tc-is-gcc; then
262
			# USE="mrustc-bootstrap" reqires that the build environment use GCC
263
			export CC=${CHOST}-gcc
264
			export CXX=${CHOST}-g++
265
			tc-is-gcc || die "tc-is-gcc failed in spite of CC=${CC}"
266
		fi
267
	else
268
		rust_pkg_setup
269
	fi
272
	rust_pkg_setup
270 273

  
271 274
	if use system-llvm; then
272 275
		llvm-r1_pkg_setup
......
337 340
		_EOF_
338 341
	elif use verify-sig ; then
339 342
		# Patch tarballs are not signed (but we trust Gentoo infra)
340
		verify-sig_verify_detached "${DISTDIR}"/rustc-${PV}-src.tar.xz{,.asc}
343
		verify-sig_verify_detached "${DISTDIR}"/rustc-${RUST_PV}-src.tar.xz{,.asc}
341 344
		default
342 345
	else
343 346
		default
......
345 348
}
346 349

  
347 350
src_prepare() {
348
	if [[ ${PV} = *9999* ]]; then
349
		# We need to update / generate lockfiles for the workspace
350
		${CARGO} generate-lockfile --offline || die "Failed to generate lockfiles"
351
	fi
352

  
353 351
	# Commit patches to the appropriate branch in proj/rust-patches.git
354 352
	# then cut a new tag / tarball. Don't add patches to ${FILESDIR}
355 353
	PATCHES=(
356 354
		"${WORKDIR}/rust-patches-${RUST_PATCH_VER}/"
357 355
	)
358
	# Apply patches for bootstrapping with a particular Rust version (RUST_SLOT).
359
	if [[ -n ${RUST_SLOT} ]]; then # Not set set if using mrustc-bootstrap
360
		local bootstrap_patchdir="${WORKDIR}/rust-patches-${RUST_PATCH_VER}/${RUST_SLOT}"
361
		if [[ -d "${bootstrap_patchdir}" ]]; then
362
			PATCHES+=(
363
				"${bootstrap_patchdir}"
364
			)
365
		fi
366
	fi
367 356

  
368 357
	if use lto && tc-is-clang && ! tc-ld-is-lld && ! tc-ld-is-mold; then
369 358
		export RUSTFLAGS+=" -C link-arg=-fuse-ld=lld"
370 359
	fi
371 360

  
372 361
	default
373

  
374
	if use mrustc-bootstrap; then
375
		pushd "${S}" 2>/dev/null || die
376
		einfo "Applying patches to enable bootstrap with mrustc ${MRUSTC_VERSION}"
377
		patch -p0 < "${BROOT}"/usr/share/mrustc-${MRUSTC_VERSION}/patches/rustc-${MRUSTC_RUST_VERSION}-src.patch ||
378
			die "Failed to patch sources to enable bootstrap with mrustc"
379
		popd 2>/dev/null || die
380
	fi
381 362
}
382 363

  
383 364
src_configure() {
......
426 407
		use miri && tools+=',"miri"'
427 408
	fi
428 409

  
429
	if use mrustc-bootstrap; then
430
		local rust_stage0_root="${WORKDIR}/bootstrap/rust-${PV}"
431
	else
432
		local rust_stage0_root="$(${RUSTC} --print sysroot || die "Can't determine rust's sysroot")"
433
		# in case of prefix it will be already prefixed, as --print sysroot returns full path
434
		[[ -d ${rust_stage0_root} ]] || die "${rust_stage0_root} is not a directory"
435
	fi
410
	local rust_stage0_root="$(${RUSTC} --print sysroot || die "Can't determine rust's sysroot")"
411
	# in case of prefix it will be already prefixed, as --print sysroot returns full path
412
	[[ -d ${rust_stage0_root} ]] || die "${rust_stage0_root} is not a directory"
436 413

  
437 414
	rust_target="$(rust_abi)"
438 415
	rust_build="$(rust_abi "${CBUILD}")"
......
460 437
			build_channel="stable"
461 438
			;;
462 439
	esac
440

  
441
	# TODO: Add optimized-compiler-builtins for system-llvm to avoid
442
	# building bundled compiler-rt.
463 443
	cat <<- _EOF_ > "${S}"/bootstrap.toml
464 444
		# Suppresses a warning about tracking changes which we don't care about.
465 445
		change-id = "ignore"
......
608 588
	done
609 589
	if use rust_sysroots_wasm; then
610 590
		wasm_target="wasm32-unknown-unknown"
611
		export CFLAGS_${wasm_target//-/_}="$(filter-flags '-mcpu*' '-march*' '-mtune*'; echo "$CFLAGS")"
591
		if tc-is-clang; then
592
			local wasm_cc=$(tc-getCC)
593
			local wasm_cxx=$(tc-getCXX)
594
		else
595
			local wasm_cc=${CHOST}-clang
596
			local wasm_cxx=${CHOST}-clang++
597
		fi
598
		export CFLAGS_${wasm_target//-/_}="$(
599
			CC="${wasm_cc} --target=wasm32-unknown-unknown"
600
			filter-flags '-mcpu*' '-march*' '-mtune*'
601
			strip-unsupported-flags
602
			echo "${CFLAGS}"
603
		)"
612 604
		cat <<- _EOF_ >> "${S}"/bootstrap.toml
613 605
			[target.wasm32-unknown-unknown]
606
			cc = "${wasm_cc}"
607
			cxx = "${wasm_cxx}"
614 608
			linker = "$(usex system-llvm lld rust-lld)"
615 609
			# wasm target does not have profiler_builtins https://bugs.gentoo.org/848483
616 610
			profiler = false
......
720 714
	echo
721 715
}
722 716

  
723
llvm_bootstrap() {
724
	# Reference ${P}/src/bootstrap/native.rs for these values
725
	local llvm_cmake_opts=(
726
		"-G Ninja"
727
		"-DLLVM_TARGET_ARCH=${CFG_COMPILER_HOST_TRIPLE%%-*}"
728
		"-DLLVM_DEFAULT_TARGET_TRIPLE=${CFG_COMPILER_HOST_TRIPLE}"
729
		#;Mips;PowerPC;SystemZ;JSBackend;MSP430;Sparc;NVPTX
730
		"-DLLVM_TARGETS_TO_BUILD=${BOOTSTRAP_LLVM_TARGETS:=X86;ARM;AArch64}"
731
		"-DLLVM_ENABLE_ASSERTIONS=OFF"
732
		"-DLLVM_INCLUDE_EXAMPLES=OFF"
733
		"-DLLVM_INCLUDE_TESTS=OFF"
734
		"-DLLVM_INCLUDE_DOCS=OFF"
735
		"-DLLVM_INCLUDE_BENCHMARKS=OFF"
736
		"-DLLVM_ENABLE_ZLIB=OFF"
737
		"-DLLVM_ENABLE_TERMINFO=OFF"
738
		"-DLLVM_ENABLE_LIBEDIT=OFF"
739
		"-DCMAKE_CXX_COMPILER=$(tc-getCXX)"
740
		"-DCMAKE_C_COMPILER=$(tc-getCC)"
741
		"-DCMAKE_BUILD_TYPE=Release"
742
	)
743

  
744
	if [[ -z "${LLVM_CMAKE_OPTS_EXTRA}" ]]; then
745
		llvm_cmake_opts+=( "${LLVM_CMAKE_OPTS_EXTRA}")
746
	fi
747

  
748
	elog "Building bootstrap llvm ..."
749

  
750
	mkdir -p "${WORKDIR}/bootstrap/llvm" || die
751
	pushd "${WORKDIR}/bootstrap/llvm" 2>/dev/null || die
752
		edo cmake ${llvm_cmake_opts[*]} "${S}/src/llvm-project/llvm"
753
		eninja || die "Failed to build bootstrap llvm"
754
	popd 2>/dev/null || die
755
}
756

  
757
# High level steps:
758
# Our system mrustc package has built stdlib for our current platform.
759
# - Step 1: Use system-installed mrustc, (m)rust(c) stdlib, and minicargo to
760
#	bootstrap a `cargo` and `rustc` (mrustc-stage0)
761
# - Step 2: Use minicargo and the built `rustc` to build a working `sysroot`
762
#			(includes `std`, `panic_unwind``, `test`, etc.) (mrustc-stage0)
763
# - Step 3: Build build libs again (this time using `cargo` and `rustc`) (mrustc-stage1)
764
# - Step 4: Build a `rustc` using those libs (mrustc-stage1)
765
#  - Done so there's an optimised rustc around (mrustc is bad at codegen)
766
# - Step 5: Build `libstd` with this `rustc` (mrustc-stage2)
767
#  - Needed to match ABIs
768
# Stages:
769
# - mrustc-stage0: mrustc-built cargo and rustc
770
# - mrustc-stage1: rustc and sysroot built with mrustc-stage0
771
# - mrustc-stage2: rustc from stage1 with sysroot built with stage0
772
# See:
773
# - https://github.com/thepowersgang/mrustc/blob/master/run_rustc/Makefile
774
# - https://github.com/thepowersgang/mrustc/blob/master/TestRustcBootstrap.sh
775
# - Upstream Windows .cmd files are also a good reference for early bootstrap
776
mrustc_bootstrap() {
777
	export RUSTC_BOOTSTRAP=1 # Possibly the only intended use of this variable in ::gentoo
778
	# export these variables now and unset them at the end of the function so they don't leak
779
	# into the rest of the build.
780
	export CFG_COMPILER_HOST_TRIPLE="$(rust_abi)"
781
	export CFG_RELEASE="${MRUSTC_RUST_VERSION}"	# Let's pretend we're 1.90.0
782
	export CFG_RELEASE_CHANNEL="stable"
783
	export CFG_VERSION="${MRUSTC_RUST_VERSION}-stable-mrustc"
784
	export CFG_PREFIX="mrustc"
785
	export CFG_LIBDIR_RELATIVE="lib"
786
	# We actually patch our executable, but this suppresses a warning on every invocation of the bootstrap compiler.
787
	export MRUSTC_TARGET_VER="${MRUSTC_RUST_VERSION%.*}"
788
	export RUSTC_INSTALL_BINDIR="bin"
789
	export REAL_LIBRARY_PATH_VAR="LD_LIBRARY_PATH"
790
	export STD_ENV_ARCH="${CFG_COMPILER_HOST_TRIPLE%%-*}"
791

  
792
	# These flags are used in every invocation of our bootstrap `cargo`.
793
	local cargo_flags="--target ${CFG_COMPILER_HOST_TRIPLE} -j $(makeopts_jobs) --release --verbose"
794

  
795
	# for bootstrap, let's using the built-in stdlib of compiler (could be the bundled one)
796
	filter-flags '-stdlib=*'
797

  
798
	# mrustc requires gcc, so disable libcxx to avoid linker failure on w/o '-lstdc++'
799
	[[ "${LLVM_USE_LIBCXX}" == "1" ]] && unset LLVM_USE_LIBCXX
800

  
801
	local llvm_config_wrapper_cxxflags=0
802

  
803
	if use system-llvm; then
804
		export LLVM_CONFIG="$(get_llvm_prefix)/bin/llvm-config"
805

  
806
		local llvm_config_cxxflags=$(${LLVM_CONFIG} --cxxflags)
807
		elog "Checking llvm-config --cxxflags: '${llvm_config_cxxflags}'"
808
		[[ "${llvm_config_cxxflags}" =~ (^|[[:space:]])-stdlib=libc\+\+([[:space:]]|$) ]] && {
809
			elog "Found LLVM CXXFLAGS has \"--stdlib=libc++\""
810
			llvm_config_wrapper_cxxflags=1
811
		}
812
	else
813
		llvm_bootstrap
814
		export LLVM_CONFIG="${WORKDIR}/bootstrap/llvm/bin/llvm-config"
815
	fi
816

  
817
	elog "LLVM_CONFIG before wrappers: ${LLVM_CONFIG}"
818

  
819
	# workaround for gcc bug 122409 on musl by wrapping llvm-config
820
	# to append libc++ header if has "-stdlib=libc++"
821
	elog "Preparing wrapper of llvm-config (${WORKDIR}/llvm-config)"
822
	cat > ${WORKDIR}/llvm-config <<-EOF || die
823
	#!/bin/bash
824

  
825
	RULES=()
826
	for flag in "\$@"; do
827
	    case "\${flag}" in
828
	$([[ "${llvm_config_wrapper_cxxflags}" == 1 ]] && {
829
		echo "        --cxxflags) RULES+=( \"-E\" \"s@(^|[[:space:]]+)(-stdlib=libc\\+\\+)(\\$|[[:space:]])@\\1-I${EPREFIX}/usr/include/c++/v1 \\2\\3@g\" ) ;;"
830
	})
831
	        *)
832
	            ;;
833
	    esac
834
	done
835

  
836
	[[ -z "\${RULES}" ]] && {
837
	    ${LLVM_CONFIG} "\$@"
838
	} || {
839
	    ${LLVM_CONFIG} "\$@" | \\
840
	        tee -a ${T}/llvm-config.0.log | \\
841
	        sed "\${RULES[@]}" | \\
842
	        tee -a ${T}/llvm-config.1.log
843
	    exit \${PIPESTATUS[0]}
844
	}
845
	EOF
846
	export LLVM_CONFIG="${WORKDIR}/llvm-config"
847
	chmod +x ${WORKDIR}/llvm-config || die
848

  
849
	einfo "llvm-config wrapper contents:"
850
	cat "${LLVM_CONFIG}" || die
851
	echo
852

  
853
	# define the mrustc sysroot and common minicargo arguments.
854
	local mrustc_sysroot="${BROOT}/usr/lib/rust/mrustc-${MRUSTC_VERSION}/lib/rustlib/${CFG_COMPILER_HOST_TRIPLE}/lib"
855
	local minicargo_common_args=(
856
		"-L" "${mrustc_sysroot}"
857
		"-j" "$(makeopts_jobs)"
858
		"--vendor-dir" "${S}/vendor"
859
		"--manifest-overrides"
860
		"${BROOT}/usr/share/mrustc-${MRUSTC_VERSION}/patches/rustc-${MRUSTC_RUST_VERSION}-overrides.toml"
861
	)
862
	# There's a very good chance that minicargo and mrustc are not in the PATH.
863
	if ! command -v minicargo &> /dev/null; then
864
		export PATH="${BROOT}/usr/lib/rust/mrustc-${MRUSTC_VERSION}/bin:${PATH}"
865
	fi
866
	# Sanity check our bootstrap compiler & stdlib.
867
	elog "Sanity checking mrustc and stdlib ..."
868
	edo mrustc "${S}/tests/ui/parallel-rustc/hello_world.rs" -L "${mrustc_sysroot}" -o "${T}"/hello -g
869
	"${T}"/hello || die "Failed to run hello_world"
870
	# Seems fine, let's build some tools!
871

  
872
	# Step 1: Build a `cargo` and `rustc` using system-installed mrustc
873
	# Anything we produce is going to be terribly unoptimised; mrustc does not do fantastic codegen.
874
	# It's good enough to bootstrap the "real" rustc though.
875
	elog "Building bootstrap cargo and rustc using mrustc and minicargo (mrustc-stage0) ..."
876
	local stage0="${WORKDIR}/bootstrap/mrustc-stage0"
877
	mkdir -p "${stage0}" || die
878
	edo minicargo "${S}"/src/tools/cargo --output-dir "${stage0}"/cargo-build ${minicargo_common_args[*]}
879
	"${stage0}"/cargo-build/cargo --version || die "Bootstrap cargo failed basic sanity check"
880
	edo minicargo "${S}"/compiler/rustc --output-dir "${stage0}"/rustc-build ${minicargo_common_args[*]} \
881
		--features llvm
882
	"${stage0}"/rustc-build/rustc_main --version || die "Bootstrap rustc failed basic sanity check"
883
	# minicargo has special-casing for `rustc` so we need to rename it.
884
	mv "${stage0}"/rustc-build/rustc_main "${stage0}"/rustc-build/rustc || die "Failed to rename rustc_main to rustc"
885
	# rustc wants these here
886
	mkdir -p "${stage0}"/codegen-backends || die
887
	mv "${stage0}"/rustc-build/librustc_codegen_llvm.* "${stage0}"/codegen-backends || die
888

  
889
	# Step 2: use the bootstrapped rustc to build sysroot; we need to use `minicargo` for this -
890
	# mrustc does not accept all of the arguments that rustc does, even with the rustc_proxy wrapper.
891
	# `--script-overrides`:  If the overrides are available, build scripts (and build-deps) are not built
892
	# which is good since we don't have a working compiler yet, and can't build them.
893

  
894
	local stage0_sysroot_lib="${stage0}/lib/rustlib/${CFG_COMPILER_HOST_TRIPLE}/lib"
895
	# minicargo <= 0.12.0 doesn't create this directory and silently fails, besides it's better to be explicit, right?
896
	mkdir -p "${stage0_sysroot_lib}" || die "Failed to create stage0 directory"
897

  
898
	elog "Building 'sysroot' using bootstrap rustc (mrustc-stage0) ..."
899
	local o_dir="${BROOT}/usr/share/mrustc-${MRUSTC_VERSION}/script-overrides/stable-${MRUSTC_RUST_VERSION}-linux/"
900
	edo env MRUSTC_PATH="${stage0}/rustc-build/rustc" minicargo -j $(makeopts_jobs) --vendor-dir "${S}"/vendor \
901
		--script-overrides  "${o_dir}" \
902
		--output-dir "${stage0_sysroot_lib}" "${S}"/library/sysroot ||
903
			die "Failed to build sysroot with bootstrap rust (mrustc-stage0)"
904

  
905
	elog "Sanity checking sysroot and rustc ..."
906
	mkdir -p "${T}"/stage0-hello || die
907
	edo "${stage0}"/rustc-build/rustc -L "${stage0_sysroot_lib}" -g "${S}/tests/ui/parallel-rustc/hello_world.rs" \
908
		-o "${T}"/stage0-hello/hello
909
	"${T}"/stage0-hello/hello || die "Failed to run hello_world built with bootstrap rust stage0"
910

  
911
	elog "mrustc bootstrap stage0 complete!"
912

  
913
	# Step 3: Build a "proper" libstd, including dynamic libs using our bootstrap cargo and rustc.
914
	elog "Building 'sysroot' with the stage0 rustc (mrustc-stage1) ..."
915
	local stage1="${WORKDIR}/bootstrap/mrustc-stage1"
916
	local stage1_sysroot_lib="${stage1}/lib/rustlib/${CFG_COMPILER_HOST_TRIPLE}/lib"
917
	mkdir -p "${stage1_sysroot_lib}" || die "Failed to create stage1 directory"
918
	mkdir -p "${stage1}/bin" || die
919

  
920
	# Simplified to avoid calling rustc_proxy; We don't need stage1 rustc until after this is built...
921
	edo env RUSTFLAGS="-Z force-unstable-if-unmarked" CARGO_TARGET_DIR="${stage1}/sysroot-build" \
922
		RUSTC="${stage0}/rustc-build/rustc" "${stage0}"/cargo-build/cargo build ${cargo_flags} \
923
		--manifest-path "${S}/library/sysroot/Cargo.toml" --features panic-unwind
924

  
925
	# Move the built libs into the sysroot libdir.
926
	mv "${stage1}/sysroot-build/${CFG_COMPILER_HOST_TRIPLE}/release/deps"/*.{rlib,rmeta,so} \
927
		"${stage1_sysroot_lib}" || die "Failed to move stage1 libs to stage1 sysroot"
928

  
929
	# We need to copy the stage0 rustc to the stage1 sysroot; this "updates" the sysroot location and enables
930
	# resolution of stage1 libs. (run `rustc --print sysroot` on stage0 and stage1 rustc to verify)
931
	cp "${stage0}/rustc-build/rustc" "${stage1}/bin/rustc" || die "Failed to copy rustc to stage1 sysroot"
932

  
933
	# Step 4: Build `rustc` with itself, so we have a rustc with the right ABI.
934
	# This will be our final `rustc` for the bootstrap process.
935
	elog "Building rustc with stage1 libs (mrustc-stage1) ..."
936
	mkdir -p "${stage1}/rustc-build" || die
937
	edo env RUSTFLAGS="-Z force-unstable-if-unmarked -C link_args=-Wl,-rpath,\$ORIGIN/../lib" \
938
		LD_LIBRARY_PATH="${stage2_sysroot_lib}" CARGO_TARGET_DIR="${stage1}/rustc-build" \
939
		RUSTC="${stage1}/bin/rustc" TMPDIR="${T}" "${stage0}"/cargo-build/cargo build ${cargo_flags} \
940
		--manifest-path "${S}/compiler/rustc/Cargo.toml"  --features llvm
941

  
942
	# Step 5: Build `sysroot` with this `rustc` - Needed to match ABI
943
	# We need to use the previous sysroot; we could reuse that dir but it's easier to just copy it.
944
	elog "Building final 'sysroot' with the final rustc (mrustc-stage2) ..."
945
	local stage2="${WORKDIR}/bootstrap/mrustc-stage2"
946
	local stage2_sysroot_lib="${stage2}/lib/rustlib/${CFG_COMPILER_HOST_TRIPLE}/lib"
947
	mkdir -p "${stage2_sysroot_lib}" || die "Failed to create stage2 directory"
948
	mkdir -p "${stage2}/bin" || die
949

  
950
	# Copy required files from stage1 to stage2 sysroot
951
	cp "${stage1}/rustc-build/${CFG_COMPILER_HOST_TRIPLE}"/release/rustc-main "${stage2}/bin/rustc_binary" ||
952
		die "Failed to copy final rustc to stage2 sysroot"
953
	cp "${stage1}/rustc-build/${CFG_COMPILER_HOST_TRIPLE}"/release/librustc_driver.so "${stage2}/lib" ||
954
		die "Failed to copy librustc_driver to sysroot"
955
	cp "${stage1}/rustc-build/${CFG_COMPILER_HOST_TRIPLE}"/release/deps/*.{rlib,so} "${stage2_sysroot_lib}" ||
956
		die "Failed to copy final rustc libs to stage2 sysroot"
957
	cp "${stage1_sysroot_lib}"/* "${stage2_sysroot_lib}" || die "Failed to copy stage1 so files to stage2 sysroot"
958

  
959
	# There's a magic script used in place of rustc so that libs can be found
960
	cat <<- EOF > "${stage2}/bin/rustc" || die "Failed to create rustc wrapper"
961
		#!/bin/sh
962
		LD_LIBRARY_PATH="${stage2}/lib:${stage2_sysroot_lib}" ${stage2}/bin/rustc_binary "\$@"
963
	EOF
964
	chmod +x "${stage2}/bin/rustc" || die "Failed to make rustc wrapper executable"
965

  
966
	# Use rustc to build 'sysroot'; this is the final step in the bootstrap process.
967
	# rpath probably isn't needed here, but it doesn't hurt.
968
	edo env RUSTFLAGS="-Z force-unstable-if-unmarked -C link_args=-Wl,-rpath,\$ORIGIN/../lib" \
969
		CARGO_TARGET_DIR="${stage2}/stdlib-build" RUSTC="${stage2}/bin/rustc" \
970
		"${stage0}"/cargo-build/cargo build ${cargo_flags} --manifest-path "${S}/library/sysroot/Cargo.toml" \
971
		--features panic-unwind
972

  
973
	# Build our final output sysroot
974
	local output="${WORKDIR}/bootstrap/rust-${PV}"
975
	local output_sysroot_lib="${output}/lib/rustlib/${CFG_COMPILER_HOST_TRIPLE}/lib"
976
	mkdir -p "${output_sysroot_lib}" || die "Failed to create output directory"
977
	mkdir -p "${output}/bin" || die "Failed to create output directory"
978

  
979
	# Copy our various output files into the output sysroot
980
	# rustc
981
	cp "${stage1}/rustc-build/${CFG_COMPILER_HOST_TRIPLE}"/release/rustc-main "${output}/bin/rustc_binary" ||
982
		die "Failed to copy final rustc to output"
983
	cp "${stage1}/rustc-build/${CFG_COMPILER_HOST_TRIPLE}"/release/librustc_driver.so "${output}/lib" ||
984
		die "Failed to copy librustc_driver to output"
985
	cp "${stage1}/rustc-build/${CFG_COMPILER_HOST_TRIPLE}"/release/deps/*.{rlib,so} "${output_sysroot_lib}" ||
986
		die "Failed to copy final rustc libs to output"
987
	# cargo; no need to build an optimised cargo if we're using this to build a complelety new Rust.
988
	cp "${stage0}/cargo-build/cargo" "${output}/bin/cargo" || die "Failed to copy cargo to output"
989
	# libs
990
	mv "${stage2}/stdlib-build/${CFG_COMPILER_HOST_TRIPLE}/release/deps"/*.{rlib,rmeta,so} "${output_sysroot_lib}" ||
991
		die "Failed to copy stage2 libs to output"
992
	# Our trusty rustc wrapper
993
	cat <<- EOF > "${output}/bin/rustc" || die "Failed to create rustc wrapper"
994
		#!/bin/sh
995
		LD_LIBRARY_PATH="${output}/lib:${output_sysroot_lib}" ${output}/bin/rustc_binary "\$@"
996
	EOF
997
	chmod +x "${output}/bin/rustc" || die "Failed to make rustc wrapper executable"
998

  
999
	# Perform a sanity check on the final Rust.
1000
	mkdir -p "${T}"/output-hello || die
1001
	edo "${output}/bin/rustc" -L "${output_sysroot_lib}" -g "${S}/tests/ui/parallel-rustc/hello_world.rs" \
1002
		-o "${T}"/output-hello/hello
1003
	"${T}"/output-hello/hello || die "Failed to run hello_world built with bootstrapped Rust"
1004

  
1005
	elog "Successfully bootstrapped Rust using mrustc!"
1006

  
1007
	# Note: The Rust sysroot that we've produced is pretty close to what we'd expect from a normal Rust build.
1008
	# If someone was so inclined they could build an optimised cargo using the stage2 rustc and sysroot,
1009
	# and install the output directly. This is untested, as I'm sure there's more to it than that.
1010
	# I'm satisfied with being able to build Rust normally at this point.
1011

  
1012
	# Tidy up the Rust sources; revert mrustc changes so Rust can be built normally.
1013
	pushd "${S}" 2>/dev/null || die
1014
		patch -R -p0 < "${BROOT}"/usr/share/mrustc-${MRUSTC_VERSION}/patches/rustc-${MRUSTC_RUST_VERSION}-src.patch ||
1015
			die "Failed to revert mrustc patches"
1016
	popd 2>/dev/null || die
1017

  
1018
	# Tidy up any environment variables we've set in the bootstrap process.
1019
	unset CFG_COMPILER_HOST_TRIPLE CFG_RELEASE CFG_RELEASE_CHANNEL CFG_PREFIX CFG_VERSION CFG_LIBDIR_RELATIVE
1020
	unset LLVM_CONFIG MRUSTC_TARGET_VER REAL_LIBRARY_PATH_VAR RUSTFLAGS RUSTC_BOOTSTRAP RUSTC_INSTALL_BINDIR
1021
}
1022

  
1023 717
src_compile() {
1024
	use mrustc-bootstrap && mrustc_bootstrap
1025 718
	# -v will show invocations, -vv "very verbose" is overkill, -vvv "very very verbose" is insane
1026 719
	RUST_BACKTRACE=1 "${EPYTHON}" ./x.py build -v --config="${S}"/bootstrap.toml -j$(makeopts_jobs) || die
1027 720
}
......
1083 776

  
1084 777
	docompress /usr/lib/${PN}/${SLOT}/share/man/
1085 778

  
1086
	# bug #689562, #689160
779
	# bash-completion files are installed by dev-lang/rust-common instead
780
	# bug #689562, #689160.
1087 781
	rm -v "${ED}/usr/lib/${PN}/${SLOT}/etc/bash_completion.d/cargo" || die
1088
	rmdir -v "${ED}/usr/lib/${PN}/${SLOT}"/etc{/bash_completion.d,} || die
782
	rmdir -v "${ED}/usr/lib/${PN}/${SLOT}/etc/bash_completion.d" || die
1089 783

  
1090 784
	local symlinks=(
1091 785
		cargo
......
1109 803
		# we need realpath on /usr/bin/* symlink return version-appended binary path.
1110 804
		# so /usr/bin/rustc should point to /usr/lib/rust/<ver>/bin/rustc-<ver>
1111 805
		# need to fix eselect-rust to remove this hack.
1112
		local ver_i="${i}-${PV%%_*}"
806
		local ver_i="${i}-${RUST_PV%%_*}"
1113 807
		if [[ -f "${ED}/usr/lib/${PN}/${SLOT}/bin/${i}" ]]; then
1114 808
			einfo "Installing ${i} symlink"
1115 809
			ln -v "${ED}/usr/lib/${PN}/${SLOT}/bin/${i}" "${ED}/usr/lib/${PN}/${SLOT}/bin/${ver_i}" || die
......
1125 819
	use rust-analyzer && dosym "${SLOT}/libexec" "/usr/lib/${PN}/libexec-${SLOT}"
1126 820
	dosym "${SLOT}/share/man" "/usr/lib/${PN}/man-${SLOT}"
1127 821
	dosym "rust/${SLOT}/lib/rustlib" "/usr/lib/rustlib-${SLOT}"
1128
	dosym "../../lib/${PN}/${SLOT}/share/doc/rust" "/usr/share/doc/${P}"
822
	dosym "../../lib/${PN}/${SLOT}/share/doc/rust" "/usr/share/doc/${RUST_P}"
1129 823

  
1130
	newenvd - "50${P}" <<-_EOF_
824
	newenvd - "50${RUST_P}" <<-_EOF_
1131 825
		MANPATH="${EPREFIX}/usr/lib/rust/man-${SLOT}"
1132 826
	_EOF_
1133 827

  
......
1149 843
	_EOF_
1150 844

  
1151 845
	if use clippy; then
1152
		echo /usr/bin/clippy-driver >> "${T}/provider-${P}"
1153
		echo /usr/bin/cargo-clippy >> "${T}/provider-${P}"
846
		echo /usr/bin/clippy-driver >> "${T}/provider-${RUST_P}"
847
		echo /usr/bin/cargo-clippy >> "${T}/provider-${RUST_P}"
1154 848
	fi
1155 849
	if [[ ${SLOT} == *9999* ]] && use miri; then
1156
		echo /usr/bin/miri >> "${T}/provider-${P}"
1157
		echo /usr/bin/cargo-miri >> "${T}/provider-${P}"
850
		echo /usr/bin/miri >> "${T}/provider-${RUST_P}"
851
		echo /usr/bin/cargo-miri >> "${T}/provider-${RUST_P}"
1158 852
	fi
1159 853
	if use rustfmt; then
1160
		echo /usr/bin/rustfmt >> "${T}/provider-${P}"
1161
		echo /usr/bin/cargo-fmt >> "${T}/provider-${P}"
854
		echo /usr/bin/rustfmt >> "${T}/provider-${RUST_P}"
855
		echo /usr/bin/cargo-fmt >> "${T}/provider-${RUST_P}"
1162 856
	fi
1163 857
	if use rust-analyzer; then
1164
		echo /usr/lib/rust/libexec >> "${T}/provider-${P}"
1165
		echo /usr/bin/rust-analyzer >> "${T}/provider-${P}"
858
		echo /usr/lib/rust/libexec >> "${T}/provider-${RUST_P}"
859
		echo /usr/bin/rust-analyzer >> "${T}/provider-${RUST_P}"
1166 860
	fi
1167 861

  
1168 862
	insinto /etc/env.d/rust
......
1176 870
}
1177 871

  
1178 872
pkg_postinst() {
1179

  
1180 873
	eselect rust update
1181 874

  
1182 875
	if has_version dev-debug/gdb || has_version llvm-core/lldb; then
1183 876
		elog "Rust installs helper scripts for calling GDB and LLDB,"
1184
		elog "for convenience they are installed under /usr/bin/rust-{gdb,lldb}-${PV}."
877
		elog "for convenience they are installed under /usr/bin/rust-{gdb,lldb}-${RUST_PV}."
1185 878
	fi
1186 879

  
1187 880
	if has_version app-editors/emacs; then
Thank you!