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