1 # Copyright 1999-2019 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 if [[ ${PV} == *9999* ]]; then
16 # we need at least this for cargo vendor subommand
17 CARGO_DEPEND=">=virtual/cargo-1.37.0"
19 CARGO_DEPEND="virtual/cargo"
23 6) DEPEND="${CARGO_DEPEND}";;
24 7) BDEPEND="${CARGO_DEPEND}";;
25 *) die "EAPI=${EAPI:-0} is not supported" ;;
28 inherit multiprocessing
30 EXPORT_FUNCTIONS src_unpack src_compile src_install src_test
34 ECARGO_HOME="${WORKDIR}/cargo_home"
35 ECARGO_VENDOR="${ECARGO_HOME}/gentoo"
37 # @ECLASS-VARIABLE: CARGO_INSTALL_PATH
39 # Allows overriding the default cwd to run cargo install from
40 : ${CARGO_INSTALL_PATH:=.}
42 # @FUNCTION: cargo_crate_uris
44 # Generates the URIs to put in SRC_URI to help fetch dependencies.
48 local name version url pretag
50 version="${crate##*-}"
52 if [[ $version =~ $pretag ]]; then
53 version="${name##*-}-${version}"
56 url="https://crates.io/api/v1/crates/${name}/${version}/download -> ${crate}.crate"
61 # @FUNCTION: cargo_src_unpack
63 # Unpacks the package and the cargo registry
65 debug-print-function ${FUNCNAME} "$@"
67 mkdir -p "${ECARGO_VENDOR}" || die
68 mkdir -p "${S}" || die
70 local archive shasum pkg
71 for archive in ${A}; do
74 ebegin "Loading ${archive} into Cargo registry"
75 tar -xf "${DISTDIR}"/${archive} -C "${ECARGO_VENDOR}/" || die
76 # generate sha256sum of the crate itself as cargo needs this
77 shasum=$(sha256sum "${DISTDIR}"/${archive} | cut -d ' ' -f 1)
78 pkg=$(basename ${archive} .crate)
79 cat <<- EOF > ${ECARGO_VENDOR}/${pkg}/.cargo-checksum.json
81 "package": "${shasum}",
85 # if this is our target package we need it in ${WORKDIR} too
86 # to make ${S} (and handle any revisions too)
87 if [[ ${P} == ${pkg}* ]]; then
88 tar -xf "${DISTDIR}"/${archive} -C "${WORKDIR}" || die
93 ebegin "Unpacking ${archive}"
94 mkdir -p "${S}"/target/snapshot
95 tar -xzf "${DISTDIR}"/${archive} -C "${S}"/target/snapshot --strip-components 2 || die
96 # cargo's makefile needs this otherwise it will try to
98 touch "${S}"/target/snapshot/bin/cargo || die
110 # @FUNCTION: cargo_live_src_unpack
112 # Runs 'cargo fetch' and vendors downloaded crates for offline use, used in live ebuilds
114 cargo_live_src_unpack() {
115 debug-print-function ${FUNCNAME} "$@"
117 [[ "${PV}" == *9999* ]] || die "${FUNCNAME} only allowed in live/9999 ebuilds"
118 [[ "${EBUILD_PHASE}" == unpack ]] || die "${FUNCNAME} only allowed in src_unpack"
120 mkdir -p "${S}" || die
122 pushd "${S}" > /dev/null || die
123 CARGO_HOME="${ECARGO_HOME}" cargo fetch || die
124 CARGO_HOME="${ECARGO_HOME}" cargo vendor "${ECARGO_VENDOR}" || die
125 popd > /dev/null || die
130 # @FUNCTION: cargo_gen_config
132 # Generate the $CARGO_HOME/config necessary to use our local registry
134 debug-print-function ${FUNCNAME} "$@"
136 cat <<- EOF > "${ECARGO_HOME}/config"
138 directory = "${ECARGO_VENDOR}"
141 replace-with = "gentoo"
142 local-registry = "/nonexistant"
146 # @FUNCTION: cargo_src_compile
148 # Build the package using cargo build
149 cargo_src_compile() {
150 debug-print-function ${FUNCNAME} "$@"
152 export CARGO_HOME="${ECARGO_HOME}"
154 cargo build -vv -j $(makeopts_jobs) $(usex debug "" --release) "$@" \
155 || die "cargo build failed"
158 # @FUNCTION: cargo_src_install
160 # Installs the binaries generated by cargo
161 cargo_src_install() {
162 debug-print-function ${FUNCNAME} "$@"
164 cargo install -vv -j $(makeopts_jobs) --path ${CARGO_INSTALL_PATH} \
165 --root="${ED}/usr" $(usex debug --debug "") "$@" \
166 || die "cargo install failed"
167 rm -f "${ED}/usr/.crates.toml"
169 [ -d "${S}/man" ] && doman "${S}/man" || return 0
172 # @FUNCTION: cargo_src_test
174 # Test the package using cargo test
176 debug-print-function ${FUNCNAME} "$@"
178 cargo test -vv -j $(makeopts_jobs) $(usex debug "" --release) "$@" \
179 || die "cargo test failed"