1 # Copyright 1999-2016 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
5 # @ECLASS: rebar.eclass
7 # Amadeusz Żołnowski <aidecoe@gentoo.org>
9 # 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_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
85 # Run rebar with verbose flag. Die on failure.
87 debug-print-function ${FUNCNAME} "${@}"
89 (( $# > 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"
95 # @FUNCTION: rebar_fix_include_path
96 # @USAGE: <project_name>
98 # Fix path in rebar.config to 'include' directory of dependant project/package,
99 # so it points to installation in system Erlang lib rather than relative 'deps'
102 # The function dies on failure.
103 rebar_fix_include_path() {
104 debug-print-function ${FUNCNAME} "${@}"
107 local erl_libs="${EPREFIX}$(get_erl_libs)"
110 p="$(_rebar_find_dep "${pn}")" \
111 || die "failed to unambiguously resolve dependency of '${pn}'"
114 -v erl_libs="${erl_libs}" -v pn="${pn}" -v p="${p}" '
115 /^{[[:space:]]*erl_opts[[:space:]]*,/, /}[[:space:]]*\.$/ {
116 pattern = "\"(./)?deps/" pn "/include\"";
117 if (match($0, "{i,[[:space:]]*" pattern "[[:space:]]*}")) {
118 sub(pattern, "\"" erl_libs "/" p "/include\"");
124 ' rebar.config || die "failed to fix include paths in rebar.config for '${pn}'"
127 # @FUNCTION: rebar_remove_deps
129 # Remove dependencies list from rebar.config and deceive build rules that any
130 # dependencies are already fetched and built. Otherwise rebar tries to fetch
131 # dependencies and compile them.
133 # The function dies on failure.
134 rebar_remove_deps() {
135 debug-print-function ${FUNCNAME} "${@}"
137 mkdir -p "${S}/deps" && :>"${S}/deps/.got" && :>"${S}/deps/.built" || die
139 /^{[[:space:]]*deps[[:space:]]*,/, /}[[:space:]]*\.$/ {
140 if ($0 ~ /}[[:space:]]*\.$/) {
146 ' rebar.config || die "failed to remove deps from rebar.config"
149 # @FUNCTION: rebar_set_vsn
150 # @USAGE: [<version>]
152 # Set version in project description file if it's not set.
154 # <version> is optional. Default is PV stripped from version suffix.
156 # The function dies on failure.
158 debug-print-function ${FUNCNAME} "${@}"
160 local version="${1:-${PV%_*}}"
162 sed -e "s/vsn, git/vsn, \"${version}\"/" \
163 -i "${S}/${REBAR_APP_SRC}" \
164 || die "failed to set version in src/${PN}.app.src"
167 # @FUNCTION: rebar_src_prepare
169 # Prevent rebar from fetching and compiling dependencies. Set version in
170 # project description file if it's not set.
172 # Existence of rebar.config is optional, but file description file must exist
173 # at 'src/${PN}.app.src'.
174 rebar_src_prepare() {
175 debug-print-function ${FUNCNAME} "${@}"
179 [[ -f rebar.config ]] && rebar_remove_deps
182 # @FUNCTION: rebar_src_configure
184 # Configure with ERL_LIBS set.
185 rebar_src_configure() {
186 debug-print-function ${FUNCNAME} "${@}"
188 local -x ERL_LIBS="${EPREFIX}$(get_erl_libs)"
192 # @FUNCTION: rebar_src_compile
194 # Compile project with rebar.
195 rebar_src_compile() {
196 debug-print-function ${FUNCNAME} "${@}"
201 # @FUNCTION: rebar_src_install
203 # Install BEAM files, include headers, executables and native libraries.
204 # Install standard docs like README or defined in DOCS variable.
206 # Function expects that project conforms to Erlang/OTP structure.
207 rebar_src_install() {
208 debug-print-function ${FUNCNAME} "${@}"
211 local dest="$(get_erl_libs)/${P}"
215 [[ -d include ]] && doins -r include
216 [[ -d bin ]] && for bin in bin/*; do dobin "$bin"; done
218 if [[ -d priv ]]; then
219 cp -pR priv "${ED}${dest}/" || die "failed to install priv/"