1 # Copyright 1999-2016 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
5 # @ECLASS: cargo.eclass
9 # Doug Goldstein <cardoe@gentoo.org>
10 # @BLURB: common functions and variables for cargo builds
12 if [[ -z ${_CARGO_ECLASS} ]]; then
17 *) die "EAPI=${EAPI:-0} is not supported" ;;
20 EXPORT_FUNCTIONS src_unpack src_compile src_install
24 [[ ${CATEGORY}/${PN} != dev-util/cargo ]] && DEPEND=">=dev-util/cargo-0.13.0"
26 ECARGO_HOME="${WORKDIR}/cargo_home"
27 ECARGO_VENDOR="${ECARGO_HOME}/gentoo"
29 # @FUNCTION: cargo_crate_uris
31 # Generates the URIs to put in SRC_URI to help fetch dependencies.
34 local name version url
36 version="${crate##*-}"
37 url="https://crates.io/api/v1/crates/${name}/${version}/download -> ${crate}.crate"
42 # @FUNCTION: cargo_src_unpack
44 # Unpacks the package and the cargo registry
46 debug-print-function ${FUNCNAME} "$@"
48 mkdir -p "${ECARGO_VENDOR}" || die
49 mkdir -p "${S}" || die
52 for archive in ${A}; do
55 ebegin "Loading ${archive} into Cargo registry"
56 tar -xf "${DISTDIR}"/${archive} -C "${ECARGO_VENDOR}/" || die
57 # generate sha256sum of the crate itself as cargo needs this
58 shasum=$(sha256sum "${DISTDIR}"/${archive} | cut -d ' ' -f 1)
59 pkg=$(basename ${archive} .crate)
60 cat <<- EOF > ${ECARGO_VENDOR}/${pkg}/.cargo-checksum.json
62 "package": "${shasum}",
66 # if this is our target package we need it in ${WORKDIR} too
67 # to make ${S} (and handle any revisions too)
68 if [[ ${P} == ${pkg}* ]]; then
69 tar -xf "${DISTDIR}"/${archive} -C "${WORKDIR}" || die
74 ebegin "Unpacking ${archive}"
75 mkdir -p "${S}"/target/snapshot
76 tar -xzf "${DISTDIR}"/${archive} -C "${S}"/target/snapshot --strip-components 2 || die
77 # cargo's makefile needs this otherwise it will try to
79 touch "${S}"/target/snapshot/bin/cargo || die
91 # @FUNCTION: cargo_gen_config
93 # Generate the $CARGO_HOME/config necessary to use our local registry
95 debug-print-function ${FUNCNAME} "$@"
97 cat <<- EOF > ${ECARGO_HOME}/config
99 directory = "${ECARGO_VENDOR}"
102 replace-with = "gentoo"
103 local-registry = "/nonexistant"
107 # @FUNCTION: cargo_src_compile
109 # Build the package using cargo build
110 cargo_src_compile() {
111 debug-print-function ${FUNCNAME} "$@"
113 export CARGO_HOME="${ECARGO_HOME}"
115 cargo build -v $(usex debug "" --release) \
116 || die "cargo build failed"
119 # @FUNCTION: cargo_src_install
121 # Installs the binaries generated by cargo
122 cargo_src_install() {
123 debug-print-function ${FUNCNAME} "$@"
125 cargo install --root="${D}/usr" $(usex debug --debug "") \
126 || die "cargo install failed"
127 rm -f "${D}/usr/.crates.toml"