dev-python/cryptography: Bump to 2.8
[gentoo.git] / eclass / rebar.eclass
1 # Copyright 1999-2016 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: rebar.eclass
5 # @MAINTAINER:
6 # Amadeusz Żołnowski <aidecoe@gentoo.org>
7 # @AUTHOR:
8 # Amadeusz Żołnowski <aidecoe@gentoo.org>
9 # @SUPPORTED_EAPIS: 6
10 # @BLURB: Build Erlang/OTP projects using dev-util/rebar.
11 # @DESCRIPTION:
12 # An eclass providing functions to build Erlang/OTP projects using
13 # dev-util/rebar.
14 #
15 # rebar is a tool which tries to resolve dependencies itself which is by
16 # cloning remote git repositories. Dependant projects are usually expected to
17 # be in sub-directory 'deps' rather than looking at system Erlang lib
18 # directory. Projects relying on rebar usually don't have 'install' make
19 # targets. The eclass workarounds some of these problems. It handles
20 # installation in a generic way for Erlang/OTP structured projects.
21
22 case "${EAPI:-0}" in
23         0|1|2|3|4|5)
24                 die "Unsupported EAPI=${EAPI:-0} (too old) for ${ECLASS}"
25                 ;;
26         6)
27                 ;;
28         *)
29                 die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
30                 ;;
31 esac
32
33 EXPORT_FUNCTIONS src_prepare src_compile src_test src_install
34
35 RDEPEND="dev-lang/erlang"
36 DEPEND="${RDEPEND}
37         dev-util/rebar
38         >=sys-apps/gawk-4.1"
39
40 # @ECLASS-VARIABLE: REBAR_APP_SRC
41 # @DESCRIPTION:
42 # Relative path to .app.src description file.
43 REBAR_APP_SRC="${REBAR_APP_SRC-src/${PN}.app.src}"
44
45 # @FUNCTION: get_erl_libs
46 # @RETURN: the path to Erlang lib directory
47 # @DESCRIPTION:
48 # Get the full path without EPREFIX to Erlang lib directory.
49 get_erl_libs() {
50         echo "/usr/$(get_libdir)/erlang/lib"
51 }
52
53 # @FUNCTION: _rebar_find_dep
54 # @INTERNAL
55 # @USAGE: <project_name>
56 # @RETURN: full path with EPREFIX to a Erlang package/project on success,
57 # code 1 when dependency is not found and code 2 if multiple versions of
58 # dependency are found.
59 # @DESCRIPTION:
60 # Find a Erlang package/project by name in Erlang lib directory. Project
61 # directory is usually suffixed with version. It is matched to '<project_name>'
62 # or '<project_name>-*'.
63 _rebar_find_dep() {
64         local pn="$1"
65         local p
66         local result
67
68         pushd "${EPREFIX}$(get_erl_libs)" >/dev/null || return 1
69         for p in ${pn} ${pn}-*; do
70                 if [[ -d ${p} ]]; then
71                         # Ensure there's at most one matching.
72                         [[ ${result} ]] && return 2
73                         result="${p}"
74                 fi
75         done
76         popd >/dev/null || die
77
78         [[ ${result} ]] || return 1
79         echo "${result}"
80 }
81
82 # @FUNCTION: rebar_disable_coverage
83 # @USAGE: [<rebar_config>]
84 # @DESCRIPTION:
85 # Disable coverage in rebar.config. This is a workaround for failing coverage.
86 # Coverage is not relevant in this context, so there's no harm to disable it,
87 # although the issue should be fixed.
88 rebar_disable_coverage() {
89         debug-print-function ${FUNCNAME} "${@}"
90
91         local rebar_config="${1:-rebar.config}"
92
93         sed -e 's/{cover_enabled, true}/{cover_enabled, false}/' \
94                 -i "${rebar_config}" \
95                 || die "failed to disable coverage in ${rebar_config}"
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 -n "rebar $@ failed"
109 }
110
111 # @FUNCTION: rebar_fix_include_path
112 # @USAGE: <project_name> [<rebar_config>]
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 # <rebar_config> is optional. Default is 'rebar.config'.
119 #
120 # The function dies on failure.
121 rebar_fix_include_path() {
122         debug-print-function ${FUNCNAME} "${@}"
123
124         local pn="$1"
125         local rebar_config="${2:-rebar.config}"
126         local erl_libs="${EPREFIX}$(get_erl_libs)"
127         local p
128
129         p="$(_rebar_find_dep "${pn}")" \
130                 || die "failed to unambiguously resolve dependency of '${pn}'"
131
132         gawk -i inplace \
133                 -v erl_libs="${erl_libs}" -v pn="${pn}" -v p="${p}" '
134 /^{[[:space:]]*erl_opts[[:space:]]*,/, /}[[:space:]]*\.$/ {
135         pattern = "\"(./)?deps/" pn "/include\"";
136         if (match($0, "{i,[[:space:]]*" pattern "[[:space:]]*}")) {
137                 sub(pattern, "\"" erl_libs "/" p "/include\"");
138         }
139         print $0;
140         next;
141 }
142 1
143 ' "${rebar_config}" || die "failed to fix include paths in ${rebar_config} for '${pn}'"
144 }
145
146 # @FUNCTION: rebar_remove_deps
147 # @USAGE: [<rebar_config>]
148 # @DESCRIPTION:
149 # Remove dependencies list from rebar.config and deceive build rules that any
150 # dependencies are already fetched and built. Otherwise rebar tries to fetch
151 # dependencies and compile them.
152 #
153 # <rebar_config> is optional. Default is 'rebar.config'.
154 #
155 # The function dies on failure.
156 rebar_remove_deps() {
157         debug-print-function ${FUNCNAME} "${@}"
158
159         local rebar_config="${1:-rebar.config}"
160
161         mkdir -p "${S}/deps" && :>"${S}/deps/.got" && :>"${S}/deps/.built" || die
162         gawk -i inplace '
163 /^{[[:space:]]*deps[[:space:]]*,/, /}[[:space:]]*\.$/ {
164         if ($0 ~ /}[[:space:]]*\.$/) {
165                 print "{deps, []}.";
166         }
167         next;
168 }
169 1
170 ' "${rebar_config}" || die "failed to remove deps from ${rebar_config}"
171 }
172
173 # @FUNCTION: rebar_set_vsn
174 # @USAGE: [<version>]
175 # @DESCRIPTION:
176 # Set version in project description file if it's not set.
177 #
178 # <version> is optional. Default is PV stripped from version suffix.
179 #
180 # The function dies on failure.
181 rebar_set_vsn() {
182         debug-print-function ${FUNCNAME} "${@}"
183
184         local version="${1:-${PV%_*}}"
185
186         sed -e "s/vsn, git/vsn, \"${version}\"/" \
187                 -i "${S}/${REBAR_APP_SRC}" \
188                 || die "failed to set version in src/${PN}.app.src"
189 }
190
191 # @FUNCTION: rebar_src_prepare
192 # @DESCRIPTION:
193 # Prevent rebar from fetching and compiling dependencies. Set version in
194 # project description file if it's not set.
195 #
196 # Existence of rebar.config is optional, but file description file must exist
197 # at 'src/${PN}.app.src'.
198 rebar_src_prepare() {
199         debug-print-function ${FUNCNAME} "${@}"
200
201         default
202         rebar_set_vsn
203         if [[ -f rebar.config ]]; then
204                 rebar_disable_coverage
205                 rebar_remove_deps
206         fi
207 }
208
209 # @FUNCTION: rebar_src_configure
210 # @DESCRIPTION:
211 # Configure with ERL_LIBS set.
212 rebar_src_configure() {
213         debug-print-function ${FUNCNAME} "${@}"
214
215         local -x ERL_LIBS="${EPREFIX}$(get_erl_libs)"
216         default
217 }
218
219 # @FUNCTION: rebar_src_compile
220 # @DESCRIPTION:
221 # Compile project with rebar.
222 rebar_src_compile() {
223         debug-print-function ${FUNCNAME} "${@}"
224
225         erebar compile
226 }
227
228 # @FUNCTION: rebar_src_test
229 # @DESCRIPTION:
230 # Run unit tests.
231 rebar_src_test() {
232         debug-print-function ${FUNCNAME} "${@}"
233
234         erebar eunit
235 }
236
237 # @FUNCTION: rebar_src_install
238 # @DESCRIPTION:
239 # Install BEAM files, include headers, executables and native libraries.
240 # Install standard docs like README or defined in DOCS variable.
241 #
242 # Function expects that project conforms to Erlang/OTP structure.
243 rebar_src_install() {
244         debug-print-function ${FUNCNAME} "${@}"
245
246         local bin
247         local dest="$(get_erl_libs)/${P}"
248
249         insinto "${dest}"
250         doins -r ebin
251         [[ -d include ]] && doins -r include
252         [[ -d bin ]] && for bin in bin/*; do dobin "$bin"; done
253
254         if [[ -d priv ]]; then
255                 cp -pR priv "${ED}${dest}/" || die "failed to install priv/"
256         fi
257
258         einstalldocs
259 }