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