cargo.eclass: add standard src_test
[gentoo.git] / eclass / cargo.eclass
1 # Copyright 1999-2019 Gentoo Authors
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 # @SUPPORTED_EAPIS: 6 7
10 # @BLURB: common functions and variables for cargo builds
11
12 if [[ -z ${_CARGO_ECLASS} ]]; then
13 _CARGO_ECLASS=1
14
15 CARGO_DEPEND=""
16 [[ ${CATEGORY}/${PN} != dev-util/cargo ]] && CARGO_DEPEND="virtual/cargo"
17
18 case ${EAPI} in
19         6) DEPEND="${CARGO_DEPEND}";;
20         7) BDEPEND="${CARGO_DEPEND}";;
21         *) die "EAPI=${EAPI:-0} is not supported" ;;
22 esac
23
24 inherit multiprocessing
25
26 EXPORT_FUNCTIONS src_unpack src_compile src_install src_test
27
28 IUSE="${IUSE} debug"
29
30 ECARGO_HOME="${WORKDIR}/cargo_home"
31 ECARGO_VENDOR="${ECARGO_HOME}/gentoo"
32
33 # @FUNCTION: cargo_crate_uris
34 # @DESCRIPTION:
35 # Generates the URIs to put in SRC_URI to help fetch dependencies.
36 cargo_crate_uris() {
37         local crate
38         for crate in "$@"; do
39                 local name version url pretag
40                 name="${crate%-*}"
41                 version="${crate##*-}"
42                 pretag="^[a-zA-Z]+"
43                 if [[ $version =~ $pretag ]]; then
44                         version="${name##*-}-${version}"
45                         name="${name%-*}"
46                 fi
47                 url="https://crates.io/api/v1/crates/${name}/${version}/download -> ${crate}.crate"
48                 echo "${url}"
49         done
50 }
51
52 # @FUNCTION: cargo_src_unpack
53 # @DESCRIPTION:
54 # Unpacks the package and the cargo registry
55 cargo_src_unpack() {
56         debug-print-function ${FUNCNAME} "$@"
57
58         mkdir -p "${ECARGO_VENDOR}" || die
59         mkdir -p "${S}" || die
60
61         local archive shasum pkg
62         for archive in ${A}; do
63                 case "${archive}" in
64                         *.crate)
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
71                                 {
72                                         "package": "${shasum}",
73                                         "files": {}
74                                 }
75                                 EOF
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
80                                 fi
81                                 eend $?
82                                 ;;
83                         cargo-snapshot*)
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
88                                 # download it
89                                 touch "${S}"/target/snapshot/bin/cargo || die
90                                 eend $?
91                                 ;;
92                         *)
93                                 unpack ${archive}
94                                 ;;
95                 esac
96         done
97
98         cargo_gen_config
99 }
100
101 # @FUNCTION: cargo_gen_config
102 # @DESCRIPTION:
103 # Generate the $CARGO_HOME/config necessary to use our local registry
104 cargo_gen_config() {
105         debug-print-function ${FUNCNAME} "$@"
106
107         cat <<- EOF > "${ECARGO_HOME}/config"
108         [source.gentoo]
109         directory = "${ECARGO_VENDOR}"
110
111         [source.crates-io]
112         replace-with = "gentoo"
113         local-registry = "/nonexistant"
114         EOF
115 }
116
117 # @FUNCTION: cargo_src_compile
118 # @DESCRIPTION:
119 # Build the package using cargo build
120 cargo_src_compile() {
121         debug-print-function ${FUNCNAME} "$@"
122
123         export CARGO_HOME="${ECARGO_HOME}"
124
125         cargo build -j $(makeopts_jobs) $(usex debug "" --release) "$@" \
126                 || die "cargo build failed"
127 }
128
129 # @FUNCTION: cargo_src_install
130 # @DESCRIPTION:
131 # Installs the binaries generated by cargo
132 cargo_src_install() {
133         debug-print-function ${FUNCNAME} "$@"
134
135         cargo install -j $(makeopts_jobs) --root="${D}/usr" $(usex debug --debug "") "$@" \
136                 || die "cargo install failed"
137         rm -f "${D}/usr/.crates.toml"
138
139         [ -d "${S}/man" ] && doman "${S}/man" || return 0
140 }
141
142 # @FUNCTION: cargo_src_test
143 # @DESCRIPTION:
144 # Test the package using cargo test
145 cargo_src_test() {
146         debug-print-function ${FUNCNAME} "$@"
147
148         cargo test -j $(makeopts_jobs) $(usex debug "" --release) "$@" \
149                 || die "cargo test failed"
150 }
151
152 fi