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