cargo.eclass: make cargo verbosity configurable
[gentoo.git] / eclass / cargo.eclass
1 # Copyright 1999-2019 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 CARGO_DEPEND=">=virtual/cargo-1.37.0"
17
18 case ${EAPI} in
19         6) DEPEND="${CARGO_DEPEND}";;
20         7) BDEPEND="${CARGO_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 crate
43         for crate in "$@"; do
44                 local name version url pretag
45                 name="${crate%-*}"
46                 version="${crate##*-}"
47                 pretag="^[a-zA-Z]+"
48                 if [[ $version =~ $pretag ]]; then
49                         version="${name##*-}-${version}"
50                         name="${name%-*}"
51                 fi
52                 url="https://crates.io/api/v1/crates/${name}/${version}/download -> ${crate}.crate"
53                 echo "${url}"
54         done
55 }
56
57 # @FUNCTION: cargo_src_unpack
58 # @DESCRIPTION:
59 # Unpacks the package and the cargo registry
60 cargo_src_unpack() {
61         debug-print-function ${FUNCNAME} "$@"
62
63         mkdir -p "${ECARGO_VENDOR}" || die
64         mkdir -p "${S}" || die
65
66         local archive shasum pkg
67         for archive in ${A}; do
68                 case "${archive}" in
69                         *.crate)
70                                 ebegin "Loading ${archive} into Cargo registry"
71                                 tar -xf "${DISTDIR}"/${archive} -C "${ECARGO_VENDOR}/" || die
72                                 # generate sha256sum of the crate itself as cargo needs this
73                                 shasum=$(sha256sum "${DISTDIR}"/${archive} | cut -d ' ' -f 1)
74                                 pkg=$(basename ${archive} .crate)
75                                 cat <<- EOF > ${ECARGO_VENDOR}/${pkg}/.cargo-checksum.json
76                                 {
77                                         "package": "${shasum}",
78                                         "files": {}
79                                 }
80                                 EOF
81                                 # if this is our target package we need it in ${WORKDIR} too
82                                 # to make ${S} (and handle any revisions too)
83                                 if [[ ${P} == ${pkg}* ]]; then
84                                         tar -xf "${DISTDIR}"/${archive} -C "${WORKDIR}" || die
85                                 fi
86                                 eend $?
87                                 ;;
88                         cargo-snapshot*)
89                                 ebegin "Unpacking ${archive}"
90                                 mkdir -p "${S}"/target/snapshot
91                                 tar -xzf "${DISTDIR}"/${archive} -C "${S}"/target/snapshot --strip-components 2 || die
92                                 # cargo's makefile needs this otherwise it will try to
93                                 # download it
94                                 touch "${S}"/target/snapshot/bin/cargo || die
95                                 eend $?
96                                 ;;
97                         *)
98                                 unpack ${archive}
99                                 ;;
100                 esac
101         done
102
103         cargo_gen_config
104 }
105
106 # @FUNCTION: cargo_live_src_unpack
107 # @DESCRIPTION:
108 # Runs 'cargo fetch' and vendors downloaded crates for offline use, used in live ebuilds
109
110 cargo_live_src_unpack() {
111         debug-print-function ${FUNCNAME} "$@"
112
113         [[ "${PV}" == *9999* ]] || die "${FUNCNAME} only allowed in live/9999 ebuilds"
114         [[ "${EBUILD_PHASE}" == unpack ]] || die "${FUNCNAME} only allowed in src_unpack"
115
116         mkdir -p "${S}" || die
117
118         pushd "${S}" > /dev/null || die
119         CARGO_HOME="${ECARGO_HOME}" cargo fetch || die
120         CARGO_HOME="${ECARGO_HOME}" cargo vendor "${ECARGO_VENDOR}" || die
121         popd > /dev/null || die
122
123         cargo_gen_config
124 }
125
126 # @FUNCTION: cargo_gen_config
127 # @DESCRIPTION:
128 # Generate the $CARGO_HOME/config necessary to use our local registry and settings.
129 # Cargo can also be configured through environment variables in addition to the TOML syntax below.
130 # For each configuration key below of the form foo.bar the environment variable CARGO_FOO_BAR
131 # can also be used to define the value.
132 # Environment variables will take precedent over TOML configuration,
133 # and currently only integer, boolean, and string keys are supported.
134 # For example the build.jobs key can also be defined by CARGO_BUILD_JOBS.
135 # Or setting CARGO_TERM_VERBOSE=false in make.conf will make build quieter.
136 cargo_gen_config() {
137         debug-print-function ${FUNCNAME} "$@"
138
139         cat <<- EOF > "${ECARGO_HOME}/config"
140         [source.gentoo]
141         directory = "${ECARGO_VENDOR}"
142
143         [source.crates-io]
144         replace-with = "gentoo"
145         local-registry = "/nonexistant"
146
147         [net]
148         offline = true
149
150         [build]
151         jobs = $(makeopts_jobs)
152
153         [term]
154         verbose = true
155         EOF
156 }
157
158 # @FUNCTION: cargo_src_compile
159 # @DESCRIPTION:
160 # Build the package using cargo build
161 cargo_src_compile() {
162         debug-print-function ${FUNCNAME} "$@"
163
164         export CARGO_HOME="${ECARGO_HOME}"
165
166         cargo build $(usex debug "" --release) "$@" \
167                 || die "cargo build failed"
168 }
169
170 # @FUNCTION: cargo_src_install
171 # @DESCRIPTION:
172 # Installs the binaries generated by cargo
173 cargo_src_install() {
174         debug-print-function ${FUNCNAME} "$@"
175
176         cargo install --path ${CARGO_INSTALL_PATH} \
177                 --root="${ED}/usr" $(usex debug --debug "") "$@" \
178                 || die "cargo install failed"
179         rm -f "${ED}/usr/.crates.toml"
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