1 # Copyright 1999-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: rebar.eclass
6 # maintainer-needed@gentoo.org
8 # Amadeusz Żołnowski <aidecoe@gentoo.org>
10 # @BLURB: Build Erlang/OTP projects using dev-util/rebar.
12 # An eclass providing functions to build Erlang/OTP projects using
15 # rebar 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 rebar 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.
24 die "Unsupported EAPI=${EAPI:-0} (too old) for ${ECLASS}"
29 die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
33 EXPORT_FUNCTIONS src_prepare src_compile src_test src_install
35 RDEPEND="dev-lang/erlang:="
40 # @ECLASS-VARIABLE: REBAR_APP_SRC
42 # Relative path to .app.src description file.
43 REBAR_APP_SRC="${REBAR_APP_SRC-src/${PN}.app.src}"
45 # @FUNCTION: get_erl_libs
46 # @RETURN: the path to Erlang lib directory
48 # Get the full path without EPREFIX to Erlang lib directory.
50 echo "/usr/$(get_libdir)/erlang/lib"
53 # @FUNCTION: _rebar_find_dep
55 # @USAGE: <project_name>
56 # @RETURN: full path with EPREFIX to a Erlang package/project on success,
57 # code 1 when dependency is not found and code 2 if multiple versions of
58 # dependency are found.
60 # Find a Erlang package/project by name in Erlang lib directory. Project
61 # directory is usually suffixed with version. It is matched to '<project_name>'
62 # or '<project_name>-*'.
68 pushd "${EPREFIX}$(get_erl_libs)" >/dev/null || return 1
69 for p in ${pn} ${pn}-*; do
70 if [[ -d ${p} ]]; then
71 # Ensure there's at most one matching.
72 [[ ${result} ]] && return 2
76 popd >/dev/null || die
78 [[ ${result} ]] || return 1
82 # @FUNCTION: rebar_disable_coverage
83 # @USAGE: [<rebar_config>]
85 # Disable coverage in rebar.config. This is a workaround for failing coverage.
86 # Coverage is not relevant in this context, so there's no harm to disable it,
87 # although the issue should be fixed.
88 rebar_disable_coverage() {
89 debug-print-function ${FUNCNAME} "${@}"
91 local rebar_config="${1:-rebar.config}"
93 sed -e 's/{cover_enabled, true}/{cover_enabled, false}/' \
94 -i "${rebar_config}" \
95 || die "failed to disable coverage in ${rebar_config}"
101 # Run rebar with verbose flag. Die on failure.
103 debug-print-function ${FUNCNAME} "${@}"
105 (( $# > 0 )) || die "erebar: at least one target is required"
107 local -x ERL_LIBS="${EPREFIX}$(get_erl_libs)"
108 [[ ${1} == eunit ]] && local -x ERL_LIBS="."
110 rebar -v skip_deps=true "$@" || die -n "rebar $@ failed"
113 # @FUNCTION: rebar_fix_include_path
114 # @USAGE: <project_name> [<rebar_config>]
116 # Fix path in rebar.config to 'include' directory of dependant project/package,
117 # so it points to installation in system Erlang lib rather than relative 'deps'
120 # <rebar_config> is optional. Default is 'rebar.config'.
122 # The function dies on failure.
123 rebar_fix_include_path() {
124 debug-print-function ${FUNCNAME} "${@}"
127 local rebar_config="${2:-rebar.config}"
128 local erl_libs="${EPREFIX}$(get_erl_libs)"
131 p="$(_rebar_find_dep "${pn}")" \
132 || die "failed to unambiguously resolve dependency of '${pn}'"
135 -v erl_libs="${erl_libs}" -v pn="${pn}" -v p="${p}" '
136 /^{[[:space:]]*erl_opts[[:space:]]*,/, /}[[:space:]]*\.$/ {
137 pattern = "\"(./)?deps/" pn "/include\"";
138 if (match($0, "{i,[[:space:]]*" pattern "[[:space:]]*}")) {
139 sub(pattern, "\"" erl_libs "/" p "/include\"");
145 ' "${rebar_config}" || die "failed to fix include paths in ${rebar_config} for '${pn}'"
148 # @FUNCTION: rebar_remove_deps
149 # @USAGE: [<rebar_config>]
151 # Remove dependencies list from rebar.config and deceive build rules that any
152 # dependencies are already fetched and built. Otherwise rebar tries to fetch
153 # dependencies and compile them.
155 # <rebar_config> is optional. Default is 'rebar.config'.
157 # The function dies on failure.
158 rebar_remove_deps() {
159 debug-print-function ${FUNCNAME} "${@}"
161 local rebar_config="${1:-rebar.config}"
163 mkdir -p "${S}/deps" && :>"${S}/deps/.got" && :>"${S}/deps/.built" || die
165 /^{[[:space:]]*deps[[:space:]]*,/, /}[[:space:]]*\.$/ {
166 if ($0 ~ /}[[:space:]]*\.$/) {
172 ' "${rebar_config}" || die "failed to remove deps from ${rebar_config}"
175 # @FUNCTION: rebar_set_vsn
176 # @USAGE: [<version>]
178 # Set version in project description file if it's not set.
180 # <version> is optional. Default is PV stripped from version suffix.
182 # The function dies on failure.
184 debug-print-function ${FUNCNAME} "${@}"
186 local version="${1:-${PV%_*}}"
188 sed -e "s/vsn, git/vsn, \"${version}\"/" \
189 -i "${S}/${REBAR_APP_SRC}" \
190 || die "failed to set version in src/${PN}.app.src"
193 # @FUNCTION: rebar_src_prepare
195 # Prevent rebar from fetching and compiling dependencies. Set version in
196 # project description file if it's not set.
198 # Existence of rebar.config is optional, but file description file must exist
199 # at 'src/${PN}.app.src'.
200 rebar_src_prepare() {
201 debug-print-function ${FUNCNAME} "${@}"
205 if [[ -f rebar.config ]]; then
206 rebar_disable_coverage
211 # @FUNCTION: rebar_src_configure
213 # Configure with ERL_LIBS set.
214 rebar_src_configure() {
215 debug-print-function ${FUNCNAME} "${@}"
217 local -x ERL_LIBS="${EPREFIX}$(get_erl_libs)"
221 # @FUNCTION: rebar_src_compile
223 # Compile project with rebar.
224 rebar_src_compile() {
225 debug-print-function ${FUNCNAME} "${@}"
230 # @FUNCTION: rebar_src_test
234 debug-print-function ${FUNCNAME} "${@}"
239 # @FUNCTION: rebar_src_install
241 # Install BEAM files, include headers, executables and native libraries.
242 # Install standard docs like README or defined in DOCS variable.
244 # Function expects that project conforms to Erlang/OTP structure.
245 rebar_src_install() {
246 debug-print-function ${FUNCNAME} "${@}"
249 local dest="$(get_erl_libs)/${P}"
253 [[ -d include ]] && doins -r include
254 [[ -d bin ]] && for bin in bin/*; do dobin "$bin"; done
256 if [[ -d priv ]]; then
257 cp -pR priv "${ED}${dest}/" || die "failed to install priv/"