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