rebar.eclass: Build Erlang/OTP projects using dev-util/rebar
[gentoo.git] / eclass / rebar.eclass
1 # Copyright 1999-2016 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Id$
4
5 # @ECLASS: rebar.eclass
6 # @MAINTAINER:
7 # Amadeusz Żołnowski <aidecoe@gentoo.org>
8 # @AUTHOR:
9 # Amadeusz Żołnowski <aidecoe@gentoo.org>
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_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: erebar
83 # @USAGE: <targets>
84 # @DESCRIPTION:
85 # Run rebar with verbose flag. Die on failure.
86 erebar() {
87         debug-print-function ${FUNCNAME} "${@}"
88
89         (( $# > 0 )) || die "erebar: at least one target is required"
90
91         local -x ERL_LIBS="${EPREFIX}$(get_erl_libs)"
92         rebar -v skip_deps=true "$@" || die -n "rebar $@ failed"
93 }
94
95 # @FUNCTION: rebar_fix_include_path
96 # @USAGE: <project_name>
97 # @DESCRIPTION:
98 # Fix path in rebar.config to 'include' directory of dependant project/package,
99 # so it points to installation in system Erlang lib rather than relative 'deps'
100 # directory.
101 #
102 # The function dies on failure.
103 rebar_fix_include_path() {
104         debug-print-function ${FUNCNAME} "${@}"
105
106         local pn="$1"
107         local erl_libs="${EPREFIX}$(get_erl_libs)"
108         local p
109
110         p="$(_rebar_find_dep "${pn}")" \
111                 || die "failed to unambiguously resolve dependency of '${pn}'"
112
113         gawk -i inplace \
114                 -v erl_libs="${erl_libs}" -v pn="${pn}" -v p="${p}" '
115 /^{[[:space:]]*erl_opts[[:space:]]*,/, /}[[:space:]]*\.$/ {
116         pattern = "\"(./)?deps/" pn "/include\"";
117         if (match($0, "{i,[[:space:]]*" pattern "[[:space:]]*}")) {
118                 sub(pattern, "\"" erl_libs "/" p "/include\"");
119         }
120         print $0;
121         next;
122 }
123 1
124 ' rebar.config || die "failed to fix include paths in rebar.config for '${pn}'"
125 }
126
127 # @FUNCTION: rebar_remove_deps
128 # @DESCRIPTION:
129 # Remove dependencies list from rebar.config and deceive build rules that any
130 # dependencies are already fetched and built. Otherwise rebar tries to fetch
131 # dependencies and compile them.
132 #
133 # The function dies on failure.
134 rebar_remove_deps() {
135         debug-print-function ${FUNCNAME} "${@}"
136
137         mkdir -p "${S}/deps" && :>"${S}/deps/.got" && :>"${S}/deps/.built" || die
138         gawk -i inplace '
139 /^{[[:space:]]*deps[[:space:]]*,/, /}[[:space:]]*\.$/ {
140         if ($0 ~ /}[[:space:]]*\.$/) {
141                 print "{deps, []}.";
142         }
143         next;
144 }
145 1
146 ' rebar.config || die "failed to remove deps from rebar.config"
147 }
148
149 # @FUNCTION: rebar_set_vsn
150 # @USAGE: [<version>]
151 # @DESCRIPTION:
152 # Set version in project description file if it's not set.
153 #
154 # <version> is optional. Default is PV stripped from version suffix.
155 #
156 # The function dies on failure.
157 rebar_set_vsn() {
158         debug-print-function ${FUNCNAME} "${@}"
159
160         local version="${1:-${PV%_*}}"
161
162         sed -e "s/vsn, git/vsn, \"${version}\"/" \
163                 -i "${S}/${REBAR_APP_SRC}" \
164                 || die "failed to set version in src/${PN}.app.src"
165 }
166
167 # @FUNCTION: rebar_src_prepare
168 # @DESCRIPTION:
169 # Prevent rebar from fetching and compiling dependencies. Set version in
170 # project description file if it's not set.
171 #
172 # Existence of rebar.config is optional, but file description file must exist
173 # at 'src/${PN}.app.src'.
174 rebar_src_prepare() {
175         debug-print-function ${FUNCNAME} "${@}"
176
177         default
178         rebar_set_vsn
179         [[ -f rebar.config ]] && rebar_remove_deps
180 }
181
182 # @FUNCTION: rebar_src_configure
183 # @DESCRIPTION:
184 # Configure with ERL_LIBS set.
185 rebar_src_configure() {
186         debug-print-function ${FUNCNAME} "${@}"
187
188         local -x ERL_LIBS="${EPREFIX}$(get_erl_libs)"
189         default
190 }
191
192 # @FUNCTION: rebar_src_compile
193 # @DESCRIPTION:
194 # Compile project with rebar.
195 rebar_src_compile() {
196         debug-print-function ${FUNCNAME} "${@}"
197
198         erebar compile
199 }
200
201 # @FUNCTION: rebar_src_install
202 # @DESCRIPTION:
203 # Install BEAM files, include headers, executables and native libraries.
204 # Install standard docs like README or defined in DOCS variable.
205 #
206 # Function expects that project conforms to Erlang/OTP structure.
207 rebar_src_install() {
208         debug-print-function ${FUNCNAME} "${@}"
209
210         local bin
211         local dest="$(get_erl_libs)/${P}"
212
213         insinto "${dest}"
214         doins -r ebin
215         [[ -d include ]] && doins -r include
216         [[ -d bin ]] && for bin in bin/*; do dobin "$bin"; done
217
218         if [[ -d priv ]]; then
219                 cp -pR priv "${ED}${dest}/" || die "failed to install priv/"
220         fi
221
222         einstalldocs
223 }