cargo.eclass 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # Copyright 1999-2016 Gentoo Foundation
  2. # Distributed under the terms of the GNU General Public License v2
  3. # @ECLASS: cargo.eclass
  4. # @MAINTAINER:
  5. # rust@gentoo.org
  6. # @AUTHOR:
  7. # Doug Goldstein <cardoe@gentoo.org>
  8. # @BLURB: common functions and variables for cargo builds
  9. if [[ -z ${_CARGO_ECLASS} ]]; then
  10. _CARGO_ECLASS=1
  11. case ${EAPI} in
  12. 6) : ;;
  13. *) die "EAPI=${EAPI:-0} is not supported" ;;
  14. esac
  15. EXPORT_FUNCTIONS src_unpack src_compile src_install
  16. IUSE="${IUSE} debug"
  17. [[ ${CATEGORY}/${PN} != dev-util/cargo ]] && DEPEND=">=dev-util/cargo-0.13.0"
  18. ECARGO_HOME="${WORKDIR}/cargo_home"
  19. ECARGO_VENDOR="${ECARGO_HOME}/gentoo"
  20. # @FUNCTION: cargo_crate_uris
  21. # @DESCRIPTION:
  22. # Generates the URIs to put in SRC_URI to help fetch dependencies.
  23. cargo_crate_uris() {
  24. for crate in $*; do
  25. local name version url
  26. name="${crate%-*}"
  27. version="${crate##*-}"
  28. url="https://crates.io/api/v1/crates/${name}/${version}/download -> ${crate}.crate"
  29. echo $url
  30. done
  31. }
  32. # @FUNCTION: cargo_src_unpack
  33. # @DESCRIPTION:
  34. # Unpacks the package and the cargo registry
  35. cargo_src_unpack() {
  36. debug-print-function ${FUNCNAME} "$@"
  37. mkdir -p "${ECARGO_VENDOR}" || die
  38. mkdir -p "${S}" || die
  39. local archive
  40. for archive in ${A}; do
  41. case "${archive}" in
  42. *.crate)
  43. ebegin "Loading ${archive} into Cargo registry"
  44. tar -xf "${DISTDIR}"/${archive} -C "${ECARGO_VENDOR}/" || die
  45. # generate sha256sum of the crate itself as cargo needs this
  46. shasum=$(sha256sum "${DISTDIR}"/${archive} | cut -d ' ' -f 1)
  47. pkg=$(basename ${archive} .crate)
  48. cat <<- EOF > ${ECARGO_VENDOR}/${pkg}/.cargo-checksum.json
  49. {
  50. "package": "${shasum}",
  51. "files": {}
  52. }
  53. EOF
  54. # if this is our target package we need it in ${WORKDIR} too
  55. # to make ${S} (and handle any revisions too)
  56. if [[ ${P} == ${pkg}* ]]; then
  57. tar -xf "${DISTDIR}"/${archive} -C "${WORKDIR}" || die
  58. fi
  59. eend $?
  60. ;;
  61. cargo-snapshot*)
  62. ebegin "Unpacking ${archive}"
  63. mkdir -p "${S}"/target/snapshot
  64. tar -xzf "${DISTDIR}"/${archive} -C "${S}"/target/snapshot --strip-components 2 || die
  65. # cargo's makefile needs this otherwise it will try to
  66. # download it
  67. touch "${S}"/target/snapshot/bin/cargo || die
  68. eend $?
  69. ;;
  70. *)
  71. unpack ${archive}
  72. ;;
  73. esac
  74. done
  75. cargo_gen_config
  76. }
  77. # @FUNCTION: cargo_gen_config
  78. # @DESCRIPTION:
  79. # Generate the $CARGO_HOME/config necessary to use our local registry
  80. cargo_gen_config() {
  81. debug-print-function ${FUNCNAME} "$@"
  82. cat <<- EOF > ${ECARGO_HOME}/config
  83. [source.gentoo]
  84. directory = "${ECARGO_VENDOR}"
  85. [source.crates-io]
  86. replace-with = "gentoo"
  87. local-registry = "/nonexistant"
  88. EOF
  89. }
  90. # @FUNCTION: cargo_src_compile
  91. # @DESCRIPTION:
  92. # Build the package using cargo build
  93. cargo_src_compile() {
  94. debug-print-function ${FUNCNAME} "$@"
  95. export CARGO_HOME="${ECARGO_HOME}"
  96. cargo build -v $(usex debug "" --release) \
  97. || die "cargo build failed"
  98. }
  99. # @FUNCTION: cargo_src_install
  100. # @DESCRIPTION:
  101. # Installs the binaries generated by cargo
  102. cargo_src_install() {
  103. debug-print-function ${FUNCNAME} "$@"
  104. cargo install --root="${D}/usr" $(usex debug --debug "") \
  105. || die "cargo install failed"
  106. rm -f "${D}/usr/.crates.toml"
  107. [ -d "${S}/man" ] && doman "${S}/man" || return 0
  108. }
  109. fi