rebar.eclass 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. # Copyright 1999-2016 Gentoo Foundation
  2. # Distributed under the terms of the GNU General Public License v2
  3. # @ECLASS: rebar.eclass
  4. # @MAINTAINER:
  5. # Amadeusz Żołnowski <aidecoe@gentoo.org>
  6. # @AUTHOR:
  7. # Amadeusz Żołnowski <aidecoe@gentoo.org>
  8. # @BLURB: Build Erlang/OTP projects using dev-util/rebar.
  9. # @DESCRIPTION:
  10. # An eclass providing functions to build Erlang/OTP projects using
  11. # dev-util/rebar.
  12. #
  13. # rebar is a tool which tries to resolve dependencies itself which is by
  14. # cloning remote git repositories. Dependant projects are usually expected to
  15. # be in sub-directory 'deps' rather than looking at system Erlang lib
  16. # directory. Projects relying on rebar usually don't have 'install' make
  17. # targets. The eclass workarounds some of these problems. It handles
  18. # installation in a generic way for Erlang/OTP structured projects.
  19. case "${EAPI:-0}" in
  20. 0|1|2|3|4|5)
  21. die "Unsupported EAPI=${EAPI:-0} (too old) for ${ECLASS}"
  22. ;;
  23. 6)
  24. ;;
  25. *)
  26. die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
  27. ;;
  28. esac
  29. EXPORT_FUNCTIONS src_prepare src_compile src_test src_install
  30. RDEPEND="dev-lang/erlang"
  31. DEPEND="${RDEPEND}
  32. dev-util/rebar
  33. >=sys-apps/gawk-4.1"
  34. # @ECLASS-VARIABLE: REBAR_APP_SRC
  35. # @DESCRIPTION:
  36. # Relative path to .app.src description file.
  37. REBAR_APP_SRC="${REBAR_APP_SRC-src/${PN}.app.src}"
  38. # @FUNCTION: get_erl_libs
  39. # @RETURN: the path to Erlang lib directory
  40. # @DESCRIPTION:
  41. # Get the full path without EPREFIX to Erlang lib directory.
  42. get_erl_libs() {
  43. echo "/usr/$(get_libdir)/erlang/lib"
  44. }
  45. # @FUNCTION: _rebar_find_dep
  46. # @INTERNAL
  47. # @USAGE: <project_name>
  48. # @RETURN: full path with EPREFIX to a Erlang package/project on success,
  49. # code 1 when dependency is not found and code 2 if multiple versions of
  50. # dependency are found.
  51. # @DESCRIPTION:
  52. # Find a Erlang package/project by name in Erlang lib directory. Project
  53. # directory is usually suffixed with version. It is matched to '<project_name>'
  54. # or '<project_name>-*'.
  55. _rebar_find_dep() {
  56. local pn="$1"
  57. local p
  58. local result
  59. pushd "${EPREFIX}$(get_erl_libs)" >/dev/null || return 1
  60. for p in ${pn} ${pn}-*; do
  61. if [[ -d ${p} ]]; then
  62. # Ensure there's at most one matching.
  63. [[ ${result} ]] && return 2
  64. result="${p}"
  65. fi
  66. done
  67. popd >/dev/null || die
  68. [[ ${result} ]] || return 1
  69. echo "${result}"
  70. }
  71. # @FUNCTION: rebar_disable_coverage
  72. # @USAGE: [<rebar_config>]
  73. # @DESCRIPTION:
  74. # Disable coverage in rebar.config. This is a workaround for failing coverage.
  75. # Coverage is not relevant in this context, so there's no harm to disable it,
  76. # although the issue should be fixed.
  77. rebar_disable_coverage() {
  78. debug-print-function ${FUNCNAME} "${@}"
  79. local rebar_config="${1:-rebar.config}"
  80. sed -e 's/{cover_enabled, true}/{cover_enabled, false}/' \
  81. -i "${rebar_config}" \
  82. || die "failed to disable coverage in ${rebar_config}"
  83. }
  84. # @FUNCTION: erebar
  85. # @USAGE: <targets>
  86. # @DESCRIPTION:
  87. # Run rebar with verbose flag. Die on failure.
  88. erebar() {
  89. debug-print-function ${FUNCNAME} "${@}"
  90. (( $# > 0 )) || die "erebar: at least one target is required"
  91. local -x ERL_LIBS="${EPREFIX}$(get_erl_libs)"
  92. rebar -v skip_deps=true "$@" || die -n "rebar $@ failed"
  93. }
  94. # @FUNCTION: rebar_fix_include_path
  95. # @USAGE: <project_name> [<rebar_config>]
  96. # @DESCRIPTION:
  97. # Fix path in rebar.config to 'include' directory of dependant project/package,
  98. # so it points to installation in system Erlang lib rather than relative 'deps'
  99. # directory.
  100. #
  101. # <rebar_config> is optional. Default is 'rebar.config'.
  102. #
  103. # The function dies on failure.
  104. rebar_fix_include_path() {
  105. debug-print-function ${FUNCNAME} "${@}"
  106. local pn="$1"
  107. local rebar_config="${2:-rebar.config}"
  108. local erl_libs="${EPREFIX}$(get_erl_libs)"
  109. local p
  110. p="$(_rebar_find_dep "${pn}")" \
  111. || die "failed to unambiguously resolve dependency of '${pn}'"
  112. gawk -i inplace \
  113. -v erl_libs="${erl_libs}" -v pn="${pn}" -v p="${p}" '
  114. /^{[[:space:]]*erl_opts[[:space:]]*,/, /}[[:space:]]*\.$/ {
  115. pattern = "\"(./)?deps/" pn "/include\"";
  116. if (match($0, "{i,[[:space:]]*" pattern "[[:space:]]*}")) {
  117. sub(pattern, "\"" erl_libs "/" p "/include\"");
  118. }
  119. print $0;
  120. next;
  121. }
  122. 1
  123. ' "${rebar_config}" || die "failed to fix include paths in ${rebar_config} for '${pn}'"
  124. }
  125. # @FUNCTION: rebar_remove_deps
  126. # @USAGE: [<rebar_config>]
  127. # @DESCRIPTION:
  128. # Remove dependencies list from rebar.config and deceive build rules that any
  129. # dependencies are already fetched and built. Otherwise rebar tries to fetch
  130. # dependencies and compile them.
  131. #
  132. # <rebar_config> is optional. Default is 'rebar.config'.
  133. #
  134. # The function dies on failure.
  135. rebar_remove_deps() {
  136. debug-print-function ${FUNCNAME} "${@}"
  137. local rebar_config="${1:-rebar.config}"
  138. mkdir -p "${S}/deps" && :>"${S}/deps/.got" && :>"${S}/deps/.built" || die
  139. gawk -i inplace '
  140. /^{[[:space:]]*deps[[:space:]]*,/, /}[[:space:]]*\.$/ {
  141. if ($0 ~ /}[[:space:]]*\.$/) {
  142. print "{deps, []}.";
  143. }
  144. next;
  145. }
  146. 1
  147. ' "${rebar_config}" || die "failed to remove deps from ${rebar_config}"
  148. }
  149. # @FUNCTION: rebar_set_vsn
  150. # @USAGE: [<version>]
  151. # @DESCRIPTION:
  152. # Set version in project description file if it's not set.
  153. #
  154. # <version> is optional. Default is PV stripped from version suffix.
  155. #
  156. # The function dies on failure.
  157. rebar_set_vsn() {
  158. debug-print-function ${FUNCNAME} "${@}"
  159. local version="${1:-${PV%_*}}"
  160. sed -e "s/vsn, git/vsn, \"${version}\"/" \
  161. -i "${S}/${REBAR_APP_SRC}" \
  162. || die "failed to set version in src/${PN}.app.src"
  163. }
  164. # @FUNCTION: rebar_src_prepare
  165. # @DESCRIPTION:
  166. # Prevent rebar from fetching and compiling dependencies. Set version in
  167. # project description file if it's not set.
  168. #
  169. # Existence of rebar.config is optional, but file description file must exist
  170. # at 'src/${PN}.app.src'.
  171. rebar_src_prepare() {
  172. debug-print-function ${FUNCNAME} "${@}"
  173. default
  174. rebar_set_vsn
  175. if [[ -f rebar.config ]]; then
  176. rebar_disable_coverage
  177. rebar_remove_deps
  178. fi
  179. }
  180. # @FUNCTION: rebar_src_configure
  181. # @DESCRIPTION:
  182. # Configure with ERL_LIBS set.
  183. rebar_src_configure() {
  184. debug-print-function ${FUNCNAME} "${@}"
  185. local -x ERL_LIBS="${EPREFIX}$(get_erl_libs)"
  186. default
  187. }
  188. # @FUNCTION: rebar_src_compile
  189. # @DESCRIPTION:
  190. # Compile project with rebar.
  191. rebar_src_compile() {
  192. debug-print-function ${FUNCNAME} "${@}"
  193. erebar compile
  194. }
  195. # @FUNCTION: rebar_src_test
  196. # @DESCRIPTION:
  197. # Run unit tests.
  198. rebar_src_test() {
  199. debug-print-function ${FUNCNAME} "${@}"
  200. erebar eunit
  201. }
  202. # @FUNCTION: rebar_src_install
  203. # @DESCRIPTION:
  204. # Install BEAM files, include headers, executables and native libraries.
  205. # Install standard docs like README or defined in DOCS variable.
  206. #
  207. # Function expects that project conforms to Erlang/OTP structure.
  208. rebar_src_install() {
  209. debug-print-function ${FUNCNAME} "${@}"
  210. local bin
  211. local dest="$(get_erl_libs)/${P}"
  212. insinto "${dest}"
  213. doins -r ebin
  214. [[ -d include ]] && doins -r include
  215. [[ -d bin ]] && for bin in bin/*; do dobin "$bin"; done
  216. if [[ -d priv ]]; then
  217. cp -pR priv "${ED}${dest}/" || die "failed to install priv/"
  218. fi
  219. einstalldocs
  220. }