Gentoo Archives: gentoo-dev

From: "Michał Górny" <mgorny@g.o>
To: aidecoe@g.o
Cc: gentoo-dev@l.g.o
Subject: Re: [gentoo-dev] [PATCH] rebar.eclass: Build Erlang/OTP projects using dev-util/rebar
Date: Sat, 21 May 2016 20:48:31
Message-Id: 20160521224814.55493d6c.mgorny@gentoo.org
In Reply to: [gentoo-dev] [PATCH] rebar.eclass: Build Erlang/OTP projects using dev-util/rebar by aidecoe@gentoo.org
1 On Sat, 21 May 2016 14:26:00 +0100
2 aidecoe@g.o wrote:
3
4 > From: Amadeusz Żołnowski <aidecoe@g.o>
5 >
6 > It is an eclass providing functions to build Erlang/OTP projects using
7 > dev-util/rebar. All packages in upcoming category dev-erlang are going
8 > to use this eclass.
9 > ---
10 > eclass/rebar.eclass | 217 ++++++++++++++++++++++++++++++++++++++++++++++++++++
11 > 1 file changed, 217 insertions(+)
12 > create mode 100644 eclass/rebar.eclass
13 >
14 > diff --git a/eclass/rebar.eclass b/eclass/rebar.eclass
15 > new file mode 100644
16 > index 0000000..9da1340
17 > --- /dev/null
18 > +++ b/eclass/rebar.eclass
19 > @@ -0,0 +1,217 @@
20 > +# Copyright 1999-2016 Gentoo Foundation
21 > +# Distributed under the terms of the GNU General Public License v2
22 > +# $Id$
23 > +
24 > +# @ECLASS: rebar.eclass
25 > +# @MAINTAINER:
26 > +# Amadeusz Żołnowski <aidecoe@g.o>
27 > +# @AUTHOR:
28 > +# Amadeusz Żołnowski <aidecoe@g.o>
29 > +# @BLURB: Build Erlang/OTP projects using dev-util/rebar.
30 > +# @DESCRIPTION:
31 > +# An eclass providing functions to build Erlang/OTP projects using
32 > +# dev-util/rebar.
33 > +#
34 > +# rebar is a tool which tries to resolve dependencies itself which is by
35 > +# cloning remote git repositories. Dependant projects are usually expected to
36 > +# be in sub-directory 'deps' rather than looking at system Erlang lib
37 > +# directory. Projects relying on rebar usually don't have 'install' make
38 > +# targets. The eclass workarounds some of these problems. It handles
39 > +# installation in a generic way for Erlang/OTP structured projects.
40 > +
41 > +case "${EAPI:-0}" in
42 > + 0|1|2|3|4|5)
43 > + die "Unsupported EAPI=${EAPI:-0} (too old) for ${ECLASS}"
44 > + ;;
45 > + 6)
46 > + ;;
47 > + *)
48 > + die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
49 > + ;;
50 > +esac
51 > +
52 > +inherit eutils
53 > +
54 > +EXPORT_FUNCTIONS src_prepare src_compile src_install
55 > +
56 > +RDEPEND="dev-lang/erlang"
57 > +DEPEND="${RDEPEND}
58 > + dev-util/rebar"
59 > +
60 > +# @ECLASS-VARIABLE: REBAR_APP_SRC
61 > +# @DESCRIPTION:
62 > +# Relative path to .app.src description file.
63 > +REBAR_APP_SRC="${REBAR_APP_SRC-src/${PN}.app.src}"
64 > +
65 > +# @FUNCTION: get_erl_libs
66 > +# @RETURN: the path to Erlang lib directory
67 > +# @DESCRIPTION:
68 > +# Get the full path without EPREFIX to Erlang lib directory.
69 > +get_erl_libs() {
70 > + echo "/usr/$(get_libdir)/erlang/lib"
71 > +}
72 > +
73 > +# @FUNCTION: _rebar_find_dep_version
74 > +# @INTERNAL
75 > +# @USAGE: <project_name>
76 > +# @RETURN: full path with EPREFIX to a Erlang package/project
77 > +# @DESCRIPTION:
78 > +# Find a Erlang package/project by name in Erlang lib directory. Project
79 > +# directory is usually suffixed with version. First match to <project_name> or
80 > +# <project_name>-* is returned.
81 > +_rebar_find_dep_version() {
82 > + local pn="$1"
83 > + local p
84 > +
85 > + pushd "${EPREFIX}$(get_erl_libs)" >/dev/null || die
86 > + for p in ${pn} ${pn}-*; do
87 > + if [[ -d ${p} ]]; then
88 > + echo "${p#${pn}-}"
89 > + break
90 > + fi
91 > + done
92 > + popd >/dev/null || die
93 > +
94 > + [[ -d ${p} ]]
95 > +}
96 > +
97 > +# @FUNCTION: erebar
98 > +# @USAGE: <targets>
99 > +# @DESCRIPTION:
100 > +# Run rebar with verbose flag. Die on failure.
101 > +erebar() {
102 > + debug-print-function ${FUNCNAME} "${@}"
103 > +
104 > + (( $# > 0 )) || die 'erebar: at least one target is required'
105
106 Why not [[ $# -gt 0 ]]? It's the first time I see someone using (( ))
107 for conditionals.
108
109 > +
110 > + evar_push ERL_LIBS
111 > + export ERL_LIBS="${EPREFIX}$(get_erl_libs)"
112
113 local -x ERL_LIBS=...
114
115 We don't really have to employ terribly ugly eval hackery to have
116 a local variable.
117
118 > + rebar -v skip_deps=true "$@" || die "rebar $@ failed"
119 > + evar_pop
120 > +}
121 > +
122 > +# @FUNCTION: rebar_fix_include_path
123 > +# @USAGE: <project_name>
124 > +# @DESCRIPTION:
125 > +# Fix path in rebar.config to 'include' directory of dependant project/package,
126 > +# so it points to installation in system Erlang lib rather than relative 'deps'
127 > +# directory.
128 > +#
129 > +# The function dies on failure.
130 > +rebar_fix_include_path() {
131 > + debug-print-function ${FUNCNAME} "${@}"
132 > +
133 > + local pn="$1"
134 > + local erl_libs="${EPREFIX}$(get_erl_libs)"
135 > + local pv="$(_rebar_find_dep_version "${pn}")"
136 > +
137 > + eawk rebar.config \
138 > + -v erl_libs="${erl_libs}" -v pn="${pn}" -v pv="${pv}" \
139 > + '/^{[[:space:]]*erl_opts[[:space:]]*,/, /}[[:space:]]*\.$/ {
140 > + pattern = "\"(./)?deps/" pn "/include\"";
141 > + if (match($0, "{i,[[:space:]]*" pattern "[[:space:]]*}")) {
142 > + sub(pattern, "\"" erl_libs "/" pn "-" pv "/include\"");
143 > + }
144 > + print $0;
145 > + next;
146 > +}
147 > +1
148 > +' || die "failed to fix include paths in rebar.config"
149
150 I meant indent like this:
151
152 + eawk rebar.config \
153 + -v erl_libs="${erl_libs}" -v pn="${pn}" -v pv="${pv}" \
154 + '/^{[[:space:]]*erl_opts[[:space:]]*,/, /}[[:space:]]*\.$/ {
155 + pattern = "\"(./)?deps/" pn "/include\"";
156 + if (match($0, "{i,[[:space:]]*" pattern "[[:space:]]*}")) {
157 + sub(pattern, "\"" erl_libs "/" pn "-" pv "/include\"");
158 + }
159 + print $0;
160 + next;
161 + }
162 + 1
163 + ' || die "failed to fix include paths in rebar.config"
164
165
166 > +}
167 > +
168 > +# @FUNCTION: rebar_remove_deps
169 > +# @DESCRIPTION:
170 > +# Remove dependencies list from rebar.config and deceive build rules that any
171 > +# dependencies are already fetched and built. Otherwise rebar tries to fetch
172 > +# dependencies and compile them.
173 > +#
174 > +# The function dies on failure.
175 > +rebar_remove_deps() {
176 > + debug-print-function ${FUNCNAME} "${@}"
177 > +
178 > + mkdir -p "${S}/deps" && :>"${S}/deps/.got" && :>"${S}/deps/.built" || die
179 > + eawk rebar.config \
180 > + '/^{[[:space:]]*deps[[:space:]]*,/, /}[[:space:]]*\.$/ {
181 > + if ($0 ~ /}[[:space:]]*\.$/) {
182 > + print "{deps, []}.";
183 > + }
184 > + next;
185 > +}
186 > +1
187 > +' || die "failed to remove deps from rebar.config"
188 > +}
189 > +
190 > +# @FUNCTION: rebar_set_vsn
191 > +# @USAGE: [<version>]
192 > +# @DESCRIPTION:
193 > +# Set version in project description file if it's not set.
194 > +#
195 > +# <version> is optional. Default is PV stripped from version suffix.
196 > +#
197 > +# The function dies on failure.
198 > +rebar_set_vsn() {
199 > + debug-print-function ${FUNCNAME} "${@}"
200 > +
201 > + local version="${1:-${PV%_*}}"
202 > +
203 > + sed -e "s/vsn, git/vsn, \"${version}\"/" \
204 > + -i "${S}/${REBAR_APP_SRC}" \
205 > + || die "failed to set version in src/${PN}.app.src"
206 > +}
207 > +
208 > +# @FUNCTION: rebar_src_prepare
209 > +# @DESCRIPTION:
210 > +# Prevent rebar from fetching in compiling dependencies. Set version in project
211 > +# description file if it's not set.
212 > +#
213 > +# Existence of rebar.config is optional, but file description file must exist
214 > +# at 'src/${PN}.app.src'.
215 > +rebar_src_prepare() {
216 > + debug-print-function ${FUNCNAME} "${@}"
217 > +
218 > + default
219 > + rebar_set_vsn
220 > + [[ -f rebar.config ]] && rebar_remove_deps
221 > +}
222 > +
223 > +# @FUNCTION: rebar_src_configure
224 > +# @DESCRIPTION:
225 > +# Configure with ERL_LIBS set.
226 > +rebar_src_configure() {
227 > + debug-print-function ${FUNCNAME} "${@}"
228 > +
229 > + evar_push ERL_LIBS
230 > + export ERL_LIBS="${EPREFIX}$(get_erl_libs)"
231 > + default
232 > + evar_pop
233 > +}
234 > +
235 > +# @FUNCTION: rebar_src_compile
236 > +# @DESCRIPTION:
237 > +# Compile project with rebar.
238 > +rebar_src_compile() {
239 > + debug-print-function ${FUNCNAME} "${@}"
240 > +
241 > + erebar compile
242 > +}
243 > +
244 > +# @FUNCTION: rebar_src_install
245 > +# @DESCRIPTION:
246 > +# Install BEAM files, include headers, executables and native libraries.
247 > +# Install standard docs like README or defined in DOCS variable.
248 > +#
249 > +# Function expects that project conforms to Erlang/OTP structure.
250 > +rebar_src_install() {
251 > + debug-print-function ${FUNCNAME} "${@}"
252 > +
253 > + local bin
254 > + local dest="$(get_erl_libs)/${P}"
255 > +
256 > + insinto "${dest}"
257 > + doins -r ebin
258 > + [[ -d include ]] && doins -r include
259 > + [[ -d bin ]] && for bin in bin/*; do dobin "$bin"; done
260 > + [[ -d priv ]] && cp -pR priv "${ED}${dest}/"
261
262 Missing ||die. Just don't do it as one-liner :-P.
263
264 > +
265 > + einstalldocs
266 > +}
267
268
269
270 --
271 Best regards,
272 Michał Górny
273 <http://dev.gentoo.org/~mgorny/>

Replies