cargo.eclass: respect parallel jobs setting from MAKEOPTS
[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 inherit multiprocessing
20
21 EXPORT_FUNCTIONS src_unpack src_compile src_install
22
23 IUSE="${IUSE} debug"
24
25 [[ ${CATEGORY}/${PN} != dev-util/cargo ]] && DEPEND=">=dev-util/cargo-0.13.0"
26
27 ECARGO_HOME="${WORKDIR}/cargo_home"
28 ECARGO_VENDOR="${ECARGO_HOME}/gentoo"
29
30 # @FUNCTION: cargo_crate_uris
31 # @DESCRIPTION:
32 # Generates the URIs to put in SRC_URI to help fetch dependencies.
33 cargo_crate_uris() {
34         local crate
35         for crate in "$@"; do
36                 local name version url pretag
37                 name="${crate%-*}"
38                 version="${crate##*-}"
39                 pretag="[a-zA-Z]+"
40                 if [[ $version =~ $pretag ]]; then
41                         version="${name##*-}-${version}"
42                         name="${name%-*}"
43                 fi
44                 url="https://crates.io/api/v1/crates/${name}/${version}/download -> ${crate}.crate"
45                 echo "${url}"
46         done
47 }
48
49 # @FUNCTION: cargo_src_unpack
50 # @DESCRIPTION:
51 # Unpacks the package and the cargo registry
52 cargo_src_unpack() {
53         debug-print-function ${FUNCNAME} "$@"
54
55         mkdir -p "${ECARGO_VENDOR}" || die
56         mkdir -p "${S}" || die
57
58         local archive shasum pkg
59         for archive in ${A}; do
60                 case "${archive}" in
61                         *.crate)
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
68                                 {
69                                         "package": "${shasum}",
70                                         "files": {}
71                                 }
72                                 EOF
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
77                                 fi
78                                 eend $?
79                                 ;;
80                         cargo-snapshot*)
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
85                                 # download it
86                                 touch "${S}"/target/snapshot/bin/cargo || die
87                                 eend $?
88                                 ;;
89                         *)
90                                 unpack ${archive}
91                                 ;;
92                 esac
93         done
94
95         cargo_gen_config
96 }
97
98 # @FUNCTION: cargo_gen_config
99 # @DESCRIPTION:
100 # Generate the $CARGO_HOME/config necessary to use our local registry
101 cargo_gen_config() {
102         debug-print-function ${FUNCNAME} "$@"
103
104         cat <<- EOF > "${ECARGO_HOME}/config"
105         [source.gentoo]
106         directory = "${ECARGO_VENDOR}"
107
108         [source.crates-io]
109         replace-with = "gentoo"
110         local-registry = "/nonexistant"
111         EOF
112 }
113
114 # @FUNCTION: cargo_src_compile
115 # @DESCRIPTION:
116 # Build the package using cargo build
117 cargo_src_compile() {
118         debug-print-function ${FUNCNAME} "$@"
119
120         export CARGO_HOME="${ECARGO_HOME}"
121
122         cargo build -v -j $(makeopts_jobs) $(usex debug "" --release) \
123                 || die "cargo build failed"
124 }
125
126 # @FUNCTION: cargo_src_install
127 # @DESCRIPTION:
128 # Installs the binaries generated by cargo
129 cargo_src_install() {
130         debug-print-function ${FUNCNAME} "$@"
131
132         cargo install -j $(makeopts_jobs) --root="${D}/usr" $(usex debug --debug "") \
133                 || die "cargo install failed"
134         rm -f "${D}/usr/.crates.toml"
135
136         [ -d "${S}/man" ] && doman "${S}/man" || return 0
137 }
138
139 fi