eclass/cargo: ensure people have a good cargo depend
[gentoo.git] / eclass / cargo.eclass
1 # Copyright 1999-2016 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Id$
4
5 # @ECLASS: cargo.eclass
6 # @MAINTAINER:
7 # rust@gentoo.org
8 # @AUTHOR:
9 # Doug Goldstein <cardoe@gentoo.org>
10 # @BLURB: common functions and variables for cargo builds
11
12 if [[ -z ${_CARGO_ECLASS} ]]; then
13 _CARGO_ECLASS=1
14
15 case ${EAPI} in
16         6) : ;;
17         *) die "EAPI=${EAPI:-0} is not supported" ;;
18 esac
19
20 EXPORT_FUNCTIONS src_unpack src_compile src_install
21
22 IUSE="${IUSE} debug"
23
24 [[ ${CATEGORY}/${PN} != dev-util/cargo ]] && DEPEND=">=dev-util/cargo-0.13.0"
25
26 ECARGO_HOME="${WORKDIR}/cargo_home"
27 ECARGO_VENDOR="${ECARGO_HOME}/gentoo"
28
29 # @FUNCTION: cargo_crate_uris
30 # @DESCRIPTION:
31 # Generates the URIs to put in SRC_URI to help fetch dependencies.
32 cargo_crate_uris() {
33         for crate in $*; do
34                 local name version url
35                 name="${crate%-*}"
36                 version="${crate##*-}"
37                 url="https://crates.io/api/v1/crates/${name}/${version}/download -> ${crate}.crate"
38                 echo $url
39         done
40 }
41
42 # @FUNCTION: cargo_src_unpack
43 # @DESCRIPTION:
44 # Unpacks the package and the cargo registry
45 cargo_src_unpack() {
46         debug-print-function ${FUNCNAME} "$@"
47
48         mkdir -p "${ECARGO_VENDOR}" || die
49         mkdir -p "${S}" || die
50
51         local archive
52         for archive in ${A}; do
53                 case "${archive}" in
54                         *.crate)
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
61                                 {
62                                         "package": "${shasum}",
63                                         "files": {}
64                                 }
65                                 EOF
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
70                                 fi
71                                 eend $?
72                                 ;;
73                         cargo-snapshot*)
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
78                                 # download it
79                                 touch "${S}"/target/snapshot/bin/cargo || die
80                                 eend $?
81                                 ;;
82                         *)
83                                 unpack ${archive}
84                                 ;;
85                 esac
86         done
87
88         cargo_gen_config
89 }
90
91 # @FUNCTION: cargo_gen_config
92 # @DESCRIPTION:
93 # Generate the $CARGO_HOME/config necessary to use our local registry
94 cargo_gen_config() {
95         debug-print-function ${FUNCNAME} "$@"
96
97         cat <<- EOF > ${ECARGO_HOME}/config
98         [source.gentoo]
99         directory = "${ECARGO_VENDOR}"
100
101         [source.crates-io]
102         replace-with = "gentoo"
103         local-registry = "/nonexistant"
104         EOF
105 }
106
107 # @FUNCTION: cargo_src_compile
108 # @DESCRIPTION:
109 # Build the package using cargo build
110 cargo_src_compile() {
111         debug-print-function ${FUNCNAME} "$@"
112
113         export CARGO_HOME="${ECARGO_HOME}"
114
115         cargo build -v $(usex debug "" --release) \
116                 || die "cargo build failed"
117 }
118
119 # @FUNCTION: cargo_src_install
120 # @DESCRIPTION:
121 # Installs the binaries generated by cargo
122 cargo_src_install() {
123         debug-print-function ${FUNCNAME} "$@"
124
125         cargo install --root="${D}/usr" $(usex debug --debug "") \
126                 || die "cargo install failed"
127         rm -f "${D}/usr/.crates.toml"
128 }
129
130 fi