summaryrefslogtreecommitdiff
blob: a8be8b4f57e90ae1edd924214a707d4c952ffd5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# eclass for ant based Java packages
#
# Copyright (c) 2004-2005, Thomas Matthijs <axxo@gentoo.org>
# Copyright (c) 2004-2005, Gentoo Foundation
# Changes:
#   May 2007:
#     Made bsfix make one pass for all things and add some glocal targets for
#     setting up the whole thing. Contributed by  kiorky
#     (kiorky@cryptelium.net).
#   December 2006:
#     I pretty much rewrote the logic of the bsfix functions
#     and xml-rewrite.py because they were so slow
#     Petteri Räty (betelgeuse@gentoo.org)
#
# Licensed under the GNU General Public License, v2
#
# $Header: /var/cvsroot/gentoo-x86/eclass/java-ant-2.eclass,v 1.48 2010/02/12 23:51:44 caster Exp $

inherit java-utils-2

# This eclass provides functionality for Java packages which use
# ant to build. In particular, it will attempt to fix build.xml files, so that
# they use the appropriate 'target' and 'source' attributes.

# -----------------------------------------------------------------------------
# @variable-preinherit WANT_ANT_TASKS
# @variable-default ""
#
# Please see the description in java-utils-2.eclass.
#WANT_ANT_TASKS

# -----------------------------------------------------------------------------
# @variable-preinherit JAVA_ANT_DISABLE_ANT_CORE_DEP
# @variable-default unset for java-pkg-2, true for java-pkg-opt-2
#
# Setting this variable non-empty before inheriting java-ant-2 disables adding
# dev-java/ant-core into DEPEND.

# construct ant-speficic DEPEND
JAVA_ANT_E_DEPEND=""
# add ant-core into DEPEND, unless disabled
if [[ -z "${JAVA_ANT_DISABLE_ANT_CORE_DEP}" ]]; then
		JAVA_ANT_E_DEPEND="${JAVA_ANT_E_DEPEND} >=dev-java/ant-core-1.7.0"
fi

# add ant tasks specified in WANT_ANT_TASKS to DEPEND
local ANT_TASKS_DEPEND;
ANT_TASKS_DEPEND="$(java-pkg_ant-tasks-depend)"
# check that java-pkg_ant-tasks-depend didn't fail
if [[ $? != 0 ]]; then
	eerror "${ANT_TASKS_DEPEND}"
	die "java-pkg_ant-tasks-depend() failed"
fi

# We need some tools from javatoolkit. We also need portage 2.1 for phase hooks
# and ant dependencies constructed above.
JAVA_ANT_E_DEPEND="${JAVA_ANT_E_DEPEND}
	${ANT_TASKS_DEPEND}
	${JAVA_PKG_PORTAGE_DEP}
	>=dev-java/javatoolkit-0.3.0-r2"

# this eclass must be inherited after java-pkg-2 or java-pkg-opt-2
# if it's java-pkg-opt-2, ant dependencies are pulled based on USE flag
if hasq java-pkg-opt-2 ${INHERITED}; then
	JAVA_ANT_E_DEPEND="${JAVA_PKG_OPT_USE}? ( ${JAVA_ANT_E_DEPEND} )"
elif ! hasq java-pkg-2 ${INHERITED}; then
	eerror "java-ant-2 eclass can only be inherited AFTER java-pkg-2 or java-pkg-opt-2"
fi

DEPEND="${JAVA_ANT_E_DEPEND}"

# ------------------------------------------------------------------------------
# @global JAVA_PKG_BSFIX
#
# Should we attempt to 'fix' ant build files to include the source/target
# attributes when calling javac?
#
# default: on
# ------------------------------------------------------------------------------
JAVA_PKG_BSFIX=${JAVA_PKG_BSFIX:-"on"}

# ------------------------------------------------------------------------------
# @global JAVA_PKG_BSFIX_ALL
#
# If we're fixing build files, should we try to fix all the ones we can find?
#
# default: yes
# ------------------------------------------------------------------------------
JAVA_PKG_BSFIX_ALL=${JAVA_PKG_BSFIX_ALL:-"yes"}

# ------------------------------------------------------------------------------
# @global JAVA_PKG_BSFIX_NAME
#
# Filename of build files to fix/search for
#
# default: build.xml
# ------------------------------------------------------------------------------
JAVA_PKG_BSFIX_NAME=${JAVA_PKG_BSFIX_NAME:-"build.xml"}

