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 inherit multiprocessing
21 EXPORT_FUNCTIONS src_unpack src_compile src_install
25 [[ ${CATEGORY}/${PN} != dev-util/cargo ]] && DEPEND=">=dev-util/cargo-0.13.0"
27 ECARGO_HOME="${WORKDIR}/cargo_home"
28 ECARGO_VENDOR="${ECARGO_HOME}/gentoo"
30 # @FUNCTION: cargo_crate_uris
32 # Generates the URIs to put in SRC_URI to help fetch dependencies.
36 local name version url pretag
38 version="${crate##*-}"
40 if [[ $version =~ $pretag ]]; then
41 version="${name##*-}-${version}"
44 url="https://crates.io/api/v1/crates/${name}/${version}/download -> ${crate}.crate"
49 # @FUNCTION: cargo_src_unpack
51 # Unpacks the package and the cargo registry
53 debug-print-function ${FUNCNAME} "$@"
55 mkdir -p "${ECARGO_VENDOR}" || die
56 mkdir -p "${S}" || die
58 local archive shasum pkg
59 for archive in ${A}; do
62 ebegin "Loading ${archive} into Cargo registry"
63 tar -xf "${DISTDIR}"/${archive} -C "${ECARGO_VENDOR}/" || die
64 # generate sha256sum of the crate itself as cargo needs this
65 shasum=$(sha256sum "${DISTDIR}"/${archive} | cut -d ' ' -f 1)
66 pkg=$(basename ${archive} .crate)
67 cat <<- EOF > ${ECARGO_VENDOR}/${pkg}/.cargo-checksum.json
69 "package": "${shasum}",
73 # if this is our target package we need it in ${WORKDIR} too
74 # to make ${S} (and handle any revisions too)
75 if [[ ${P} == ${pkg}* ]]; then
76 tar -xf "${DISTDIR}"/${archive} -C "${WORKDIR}" || die
81 ebegin "Unpacking ${archive}"
82 mkdir -p "${S}"/target/snapshot
83 tar -xzf "${DISTDIR}"/${archive} -C "${S}"/target/snapshot --strip-components 2 || die
84 # cargo's makefile needs this otherwise it will try to
86 touch "${S}"/target/snapshot/bin/cargo || die
98 # @FUNCTION: cargo_gen_config
100 # Generate the $CARGO_HOME/config necessary to use our local registry
102 debug-print-function ${FUNCNAME} "$@"
104 cat <<- EOF > "${ECARGO_HOME}/config"
106 directory = "${ECARGO_VENDOR}"
109 replace-with = "gentoo"
110 local-registry = "/nonexistant"
114 # @FUNCTION: cargo_src_compile
116 # Build the package using cargo build
117 cargo_src_compile() {
118 debug-print-function ${FUNCNAME} "$@"
120 export CARGO_HOME="${ECARGO_HOME}"
122 cargo build -v -j $(makeopts_jobs) $(usex debug "" --release) \
123 || die "cargo build failed"
126 # @FUNCTION: cargo_src_install
128 # Installs the binaries generated by cargo
129 cargo_src_install() {
130 debug-print-function ${FUNCNAME} "$@"
132 cargo install -j $(makeopts_jobs) --root="${D}/usr" $(usex debug --debug "") \
133 || die "cargo install failed"
134 rm -f "${D}/usr/.crates.toml"
136 [ -d "${S}/man" ] && doman "${S}/man" || return 0