dev-qt/qtopengl: stable 5.14.2 for ppc, bug #719732
[gentoo.git] / eclass / cargo.eclass
1 # Copyright 1999-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: cargo.eclass
5 # @MAINTAINER:
6 # rust@gentoo.org
7 # @AUTHOR:
8 # Doug Goldstein <cardoe@gentoo.org>
9 # @SUPPORTED_EAPIS: 6 7
10 # @BLURB: common functions and variables for cargo builds
11
12 if [[ -z ${_CARGO_ECLASS} ]]; then
13 _CARGO_ECLASS=1
14
15 # we need this for 'cargo vendor' subcommand and net.offline config knob
16 RUST_DEPEND=">=virtual/rust-1.37.0"
17
18 case ${EAPI} in
19         6) DEPEND="${RUST_DEPEND}";;
20         7) BDEPEND="${RUST_DEPEND}";;
21         *) die "EAPI=${EAPI:-0} is not supported" ;;
22 esac
23
24 inherit multiprocessing
25
26 EXPORT_FUNCTIONS src_unpack src_compile src_install src_test
27
28 IUSE="${IUSE} debug"
29
30 ECARGO_HOME="${WORKDIR}/cargo_home"
31 ECARGO_VENDOR="${ECARGO_HOME}/gentoo"
32
33 # @ECLASS-VARIABLE: CARGO_INSTALL_PATH
34 # @DESCRIPTION:
35 # Allows overriding the default cwd to run cargo install from
36 : ${CARGO_INSTALL_PATH:=.}
37
38 # @FUNCTION: cargo_crate_uris
39 # @DESCRIPTION:
40 # Generates the URIs to put in SRC_URI to help fetch dependencies.
41 cargo_crate_uris() {
42         local -r regex='^([a-zA-Z0-9_\-]+)-([0-9]+\.[0-9]+\.[0-9]+.*)$'
43         local crate
44         for crate in "$@"; do
45                 local name version url
46                 [[ $crate =~ $regex ]] || die "Could not parse name and version from crate: $crate"
47                 name="${BASH_REMATCH[1]}"
48                 version="${BASH_REMATCH[2]}"
49                 url="https://crates.io/api/v1/crates/${name}/${version}/download -> ${crate}.crate"
50                 echo "${url}"
51         done
52 }
53
54 # @FUNCTION: cargo_src_unpack
55 # @DESCRIPTION:
56 # Unpacks the package and the cargo registry
57 cargo_src_unpack() {
58         debug-print-function ${FUNCNAME} "$@"
59
60         mkdir -p "${ECARGO_VENDOR}" || die
61         mkdir -p "${S}" || die
62
63         local archive shasum pkg
64         for archive in ${A}; do
65                 case "${archive}" in
66                         *.crate)
67                                 ebegin "Loading ${archive} into Cargo registry"
68                                 tar -xf "${DISTDIR}"/${archive} -C "${ECARGO_VENDOR}/" || die
69                                 # generate sha256sum of the crate itself as cargo needs this
70                                 shasum=$(sha256sum "${DISTDIR}"/${archive} | cut -d ' ' -f 1)
71                                 pkg=$(basename ${archive} .crate)
72                                 cat <<- EOF > ${ECARGO_VENDOR}/${pkg}/.cargo-checksum.json
73                                 {
74                                         "package": "${shasum}",
75                                         "files": {}
76                                 }
77                                 EOF
78                                 # if this is our target package we need it in ${WORKDIR} too
79                                 # to make ${S} (and handle any revisions too)
80                                 if [[ ${P} == ${pkg}* ]]; then
81                                         tar -xf "${DISTDIR}"/${archive} -C "${WORKDIR}" || die
82                                 fi
83                                 eend $?
84                                 ;;
85                         cargo-snapshot*)
86                                 ebegin "Unpacking ${archive}"
87                                 mkdir -p "${S}"/target/snapshot
88                                 tar -xzf "${DISTDIR}"/${archive} -C "${S}"/target/snapshot --strip-components 2 || die
89                                 # cargo's makefile needs this otherwise it will try to
90                                 # download it
91                                 touch "${S}"/target/snapshot/bin/cargo || die
92                                 eend $?
93                                 ;;
94                         *)
95                                 unpack ${archive}
96                                 ;;
97                 esac
98         done
99
100         cargo_gen_config
101 }
102
103 # @FUNCTION: cargo_live_src_unpack
104 # @DESCRIPTION:
105 # Runs 'cargo fetch' and vendors downloaded crates for offline use, used in live ebuilds
106
107 cargo_live_src_unpack() {
108         debug-print-function ${FUNCNAME} "$@"
109
110         [[ "${PV}" == *9999* ]] || die "${FUNCNAME} only allowed in live/9999 ebuilds"
111         [[ "${EBUILD_PHASE}" == unpack ]] || die "${FUNCNAME} only allowed in src_unpack"
112
113         mkdir -p "${S}" || die
114
115         pushd "${S}" > /dev/null || die
116         CARGO_HOME="${ECARGO_HOME}" cargo fetch || die
117         CARGO_HOME="${ECARGO_HOME}" cargo vendor "${ECARGO_VENDOR}" || die
118         popd > /dev/null || die
119
120         cargo_gen_config
121 }
122
123 # @FUNCTION: cargo_gen_config
124 # @DESCRIPTION:
125 # Generate the $CARGO_HOME/config necessary to use our local registry and settings.
126 # Cargo can also be configured through environment variables in addition to the TOML syntax below.
127 # For each configuration key below of the form foo.bar the environment variable CARGO_FOO_BAR
128 # can also be used to define the value.
129 # Environment variables will take precedent over TOML configuration,
130 # and currently only integer, boolean, and string keys are supported.
131 # For example the build.jobs key can also be defined by CARGO_BUILD_JOBS.
132 # Or setting CARGO_TERM_VERBOSE=false in make.conf will make build quieter.
133 cargo_gen_config() {
134         debug-print-function ${FUNCNAME} "$@"
135
136         cat <<- EOF > "${ECARGO_HOME}/config"
137         [source.gentoo]
138         directory = "${ECARGO_VENDOR}"
139
140         [source.crates-io]
141         replace-with = "gentoo"
142         local-registry = "/nonexistant"
143
144         [net]
145         offline = true
146
147         [build]
148         jobs = $(makeopts_jobs)
149
150         [term]
151         verbose = true
152         EOF
153         # honor NOCOLOR setting
154         [[ "${NOCOLOR}" = true || "${NOCOLOR}" = yes ]] && echo "color = 'never'" >> "${ECARGO_HOME}/config"
155 }
156
157 # @FUNCTION: cargo_src_compile
158 # @DESCRIPTION:
159 # Build the package using cargo build
160 cargo_src_compile() {
161         debug-print-function ${FUNCNAME} "$@"
162
163         export CARGO_HOME="${ECARGO_HOME}"
164
165         cargo build $(usex debug "" --release) "$@" \
166                 || die "cargo build failed"
167 }
168
169 # @FUNCTION: cargo_src_install
170 # @DESCRIPTION:
171 # Installs the binaries generated by cargo
172 cargo_src_install() {
173         debug-print-function ${FUNCNAME} "$@"
174
175         cargo install --path ${CARGO_INSTALL_PATH} \
176                 --root="${ED}/usr" $(usex debug --debug "") "$@" \
177                 || die "cargo install failed"
178         rm -f "${ED}/usr/.crates.toml"
179         rm -f "${ED}/usr/.crates2.json"
180
181         [ -d "${S}/man" ] && doman "${S}/man" || return 0
182 }
183
184 # @FUNCTION: cargo_src_test
185 # @DESCRIPTION:
186 # Test the package using cargo test
187 cargo_src_test() {
188         debug-print-function ${FUNCNAME} "$@"
189
190         cargo test $(usex debug "" --release) "$@" \
191                 || die "cargo test failed"
192 }
193
194 fi