1 # Copyright 1999-2016 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: rebar.eclass
6 # Amadeusz Żołnowski <aidecoe@gentoo.org>
8 # Amadeusz Żołnowski <aidecoe@gentoo.org>
9 # @BLURB: Build Erlang/OTP projects using dev-util/rebar.
11 # An eclass providing functions to build Erlang/OTP projects using
14 # rebar is a tool which tries to resolve dependencies itself which is by
15 # cloning remote git repositories. Dependant projects are usually expected to
16 # be in sub-directory 'deps' rather than looking at system Erlang lib
17 # directory. Projects relying on rebar usually don't have 'install' make
18 # targets. The eclass workarounds some of these problems. It handles
19 # installation in a generic way for Erlang/OTP structured projects.
23 die "Unsupported EAPI=${EAPI:-0} (too old) for ${ECLASS}"
28 die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
32 EXPORT_FUNCTIONS src_prepare src_compile src_test src_install
34 RDEPEND="dev-lang/erlang"
39 # @ECLASS-VARIABLE: REBAR_APP_SRC
41 # Relative path to .app.src description file.
42 REBAR_APP_SRC="${REBAR_APP_SRC-src/${PN}.app.src}"
44 # @FUNCTION: get_erl_libs
45 # @RETURN: the path to Erlang lib directory
47 # Get the full path without EPREFIX to Erlang lib directory.
49 echo "/usr/$(get_libdir)/erlang/lib"
52 # @FUNCTION: _rebar_find_dep
54 # @USAGE: <project_name>
55 # @RETURN: full path with EPREFIX to a Erlang package/project on success,
56 # code 1 when dependency is not found and code 2 if multiple versions of
57 # dependency are found.
59 # Find a Erlang package/project by name in Erlang lib directory. Project
60 # directory is usually suffixed with version. It is matched to '<project_name>'
61 # or '<project_name>-*'.
67 pushd "${EPREFIX}$(get_erl_libs)" >/dev/null || return 1
68 for p in ${pn} ${pn}-*; do
69 if [[ -d ${p} ]]; then
70 # Ensure there's at most one matching.
71 [[ ${result} ]] && return 2
75 popd >/dev/null || die
77 [[ ${result} ]] || return 1
81 # @FUNCTION: rebar_disable_coverage
82 # @USAGE: [<rebar_config>]
84 # Disable coverage in rebar.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 rebar_disable_coverage() {
88 debug-print-function ${FUNCNAME} "${@}"
90 local rebar_config="${1:-rebar.config}"
92 sed -e 's/{cover_enabled, true}/{cover_enabled, false}/' \
93 -i "${rebar_config}" \
94 || die "failed to disable coverage in ${rebar_config}"
100 # Run rebar with verbose flag. Die on failure.
102 debug-print-function ${FUNCNAME} "${@}"
104 (( $# > 0 )) || die "erebar: at least one target is required"
106 local -x ERL_LIBS="${EPREFIX}$(get_erl_libs)"
107 rebar -v skip_deps=true "$@" || die -n "rebar $@ failed"
110 # @FUNCTION: rebar_fix_include_path
111 # @USAGE: <project_name> [<rebar_config>]
113 # Fix path in rebar.config to 'include' directory of dependant project/package,
114 # so it points to installation in system Erlang lib rather than relative 'deps'
117 # <rebar_config> is optional. Default is 'rebar.config'.
119 # The function dies on failure.
120 rebar_fix_include_path() {
121 debug-print-function ${FUNCNAME} "${@}"
124 local rebar_config="${2:-rebar.config}"
125 local erl_libs="${EPREFIX}$(get_erl_libs)"
128 p="$(_rebar_find_dep "${pn}")" \
129 || die "failed to unambiguously resolve dependency of '${pn}'"
132 -v erl_libs="${erl_libs}" -v pn="${pn}" -v p="${p}" '
133 /^{[[:space:]]*erl_opts[[:space:]]*,/, /}[[:space:]]*\.$/ {
134 pattern = "\"(./)?deps/" pn "/include\"";
135 if (match($0, "{i,[[:space:]]*" pattern "[[:space:]]*}")) {
136 sub(pattern, "\"" erl_libs "/" p "/include\"");
142 ' "${rebar_config}" || die "failed to fix include paths in ${rebar_config} for '${pn}'"
145 # @FUNCTION: rebar_remove_deps
146 # @USAGE: [<rebar_config>]
148 # Remove dependencies list from rebar.config and deceive build rules that any
149 # dependencies are already fetched and built. Otherwise rebar tries to fetch
150 # dependencies and compile them.
152 # <rebar_config> is optional. Default is 'rebar.config'.
154 # The function dies on failure.
155 rebar_remove_deps() {
156 debug-print-function ${FUNCNAME} "${@}"
158 local rebar_config="${1:-rebar.config}"
160 mkdir -p "${S}/deps" && :>"${S}/deps/.got" && :>"${S}/deps/.built" || die
162 /^{[[:space:]]*deps[[:space:]]*,/, /}[[:space:]]*\.$/ {
163 if ($0 ~ /}[[:space:]]*\.$/) {
169 ' "${rebar_config}" || die "failed to remove deps from ${rebar_config}"
172 # @FUNCTION: rebar_set_vsn
173 # @USAGE: [<version>]
175 # Set version in project description file if it's not set.
177 # <version> is optional. Default is PV stripped from version suffix.
179 # The function dies on failure.
181 debug-print-function ${FUNCNAME} "${@}"
183 local version="${1:-${PV%_*}}"
185 sed -e "s/vsn, git/vsn, \"${version}\"/" \
186 -i "${S}/${REBAR_APP_SRC}" \
187 || die "failed to set version in src/${PN}.app.src"
190 # @FUNCTION: rebar_src_prepare
192 # Prevent rebar from fetching and compiling dependencies. Set version in
193 # project description file if it's not set.
195 # Existence of rebar.config is optional, but file description file must exist
196 # at 'src/${PN}.app.src'.
197 rebar_src_prepare() {
198 debug-print-function ${FUNCNAME} "${@}"
202 if [[ -f rebar.config ]]; then
203 rebar_disable_coverage
208 # @FUNCTION: rebar_src_configure
210 # Configure with ERL_LIBS set.
211 rebar_src_configure() {
212 debug-print-function ${FUNCNAME} "${@}"
214 local -x ERL_LIBS="${EPREFIX}$(get_erl_libs)"
218 # @FUNCTION: rebar_src_compile
220 # Compile project with rebar.
221 rebar_src_compile() {
222 debug-print-function ${FUNCNAME} "${@}"
227 # @FUNCTION: rebar_src_test
231 debug-print-function ${FUNCNAME} "${@}"
236 # @FUNCTION: rebar_src_install
238 # Install BEAM files, include headers, executables and native libraries.
239 # Install standard docs like README or defined in DOCS variable.
241 # Function expects that project conforms to Erlang/OTP structure.
242 rebar_src_install() {
243 debug-print-function ${FUNCNAME} "${@}"
246 local dest="$(get_erl_libs)/${P}"
250 [[ -d include ]] && doins -r include
251 [[ -d bin ]] && for bin in bin/*; do dobin "$bin"; done
253 if [[ -d priv ]]; then
254 cp -pR priv "${ED}${dest}/" || die "failed to install priv/"