# ------------------------------------------------------------------------------
# @global JAVA_PKG_BSFIX_TARGETS_TAGS
#
# Targets to fix the 'source' attribute in
#
# default: javac xjavac javac.preset
# ------------------------------------------------------------------------------
JAVA_PKG_BSFIX_TARGET_TAGS=${JAVA_PKG_BSFIX_TARGET_TAGS:-"javac xjavac javac.preset"}

# ------------------------------------------------------------------------------
# @global JAVA_PKG_BSFIX_SOURCE_TAGS
#
# Targets to fix the 'target' attribute in
#
# default: javacdoc javac xjavac javac.preset
# ------------------------------------------------------------------------------
JAVA_PKG_BSFIX_SOURCE_TAGS=${JAVA_PKG_BSFIX_SOURCE_TAGS:-"javadoc javac xjavac javac.preset"}

# ------------------------------------------------------------------------------
# @global JAVA_ANT_CLASSPATH_TAGS
#
# Targets to add the classpath attribute to
#
# default: javac xjavac
# ------------------------------------------------------------------------------
JAVA_ANT_CLASSPATH_TAGS="javac xjavac"

# ------------------------------------------------------------------------------
# @global JAVA_ANT_IGNORE_SYSTEM_CLASSES
#
# Rewrites available tasks to ignore ant classpath.
#
# default: off
# ------------------------------------------------------------------------------

case "${EAPI:-0}" in
	0|1) : ;;
	*) EXPORT_FUNCTIONS src_configure ;;
esac

# ------------------------------------------------------------------------------
# @eclass-src_configure
#
# src_configure rewrites the build.xml files
# ------------------------------------------------------------------------------
java-ant-2_src_configure() {
	# eant will call us unless called by Portage
	[[ -e "${T}/java-ant-2_src_configure-run" ]] && return

	[[ "${JAVA_ANT_IGNORE_SYSTEM_CLASSES}" ]] \
		&& java-ant_ignore-system-classes "${S}/build.xml"

	java-ant_bsfix
	touch "${T}/java-ant-2_src_configure-run"
}

# ------------------------------------------------------------------------------
# @private java-ant_bsfix
#
# Attempts to fix build files. The following variables will affect its behavior
# as listed above:
# 	JAVA_PKG_BSFIX
#	JAVA_PKG_BSFIX_ALL
#	JAVA_PKG_BSFIX_NAME,
# ------------------------------------------------------------------------------
java-ant_bsfix() {
	debug-print-function ${FUNCNAME} $*

	[[ "${JAVA_PKG_BSFIX}" != "on" ]] && return
	if ! java-pkg_needs-vm; then
		echo "QA Notice: Package is using java-ant, but doesn't depend on a Java VM"
	fi

	pushd "${S}" >/dev/null

	local find_args=""
	[[ "${JAVA_PKG_BSFIX_ALL}" == "yes" ]] || find_args="-maxdepth 1"

	find_args="${find_args} -type f -name ${JAVA_PKG_BSFIX_NAME// / -o -name } "

	# This voodoo is done for paths with spaces
	local bsfix_these
	while read line; do
		[[ -z ${line} ]] && continue
		bsfix_these="${bsfix_these} '${line}'"
	done <<-EOF
			$(find . ${find_args})
		EOF

	[[ "${bsfix_these// /}" ]] && eval java-ant_bsfix_files ${bsfix_these}

	popd > /dev/null
}

_bsfix_die() {
	if has_version dev-python/pyxml; then
		eerror "If the output above contains:"
		eerror "ImportError:"
		eerror "/usr/lib/python2.4/site-packages/_xmlplus/parsers/pyexpat.so:"
		eerror "undefined symbol: PyUnicodeUCS2_DecodeUTF8"
		eerror "Try re-emerging dev-python/pyxml"
		die ${1} " Look at the eerror message above"
	else
		die ${1}
	fi
}

