media-gfx/splash-themes-livecd: Merge gentoo-functions use fix
[gentoo.git] / eclass / java-ant-2.eclass
1 # Copyright 2004-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Id$
4
5 # @ECLASS: java-ant-2.eclass
6 # @MAINTAINER:
7 # java@gentoo.org
8 # @AUTHOR:
9 # kiorky (kiorky@cryptelium.net), Petteri Räty (betelgeuse@gentoo.org)
10 # @BLURB: eclass for ant based Java packages
11 # @DESCRIPTION:
12 # Eclass for Ant-based Java packages. Provides support for both automatic and
13 # manual manipulation of build.xml files. Should be inherited after java-pkg-2
14 # or java-pkg-opt-2 eclass.
15
16 inherit java-utils-2 multilib
17
18 # This eclass provides functionality for Java packages which use
19 # ant to build. In particular, it will attempt to fix build.xml files, so that
20 # they use the appropriate 'target' and 'source' attributes.
21
22 # @ECLASS-VARIABLE: WANT_ANT_TASKS
23 # @DEFAULT_UNSET
24 # @DESCRIPTION:
25 # An $IFS separated list of ant tasks.
26 # Ebuild can specify this variable before inheriting java-ant-2 eclass to
27 # determine ANT_TASKS it needs. They will be automatically translated to
28 # DEPEND variable and ANT_TASKS variable. JAVA_PKG_FORCE_ANT_TASKS can override
29 # ANT_TASKS set by WANT_ANT_TASKS, but not the DEPEND due to caching.
30 # Ebuilds that need to depend conditionally on certain tasks and specify them
31 # differently for different eant calls can't use this simplified approach.
32 # You also cannot specify version or anything else than ant-*.
33 #
34 # @CODE
35 # WANT_ANT_TASKS="ant-junit ant-trax"
36 # @CODE
37
38 #The implementation of dependencies is handled by java-utils-2.eclass
39 #WANT_ANT_TASKS
40
41 # @ECLASS-VARIABLE: JAVA_ANT_DISABLE_ANT_CORE_DEP
42 # @DEFAULT_UNSET
43 # @DESCRIPTION:
44 # Setting this variable non-empty before inheriting java-ant-2 disables adding
45 # dev-java/ant-core into DEPEND.
46 if [[ -z "${JAVA_ANT_DISABLE_ANT_CORE_DEP}" ]]; then
47         JAVA_ANT_E_DEPEND+=" >=dev-java/ant-core-1.8.2"
48         [[ "${EAPI:-0}" != 0 ]] && JAVA_ANT_E_DEPEND+=":0"
49 fi
50
51 # add ant tasks specified in WANT_ANT_TASKS to DEPEND
52 local ANT_TASKS_DEPEND;
53 ANT_TASKS_DEPEND="$(java-pkg_ant-tasks-depend)"
54 # check that java-pkg_ant-tasks-depend didn't fail
55 if [[ $? != 0 ]]; then
56         eerror "${ANT_TASKS_DEPEND}"
57         die "java-pkg_ant-tasks-depend() failed"
58 fi
59
60 # We need some tools from javatoolkit. We also need portage 2.1 for phase hooks
61 # and ant dependencies constructed above. Python is there for
62 # java-ant_remove-taskdefs
63 JAVA_ANT_E_DEPEND="${JAVA_ANT_E_DEPEND}
64            ${ANT_TASKS_DEPEND}
65            ${JAVA_PKG_PORTAGE_DEP}
66            >=dev-java/javatoolkit-0.3.0-r2"
67
68 # this eclass must be inherited after java-pkg-2 or java-pkg-opt-2
69 # if it's java-pkg-opt-2, ant dependencies are pulled based on USE flag
70 if has java-pkg-opt-2 ${INHERITED}; then
71         JAVA_ANT_E_DEPEND="${JAVA_PKG_OPT_USE}? ( ${JAVA_ANT_E_DEPEND} )"
72 elif ! has java-pkg-2 ${INHERITED}; then
73         eerror "java-ant-2 eclass can only be inherited AFTER java-pkg-2 or java-pkg-opt-2"
74 fi
75
76 DEPEND="${JAVA_ANT_E_DEPEND}"
77
78 # @ECLASS-VARIABLE: JAVA_PKG_BSFIX
79 # @DESCRIPTION:
80 # Should we attempt to 'fix' ant build files to include the source/target
81 # attributes when calling javac?
82 JAVA_PKG_BSFIX=${JAVA_PKG_BSFIX:-"on"}
83
84 # @ECLASS-VARIABLE: JAVA_PKG_BSFIX_ALL
85 # @DESCRIPTION:
86 # If we're fixing build files, should we try to fix all the ones we can find?
87 JAVA_PKG_BSFIX_ALL=${JAVA_PKG_BSFIX_ALL:-"yes"}
88
89 # @ECLASS-VARIABLE: JAVA_PKG_BSFIX_NAME
90 # @DESCRIPTION:
91 # Filename of build files to fix/search for
92 JAVA_PKG_BSFIX_NAME=${JAVA_PKG_BSFIX_NAME:-"build.xml"}
93
94 # @ECLASS-VARIABLE: JAVA_PKG_BSFIX_TARGET_TAGS
95 # @DESCRIPTION:
96 # Targets to fix the 'source' attribute in
97 JAVA_PKG_BSFIX_TARGET_TAGS=${JAVA_PKG_BSFIX_TARGET_TAGS:-"javac xjavac javac.preset"}
98
99 # @ECLASS-VARIABLE: JAVA_PKG_BSFIX_SOURCE_TAGS
100 # @DESCRIPTION:
101 # Targets to fix the 'target' attribute in
102 JAVA_PKG_BSFIX_SOURCE_TAGS=${JAVA_PKG_BSFIX_SOURCE_TAGS:-"javadoc javac xjavac javac.preset"}
103
104 # @ECLASS-VARIABLE: JAVA_ANT_CLASSPATH_TAGS
105 # @DESCRIPTION:
106 # Targets to add the classpath attribute to
107 JAVA_ANT_CLASSPATH_TAGS="javac xjavac"
108
109 # @ECLASS-VARIABLE: JAVA_ANT_IGNORE_SYSTEM_CLASSES
110 # @DEFAULT_UNSET
111 # @DESCRIPTION:
112 # When set, <available> Ant tasks are rewritten to ignore Ant's runtime classpath.
113
114 case "${EAPI:-0}" in
115         0|1) : ;;
116         *) EXPORT_FUNCTIONS src_configure ;;
117 esac
118
119 # @FUNCTION: java-ant-2_src_configure
120 # @DESCRIPTION:
121 # src_configure rewrites the build.xml files automatically, unless EAPI is undefined, 0 or 1.
122 java-ant-2_src_configure() {
123         # if java support is optional, don't perform this when the USE flag is off
124         if has java-pkg-opt-2 ${INHERITED}; then
125                 use ${JAVA_PKG_OPT_USE} || return
126         fi
127
128         # eant will call us unless called by Portage
129         [[ -e "${T}/java-ant-2_src_configure-run" ]] && return
130
131         [[ "${JAVA_ANT_IGNORE_SYSTEM_CLASSES}" ]] \
132                 && java-ant_ignore-system-classes "${S}/build.xml"
133
134         java-ant_bsfix
135         touch "${T}/java-ant-2_src_configure-run"
136 }
137
138 # @FUNCTION: java-ant_bsfix
139 # @INTERNAL
140 # @DESCRIPTION:
141 # Attempts to fix build files.
142 #
143 # @CODE
144 # Affected by variables:
145 #       JAVA_PKG_BSFIX
146 #       JAVA_PKG_BSFIX_ALL
147 #       JAVA_PKG_BSFIX_NAME,
148 # @CODE
149 java-ant_bsfix() {
150         debug-print-function ${FUNCNAME} $*
151
152         [[ "${JAVA_PKG_BSFIX}" != "on" ]] && return
153         if ! java-pkg_needs-vm; then
154                 echo "QA Notice: Package is using java-ant, but doesn't depend on a Java VM"
155         fi
156
157         pushd "${S}" >/dev/null || die
158
159         local find_args=""
160         [[ "${JAVA_PKG_BSFIX_ALL}" == "yes" ]] || find_args="-maxdepth 1"
161
162         find_args="${find_args} -type f ( -name ${JAVA_PKG_BSFIX_NAME// / -o -name } )"
163
164         # This voodoo is done for paths with spaces
165         local bsfix_these
166         while read line; do
167                 [[ -z ${line} ]] && continue
168                 bsfix_these="${bsfix_these} '${line}'"
169         done <<-EOF
170                         $(find . ${find_args})
171                 EOF
172
173         [[ "${bsfix_these// /}" ]] && eval java-ant_bsfix_files ${bsfix_these}
174
175         popd > /dev/null || die
176 }
177
178 _bsfix_die() {
179         if has_version dev-python/pyxml; then
180                 eerror "If the output above contains:"
181                 eerror "ImportError:"
182                 eerror "/usr/lib/python2.4/site-packages/_xmlplus/parsers/pyexpat.so:"
183                 eerror "undefined symbol: PyUnicodeUCS2_DecodeUTF8"
184                 eerror "Try re-emerging dev-python/pyxml"
185                 die ${1} " Look at the eerror message above"
186         else
187                 die ${1}
188         fi
189 }
190
191 # @FUNCTION: java-ant_bsfix_files
192 # @USAGE: <path/to/first/build.xml> [path/to/second.build.xml ...]
193 # @DESCRIPTION:
194 # Attempts to fix named build files.
195 #
196 # @CODE
197 # Affected by variables:
198 #       JAVA_PKG_BSFIX_SOURCE_TAGS
199 #       JAVA_PKG_BSFIX_TARGET_TAGS
200 #       JAVA_ANT_REWRITE_CLASSPATH
201 #       JAVA_ANT_JAVADOC_INPUT_DIRS: Where we can find java sources for javadoc
202 #                                input. Can be a space separated list of
203 #                                directories
204 #       JAVA_ANT_BSFIX_EXTRA_ARGS: You can use this to pass extra variables to the
205 #                                  rewriter if you know what you are doing.
206 # @CODE
207 #
208 # If JAVA_ANT_JAVADOC_INPUT_DIRS is set, we will turn on the adding of a basic
209 # javadoc target to the ant's build.xml with the javadoc xml-rewriter feature.
210 # Then we will set EANT DOC TARGET to the added javadoc target
211 # NOTE: the variable JAVA_ANT_JAVADOC_OUTPUT_DIR points where we will
212 #       generate the javadocs. This is a read-only variable, dont change it.
213
214 # When changing this function, make sure that it works with paths with spaces in
215 # them.
216 java-ant_bsfix_files() {
217         debug-print-function ${FUNCNAME} $*
218
219         [[ ${#} = 0 ]] && die "${FUNCNAME} called without arguments"
220
221         local want_source="$(java-pkg_get-source)"
222         local want_target="$(java-pkg_get-target)"
223
224         debug-print "${FUNCNAME}: target: ${want_target} source: ${want_source}"
225
226         if [ -z "${want_source}" -o -z "${want_target}" ]; then
227                 eerror "Could not find valid -source/-target values"
228                 eerror "Please file a bug about this on bugs.gentoo.org"
229                 die "Could not find valid -source/-target values"
230         else
231                 local files
232
233                 for file in "${@}"; do
234                         debug-print "${FUNCNAME}: ${file}"
235
236                         if [[ -n "${JAVA_PKG_DEBUG}" ]]; then
237                                 cp "${file}" "${file}.orig" || die "failed to copy ${file}"
238                         fi
239
240                         if [[ ! -w "${file}" ]]; then
241                                 chmod u+w "${file}" || die "chmod u+w ${file} failed"
242                         fi
243
244                         files="${files} -f '${file}'"
245                 done
246
247                 # for javadoc target and all in one pass, we need the new rewriter.
248                 local rewriter3="/usr/share/javatoolkit/xml-rewrite-3.py"
249                 if [[ ! -f ${rewriter3} ]]; then
250                         rewriter3="/usr/$(get_libdir)/javatoolkit/bin/xml-rewrite-3.py"
251                 fi
252
253                 local rewriter4="/usr/$(get_libdir)/javatoolkit/bin/build-xml-rewrite"
254
255                 if [[ -x ${rewriter4} && ${JAVA_ANT_ENCODING} ]]; then
256                         [[ ${JAVA_ANT_REWRITE_CLASSPATH} ]] && local gcp="-g"
257                         [[ ${JAVA_ANT_ENCODING} ]] && local enc="-e ${JAVA_ANT_ENCODING}"
258                         eval echo "cElementTree rewriter"
259                         debug-print "${rewriter4} extra args: ${gcp} ${enc}"
260                         ${rewriter4} ${gcp} ${enc} \
261                                 -c "${JAVA_PKG_BSFIX_SOURCE_TAGS}" source ${want_source} \
262                                 -c "${JAVA_PKG_BSFIX_TARGET_TAGS}" target ${want_target} \
263                                 "${@}" || die "build-xml-rewrite failed"
264                 elif [[ ! -f ${rewriter3} ]]; then
265                         debug-print "Using second generation rewriter"
266                         eval echo "Rewriting source attributes"
267                         eval xml-rewrite-2.py ${files} \
268                                 -c -e ${JAVA_PKG_BSFIX_SOURCE_TAGS// / -e } \
269                                 -a source -v ${want_source} || _bsfix_die "xml-rewrite2 failed: ${file}"
270
271                         eval echo "Rewriting target attributes"
272                         eval xml-rewrite-2.py ${files} \
273                                 -c -e ${JAVA_PKG_BSFIX_TARGET_TAGS// / -e } \
274                                 -a target -v ${want_target} || _bsfix_die "xml-rewrite2 failed: ${file}"
275
276                         eval echo "Rewriting nowarn attributes"
277                         eval xml-rewrite-2.py ${files} \
278                                 -c -e ${JAVA_PKG_BSFIX_TARGET_TAGS// / -e } \
279                                 -a nowarn -v yes || _bsfix_die "xml-rewrite2 failed: ${file}"
280
281                         if [[ ${JAVA_ANT_REWRITE_CLASSPATH} ]]; then
282                                 eval echo "Adding gentoo.classpath to javac tasks"
283                                 eval xml-rewrite-2.py ${files} \
284                                          -c -e javac -e xjavac -a classpath -v \
285                                          '\${gentoo.classpath}' \
286                                          || _bsfix_die "xml-rewrite2 failed"
287                         fi
288                 else
289                         debug-print "Using third generation rewriter"
290                         eval echo "Rewriting attributes"
291                         local bsfix_extra_args=""
292                         # WARNING KEEP THE ORDER, ESPECIALLY FOR CHANGED ATTRIBUTES!
293                         if [[ -n ${JAVA_ANT_REWRITE_CLASSPATH} ]]; then
294                                 local cp_tags="${JAVA_ANT_CLASSPATH_TAGS// / -e }"
295                                 bsfix_extra_args="${bsfix_extra_args} -g -e ${cp_tags}"
296                                 bsfix_extra_args="${bsfix_extra_args} -a classpath -v '\${gentoo.classpath}'"
297                         fi
298                         if [[ -n ${JAVA_ANT_JAVADOC_INPUT_DIRS} ]]; then
299                                 if [[ -n ${JAVA_ANT_JAVADOC_OUTPUT_DIR} ]]; then
300                                         die "Do not define JAVA_ANT_JAVADOC_OUTPUT_DIR!"
301                                 fi
302                                 # Where will our generated javadoc go.
303                                 readonly JAVA_ANT_JAVADOC_OUTPUT_DIR="${WORKDIR}/gentoo_javadoc"
304                                 mkdir -p "${JAVA_ANT_JAVADOC_OUTPUT_DIR}" || die
305
306                                 if has doc ${IUSE}; then
307                                         if use doc; then
308                                                 if [[ -z ${EANT_DOC_TARGET} ]]; then
309                                                         EANT_DOC_TARGET="gentoojavadoc"
310                                                 else
311                                                         die "You can't use javadoc adding and set EANT_DOC_TARGET too."
312                                                 fi
313
314                                                 for dir in ${JAVA_ANT_JAVADOC_INPUT_DIRS};do
315                                                         if [[ ! -d ${dir} ]]; then
316                                                                 eerror "This dir: ${dir} doesnt' exists"
317                                                                 die "You must specify directories for javadoc input/output dirs."
318                                                         fi
319                                                 done
320                                                 bsfix_extra_args="${bsfix_extra_args} --javadoc --source-directory "
321                                                 # filter third/double spaces
322                                                 JAVA_ANT_JAVADOC_INPUT_DIRS=${JAVA_ANT_JAVADOC_INPUT_DIRS//   /}
323                                                 JAVA_ANT_JAVADOC_INPUT_DIRS=${JAVA_ANT_JAVADOC_INPUT_DIRS//  /}
324                                                 bsfix_extra_args="${bsfix_extra_args} ${JAVA_ANT_JAVADOC_INPUT_DIRS// / --source-directory }"
325                                                 bsfix_extra_args="${bsfix_extra_args} --output-directory ${JAVA_ANT_JAVADOC_OUTPUT_DIR}"
326                                         fi
327                                 else
328                                         die "You need to have doc in IUSE when using JAVA_ANT_JAVADOC_INPUT_DIRS"
329                                 fi
330                         fi
331
332                         [[ -n ${JAVA_ANT_BSFIX_EXTRA_ARGS} ]] \
333                                 && bsfix_extra_args="${bsfix_extra_args} ${JAVA_ANT_BSFIX_EXTRA_ARGS}"
334
335                         debug-print "bsfix_extra_args: ${bsfix_extra_args}"
336
337                         eval ${rewriter3}  ${files} \
338                                 -c --source-element ${JAVA_PKG_BSFIX_SOURCE_TAGS// / --source-element } \
339                                 --source-attribute source --source-value ${want_source} \
340                                 --target-element   ${JAVA_PKG_BSFIX_TARGET_TAGS// / --target-element }  \
341                                 --target-attribute target --target-value ${want_target} \
342                                 --target-attribute nowarn --target-value yes \
343                                 ${bsfix_extra_args} \
344                                 || _bsfix_die "xml-rewrite2 failed: ${file}"
345                 fi
346
347                 if [[ -n "${JAVA_PKG_DEBUG}" ]]; then
348                         for file in "${@}"; do
349                                 diff -NurbB "${file}.orig" "${file}"
350                         done
351                 fi
352         fi
353         return 0 # so that the 1 for diff doesn't get reported
354 }
355
356
357 # @FUNCTION: java-ant_bsfix_one
358 # @USAGE: <path/to/build.xml>
359 # @DESCRIPTION:
360 # Attempts to fix named build file.
361 #
362 # @CODE
363 # Affected by variables:
364 #       JAVA_PKG_BSFIX_SOURCE_TAGS
365 #       JAVA_PKG_BSFIX_TARGET_TAGS
366 # @CODE
367 java-ant_bsfix_one() {
368         debug-print-function ${FUNCNAME} $*
369
370         if [ -z "${1}" ]; then
371                 eerror "${FUNCNAME} needs one argument"
372                 die "${FUNCNAME} needs one argument"
373         fi
374
375         java-ant_bsfix_files "${1}"
376 }
377
378 # @FUNCTION: java-ant_rewrite-classpath
379 # @USAGE: [path/to/build.xml]
380 # @DESCRIPTION:
381 # Adds 'classpath="${gentoo.classpath}"' to specified build file.
382 #
383 # Affected by:
384 #       JAVA_ANT_CLASSPATH_TAGS
385 #
386 # Parameter defaults to build.xml when not specified
387 java-ant_rewrite-classpath() {
388         debug-print-function ${FUNCNAME} $*
389
390         local file="${1}"
391         [[ -z "${1}" ]] && file=build.xml
392         [[ ${#} -gt 1 ]] && die "${FUNCNAME} currently can only rewrite one file."
393
394         echo "Adding gentoo.classpath to ${file}"
395         debug-print "java-ant_rewrite-classpath: ${file}"
396
397         cp "${file}" "${file}.orig" || die "failed to copy ${file}"
398
399         chmod u+w "${file}"
400
401         java-ant_xml-rewrite -f "${file}" --change \
402                 -e ${JAVA_ANT_CLASSPATH_TAGS// / -e } -a classpath -v '${gentoo.classpath}'
403
404         if [[ -n "${JAVA_PKG_DEBUG}" ]]; then
405                 diff -NurbB "${file}.orig" "${file}"
406         fi
407 }
408
409 # @FUNCTION: java-ant_remove-taskdefs
410 # @USAGE: [--name NAME] [path/to/build.xml]
411 # @DESCRIPTION:
412 # Removes (named) taskdef elements from the build.xml file.
413 # When --name NAME is specified, only remove taskdef with name NAME. Otherwise,
414 # all taskdefs are removed.
415 # The file to rewrite defaults to build.xml when not specified.
416 java-ant_remove-taskdefs() {
417         debug-print-function ${FUNCNAME} $*
418
419         die "${FUNCNAME} has been banned, see bug #479838."
420
421         local task_name
422         if [[ "${1}" == --name ]]; then
423                 task_name="${2}"
424                 shift 2
425         fi
426         local file="${1:-build.xml}"
427         echo "Removing taskdefs from ${file}"
428         python <<EOF
429 import sys
430 from xml.dom.minidom import parse
431 dom = parse("${file}")
432 for elem in dom.getElementsByTagName('taskdef'):
433         if (len("${task_name}") == 0 or elem.getAttribute("name") == "${task_name}"):
434                 elem.parentNode.removeChild(elem)
435                 elem.unlink()
436 f = open("${file}", "w")
437 dom.writexml(f)
438 f.close()
439 EOF
440         [[ $? != 0 ]] && die "Removing taskdefs failed"
441 }
442
443 # @FUNCTION: java-ant_ignore-system-classes
444 # @USAGE: [path/to/build.xml]
445 # @DESCRIPTION:
446 # Makes the available task ignore classes in the system classpath
447 # Parameter defaults to build.xml when not specified
448 java-ant_ignore-system-classes() {
449         debug-print-function ${FUNCNAME} $*
450         local file=${1:-build.xml}
451         echo "Changing ignoresystemclasses to true for available tasks in ${file}"
452         java-ant_xml-rewrite -f "${file}" --change \
453                 -e available -a ignoresystemclasses -v "true"
454 }
455
456 # @FUNCTION: java-ant_xml-rewrite
457 # @USAGE: <xml rewriter arguments>
458 # @DESCRIPTION:
459 # Run the right xml-rewrite binary with the given arguments
460 java-ant_xml-rewrite() {
461         local gen2="/usr/bin/xml-rewrite-2.py"
462         local gen2_1="/usr/$(get_libdir)/javatoolkit/bin/xml-rewrite-2.py"
463         # gen1 is deprecated
464         if [[ -x "${gen2}" ]]; then
465                 ${gen2} "${@}" || die "${gen2} failed"
466         elif [[ -x "${gen2_1}" ]]; then
467                 ${gen2_1} "${@}" || die "${gen2_1} failed"
468         else
469                 eerror "No binary for rewriting found."
470                 eerror "Do you have dev-java/javatoolkit installed?"
471                 die "xml-rewrite not found"
472         fi
473 }
474
475 # @FUNCTION: java-ant_rewrite-bootclasspath
476 # @USAGE: <version> [path/to/build.xml] [prepend] [append]
477 # @DESCRIPTION:
478 # Adds bootclasspath to javac-like tasks in build.xml filled with jars of a
479 # bootclasspath package of given version.
480 #
481 # @CODE
482 # Affected by:
483 #       JAVA_PKG_BSFIX_TARGET_TAGS - the tags of javac tasks
484 #
485 # Parameters:
486 # $1 - the version of bootclasspath (e.g. 1.5), 'auto' for bootclasspath
487 #      of the current JDK
488 # $2 - path to desired build.xml file, defaults to 'build.xml'
489 # $3 - (optional) what to prepend the bootclasspath with (to override)
490 # $4 - (optional) what to append to the bootclasspath
491 # @CODE
492 java-ant_rewrite-bootclasspath() {
493         local version="${1}"
494         local file="${2-build.xml}"
495         local extra_before="${3}"
496         local extra_after="${4}"
497
498         local bcp="$(java-pkg_get-bootclasspath "${version}")"
499
500         if [[ -n "${extra_before}" ]]; then
501                 bcp="${extra_before}:${bcp}"
502         fi
503         if [[ -n "${extra_after}" ]]; then
504                 bcp="${bcp}:${extra_after}"
505         fi
506
507         java-ant_xml-rewrite -f "${file}" -c -e ${JAVA_PKG_BSFIX_TARGET_TAGS// / -e } \
508                 -a bootclasspath -v "${bcp}"
509 }