Gentoo Archives: gentoo-dev

From: Georgy Yakovlev <gyakovlev@g.o>
To: gentoo-dev@l.g.o
Cc: Georgy Yakovlev <gyakovlev@g.o>
Subject: [gentoo-dev] [PATCH 1/2] cargo.eclass: support EAPI=8, misc changes
Date: Fri, 02 Jul 2021 00:51:29
Message-Id: 20210702005107.3582488-1-gyakovlev@gentoo.org
1 remove cargo-snapshot* unpacker, it was needed
2 for separate dev-util/cargo we used to have.
3
4 Bug: https://bugs.gentoo.org/715890
5 Signed-off-by: Georgy Yakovlev <gyakovlev@g.o>
6 ---
7 eclass/cargo.eclass | 39 ++++++++++++++++++++++++++-------------
8 1 file changed, 26 insertions(+), 13 deletions(-)
9
10 diff --git a/eclass/cargo.eclass b/eclass/cargo.eclass
11 index 15b9d455bdef..9923b1c9deb2 100644
12 --- a/eclass/cargo.eclass
13 +++ b/eclass/cargo.eclass
14 @@ -1,396 +1,409 @@
15 # Copyright 1999-2021 Gentoo Authors
16 # Distributed under the terms of the GNU General Public License v2
17
18 # @ECLASS: cargo.eclass
19 # @MAINTAINER:
20 # rust@g.o
21 # @AUTHOR:
22 # Doug Goldstein <cardoe@g.o>
23 # Georgy Yakovlev <gyakovlev@×××××××.org>
24 -# @SUPPORTED_EAPIS: 7
25 +# @SUPPORTED_EAPIS: 7 8
26 # @BLURB: common functions and variables for cargo builds
27
28 if [[ -z ${_CARGO_ECLASS} ]]; then
29 _CARGO_ECLASS=1
30
31 -# we need this for 'cargo vendor' subcommand and net.offline config knob
32 -RUST_DEPEND=">=virtual/rust-1.37.0"
33 +# check and document RUST_DEPEND and options we need below in case conditions.
34 +# https://github.com/rust-lang/cargo/blob/master/CHANGELOG.md
35 +RUST_DEPEND="virtual/rust"
36
37 case "${EAPI:-0}" in
38 0|1|2|3|4|5|6)
39 die "Unsupported EAPI=${EAPI:-0} (too old) for ${ECLASS}"
40 ;;
41 7)
42 + # 1.37 added 'cargo vendor' subcommand and net.offline config knob
43 + RUST_DEPEND=">=virtual/rust-1.37.0"
44 + ;;
45 +
46 + 8)
47 + # 1.39 added --workspace
48 + # 1.46 added --target dir
49 + # 1.48 added term.progress config option
50 + # 1.51 added split-debuginfo profile option
51 + # 1.52 may need setting RUSTC_BOOTSTRAP envvar for some crates
52 + # 1.53 added cargo update --offline, can be used to update vulnerable crates from pre-fetched registry without editing toml
53 + RUST_DEPEND=">=virtual/rust-1.53"
54 ;;
55 *)
56 die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
57 ;;
58 esac
59
60 inherit multiprocessing toolchain-funcs
61
62 if [[ ! ${CARGO_OPTIONAL} ]]; then
63 BDEPEND="${RUST_DEPEND}"
64 EXPORT_FUNCTIONS src_unpack src_configure src_compile src_install src_test
65 fi
66
67 IUSE="${IUSE} debug"
68
69 ECARGO_HOME="${WORKDIR}/cargo_home"
70 ECARGO_VENDOR="${ECARGO_HOME}/gentoo"
71
72 # @ECLASS-VARIABLE: CARGO_OPTIONAL
73 # @DEFAULT_UNSET
74 # @PRE_INHERIT
75 # @DESCRIPTION:
76 # If set to a non-null value, before inherit cargo part of the ebuild will
77 # be considered optional. No dependencies will be added and no phase
78 # functions will be exported.
79 #
80 # If you enable CARGO_OPTIONAL, you have to set BDEPEND on virtual/rust
81 # for your package and call at least cargo_gen_config manually before using
82 # other src_ functions of this eclass.
83 # note that cargo_gen_config is automatically called by cargo_src_unpack.
84
85 # @ECLASS_VARIABLE: myfeatures
86 # @DEFAULT_UNSET
87 # @DESCRIPTION:
88 # Optional cargo features defined as bash array.
89 # Should be defined before calling cargo_src_configure().
90 #
91 # Example package that has x11 and wayland as features, and disables default.
92 # @CODE
93 # src_configure() {
94 # local myfeatures=(
95 # $(usex X x11 '')
96 # $(usev wayland)
97 # )
98 # cargo_src_configure --no-default-features
99 # }
100 # @CODE
101
102 # @ECLASS-VARIABLE: ECARGO_REGISTRY_DIR
103 # @USER_VARIABLE
104 # @DEFAULT_UNSET
105 # @DESCRIPTION:
106 # Storage directory for cargo registry.
107 # Used by cargo_live_src_unpack to cache downloads.
108 # This is intended to be set by users.
109 # Ebuilds must not set it.
110 #
111 # Defaults to "${DISTDIR}/cargo-registry" it not set.
112
113 # @ECLASS-VARIABLE: ECARGO_OFFLINE
114 # @USER_VARIABLE
115 # @DEFAULT_UNSET
116 # @DESCRIPTION:
117 # If non-empty, this variable prevents online operations in
118 # cargo_live_src_unpack.
119 # Inherits value of EVCS_OFFLINE if not set explicitly.
120
121 # @ECLASS-VARIABLE: EVCS_UMASK
122 # @USER_VARIABLE
123 # @DEFAULT_UNSET
124 # @DESCRIPTION:
125 # Set this variable to a custom umask. This is intended to be set by
126 # users. By setting this to something like 002, it can make life easier
127 # for people who use cargo in a home directory, but are in the portage
128 # group, and then switch over to building with FEATURES=userpriv.
129 # Or vice-versa.
130
131 # @FUNCTION: cargo_crate_uris
132 # @DESCRIPTION:
133 # Generates the URIs to put in SRC_URI to help fetch dependencies.
134 cargo_crate_uris() {
135 local -r regex='^([a-zA-Z0-9_\-]+)-([0-9]+\.[0-9]+\.[0-9]+.*)$'
136 local crate
137 for crate in "$@"; do
138 local name version url
139 [[ $crate =~ $regex ]] || die "Could not parse name and version from crate: $crate"
140 name="${BASH_REMATCH[1]}"
141 version="${BASH_REMATCH[2]}"
142 url="https://crates.io/api/v1/crates/${name}/${version}/download -> ${crate}.crate"
143 echo "${url}"
144 done
145 }
146
147 # @FUNCTION: cargo_gen_config
148 # @DESCRIPTION:
149 # Generate the $CARGO_HOME/config necessary to use our local registry and settings.
150 # Cargo can also be configured through environment variables in addition to the TOML syntax below.
151 # For each configuration key below of the form foo.bar the environment variable CARGO_FOO_BAR
152 # can also be used to define the value.
153 # Environment variables will take precedence over TOML configuration,
154 # and currently only integer, boolean, and string keys are supported.
155 # For example the build.jobs key can also be defined by CARGO_BUILD_JOBS.
156 # Or setting CARGO_TERM_VERBOSE=false in make.conf will make build quieter.
157 cargo_gen_config() {
158 debug-print-function ${FUNCNAME} "$@"
159
160 mkdir -p "${ECARGO_HOME}" || die
161
162 cat > "${ECARGO_HOME}/config" <<- _EOF_ || die "Failed to create cargo config"
163 [source.gentoo]
164 directory = "${ECARGO_VENDOR}"
165
166 [source.crates-io]
167 replace-with = "gentoo"
168 local-registry = "/nonexistant"
169
170 [net]
171 offline = true
172
173 [build]
174 jobs = $(makeopts_jobs)
175 incremental = false
176
177 [term]
178 verbose = true
179 $([[ "${NOCOLOR}" = true || "${NOCOLOR}" = yes ]] && echo "color = 'never'")
180 _EOF_
181
182 export CARGO_HOME="${ECARGO_HOME}"
183 _CARGO_GEN_CONFIG_HAS_RUN=1
184 }
185
186 # @FUNCTION: cargo_src_unpack
187 # @DESCRIPTION:
188 # Unpacks the package and the cargo registry
189 cargo_src_unpack() {
190 debug-print-function ${FUNCNAME} "$@"
191
192 mkdir -p "${ECARGO_VENDOR}" || die
193 mkdir -p "${S}" || die
194
195 local archive shasum pkg
196 for archive in ${A}; do
197 case "${archive}" in
198 *.crate)
199 ebegin "Loading ${archive} into Cargo registry"
200 tar -xf "${DISTDIR}"/${archive} -C "${ECARGO_VENDOR}/" || die
201 # generate sha256sum of the crate itself as cargo needs this
202 shasum=$(sha256sum "${DISTDIR}"/${archive} | cut -d ' ' -f 1)
203 pkg=$(basename ${archive} .crate)
204 cat <<- EOF > ${ECARGO_VENDOR}/${pkg}/.cargo-checksum.json
205 {
206 "package": "${shasum}",
207 "files": {}
208 }
209 EOF
210 # if this is our target package we need it in ${WORKDIR} too
211 # to make ${S} (and handle any revisions too)
212 if [[ ${P} == ${pkg}* ]]; then
213 tar -xf "${DISTDIR}"/${archive} -C "${WORKDIR}" || die
214 fi
215 eend $?
216 ;;
217 - cargo-snapshot*)
218 - ebegin "Unpacking ${archive}"
219 - mkdir -p "${S}"/target/snapshot
220 - tar -xzf "${DISTDIR}"/${archive} -C "${S}"/target/snapshot --strip-components 2 || die
221 - # cargo's makefile needs this otherwise it will try to
222 - # download it
223 - touch "${S}"/target/snapshot/bin/cargo || die
224 - eend $?
225 - ;;
226 *)
227 unpack ${archive}
228 ;;
229 esac
230 done
231
232 cargo_gen_config
233 }
234
235 # @FUNCTION: cargo_live_src_unpack
236 # @DESCRIPTION:
237 # Runs 'cargo fetch' and vendors downloaded crates for offline use, used in live ebuilds
238 cargo_live_src_unpack() {
239 debug-print-function ${FUNCNAME} "$@"
240
241 [[ "${PV}" == *9999* ]] || die "${FUNCNAME} only allowed in live/9999 ebuilds"
242 [[ "${EBUILD_PHASE}" == unpack ]] || die "${FUNCNAME} only allowed in src_unpack"
243
244 mkdir -p "${S}" || die
245 mkdir -p "${ECARGO_VENDOR}" || die
246 mkdir -p "${ECARGO_HOME}" || die
247
248 local distdir=${PORTAGE_ACTUAL_DISTDIR:-${DISTDIR}}
249 : ${ECARGO_REGISTRY_DIR:=${distdir}/cargo-registry}
250
251 local offline="${ECARGO_OFFLINE:-${EVCS_OFFLINE}}"
252
253 if [[ ! -d ${ECARGO_REGISTRY_DIR} && ! ${offline} ]]; then
254 (
255 addwrite "${ECARGO_REGISTRY_DIR}"
256 mkdir -p "${ECARGO_REGISTRY_DIR}"
257 ) || die "Unable to create ${ECARGO_REGISTRY_DIR}"
258 fi
259
260 if [[ ${offline} ]]; then
261 local subdir
262 for subdir in cache index src; do
263 if [[ ! -d ${ECARGO_REGISTRY_DIR}/registry/${subdir} ]]; then
264 eerror "Networking activity has been disabled via ECARGO_OFFLINE or EVCS_OFFLINE"
265 eerror "However, no valid cargo registry available at ${ECARGO_REGISTRY_DIR}"
266 die "Unable to proceed with ECARGO_OFFLINE/EVCS_OFFLINE."
267 fi
268 done
269 fi
270
271 if [[ ${EVCS_UMASK} ]]; then
272 local saved_umask=$(umask)
273 umask "${EVCS_UMASK}" || die "Bad options to umask: ${EVCS_UMASK}"
274 fi
275
276 pushd "${S}" > /dev/null || die
277
278 # Respect user settings befire cargo_gen_config is called.
279 if [[ ! ${CARGO_TERM_COLOR} ]]; then
280 [[ "${NOCOLOR}" = true || "${NOCOLOR}" = yes ]] && export CARGO_TERM_COLOR=never
281 local unset_color=true
282 fi
283 if [[ ! ${CARGO_TERM_VERBOSE} ]]; then
284 export CARGO_TERM_VERBOSE=true
285 local unset_verbose=true
286 fi
287
288 # Let cargo fetch to system-wide location.
289 # It will keep directory organized by itself.
290 addwrite "${ECARGO_REGISTRY_DIR}"
291 export CARGO_HOME="${ECARGO_REGISTRY_DIR}"
292
293 # Absence of quotes around offline arg is intentional, as cargo bails out if it encounters ''
294 einfo "cargo fetch ${offline:+--offline}"
295 cargo fetch ${offline:+--offline} || die #nowarn
296
297 # Let cargo copy all required crates to "${WORKDIR}" for offline use in later phases.
298 einfo "cargo vendor ${offline:+--offline} ${ECARGO_VENDOR}"
299 cargo vendor ${offline:+--offline} "${ECARGO_VENDOR}" || die #nowarn
300
301 # Users may have git checkouts made by cargo.
302 # While cargo vendors the sources, it still needs git checkout to be present.
303 # Copying full dir is an overkill, so just symlink it.
304 if [[ -d ${ECARGO_REGISTRY_DIR}/git ]]; then
305 ln -sv "${ECARGO_REGISTRY_DIR}/git" "${ECARGO_HOME}/git" || die
306 fi
307
308 popd > /dev/null || die
309
310 # Restore settings if needed.
311 [[ ${unset_color} ]] && unset CARGO_TERM_COLOR
312 [[ ${unset_verbose} ]] && unset CARGO_TERM_VERBOSE
313 if [[ ${saved_umask} ]]; then
314 umask "${saved_umask}" || die
315 fi
316
317 # After following calls, cargo will no longer use ${ECARGO_REGISTRY_DIR} as CARGO_HOME
318 # It will be forced into offline mode to prevent network access.
319 # But since we already vendored crates and symlinked git, it has all it needs to build.
320 unset CARGO_HOME
321 cargo_gen_config
322 }
323
324 # @FUNCTION: cargo_src_configure
325 # @DESCRIPTION:
326 # Configure cargo package features and arguments.
327 # Extra positional arguments supplied to this function
328 # will be passed to cargo in all phases.
329 # Make sure all cargo subcommands support flags passed here.
330 #
331 # Example for package that explicitly builds only 'baz' binary and
332 # enables 'barfeature' and optional 'foo' feature.
333 # will pass '--features barfeature --features foo --bin baz'
334 # in src_{compile,test,install}
335 #
336 # @CODE
337 # src_configure() {
338 # local myfeatures=(
339 # barfeature
340 # $(usev foo)
341 # )
342 # cargo_src_configure --bin baz
343 # }
344 # @CODE
345 #
346 # In some cases crates may need '--no-default-features' option,
347 # as there is no way to disable single feature, except disabling all.
348 # It can be passed directly to cargo_src_configure().
349 cargo_src_configure() {
350 debug-print-function ${FUNCNAME} "$@"
351
352 [[ -z ${myfeatures} ]] && declare -a myfeatures=()
353 local myfeaturestype=$(declare -p myfeatures 2>&-)
354 if [[ "${myfeaturestype}" != "declare -a myfeatures="* ]]; then
355 die "myfeatures must be declared as array"
356 fi
357
358 # transform array from simple feature list
359 # to multiple cargo args:
360 # --features feature1 --features feature2 ...
361 # this format is chosen because 2 other methods of
362 # listing features (space OR comma separated) require
363 # more fiddling with strings we'd like to avoid here.
364 myfeatures=( ${myfeatures[@]/#/--features } )
365
366 readonly ECARGO_ARGS=( ${myfeatures[@]} ${@} ${ECARGO_EXTRA_ARGS} )
367
368 [[ ${ECARGO_ARGS[@]} ]] && einfo "Configured with: ${ECARGO_ARGS[@]}"
369 }
370
371 # @FUNCTION: cargo_src_compile
372 # @DESCRIPTION:
373 # Build the package using cargo build
374 cargo_src_compile() {
375 debug-print-function ${FUNCNAME} "$@"
376
377 [[ ${_CARGO_GEN_CONFIG_HAS_RUN} ]] || \
378 die "FATAL: please call cargo_gen_config before using ${FUNCNAME}"
379
380 tc-export AR CC CXX PKG_CONFIG
381
382 set -- cargo build $(usex debug "" --release) ${ECARGO_ARGS[@]} "$@"
383 einfo "${@}"
384 "${@}" || die "cargo build failed"
385 }
386
387 # @FUNCTION: cargo_src_install
388 # @DESCRIPTION:
389 # Installs the binaries generated by cargo
390 # In come case workspaces need alternative --path parameter
391 # default is '--path ./' if nothing specified.
392 # '--path ./somedir' can be passed directly to cargo_src_install()
393 cargo_src_install() {
394 debug-print-function ${FUNCNAME} "$@"
395
396 [[ ${_CARGO_GEN_CONFIG_HAS_RUN} ]] || \
397 die "FATAL: please call cargo_gen_config before using ${FUNCNAME}"
398
399 set -- cargo install $(has --path ${@} || echo --path ./) \
400 --root "${ED}/usr" \
401 $(usex debug --debug "") \
402 ${ECARGO_ARGS[@]} "$@"
403 einfo "${@}"
404 "${@}" || die "cargo install failed"
405
406 rm -f "${ED}/usr/.crates.toml" || die
407 rm -f "${ED}/usr/.crates2.json" || die
408
409 - [ -d "${S}/man" ] && doman "${S}/man" || return 0
410 + # it turned out to be non-standard dir, so get rid of it future EAPI
411 + # and only run for EAPI=7
412 + # https://bugs.gentoo.org/715890
413 + case ${EAPI:-0} in
414 + 7)
415 + if [ -d "${S}/man" ]; then
416 + doman "${S}/man" || return 0
417 + fi
418 + ;;
419 + esac
420 }
421
422 # @FUNCTION: cargo_src_test
423 # @DESCRIPTION:
424 # Test the package using cargo test
425 cargo_src_test() {
426 debug-print-function ${FUNCNAME} "$@"
427
428 [[ ${_CARGO_GEN_CONFIG_HAS_RUN} ]] || \
429 die "FATAL: please call cargo_gen_config before using ${FUNCNAME}"
430
431 set -- cargo test $(usex debug "" --release) ${ECARGO_ARGS[@]} "$@"
432 einfo "${@}"
433 "${@}" || die "cargo test failed"
434 }
435
436 fi
437 --
438 2.32.0

Replies

Subject Author
[gentoo-dev] [PATCH 2/2] cargo.eclass: make CRATES an eclass variable Georgy Yakovlev <gyakovlev@g.o>