1 # Copyright 1999-2019 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: cargo.eclass
8 # Doug Goldstein <cardoe@gentoo.org>
9 # @SUPPORTED_EAPIS: 6 7
10 # @BLURB: common functions and variables for cargo builds
12 if [[ -z ${_CARGO_ECLASS} ]]; then
15 # we need this for 'cargo vendor' subcommand and net.offline config knob
16 RUST_DEPEND=">=virtual/rust-1.37.0"
19 6) DEPEND="${RUST_DEPEND}";;
20 7) BDEPEND="${RUST_DEPEND}";;
21 *) die "EAPI=${EAPI:-0} is not supported" ;;
24 inherit multiprocessing
26 EXPORT_FUNCTIONS src_unpack src_compile src_install src_test
30 ECARGO_HOME="${WORKDIR}/cargo_home"
31 ECARGO_VENDOR="${ECARGO_HOME}/gentoo"
33 # @ECLASS-VARIABLE: CARGO_INSTALL_PATH
35 # Allows overriding the default cwd to run cargo install from
36 : ${CARGO_INSTALL_PATH:=.}
38 # @FUNCTION: cargo_crate_uris
40 # Generates the URIs to put in SRC_URI to help fetch dependencies.
44 local name version url pretag
46 version="${crate##*-}"
48 if [[ $version =~ $pretag ]]; then
49 version="${name##*-}-${version}"
52 url="https://crates.io/api/v1/crates/${name}/${version}/download -> ${crate}.crate"
57 # @FUNCTION: cargo_src_unpack
59 # Unpacks the package and the cargo registry
61 debug-print-function ${FUNCNAME} "$@"
63 mkdir -p "${ECARGO_VENDOR}" || die
64 mkdir -p "${S}" || die
66 local archive shasum pkg
67 for archive in ${A}; do
70 ebegin "Loading ${archive} into Cargo registry"
71 tar -xf "${DISTDIR}"/${archive} -C "${ECARGO_VENDOR}/" || die
72 # generate sha256sum of the crate itself as cargo needs this
73 shasum=$(sha256sum "${DISTDIR}"/${archive} | cut -d ' ' -f 1)
74 pkg=$(basename ${archive} .crate)
75 cat <<- EOF > ${ECARGO_VENDOR}/${pkg}/.cargo-checksum.json
77 "package": "${shasum}",
81 # if this is our target package we need it in ${WORKDIR} too
82 # to make ${S} (and handle any revisions too)
83 if [[ ${P} == ${pkg}* ]]; then
84 tar -xf "${DISTDIR}"/${archive} -C "${WORKDIR}" || die
89 ebegin "Unpacking ${archive}"
90 mkdir -p "${S}"/target/snapshot
91 tar -xzf "${DISTDIR}"/${archive} -C "${S}"/target/snapshot --strip-components 2 || die
92 # cargo's makefile needs this otherwise it will try to
94 touch "${S}"/target/snapshot/bin/cargo || die
106 # @FUNCTION: cargo_live_src_unpack
108 # Runs 'cargo fetch' and vendors downloaded crates for offline use, used in live ebuilds
110 cargo_live_src_unpack() {
111 debug-print-function ${FUNCNAME} "$@"
113 [[ "${PV}" == *9999* ]] || die "${FUNCNAME} only allowed in live/9999 ebuilds"
114 [[ "${EBUILD_PHASE}" == unpack ]] || die "${FUNCNAME} only allowed in src_unpack"
116 mkdir -p "${S}" || die
118 pushd "${S}" > /dev/null || die
119 CARGO_HOME="${ECARGO_HOME}" cargo fetch || die
120 CARGO_HOME="${ECARGO_HOME}" cargo vendor "${ECARGO_VENDOR}" || die
121 popd > /dev/null || die
126 # @FUNCTION: cargo_gen_config
128 # Generate the $CARGO_HOME/config necessary to use our local registry and settings.
129 # Cargo can also be configured through environment variables in addition to the TOML syntax below.
130 # For each configuration key below of the form foo.bar the environment variable CARGO_FOO_BAR
131 # can also be used to define the value.
132 # Environment variables will take precedent over TOML configuration,
133 # and currently only integer, boolean, and string keys are supported.
134 # For example the build.jobs key can also be defined by CARGO_BUILD_JOBS.
135 # Or setting CARGO_TERM_VERBOSE=false in make.conf will make build quieter.
137 debug-print-function ${FUNCNAME} "$@"
139 cat <<- EOF > "${ECARGO_HOME}/config"
141 directory = "${ECARGO_VENDOR}"
144 replace-with = "gentoo"
145 local-registry = "/nonexistant"
151 jobs = $(makeopts_jobs)
156 # honor NOCOLOR setting
157 [[ "${NOCOLOR}" = true || "${NOCOLOR}" = yes ]] && echo "color = 'never'" >> "${ECARGO_HOME}/config"
160 # @FUNCTION: cargo_src_compile
162 # Build the package using cargo build
163 cargo_src_compile() {
164 debug-print-function ${FUNCNAME} "$@"
166 export CARGO_HOME="${ECARGO_HOME}"
168 cargo build $(usex debug "" --release) "$@" \
169 || die "cargo build failed"
172 # @FUNCTION: cargo_src_install
174 # Installs the binaries generated by cargo
175 cargo_src_install() {
176 debug-print-function ${FUNCNAME} "$@"
178 cargo install --path ${CARGO_INSTALL_PATH} \
179 --root="${ED}/usr" $(usex debug --debug "") "$@" \
180 || die "cargo install failed"
181 rm -f "${ED}/usr/.crates.toml"
183 [ -d "${S}/man" ] && doman "${S}/man" || return 0
186 # @FUNCTION: cargo_src_test
188 # Test the package using cargo test
190 debug-print-function ${FUNCNAME} "$@"
192 cargo test $(usex debug "" --release) "$@" \
193 || die "cargo test failed"