1 # eclass for ant based Java packages
3 # Copyright (c) 2004-2005, Thomas Matthijs <axxo@gentoo.org>
4 # Copyright (c) 2004-2015, Gentoo Foundation
7 # Made bsfix make one pass for all things and add some glocal targets for
8 # setting up the whole thing. Contributed by kiorky
9 # (kiorky@cryptelium.net).
11 # I pretty much rewrote the logic of the bsfix functions
12 # and xml-rewrite.py because they were so slow
13 # Petteri Räty (betelgeuse@gentoo.org)
15 # Licensed under the GNU General Public License, v2
19 # @ECLASS: java-ant-2.eclass
23 # kiorky (kiorky@cryptelium.net), Petteri Räty (betelgeuse@gentoo.org)
24 # @BLURB: eclass for ant based Java packages
26 # Eclass for Ant-based Java packages. Provides support for both automatic and
27 # manual manipulation of build.xml files. Should be inherited after java-pkg-2
28 # or java-pkg-opt-2 eclass.
30 inherit java-utils-2 multilib
32 # This eclass provides functionality for Java packages which use
33 # ant to build. In particular, it will attempt to fix build.xml files, so that
34 # they use the appropriate 'target' and 'source' attributes.
36 # @ECLASS-VARIABLE: WANT_ANT_TASKS
39 # An $IFS separated list of ant tasks.
40 # Ebuild can specify this variable before inheriting java-ant-2 eclass to
41 # determine ANT_TASKS it needs. They will be automatically translated to
42 # DEPEND variable and ANT_TASKS variable. JAVA_PKG_FORCE_ANT_TASKS can override
43 # ANT_TASKS set by WANT_ANT_TASKS, but not the DEPEND due to caching.
44 # Ebuilds that need to depend conditionally on certain tasks and specify them
45 # differently for different eant calls can't use this simplified approach.
46 # You also cannot specify version or anything else than ant-*.
49 # WANT_ANT_TASKS="ant-junit ant-trax"
52 #The implementation of dependencies is handled by java-utils-2.eclass
55 # @ECLASS-VARIABLE: JAVA_ANT_DISABLE_ANT_CORE_DEP
58 # Setting this variable non-empty before inheriting java-ant-2 disables adding
59 # dev-java/ant-core into DEPEND.
60 if [[ -z "${JAVA_ANT_DISABLE_ANT_CORE_DEP}" ]]; then
61 JAVA_ANT_E_DEPEND+=" >=dev-java/ant-core-1.8.2"
62 [[ "${EAPI:-0}" != 0 ]] && JAVA_ANT_E_DEPEND+=":0"
65 # add ant tasks specified in WANT_ANT_TASKS to DEPEND
66 local ANT_TASKS_DEPEND;
67 ANT_TASKS_DEPEND="$(java-pkg_ant-tasks-depend)"
68 # check that java-pkg_ant-tasks-depend didn't fail
69 if [[ $? != 0 ]]; then
70 eerror "${ANT_TASKS_DEPEND}"
71 die "java-pkg_ant-tasks-depend() failed"
74 # We need some tools from javatoolkit. We also need portage 2.1 for phase hooks
75 # and ant dependencies constructed above. Python is there for
76 # java-ant_remove-taskdefs
77 JAVA_ANT_E_DEPEND="${JAVA_ANT_E_DEPEND}
79 ${JAVA_PKG_PORTAGE_DEP}
80 >=dev-java/javatoolkit-0.3.0-r2"
82 # this eclass must be inherited after java-pkg-2 or java-pkg-opt-2
83 # if it's java-pkg-opt-2, ant dependencies are pulled based on USE flag
84 if has java-pkg-opt-2 ${INHERITED}; then
85 JAVA_ANT_E_DEPEND="${JAVA_PKG_OPT_USE}? ( ${JAVA_ANT_E_DEPEND} )"
86 elif ! has java-pkg-2 ${INHERITED}; then
87 eerror "java-ant-2 eclass can only be inherited AFTER java-pkg-2 or java-pkg-opt-2"
90 DEPEND="${JAVA_ANT_E_DEPEND}"
92 # @ECLASS-VARIABLE: JAVA_PKG_BSFIX
94 # Should we attempt to 'fix' ant build files to include the source/target
95 # attributes when calling javac?
96 JAVA_PKG_BSFIX=${JAVA_PKG_BSFIX:-"on"}
98 # @ECLASS-VARIABLE: JAVA_PKG_BSFIX_ALL
100 # If we're fixing build files, should we try to fix all the ones we can find?
101 JAVA_PKG_BSFIX_ALL=${JAVA_PKG_BSFIX_ALL:-"yes"}
103 # @ECLASS-VARIABLE: JAVA_PKG_BSFIX_NAME
105 # Filename of build files to fix/search for
106 JAVA_PKG_BSFIX_NAME=${JAVA_PKG_BSFIX_NAME:-"build.xml"}
108 # @ECLASS-VARIABLE: JAVA_PKG_BSFIX_TARGET_TAGS
110 # Targets to fix the 'source' attribute in
111 JAVA_PKG_BSFIX_TARGET_TAGS=${JAVA_PKG_BSFIX_TARGET_TAGS:-"javac xjavac javac.preset"}
113 # @ECLASS-VARIABLE: JAVA_PKG_BSFIX_SOURCE_TAGS
115 # Targets to fix the 'target' attribute in
116 JAVA_PKG_BSFIX_SOURCE_TAGS=${JAVA_PKG_BSFIX_SOURCE_TAGS:-"javadoc javac xjavac javac.preset"}
118 # @ECLASS-VARIABLE: JAVA_ANT_CLASSPATH_TAGS
120 # Targets to add the classpath attribute to
121 JAVA_ANT_CLASSPATH_TAGS="javac xjavac"
123 # @ECLASS-VARIABLE: JAVA_ANT_IGNORE_SYSTEM_CLASSES
126 # When set, <available> Ant tasks are rewritten to ignore Ant's runtime classpath.
130 *) EXPORT_FUNCTIONS src_configure ;;
133 # @FUNCTION: java-ant-2_src_configure
135 # src_configure rewrites the build.xml files automatically, unless EAPI is undefined, 0 or 1.
136 java-ant-2_src_configure() {
137 # if java support is optional, don't perform this when the USE flag is off
138 if has java-pkg-opt-2 ${INHERITED}; then
139 use ${JAVA_PKG_OPT_USE} || return
142 # eant will call us unless called by Portage
143 [[ -e "${T}/java-ant-2_src_configure-run" ]] && return
145 [[ "${JAVA_ANT_IGNORE_SYSTEM_CLASSES}" ]] \
146 && java-ant_ignore-system-classes "${S}/build.xml"
149 touch "${T}/java-ant-2_src_configure-run"
152 # @FUNCTION: java-ant_bsfix
155 # Attempts to fix build files.
158 # Affected by variables:
161 # JAVA_PKG_BSFIX_NAME,
164 debug-print-function ${FUNCNAME} $*
166 [[ "${JAVA_PKG_BSFIX}" != "on" ]] && return
167 if ! java-pkg_needs-vm; then
168 echo "QA Notice: Package is using java-ant, but doesn't depend on a Java VM"
171 pushd "${S}" >/dev/null
174 [[ "${JAVA_PKG_BSFIX_ALL}" == "yes" ]] || find_args="-maxdepth 1"
176 find_args="${find_args} -type f ( -name ${JAVA_PKG_BSFIX_NAME// / -o -name } )"
178 # This voodoo is done for paths with spaces
181 [[ -z ${line} ]] && continue
182 bsfix_these="${bsfix_these} '${line}'"
184 $(find . ${find_args})
187 [[ "${bsfix_these// /}" ]] && eval java-ant_bsfix_files ${bsfix_these}
193 if has_version dev-python/pyxml; then
194 eerror "If the output above contains:"
195 eerror "ImportError:"
196 eerror "/usr/lib/python2.4/site-packages/_xmlplus/parsers/pyexpat.so:"
197 eerror "undefined symbol: PyUnicodeUCS2_DecodeUTF8"
198 eerror "Try re-emerging dev-python/pyxml"
199 die ${1} " Look at the eerror message above"
205 # @FUNCTION: java-ant_bsfix_files
206 # @USAGE: <path/to/first/build.xml> [path/to/second.build.xml ...]
208 # Attempts to fix named build files.
211 # Affected by variables:
212 # JAVA_PKG_BSFIX_SOURCE_TAGS
213 # JAVA_PKG_BSFIX_TARGET_TAGS
214 # JAVA_ANT_REWRITE_CLASSPATH
215 # JAVA_ANT_JAVADOC_INPUT_DIRS: Where we can find java sources for javadoc
216 # input. Can be a space separated list of
218 # JAVA_ANT_BSFIX_EXTRA_ARGS: You can use this to pass extra variables to the
219 # rewriter if you know what you are doing.
222 # If JAVA_ANT_JAVADOC_INPUT_DIRS is set, we will turn on the adding of a basic
223 # javadoc target to the ant's build.xml with the javadoc xml-rewriter feature.
224 # Then we will set EANT DOC TARGET to the added javadoc target
225 # NOTE: the variable JAVA_ANT_JAVADOC_OUTPUT_DIR points where we will
226 # generate the javadocs. This is a read-only variable, dont change it.
228 # When changing this function, make sure that it works with paths with spaces in
230 java-ant_bsfix_files() {
231 debug-print-function ${FUNCNAME} $*
233 [[ ${#} = 0 ]] && die "${FUNCNAME} called without arguments"
235 local want_source="$(java-pkg_get-source)"
236 local want_target="$(java-pkg_get-target)"
238 debug-print "${FUNCNAME}: target: ${want_target} source: ${want_source}"
240 if [ -z "${want_source}" -o -z "${want_target}" ]; then
241 eerror "Could not find valid -source/-target values"
242 eerror "Please file a bug about this on bugs.gentoo.org"
243 die "Could not find valid -source/-target values"
247 for file in "${@}"; do
248 debug-print "${FUNCNAME}: ${file}"
250 if [[ -n "${JAVA_PKG_DEBUG}" ]]; then
251 cp "${file}" "${file}.orig" || die "failed to copy ${file}"
254 if [[ ! -w "${file}" ]]; then
255 chmod u+w "${file}" || die "chmod u+w ${file} failed"
258 files="${files} -f '${file}'"
261 # for javadoc target and all in one pass, we need the new rewriter.
262 local rewriter3="/usr/share/javatoolkit/xml-rewrite-3.py"
263 if [[ ! -f ${rewriter3} ]]; then
264 rewriter3="/usr/$(get_libdir)/javatoolkit/bin/xml-rewrite-3.py"
267 local rewriter4="/usr/$(get_libdir)/javatoolkit/bin/build-xml-rewrite"
269 if [[ -x ${rewriter4} && ${JAVA_ANT_ENCODING} ]]; then
270 [[ ${JAVA_ANT_REWRITE_CLASSPATH} ]] && local gcp="-g"
271 [[ ${JAVA_ANT_ENCODING} ]] && local enc="-e ${JAVA_ANT_ENCODING}"
272 eval echo "cElementTree rewriter"
273 debug-print "${rewriter4} extra args: ${gcp} ${enc}"
274 ${rewriter4} ${gcp} ${enc} \
275 -c "${JAVA_PKG_BSFIX_SOURCE_TAGS}" source ${want_source} \
276 -c "${JAVA_PKG_BSFIX_TARGET_TAGS}" target ${want_target} \
277 "${@}" || die "build-xml-rewrite failed"
278 elif [[ ! -f ${rewriter3} ]]; then
279 debug-print "Using second generation rewriter"
280 eval echo "Rewriting source attributes"
281 eval xml-rewrite-2.py ${files} \
282 -c -e ${JAVA_PKG_BSFIX_SOURCE_TAGS// / -e } \
283 -a source -v ${want_source} || _bsfix_die "xml-rewrite2 failed: ${file}"
285 eval echo "Rewriting target attributes"
286 eval xml-rewrite-2.py ${files} \
287 -c -e ${JAVA_PKG_BSFIX_TARGET_TAGS// / -e } \
288 -a target -v ${want_target} || _bsfix_die "xml-rewrite2 failed: ${file}"
290 eval echo "Rewriting nowarn attributes"
291 eval xml-rewrite-2.py ${files} \
292 -c -e ${JAVA_PKG_BSFIX_TARGET_TAGS// / -e } \
293 -a nowarn -v yes || _bsfix_die "xml-rewrite2 failed: ${file}"
295 if [[ ${JAVA_ANT_REWRITE_CLASSPATH} ]]; then
296 eval echo "Adding gentoo.classpath to javac tasks"
297 eval xml-rewrite-2.py ${files} \
298 -c -e javac -e xjavac -a classpath -v \
299 '\${gentoo.classpath}' \
300 || _bsfix_die "xml-rewrite2 failed"
303 debug-print "Using third generation rewriter"
304 eval echo "Rewriting attributes"
305 local bsfix_extra_args=""
306 # WARNING KEEP THE ORDER, ESPECIALLY FOR CHANGED ATTRIBUTES!
307 if [[ -n ${JAVA_ANT_REWRITE_CLASSPATH} ]]; then
308 local cp_tags="${JAVA_ANT_CLASSPATH_TAGS// / -e }"
309 bsfix_extra_args="${bsfix_extra_args} -g -e ${cp_tags}"
310 bsfix_extra_args="${bsfix_extra_args} -a classpath -v '\${gentoo.classpath}'"
312 if [[ -n ${JAVA_ANT_JAVADOC_INPUT_DIRS} ]]; then
313 if [[ -n ${JAVA_ANT_JAVADOC_OUTPUT_DIR} ]]; then
314 die "Do not define JAVA_ANT_JAVADOC_OUTPUT_DIR!"
316 # Where will our generated javadoc go.
317 readonly JAVA_ANT_JAVADOC_OUTPUT_DIR="${WORKDIR}/gentoo_javadoc"
318 mkdir -p "${JAVA_ANT_JAVADOC_OUTPUT_DIR}" || die
320 if has doc ${IUSE}; then
322 if [[ -z ${EANT_DOC_TARGET} ]]; then
323 EANT_DOC_TARGET="gentoojavadoc"
325 die "You can't use javadoc adding and set EANT_DOC_TARGET too."
328 for dir in ${JAVA_ANT_JAVADOC_INPUT_DIRS};do
329 if [[ ! -d ${dir} ]]; then
330 eerror "This dir: ${dir} doesnt' exists"
331 die "You must specify directories for javadoc input/output dirs."
334 bsfix_extra_args="${bsfix_extra_args} --javadoc --source-directory "
335 # filter third/double spaces
336 JAVA_ANT_JAVADOC_INPUT_DIRS=${JAVA_ANT_JAVADOC_INPUT_DIRS// /}
337 JAVA_ANT_JAVADOC_INPUT_DIRS=${JAVA_ANT_JAVADOC_INPUT_DIRS// /}
338 bsfix_extra_args="${bsfix_extra_args} ${JAVA_ANT_JAVADOC_INPUT_DIRS// / --source-directory }"
339 bsfix_extra_args="${bsfix_extra_args} --output-directory ${JAVA_ANT_JAVADOC_OUTPUT_DIR}"
342 die "You need to have doc in IUSE when using JAVA_ANT_JAVADOC_INPUT_DIRS"
346 [[ -n ${JAVA_ANT_BSFIX_EXTRA_ARGS} ]] \
347 && bsfix_extra_args="${bsfix_extra_args} ${JAVA_ANT_BSFIX_EXTRA_ARGS}"
349 debug-print "bsfix_extra_args: ${bsfix_extra_args}"
351 eval ${rewriter3} ${files} \
352 -c --source-element ${JAVA_PKG_BSFIX_SOURCE_TAGS// / --source-element } \
353 --source-attribute source --source-value ${want_source} \
354 --target-element ${JAVA_PKG_BSFIX_TARGET_TAGS// / --target-element } \
355 --target-attribute target --target-value ${want_target} \
356 --target-attribute nowarn --target-value yes \
357 ${bsfix_extra_args} \
358 || _bsfix_die "xml-rewrite2 failed: ${file}"
361 if [[ -n "${JAVA_PKG_DEBUG}" ]]; then
362 for file in "${@}"; do
363 diff -NurbB "${file}.orig" "${file}"
367 return 0 # so that the 1 for diff doesn't get reported
371 # @FUNCTION: java-ant_bsfix_one
372 # @USAGE: <path/to/build.xml>
374 # Attempts to fix named build file.
377 # Affected by variables:
378 # JAVA_PKG_BSFIX_SOURCE_TAGS
379 # JAVA_PKG_BSFIX_TARGET_TAGS
381 java-ant_bsfix_one() {
382 debug-print-function ${FUNCNAME} $*
384 if [ -z "${1}" ]; then
385 eerror "${FUNCNAME} needs one argument"
386 die "${FUNCNAME} needs one argument"
389 java-ant_bsfix_files "${1}"
392 # @FUNCTION: java-ant_rewrite-classpath
393 # @USAGE: [path/to/build.xml]
395 # Adds 'classpath="${gentoo.classpath}"' to specified build file.
398 # JAVA_ANT_CLASSPATH_TAGS
400 # Parameter defaults to build.xml when not specified
401 java-ant_rewrite-classpath() {
402 debug-print-function ${FUNCNAME} $*
405 [[ -z "${1}" ]] && file=build.xml
406 [[ ${#} -gt 1 ]] && die "${FUNCNAME} currently can only rewrite one file."
408 echo "Adding gentoo.classpath to ${file}"
409 debug-print "java-ant_rewrite-classpath: ${file}"
411 cp "${file}" "${file}.orig" || die "failed to copy ${file}"
415 java-ant_xml-rewrite -f "${file}" --change \
416 -e ${JAVA_ANT_CLASSPATH_TAGS// / -e } -a classpath -v '${gentoo.classpath}'
418 if [[ -n "${JAVA_PKG_DEBUG}" ]]; then
419 diff -NurbB "${file}.orig" "${file}"
423 # @FUNCTION: java-ant_remove-taskdefs
424 # @USAGE: [--name NAME] [path/to/build.xml]
426 # Removes (named) taskdef elements from the build.xml file.
427 # When --name NAME is specified, only remove taskdef with name NAME. Otherwise,
428 # all taskdefs are removed.
429 # The file to rewrite defaults to build.xml when not specified.
430 java-ant_remove-taskdefs() {
431 debug-print-function ${FUNCNAME} $*
433 die "${FUNCNAME} has been banned, see bug #479838."
436 if [[ "${1}" == --name ]]; then
440 local file="${1:-build.xml}"
441 echo "Removing taskdefs from ${file}"
444 from xml.dom.minidom import parse
445 dom = parse("${file}")
446 for elem in dom.getElementsByTagName('taskdef'):
447 if (len("${task_name}") == 0 or elem.getAttribute("name") == "${task_name}"):
448 elem.parentNode.removeChild(elem)
450 f = open("${file}", "w")
454 [[ $? != 0 ]] && die "Removing taskdefs failed"
457 # @FUNCTION: java-ant_ignore-system-classes
458 # @USAGE: [path/to/build.xml]
460 # Makes the available task ignore classes in the system classpath
461 # Parameter defaults to build.xml when not specified
462 java-ant_ignore-system-classes() {
463 debug-print-function ${FUNCNAME} $*
464 local file=${1:-build.xml}
465 echo "Changing ignoresystemclasses to true for available tasks in ${file}"
466 java-ant_xml-rewrite -f "${file}" --change \
467 -e available -a ignoresystemclasses -v "true"
470 # @FUNCTION: java-ant_xml-rewrite
471 # @USAGE: <xml rewriter arguments>
473 # Run the right xml-rewrite binary with the given arguments
474 java-ant_xml-rewrite() {
475 local gen2="/usr/bin/xml-rewrite-2.py"
476 local gen2_1="/usr/$(get_libdir)/javatoolkit/bin/xml-rewrite-2.py"
478 if [[ -x "${gen2}" ]]; then
479 ${gen2} "${@}" || die "${gen2} failed"
480 elif [[ -x "${gen2_1}" ]]; then
481 ${gen2_1} "${@}" || die "${gen2_1} failed"
483 eerror "No binary for rewriting found."
484 eerror "Do you have dev-java/javatoolkit installed?"
485 die "xml-rewrite not found"
489 # @FUNCTION: java-ant_rewrite-bootclasspath
490 # @USAGE: <version> [path/to/build.xml] [prepend] [append]
492 # Adds bootclasspath to javac-like tasks in build.xml filled with jars of a
493 # bootclasspath package of given version.
497 # JAVA_PKG_BSFIX_TARGET_TAGS - the tags of javac tasks
500 # $1 - the version of bootclasspath (e.g. 1.5), 'auto' for bootclasspath
502 # $2 - path to desired build.xml file, defaults to 'build.xml'
503 # $3 - (optional) what to prepend the bootclasspath with (to override)
504 # $4 - (optional) what to append to the bootclasspath
506 java-ant_rewrite-bootclasspath() {
508 local file="${2-build.xml}"
509 local extra_before="${3}"
510 local extra_after="${4}"
512 local bcp="$(java-pkg_get-bootclasspath "${version}")"
514 if [[ -n "${extra_before}" ]]; then
515 bcp="${extra_before}:${bcp}"
517 if [[ -n "${extra_after}" ]]; then
518 bcp="${bcp}:${extra_after}"
521 java-ant_xml-rewrite -f "${file}" -c -e ${JAVA_PKG_BSFIX_TARGET_TAGS// / -e } \
522 -a bootclasspath -v "${bcp}"