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: Sat, 21 May 2016 23:20:09
Message-Id: 1463872789-18083-1-git-send-email-aidecoe@gentoo.org
In Reply to: [gentoo-dev] [PATCH] rebar.eclass: Build Erlang/OTP projects using dev-util/rebar by 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 | 223 ++++++++++++++++++++++++++++++++++++++++++++++++++++
8 1 file changed, 223 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..1a4eaba
14 --- /dev/null
15 +++ b/eclass/rebar.eclass
16 @@ -0,0 +1,223 @@
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 +EXPORT_FUNCTIONS src_prepare src_compile src_install
50 +
51 +RDEPEND="dev-lang/erlang"
52 +DEPEND="${RDEPEND}
53 + dev-util/rebar
54 + >=sys-apps/gawk-4.1"
55 +
56 +# @ECLASS-VARIABLE: REBAR_APP_SRC
57 +# @DESCRIPTION:
58 +# Relative path to .app.src description file.
59 +REBAR_APP_SRC="${REBAR_APP_SRC-src/${PN}.app.src}"
60 +
61 +# @FUNCTION: get_erl_libs
62 +# @RETURN: the path to Erlang lib directory
63 +# @DESCRIPTION:
64 +# Get the full path without EPREFIX to Erlang lib directory.
65 +get_erl_libs() {
66 + echo "/usr/$(get_libdir)/erlang/lib"
67 +}
68 +
69 +# @FUNCTION: _rebar_find_dep
70 +# @INTERNAL
71 +# @USAGE: <project_name>
72 +# @RETURN: full path with EPREFIX to a Erlang package/project on success,
73 +# code 1 when dependency is not found and code 2 if multiple versions of
74 +# dependency are found.
75 +# @DESCRIPTION:
76 +# Find a Erlang package/project by name in Erlang lib directory. Project
77 +# directory is usually suffixed with version. It is matched to '<project_name>'
78 +# or '<project_name>-*'.
79 +_rebar_find_dep() {
80 + local pn="$1"
81 + local p
82 + local result
83 +
84 + pushd "${EPREFIX}$(get_erl_libs)" >/dev/null || return 1
85 + for p in ${pn} ${pn}-*; do
86 + if [[ -d ${p} ]]; then
87 + # Ensure there's at most one matching.
88 + [[ ${result} ]] && return 2
89 + result="${p}"
90 + fi
91 + done
92 + popd >/dev/null || die
93 +
94 + [[ ${result} ]] || return 1
95 + echo "${result}"
96 +}
97 +
98 +# @FUNCTION: erebar
99 +# @USAGE: <targets>
100 +# @DESCRIPTION:
101 +# Run rebar with verbose flag. Die on failure.
102 +erebar() {
103 + debug-print-function ${FUNCNAME} "${@}"
104 +
105 + (( $# > 0 )) || die "erebar: at least one target is required"
106 +
107 + local -x ERL_LIBS="${EPREFIX}$(get_erl_libs)"
108 + rebar -v skip_deps=true "$@" || die "rebar $@ failed"
109 +}
110 +
111 +# @FUNCTION: rebar_fix_include_path
112 +# @USAGE: <project_name>
113 +# @DESCRIPTION:
114 +# Fix path in rebar.config to 'include' directory of dependant project/package,
115 +# so it points to installation in system Erlang lib rather than relative 'deps'
116 +# directory.
117 +#
118 +# The function dies on failure.
119 +rebar_fix_include_path() {
120 + debug-print-function ${FUNCNAME} "${@}"
121 +
122 + local pn="$1"
123 + local erl_libs="${EPREFIX}$(get_erl_libs)"
124 + local p
125 +
126 + p="$(_rebar_find_dep "${pn}")" \
127 + || die "failed to unambiguously resolve dependency of '${pn}'"
128 +
129 + gawk -i inplace \
130 + -v erl_libs="${erl_libs}" -v pn="${pn}" -v p="${p}" '
131 +/^{[[:space:]]*erl_opts[[:space:]]*,/, /}[[:space:]]*\.$/ {
132 + pattern = "\"(./)?deps/" pn "/include\"";
133 + if (match($0, "{i,[[:space:]]*" pattern "[[:space:]]*}")) {
134 + sub(pattern, "\"" erl_libs "/" p "/include\"");
135 + }
136 + print $0;
137 + next;
138 +}
139 +1
140 +' rebar.config || die "failed to fix include paths in rebar.config for '${pn}'"
141 +}
142 +
143 +# @FUNCTION: rebar_remove_deps
144 +# @DESCRIPTION:
145 +# Remove dependencies list from rebar.config and deceive build rules that any
146 +# dependencies are already fetched and built. Otherwise rebar tries to fetch
147 +# dependencies and compile them.
148 +#
149 +# The function dies on failure.
150 +rebar_remove_deps() {
151 + debug-print-function ${FUNCNAME} "${@}"
152 +
153 + mkdir -p "${S}/deps" && :>"${S}/deps/.got" && :>"${S}/deps/.built" || die
154 + gawk -i inplace '
155 +/^{[[:space:]]*deps[[:space:]]*,/, /}[[:space:]]*\.$/ {
156 + if ($0 ~ /}[[:space:]]*\.$/) {
157 + print "{deps, []}.";
158 + }
159 + next;
160 +}
161 +1
162 +' rebar.config || die "failed to remove deps from rebar.config"
163 +}
164 +
165 +# @FUNCTION: rebar_set_vsn
166 +# @USAGE: [<version>]
167 +# @DESCRIPTION:
168 +# Set version in project description file if it's not set.
169 +#
170 +# <version> is optional. Default is PV stripped from version suffix.
171 +#
172 +# The function dies on failure.
173 +rebar_set_vsn() {
174 + debug-print-function ${FUNCNAME} "${@}"
175 +
176 + local version="${1:-${PV%_*}}"
177 +
178 + sed -e "s/vsn, git/vsn, \"${version}\"/" \
179 + -i "${S}/${REBAR_APP_SRC}" \
180 + || die "failed to set version in src/${PN}.app.src"
181 +}
182 +
183 +# @FUNCTION: rebar_src_prepare
184 +# @DESCRIPTION:
185 +# Prevent rebar from fetching in compiling dependencies. Set version in project
186 +# description file if it's not set.
187 +#
188 +# Existence of rebar.config is optional, but file description file must exist
189 +# at 'src/${PN}.app.src'.
190 +rebar_src_prepare() {
191 + debug-print-function ${FUNCNAME} "${@}"
192 +
193 + default
194 + rebar_set_vsn
195 + [[ -f rebar.config ]] && rebar_remove_deps
196 +}
197 +
198 +# @FUNCTION: rebar_src_configure
199 +# @DESCRIPTION:
200 +# Configure with ERL_LIBS set.
201 +rebar_src_configure() {
202 + debug-print-function ${FUNCNAME} "${@}"
203 +
204 + local -x ERL_LIBS="${EPREFIX}$(get_erl_libs)"
205 + default
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.
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 +
234 + if [[ -d priv ]]; then
235 + cp -pR priv "${ED}${dest}/" || die "failed to install priv/"
236 + fi
237 +
238 + einstalldocs
239 +}
240 --
241 2.8.2

Replies