*/*: Clean PYTHON_COMPAT of obsolete impls
[gentoo.git] / dev-lang / rust / rust-1.37.0.ebuild
1 # Copyright 1999-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 EAPI=7
5
6 PYTHON_COMPAT=( python2_7 python3_{6,7} )
7
8 inherit check-reqs estack flag-o-matic llvm multiprocessing multilib-build python-any-r1 rust-toolchain toolchain-funcs
9
10 if [[ ${PV} = *beta* ]]; then
11         betaver=${PV//*beta}
12         BETA_SNAPSHOT="${betaver:0:4}-${betaver:4:2}-${betaver:6:2}"
13         MY_P="rustc-beta"
14         SLOT="beta/${PV}"
15         SRC="${BETA_SNAPSHOT}/rustc-beta-src.tar.xz"
16 else
17         ABI_VER="$(ver_cut 1-2)"
18         SLOT="stable/${ABI_VER}"
19         MY_P="rustc-${PV}"
20         SRC="${MY_P}-src.tar.xz"
21         KEYWORDS="amd64 arm64 ppc64 x86"
22 fi
23
24 RUST_STAGE0_VERSION="1.$(($(ver_cut 2) - 1)).0"
25
26 DESCRIPTION="Systems programming language from Mozilla"
27 HOMEPAGE="https://www.rust-lang.org/"
28
29 SRC_URI="https://static.rust-lang.org/dist/${SRC} -> rustc-${PV}-src.tar.xz
30         $(rust_all_arch_uris rust-${RUST_STAGE0_VERSION})"
31
32 ALL_LLVM_TARGETS=( AArch64 AMDGPU ARM BPF Hexagon Lanai Mips MSP430
33         NVPTX PowerPC Sparc SystemZ WebAssembly X86 XCore )
34 ALL_LLVM_TARGETS=( "${ALL_LLVM_TARGETS[@]/#/llvm_targets_}" )
35 LLVM_TARGET_USEDEPS=${ALL_LLVM_TARGETS[@]/%/?}
36
37 LICENSE="|| ( MIT Apache-2.0 ) BSD-1 BSD-2 BSD-4 UoI-NCSA"
38
39 IUSE="clippy cpu_flags_x86_sse2 debug doc libressl rls rustfmt system-llvm wasm ${ALL_LLVM_TARGETS[*]}"
40
41 # Please keep the LLVM dependency block separate. Since LLVM is slotted,
42 # we need to *really* make sure we're not pulling one than more slot
43 # simultaneously.
44
45 # How to use it:
46 # 1. List all the working slots (with min versions) in ||, newest first.
47 # 2. Update the := to specify *max* version, e.g. < 9.
48 # 3. Specify LLVM_MAX_SLOT, e.g. 8.
49 LLVM_DEPEND="
50         || (
51                 sys-devel/llvm:8[llvm_targets_WebAssembly?]
52                 wasm? ( =sys-devel/lld-8* )
53         )
54         <sys-devel/llvm-9:=
55 "
56 LLVM_MAX_SLOT=8
57
58 COMMON_DEPEND="
59         sys-libs/zlib
60         !libressl? ( dev-libs/openssl:0= )
61         libressl? ( dev-libs/libressl:0= )
62         net-libs/libssh2
63         net-libs/http-parser:=
64         net-misc/curl[ssl]
65         system-llvm? (
66                 ${LLVM_DEPEND}
67         )
68 "
69
70 DEPEND="${COMMON_DEPEND}
71         ${PYTHON_DEPS}
72         || (
73                 >=sys-devel/gcc-4.7
74                 >=sys-devel/clang-3.5
75         )
76         dev-util/cmake
77 "
78
79 RDEPEND="${COMMON_DEPEND}
80         >=app-eselect/eselect-rust-20190311
81         !dev-util/cargo
82         rustfmt? ( !dev-util/rustfmt )
83 "
84
85 REQUIRED_USE="|| ( ${ALL_LLVM_TARGETS[*]} )
86         wasm? ( llvm_targets_WebAssembly )
87         x86? ( cpu_flags_x86_sse2 )
88 "
89
90 PATCHES=(
91         "${FILESDIR}"/0001-llvm-cmake-Add-additional-headers-only-if-they-exist.patch
92         "${FILESDIR}"/1.34.2-fix-custom-libdir.patch
93         "${FILESDIR}"/1.35.0-revert-commits-triggering-multiple-llvm-rebuilds.patch
94         "${FILESDIR}"/1.36.0-libressl.patch
95 )
96
97 S="${WORKDIR}/${MY_P}-src"
98
99 toml_usex() {
100         usex "$1" true false
101 }
102
103 pre_build_checks() {
104         CHECKREQS_DISK_BUILD="7G"
105         eshopts_push -s extglob
106         if is-flagq '-g?(gdb)?([1-9])'; then
107                 CHECKREQS_DISK_BUILD="10G"
108         fi
109         eshopts_pop
110         check-reqs_pkg_setup
111 }
112
113 pkg_pretend() {
114         pre_build_checks
115 }
116
117 pkg_setup() {
118         pre_build_checks
119         python-any-r1_pkg_setup
120         use system-llvm && llvm_pkg_setup
121 }
122
123 src_prepare() {
124         local rust_stage0_root="${WORKDIR}"/rust-stage0
125
126         local rust_stage0="rust-${RUST_STAGE0_VERSION}-$(rust_abi)"
127
128         "${WORKDIR}/${rust_stage0}"/install.sh --disable-ldconfig --destdir="${rust_stage0_root}" --prefix=/ || die
129
130         default
131 }
132
133 src_configure() {
134         local rust_target="" rust_targets="" arch_cflags
135
136         # Collect rust target names to compile standard libs for all ABIs.
137         for v in $(multilib_get_enabled_abi_pairs); do
138                 rust_targets="${rust_targets},\"$(rust_abi $(get_abi_CHOST ${v##*.}))\""
139         done
140         if use wasm; then
141                 rust_targets="${rust_targets},\"wasm32-unknown-unknown\""
142         fi
143         rust_targets="${rust_targets#,}"
144
145         local extended="true" tools="\"cargo\","
146         if use clippy; then
147                 tools="\"clippy\",$tools"
148         fi
149         if use rls; then
150                 tools="\"rls\",\"analysis\",\"src\",$tools"
151         fi
152         if use rustfmt; then
153                 tools="\"rustfmt\",$tools"
154         fi
155
156         local rust_stage0_root="${WORKDIR}"/rust-stage0
157
158         rust_target="$(rust_abi)"
159
160         cat <<- EOF > "${S}"/config.toml
161                 [llvm]
162                 optimize = $(toml_usex !debug)
163                 release-debuginfo = $(toml_usex debug)
164                 assertions = $(toml_usex debug)
165                 targets = "${LLVM_TARGETS// /;}"
166                 experimental-targets = ""
167                 link-shared = $(toml_usex system-llvm)
168                 [build]
169                 build = "${rust_target}"
170                 host = ["${rust_target}"]
171                 target = [${rust_targets}]
172                 cargo = "${rust_stage0_root}/bin/cargo"
173                 rustc = "${rust_stage0_root}/bin/rustc"
174                 docs = $(toml_usex doc)
175                 submodules = false
176                 python = "${EPYTHON}"
177                 locked-deps = true
178                 vendor = true
179                 extended = ${extended}
180                 tools = [${tools}]
181                 verbose = 2
182                 [install]
183                 prefix = "${EPREFIX}/usr"
184                 libdir = "$(get_libdir)/${P}"
185                 docdir = "share/doc/${P}"
186                 mandir = "share/${P}/man"
187                 [rust]
188                 optimize = $(toml_usex !debug)
189                 debug = $(toml_usex debug)
190                 debug-assertions = $(toml_usex debug)
191                 default-linker = "$(tc-getCC)"
192                 channel = "stable"
193                 rpath = false
194                 lld = $(usex system-llvm false $(toml_usex wasm))
195         EOF
196
197         for v in $(multilib_get_enabled_abi_pairs); do
198                 rust_target=$(rust_abi $(get_abi_CHOST ${v##*.}))
199                 arch_cflags="$(get_abi_CFLAGS ${v##*.})"
200
201                 cat <<- EOF >> "${S}"/config.env
202                         CFLAGS_${rust_target}=${arch_cflags}
203                 EOF
204
205                 cat <<- EOF >> "${S}"/config.toml
206                         [target.${rust_target}]
207                         cc = "$(tc-getBUILD_CC)"
208                         cxx = "$(tc-getBUILD_CXX)"
209                         linker = "$(tc-getCC)"
210                         ar = "$(tc-getAR)"
211                 EOF
212                 if use system-llvm; then
213                         cat <<- EOF >> "${S}"/config.toml
214                                 llvm-config = "$(get_llvm_prefix "${LLVM_MAX_SLOT}")/bin/llvm-config"
215                         EOF
216                 fi
217         done
218
219         if use wasm; then
220                 cat <<- EOF >> "${S}"/config.toml
221                         [target.wasm32-unknown-unknown]
222                         linker = "$(usex system-llvm lld rust-lld)"
223                 EOF
224         fi
225 }
226
227 src_compile() {
228         env $(cat "${S}"/config.env)\
229                 "${EPYTHON}" ./x.py build -vv --config="${S}"/config.toml -j$(makeopts_jobs) \
230                 --exclude src/tools/miri || die # https://github.com/rust-lang/rust/issues/52305
231 }
232
233 src_install() {
234         local rust_target abi_libdir
235
236         env DESTDIR="${D}" "${EPYTHON}" ./x.py install -vv --config="${S}"/config.toml \
237         --exclude src/tools/miri || die
238
239         mv "${ED}/usr/bin/rustc" "${ED}/usr/bin/rustc-${PV}" || die
240         mv "${ED}/usr/bin/rustdoc" "${ED}/usr/bin/rustdoc-${PV}" || die
241         mv "${ED}/usr/bin/rust-gdb" "${ED}/usr/bin/rust-gdb-${PV}" || die
242         mv "${ED}/usr/bin/rust-gdbgui" "${ED}/usr/bin/rust-gdbgui-${PV}" || die
243         mv "${ED}/usr/bin/rust-lldb" "${ED}/usr/bin/rust-lldb-${PV}" || die
244         mv "${ED}/usr/bin/cargo" "${ED}/usr/bin/cargo-${PV}" || die
245         if use clippy; then
246                 mv "${ED}/usr/bin/clippy-driver" "${ED}/usr/bin/clippy-driver-${PV}" || die
247                 mv "${ED}/usr/bin/cargo-clippy" "${ED}/usr/bin/cargo-clippy-${PV}" || die
248         fi
249         if use rls; then
250                 mv "${ED}/usr/bin/rls" "${ED}/usr/bin/rls-${PV}" || die
251         fi
252         if use rustfmt; then
253                 mv "${ED}/usr/bin/rustfmt" "${ED}/usr/bin/rustfmt-${PV}" || die
254                 mv "${ED}/usr/bin/cargo-fmt" "${ED}/usr/bin/cargo-fmt-${PV}" || die
255         fi
256
257         # Copy shared library versions of standard libraries for all targets
258         # into the system's abi-dependent lib directories because the rust
259         # installer only does so for the native ABI.
260         for v in $(multilib_get_enabled_abi_pairs); do
261                 if [ ${v##*.} = ${DEFAULT_ABI} ]; then
262                         continue
263                 fi
264                 abi_libdir=$(get_abi_LIBDIR ${v##*.})
265                 rust_target=$(rust_abi $(get_abi_CHOST ${v##*.}))
266                 mkdir -p "${ED}/usr/${abi_libdir}/${P}"
267                 cp "${ED}/usr/$(get_libdir)/${P}/rustlib/${rust_target}/lib"/*.so \
268                    "${ED}/usr/${abi_libdir}/${P}" || die
269         done
270
271         dodoc COPYRIGHT
272
273         # FIXME:
274         # Really not sure if that env is needed, specailly LDPATH
275         cat <<-EOF > "${T}"/50${P}
276                 LDPATH="${EPREFIX}/usr/$(get_libdir)/${P}"
277                 MANPATH="${EPREFIX}/usr/share/${P}/man"
278         EOF
279         doenvd "${T}"/50${P}
280
281         # note: eselect-rust adds EROOT to all paths below
282         cat <<-EOF > "${T}/provider-${P}"
283                 /usr/bin/rustdoc
284                 /usr/bin/rust-gdb
285                 /usr/bin/rust-gdbgui
286                 /usr/bin/rust-lldb
287         EOF
288         echo /usr/bin/cargo >> "${T}/provider-${P}"
289         if use clippy; then
290                 echo /usr/bin/clippy-driver >> "${T}/provider-${P}"
291                 echo /usr/bin/cargo-clippy >> "${T}/provider-${P}"
292         fi
293         if use rls; then
294                 echo /usr/bin/rls >> "${T}/provider-${P}"
295         fi
296         if use rustfmt; then
297                 echo /usr/bin/rustfmt >> "${T}/provider-${P}"
298                 echo /usr/bin/cargo-fmt >> "${T}/provider-${P}"
299         fi
300         dodir /etc/env.d/rust
301         insinto /etc/env.d/rust
302         doins "${T}/provider-${P}"
303 }
304
305 pkg_postinst() {
306         eselect rust update --if-unset
307
308         elog "Rust installs a helper script for calling GDB and LLDB,"
309         elog "for your convenience it is installed under /usr/bin/rust-{gdb,lldb}-${PV}."
310
311         ewarn "cargo is now installed from dev-lang/rust{,-bin} instead of dev-util/cargo."
312         ewarn "This might have resulted in a dangling symlink for /usr/bin/cargo on some"
313         ewarn "systems. This can be resolved by calling 'sudo eselect rust set ${P}'."
314
315         if has_version app-editors/emacs; then
316                 elog "install app-emacs/rust-mode to get emacs support for rust."
317         fi
318
319         if has_version app-editors/gvim || has_version app-editors/vim; then
320                 elog "install app-vim/rust-vim to get vim support for rust."
321         fi
322 }
323
324 pkg_postrm() {
325         eselect rust cleanup
326 }