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>
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 rebar -v skip_deps=true "$@" || die -n "rebar $@ failed"
111 # @FUNCTION: rebar_fix_include_path
112 # @USAGE: <project_name> [<rebar_config>]
114 # Fix path in rebar.config to 'include' directory of dependant project/package,
115 # so it points to installation in system Erlang lib rather than relative 'deps'
118 # <rebar_config> is optional. Default is 'rebar.config'.
120 # The function dies on failure.
121 rebar_fix_include_path() {
122 debug-print-function ${FUNCNAME} "${@}"
125 local rebar_config="${2:-rebar.config}"
126 local erl_libs="${EPREFIX}$(get_erl_libs)"
129 p="$(_rebar_find_dep "${pn}")" \
130 || die "failed to unambiguously resolve dependency of '${pn}'"
133 -v erl_libs="${erl_libs}" -v pn="${pn}" -v p="${p}" '
134 /^{[[:space:]]*erl_opts[[:space:]]*,/, /}[[:space:]]*\.$/ {
135 pattern = "\"(./)?deps/" pn "/include\"";
136 if (match($0, "{i,[[:space:]]*" pattern "[[:space:]]*}")) {
137 sub(pattern, "\"" erl_libs "/" p "/include\"");
143 ' "${rebar_config}" || die "failed to fix include paths in ${rebar_config} for '${pn}'"
146 # @FUNCTION: rebar_remove_deps
147 # @USAGE: [<rebar_config>]
149 # Remove dependencies list from rebar.config and deceive build rules that any
150 # dependencies are already fetched and built. Otherwise rebar tries to fetch
151 # dependencies and compile them.
153 # <rebar_config> is optional. Default is 'rebar.config'.
155 # The function dies on failure.
156 rebar_remove_deps() {
157 debug-print-function ${FUNCNAME} "${@}"
159 local rebar_config="${1:-rebar.config}"
161 mkdir -p "${S}/deps" && :>"${S}/deps/.got" && :>"${S}/deps/.built" || die
163 /^{[[:space:]]*deps[[:space:]]*,/, /}[[:space:]]*\.$/ {
164 if ($0 ~ /}[[:space:]]*\.$/) {
170 ' "${rebar_config}" || die "failed to remove deps from ${rebar_config}"
173 # @FUNCTION: rebar_set_vsn
174 # @USAGE: [<version>]
176 # Set version in project description file if it's not set.
178 # <version> is optional. Default is PV stripped from version suffix.
180 # The function dies on failure.
182 debug-print-function ${FUNCNAME} "${@}"
184 local version="${1:-${PV%_*}}"
186 sed -e "s/vsn, git/vsn, \"${version}\"/" \
187 -i "${S}/${REBAR_APP_SRC}" \
188 || die "failed to set version in src/${PN}.app.src"
191 # @FUNCTION: rebar_src_prepare
193 # Prevent rebar from fetching and compiling dependencies. Set version in
194 # project description file if it's not set.
196 # Existence of rebar.config is optional, but file description file must exist
197 # at 'src/${PN}.app.src'.
198 rebar_src_prepare() {
199 debug-print-function ${FUNCNAME} "${@}"
203 if [[ -f rebar.config ]]; then
204 rebar_disable_coverage
209 # @FUNCTION: rebar_src_configure
211 # Configure with ERL_LIBS set.
212 rebar_src_configure() {
213 debug-print-function ${FUNCNAME} "${@}"
215 local -x ERL_LIBS="${EPREFIX}$(get_erl_libs)"
219 # @FUNCTION: rebar_src_compile
221 # Compile project with rebar.
222 rebar_src_compile() {
223 debug-print-function ${FUNCNAME} "${@}"
228 # @FUNCTION: rebar_src_test
232 debug-print-function ${FUNCNAME} "${@}"
237 # @FUNCTION: rebar_src_install
239 # Install BEAM files, include headers, executables and native libraries.
240 # Install standard docs like README or defined in DOCS variable.
242 # Function expects that project conforms to Erlang/OTP structure.
243 rebar_src_install() {
244 debug-print-function ${FUNCNAME} "${@}"
247 local dest="$(get_erl_libs)/${P}"
251 [[ -d include ]] && doins -r include
252 [[ -d bin ]] && for bin in bin/*; do dobin "$bin"; done
254 if [[ -d priv ]]; then
255 cp -pR priv "${ED}${dest}/" || die "failed to install priv/"