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
16 [[ ${CATEGORY}/${PN} != dev-util/cargo ]] && CARGO_DEPEND="virtual/cargo"
19 6) DEPEND="${CARGO_DEPEND}";;
20 7) BDEPEND="${CARGO_DEPEND}";;
21 *) die "EAPI=${EAPI:-0} is not supported" ;;
24 inherit multiprocessing
26 EXPORT_FUNCTIONS src_unpack src_compile src_install src_test
30 ECARGO_HOME="${WORKDIR}/cargo_home"
31 ECARGO_VENDOR="${ECARGO_HOME}/gentoo"
33 # @FUNCTION: cargo_crate_uris
35 # Generates the URIs to put in SRC_URI to help fetch dependencies.
39 local name version url pretag
41 version="${crate##*-}"
43 if [[ $version =~ $pretag ]]; then
44 version="${name##*-}-${version}"
47 url="https://crates.io/api/v1/crates/${name}/${version}/download -> ${crate}.crate"
52 # @FUNCTION: cargo_src_unpack
54 # Unpacks the package and the cargo registry
56 debug-print-function ${FUNCNAME} "$@"
58 mkdir -p "${ECARGO_VENDOR}" || die
59 mkdir -p "${S}" || die
61 local archive shasum pkg
62 for archive in ${A}; do
65 ebegin "Loading ${archive} into Cargo registry"
66 tar -xf "${DISTDIR}"/${archive} -C "${ECARGO_VENDOR}/" || die
67 # generate sha256sum of the crate itself as cargo needs this
68 shasum=$(sha256sum "${DISTDIR}"/${archive} | cut -d ' ' -f 1)
69 pkg=$(basename ${archive} .crate)
70 cat <<- EOF > ${ECARGO_VENDOR}/${pkg}/.cargo-checksum.json
72 "package": "${shasum}",
76 # if this is our target package we need it in ${WORKDIR} too
77 # to make ${S} (and handle any revisions too)
78 if [[ ${P} == ${pkg}* ]]; then
79 tar -xf "${DISTDIR}"/${archive} -C "${WORKDIR}" || die
84 ebegin "Unpacking ${archive}"
85 mkdir -p "${S}"/target/snapshot
86 tar -xzf "${DISTDIR}"/${archive} -C "${S}"/target/snapshot --strip-components 2 || die
87 # cargo's makefile needs this otherwise it will try to
89 touch "${S}"/target/snapshot/bin/cargo || die
101 # @FUNCTION: cargo_gen_config
103 # Generate the $CARGO_HOME/config necessary to use our local registry
105 debug-print-function ${FUNCNAME} "$@"
107 cat <<- EOF > "${ECARGO_HOME}/config"
109 directory = "${ECARGO_VENDOR}"
112 replace-with = "gentoo"
113 local-registry = "/nonexistant"
117 # @FUNCTION: cargo_src_compile
119 # Build the package using cargo build
120 cargo_src_compile() {
121 debug-print-function ${FUNCNAME} "$@"
123 export CARGO_HOME="${ECARGO_HOME}"
125 cargo build -j $(makeopts_jobs) $(usex debug "" --release) "$@" \
126 || die "cargo build failed"
129 # @FUNCTION: cargo_src_install
131 # Installs the binaries generated by cargo
132 cargo_src_install() {
133 debug-print-function ${FUNCNAME} "$@"
135 cargo install -j $(makeopts_jobs) --root="${D}/usr" $(usex debug --debug "") "$@" \
136 || die "cargo install failed"
137 rm -f "${D}/usr/.crates.toml"
139 [ -d "${S}/man" ] && doman "${S}/man" || return 0
142 # @FUNCTION: cargo_src_test
144 # Test the package using cargo test
146 debug-print-function ${FUNCNAME} "$@"
148 cargo test -j $(makeopts_jobs) $(usex debug "" --release) "$@" \
149 || die "cargo test failed"