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