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.
34 local name version url pretag
36 version="${crate##*-}"
38 if [[ $version =~ $pretag ]]; then
39 version="${name##*-}-${version}"
42 url="https://crates.io/api/v1/crates/${name}/${version}/download -> ${crate}.crate"
47 # @FUNCTION: cargo_src_unpack
49 # Unpacks the package and the cargo registry
51 debug-print-function ${FUNCNAME} "$@"
53 mkdir -p "${ECARGO_VENDOR}" || die
54 mkdir -p "${S}" || die
56 local archive shasum pkg
57 for archive in ${A}; do
60 ebegin "Loading ${archive} into Cargo registry"
61 tar -xf "${DISTDIR}"/${archive} -C "${ECARGO_VENDOR}/" || die
62 # generate sha256sum of the crate itself as cargo needs this
63 shasum=$(sha256sum "${DISTDIR}"/${archive} | cut -d ' ' -f 1)
64 pkg=$(basename ${archive} .crate)
65 cat <<- EOF > ${ECARGO_VENDOR}/${pkg}/.cargo-checksum.json
67 "package": "${shasum}",
71 # if this is our target package we need it in ${WORKDIR} too
72 # to make ${S} (and handle any revisions too)
73 if [[ ${P} == ${pkg}* ]]; then
74 tar -xf "${DISTDIR}"/${archive} -C "${WORKDIR}" || die
79 ebegin "Unpacking ${archive}"
80 mkdir -p "${S}"/target/snapshot
81 tar -xzf "${DISTDIR}"/${archive} -C "${S}"/target/snapshot --strip-components 2 || die
82 # cargo's makefile needs this otherwise it will try to
84 touch "${S}"/target/snapshot/bin/cargo || die
96 # @FUNCTION: cargo_gen_config
98 # Generate the $CARGO_HOME/config necessary to use our local registry
100 debug-print-function ${FUNCNAME} "$@"
102 cat <<- EOF > "${ECARGO_HOME}/config"
104 directory = "${ECARGO_VENDOR}"
107 replace-with = "gentoo"
108 local-registry = "/nonexistant"
112 # @FUNCTION: cargo_src_compile
114 # Build the package using cargo build
115 cargo_src_compile() {
116 debug-print-function ${FUNCNAME} "$@"
118 export CARGO_HOME="${ECARGO_HOME}"
120 cargo build -v $(usex debug "" --release) \
121 || die "cargo build failed"
124 # @FUNCTION: cargo_src_install
126 # Installs the binaries generated by cargo
127 cargo_src_install() {
128 debug-print-function ${FUNCNAME} "$@"
130 cargo install --root="${D}/usr" $(usex debug --debug "") \
131 || die "cargo install failed"
132 rm -f "${D}/usr/.crates.toml"
134 [ -d "${S}/man" ] && doman "${S}/man" || return 0