darcs.eclass: use BDEPEND with EAPI >= 7
[gentoo.git] / eclass / qmake-utils.eclass
1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: qmake-utils.eclass
5 # @MAINTAINER:
6 # qt@gentoo.org
7 # @AUTHOR:
8 # Davide Pesavento <pesa@gentoo.org>
9 # @BLURB: Common functions for qmake-based packages.
10 # @DESCRIPTION:
11 # Utility eclass providing wrapper functions for Qt4 and Qt5 qmake.
12 #
13 # This eclass does not set any metadata variables nor export any phase
14 # functions. It can be inherited safely.
15
16 if [[ -z ${_QMAKE_UTILS_ECLASS} ]]; then
17 _QMAKE_UTILS_ECLASS=1
18
19 [[ ${EAPI:-0} == [012345] ]] && inherit eutils multilib
20 inherit estack toolchain-funcs
21
22 # @FUNCTION: qt4_get_bindir
23 # @DESCRIPTION:
24 # Echoes the directory where Qt4 binaries are installed.
25 # EPREFIX is already prepended to the returned path.
26 qt4_get_bindir() {
27         [[ ${EAPI:-0} == [0123456] ]] || die "${FUNCNAME[1]} is banned in EAPI 7 and later"
28         has "${EAPI:-0}" 0 1 2 && use !prefix && EPREFIX=
29
30         local qtbindir=${EPREFIX}$(qt4_get_libdir)/bin
31         if [[ -d ${qtbindir} ]]; then
32                 echo ${qtbindir}
33         else
34                 echo ${EPREFIX}/usr/bin
35         fi
36 }
37
38 # @FUNCTION: qt4_get_headerdir
39 # @DESCRIPTION:
40 # Echoes the directory where Qt4 headers are installed.
41 qt4_get_headerdir() {
42         [[ ${EAPI:-0} == [0123456] ]] || die "${FUNCNAME[1]} is banned in EAPI 7 and later"
43         echo /usr/include/qt4
44 }
45
46 # @FUNCTION: qt4_get_libdir
47 # @DESCRIPTION:
48 # Echoes the directory where Qt4 libraries are installed.
49 qt4_get_libdir() {
50         [[ ${EAPI:-0} == [0123456] ]] || die "${FUNCNAME[1]} is banned in EAPI 7 and later"
51         echo /usr/$(get_libdir)/qt4
52 }
53
54 # @FUNCTION: qt4_get_mkspecsdir
55 # @DESCRIPTION:
56 # Echoes the directory where Qt4 mkspecs are installed.
57 qt4_get_mkspecsdir() {
58         [[ ${EAPI:-0} == [0123456] ]] || die "${FUNCNAME[1]} is banned in EAPI 7 and later"
59         echo /usr/share/qt4/mkspecs
60 }
61
62 # @FUNCTION: qt4_get_plugindir
63 # @DESCRIPTION:
64 # Echoes the directory where Qt4 plugins are installed.
65 qt4_get_plugindir() {
66         [[ ${EAPI:-0} == [0123456] ]] || die "${FUNCNAME[1]} is banned in EAPI 7 and later"
67         echo $(qt4_get_libdir)/plugins
68 }
69
70 # @FUNCTION: qt5_get_bindir
71 # @DESCRIPTION:
72 # Echoes the directory where Qt5 binaries are installed.
73 # EPREFIX is already prepended to the returned path.
74 qt5_get_bindir() {
75         has "${EAPI:-0}" 0 1 2 && use !prefix && EPREFIX=
76
77         echo ${EPREFIX}$(qt5_get_libdir)/qt5/bin
78 }
79
80 # @FUNCTION: qt5_get_headerdir
81 # @DESCRIPTION:
82 # Echoes the directory where Qt5 headers are installed.
83 qt5_get_headerdir() {
84         echo /usr/include/qt5
85 }
86
87 # @FUNCTION: qt5_get_libdir
88 # @DESCRIPTION:
89 # Echoes the directory where Qt5 libraries are installed.
90 qt5_get_libdir() {
91         echo /usr/$(get_libdir)
92 }
93
94 # @FUNCTION: qt5_get_mkspecsdir
95 # @DESCRIPTION:
96 # Echoes the directory where Qt5 mkspecs are installed.
97 qt5_get_mkspecsdir() {
98         echo $(qt5_get_libdir)/qt5/mkspecs
99 }
100
101 # @FUNCTION: qt5_get_plugindir
102 # @DESCRIPTION:
103 # Echoes the directory where Qt5 plugins are installed.
104 qt5_get_plugindir() {
105         echo $(qt5_get_libdir)/qt5/plugins
106 }
107
108 # @FUNCTION: qmake-utils_find_pro_file
109 # @RETURN: zero or one qmake .pro file names
110 # @INTERNAL
111 # @DESCRIPTION:
112 # Outputs a project file name that can be passed to eqmake.
113 #   0 *.pro files found --> outputs null string;
114 #   1 *.pro file found --> outputs its name;
115 #   2 or more *.pro files found --> if "${PN}.pro" or
116 #       "$(basename ${S}).pro" are there, outputs one of them.
117 qmake-utils_find_pro_file() {
118         local dir_name=$(basename "${S}")
119
120         # set nullglob to avoid expanding *.pro to the literal
121         # string "*.pro" when there are no matching files
122         eshopts_push -s nullglob
123         local pro_files=(*.pro)
124         eshopts_pop
125
126         case ${#pro_files[@]} in
127         0)
128                 : ;;
129         1)
130                 echo "${pro_files}"
131                 ;;
132         *)
133                 for pro_file in "${pro_files[@]}"; do
134                         if [[ ${pro_file%.pro} == ${dir_name} || ${pro_file%.pro} == ${PN} ]]; then
135                                 echo "${pro_file}"
136                                 break
137                         fi
138                 done
139                 ;;
140         esac
141 }
142
143 # @VARIABLE: EQMAKE4_EXCLUDE
144 # @DEFAULT_UNSET
145 # @DESCRIPTION:
146 # List of files to be excluded from eqmake4 CONFIG processing.
147 # Paths are relative to the current working directory (usually ${S}).
148 #
149 # Example: EQMAKE4_EXCLUDE="ignore/me.pro foo/*"
150
151 # @FUNCTION: eqmake4
152 # @USAGE: [project_file] [parameters to qmake]
153 # @DESCRIPTION:
154 # Wrapper for Qt4's qmake. If project_file is not specified, eqmake4 looks
155 # for one in the current directory (non-recursively). If multiple project
156 # files are found, then ${PN}.pro is used, if it exists, otherwise eqmake4
157 # will not be able to continue.
158 #
159 # All other arguments are appended unmodified to qmake command line.
160 #
161 # For recursive build systems, i.e. those based on the subdirs template,
162 # you should run eqmake4 on the top-level project file only, unless you
163 # have a valid reason to do otherwise. During the building, qmake will
164 # be automatically re-invoked with the right arguments on every directory
165 # specified inside the top-level project file.
166 eqmake4() {
167         debug-print-function ${FUNCNAME} "$@"
168
169         [[ ${EAPI:-0} == [0123456] ]] || die "${FUNCNAME[1]} is banned in EAPI 7 and later"
170         has "${EAPI:-0}" 0 1 2 && use !prefix && EPREFIX=
171
172         ebegin "Running qmake"
173
174         local qmake_args=("$@")
175
176         # Check if the project file name was passed as first argument. If not, look for candidates.
177         local regexp='.*\.pro'
178         if ! [[ ${1} =~ ${regexp} ]]; then
179                 local project_file=$(qmake-utils_find_pro_file)
180                 if [[ -z ${project_file} ]]; then
181                         echo
182                         eerror "No project files found in '${PWD}'"
183                         eerror "This shouldn't happen - please send a bug report to https://bugs.gentoo.org/"
184                         echo
185                         die "eqmake4 failed"
186                 fi
187                 qmake_args+=("${project_file}")
188         fi
189
190         # Make sure the CONFIG variable is correctly set for both release and debug builds.
191         local config_add=release
192         local config_remove=debug
193         if in_iuse debug && use debug; then
194                 config_add=debug
195                 config_remove=release
196         fi
197
198         local awkscript='BEGIN {
199                                 printf "### eqmake4 was here ###\n" > file;
200                                 printf "CONFIG -= debug_and_release %s\n", remove >> file;
201                                 printf "CONFIG += %s\n\n", add >> file;
202                                 fixed=0;
203                         }
204                         /^[[:blank:]]*CONFIG[[:blank:]]*[\+\*]?=/ {
205                                 if (gsub("\\<((" remove ")|(debug_and_release))\\>", "") > 0) {
206                                         fixed=1;
207                                 }
208                         }
209                         /^[[:blank:]]*CONFIG[[:blank:]]*-=/ {
210                                 if (gsub("\\<" add "\\>", "") > 0) {
211                                         fixed=1;
212                                 }
213                         }
214                         {
215                                 print >> file;
216                         }
217                         END {
218                                 print fixed;
219                         }'
220
221         [[ -n ${EQMAKE4_EXCLUDE} ]] && eshopts_push -o noglob
222
223         local file
224         while read file; do
225                 local excl
226                 for excl in ${EQMAKE4_EXCLUDE}; do
227                         [[ ${file} == ${excl} ]] && continue 2
228                 done
229                 grep -q '^### eqmake4 was here ###$' "${file}" && continue
230
231                 local retval=$({
232                         rm -f "${file}" || echo FAIL
233                         awk -v file="${file}" \
234                                 -v add=${config_add} \
235                                 -v remove=${config_remove} \
236                                 -- "${awkscript}" || echo FAIL
237                         } < "${file}")
238
239                 if [[ ${retval} == 1 ]]; then
240                         einfo " - fixed CONFIG in ${file}"
241                 elif [[ ${retval} != 0 ]]; then
242                         eerror " - error while processing ${file}"
243                         die "eqmake4 failed to process ${file}"
244                 fi
245         done < <(find . -type f -name '*.pr[io]' -printf '%P\n' 2>/dev/null)
246
247         [[ -n ${EQMAKE4_EXCLUDE} ]] && eshopts_pop
248
249         "$(qt4_get_bindir)"/qmake \
250                 -makefile \
251                 QMAKE_AR="$(tc-getAR) cqs" \
252                 QMAKE_CC="$(tc-getCC)" \
253                 QMAKE_CXX="$(tc-getCXX)" \
254                 QMAKE_LINK="$(tc-getCXX)" \
255                 QMAKE_LINK_C="$(tc-getCC)" \
256                 QMAKE_OBJCOPY="$(tc-getOBJCOPY)" \
257                 QMAKE_RANLIB= \
258                 QMAKE_STRIP= \
259                 QMAKE_CFLAGS="${CFLAGS}" \
260                 QMAKE_CFLAGS_RELEASE= \
261                 QMAKE_CFLAGS_DEBUG= \
262                 QMAKE_CXXFLAGS="${CXXFLAGS}" \
263                 QMAKE_CXXFLAGS_RELEASE= \
264                 QMAKE_CXXFLAGS_DEBUG= \
265                 QMAKE_LFLAGS="${LDFLAGS}" \
266                 QMAKE_LFLAGS_RELEASE= \
267                 QMAKE_LFLAGS_DEBUG= \
268                 QMAKE_LIBDIR_QT="${EPREFIX}$(qt4_get_libdir)" \
269                 QMAKE_LIBDIR_X11="${EPREFIX}/usr/$(get_libdir)" \
270                 QMAKE_LIBDIR_OPENGL="${EPREFIX}/usr/$(get_libdir)" \
271                 "${qmake_args[@]}"
272
273         if ! eend $? ; then
274                 echo
275                 eerror "Running qmake has failed! (see above for details)"
276                 eerror "This shouldn't happen - please send a bug report to https://bugs.gentoo.org/"
277                 echo
278                 die "eqmake4 failed"
279         fi
280 }
281
282 # @FUNCTION: eqmake5
283 # @USAGE: [arguments for qmake]
284 # @DESCRIPTION:
285 # Wrapper for Qt5's qmake. All arguments are passed to qmake.
286 #
287 # For recursive build systems, i.e. those based on the subdirs template,
288 # you should run eqmake5 on the top-level project file only, unless you
289 # have a valid reason to do otherwise. During the building, qmake will
290 # be automatically re-invoked with the right arguments on every directory
291 # specified inside the top-level project file.
292 eqmake5() {
293         debug-print-function ${FUNCNAME} "$@"
294
295         ebegin "Running qmake"
296
297         "$(qt5_get_bindir)"/qmake \
298                 -makefile \
299                 QMAKE_AR="$(tc-getAR) cqs" \
300                 QMAKE_CC="$(tc-getCC)" \
301                 QMAKE_LINK_C="$(tc-getCC)" \
302                 QMAKE_LINK_C_SHLIB="$(tc-getCC)" \
303                 QMAKE_CXX="$(tc-getCXX)" \
304                 QMAKE_LINK="$(tc-getCXX)" \
305                 QMAKE_LINK_SHLIB="$(tc-getCXX)" \
306                 QMAKE_OBJCOPY="$(tc-getOBJCOPY)" \
307                 QMAKE_RANLIB= \
308                 QMAKE_STRIP= \
309                 QMAKE_CFLAGS="${CFLAGS}" \
310                 QMAKE_CFLAGS_RELEASE= \
311                 QMAKE_CFLAGS_DEBUG= \
312                 QMAKE_CXXFLAGS="${CXXFLAGS}" \
313                 QMAKE_CXXFLAGS_RELEASE= \
314                 QMAKE_CXXFLAGS_DEBUG= \
315                 QMAKE_LFLAGS="${LDFLAGS}" \
316                 QMAKE_LFLAGS_RELEASE= \
317                 QMAKE_LFLAGS_DEBUG= \
318                 "$@"
319
320         if ! eend $? ; then
321                 echo
322                 eerror "Running qmake has failed! (see above for details)"
323                 eerror "This shouldn't happen - please send a bug report to https://bugs.gentoo.org/"
324                 echo
325                 die "eqmake5 failed"
326         fi
327 }
328
329 fi