# ------------------------------------------------------------------------------
# @public java-ant_bsfix_files
#
# Attempts to fix named build files. The following variables will affect its behavior
# as listed above:
#	JAVA_PKG_BSFIX_SOURCE_TAGS
#	JAVA_PKG_BSFIX_TARGET_TAGS
#	JAVA_ANT_REWRITE_CLASSPATH
#	JAVA_ANT_JAVADOC_INPUT_DIRS: Where we can find java sources for javadoc
#                                input. Can be a space separated list of
#                                directories
#	JAVA_ANT_BSFIX_EXTRA_ARGS: You can use this to pass extra variables to the
#	                           rewriter if you know what you are doing.
#
# If JAVA_ANT_JAVADOC_INPUT_DIRS is set, we will turn on the adding of a basic
# javadoc target to the ant's build.xml with the javadoc xml-rewriter feature.
# Then we will set EANT DOC TARGET to the added javadoc target
# NOTE: the variable JAVA_ANT_JAVADOC_OUTPUT_DIR points where we will
#       generate the javadocs. This is a read-only variable, dont change it.

# When changing this function, make sure that it works with paths with spaces in
# them.
# ------------------------------------------------------------------------------
java-ant_bsfix_files() {
	debug-print-function ${FUNCNAME} $*

	[[ ${#} = 0 ]] && die "${FUNCNAME} called without arguments"

	local want_source="$(java-pkg_get-source)"
	local want_target="$(java-pkg_get-target)"

	debug-print "${FUNCNAME}: target: ${want_target} source: ${want_source}"

	if [ -z "${want_source}" -o -z "${want_target}" ]; then
		eerror "Could not find valid -source/-target values"
		eerror "Please file a bug about this on bugs.gentoo.org"
		die "Could not find valid -source/-target values"
	else
		local files

		for file in "${@}"; do
			debug-print "${FUNCNAME}: ${file}"

			if [[ -n "${JAVA_PKG_DEBUG}" ]]; then
				cp "${file}" "${file}.orig" || die "failed to copy ${file}"
			fi

			if [[ ! -w "${file}" ]]; then
				chmod u+w "${file}" || die "chmod u+w ${file} failed"
			fi

			files="${files} -f '${file}'"
		done

		# Play nice with paludis
		if [[ $(type -t quiet_mode) = function ]] && quiet_mode; then
			local output=">/dev/null"
		fi

		# for javadoc target and all in one pass, we need the new rewriter.
		local rewriter3="/usr/share/javatoolkit/xml-rewrite-3.py"
		if [[ ! -f ${rewriter3} ]]; then
			rewriter3="/usr/$(get_libdir)/javatoolkit/bin/xml-rewrite-3.py"
		fi

		local rewriter4="/usr/$(get_libdir)/javatoolkit/bin/build-xml-rewrite"

		if [[ -x ${rewriter4} && ${JAVA_ANT_ENCODING} ]]; then
			[[ ${JAVA_ANT_REWRITE_CLASSPATH} ]] && local gcp="-g"
			[[ ${JAVA_ANT_ENCODING} ]] && local enc="-e ${JAVA_ANT_ENCODING}"
			eval echo "cElementTree rewriter" ${output}
			debug-print "${rewriter4} extra args: ${gcp} ${enc}"
			${rewriter4} ${gcp} ${enc} \
				-c "${JAVA_PKG_BSFIX_SOURCE_TAGS}" source ${want_source} \
				-c "${JAVA_PKG_BSFIX_TARGET_TAGS}" target ${want_target} \
				"${@}" || die "build-xml-rewrite failed"
		elif [[ ! -f ${rewriter3} ]]; then
			debug-print "Using second generation rewriter"
			eval echo "Rewriting source attributes" ${output}
			eval xml-rewrite-2.py ${files} \
				-c -e ${JAVA_PKG_BSFIX_SOURCE_TAGS// / -e } \
				-a source -v ${want_source} ${output} || _bsfix_die "xml-rewrite2 failed: ${file}"

			eval echo "Rewriting target attributes" ${output}
			eval xml-rewrite-2.py ${files} \
				-c -e ${JAVA_PKG_BSFIX_TARGET_TAGS// / -e } \
				-a target -v ${want_target} ${output} || _bsfix_die "xml-rewrite2 failed: ${file}"

			eval echo "Rewriting nowarn attributes" ${output}
			eval xml-rewrite-2.py ${files} \
				-c -e ${JAVA_PKG_BSFIX_TARGET_TAGS// / -e } \
				-a nowarn -v yes ${output} || _bsfix_die "xml-rewrite2 failed: ${file}"

			if [[ ${JAVA_ANT_REWRITE_CLASSPATH} ]]; then
				eval echo "Adding gentoo.classpath to javac tasks" ${output}
				eval xml-rewrite-2.py ${files} \
					 -c -e javac -e xjavac -a classpath -v \
					 '\${gentoo.classpath}' \
					 || _bsfix_die "xml-rewrite2 failed"
			fi
		else
			debug-print "Using third generation rewriter"
			eval echo "Rewriting attributes" ${output}
			local bsfix_extra_args=""
			# WARNING KEEP THE ORDER, ESPECIALLY FOR CHANGED ATTRIBUTES!
			if [[ -n ${JAVA_ANT_REWRITE_CLASSPATH} ]]; then
				local cp_tags="${JAVA_ANT_CLASSPATH_TAGS// / -e }"
				bsfix_extra_args="${bsfix_extra_args} -g -e ${cp_tags}"
				bsfix_extra_args="${bsfix_extra_args} -a classpath -v '\${gentoo.classpath}'"
			fi
			if [[ -n ${JAVA_ANT_JAVADOC_INPUT_DIRS} ]]; then
				if [[ -n ${JAVA_ANT_JAVADOC_OUTPUT_DIR} ]]; then
					die "Do not define JAVA_ANT_JAVADOC_OUTPUT_DIR!"
				fi
				# Where will our generated javadoc go.
				readonly JAVA_ANT_JAVADOC_OUTPUT_DIR="${WORKDIR}/gentoo_javadoc"
				mkdir -p "${JAVA_ANT_JAVADOC_OUTPUT_DIR}" || die

				if hasq doc ${IUSE}; then
					if use doc; then
						if [[ -z ${EANT_DOC_TARGET} ]]; then
							EANT_DOC_TARGET="gentoojavadoc"
						else
							die "You can't use javadoc adding and set EANT_DOC_TARGET too."
						fi

						for dir in ${JAVA_ANT_JAVADOC_INPUT_DIRS};do
							if [[ ! -d ${dir} ]]; then
								eerror "This dir: ${dir} doesnt' exists"
								die "You must specify directories for javadoc input/output dirs."
							fi
						done
						bsfix_extra_args="${bsfix_extra_args} --javadoc --source-directory "
						# filter third/double spaces
						JAVA_ANT_JAVADOC_INPUT_DIRS=${JAVA_ANT_JAVADOC_INPUT_DIRS//   /}
						JAVA_ANT_JAVADOC_INPUT_DIRS=${JAVA_ANT_JAVADOC_INPUT_DIRS//  /}
						bsfix_extra_args="${bsfix_extra_args} ${JAVA_ANT_JAVADOC_INPUT_DIRS// / --source-directory }"
						bsfix_extra_args="${bsfix_extra_args} --output-directory ${JAVA_ANT_JAVADOC_OUTPUT_DIR}"
					fi
				else
					die "You need to have doc in IUSE when using JAVA_ANT_JAVADOC_INPUT_DIRS"
				fi
			fi

			[[ -n ${JAVA_ANT_BSFIX_EXTRA_ARGS} ]] \
				&& bsfix_extra_args="${bsfix_extra_args} ${JAVA_ANT_BSFIX_EXTRA_ARGS}"

			debug-print "bsfix_extra_args: ${bsfix_extra_args}"

			eval ${rewriter3}  ${files} \
				-c --source-element ${JAVA_PKG_BSFIX_SOURCE_TAGS// / --source-element } \
				--source-attribute source --source-value ${want_source} \
				--target-element   ${JAVA_PKG_BSFIX_TARGET_TAGS// / --target-element }  \
				--target-attribute target --target-value ${want_target} \
				--target-attribute nowarn --target-value yes \
				${bsfix_extra_args} \
				${output} || _bsfix_die "xml-rewrite2 failed: ${file}"
		fi

		if [[ -n "${JAVA_PKG_DEBUG}" ]]; then
			for file in "${@}"; do
				diff -NurbB "${file}.orig" "${file}"
			done
		fi
	fi
	return 0 # so that the 1 for diff doesn't get reported
}


# ------------------------------------------------------------------------------
# @public java-ant_bsfix_one
#
# Attempts to fix named build file. The following variables will affect its behavior
# as listed above:
#	JAVA_PKG_BSFIX_SOURCE_TAGS
#	JAVA_PKG_BSFIX_TARGET_TAGS
# ------------------------------------------------------------------------------
java-ant_bsfix_one() {
	debug-print-function ${FUNCNAME} $*

	if [ -z "${1}" ]; then
		eerror "${FUNCNAME} needs one argument"
		die "${FUNCNAME} needs one argument"
	fi

	java-ant_bsfix_files "${1}"
}

# ------------------------------------------------------------------------------
# @public java-ant_rewrite-classpath
#
# Adds 'classpath="${gentoo.classpath}"' to specified build file.
# Affected by:
#	JAVA_ANT_CLASSPATH_TAGS
# @param $1 - the file to rewrite (defaults to build.xml)
# ------------------------------------------------------------------------------
java-ant_rewrite-classpath() {
	debug-print-function ${FUNCNAME} $*

	local file="${1}"
	[[ -z "${1}" ]] && file=build.xml
	[[ ${#} -gt 1 ]] && die "${FUNCNAME} currently can only rewrite one file."

	echo "Adding gentoo.classpath to ${file}"
	debug-print "java-ant_rewrite-classpath: ${file}"

	cp "${file}" "${file}.orig" || die "failed to copy ${file}"

	chmod u+w "${file}"

	java-ant_xml-rewrite -f "${file}" --change \
		-e ${JAVA_ANT_CLASSPATH_TAGS// / -e } -a classpath -v '${gentoo.classpath}'

	if [[ -n "${JAVA_PKG_DEBUG}" ]]; then
		diff -NurbB "${file}.orig" "${file}"
	fi
}

# ------------------------------------------------------------------------------
# @public java-ant_ignore-system-classes
#
# Makes the available task ignore classes in the system classpath
# @param $1 - the file to rewrite (defaults to build.xml)
# ------------------------------------------------------------------------------
java-ant_ignore-system-classes() {
	debug-print-function ${FUNCNAME} $*
	local file=${1:-build.xml}
	echo "Changing ignoresystemclasses to true for available tasks in ${file}"
	java-ant_xml-rewrite -f "${file}" --change \
		-e available -a ignoresystemclasses -v "true"
}

# ------------------------------------------------------------------------------
# @public java-ant_xml-rewrite
# Run the right xml-rewrite binary with the given arguments
# ------------------------------------------------------------------------------
java-ant_xml-rewrite() {
	local gen2="/usr/bin/xml-rewrite-2.py"
	local gen2_1="/usr/$(get_libdir)/javatoolkit/bin/xml-rewrite-2.py"
	# gen1 is deprecated
	if [[ -x "${gen2}" ]]; then
		${gen2} "${@}" || die "${gen2} failed"
	elif [[ -x "${gen2_1}" ]]; then
		${gen2_1} "${@}" || die "${gen2_1} failed"
	else
		eerror "No binary for rewriting found."
		eerror "Do you have dev-java/javatoolkit installed?"
		die "xml-rewrite not found"
	fi
}

# ------------------------------------------------------------------------------
# @public java-ant_rewrite-bootclasspath
#
# Adds bootclasspath to javac-like tasks in build.xml filled with jars of a
# bootclasspath package of given version.
#
# Affected by:
#	JAVA_PKG_BSFIX_TARGET_TAGS - the tags of javac tasks
#
# @param $1 - the version of bootclasspath (e.g. 1.5), 'auto' for bootclasspath
#             of the current JDK
# @param $2 - path to desired build.xml file, defaults to 'build.xml'
# @param $3 - (optional) what to prepend the bootclasspath with (to override)
# @param $4 - (optional) what to append to the bootclasspath
# ------------------------------------------------------------------------------

java-ant_rewrite-bootclasspath() {
	local version="${1}"
	local file="${2-build.xml}"
	local extra_before="${3}"
	local extra_after="${4}"

	local bcp="$(java-pkg_get-bootclasspath "${version}")"

	if [[ -n "${extra_before}" ]]; then
		bcp="${extra_before}:${bcp}"
	fi
	if [[ -n "${extra_after}" ]]; then
		bcp="${bcp}:${extra_after}"
	fi

	java-ant_xml-rewrite -f "${file}" -c -e ${JAVA_PKG_BSFIX_TARGET_TAGS// / -e } \
		-a bootclasspath -v "${bcp}"
}