kde5.eclass: Add KDE_PO_DIRS
[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         local crate
33         for crate in "$@"; do
34                 local name version url pretag
35                 name="${crate%-*}"
36                 version="${crate##*-}"
37                 pretag="[a-zA-Z]+"
38                 if [[ $version =~ $pretag ]]; then
39                         version="${name##*-}-${version}"
40                         name="${name%-*}"
41                 fi
42                 url="https://crates.io/api/v1/crates/${name}/${version}/download -> ${crate}.crate"
43                 echo "${url}"
44         done
45 }
46
47 # @FUNCTION: cargo_src_unpack
48 # @DESCRIPTION:
49 # Unpacks the package and the cargo registry
50 cargo_src_unpack() {
51         debug-print-function ${FUNCNAME} "$@"
52
53         mkdir -p "${ECARGO_VENDOR}" || die
54         mkdir -p "${S}" || die
55
56         local archive shasum pkg
57         for archive in ${A}; do
58                 case "${archive}" in
59                         *.crate)
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
66                                 {
67                                         "package": "${shasum}",
68                                         "files": {}
69                                 }
70                                 EOF
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
75                                 fi
76                                 eend $?
77                                 ;;
78                         cargo-snapshot*)
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
83                                 # download it
84                                 touch "${S}"/target/snapshot/bin/cargo || die
85                                 eend $?
86                                 ;;
87                         *)
88                                 unpack ${archive}
89                                 ;;
90                 esac
91         done
92
93         cargo_gen_config
94 }
95
96 # @FUNCTION: cargo_gen_config
97 # @DESCRIPTION:
98 # Generate the $CARGO_HOME/config necessary to use our local registry
99 cargo_gen_config() {
100         debug-print-function ${FUNCNAME} "$@"
101
102         cat <<- EOF > "${ECARGO_HOME}/config"
103         [source.gentoo]
104         directory = "${ECARGO_VENDOR}"
105
106         [source.crates-io]
107         replace-with = "gentoo"
108         local-registry = "/nonexistant"
109         EOF
110 }
111
112 # @FUNCTION: cargo_src_compile
113 # @DESCRIPTION:
114 # Build the package using cargo build
115 cargo_src_compile() {
116         debug-print-function ${FUNCNAME} "$@"
117
118         export CARGO_HOME="${ECARGO_HOME}"
119
120         cargo build -v $(usex debug "" --release) \
121                 || die "cargo build failed"
122 }
123
124 # @FUNCTION: cargo_src_install
125 # @DESCRIPTION:
126 # Installs the binaries generated by cargo
127 cargo_src_install() {
128         debug-print-function ${FUNCNAME} "$@"
129
130         cargo install --root="${D}/usr" $(usex debug --debug "") \
131                 || die "cargo install failed"
132         rm -f "${D}/usr/.crates.toml"
133
134         [ -d "${S}/man" ] && doman "${S}/man" || return 0
135 }
136
137 fi