dev-python/objgraph: keyworded 3.4.1 for ia64, bug #717946
[gentoo.git] / eclass / rebar.eclass
1 # Copyright 1999-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: rebar.eclass
5 # @MAINTAINER:
6 # maintainer-needed@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         [[ ${1} == eunit ]] && local -x ERL_LIBS="."
109
110         rebar -v skip_deps=true "$@" || die -n "rebar $@ failed"
111 }
112
113 # @FUNCTION: rebar_fix_include_path
114 # @USAGE: <project_name> [<rebar_config>]
115 # @DESCRIPTION:
116 # Fix path in rebar.config to 'include' directory of dependant project/package,
117 # so it points to installation in system Erlang lib rather than relative 'deps'
118 # directory.
119 #
120 # <rebar_config> is optional. Default is 'rebar.config'.
121 #
122 # The function dies on failure.
123 rebar_fix_include_path() {
124         debug-print-function ${FUNCNAME} "${@}"
125
126         local pn="$1"
127         local rebar_config="${2:-rebar.config}"
128         local erl_libs="${EPREFIX}$(get_erl_libs)"
129         local p
130
131         p="$(_rebar_find_dep "${pn}")" \
132                 || die "failed to unambiguously resolve dependency of '${pn}'"
133
134         gawk -i inplace \
135                 -v erl_libs="${erl_libs}" -v pn="${pn}" -v p="${p}" '
136 /^{[[:space:]]*erl_opts[[:space:]]*,/, /}[[:space:]]*\.$/ {
137         pattern = "\"(./)?deps/" pn "/include\"";
138         if (match($0, "{i,[[:space:]]*" pattern "[[:space:]]*}")) {
139                 sub(pattern, "\"" erl_libs "/" p "/include\"");
140         }
141         print $0;
142         next;
143 }
144 1
145 ' "${rebar_config}" || die "failed to fix include paths in ${rebar_config} for '${pn}'"
146 }
147
148 # @FUNCTION: rebar_remove_deps
149 # @USAGE: [<rebar_config>]
150 # @DESCRIPTION:
151 # Remove dependencies list from rebar.config and deceive build rules that any
152 # dependencies are already fetched and built. Otherwise rebar tries to fetch
153 # dependencies and compile them.
154 #
155 # <rebar_config> is optional. Default is 'rebar.config'.
156 #
157 # The function dies on failure.
158 rebar_remove_deps() {
159         debug-print-function ${FUNCNAME} "${@}"
160
161         local rebar_config="${1:-rebar.config}"
162
163         mkdir -p "${S}/deps" && :>"${S}/deps/.got" && :>"${S}/deps/.built" || die
164         gawk -i inplace '
165 /^{[[:space:]]*deps[[:space:]]*,/, /}[[:space:]]*\.$/ {
166         if ($0 ~ /}[[:space:]]*\.$/) {
167                 print "{deps, []}.";
168         }
169         next;
170 }
171 1
172 ' "${rebar_config}" || die "failed to remove deps from ${rebar_config}"
173 }
174
175 # @FUNCTION: rebar_set_vsn
176 # @USAGE: [<version>]
177 # @DESCRIPTION:
178 # Set version in project description file if it's not set.
179 #
180 # <version> is optional. Default is PV stripped from version suffix.
181 #
182 # The function dies on failure.
183 rebar_set_vsn() {
184         debug-print-function ${FUNCNAME} "${@}"
185
186         local version="${1:-${PV%_*}}"
187
188         sed -e "s/vsn, git/vsn, \"${version}\"/" \
189                 -i "${S}/${REBAR_APP_SRC}" \
190                 || die "failed to set version in src/${PN}.app.src"
191 }
192
193 # @FUNCTION: rebar_src_prepare
194 # @DESCRIPTION:
195 # Prevent rebar from fetching and compiling dependencies. Set version in
196 # project description file if it's not set.
197 #
198 # Existence of rebar.config is optional, but file description file must exist
199 # at 'src/${PN}.app.src'.
200 rebar_src_prepare() {
201         debug-print-function ${FUNCNAME} "${@}"
202
203         default
204         rebar_set_vsn
205         if [[ -f rebar.config ]]; then
206                 rebar_disable_coverage
207                 rebar_remove_deps
208         fi
209 }
210
211 # @FUNCTION: rebar_src_configure
212 # @DESCRIPTION:
213 # Configure with ERL_LIBS set.
214 rebar_src_configure() {
215         debug-print-function ${FUNCNAME} "${@}"
216
217         local -x ERL_LIBS="${EPREFIX}$(get_erl_libs)"
218         default
219 }
220
221 # @FUNCTION: rebar_src_compile
222 # @DESCRIPTION:
223 # Compile project with rebar.
224 rebar_src_compile() {
225         debug-print-function ${FUNCNAME} "${@}"
226
227         erebar compile
228 }
229
230 # @FUNCTION: rebar_src_test
231 # @DESCRIPTION:
232 # Run unit tests.
233 rebar_src_test() {
234         debug-print-function ${FUNCNAME} "${@}"
235
236         erebar eunit
237 }
238
239 # @FUNCTION: rebar_src_install
240 # @DESCRIPTION:
241 # Install BEAM files, include headers, executables and native libraries.
242 # Install standard docs like README or defined in DOCS variable.
243 #
244 # Function expects that project conforms to Erlang/OTP structure.
245 rebar_src_install() {
246         debug-print-function ${FUNCNAME} "${@}"
247
248         local bin
249         local dest="$(get_erl_libs)/${P}"
250
251         insinto "${dest}"
252         doins -r ebin
253         [[ -d include ]] && doins -r include
254         [[ -d bin ]] && for bin in bin/*; do dobin "$bin"; done
255
256         if [[ -d priv ]]; then
257                 cp -pR priv "${ED}${dest}/" || die "failed to install priv/"
258         fi
259
260         einstalldocs
261 }