1 # Copyright 1999-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: cargo.eclass
8 # Doug Goldstein <cardoe@gentoo.org>
9 # @SUPPORTED_EAPIS: 6 7
10 # @BLURB: common functions and variables for cargo builds
12 if [[ -z ${_CARGO_ECLASS} ]]; then
15 # we need this for 'cargo vendor' subcommand and net.offline config knob
16 RUST_DEPEND=">=virtual/rust-1.37.0"
19 6) DEPEND="${RUST_DEPEND}";;
20 7) BDEPEND="${RUST_DEPEND}";;
21 *) die "EAPI=${EAPI:-0} is not supported" ;;
24 inherit multiprocessing
26 EXPORT_FUNCTIONS src_unpack src_compile src_install src_test
30 ECARGO_HOME="${WORKDIR}/cargo_home"
31 ECARGO_VENDOR="${ECARGO_HOME}/gentoo"
33 # @ECLASS-VARIABLE: CARGO_INSTALL_PATH
35 # Allows overriding the default cwd to run cargo install from
36 : ${CARGO_INSTALL_PATH:=.}
38 # @FUNCTION: cargo_crate_uris
40 # Generates the URIs to put in SRC_URI to help fetch dependencies.
42 local -r regex='^([a-zA-Z0-9_\-]+)-([0-9]+\.[0-9]+\.[0-9]+.*)$'
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"
54 # @FUNCTION: cargo_src_unpack
56 # Unpacks the package and the cargo registry
58 debug-print-function ${FUNCNAME} "$@"
60 mkdir -p "${ECARGO_VENDOR}" || die
61 mkdir -p "${S}" || die
63 local archive shasum pkg
64 for archive in ${A}; do
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
74 "package": "${shasum}",
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
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
91 touch "${S}"/target/snapshot/bin/cargo || die
103 # @FUNCTION: cargo_live_src_unpack
105 # Runs 'cargo fetch' and vendors downloaded crates for offline use, used in live ebuilds
107 cargo_live_src_unpack() {
108 debug-print-function ${FUNCNAME} "$@"
110 [[ "${PV}" == *9999* ]] || die "${FUNCNAME} only allowed in live/9999 ebuilds"
111 [[ "${EBUILD_PHASE}" == unpack ]] || die "${FUNCNAME} only allowed in src_unpack"
113 mkdir -p "${S}" || die
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
123 # @FUNCTION: cargo_gen_config
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.
134 debug-print-function ${FUNCNAME} "$@"
136 cat <<- EOF > "${ECARGO_HOME}/config"
138 directory = "${ECARGO_VENDOR}"
141 replace-with = "gentoo"
142 local-registry = "/nonexistant"
148 jobs = $(makeopts_jobs)
153 # honor NOCOLOR setting
154 [[ "${NOCOLOR}" = true || "${NOCOLOR}" = yes ]] && echo "color = 'never'" >> "${ECARGO_HOME}/config"
157 # @FUNCTION: cargo_src_compile
159 # Build the package using cargo build
160 cargo_src_compile() {
161 debug-print-function ${FUNCNAME} "$@"
163 export CARGO_HOME="${ECARGO_HOME}"
165 cargo build $(usex debug "" --release) "$@" \
166 || die "cargo build failed"
169 # @FUNCTION: cargo_src_install
171 # Installs the binaries generated by cargo
172 cargo_src_install() {
173 debug-print-function ${FUNCNAME} "$@"
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"
181 [ -d "${S}/man" ] && doman "${S}/man" || return 0
184 # @FUNCTION: cargo_src_test
186 # Test the package using cargo test
188 debug-print-function ${FUNCNAME} "$@"
190 cargo test $(usex debug "" --release) "$@" \
191 || die "cargo test failed"