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 # @FUNCTION: cargo_crate_uris
39 # Generates the URIs to put in SRC_URI to help fetch dependencies.
43 local name version url pretag
45 version="${crate##*-}"
47 if [[ $version =~ $pretag ]]; then
48 version="${name##*-}-${version}"
51 url="https://crates.io/api/v1/crates/${name}/${version}/download -> ${crate}.crate"
56 # @FUNCTION: cargo_src_unpack
58 # Unpacks the package and the cargo registry
60 debug-print-function ${FUNCNAME} "$@"
62 mkdir -p "${ECARGO_VENDOR}" || die
63 mkdir -p "${S}" || die
65 local archive shasum pkg
66 for archive in ${A}; do
69 ebegin "Loading ${archive} into Cargo registry"
70 tar -xf "${DISTDIR}"/${archive} -C "${ECARGO_VENDOR}/" || die
71 # generate sha256sum of the crate itself as cargo needs this
72 shasum=$(sha256sum "${DISTDIR}"/${archive} | cut -d ' ' -f 1)
73 pkg=$(basename ${archive} .crate)
74 cat <<- EOF > ${ECARGO_VENDOR}/${pkg}/.cargo-checksum.json
76 "package": "${shasum}",
80 # if this is our target package we need it in ${WORKDIR} too
81 # to make ${S} (and handle any revisions too)
82 if [[ ${P} == ${pkg}* ]]; then
83 tar -xf "${DISTDIR}"/${archive} -C "${WORKDIR}" || die
88 ebegin "Unpacking ${archive}"
89 mkdir -p "${S}"/target/snapshot
90 tar -xzf "${DISTDIR}"/${archive} -C "${S}"/target/snapshot --strip-components 2 || die
91 # cargo's makefile needs this otherwise it will try to
93 touch "${S}"/target/snapshot/bin/cargo || die
105 # @FUNCTION: cargo_live_src_unpack
107 # Runs 'cargo fetch' and vendors downloaded crates for offline use, used in live ebuilds
109 cargo_live_src_unpack() {
110 debug-print-function ${FUNCNAME} "$@"
112 [[ "${PV}" == *9999* ]] || die "${FUNCNAME} only allowed in live/9999 ebuilds"
113 [[ "${EBUILD_PHASE}" == unpack ]] || die "${FUNCNAME} only allowed in src_unpack"
115 mkdir -p "${S}" || die
117 pushd "${S}" > /dev/null || die
118 CARGO_HOME="${ECARGO_HOME}" cargo fetch || die
119 CARGO_HOME="${ECARGO_HOME}" cargo vendor "${ECARGO_VENDOR}" || die
120 popd > /dev/null || die
125 # @FUNCTION: cargo_gen_config
127 # Generate the $CARGO_HOME/config necessary to use our local registry
129 debug-print-function ${FUNCNAME} "$@"
131 cat <<- EOF > "${ECARGO_HOME}/config"
133 directory = "${ECARGO_VENDOR}"
136 replace-with = "gentoo"
137 local-registry = "/nonexistant"
141 # @FUNCTION: cargo_src_compile
143 # Build the package using cargo build
144 cargo_src_compile() {
145 debug-print-function ${FUNCNAME} "$@"
147 export CARGO_HOME="${ECARGO_HOME}"
149 cargo build -j $(makeopts_jobs) $(usex debug "" --release) "$@" \
150 || die "cargo build failed"
153 # @FUNCTION: cargo_src_install
155 # Installs the binaries generated by cargo
156 cargo_src_install() {
157 debug-print-function ${FUNCNAME} "$@"
159 cargo install -j $(makeopts_jobs) --root="${D}/usr" $(usex debug --debug "") "$@" \
160 || die "cargo install failed"
161 rm -f "${D}/usr/.crates.toml"
163 [ -d "${S}/man" ] && doman "${S}/man" || return 0
166 # @FUNCTION: cargo_src_test
168 # Test the package using cargo test
170 debug-print-function ${FUNCNAME} "$@"
172 cargo test -j $(makeopts_jobs) $(usex debug "" --release) "$@" \
173 || die "cargo test failed"