logo

overlay

My own overlay for experimentations, use with caution, no support is provided git clone https://anongit.hacktivis.me/git/overlay.git/

rebar3.eclass (7685B)


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