1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
# Copyright 1999-2023 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2
# @ECLASS: rebar3.eclass
# @MAINTAINER:
# Anna (cybertailor) Vyalkova <cyber+gentoo@sysrq.in>
# @AUTHOR:
# Amadeusz Żołnowski <aidecoe@gentoo.org>
# @SUPPORTED_EAPIS: 8
# @BLURB: Build Erlang/OTP projects using dev-util/rebar:3.
# @DESCRIPTION:
# An eclass providing functions to build Erlang/OTP projects using
# dev-util/rebar:3.
#
# rebar is a tool which tries to resolve dependencies itself which is by
# cloning remote git repositories. Dependent projects are usually expected to
# be in sub-directory 'deps' rather than looking at system Erlang lib
# directory. Projects relying on rebar usually don't have 'install' make
# targets. The eclass workarounds some of these problems. It handles
# installation in a generic way for Erlang/OTP structured projects.
case ${EAPI} in
8) ;;
*) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;;
esac
if [[ -z ${_REBAR3_ECLASS} ]]; then
inherit edo
RDEPEND="dev-lang/erlang:="
DEPEND="${RDEPEND}"
BDEPEND="
dev-util/rebar:3
>=sys-apps/gawk-4.1
"
# @ECLASS_VARIABLE: REBAR_PROFILE
# @DESCRIPTION:
# Rebar profile to use.
: "${REBAR_PROFILE:=default}"
# @ECLASS_VARIABLE: REBAR_APP_SRC
# @DESCRIPTION:
# Relative path to .app.src description file.
: "${REBAR_APP_SRC:=src/${PN}.app.src}"
# @FUNCTION: get_erl_libs
# @RETURN: the path to Erlang lib directory
# @DESCRIPTION:
# Get the full path without EPREFIX to Erlang lib directory.
get_erl_libs() {
echo "/usr/$(get_libdir)/erlang/lib"
}
# @FUNCTION: _rebar_find_dep
# @INTERNAL
# @USAGE: <project_name>
# @RETURN: 0 success, 1 dependency not found, 2 multiple versions found
# @DESCRIPTION:
# Find a Erlang package/project by name in Erlang lib directory. Project
# directory is usually suffixed with version. It is matched to '<project_name>'
# or '<project_name>-*'.
_rebar_find_dep() {
local pn="${1}"
local p
local result
pushd "${EPREFIX}/$(get_erl_libs)" >/dev/null || return 1
for p in ${pn} ${pn}-*; do
if [[ -d ${p} ]]; then
# Ensure there's at most one matching.
[[ ${result} ]] && return 2
result="${p}"
fi
done
popd >/dev/null || die
[[ ${result} ]] || return 1
echo "${result}"
}
# @FUNCTION: rebar_disable_coverage
# @USAGE: [<rebar_config>]
# @DESCRIPTION:
# Disable coverage in rebar.config. This is a workaround for failing coverage.
# Coverage is not relevant in this context, so there's no harm to disable it,
# although the issue should be fixed.
rebar_disable_coverage() {
debug-print-function ${FUNCNAME} "${@}"
local rebar_config="${1:-rebar.config}"
sed -e 's/{cover_enabled, true}/{cover_enabled, false}/' \
-i "${rebar_config}" \
|| die "failed to disable coverage in ${rebar_config}"
}
# @FUNCTION: erebar3
# @USAGE: <targets>
# @DESCRIPTION:
# Run rebar with verbose flag. Die on failure.
erebar3() {
debug-print-function ${FUNCNAME} "${@}"
(( $# > 0 )) || die "erebar: at least one target is required"
case ${1} in
eunit|ct)
local -x ERL_LIBS="." ;;
*)
local -x ERL_LIBS="${EPREFIX}/$(get_erl_libs)" ;;
esac
edo rebar3 "$@"
}
# @FUNCTION: rebar_fix_include_path
# @USAGE: <project_name> [<rebar_config>]
# @DESCRIPTION:
# Fix path in rebar.config to 'include' directory of dependent project/package,
# so it points to installation in system Erlang lib rather than relative 'deps'
# directory.
#
# <rebar_config> is optional. Default is 'rebar.config'.
#
# The function dies on failure.
rebar_fix_include_path() {
debug-print-function ${FUNCNAME} "${@}"
local pn="${1}"
local rebar_config="${2:-rebar.config}"
local erl_libs="${EPREFIX}/$(get_erl_libs)"
local p
p="$(_rebar_find_dep "${pn}")" \
|| die "failed to unambiguously resolve dependency of '${pn}'"
gawk -i inplace \
-v erl_libs="${erl_libs}" -v pn="${pn}" -v p="${p}" '
/^{[[:space:]]*erl_opts[[:space:]]*,/, /}[[:space:]]*\.$/ {
pattern = "\"(./)?deps/" pn "/include\"";
if (match($0, "{i,[[:space:]]*" pattern "[[:space:]]*}")) {
sub(pattern, "\"" erl_libs "/" p "/include\"");
}
print $0;
next;
}
1
' "${rebar_config}" || die "failed to fix include paths in ${rebar_config} for '${pn}'"
}
# @FUNCTION: rebar_remove_deps
# @USAGE: [<rebar_config>]
# @DESCRIPTION:
# Remove dependencies list from rebar.config and deceive build rules that any
# dependencies are already fetched and built. Otherwise rebar tries to fetch
# dependencies and compile them.
#
# <rebar_config> is optional. Default is 'rebar.config'.
#
# The function dies on failure.
rebar_remove_deps() {
debug-print-function ${FUNCNAME} "${@}"
local rebar_config="${1:-rebar.config}"
mkdir -p "${S}/deps" && :>"${S}/deps/.got" && :>"${S}/deps/.built" || die
gawk -i inplace '
/^{[[:space:]]*deps[[:space:]]*,/, /}[[:space:]]*\.$/ {
if ($0 ~ /}[[:space:]]*\.$/) {
print "{deps, []}.";
}
next;
}
1
' "${rebar_config}" || die "failed to remove deps from ${rebar_config}"
}
# @FUNCTION: rebar_set_vsn
# @USAGE: [<version>]
# @DESCRIPTION:
# Set version in project description file if it's not set.
#
# <version> is optional. Default is PV stripped from version suffix.
#
# The function dies on failure.
rebar_set_vsn() {
debug-print-function ${FUNCNAME} "${@}"
local version="${1:-${PV%_*}}"
sed -e "s/vsn, git/vsn, \"${version}\"/" \
-i "${S}/${REBAR_APP_SRC}" \
|| die "failed to set version in src/${PN}.app.src"
}
# @FUNCTION: rebar3_src_prepare
# @DESCRIPTION:
# Prevent rebar from fetching and compiling dependencies. Set version in
# project description file if it's not set.
#
# Existence of rebar.config is optional, but file description file must exist
# at 'src/${PN}.app.src'.
rebar3_src_prepare() {
debug-print-function ${FUNCNAME} "${@}"
default_src_prepare
rebar_set_vsn
rm -f rebar.lock
if [[ -f rebar.config ]]; then
rebar_disable_coverage
rebar_remove_deps
fi
}
# @FUNCTION: rebar3_src_configure
# @DESCRIPTION:
# Configure with ERL_LIBS set.
rebar3_src_configure() {
debug-print-function ${FUNCNAME} "${@}"
local -x ERL_LIBS="${EPREFIX}/$(get_erl_libs)"
default_src_configure
}
# @FUNCTION: rebar3_src_compile
# @DESCRIPTION:
# Compile project with rebar.
rebar3_src_compile() {
debug-print-function ${FUNCNAME} "${@}"
erebar3 as "${REBAR_PROFILE}" release --all
}
# @FUNCTION: rebar3_src_test
# @DESCRIPTION:
# Run unit tests.
rebar3_src_test() {
debug-print-function ${FUNCNAME} "${@}"
erebar3 eunit -v
}
# @FUNCTION: rebar3_install_lib
# @USAGE: <dir>
# @DESCRIPTION:
# Install BEAM files, include headers and native libraries.
#
# Function expects that project conforms to Erlang/OTP structure.
rebar3_install_lib() {
debug-print-function ${FUNCNAME} "${@}"
local dest="$(get_erl_libs)/${P}"
insinto "${dest}"
pushd "${1?}" >/dev/null || die
for dir in ebin include priv; do
if [[ -d ${dir} && ! -L ${dir} ]]; then
cp -pR ${dir} "${ED%/}/${dest}/" || die "failed to install ${dir}/"
fi
done
popd >/dev/null || die
}
# @FUNCTION: rebar3_src_install
# @DESCRIPTION:
# Install built release or library.
#
# Function expects that project conforms to Erlang/OTP structure.
rebar3_src_install() {
debug-print-function ${FUNCNAME} "${@}"
pushd "_build/${REBAR_PROFILE}" >/dev/null || die
if [[ -d rel/${PN} ]]; then
if ! declare -f rebar3_install_release >/dev/null; then
die "${FUNCNAME}: a custom function named 'rebar3_install_release' is required to install a release"
fi
pushd rel/${PN} >/dev/null || die
rebar3_install_release || die
popd >/dev/null || die
elif [[ -d lib/${PN} ]]; then
rebar3_install_lib lib/${PN}
else
die "No releases or libraries to install"
fi
popd >/dev/null || die
einstalldocs
}
_REBAR3_ECLASS=1
fi
EXPORT_FUNCTIONS src_prepare src_compile src_test src_install
|