1 # Copyright 1999-2016 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: cargo.eclass
8 # Doug Goldstein <cardoe@gentoo.org>
9 # @BLURB: common functions and variables for cargo builds
11 if [[ -z ${_CARGO_ECLASS} ]]; then
16 *) die "EAPI=${EAPI:-0} is not supported" ;;
19 EXPORT_FUNCTIONS src_unpack src_compile src_install
23 [[ ${CATEGORY}/${PN} != dev-util/cargo ]] && DEPEND=">=dev-util/cargo-0.13.0"
25 ECARGO_HOME="${WORKDIR}/cargo_home"
26 ECARGO_VENDOR="${ECARGO_HOME}/gentoo"
28 # @FUNCTION: cargo_crate_uris
30 # Generates the URIs to put in SRC_URI to help fetch dependencies.
33 local name version url
35 version="${crate##*-}"
36 url="https://crates.io/api/v1/crates/${name}/${version}/download -> ${crate}.crate"
41 # @FUNCTION: cargo_src_unpack
43 # Unpacks the package and the cargo registry
45 debug-print-function ${FUNCNAME} "$@"
47 mkdir -p "${ECARGO_VENDOR}" || die
48 mkdir -p "${S}" || die
51 for archive in ${A}; do
54 ebegin "Loading ${archive} into Cargo registry"
55 tar -xf "${DISTDIR}"/${archive} -C "${ECARGO_VENDOR}/" || die
56 # generate sha256sum of the crate itself as cargo needs this
57 shasum=$(sha256sum "${DISTDIR}"/${archive} | cut -d ' ' -f 1)
58 pkg=$(basename ${archive} .crate)
59 cat <<- EOF > ${ECARGO_VENDOR}/${pkg}/.cargo-checksum.json
61 "package": "${shasum}",
65 # if this is our target package we need it in ${WORKDIR} too
66 # to make ${S} (and handle any revisions too)
67 if [[ ${P} == ${pkg}* ]]; then
68 tar -xf "${DISTDIR}"/${archive} -C "${WORKDIR}" || die
73 ebegin "Unpacking ${archive}"
74 mkdir -p "${S}"/target/snapshot
75 tar -xzf "${DISTDIR}"/${archive} -C "${S}"/target/snapshot --strip-components 2 || die
76 # cargo's makefile needs this otherwise it will try to
78 touch "${S}"/target/snapshot/bin/cargo || die
90 # @FUNCTION: cargo_gen_config
92 # Generate the $CARGO_HOME/config necessary to use our local registry
94 debug-print-function ${FUNCNAME} "$@"
96 cat <<- EOF > ${ECARGO_HOME}/config
98 directory = "${ECARGO_VENDOR}"
101 replace-with = "gentoo"
102 local-registry = "/nonexistant"
106 # @FUNCTION: cargo_src_compile
108 # Build the package using cargo build
109 cargo_src_compile() {
110 debug-print-function ${FUNCNAME} "$@"
112 export CARGO_HOME="${ECARGO_HOME}"
114 cargo build -v $(usex debug "" --release) \
115 || die "cargo build failed"
118 # @FUNCTION: cargo_src_install
120 # Installs the binaries generated by cargo
121 cargo_src_install() {
122 debug-print-function ${FUNCNAME} "$@"
124 cargo install --root="${D}/usr" $(usex debug --debug "") \
125 || die "cargo install failed"
126 rm -f "${D}/usr/.crates.toml"
128 [ -d "${S}/man" ] && doman "${S}/man" || return 0