Gentoo Archives: gentoo-dev

From: aidecoe@g.o
To: gentoo-dev@l.g.o
Cc: "Amadeusz Żołnowski" <aidecoe@g.o>
Subject: [gentoo-dev] [PATCH] rebar.eclass: Build Erlang/OTP projects using dev-util/rebar
Date: Wed, 18 May 2016 22:35:33
Message-Id: 1463610918-4062-1-git-send-email-aidecoe@gentoo.org
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 | 220 ++++++++++++++++++++++++++++++++++++++++++++++++++++
8 1 file changed, 220 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..e1ac52f
14 --- /dev/null
15 +++ b/eclass/rebar.eclass
16 @@ -0,0 +1,220 @@
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)
40 + die "Unsupported EAPI=${EAPI:-0} (too old) for ${ECLASS}"
41 + ;;
42 + 5|6)
43 + ;;
44 + *)
45 + die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
46 + ;;
47 +esac
48 +
49 +[[ ${EAPI} = 5 ]] && 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 +# @FUNCTION: get_erl_libs
58 +# @RETURN: the path to Erlang lib directory
59 +# @DESCRIPTION:
60 +# Get the full path without EPREFIX to Erlang lib directory.
61 +get_erl_libs() {
62 + echo "/usr/$(get_libdir)/erlang/lib"
63 +}
64 +
65 +# @VARIABLE: ERL_LIBS
66 +# @DESCRIPTION:
67 +# Full path with EPREFIX to Erlang lib directory. Some rebar scripts expect it.
68 +export ERL_LIBS="${EPREFIX}$(get_erl_libs)"
69 +
70 +# @FUNCTION: _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 +_find_dep_version() {
79 + local pn="$1"
80 + local p
81 +
82 + pushd "${EPREFIX}$(get_erl_libs)" >/dev/null
83 + for p in ${pn} ${pn}-*; do
84 + if [[ -d ${p} ]]; then
85 + echo "${p#${pn}-}"
86 + return 0
87 + fi
88 + done
89 + popd >/dev/null
90 +
91 + return 1
92 +}
93 +
94 +# @FUNCTION: eawk
95 +# @USAGE: <file> <args>
96 +# @DESCRIPTION:
97 +# Edit file <file> in place with awk. Pass all arguments following <file> to
98 +# awk.
99 +eawk() {
100 + local f="$1"; shift
101 + local tmpf="$(emktemp)"
102 +
103 + cat "${f}" >"${tmpf}" || return 1
104 + awk "$@" "${tmpf}" >"${f}"
105 +}
106 +
107 +# @FUNCTION: erebar
108 +# @USAGE: <target>
109 +# @DESCRIPTION:
110 +# Run rebar with verbose flag. Die on failure.
111 +erebar() {
112 + debug-print-function ${FUNCNAME} "${@}"
113 +
114 + rebar -v skip_deps=true "$1" || die "rebar $1 failed"
115 +}
116 +
117 +# @FUNCTION: rebar_fix_include_path
118 +# @USAGE: <project_name> [<rebar_config>]
119 +# @DESCRIPTION:
120 +# Fix path in rebar.config to 'include' directory of dependant project/package,
121 +# so it points to installation in system Erlang lib rather than relative 'deps'
122 +# directory.
123 +#
124 +# <rebar_config> is optional. Default is 'rebar.config'.
125 +#
126 +# The function dies on failure.
127 +rebar_fix_include_path() {
128 + debug-print-function ${FUNCNAME} "${@}"
129 +
130 + local pn="$1"
131 + local rebar_config="${2:-rebar.config}"
132 + local erl_libs="${EPREFIX}$(get_erl_libs)"
133 + local pv="$(_find_dep_version "${pn}")"
134 +
135 + eawk "${rebar_config}" \
136 + -v erl_libs="${erl_libs}" -v pn="${pn}" -v pv="${pv}" \
137 + '/^{[[:space:]]*erl_opts[[:space:]]*,/, /}[[:space:]]*\.$/ {
138 + pattern = "\"(./)?deps/" pn "/include\"";
139 + if (match($0, "{i,[[:space:]]*" pattern "[[:space:]]*}")) {
140 + sub(pattern, "\"" erl_libs "/" pn "-" pv "/include\"");
141 + }
142 + print $0;
143 + next;
144 +}
145 +1
146 +' || die "failed to fix include paths in ${rebar_config}"
147 +}
148 +
149 +# @FUNCTION: rebar_remove_deps
150 +# @USAGE: [<rebar_config>]
151 +# @DESCRIPTION:
152 +# Remove dependencies list from rebar.config and deceive build rules that any
153 +# dependencies are already fetched and built. Otherwise rebar tries to fetch
154 +# dependencies and compile them.
155 +#
156 +# <rebar_config> is optional. Default is 'rebar.config'.
157 +#
158 +# The function dies on failure.
159 +rebar_remove_deps() {
160 + debug-print-function ${FUNCNAME} "${@}"
161 +
162 + local rebar_config="${1:-rebar.config}"
163 +
164 + mkdir -p "${S}/deps" && :>"${S}/deps/.got" && :>"${S}/deps/.built" || die
165 + eawk "${rebar_config}" \
166 + '/^{[[:space:]]*deps[[:space:]]*,/, /}[[:space:]]*\.$/ {
167 + if ($0 ~ /}[[:space:]]*\.$/) {
168 + print "{deps, []}.";
169 + }
170 + next;
171 +}
172 +1
173 +' || die "failed to remove deps from ${rebar_config}"
174 +}
175 +
176 +# @FUNCTION: rebar_set_vsn
177 +# @USAGE: [<version>]
178 +# @DESCRIPTION:
179 +# Set version in project description file if it's not set.
180 +#
181 +# <version> is optional. Default is PV stripped from version suffix.
182 +#
183 +# The function dies on failure.
184 +rebar_set_vsn() {
185 + debug-print-function ${FUNCNAME} "${@}"
186 +
187 + local version="${1:-${PV%_*}}"
188 +
189 + sed -e "s/vsn, git/vsn, \"${version}\"/" \
190 + -i "${S}/src/${PN}.app.src" \
191 + || die "failed to set version in src/${PN}.app.src"
192 +}
193 +
194 +# @FUNCTION: rebar_src_prepare
195 +# @DESCRIPTION:
196 +# Prevent rebar from fetching in compiling dependencies. Set version in project
197 +# description file if it's not set.
198 +#
199 +# Existence of rebar.config is optional, but file description file must exist
200 +# at 'src/${PN}.app.src'.
201 +rebar_src_prepare() {
202 + debug-print-function ${FUNCNAME} "${@}"
203 +
204 + rebar_set_vsn
205 + [[ -f rebar.config ]] && rebar_remove_deps
206 +}
207 +
208 +# @FUNCTION: rebar_src_compile
209 +# @DESCRIPTION:
210 +# Compile project with rebar.
211 +rebar_src_compile() {
212 + debug-print-function ${FUNCNAME} "${@}"
213 +
214 + erebar compile
215 +}
216 +
217 +# @FUNCTION: rebar_src_install
218 +# @DESCRIPTION:
219 +# Install BEAM files, include headers, executables and native libraries.
220 +# Install standard docs like README or defined in DOCS variable. Optionally
221 +#
222 +# Function expects that project conforms to Erlang/OTP structure.
223 +rebar_src_install() {
224 + debug-print-function ${FUNCNAME} "${@}"
225 +
226 + local bin
227 + local dest="$(get_erl_libs)/${P}"
228 +
229 + insinto "${dest}"
230 + doins -r ebin
231 + [[ -d include ]] && doins -r include
232 + [[ -d bin ]] && for bin in bin/*; do dobin "$bin"; done
233 + [[ -d priv ]] && cp -pR priv "${ED}${dest}/"
234 +
235 + einstalldocs
236 +}
237 --
238 2.8.2

Replies