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-09-07 20:03:06.629248090 +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_{12..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-r2 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-r2 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
		amd64? ( ~dev-lang/mrustc-${MRUSTC_VERSION} )
121
		arm64? ( ~dev-lang/mrustc-0.12.0_p20261018 )
122
		dev-build/cmake
123
		sys-devel/gcc:*
124
	)
125
	!mrustc-bootstrap? ( ${RUST_DEPEND} )
126 135
"
127 136

  
128 137
DEPEND="
......
141 150
	)
142 151
"
143 152

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

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

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

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

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

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

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

  
373 361
	default
374

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

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

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

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

  
441
	# TODO: Add optimized-compiler-builtins for system-llvm to avoid
442
	# building bundled compiler-rt.
464 443
	cat <<- _EOF_ > "${S}"/bootstrap.toml
465 444
		# Suppresses a warning about tracking changes which we don't care about.
466 445
		change-id = "ignore"
......
609 588
	done
610 589
	if use rust_sysroots_wasm; then
611 590
		wasm_target="wasm32-unknown-unknown"
612
		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
		)"
613 604
		cat <<- _EOF_ >> "${S}"/bootstrap.toml
614 605
			[target.wasm32-unknown-unknown]
606
			cc = "${wasm_cc}"
607
			cxx = "${wasm_cxx}"
615 608
			linker = "$(usex system-llvm lld rust-lld)"
616 609
			# wasm target does not have profiler_builtins https://bugs.gentoo.org/848483
617 610
			profiler = false
......
721 714
	echo
722 715
}
723 716

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

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

  
749
	elog "Building bootstrap llvm ..."
750

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

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

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

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

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

  
802
	local llvm_config_wrapper_cxxflags=0
803

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

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

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

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

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

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

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

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

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

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

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

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

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

  
912
	elog "mrustc bootstrap stage0 complete!"
913

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

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

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

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

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

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

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

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

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

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

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

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

  
1006
	elog "Successfully bootstrapped Rust using mrustc!"
1007

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

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

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

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

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

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

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

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

  
......
1150 843
	_EOF_
1151 844

  
1152 845
	if use clippy; then
1153
		echo /usr/bin/clippy-driver >> "${T}/provider-${P}"
1154
		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}"
1155 848
	fi
1156 849
	if [[ ${SLOT} == *9999* ]] && use miri; then
1157
		echo /usr/bin/miri >> "${T}/provider-${P}"
1158
		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}"
1159 852
	fi
1160 853
	if use rustfmt; then
1161
		echo /usr/bin/rustfmt >> "${T}/provider-${P}"
1162
		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}"
1163 856
	fi
1164 857
	if use rust-analyzer; then
1165
		echo /usr/lib/rust/libexec >> "${T}/provider-${P}"
1166
		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}"
1167 860
	fi
1168 861

  
1169 862
	insinto /etc/env.d/rust
......
1177 870
}
1178 871

  
1179 872
pkg_postinst() {
1180

  
1181 873
	eselect rust update
1182 874

  
1183 875
	if has_version dev-debug/gdb || has_version llvm-core/lldb; then
1184 876
		elog "Rust installs helper scripts for calling GDB and LLDB,"
1185
		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}."
1186 878
	fi
1187 879

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