kde5-functions.eclass: Fix KDE Plasma 5.13.5 QT_MINIMAL
[gentoo.git] / eclass / java-utils-2.eclass
1 # Copyright 2004-2017 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: java-utils-2.eclass
5 # @MAINTAINER:
6 # java@gentoo.org
7 # @AUTHOR:
8 # Thomas Matthijs <axxo@gentoo.org>, Karl Trygve Kalleberg <karltk@gentoo.org>
9 # @BLURB: Base eclass for Java packages
10 # @DESCRIPTION:
11 # This eclass provides functionality which is used by java-pkg-2.eclass,
12 # java-pkg-opt-2.eclass and java-ant-2 eclass, as well as from ebuilds.
13 #
14 # This eclass should not be inherited this directly from an ebuild. Instead,
15 # you should inherit java-pkg-2 for Java packages or java-pkg-opt-2 for packages
16 # that have optional Java support. In addition you can inherit java-ant-2 for
17 # Ant-based packages.
18 inherit eutils versionator multilib
19
20 IUSE="elibc_FreeBSD"
21
22 # Make sure we use java-config-2
23 export WANT_JAVA_CONFIG="2"
24
25 # Prefix variables are only available for EAPI>=3
26 has "${EAPI:-0}" 0 1 2 && ED="${D}" EPREFIX= EROOT="${ROOT}"
27
28 # @VARIABLE: JAVA_PKG_PORTAGE_DEP
29 # @INTERNAL
30 # @DESCRIPTION:
31 # The version of portage we need to function properly. Previously it was
32 # portage with phase hooks support but now we use a version with proper env
33 # saving. For EAPI 2 we have new enough stuff so let's have cleaner deps.
34 has "${EAPI}" 0 1 && JAVA_PKG_PORTAGE_DEP=">=sys-apps/portage-2.1.2.7"
35
36 # @VARIABLE: JAVA_PKG_E_DEPEND
37 # @INTERNAL
38 # @DESCRIPTION:
39 # This is a convience variable to be used from the other java eclasses. This is
40 # the version of java-config we want to use. Usually the latest stable version
41 # so that ebuilds can use new features without depending on specific versions.
42 JAVA_PKG_E_DEPEND=">=dev-java/java-config-2.2.0-r3 ${JAVA_PKG_PORTAGE_DEP}"
43 has source ${JAVA_PKG_IUSE} && JAVA_PKG_E_DEPEND="${JAVA_PKG_E_DEPEND} source? ( app-arch/zip )"
44
45 # @ECLASS-VARIABLE: JAVA_PKG_WANT_BOOTCLASSPATH
46 # @DEFAULT_UNSET
47 # @DESCRIPTION:
48 # The version of bootclasspath the package needs to work. Translates to a proper
49 # dependency. The bootclasspath can then be obtained by java-ant_rewrite-bootclasspath
50 if [[ -n "${JAVA_PKG_WANT_BOOTCLASSPATH}" ]]; then
51         if [[ "${JAVA_PKG_WANT_BOOTCLASSPATH}" == "1.5" ]]; then
52                 JAVA_PKG_E_DEPEND="${JAVA_PKG_E_DEPEND} >=dev-java/gnu-classpath-0.98-r1:0.98"
53         else
54                 eerror "Unknown value of JAVA_PKG_WANT_BOOTCLASSPATH"
55                 # since die in global scope doesn't work, this will make repoman fail
56                 JAVA_PKG_E_DEPEND="${JAVA_PKG_E_DEPEND} BAD_JAVA_PKG_WANT_BOOTCLASSPATH"
57         fi
58 fi
59
60 # @ECLASS-VARIABLE: JAVA_PKG_ALLOW_VM_CHANGE
61 # @DESCRIPTION:
62 # Allow this eclass to change the active VM?
63 # If your system VM isn't sufficient for the package, the build will fail
64 # instead of trying to switch to another VM.
65 #
66 # Overriding the default can be useful for testing specific VMs locally, but
67 # should not be used in the final ebuild.
68 JAVA_PKG_ALLOW_VM_CHANGE=${JAVA_PKG_ALLOW_VM_CHANGE:="yes"}
69
70 # @ECLASS-VARIABLE: JAVA_PKG_FORCE_VM
71 # @DEFAULT_UNSET
72 # @DESCRIPTION:
73 # Explicitly set a particular VM to use. If its not valid, it'll fall back to
74 # whatever /etc/java-config-2/build/jdk.conf would elect to use.
75 #
76 # Should only be used for testing and debugging.
77 #
78 # Example: use sun-jdk-1.5 to emerge foo:
79 # @CODE
80 #       JAVA_PKG_FORCE_VM=sun-jdk-1.5 emerge foo
81 # @CODE
82
83 # @ECLASS-VARIABLE: JAVA_PKG_WANT_BUILD_VM
84 # @DEFAULT_UNSET
85 # @DESCRIPTION:
86 # A list of VM handles to choose a build VM from. If the list contains the
87 # currently active VM use that one, otherwise step through the list till a
88 # usable/installed VM is found.
89 #
90 # This allows to use an explicit list of JDKs in DEPEND instead of a virtual.
91 # Users of this variable must make sure at least one of the listed handles is
92 # covered by DEPEND.
93 # Requires JAVA_PKG_WANT_SOURCE and JAVA_PKG_WANT_TARGET to be set as well.
94
95 # @ECLASS-VARIABLE: JAVA_PKG_WANT_SOURCE
96 # @DEFAULT_UNSET
97 # @DESCRIPTION:
98 # Specify a non-standard Java source version for compilation (via javac -source
99 # parameter or Ant equivalent via build.xml rewriting done by java-ant-2 eclass).
100 # Normally this is determined from the jdk version specified in DEPEND.
101 # See java-pkg_get-source function below.
102 #
103 # Should generally only be used for testing and debugging.
104 #
105 # Use 1.4 source to emerge baz
106 # @CODE
107 #       JAVA_PKG_WANT_SOURCE=1.4 emerge baz
108 # @CODE
109
110 # @ECLASS-VARIABLE: JAVA_PKG_WANT_TARGET
111 # @DEFAULT_UNSET
112 # @DESCRIPTION:
113 # Same as JAVA_PKG_WANT_SOURCE (see above) but for javac -target parameter,
114 # which affects the version of generated bytecode.
115 # Normally this is determined from the jre/jdk version specified in RDEPEND.
116 # See java-pkg_get-target function below.
117 #
118 # Should generallyonly be used for testing and debugging.
119 #
120 # emerge bar to be compatible with 1.3
121 # @CODE
122 #       JAVA_PKG_WANT_TARGET=1.3 emerge bar
123 # @CODE
124
125 # @ECLASS-VARIABLE: JAVA_PKG_DEBUG
126 # @DEFAULT_UNSET
127 # @DESCRIPTION:
128 # A variable to be set with "yes" or "y", or ANY string of length non equal to
129 # zero. When set, verbosity across java eclasses is increased and extra
130 # logging is displayed.
131 # @CODE
132 #       JAVA_PKG_DEBUG="yes"
133 # @CODE
134
135 # @ECLASS-VARIABLE: JAVA_RM_FILES
136 # @DEFAULT_UNSET
137 # @DESCRIPTION:
138 # An array containing a list of files to remove. If defined, this array will be
139 # automatically handed over to java-pkg_rm_files for processing during the
140 # src_prepare phase.
141 #
142 # @CODE
143 #       JAVA_RM_FILES=(
144 #               path/to/File1.java
145 #               DELETEME.txt
146 #       )
147 # @CODE
148
149 # @VARIABLE: JAVA_PKG_COMPILER_DIR
150 # @INTERNAL
151 # @DESCRIPTION:
152 # Directory where compiler settings are saved, without trailing slash.
153 # You probably shouldn't touch this variable except local testing.
154 JAVA_PKG_COMPILER_DIR=${JAVA_PKG_COMPILER_DIR:="/usr/share/java-config-2/compiler"}
155
156 # @VARIABLE: JAVA_PKG_COMPILERS_CONF
157 # @INTERNAL
158 # @DESCRIPTION:
159 # Path to file containing information about which compiler to use.
160 # Can be overloaded, but it should be overloaded only for local testing.
161 JAVA_PKG_COMPILERS_CONF=${JAVA_PKG_COMPILERS_CONF:="/etc/java-config-2/build/compilers.conf"}
162
163 # @ECLASS-VARIABLE: JAVA_PKG_FORCE_COMPILER
164 # @INTERNAL
165 # @DEFAULT_UNSET
166 # @DESCRIPTION:
167 # Explicitly set a list of compilers to choose from. This is normally read from
168 # JAVA_PKG_COMPILERS_CONF.
169 #
170 # Useful for local testing.
171 #
172 # Use jikes and javac, in that order
173 # @CODE
174 #       JAVA_PKG_FORCE_COMPILER="jikes javac"
175 # @CODE
176
177 # @ECLASS-VARIABLE: JAVA_PKG_FORCE_ANT_TASKS
178 # @DEFAULT_UNSET
179 # @DESCRIPTION:
180 # An $IFS separated list of ant tasks. Can be set in environment before calling
181 # emerge/ebuild to override variables set in ebuild, mainly for testing before
182 # putting the resulting (WANT_)ANT_TASKS into ebuild. Affects only ANT_TASKS in
183 # eant() call, not the dependencies specified in WANT_ANT_TASKS.
184 #
185 # @CODE
186 # JAVA_PKG_FORCE_ANT_TASKS="ant-junit ant-trax" \
187 #       ebuild foo.ebuild compile
188 # @CODE
189
190 # TODO document me
191 JAVA_PKG_QA_VIOLATIONS=0
192
193 # @FUNCTION: java-pkg_doexamples
194 # @USAGE: [--subdir <subdir>] <file1/dir1> [<file2> ...]
195 # @DESCRIPTION:
196 # Installs given arguments to /usr/share/doc/${PF}/examples
197 # If you give it only one parameter and it is a directory it will install
198 # everything in that directory to the examples directory.
199 #
200 # @CODE
201 # Parameters:
202 # --subdir - If the examples need a certain directory structure
203 # $* - list of files to install
204 #
205 # Examples:
206 #       java-pkg_doexamples demo
207 #       java-pkg_doexamples demo/* examples/*
208 # @CODE
209 java-pkg_doexamples() {
210         debug-print-function ${FUNCNAME} $*
211
212         [[ ${#} -lt 1 ]] && die "At least one argument needed"
213
214         java-pkg_check-phase install
215         java-pkg_init_paths_
216
217         local dest=/usr/share/doc/${PF}/examples
218         if [[ ${1} == --subdir ]]; then
219                 local dest=${dest}/${2}
220                 dodir ${dest}
221                 shift 2
222         fi
223
224         if [[ ${#} = 1 && -d ${1} ]]; then
225                 ( # dont want to pollute calling env
226                         insinto "${dest}"
227                         doins -r ${1}/*
228                 ) || die "Installing examples failed"
229         else
230                 ( # dont want to pollute calling env
231                         insinto "${dest}"
232                         doins -r "$@"
233                 ) || die "Installing examples failed"
234         fi
235
236         # Let's make a symlink to the directory we have everything else under
237         dosym "${dest}" "${JAVA_PKG_SHAREPATH}/examples" || die
238 }
239
240 # @FUNCTION: java-pkg_addres
241 # @USAGE: <jar> <dir> [<find arguments> ...]
242 # @DESCRIPTION:
243 # Adds resource files to an existing jar.
244 # It is important that the directory given is actually the root of the
245 # corresponding resource tree. The target directory as well as
246 # sources.lst, MANIFEST.MF, *.class, *.jar, and *.java files are
247 # automatically excluded. Symlinks are always followed. Additional
248 # arguments are passed through to find.
249 #
250 # @CODE
251 #       java-pkg_addres ${PN}.jar resources ! -name "*.html"
252 # @CODE
253 #
254 # @param $1 - jar file
255 # @param $2 - resource tree directory
256 # @param $* - arguments to pass to find
257 java-pkg_addres() {
258         debug-print-function ${FUNCNAME} $*
259
260         [[ ${#} -lt 2 ]] && die "at least two arguments needed"
261
262         local jar=$(realpath "$1" || die "realpath $1 failed")
263         local dir="$2"
264         shift 2
265
266         pushd "${dir}" > /dev/null || die "pushd ${dir} failed"
267         find -L -type f ! -path "./target/*" ! -path "./sources.lst" ! -name "MANIFEST.MF" ! -regex ".*\.\(class\|jar\|java\)" "${@}" -print0 | xargs -r0 jar uf "${jar}" || die "jar failed"
268         popd > /dev/null || die "popd failed"
269 }
270
271 # @FUNCTION: java-pkg_rm_files
272 # @USAGE: java-pkg_rm_files File1.java File2.java ...
273 # @DESCRIPTION:
274 # Remove unneeded files in ${S}.
275 #
276 # Every now and then, you'll run into situations whereby a file needs removing,
277 # be it a unit test or a regular java class.
278 #
279 # You can use this function by either:
280 # - calling it yourself in src_prepare() and feeding java-pkg_rm_files with
281 # the list of files you wish to remove.
282 # - defining an array in the ebuild named JAVA_RM_FILES with the list of files
283 # you wish to remove.
284 #
285 # Both way work and it is left to the developer's preferences. If the
286 # JAVA_RM_FILES array is defined, it will be automatically handed over to
287 # java-pkg_rm_files during the src_prepare phase.
288 #
289 # See java-utils-2_src_prepare.
290 #
291 # @CODE
292 #       java-pkg_rm_files File1.java File2.java
293 # @CODE
294 #
295 # @param $* - list of files to remove.
296 java-pkg_rm_files() {
297         debug-print-function ${FUNCNAME} $*
298         local IFS="\n"
299         for filename in "$@"; do
300                 [[ ! -f "${filename}" ]] && die "${filename} is not a regular file. Aborting."
301                 einfo "Removing unneeded file ${filename}"
302                 rm -f "${S}/${filename}" || die "cannot remove ${filename}"
303                 eend $?
304         done
305 }
306
307 # @FUNCTION: java-pkg_dojar
308 # @USAGE: <jar1> [<jar2> ...]
309 # @DESCRIPTION:
310 # Installs any number of jars.
311 # Jar's will be installed into /usr/share/${PN}(-${SLOT})/lib/ by default.
312 # You can use java-pkg_jarinto to change this path.
313 # You should never install a jar with a package version in the filename.
314 # Instead, use java-pkg_newjar defined below.
315 #
316 # @CODE
317 #       java-pkg_dojar dist/${PN}.jar dist/${PN}-core.jar
318 # @CODE
319 #
320 # @param $* - list of jars to install
321 java-pkg_dojar() {
322         debug-print-function ${FUNCNAME} $*
323
324         [[ ${#} -lt 1 ]] && die "At least one argument needed"
325
326         java-pkg_check-phase install
327         java-pkg_init_paths_
328
329         # Create JARDEST if it doesn't exist
330         dodir ${JAVA_PKG_JARDEST}
331
332         local jar
333         # for each jar
334         for jar in "${@}"; do
335                 local jar_basename=$(basename "${jar}")
336
337                 java-pkg_check-versioned-jar ${jar_basename}
338
339                 # check if it exists
340                 if [[ -e "${jar}" ]] ; then
341                         # Don't overwrite if jar has already been installed with the same
342                         # name
343                         local dest="${ED}${JAVA_PKG_JARDEST}/${jar_basename}"
344                         if [[ -e "${dest}" ]]; then
345                                 ewarn "Overwriting ${dest}"
346                         fi
347
348                         # install it into JARDEST if it's a non-symlink
349                         if [[ ! -L "${jar}" ]] ; then
350                                 #but first check class version when in strict mode.
351                                 is-java-strict && java-pkg_verify-classes "${jar}"
352
353                                 (
354                                         insinto "${JAVA_PKG_JARDEST}"
355                                         doins "${jar}"
356                                 ) || die "failed to install ${jar}"
357                                 java-pkg_append_ JAVA_PKG_CLASSPATH "${EPREFIX}/${JAVA_PKG_JARDEST}/${jar_basename}"
358                                 debug-print "installed ${jar} to ${ED}${JAVA_PKG_JARDEST}"
359                         # make a symlink to the original jar if it's symlink
360                         else
361                                 # TODO use dosym, once we find something that could use it
362                                 # -nichoj
363                                 ln -s "$(readlink "${jar}")" "${ED}${JAVA_PKG_JARDEST}/${jar_basename}"
364                                 debug-print "${jar} is a symlink, linking accordingly"
365                         fi
366                 else
367                         die "${jar} does not exist"
368                 fi
369         done
370
371         # Extra logging if enabled.
372         if [[ -n ${JAVA_PKG_DEBUG} ]]; then
373                 einfo "Verbose logging for \"${FUNCNAME}\" function"
374                 einfo "Jar file(s) destination: ${JAVA_PKG_JARDEST}"
375                 einfo "Jar file(s) created: ${@}"
376                 einfo "Complete command:"
377                 einfo "${FUNCNAME} ${@}"
378         fi
379
380         java-pkg_do_write_
381 }
382
383 # @FUNCTION: java-pkg_regjar
384 # @USAGE: </path/to/installed/jar>
385 # @DESCRIPTION:
386 # Records an already installed (in ${D}) jar in the package.env
387 # This would mostly be used if the package has make or a custom script to
388 # install things.
389 #
390 # WARNING:
391 # if you want to use shell expansion, you have to use ${D}/... as the for in
392 # this function will not be able to expand the path, here's an example:
393 #
394 # @CODE
395 #   java-pkg_regjar ${D}/opt/my-java/lib/*.jar
396 # @CODE
397 #
398
399 # TODO should we be making sure the jar is present on ${D} or wherever?
400 java-pkg_regjar() {
401         debug-print-function ${FUNCNAME} $*
402
403         java-pkg_check-phase install
404
405         [[ ${#} -lt 1 ]] && die "at least one argument needed"
406
407         java-pkg_init_paths_
408
409         local jar jar_dir jar_file
410         for jar in "${@}"; do
411                 # TODO use java-pkg_check-versioned-jar
412                 if [[ -e "${jar}" || -e "${D}${jar}" ]]; then
413                         [[ -d "${jar}" || -d "${D}${jar}" ]] \
414                                 && die "Called ${FUNCNAME} on a directory $*"
415
416                         #check that class version correct when in strict mode
417                         is-java-strict && java-pkg_verify-classes "${jar}"
418
419                         # nelchael: we should strip ${D} in this case too, here's why:
420                         # imagine such call:
421                         #    java-pkg_regjar ${D}/opt/java/*.jar
422                         # such call will fall into this case (-e ${jar}) and will
423                         # record paths with ${D} in package.env
424                         java-pkg_append_ JAVA_PKG_CLASSPATH     "${jar#${D}}"
425                 else
426                         if [[ ${jar} = *\** ]]; then
427                                 eerror "The argument ${jar} to ${FUNCNAME}"
428                                 eerror "has * in it. If you want it to glob in"
429                                 eerror '${D} add ${D} to the argument.'
430                         fi
431                         debug-print "${jar} or ${D}${jar} not found"
432                         die "${jar} does not exist"
433                 fi
434         done
435
436         java-pkg_do_write_
437 }
438
439 # @FUNCTION: java-pkg_newjar
440 # @USAGE: <path/to/oldname.jar> [<newname.jar>]
441 # @DESCRIPTION:
442 # Installs a jar with a new name (defaults to $PN.jar)
443 #
444 # For example, installs a versioned jar without the version
445 java-pkg_newjar() {
446         debug-print-function ${FUNCNAME} $*
447
448         local original_jar="${1}"
449         local new_jar="${2:-${PN}.jar}"
450         local new_jar_dest="${T}/${new_jar}"
451
452         [[ -z ${original_jar} ]] && die "Must specify a jar to install"
453         [[ ! -f ${original_jar} ]] \
454                 && die "${original_jar} does not exist or is not a file!"
455
456         rm -f "${new_jar_dest}" || die "Failed to remove ${new_jar_dest}"
457         cp "${original_jar}" "${new_jar_dest}" \
458                 || die "Failed to copy ${original_jar} to ${new_jar_dest}"
459         java-pkg_dojar "${new_jar_dest}"
460 }
461
462 # @FUNCTION: java-pkg_addcp
463 # @USAGE: <classpath>
464 # @DESCRIPTION:
465 # Add something to the package's classpath. For jars, you should use dojar,
466 # newjar, or regjar. This is typically used to add directories to the classpath.
467 # The parameters of this function are appended to JAVA_PKG_CLASSPATH
468 java-pkg_addcp() {
469         java-pkg_append_ JAVA_PKG_CLASSPATH "${@}"
470         java-pkg_do_write_
471 }
472
473 # @FUNCTION: java-pkg_doso
474 # @USAGE: <path/to/file1.so> [...]
475 # @DESCRIPTION:
476 # Installs any number of JNI libraries
477 # They will be installed into /usr/lib by default, but java-pkg_sointo
478 # can be used change this path
479 #
480 # @CODE
481 # Example:
482 #       java-pkg_doso *.so
483 # @CODE
484 java-pkg_doso() {
485         debug-print-function ${FUNCNAME} $*
486
487         java-pkg_check-phase install
488
489         [[ ${#} -lt 1 ]] && die "${FUNCNAME} requires at least one argument"
490
491         java-pkg_init_paths_
492
493         local lib
494         # for each lib
495         for lib in "$@" ; do
496                 # if the lib exists...
497                 if [[ -e "${lib}" ]] ; then
498                         # install if it isn't a symlink
499                         if [[ ! -L "${lib}" ]] ; then
500                                 (
501                                         insinto "${JAVA_PKG_LIBDEST}"
502                                         insopts -m0755
503                                         doins "${lib}"
504                                 ) || die "failed to install ${lib}"
505                                 java-pkg_append_ JAVA_PKG_LIBRARY "${JAVA_PKG_LIBDEST}"
506                                 debug-print "Installing ${lib} to ${JAVA_PKG_LIBDEST}"
507                         # otherwise make a symlink to the symlink's origin
508                         else
509                                 dosym "$(readlink "${lib}")" "${JAVA_PKG_LIBDEST}/${lib##*/}"
510                                 debug-print "${lib} is a symlink, linking accordantly"
511                         fi
512                 # otherwise die
513                 else
514                         die "${lib} does not exist"
515                 fi
516         done
517
518         java-pkg_do_write_
519 }
520
521 # @FUNCTION: java-pkg_regso
522 # @USAGE: <file1.so> [...]
523 # @DESCRIPTION:
524 # Registers an already installed JNI library in package.env.
525 #
526 # @CODE
527 # Parameters:
528 # $@ - JNI libraries to register
529 #
530 # Example:
531 #       java-pkg_regso *.so /path/*.so
532 # @CODE
533 java-pkg_regso() {
534         debug-print-function ${FUNCNAME} $*
535
536         java-pkg_check-phase install
537
538         [[ ${#} -lt 1 ]] && die "${FUNCNAME} requires at least one argument"
539
540         java-pkg_init_paths_
541
542         local lib target_dir
543         for lib in "$@" ; do
544                 # Check the absolute path of the lib
545                 if [[ -e "${lib}" ]] ; then
546                         target_dir="$(java-pkg_expand_dir_ ${lib})"
547                         java-pkg_append_ JAVA_PKG_LIBRARY "/${target_dir#${D}}"
548                 # Check the path of the lib relative to ${D}
549                 elif [[ -e "${D}${lib}" ]]; then
550                         target_dir="$(java-pkg_expand_dir_ ${D}${lib})"
551                         java-pkg_append_ JAVA_PKG_LIBRARY "${target_dir}"
552                 else
553                         die "${lib} does not exist"
554                 fi
555         done
556
557         java-pkg_do_write_
558 }
559
560 # @FUNCTION: java-pkg_jarinto
561 # @USAGE: </path/to/install/jars/into>
562 # @DESCRIPTION:
563 # Changes the path jars are installed into via subsequent java-pkg_dojar calls.
564 java-pkg_jarinto() {
565         debug-print-function ${FUNCNAME} $*
566
567         JAVA_PKG_JARDEST="${1}"
568 }
569
570 # @FUNCTION: java-pkg_sointo
571 # @USAGE: </path/to/install/sofiles/into>
572 # @DESCRIPTION:
573 # Changes the path that JNI libraries are installed into via subsequent
574 # java-pkg_doso calls.
575 java-pkg_sointo() {
576         debug-print-function ${FUNCNAME} $*
577
578         JAVA_PKG_LIBDEST="${1}"
579 }
580
581 # @FUNCTION: java-pkg_dohtml
582 # @USAGE: <path/to/javadoc/documentation> [...]
583 # @DESCRIPTION:
584 # Install Javadoc HTML documentation. Usage of java-pkg_dojavadoc is preferred.
585 #
586 # @CODE
587 #       java-pkg_dohtml dist/docs/
588 # @CODE
589 java-pkg_dohtml() {
590         debug-print-function ${FUNCNAME} $*
591
592         [[ ${#} -lt 1 ]] && die "At least one argument required for ${FUNCNAME}"
593
594         # Do away with dohtml and rely on dodoc instead to do the deed.
595         docinto html
596         dodoc "$@"
597
598         # This probably shouldn't be here but it provides
599         # a reasonable way to catch # docs for all of the
600         # old ebuilds.
601         java-pkg_recordjavadoc
602 }
603
604 # @FUNCTION: java-pkg_dojavadoc
605 # @USAGE: [--symlink destination] <path/to/javadocs/root>
606 # @DESCRIPTION:
607 # Installs javadoc documentation. This should be controlled by the doc use flag.
608 #
609 # @CODE
610 # Parameters:
611 # $1: optional --symlink creates to symlink like this for html
612 #            documentation bundles.
613 # $2: - The javadoc root directory.
614 #
615 # Examples:
616 #       java-pkg_dojavadoc docs/api
617 #       java-pkg_dojavadoc --symlink apidocs docs/api
618 # @CODE
619 java-pkg_dojavadoc() {
620         debug-print-function ${FUNCNAME} $*
621
622         # For html documentation bundles that link to Javadoc
623         local symlink
624         if [[ ${1} = --symlink ]]; then
625                 symlink=${2}
626                 shift 2
627         fi
628
629         local dir="$1"
630         local dest=/usr/share/doc/${PF}/html
631
632         # QA checks
633
634         java-pkg_check-phase install
635         java-pkg_init_paths_
636
637         [[ -z "${dir}" ]] && die "Must specify a directory!"
638         [[ ! -d "${dir}" ]] && die "${dir} does not exist, or isn't a directory!"
639         if [[ ! -e "${dir}/index.html" ]]; then
640                 local msg="No index.html in javadoc directory"
641                 ewarn "${msg}"
642                 is-java-strict && die "${msg}"
643         fi
644
645         if [[ -e ${D}/${dest}/api ]]; then
646                 eerror "${dest} already exists. Will not overwrite."
647                 die "${dest}"
648         fi
649
650         # Renaming to match our directory layout
651
652         local dir_to_install="${dir}"
653         if [[ "$(basename "${dir}")" != "api" ]]; then
654                 dir_to_install="${T}/api"
655                 # TODO use doins
656                 cp -r "${dir}" "${dir_to_install}" || die "cp failed"
657         fi
658
659         # Actual installation
660         java-pkg_dohtml -r "${dir_to_install}"
661
662         # Let's make a symlink to the directory we have everything else under
663         dosym ${dest}/api "${JAVA_PKG_SHAREPATH}/api" || die
664
665         if [[ ${symlink} ]]; then
666                 debug-print "symlinking ${dest}/{api,${symlink}}"
667                 dosym ${dest}/{api,${symlink}} || die
668         fi
669
670         # Extra logging if enabled.
671         if [[ -n ${JAVA_PKG_DEBUG} ]]; then
672                 einfo "Verbose logging for \"${FUNCNAME}\" function"
673                 einfo "Documentation destination: ${dest}"
674                 einfo "Directory to install: ${dir_to_install}"
675                 einfo "Complete command:"
676                 einfo "${FUNCNAME} ${@}"
677         fi
678 }
679
680 # @FUNCTION: java-pkg_dosrc
681 # @USAGE: <path/to/sources> [...]
682 # @DESCRIPTION:
683 # Installs a zip containing the source for a package, so it can used in
684 # from IDEs like eclipse and netbeans.
685 # Ebuild needs to DEPEND on app-arch/zip to use this. It also should be controlled by USE=source.
686 #
687 # @CODE
688 # Example:
689 # java-pkg_dosrc src/*
690 # @CODE
691
692 # TODO change so it the arguments it takes are the base directories containing
693 # source -nichoj
694 #
695 # TODO should we be able to handle multiple calls to dosrc? -nichoj
696 #
697 # TODO maybe we can take an existing zip/jar? -nichoj
698 #
699 # FIXME apparently this fails if you give it an empty directories
700 java-pkg_dosrc() {
701         debug-print-function ${FUNCNAME} $*
702
703         [ ${#} -lt 1 ] && die "At least one argument needed"
704
705         java-pkg_check-phase install
706
707         [[ ${#} -lt 1 ]] && die "At least one argument needed"
708
709         if ! [[ ${DEPEND} = *app-arch/zip* ]]; then
710                 local msg="${FUNCNAME} called without app-arch/zip in DEPEND"
711                 java-pkg_announce-qa-violation ${msg}
712         fi
713
714         java-pkg_init_paths_
715
716         local zip_name="${PN}-src.zip"
717         local zip_path="${T}/${zip_name}"
718         local dir
719         for dir in "${@}"; do
720                 local dir_parent=$(dirname "${dir}")
721                 local dir_name=$(basename "${dir}")
722                 pushd ${dir_parent} > /dev/null || die "problem entering ${dir_parent}"
723                 zip -q -r ${zip_path} ${dir_name} -i '*.java'
724                 local result=$?
725                 # 12 means zip has nothing to do
726                 if [[ ${result} != 12 && ${result} != 0 ]]; then
727                         die "failed to zip ${dir_name}"
728                 fi
729                 popd >/dev/null || die
730         done
731
732         # Install the zip
733         (
734                 insinto "${JAVA_PKG_SOURCESPATH}"
735                 doins ${zip_path}
736         ) || die "Failed to install source"
737
738         JAVA_SOURCES="${JAVA_PKG_SOURCESPATH}/${zip_name}"
739
740         # Extra logging if enabled.
741         if [[ -n ${JAVA_PKG_DEBUG} ]]; then
742                 einfo "Verbose logging for \"${FUNCNAME}\" function"
743                 einfo "Zip filename created: ${zip_name}"
744                 einfo "Zip file destination: ${JAVA_PKG_SOURCESPATH}"
745                 einfo "Directories zipped: ${@}"
746                 einfo "Complete command:"
747                 einfo "${FUNCNAME} ${@}"
748         fi
749
750         java-pkg_do_write_
751 }
752
753 # @FUNCTION: java-pkg_dolauncher
754 # @USAGE: <filename> [options]
755 # @DESCRIPTION:
756 # Make a wrapper script to lauch/start this package
757 # If necessary, the wrapper will switch to the appropriate VM.
758 #
759 # Can be called without parameters if the package installs only one jar
760 # that has the Main-class attribute set. The wrapper will be named ${PN}.
761 #
762 # @CODE
763 # Parameters:
764 # $1 - filename of launcher to create
765 # $2 - options, as follows:
766 #  --main the.main.class.to.start
767 #  --jar /the/jar/too/launch.jar or just <name>.jar
768 #  --java_args 'Extra arguments to pass to java'
769 #  --pkg_args 'Extra arguments to pass to the package'
770 #  --pwd Directory the launcher changes to before executing java
771 #  -into Directory to install the launcher to, instead of /usr/bin
772 #  -pre Prepend contents of this file to the launcher
773 # @CODE
774 java-pkg_dolauncher() {
775         debug-print-function ${FUNCNAME} $*
776
777         java-pkg_check-phase install
778         java-pkg_init_paths_
779
780         if [[ ${#} = 0 ]]; then
781                 local name="${PN}"
782         else
783                 local name="${1}"
784                 shift
785         fi
786
787         # TODO rename to launcher
788         local target="${T}/${name}"
789         local var_tmp="${T}/launcher_variables_tmp"
790         local target_dir pre
791
792         # Process the other the rest of the arguments
793         while [[ -n "${1}" && -n "${2}" ]]; do
794                 local var="${1}" value="${2}"
795                 if [[ "${var:0:2}" == "--" ]]; then
796                         local var=${var:2}
797                         echo "gjl_${var}=\"${value}\"" >> "${var_tmp}"
798                         local gjl_${var}="${value}"
799                 elif [[ "${var}" == "-into" ]]; then
800                         target_dir="${value}"
801                 elif [[ "${var}" == "-pre" ]]; then
802                         pre="${value}"
803                 fi
804                 shift 2
805         done
806
807         # Test if no --jar and --main arguments were given and
808         # in that case check if the package only installs one jar
809         # and use that jar.
810         if [[ -z "${gjl_jar}" && -z "${gjl_main}" ]]; then
811                 local cp="${JAVA_PKG_CLASSPATH}"
812                 if [[ "${cp/:}" = "${cp}" && "${cp%.jar}" != "${cp}" ]]; then
813                         echo "gjl_jar=\"${JAVA_PKG_CLASSPATH}\"" >> "${var_tmp}"
814                 else
815                         local msg="Not enough information to create a launcher given."
816                         msg="${msg} Please give --jar or --main argument to ${FUNCNAME}."
817                         die "${msg}"
818                 fi
819         fi
820
821         # Write the actual script
822         echo "#!/bin/bash" > "${target}"
823         if [[ -n "${pre}" ]]; then
824                 if [[ -f "${pre}" ]]; then
825                         cat "${pre}" >> "${target}"
826                 else
827                         die "-pre specified file '${pre}' does not exist"
828                 fi
829         fi
830         echo "gjl_package=${JAVA_PKG_NAME}" >> "${target}"
831         cat "${var_tmp}" >> "${target}"
832         rm -f "${var_tmp}"
833         echo "source ${EPREFIX}/usr/share/java-config-2/launcher/launcher.bash" >> "${target}"
834
835         if [[ -n "${target_dir}" ]]; then
836                 (
837                         into "${target_dir}"
838                         dobin "${target}"
839                 )
840                 local ret=$?
841                 return ${ret}
842         else
843                 dobin "${target}"
844         fi
845 }
846
847 # @FUNCTION: java-pkg_dowar
848 # @DESCRIPTION:
849 # Install war files.
850 # TODO document
851 java-pkg_dowar() {
852         debug-print-function ${FUNCNAME} $*
853
854         # Check for arguments
855         [[ ${#} -lt 1 ]] && die "At least one argument needed"
856         java-pkg_check-phase install
857
858         java-pkg_init_paths_
859
860         local war
861         for war in $* ; do
862                 local warpath
863                 # TODO evaluate if we want to handle symlinks differently -nichoj
864                 # Check for symlink
865                 if [[ -L "${war}" ]] ; then
866                         cp "${war}" "${T}"
867                         warpath="${T}$(basename "${war}")"
868                 # Check for directory
869                 # TODO evaluate if we want to handle directories differently -nichoj
870                 elif [[ -d "${war}" ]] ; then
871                         echo "dowar: warning, skipping directory ${war}"
872                         continue
873                 else
874                         warpath="${war}"
875                 fi
876
877                 # Install those files like you mean it
878                 (
879                         insopts -m0644
880                         insinto "${JAVA_PKG_WARDEST}"
881                         doins ${warpath}
882                 )
883         done
884 }
885
886 # @FUNCTION: java-pkg_recordjavadoc
887 # @INTERNAL
888 # @DESCRIPTION:
889 # Scan for JavaDocs, and record their existence in the package.env file
890
891 # TODO make sure this in the proper section
892 java-pkg_recordjavadoc()
893 {
894         debug-print-function ${FUNCNAME} $*
895         # the find statement is important
896         # as some packages include multiple trees of javadoc
897         JAVADOC_PATH="$(find ${D}/usr/share/doc/ -name allclasses-frame.html -printf '%h:')"
898         # remove $D - TODO: check this is ok with all cases of the above
899         JAVADOC_PATH="${JAVADOC_PATH//${D}}"
900         if [[ -n "${JAVADOC_PATH}" ]] ; then
901                 debug-print "javadocs found in ${JAVADOC_PATH%:}"
902                 java-pkg_do_write_
903         else
904                 debug-print "No javadocs found"
905         fi
906 }
907
908
909 # @FUNCTION: java-pkg_jar-from
910 # @USAGE: [--build-only] [--with-dependencies] [--virtual] [--into dir] <package> [<package.jar>] [<destination.jar>]
911 # @DESCRIPTION:
912 # Makes a symlink to a jar from a certain package
913 # A lot of java packages include dependencies in a lib/ directory
914 # You can use this function to replace these bundled dependencies.
915 # The dependency is recorded into package.env DEPEND line, unless "--build-only"
916 # is passed as the very first argument, for jars that have to be present only
917 # at build time and are not needed on runtime (junit testing etc).
918 #
919 # @CODE
920 # Example: get all jars from xerces slot 2
921 #       java-pkg_jar-from xerces-2
922 #
923 # Example: get a specific jar from xerces slot 2
924 #       java-pkg_jar-from xerces-2 xml-apis.jar
925 #
926 # Example: get a specific jar from xerces slot 2, and name it diffrently
927 #       java-pkg_jar-from xerces-2 xml-apis.jar xml.jar
928 #
929 # Example: get junit.jar which is needed only for building
930 #       java-pkg_jar-from --build-only junit junit.jar
931 # @CODE
932 #
933 # @CODE
934 # Parameters
935 #       --build-only - makes the jar(s) not added into package.env DEPEND line.
936 #         (assumed automatically when called inside src_test)
937 #       --with-dependencies - get jars also from requested package's dependencies
938 #         transitively.
939 #       --virtual - Packages passed to this function are to be handled as virtuals
940 #         and will not have individual jar dependencies recorded.
941 #       --into $dir - symlink jar(s) into $dir (must exist) instead of .
942 # $1 - Package to get jars from, or comma-separated list of packages in
943 #       case other parameters are not used.
944 # $2 - jar from package. If not specified, all jars will be used.
945 # $3 - When a single jar is specified, destination filename of the
946 #       symlink. Defaults to the name of the jar.
947 # @CODE
948
949 # TODO could probably be cleaned up a little
950 java-pkg_jar-from() {
951         debug-print-function ${FUNCNAME} $*
952
953         local build_only=""
954         local destdir="."
955         local deep=""
956         local virtual=""
957         local record_jar=""
958
959         [[ "${EBUILD_PHASE}" == "test" ]] && build_only="build"
960
961         while [[ "${1}" == --* ]]; do
962                 if [[ "${1}" = "--build-only" ]]; then
963                         build_only="build"
964                 elif [[ "${1}" = "--with-dependencies" ]]; then
965                         deep="--with-dependencies"
966                 elif [[ "${1}" = "--virtual" ]]; then
967                         virtual="true"
968                 elif [[ "${1}" = "--into" ]]; then
969                         destdir="${2}"
970                         shift
971                 else
972                         die "java-pkg_jar-from called with unknown parameter: ${1}"
973                 fi
974                 shift
975         done
976
977         local target_pkg="${1}" target_jar="${2}" destjar="${3}"
978
979         [[ -z ${target_pkg} ]] && die "Must specify a package"
980
981         if [[ "${EAPI}" == "1" ]]; then
982                 target_pkg="${target_pkg//:/-}"
983         fi
984
985         # default destjar to the target jar
986         [[ -z "${destjar}" ]] && destjar="${target_jar}"
987
988         local error_msg="There was a problem getting the classpath for ${target_pkg}."
989         local classpath
990         classpath="$(java-config ${deep} --classpath=${target_pkg})"
991         [[ $? != 0 ]] && die ${error_msg}
992
993         # When we have commas this functions is called to bring jars from multiple
994         # packages. This affects recording of dependencencies performed later
995         # which expects one package only, so we do it here.
996         if [[ ${target_pkg} = *,* ]]; then
997                 for pkg in ${target_pkg//,/ }; do
998                         java-pkg_ensure-dep "${build_only}" "${pkg}"
999                         [[ -z "${build_only}" ]] && java-pkg_record-jar_ "${pkg}"
1000                 done
1001                 # setting this disables further record-jar_ calls later
1002                 record_jar="true"
1003         else
1004                 java-pkg_ensure-dep "${build_only}" "${target_pkg}"
1005         fi
1006
1007         # Record the entire virtual as a dependency so that
1008         # no jars are missed.
1009         if [[ -z "${build_only}" && -n "${virtual}" ]]; then
1010                 java-pkg_record-jar_ "${target_pkg}"
1011                 # setting this disables further record-jars_ calls later
1012                 record_jar="true"
1013         fi
1014
1015         pushd ${destdir} > /dev/null \
1016                 || die "failed to change directory to ${destdir}"
1017
1018         local jar
1019         for jar in ${classpath//:/ }; do
1020                 local jar_name=$(basename "${jar}")
1021                 if [[ ! -f "${jar}" ]] ; then
1022                         debug-print "${jar} from ${target_pkg} does not exist"
1023                         die "Installation problems with jars in ${target_pkg} - is it installed?"
1024                 fi
1025                 # If no specific target jar was indicated, link it
1026                 if [[ -z "${target_jar}" ]] ; then
1027                         [[ -f "${target_jar}" ]]  && rm "${target_jar}"
1028                         ln -snf "${jar}" \
1029                                 || die "Failed to make symlink from ${jar} to ${jar_name}"
1030                         if [[ -z "${record_jar}" ]]; then
1031                                 if [[ -z "${build_only}" ]]; then
1032                                         java-pkg_record-jar_ "${target_pkg}" "${jar}"
1033                                 else
1034                                         java-pkg_record-jar_ --build-only "${target_pkg}" "${jar}"
1035                                 fi
1036                         fi
1037                 # otherwise, if the current jar is the target jar, link it
1038                 elif [[ "${jar_name}" == "${target_jar}" ]] ; then
1039                         [[ -f "${destjar}" ]]  && rm "${destjar}"
1040                         ln -snf "${jar}" "${destjar}" \
1041                                 || die "Failed to make symlink from ${jar} to ${destjar}"
1042                         if [[ -z "${record_jar}" ]]; then
1043                                 if [[ -z "${build_only}" ]]; then
1044                                         java-pkg_record-jar_ "${target_pkg}" "${jar}"
1045                                 else
1046                                         java-pkg_record-jar_ --build-only "${target_pkg}" "${jar}"
1047                                 fi
1048                         fi
1049                         popd > /dev/null || die
1050                         return 0
1051                 fi
1052         done
1053         popd > /dev/null || die
1054         # if no target was specified, we're ok
1055         if [[ -z "${target_jar}" ]] ; then
1056                 return 0
1057         # otherwise, die bitterly
1058         else
1059                 die "Failed to find ${target_jar:-jar} in ${target_pkg}"
1060         fi
1061 }
1062
1063 # @FUNCTION: java-pkg_jarfrom
1064 # @DESCRIPTION:
1065 # See java-pkg_jar-from
1066 java-pkg_jarfrom() {
1067         java-pkg_jar-from "$@"
1068 }
1069
1070 # @FUNCTION: java-pkg_getjars
1071 # @USAGE: [--build-only] [--with-dependencies] <package1>[,<package2>...]
1072 # @DESCRIPTION:
1073 # Get the classpath provided by any number of packages
1074 # Among other things, this can be passed to 'javac -classpath' or 'ant -lib'.
1075 # The providing packages are recorded as dependencies into package.env DEPEND
1076 # line, unless "--build-only" is passed as the very first argument, for jars
1077 # that have to be present only at build time and are not needed on runtime
1078 # (junit testing etc).
1079 #
1080 # @CODE
1081 # Example: Get the classpath for xerces-2 and xalan,
1082 #       java-pkg_getjars xerces-2,xalan
1083 #
1084 # Example Return:
1085 #       /usr/share/xerces-2/lib/xml-apis.jar:/usr/share/xerces-2/lib/xmlParserAPIs.jar:/usr/share/xalan/lib/xalan.jar
1086 #
1087 #
1088 # Parameters:
1089 #       --build-only - makes the jar(s) not added into package.env DEPEND line.
1090 #         (assumed automatically when called inside src_test)
1091 #       --with-dependencies - get jars also from requested package's dependencies
1092 #         transitively.
1093 # $1 - list of packages to get jars from
1094 #   (passed to java-config --classpath)
1095 # @CODE
1096 java-pkg_getjars() {
1097         debug-print-function ${FUNCNAME} $*
1098
1099         local build_only=""
1100         local deep=""
1101
1102         [[ "${EBUILD_PHASE}" == "test" ]] && build_only="build"
1103
1104         while [[ "${1}" == --* ]]; do
1105                 if [[ "${1}" = "--build-only" ]]; then
1106                         build_only="build"
1107                 elif [[ "${1}" = "--with-dependencies" ]]; then
1108                         deep="--with-dependencies"
1109                 else
1110                         die "java-pkg_jar-from called with unknown parameter: ${1}"
1111                 fi
1112                 shift
1113         done
1114
1115         [[ ${#} -ne 1 ]] && die "${FUNCNAME} takes only one argument besides --*"
1116
1117
1118         local pkgs="${1}"
1119
1120         if [[ "${EAPI}" == "1" ]]; then
1121                 pkgs="${pkgs//:/-}"
1122         fi
1123
1124         jars="$(java-config ${deep} --classpath=${pkgs})"
1125         [[ $? != 0 ]] && die "java-config --classpath=${pkgs} failed"
1126         debug-print "${pkgs}:${jars}"
1127
1128         for pkg in ${pkgs//,/ }; do
1129                 java-pkg_ensure-dep "${build_only}" "${pkg}"
1130         done
1131
1132         for pkg in ${pkgs//,/ }; do
1133                 if [[ -z "${build_only}" ]]; then
1134                         java-pkg_record-jar_ "${pkg}"
1135                 else
1136                         java-pkg_record-jar_ --build-only "${pkg}"
1137                 fi
1138         done
1139
1140         echo "${jars}"
1141 }
1142
1143 # @FUNCTION: java-pkg_getjar
1144 # @USAGE: [--build-only] [--virtual] <package> <jarfile>
1145 # @DESCRIPTION:
1146 # Get the complete path of a single jar from a package
1147 # The providing package is recorded as runtime dependency into package.env
1148 # DEPEND line, unless "--build-only" is passed as the very first argument, for
1149 # jars that have to be present only at build time and are not needed on runtime
1150 # (junit testing etc).
1151 #
1152 # @CODE
1153 # Example:
1154 #       java-pkg_getjar xerces-2 xml-apis.jar
1155 # returns
1156 #       /usr/share/xerces-2/lib/xml-apis.jar
1157 #
1158 # Parameters:
1159 #       --build-only - makes the jar not added into package.env DEPEND line.
1160 #       --virtual - Packages passed to this function are to be handled as virtuals
1161 #         and will not have individual jar dependencies recorded.
1162 # $1 - package to use
1163 # $2 - jar to get
1164 # @CODE
1165 java-pkg_getjar() {
1166         debug-print-function ${FUNCNAME} $*
1167
1168         local build_only=""
1169         local virtual=""
1170         local record_jar=""
1171
1172         [[ "${EBUILD_PHASE}" == "test" ]] && build_only="build"
1173
1174         while [[ "${1}" == --* ]]; do
1175                 if [[ "${1}" = "--build-only" ]]; then
1176                         build_only="build"
1177                 elif [[ "${1}" == "--virtual" ]]; then
1178                         virtual="true"
1179                 else
1180                         die "java-pkg_getjar called with unknown parameter: ${1}"
1181                 fi
1182                 shift
1183         done
1184
1185         [[ ${#} -ne 2 ]] && die "${FUNCNAME} takes only two arguments besides --*"
1186
1187         local pkg="${1}" target_jar="${2}" jar
1188
1189         if [[ "${EAPI}" == "1" ]]; then
1190                 pkg="${pkg//:/-}"
1191         fi
1192
1193         [[ -z ${pkg} ]] && die "Must specify package to get a jar from"
1194         [[ -z ${target_jar} ]] && die "Must specify jar to get"
1195
1196         local error_msg="Could not find classpath for ${pkg}. Are you sure its installed?"
1197         local classpath
1198         classpath=$(java-config --classpath=${pkg})
1199         [[ $? != 0 ]] && die ${error_msg}
1200
1201         java-pkg_ensure-dep "${build_only}" "${pkg}"
1202
1203         # Record the package(Virtual) as a dependency and then set build_only
1204         # So that individual jars are not recorded.
1205         if [[ -n "${virtual}" ]]; then
1206                 if [[ -z "${build_only}" ]]; then
1207                         java-pkg_record-jar_ "${pkg}"
1208                 else
1209                         java-pkg_record-jar_ --build-only "${pkg}"
1210                 fi
1211                 record_jar="true"
1212         fi
1213
1214         for jar in ${classpath//:/ }; do
1215                 if [[ ! -f "${jar}" ]] ; then
1216                         die "Installation problem with jar ${jar} in ${pkg} - is it installed?"
1217                 fi
1218
1219                 if [[ "$(basename ${jar})" == "${target_jar}" ]] ; then
1220                         # Only record jars that aren't build-only
1221                         if [[ -z "${record_jar}" ]]; then
1222                                 if [[ -z "${build_only}" ]]; then
1223                                         java-pkg_record-jar_ "${pkg}" "${jar}"
1224                                 else
1225                                         java-pkg_record-jar_ --build-only "${pkg}" "${jar}"
1226                                 fi
1227                         fi
1228                         echo "${jar}"
1229                         return 0
1230                 fi
1231         done
1232
1233         die "Could not find ${target_jar} in ${pkg}"
1234         return 1
1235 }
1236
1237 # @FUNCTION: java-pkg_register-dependency
1238 # @USAGE: <package>[,<package2>...] [<jarfile>]
1239 # @DESCRIPTION:
1240 # Registers runtime dependency on a package, list of packages, or a single jar
1241 # from a package, into package.env DEPEND line. Can only be called in
1242 # src_install phase.
1243 # Intended for binary packages where you don't need to symlink the jars or get
1244 # their classpath during build. As such, the dependencies only need to be
1245 # specified in ebuild's RDEPEND, and should be omitted in DEPEND.
1246 #
1247 # @CODE
1248 # Parameters:
1249 # $1 - comma-separated list of packages, or a single package
1250 # $2 - if param $1 is a single package, optionally specify the jar
1251 #   to depend on
1252 #
1253 # Examples:
1254 # Record the dependency on whole xerces-2 and xalan,
1255 #       java-pkg_register-dependency xerces-2,xalan
1256 #
1257 # Record the dependency on ant.jar from ant-core
1258 #       java-pkg_register-dependency ant-core ant.jar
1259 # @CODE
1260 #
1261 # Note: Passing both list of packages as the first parameter AND specifying the
1262 # jar as the second is not allowed and will cause the function to die. We assume
1263 # that there's more chance one passes such combination as a mistake, than that
1264 # there are more packages providing identically named jar without class
1265 # collisions.
1266 java-pkg_register-dependency() {
1267         debug-print-function ${FUNCNAME} $*
1268
1269         java-pkg_check-phase install
1270
1271         [[ ${#} -gt 2 ]] && die "${FUNCNAME} takes at most two arguments"
1272
1273         local pkgs="${1}"
1274         local jar="${2}"
1275
1276         [[ -z "${pkgs}" ]] && die "${FUNCNAME} called with no package(s) specified"
1277
1278         if [[ "${EAPI}" == "1" ]]; then
1279                 pkgs="${pkgs//:/-}"
1280         fi
1281
1282         if [[ -z "${jar}" ]]; then
1283                 for pkg in ${pkgs//,/ }; do
1284                         java-pkg_ensure-dep runtime "${pkg}"
1285                         java-pkg_record-jar_ "${pkg}"
1286                 done
1287         else
1288                 [[ ${pkgs} == *,* ]] && \
1289                         die "${FUNCNAME} called with both package list and jar name"
1290                 java-pkg_ensure-dep runtime "${pkgs}"
1291                 java-pkg_record-jar_ "${pkgs}" "${jar}"
1292         fi
1293
1294         java-pkg_do_write_
1295 }
1296
1297 # @FUNCTION: java-pkg_register-optional-dependency
1298 # @USAGE: <package>[,<package2>...] [<jarfile>]
1299 # @DESCRIPTION:
1300 # Registers optional runtime dependency on a package, list of packages, or a
1301 # single jar from a package, into package.env OPTIONAL_DEPEND line. Can only be
1302 # called in src_install phase.
1303 # Intended for packages that can use other packages when those are in classpath.
1304 # Will be put on classpath by launcher if they are installed. Typical case is
1305 # JDBC implementations for various databases. It's better than having USE flag
1306 # for each implementation triggering hard dependency.
1307 #
1308 # @CODE
1309 # Parameters:
1310 # $1 - comma-separated list of packages, or a single package
1311 # $2 - if param $1 is a single package, optionally specify the jar to depend on
1312 #
1313 # Example:
1314 # Record the optional dependency on some jdbc providers
1315 #       java-pkg_register-optional-dependency jdbc-jaybird,jtds-1.2,jdbc-mysql
1316 # @CODE
1317 #
1318 # Note: Passing both list of packages as the first parameter AND specifying the
1319 # jar as the second is not allowed and will cause the function to die. We assume
1320 # that there's more chance one passes such combination as a mistake, than that
1321 # there are more packages providing identically named jar without class
1322 # collisions.
1323 java-pkg_register-optional-dependency() {
1324         debug-print-function ${FUNCNAME} $*
1325
1326         java-pkg_check-phase install
1327
1328         [[ ${#} -gt 2 ]] && die "${FUNCNAME} takes at most two arguments"
1329
1330         local pkgs="${1}"
1331         local jar="${2}"
1332
1333         [[ -z "${pkgs}" ]] && die "${FUNCNAME} called with no package(s) specified"
1334
1335         if [[ "${EAPI}" == "1" ]]; then
1336                 pkgs="${pkgs//:/-}"
1337         fi
1338
1339         if [[ -z "${jar}" ]]; then
1340                 for pkg in ${pkgs//,/ }; do
1341                         java-pkg_record-jar_ --optional "${pkg}"
1342                 done
1343         else
1344                 [[ ${pkgs} == *,* ]] && \
1345                         die "${FUNCNAME} called with both package list and jar name"
1346                 java-pkg_record-jar_ --optional "${pkgs}" "${jar}"
1347         fi
1348
1349         java-pkg_do_write_
1350 }
1351
1352 # @FUNCTION: java-pkg_register-environment-variable
1353 # @USAGE: <name> <value>
1354 # @DESCRIPTION:
1355 # Register an arbitrary environment variable into package.env. The gjl launcher
1356 # for this package or any package depending on this will export it into
1357 # environement before executing java command.
1358 # Must only be called in src_install phase.
1359 JAVA_PKG_EXTRA_ENV="${T}/java-pkg-extra-env"
1360 JAVA_PKG_EXTRA_ENV_VARS=""
1361 java-pkg_register-environment-variable() {
1362         debug-print-function ${FUNCNAME} $*
1363
1364         java-pkg_check-phase install
1365
1366         [[ ${#} != 2 ]] && die "${FUNCNAME} takes two arguments"
1367
1368         echo "${1}=\"${2}\"" >> ${JAVA_PKG_EXTRA_ENV}
1369         JAVA_PKG_EXTRA_ENV_VARS="${JAVA_PKG_EXTRA_ENV_VARS} ${1}"
1370
1371         java-pkg_do_write_
1372 }
1373
1374 # @FUNCTION: java-pkg_get-bootclasspath
1375 # @USAGE: <version>
1376 # @DESCRIPTION:
1377 # Returns classpath of a given bootclasspath-providing package version.
1378 #
1379 # @param $1 - the version of bootclasspath (e.g. 1.5), 'auto' for bootclasspath
1380 #             of the current JDK
1381 java-pkg_get-bootclasspath() {
1382         local version="${1}"
1383
1384         local bcp
1385         case "${version}" in
1386                 auto)
1387                         bcp="$(java-config -g BOOTCLASSPATH)"
1388                         ;;
1389                 1.5)
1390                         bcp="$(java-pkg_getjars --build-only gnu-classpath-0.98)"
1391                         ;;
1392                 *)
1393                         eerror "unknown parameter of java-pkg_get-bootclasspath"
1394                         die "unknown parameter of java-pkg_get-bootclasspath"
1395                         ;;
1396         esac
1397
1398         echo "${bcp}"
1399 }
1400
1401
1402 # This function reads stdin, and based on that input, figures out how to
1403 # populate jars from the filesystem.
1404 # Need to figure out a good way of making use of this, ie be able to use a
1405 # string that was built instead of stdin
1406 # NOTE: this isn't quite ready for primetime.
1407 #java-pkg_populate-jars() {
1408 #       local line
1409 #
1410 #       read line
1411 #       while [[ -n "${line}" ]]; do
1412 #               # Ignore comments
1413 #               [[ ${line%%#*} == "" ]] && continue
1414 #
1415 #               # get rid of any spaces
1416 #               line="${line// /}"
1417 #
1418 #               # format: path=jarinfo
1419 #               local path=${line%%=*}
1420 #               local jarinfo=${line##*=}
1421 #
1422 #               # format: jar@package
1423 #               local jar=${jarinfo%%@*}.jar
1424 #               local package=${jarinfo##*@}
1425 #               if [[ -n ${replace_only} ]]; then
1426 #                       [[ ! -f $path ]] && die "No jar exists at ${path}"
1427 #               fi
1428 #               if [[ -n ${create_parent} ]]; then
1429 #                       local parent=$(dirname ${path})
1430 #                       mkdir -p "${parent}"
1431 #               fi
1432 #               java-pkg_jar-from "${package}" "${jar}" "${path}"
1433 #
1434 #               read line
1435 #       done
1436 #}
1437
1438 # @FUNCTION: java-pkg_find-normal-jars
1439 # @USAGE: [<path/to/directory>]
1440 # @DESCRIPTION:
1441 # Find the files with suffix .jar file in the given directory (default: $WORKDIR)
1442 java-pkg_find-normal-jars() {
1443         local dir=$1
1444         [[ "${dir}" ]] || dir="${WORKDIR}"
1445         local found
1446         for jar in $(find "${dir}" -name "*.jar" -type f); do
1447                 echo "${jar}"
1448                 found="true"
1449         done
1450         [[ "${found}" ]]
1451         return $?
1452 }
1453
1454 # @FUNCTION: java-pkg_ensure-no-bundled-jars
1455 # @DESCRIPTION:
1456 # Try to locate bundled jar files in ${WORKDIR} and die if found.
1457 # This function should be called after WORKDIR has been populated with symlink
1458 # to system jar files or bundled jars removed.
1459 java-pkg_ensure-no-bundled-jars() {
1460         debug-print-function ${FUNCNAME} $*
1461
1462         local bundled_jars=$(java-pkg_find-normal-jars)
1463         if [[ -n ${bundled_jars} ]]; then
1464                 echo "Bundled jars found:"
1465                 local jar
1466                 for jar in ${bundled_jars}; do
1467                         echo $(pwd)${jar/./}
1468                 done
1469                 die "Bundled jars found!"
1470         fi
1471 }
1472
1473 # @FUNCTION: java-pkg_ensure-vm-version-sufficient
1474 # @INTERNAL
1475 # @DESCRIPTION:
1476 # Checks if we have a sufficient VM and dies if we don't.
1477 java-pkg_ensure-vm-version-sufficient() {
1478         debug-print-function ${FUNCNAME} $*
1479
1480         if ! java-pkg_is-vm-version-sufficient; then
1481                 debug-print "VM is not suffient"
1482                 eerror "Current Java VM cannot build this package"
1483                 einfo "Please use java-config -S to set the correct one"
1484                 die "Active Java VM cannot build this package"
1485         fi
1486 }
1487
1488 # @FUNCTION: java-pkg_is-vm-version-sufficient
1489 # @INTERNAL
1490 # @DESCRIPTION:
1491 # @RETURN: zero - VM is sufficient; non-zero - VM is not sufficient
1492 java-pkg_is-vm-version-sufficient() {
1493         debug-print-function ${FUNCNAME} $*
1494
1495         depend-java-query --is-sufficient "${DEPEND}" > /dev/null
1496         return $?
1497 }
1498
1499 # @FUNCTION: java-pkg_ensure-vm-version-eq
1500 # @INTERNAL
1501 # @DESCRIPTION:
1502 # Die if the current VM is not equal to the argument passed.
1503 #
1504 # @param $@ - Desired VM version to ensure
1505 java-pkg_ensure-vm-version-eq() {
1506         debug-print-function ${FUNCNAME} $*
1507
1508         if ! java-pkg_is-vm-version-eq $@ ; then
1509                 debug-print "VM is not suffient"
1510                 eerror "This package requires a Java VM version = $@"
1511                 einfo "Please use java-config -S to set the correct one"
1512                 die "Active Java VM too old"
1513         fi
1514 }
1515
1516 # @FUNCTION: java-pkg_is-vm-version-eq
1517 # @USAGE: <version>
1518 # @INTERNAL
1519 # @RETURN: zero - VM versions are equal; non-zero - VM version are not equal
1520 java-pkg_is-vm-version-eq() {
1521         debug-print-function ${FUNCNAME} $*
1522
1523         local needed_version="$@"
1524
1525         [[ -z "${needed_version}" ]] && die "need an argument"
1526
1527         local vm_version="$(java-pkg_get-vm-version)"
1528
1529         vm_version="$(get_version_component_range 1-2 "${vm_version}")"
1530         needed_version="$(get_version_component_range 1-2 "${needed_version}")"
1531
1532         if [[ -z "${vm_version}" ]]; then
1533                 debug-print "Could not get JDK version from DEPEND"
1534                 return 1
1535         else
1536                 if [[ "${vm_version}" == "${needed_version}" ]]; then
1537                         debug-print "Detected a JDK(${vm_version}) = ${needed_version}"
1538                         return 0
1539                 else
1540                         debug-print "Detected a JDK(${vm_version}) != ${needed_version}"
1541                         return 1
1542                 fi
1543         fi
1544 }
1545
1546 # @FUNCTION: java-pkg_ensure-vm-version-ge
1547 # @INTERNAL
1548 # @DESCRIPTION:
1549 # Die if the current VM is not greater than the desired version
1550 #
1551 # @param $@ - VM version to compare current to
1552 java-pkg_ensure-vm-version-ge() {
1553         debug-print-function ${FUNCNAME} $*
1554
1555         if ! java-pkg_is-vm-version-ge "$@" ; then
1556                 debug-print "vm is not suffient"
1557                 eerror "This package requires a Java VM version >= $@"
1558                 einfo "Please use java-config -S to set the correct one"
1559                 die "Active Java VM too old"
1560         fi
1561 }
1562
1563 # @FUNCTION: java-pkg_is-vm-version-ge
1564 # @INTERNAL
1565 # @DESCRIPTION:
1566 # @CODE
1567 # Parameters:
1568 # $@ - VM version to compare current VM to
1569 # @CODE
1570 # @RETURN: zero - current VM version is greater than checked version;
1571 #       non-zero - current VM version is not greater than checked version
1572 java-pkg_is-vm-version-ge() {
1573         debug-print-function ${FUNCNAME} $*
1574
1575         local needed_version=$@
1576         local vm_version=$(java-pkg_get-vm-version)
1577         if [[ -z "${vm_version}" ]]; then
1578                 debug-print "Could not get JDK version from DEPEND"
1579                 return 1
1580         else
1581                 if version_is_at_least "${needed_version}" "${vm_version}"; then
1582                         debug-print "Detected a JDK(${vm_version}) >= ${needed_version}"
1583                         return 0
1584                 else
1585                         debug-print "Detected a JDK(${vm_version}) < ${needed_version}"
1586                         return 1
1587                 fi
1588         fi
1589 }
1590
1591 java-pkg_set-current-vm() {
1592         export GENTOO_VM=${1}
1593 }
1594
1595 java-pkg_get-current-vm() {
1596         echo ${GENTOO_VM}
1597 }
1598
1599 java-pkg_current-vm-matches() {
1600         has $(java-pkg_get-current-vm) ${@}
1601         return $?
1602 }
1603
1604 # @FUNCTION: java-pkg_get-source
1605 # @DESCRIPTION:
1606 # Determines what source version should be used, for passing to -source.
1607 # Unless you want to break things you probably shouldn't set _WANT_SOURCE
1608 #
1609 # @RETURN: string - Either the lowest possible source, or JAVA_PKG_WANT_SOURCE
1610 java-pkg_get-source() {
1611         echo ${JAVA_PKG_WANT_SOURCE:-$(depend-java-query --get-lowest "${DEPEND} ${RDEPEND}")}
1612 }
1613
1614 # @FUNCTION: java-pkg_get-target
1615 # @DESCRIPTION:
1616 # Determines what target version should be used, for passing to -target.
1617 # If you don't care about lower versions, you can set _WANT_TARGET to the
1618 # version of your JDK.
1619 #
1620 # @RETURN: string - Either the lowest possible target, or JAVA_PKG_WANT_TARGET
1621 java-pkg_get-target() {
1622         echo ${JAVA_PKG_WANT_TARGET:-$(depend-java-query --get-lowest "${DEPEND} ${RDEPEND}")}
1623 }
1624
1625 # @FUNCTION: java-pkg_get-javac
1626 # @DESCRIPTION:
1627 # Returns the compiler executable
1628 java-pkg_get-javac() {
1629         debug-print-function ${FUNCNAME} $*
1630
1631         java-pkg_init-compiler_
1632         local compiler="${GENTOO_COMPILER}"
1633
1634         local compiler_executable
1635         if [[ "${compiler}" = "javac" ]]; then
1636                 # nothing fancy needs to be done for javac
1637                 compiler_executable="javac"
1638         else
1639                 # for everything else, try to determine from an env file
1640
1641                 local compiler_env="/usr/share/java-config-2/compiler/${compiler}"
1642                 if [[ -f ${compiler_env} ]]; then
1643                         local old_javac=${JAVAC}
1644                         unset JAVAC
1645                         # try to get value of JAVAC
1646                         compiler_executable="$(source ${compiler_env} 1>/dev/null 2>&1; echo ${JAVAC})"
1647                         export JAVAC=${old_javac}
1648
1649                         if [[ -z ${compiler_executable} ]]; then
1650                                 die "JAVAC is empty or undefined in ${compiler_env}"
1651                         fi
1652
1653                         # check that it's executable
1654                         if [[ ! -x ${compiler_executable} ]]; then
1655                                 die "${compiler_executable} doesn't exist, or isn't executable"
1656                         fi
1657                 else
1658                         die "Could not find environment file for ${compiler}"
1659                 fi
1660         fi
1661         echo ${compiler_executable}
1662 }
1663
1664 # @FUNCTION: java-pkg_javac-args
1665 # @DESCRIPTION:
1666 # If an ebuild uses javac directly, instead of using ejavac, it should call this
1667 # to know what -source/-target to use.
1668 #
1669 # @RETURN: string - arguments to pass to javac, complete with -target and -source
1670 java-pkg_javac-args() {
1671         debug-print-function ${FUNCNAME} $*
1672
1673         local want_source="$(java-pkg_get-source)"
1674         local want_target="$(java-pkg_get-target)"
1675
1676         local source_str="-source ${want_source}"
1677         local target_str="-target ${want_target}"
1678
1679         debug-print "want source: ${want_source}"
1680         debug-print "want target: ${want_target}"
1681
1682         if [[ -z "${want_source}" || -z "${want_target}" ]]; then
1683                 die "Could not find valid -source/-target values for javac"
1684         else
1685                 echo "${source_str} ${target_str}"
1686         fi
1687 }
1688
1689 # @FUNCTION: java-pkg_get-jni-cflags
1690 # @DESCRIPTION:
1691 # Echos the CFLAGS for JNI compilations
1692 java-pkg_get-jni-cflags() {
1693         local flags="-I${JAVA_HOME}/include"
1694
1695         local platform="linux"
1696         use elibc_FreeBSD && platform="freebsd"
1697
1698         # TODO do a check that the directories are valid
1699         flags="${flags} -I${JAVA_HOME}/include/${platform}"
1700
1701         echo ${flags}
1702 }
1703
1704 java-pkg_ensure-gcj() {
1705         # was enforcing sys-devel/gcc[gcj]
1706         die "${FUNCNAME} was removed. Use use-deps available as of EAPI 2 instead. #261562"
1707 }
1708
1709 java-pkg_ensure-test() {
1710         # was enforcing USE=test if FEATURES=test
1711         die "${FUNCNAME} was removed. Package mangers handle this already. #278965"
1712 }
1713
1714 # @FUNCTION: java-pkg_register-ant-task
1715 # @USAGE: [--version x.y] [<name>]
1716 # @DESCRIPTION:
1717 # Register this package as ant task, so that ant will load it when no specific
1718 # ANT_TASKS are specified. Note that even without this registering, all packages
1719 # specified in ANT_TASKS will be loaded. Mostly used by the actual ant tasks
1720 # packages, but can be also used by other ebuilds that used to symlink their
1721 # .jar into /usr/share/ant-core/lib to get autoloaded, for backwards
1722 # compatibility.
1723 #
1724 # @CODE
1725 # Parameters
1726 # --version x.y Register only for ant version x.y (otherwise for any ant
1727 #               version). Used by the ant-* packages to prevent loading of mismatched
1728 #               ant-core ant tasks after core was updated, before the tasks are updated,
1729 #               without a need for blockers.
1730 # $1 Name to register as. Defaults to JAVA_PKG_NAME ($PN[-$SLOT])
1731 # @CODE
1732 java-pkg_register-ant-task() {
1733         local TASKS_DIR="tasks"
1734
1735         # check for --version x.y parameters
1736         while [[ -n "${1}" && -n "${2}" ]]; do
1737                 local var="${1#--}"
1738                 local val="${2}"
1739                 if [[ "${var}" == "version" ]]; then
1740                         TASKS_DIR="tasks-${val}"
1741                 else
1742                         die "Unknown parameter passed to java-pkg_register-ant-tasks: ${1} ${2}"
1743                 fi
1744                 shift 2
1745         done
1746
1747         local TASK_NAME="${1:-${JAVA_PKG_NAME}}"
1748
1749         dodir /usr/share/ant/${TASKS_DIR}
1750         touch "${ED}/usr/share/ant/${TASKS_DIR}/${TASK_NAME}"
1751 }
1752
1753 # @FUNCTION: java-pkg_ant-tasks-depend
1754 # @INTERNAL
1755 # @DESCRIPTION:
1756 # Translates the WANT_ANT_TASKS variable into valid dependencies.
1757 java-pkg_ant-tasks-depend() {
1758         debug-print-function ${FUNCNAME} ${WANT_ANT_TASKS}
1759
1760         if [[ -n "${WANT_ANT_TASKS}" ]]; then
1761                 local DEP=""
1762                 for i in ${WANT_ANT_TASKS}
1763                 do
1764                         if [[ ${i} = ant-* ]]; then
1765                                 DEP="${DEP}dev-java/${i} "
1766                         elif [[ ${i} = */*:* ]]; then
1767                                 DEP="${DEP}${i} "
1768                         else
1769                                 echo "Invalid atom in WANT_ANT_TASKS: ${i}"
1770                                 return 1
1771                         fi
1772                 done
1773                 echo ${DEP}
1774                 return 0
1775         else
1776                 return 0
1777         fi
1778 }
1779
1780
1781 # @FUNCTION: ejunit_
1782 # @INTERNAL
1783 # @DESCRIPTION:
1784 # Internal Junit wrapper function. Makes it easier to run the tests and checks for
1785 # dev-java/junit in DEPEND. Launches the tests using junit.textui.TestRunner.
1786 # @CODE
1787 # Parameters:
1788 # $1 - junit package (junit or junit-4)
1789 # $2 - -cp or -classpath
1790 # $3 - classpath; junit and recorded dependencies get appended
1791 # $@ - the rest of the parameters are passed to java
1792 # @CODE
1793 ejunit_() {
1794         debug-print-function ${FUNCNAME} $*
1795
1796         local pkgs
1797         if [[ -f ${JAVA_PKG_DEPEND_FILE} ]]; then
1798                 for atom in $(cat ${JAVA_PKG_DEPEND_FILE} | tr : ' '); do
1799                         pkgs=${pkgs},$(echo ${atom} | sed -re "s/^.*@//")
1800                 done
1801         fi
1802
1803         local junit=${1}
1804         shift 1
1805
1806         local cp=$(java-pkg_getjars --with-dependencies ${junit}${pkgs})
1807         if [[ ${1} = -cp || ${1} = -classpath ]]; then
1808                 cp="${2}:${cp}"
1809                 shift 2
1810         else
1811                 cp=".:${cp}"
1812         fi
1813
1814         local runner=junit.textui.TestRunner
1815         if [[ "${junit}" == "junit-4" ]] ; then
1816                 runner=org.junit.runner.JUnitCore
1817         fi
1818         debug-print "Calling: java -cp \"${cp}\" -Djava.io.tmpdir=\"${T}\" -Djava.awt.headless=true ${runner} ${@}"
1819         java -cp "${cp}" -Djava.io.tmpdir="${T}/" -Djava.awt.headless=true ${runner} "${@}" || die "Running junit failed"
1820 }
1821
1822 # @FUNCTION: ejunit
1823 # @DESCRIPTION:
1824 # Junit wrapper function. Makes it easier to run the tests and checks for
1825 # dev-java/junit in DEPEND. Launches the tests using org.junit.runner.JUnitCore.
1826 #
1827 # @CODE
1828 # Parameters:
1829 # $1 - -cp or -classpath
1830 # $2 - classpath; junit and recorded dependencies get appended
1831 # $@ - the rest of the parameters are passed to java
1832 #
1833 # Examples:
1834 # ejunit -cp build/classes org.blinkenlights.jid3.test.AllTests
1835 # ejunit org.blinkenlights.jid3.test.AllTests
1836 # ejunit org.blinkenlights.jid3.test.FirstTest org.blinkenlights.jid3.test.SecondTest
1837 # @CODE
1838 ejunit() {
1839         debug-print-function ${FUNCNAME} $*
1840
1841         ejunit_ "junit" "${@}"
1842 }
1843
1844 # @FUNCTION: ejunit4
1845 # @DESCRIPTION:
1846 # Junit4 wrapper function. Makes it easier to run the tests and checks for
1847 # dev-java/junit:4 in DEPEND. Launches the tests using junit.textui.TestRunner.
1848 #
1849 # @CODE
1850 # Parameters:
1851 # $1 - -cp or -classpath
1852 # $2 - classpath; junit and recorded dependencies get appended
1853 # $@ - the rest of the parameters are passed to java
1854 #
1855 # Examples:
1856 # ejunit4 -cp build/classes org.blinkenlights.jid3.test.AllTests
1857 # ejunit4 org.blinkenlights.jid3.test.AllTests
1858 # ejunit4 org.blinkenlights.jid3.test.FirstTest \
1859 #         org.blinkenlights.jid3.test.SecondTest
1860 # @CODE
1861 ejunit4() {
1862         debug-print-function ${FUNCNAME} $*
1863
1864         ejunit_ "junit-4" "${@}"
1865 }
1866
1867 # @FUNCTION: java-utils-2_src_prepare
1868 # @DESCRIPTION:
1869 # src_prepare Searches for bundled jars
1870 # Don't call directly, but via java-pkg-2_src_prepare!
1871 java-utils-2_src_prepare() {
1872         case ${EAPI:-0} in
1873                 [0-5])
1874                         java-pkg_func-exists java_prepare && java_prepare ;;
1875                 *)
1876                         java-pkg_func-exists java_prepare &&
1877                                 eqawarn "java_prepare is no longer called, define src_prepare instead."
1878                         eapply_user ;;
1879         esac
1880
1881         # Check for files in JAVA_RM_FILES array.
1882         if [[ ${JAVA_RM_FILES[@]} ]]; then
1883                 debug-print "$FUNCNAME: removing unneeded files"
1884                 java-pkg_rm_files "${JAVA_RM_FILES[@]}"
1885         fi
1886
1887         if is-java-strict; then
1888                 echo "Searching for bundled jars:"
1889                 java-pkg_find-normal-jars || echo "None found."
1890                 echo "Searching for bundled classes (no output if none found):"
1891                 find "${WORKDIR}" -name "*.class"
1892                 echo "Search done."
1893         fi
1894 }
1895
1896 # @FUNCTION: java-utils-2_pkg_preinst
1897 # @DESCRIPTION:
1898 # pkg_preinst Searches for missing and unneeded dependencies
1899 # Don't call directly, but via java-pkg-2_pkg_preinst!
1900 java-utils-2_pkg_preinst() {
1901         if is-java-strict; then
1902                 if [[ ! -e "${JAVA_PKG_ENV}" ]] || has ant-tasks ${INHERITED}; then
1903                         return
1904                 fi
1905
1906                 if has_version dev-java/java-dep-check; then
1907                         local output=$(GENTOO_VM= java-dep-check --image "${D}" "${JAVA_PKG_ENV}")
1908                         [[ ${output} ]] && ewarn "${output}"
1909                 else
1910                         eerror "Install dev-java/java-dep-check for dependency checking"
1911                 fi
1912         fi
1913 }
1914
1915 # @FUNCTION: eant
1916 # @USAGE: <ant_build_target(s)>
1917 # @DESCRIPTION:
1918 # Ant wrapper function. Will use the appropriate compiler, based on user-defined
1919 # compiler. Will also set proper ANT_TASKS from the variable ANT_TASKS,
1920 # variables:
1921 #
1922 # @CODE
1923 # Variables:
1924 # EANT_GENTOO_CLASSPATH - calls java-pkg_getjars for the value and adds to the
1925 #               gentoo.classpath property. Be sure to call java-ant_rewrite-classpath in src_unpack.
1926 # EANT_NEEDS_TOOLS - add tools.jar to the gentoo.classpath. Should only be used
1927 #               for build-time purposes, the dependency is not recorded to
1928 #               package.env!
1929 # ANT_TASKS - used to determine ANT_TASKS before calling Ant.
1930 # @CODE
1931 eant() {
1932         debug-print-function ${FUNCNAME} $*
1933
1934         if [[ ${EBUILD_PHASE} = compile ]]; then
1935                 java-ant-2_src_configure
1936         fi
1937
1938         if ! has java-ant-2 ${INHERITED}; then
1939                 local msg="You should inherit java-ant-2 when using eant"
1940                 java-pkg_announce-qa-violation "${msg}"
1941         fi
1942
1943         local antflags="-Dnoget=true -Dmaven.mode.offline=true -Dbuild.sysclasspath=ignore"
1944
1945         java-pkg_init-compiler_
1946         local compiler="${GENTOO_COMPILER}"
1947
1948         local compiler_env="${JAVA_PKG_COMPILER_DIR}/${compiler}"
1949         local build_compiler="$(source ${compiler_env} 1>/dev/null 2>&1; echo ${ANT_BUILD_COMPILER})"
1950         if [[ "${compiler}" != "javac" && -z "${build_compiler}" ]]; then
1951                 die "ANT_BUILD_COMPILER undefined in ${compiler_env}"
1952         fi
1953
1954         if [[ ${compiler} != "javac" ]]; then
1955                 antflags="${antflags} -Dbuild.compiler=${build_compiler}"
1956                 # Figure out any extra stuff to put on the classpath for compilers aside
1957                 # from javac
1958                 # ANT_BUILD_COMPILER_DEPS should be something that could be passed to
1959                 # java-config -p
1960                 local build_compiler_deps="$(source ${JAVA_PKG_COMPILER_DIR}/${compiler} 1>/dev/null 2>&1; echo ${ANT_BUILD_COMPILER_DEPS})"
1961                 if [[ -n ${build_compiler_deps} ]]; then
1962                         antflags="${antflags} -lib $(java-config -p ${build_compiler_deps})"
1963                 fi
1964         fi
1965
1966         for arg in "${@}"; do
1967                 if [[ ${arg} = -lib ]]; then
1968                         if is-java-strict; then
1969                                 eerror "You should not use the -lib argument to eant because it will fail"
1970                                 eerror "with JAVA_PKG_STRICT. Please use for example java-pkg_jar-from"
1971                                 eerror "or ant properties to make dependencies available."
1972                                 eerror "For ant tasks use WANT_ANT_TASKS or ANT_TASKS from."
1973                                 eerror "split ant (>=dev-java/ant-core-1.7)."
1974                                 die "eant -lib is deprecated/forbidden"
1975                         else
1976                                 echo "eant -lib is deprecated. Turn JAVA_PKG_STRICT on for"
1977                                 echo "more info."
1978                         fi
1979                 fi
1980         done
1981
1982         # parse WANT_ANT_TASKS for atoms
1983         local want_ant_tasks
1984         for i in ${WANT_ANT_TASKS}; do
1985                 if [[ ${i} = */*:* ]]; then
1986                         i=${i#*/}
1987                         i=${i%:0}
1988                         want_ant_tasks+="${i/:/-} "
1989                 else
1990                         want_ant_tasks+="${i} "
1991                 fi
1992         done
1993         # default ANT_TASKS to WANT_ANT_TASKS, if ANT_TASKS is not set explicitly
1994         ANT_TASKS="${ANT_TASKS:-${want_ant_tasks% }}"
1995
1996         # override ANT_TASKS with JAVA_PKG_FORCE_ANT_TASKS if it's set
1997         ANT_TASKS="${JAVA_PKG_FORCE_ANT_TASKS:-${ANT_TASKS}}"
1998
1999         # if ant-tasks is not set by ebuild or forced, use none
2000         ANT_TASKS="${ANT_TASKS:-none}"
2001
2002         # at this point, ANT_TASKS should be "all", "none" or explicit list
2003         if [[ "${ANT_TASKS}" == "all" ]]; then
2004                 einfo "Using all available ANT_TASKS"
2005         elif [[ "${ANT_TASKS}" == "none" ]]; then
2006                 einfo "Disabling all optional ANT_TASKS"
2007         else
2008                 einfo "Using following ANT_TASKS: ${ANT_TASKS}"
2009         fi
2010
2011         export ANT_TASKS
2012
2013         [[ -n ${JAVA_PKG_DEBUG} ]] && antflags="${antflags} --execdebug -debug"
2014         [[ -n ${PORTAGE_QUIET} ]] && antflags="${antflags} -q"
2015
2016         local gcp="${EANT_GENTOO_CLASSPATH}"
2017         local getjarsarg=""
2018
2019         if [[ ${EBUILD_PHASE} = "test" ]]; then
2020                 antflags="${antflags} -DJunit.present=true"
2021                 getjarsarg="--with-dependencies"
2022
2023                 local re="\bant-junit4?([-:]\S+)?\b"
2024                 [[ ${ANT_TASKS} =~ ${re} ]] && gcp+=" ${BASH_REMATCH[0]}"
2025         else
2026                 antflags="${antflags} -Dmaven.test.skip=true"
2027         fi
2028
2029         local cp
2030
2031         for atom in ${gcp}; do
2032                 cp+=":$(java-pkg_getjars ${getjarsarg} ${atom})"
2033         done
2034
2035         [[ ${EANT_NEEDS_TOOLS} ]] && cp+=":$(java-config --tools)"
2036         [[ ${EANT_GENTOO_CLASSPATH_EXTRA} ]] && cp+=":${EANT_GENTOO_CLASSPATH_EXTRA}"
2037
2038         if [[ ${cp#:} ]]; then
2039                 # It seems ant does not like single quotes around ${cp}
2040                 antflags="${antflags} -Dgentoo.classpath=\"${cp#:}\""
2041         fi
2042
2043         [[ -n ${JAVA_PKG_DEBUG} ]] && echo ant ${antflags} "${@}"
2044         debug-print "Calling ant (GENTOO_VM: ${GENTOO_VM}): ${antflags} ${@}"
2045         ant ${antflags} "${@}" || die "eant failed"
2046 }
2047
2048 # @FUNCTION: ejavac
2049 # @USAGE: <javac_arguments>
2050 # @DESCRIPTION:
2051 # Javac wrapper function. Will use the appropriate compiler, based on
2052 # /etc/java-config/compilers.conf
2053 ejavac() {
2054         debug-print-function ${FUNCNAME} $*
2055
2056         local compiler_executable
2057         compiler_executable=$(java-pkg_get-javac)
2058
2059         local javac_args
2060         javac_args="$(java-pkg_javac-args)"
2061
2062         if [[ -n ${JAVA_PKG_DEBUG} ]]; then
2063                 einfo "Verbose logging for \"${FUNCNAME}\" function"
2064                 einfo "Compiler executable: ${compiler_executable}"
2065                 einfo "Extra arguments: ${javac_args}"
2066                 einfo "Complete command:"
2067                 einfo "${compiler_executable} ${javac_args} ${@}"
2068         fi
2069
2070         ebegin "Compiling"
2071         ${compiler_executable} ${javac_args} "${@}" || die "ejavac failed"
2072 }
2073
2074 # @FUNCTION: ejavadoc
2075 # @USAGE: <javadoc_arguments>
2076 # @DESCRIPTION:
2077 # javadoc wrapper function. Will set some flags based on the VM version
2078 # due to strict javadoc rules in 1.8.
2079 ejavadoc() {
2080         debug-print-function ${FUNCNAME} $*
2081
2082         local javadoc_args=""
2083
2084         if java-pkg_is-vm-version-ge "1.8" ; then
2085                 javadoc_args="-Xdoclint:none"
2086         fi
2087
2088         if [[ -n ${JAVA_PKG_DEBUG} ]]; then
2089                 einfo "Verbose logging for \"${FUNCNAME}\" function"
2090                 einfo "Javadoc executable: javadoc"
2091                 einfo "Extra arguments: ${javadoc_args}"
2092                 einfo "Complete command:"
2093                 einfo "javadoc ${javadoc_args} ${@}"
2094         fi
2095
2096         ebegin "Generating JavaDoc"
2097         javadoc ${javadoc_args} "${@}" || die "ejavadoc failed"
2098 }
2099
2100 # @FUNCTION: java-pkg_filter-compiler
2101 # @USAGE: <compiler(s)_to_filter>
2102 # @DESCRIPTION:
2103 # Used to prevent the use of some compilers. Should be used in src_compile.
2104 # Basically, it just appends onto JAVA_PKG_FILTER_COMPILER
2105 java-pkg_filter-compiler() {
2106         JAVA_PKG_FILTER_COMPILER="${JAVA_PKG_FILTER_COMPILER} $@"
2107 }
2108
2109 # @FUNCTION: java-pkg_force-compiler
2110 # @USAGE: <compiler(s)_to_force>
2111 # @DESCRIPTION:
2112 # Used to force the use of particular compilers. Should be used in src_compile.
2113 # A common use of this would be to force ecj-3.1 to be used on amd64, to avoid
2114 # OutOfMemoryErrors that may come up.
2115 java-pkg_force-compiler() {
2116         JAVA_PKG_FORCE_COMPILER="$@"
2117 }
2118
2119 # @FUNCTION: use_doc
2120 # @DESCRIPTION:
2121 #
2122 # Helper function for getting ant to build javadocs. If the user has USE=doc,
2123 # then 'javadoc' or the argument are returned. Otherwise, there is no return.
2124 #
2125 # The output of this should be passed to ant.
2126 # @CODE
2127 # Parameters:
2128 # $@ - Option value to return. Defaults to 'javadoc'
2129 #
2130 # Examples:
2131 # build javadocs by calling 'javadoc' target
2132 #       eant $(use_doc)
2133 #
2134 # build javadocs by calling 'apidoc' target
2135 #       eant $(use_doc apidoc)
2136 # @CODE
2137 # @RETURN string - Name of the target to create javadocs
2138 use_doc() {
2139         use doc && echo ${@:-javadoc}
2140 }
2141
2142
2143 # @FUNCTION: java-pkg_init
2144 # @INTERNAL
2145 # @DESCRIPTION:
2146 # The purpose of this function, as the name might imply, is to initialize the
2147 # Java environment. It ensures that that there aren't any environment variables
2148 # that'll muss things up. It initializes some variables, which are used
2149 # internally. And most importantly, it'll switch the VM if necessary.
2150 #
2151 # This shouldn't be used directly. Instead, java-pkg and java-pkg-opt will
2152 # call it during each of the phases of the merge process.
2153 java-pkg_init() {
2154         debug-print-function ${FUNCNAME} $*
2155
2156         # Don't set up build environment if installing from binary. #206024 #258423
2157         [[ "${MERGE_TYPE}" == "binary" ]] && return
2158         # Also try Portage's nonstandard EMERGE_FROM for old EAPIs, if it doesn't
2159         # work nothing is lost.
2160         has ${EAPI:-0} 0 1 2 3 && [[ "${EMERGE_FROM}" == "binary" ]] && return
2161
2162         unset JAVAC
2163         unset JAVA_HOME
2164
2165         java-config --help >/dev/null || {
2166                 eerror ""
2167                 eerror "Can't run java-config --help"
2168                 eerror "Have you upgraded python recently but haven't"
2169                 eerror "run python-updater yet?"
2170                 die "Can't run java-config --help"
2171         }
2172
2173         # People do all kinds of weird things.
2174         # https://forums.gentoo.org/viewtopic-p-3943166.html
2175         local silence="${SILENCE_JAVA_OPTIONS_WARNING}"
2176         local accept="${I_WANT_GLOBAL_JAVA_OPTIONS}"
2177         if [[ -n ${_JAVA_OPTIONS} && -z ${accept} && -z ${silence} ]]; then
2178                 ewarn "_JAVA_OPTIONS changes what java -version outputs at least for"
2179                 ewarn "sun-jdk vms and and as such break configure scripts that"
2180                 ewarn "use it (for example app-office/openoffice) so we filter it out."
2181                 ewarn "Use SILENCE_JAVA_OPTIONS_WARNING=true in the environment (use"
2182                 ewarn "make.conf for example) to silence this warning or"
2183                 ewarn "I_WANT_GLOBAL_JAVA_OPTIONS to not filter it."
2184         fi
2185
2186         if [[ -z ${accept} ]]; then
2187                 # export _JAVA_OPTIONS= doesn't work because it will show up in java
2188                 # -version output
2189                 unset _JAVA_OPTIONS
2190                 # phase hooks make this run many times without this
2191                 I_WANT_GLOBAL_JAVA_OPTIONS="true"
2192         fi
2193
2194         java-pkg_switch-vm
2195         PATH=${JAVA_HOME}/bin:${PATH}
2196
2197         # TODO we will probably want to set JAVAC and JAVACFLAGS
2198
2199         # Do some QA checks
2200         java-pkg_check-jikes
2201
2202         # Can't use unset here because Portage does not save the unset
2203         # see https://bugs.gentoo.org/show_bug.cgi?id=189417#c11
2204
2205         # When users have crazy classpaths some packages can fail to compile.
2206         # and everything should work with empty CLASSPATH.
2207         # This also helps prevent unexpected dependencies on random things
2208         # from the CLASSPATH.
2209         export CLASSPATH=
2210
2211         # Unset external ANT_ stuff
2212         export ANT_TASKS=
2213         export ANT_OPTS=
2214         export ANT_RESPECT_JAVA_HOME=
2215 }
2216
2217 # @FUNCTION: java-pkg-init-compiler_
2218 # @INTERNAL
2219 # @DESCRIPTION:
2220 # This function attempts to figure out what compiler should be used. It does
2221 # this by reading the file at JAVA_PKG_COMPILERS_CONF, and checking the
2222 # COMPILERS variable defined there.
2223 # This can be overridden by a list in JAVA_PKG_FORCE_COMPILER
2224 #
2225 # It will go through the list of compilers, and verify that it supports the
2226 # target and source that are needed. If it is not suitable, then the next
2227 # compiler is checked. When JAVA_PKG_FORCE_COMPILER is defined, this checking
2228 # isn't done.
2229 #
2230 # Once the which compiler to use has been figured out, it is set to
2231 # GENTOO_COMPILER.
2232 #
2233 # If you hadn't guessed, JAVA_PKG_FORCE_COMPILER is for testing only.
2234 #
2235 # If the user doesn't defined anything in JAVA_PKG_COMPILERS_CONF, or no
2236 # suitable compiler was found there, then the default is to use javac provided
2237 # by the current VM.
2238 #
2239 #
2240 # @RETURN name of the compiler to use
2241 java-pkg_init-compiler_() {
2242         debug-print-function ${FUNCNAME} $*
2243
2244         if [[ -n ${GENTOO_COMPILER} ]]; then
2245                 debug-print "GENTOO_COMPILER already set"
2246                 return
2247         fi
2248
2249         local compilers;
2250         if [[ -z ${JAVA_PKG_FORCE_COMPILER} ]]; then
2251                 compilers="$(source ${JAVA_PKG_COMPILERS_CONF} 1>/dev/null 2>&1; echo   ${COMPILERS})"
2252         else
2253                 compilers=${JAVA_PKG_FORCE_COMPILER}
2254         fi
2255
2256         debug-print "Read \"${compilers}\" from ${JAVA_PKG_COMPILERS_CONF}"
2257
2258         # Figure out if we should announce what compiler we're using
2259         local compiler
2260         for compiler in ${compilers}; do
2261                 debug-print "Checking ${compiler}..."
2262                 # javac should always be alright
2263                 if [[ ${compiler} = "javac" ]]; then
2264                         debug-print "Found javac... breaking"
2265                         export GENTOO_COMPILER="javac"
2266                         break
2267                 fi
2268
2269                 if has ${compiler} ${JAVA_PKG_FILTER_COMPILER}; then
2270                         if [[ -z ${JAVA_PKG_FORCE_COMPILER} ]]; then
2271                                 einfo "Filtering ${compiler}" >&2
2272                                 continue
2273                         fi
2274                 fi
2275
2276                 # for non-javac, we need to make sure it supports the right target and
2277                 # source
2278                 local compiler_env="${JAVA_PKG_COMPILER_DIR}/${compiler}"
2279                 if [[ -f ${compiler_env} ]]; then
2280                         local desired_target="$(java-pkg_get-target)"
2281                         local desired_source="$(java-pkg_get-source)"
2282
2283
2284                         # Verify that the compiler supports target
2285                         local supported_target=$(source ${compiler_env} 1>/dev/null 2>&1; echo ${SUPPORTED_TARGET})
2286                         if ! has ${desired_target} ${supported_target}; then
2287                                 ewarn "${compiler} does not support -target ${desired_target},  skipping"
2288                                 continue
2289                         fi
2290
2291                         # Verify that the compiler supports source
2292                         local supported_source=$(source ${compiler_env} 1>/dev/null 2>&1; echo ${SUPPORTED_SOURCE})
2293                         if ! has ${desired_source} ${supported_source}; then
2294                                 ewarn "${compiler} does not support -source ${desired_source}, skipping"
2295                                 continue
2296                         fi
2297
2298                         # if you get here, then the compiler should be good to go
2299                         export GENTOO_COMPILER="${compiler}"
2300                         break
2301                 else
2302                         ewarn "Could not find configuration for ${compiler}, skipping"
2303                         ewarn "Perhaps it is not installed?"
2304                         continue
2305                 fi
2306         done
2307
2308         # If it hasn't been defined already, default to javac
2309         if [[ -z ${GENTOO_COMPILER} ]]; then
2310                 if [[ -n ${compilers} ]]; then
2311                         einfo "No suitable compiler found: defaulting to JDK default for compilation" >&2
2312                 else
2313                         # probably don't need to notify users about the default.
2314                         :;#einfo "Defaulting to javac for compilation" >&2
2315                 fi
2316                 if java-config -g GENTOO_COMPILER 2> /dev/null; then
2317                         export GENTOO_COMPILER=$(java-config -g GENTOO_COMPILER)
2318                 else
2319                         export GENTOO_COMPILER=javac
2320                 fi
2321         else
2322                 einfo "Using ${GENTOO_COMPILER} for compilation" >&2
2323         fi
2324
2325 }
2326
2327 # @FUNCTION: init_paths_
2328 # @INTERNAL
2329 # @DESCRIPTION:
2330 # Initializes some variables that will be used. These variables are mostly used
2331 # to determine where things will eventually get installed.
2332 java-pkg_init_paths_() {
2333         debug-print-function ${FUNCNAME} $*
2334
2335         local pkg_name
2336         if [[ "${SLOT%/*}" == "0" ]] ; then
2337                 JAVA_PKG_NAME="${PN}"
2338         else
2339                 JAVA_PKG_NAME="${PN}-${SLOT%/*}"
2340         fi
2341
2342         JAVA_PKG_SHAREPATH="/usr/share/${JAVA_PKG_NAME}"
2343         JAVA_PKG_SOURCESPATH="${JAVA_PKG_SHAREPATH}/sources/"
2344         JAVA_PKG_ENV="${ED}${JAVA_PKG_SHAREPATH}/package.env"
2345         JAVA_PKG_VIRTUALS_PATH="/usr/share/java-config-2/virtuals"
2346         JAVA_PKG_VIRTUAL_PROVIDER="${ED}${JAVA_PKG_VIRTUALS_PATH}/${JAVA_PKG_NAME}"
2347
2348         [[ -z "${JAVA_PKG_JARDEST}" ]] && JAVA_PKG_JARDEST="${JAVA_PKG_SHAREPATH}/lib"
2349         [[ -z "${JAVA_PKG_LIBDEST}" ]] && JAVA_PKG_LIBDEST="/usr/$(get_libdir)/${JAVA_PKG_NAME}"
2350         [[ -z "${JAVA_PKG_WARDEST}" ]] && JAVA_PKG_WARDEST="${JAVA_PKG_SHAREPATH}/webapps"
2351
2352         # TODO maybe only print once?
2353         debug-print "JAVA_PKG_SHAREPATH: ${JAVA_PKG_SHAREPATH}"
2354         debug-print "JAVA_PKG_ENV: ${JAVA_PKG_ENV}"
2355         debug-print "JAVA_PKG_JARDEST: ${JAVA_PKG_JARDEST}"
2356         debug-print "JAVA_PKG_LIBDEST: ${JAVA_PKG_LIBDEST}"
2357         debug-print "JAVA_PKG_WARDEST: ${JAVA_PKG_WARDEST}"
2358 }
2359
2360 # @FUNCTION: java-pkg_do_write_
2361 # @INTERNAL
2362 # @DESCRIPTION:
2363 # Writes the package.env out to disk.
2364 #
2365 # TODO change to do-write, to match everything else
2366 java-pkg_do_write_() {
2367         debug-print-function ${FUNCNAME} $*
2368         java-pkg_init_paths_
2369         # Create directory for package.env
2370         dodir "${JAVA_PKG_SHAREPATH}"
2371
2372         # Create package.env
2373         (
2374                 echo "DESCRIPTION=\"${DESCRIPTION}\""
2375                 echo "GENERATION=\"2\""
2376                 echo "SLOT=\"${SLOT}\""
2377                 echo "CATEGORY=\"${CATEGORY}\""
2378                 echo "PVR=\"${PVR}\""
2379
2380                 [[ -n "${JAVA_PKG_CLASSPATH}" ]] && echo "CLASSPATH=\"${JAVA_PKG_CLASSPATH}\""
2381                 [[ -n "${JAVA_PKG_LIBRARY}" ]] && echo "LIBRARY_PATH=\"${JAVA_PKG_LIBRARY}\""
2382                 [[ -n "${JAVA_PROVIDE}" ]] && echo "PROVIDES=\"${JAVA_PROVIDE}\""
2383                 [[ -f "${JAVA_PKG_DEPEND_FILE}" ]] \
2384                         && echo "DEPEND=\"$(sort -u "${JAVA_PKG_DEPEND_FILE}" | tr '\n' ':')\""
2385                 [[ -f "${JAVA_PKG_OPTIONAL_DEPEND_FILE}" ]] \
2386                         && echo "OPTIONAL_DEPEND=\"$(sort -u "${JAVA_PKG_OPTIONAL_DEPEND_FILE}" | tr '\n' ':')\""
2387                 echo "VM=\"$(echo ${RDEPEND} ${DEPEND} | sed -e 's/ /\n/g' | sed -n -e '/virtual\/\(jre\|jdk\)/ { p;q }')\"" # TODO cleanup !
2388                 [[ -f "${JAVA_PKG_BUILD_DEPEND_FILE}" ]] \
2389                         && echo "BUILD_DEPEND=\"$(sort -u "${JAVA_PKG_BUILD_DEPEND_FILE}" | tr '\n' ':')\""
2390         ) > "${JAVA_PKG_ENV}"
2391
2392         # register target/source
2393         local target="$(java-pkg_get-target)"
2394         local source="$(java-pkg_get-source)"
2395         [[ -n ${target} ]] && echo "TARGET=\"${target}\"" >> "${JAVA_PKG_ENV}"
2396         [[ -n ${source} ]] && echo "SOURCE=\"${source}\"" >> "${JAVA_PKG_ENV}"
2397
2398         # register javadoc info
2399         [[ -n ${JAVADOC_PATH} ]] && echo "JAVADOC_PATH=\"${JAVADOC_PATH}\"" \
2400                 >> ${JAVA_PKG_ENV}
2401         # register source archives
2402         [[ -n ${JAVA_SOURCES} ]] && echo "JAVA_SOURCES=\"${JAVA_SOURCES}\"" \
2403                 >> ${JAVA_PKG_ENV}
2404
2405         echo "MERGE_VM=\"${GENTOO_VM}\"" >> "${JAVA_PKG_ENV}"
2406         [[ -n ${GENTOO_COMPILER} ]] && echo "MERGE_COMPILER=\"${GENTOO_COMPILER}\"" >> "${JAVA_PKG_ENV}"
2407
2408         # extra env variables
2409         if [[ -n "${JAVA_PKG_EXTRA_ENV_VARS}" ]]; then
2410                 cat "${JAVA_PKG_EXTRA_ENV}" >> "${JAVA_PKG_ENV}" || die
2411                 # nested echo to remove leading/trailing spaces
2412                 echo "ENV_VARS=\"$(echo ${JAVA_PKG_EXTRA_ENV_VARS})\"" \
2413                         >> "${JAVA_PKG_ENV}" || die
2414         fi
2415
2416         # Strip unnecessary leading and trailing colons
2417         # TODO try to cleanup if possible
2418         sed -e "s/=\":/=\"/" -e "s/:\"$/\"/" -i "${JAVA_PKG_ENV}" || die "Did you forget to call java_init ?"
2419 }
2420
2421 # @FUNCTION: java-pkg_record-jar_
2422 # @INTERNAL
2423 # @DESCRIPTION:
2424 # Record an (optional) dependency to the package.env
2425 # @CODE
2426 # Parameters:
2427 # --optional - record dependency as optional
2428 # --build - record dependency as build_only
2429 # $1 - package to record
2430 # $2 - (optional) jar of package to record
2431 # @CODE
2432 JAVA_PKG_DEPEND_FILE="${T}/java-pkg-depend"
2433 JAVA_PKG_OPTIONAL_DEPEND_FILE="${T}/java-pkg-optional-depend"
2434 JAVA_PKG_BUILD_DEPEND_FILE="${T}/java-pkg-build-depend"
2435
2436 java-pkg_record-jar_() {
2437         debug-print-function ${FUNCNAME} $*
2438
2439         local depend_file="${JAVA_PKG_DEPEND_FILE}"
2440         case "${1}" in
2441                 "--optional") depend_file="${JAVA_PKG_OPTIONAL_DEPEND_FILE}"; shift;;
2442                 "--build-only") depend_file="${JAVA_PKG_BUILD_DEPEND_FILE}"; shift;;
2443         esac
2444
2445         local pkg=${1} jar=${2} append
2446         if [[ -z "${jar}" ]]; then
2447                 append="${pkg}"
2448         else
2449                 append="$(basename ${jar})@${pkg}"
2450         fi
2451
2452         echo "${append}" >> "${depend_file}"
2453 }
2454
2455 # @FUNCTION: java-pkg_append_
2456 # @INTERNAL
2457 # @DESCRIPTION:
2458 # Appends a value to a variable
2459 #
2460 # @CODE
2461 # Parameters:
2462 # $1 variable name to modify
2463 # $2 value to append
2464 #
2465 # Examples:
2466 #       java-pkg_append_ CLASSPATH foo.jar
2467 # @CODE
2468 java-pkg_append_() {
2469         debug-print-function ${FUNCNAME} $*
2470
2471         local var="${1}" value="${2}"
2472         if [[ -z "${!var}" ]] ; then
2473                 export ${var}="${value}"
2474         else
2475                 local oldIFS=${IFS} cur haveit
2476                 IFS=':'
2477                 for cur in ${!var}; do
2478                         if [[ ${cur} == ${value} ]]; then
2479                                 haveit="yes"
2480                                 break
2481                         fi
2482                 done
2483                 [[ -z ${haveit} ]] && export ${var}="${!var}:${value}"
2484                 IFS=${oldIFS}
2485         fi
2486 }
2487
2488 # @FUNCTION: java-pkg_expand_dir_
2489 # @INTERNAL
2490 # @DESCRIPTION:
2491 # Gets the full path of the file/directory's parent.
2492 # @CODE
2493 # Parameters:
2494 # $1 - file/directory to find parent directory for
2495 # @CODE
2496 # @RETURN: path to $1's parent directory
2497 java-pkg_expand_dir_() {
2498         pushd "$(dirname "${1}")" >/dev/null 2>&1 || die
2499         pwd
2500         popd >/dev/null 2>&1 || die
2501 }
2502
2503 # @FUNCTION: java-pkg_func-exists
2504 # @INTERNAL
2505 # @DESCRIPTION:
2506 # Does the indicated function exist?
2507 # @RETURN: 0 - function is declared, 1 - function is undeclared
2508 java-pkg_func-exists() {
2509         declare -F ${1} > /dev/null
2510 }
2511
2512 # @FUNCTION: java-pkg_setup-vm
2513 # @INTERNAL
2514 # @DESCRIPTION:
2515 # Sets up the environment for a specific VM
2516 java-pkg_setup-vm() {
2517         debug-print-function ${FUNCNAME} $*
2518
2519         local vendor="$(java-pkg_get-vm-vendor)"
2520         if [[ "${vendor}" == "sun" ]] && java-pkg_is-vm-version-ge "1.5" ; then
2521                 addpredict "/dev/random"
2522         elif [[ "${vendor}" == "ibm" ]]; then
2523                 addpredict "/proc/self/maps"
2524                 addpredict "/proc/cpuinfo"
2525                 addpredict "/proc/self/coredump_filter"
2526         elif [[ "${vendor}" == "oracle" ]]; then
2527                 addpredict "/dev/random"
2528                 addpredict "/proc/self/coredump_filter"
2529         elif [[ "${vendor}" == icedtea* ]] && java-pkg_is-vm-version-ge "1.7" ; then
2530                 addpredict "/dev/random"
2531                 addpredict "/proc/self/coredump_filter"
2532         elif [[ "${vendor}" == "jrockit" ]]; then
2533                 addpredict "/proc/cpuinfo"
2534         fi
2535 }
2536
2537 # @FUNCTION: java-pkg_needs-vm
2538 # @INTERNAL
2539 # @DESCRIPTION:
2540 # Does the current package depend on virtual/jdk or does it set
2541 # JAVA_PKG_WANT_BUILD_VM?
2542 #
2543 # @RETURN: 0 - Package depends on virtual/jdk; 1 - Package does not depend on virtual/jdk
2544 java-pkg_needs-vm() {
2545         debug-print-function ${FUNCNAME} $*
2546
2547         if [[ -n "$(echo ${JAVA_PKG_NV_DEPEND:-${DEPEND}} | sed -e '\:virtual/jdk:!d')" ]]; then
2548                 return 0
2549         fi
2550
2551         [[ -n "${JAVA_PKG_WANT_BUILD_VM}" ]] && return 0
2552
2553         return 1
2554 }
2555
2556 # @FUNCTION: java-pkg_get-current-vm
2557 # @INTERNAL
2558 # @RETURN - The current VM being used
2559 java-pkg_get-current-vm() {
2560         java-config -f
2561 }
2562
2563 # @FUNCTION: java-pkg_get-vm-vendor
2564 # @INTERNAL
2565 # @RETURN - The vendor of the current VM
2566 java-pkg_get-vm-vendor() {
2567         debug-print-function ${FUNCNAME} $*
2568
2569         local vm="$(java-pkg_get-current-vm)"
2570         vm="${vm/-*/}"
2571         echo "${vm}"
2572 }
2573
2574 # @FUNCTION: java-pkg_get-vm-version
2575 # @INTERNAL
2576 # @RETURN - The version of the current VM
2577 java-pkg_get-vm-version() {
2578         debug-print-function ${FUNCNAME} $*
2579
2580         java-config -g PROVIDES_VERSION
2581 }
2582
2583 # @FUNCTION: java-pkg_build-vm-from-handle
2584 # @INTERNAL
2585 # @DESCRIPTION:
2586 # Selects a build vm from a list of vm handles. First checks for the system-vm
2587 # beeing usable, then steps through the listed handles till a suitable vm is
2588 # found.
2589 #
2590 # @RETURN - VM handle of an available JDK
2591 java-pkg_build-vm-from-handle() {
2592         debug-print-function ${FUNCNAME} "$*"
2593
2594         local vm
2595         vm=$(java-pkg_get-current-vm 2>/dev/null)
2596         if [[ $? -eq 0 ]]; then
2597                 if has ${vm} ${JAVA_PKG_WANT_BUILD_VM}; then
2598                         echo ${vm}
2599                         return 0
2600                 fi
2601         fi
2602
2603         for vm in ${JAVA_PKG_WANT_BUILD_VM}; do
2604                 if java-config-2 --select-vm=${vm} 2>/dev/null; then
2605                         echo ${vm}
2606                         return 0
2607                 fi
2608         done
2609
2610         eerror "${FUNCNAME}: No vm found for handles: ${JAVA_PKG_WANT_BUILD_VM}"
2611         return 1
2612 }
2613
2614 # @FUNCTION: java-pkg_switch-vm
2615 # @INTERNAL
2616 # @DESCRIPTION:
2617 # Switch VM if we're allowed to (controlled by JAVA_PKG_ALLOW_VM_CHANGE), and
2618 # verify that the current VM is sufficient.
2619 # Setup the environment for the VM being used.
2620 java-pkg_switch-vm() {
2621         debug-print-function ${FUNCNAME} $*
2622
2623         if java-pkg_needs-vm; then
2624                 # Use the VM specified by JAVA_PKG_FORCE_VM
2625                 if [[ -n "${JAVA_PKG_FORCE_VM}" ]]; then
2626                         # If you're forcing the VM, I hope you know what your doing...
2627                         debug-print "JAVA_PKG_FORCE_VM used: ${JAVA_PKG_FORCE_VM}"
2628                         export GENTOO_VM="${JAVA_PKG_FORCE_VM}"
2629                 # if we're allowed to switch the vm...
2630                 elif [[ "${JAVA_PKG_ALLOW_VM_CHANGE}" == "yes" ]]; then
2631                         # if there is an explicit list of handles to choose from
2632                         if [[ -n "${JAVA_PKG_WANT_BUILD_VM}" ]]; then
2633                                 debug-print "JAVA_PKG_WANT_BUILD_VM used: ${JAVA_PKG_WANT_BUILD_VM}"
2634                                 GENTOO_VM=$(java-pkg_build-vm-from-handle)
2635                                 if [[ $? != 0 ]]; then
2636                                         eerror "${FUNCNAME}: No VM found for handles: ${JAVA_PKG_WANT_BUILD_VM}"
2637                                         die "${FUNCNAME}: Failed to determine VM for building"
2638                                 fi
2639                                 # JAVA_PKG_WANT_SOURCE and JAVA_PKG_WANT_TARGET are required as
2640                                 # they can't be deduced from handles.
2641                                 if [[ -z "${JAVA_PKG_WANT_SOURCE}" ]]; then
2642                                         eerror "JAVA_PKG_WANT_BUILD_VM specified but not JAVA_PKG_WANT_SOURCE"
2643                                         die "Specify JAVA_PKG_WANT_SOURCE"
2644                                 fi
2645                                 if [[ -z "${JAVA_PKG_WANT_TARGET}" ]]; then
2646                                         eerror "JAVA_PKG_WANT_BUILD_VM specified but not JAVA_PKG_WANT_TARGET"
2647                                         die "Specify JAVA_PKG_WANT_TARGET"
2648                                 fi
2649                         # otherwise determine a vm from dep string
2650                         else
2651                                 debug-print "depend-java-query:  NV_DEPEND:     ${JAVA_PKG_NV_DEPEND:-${DEPEND}}"
2652                                 GENTOO_VM="$(depend-java-query --get-vm "${JAVA_PKG_NV_DEPEND:-${DEPEND}}")"
2653                                 if [[ -z "${GENTOO_VM}" || "${GENTOO_VM}" == "None" ]]; then
2654                                         eerror "Unable to determine VM for building from dependencies:"
2655                                         echo "NV_DEPEND: ${JAVA_PKG_NV_DEPEND:-${DEPEND}}"
2656                                         die "Failed to determine VM for building."
2657                                 fi
2658                         fi
2659                         export GENTOO_VM
2660                 # otherwise just make sure the current VM is sufficient
2661                 else
2662                         java-pkg_ensure-vm-version-sufficient
2663                 fi
2664                 debug-print "Using: $(java-config -f)"
2665
2666                 java-pkg_setup-vm
2667
2668                 export JAVA=$(java-config --java)
2669                 export JAVAC=$(java-config --javac)
2670                 JAVACFLAGS="$(java-pkg_javac-args)"
2671                 [[ -n ${JAVACFLAGS_EXTRA} ]] && JAVACFLAGS="${JAVACFLAGS_EXTRA} ${JAVACFLAGS}"
2672                 export JAVACFLAGS
2673
2674                 export JAVA_HOME="$(java-config -g JAVA_HOME)"
2675                 export JDK_HOME=${JAVA_HOME}
2676
2677                 #TODO If you know a better solution let us know.
2678                 java-pkg_append_ LD_LIBRARY_PATH "$(java-config -g LDPATH)"
2679
2680                 local tann="${T}/announced-vm"
2681                 # With the hooks we should only get here once from pkg_setup but better safe than sorry
2682                 # if people have for example modified eclasses some where
2683                 if [[ -n "${JAVA_PKG_DEBUG}" ]] || [[ ! -f "${tann}" ]] ; then
2684                         einfo "Using: $(java-config -f)"
2685                         [[ ! -f "${tann}" ]] && touch "${tann}"
2686                 fi
2687
2688         else
2689                 [[ -n "${JAVA_PKG_DEBUG}" ]] && ewarn "!!! This package inherits java-pkg but doesn't depend on a JDK. -bin or broken dependency!!!"
2690         fi
2691 }
2692
2693 # @FUNCTION: java-pkg_die
2694 # @INTERNAL
2695 # @DESCRIPTION:
2696 # Enhanced die for Java packages, which displays some information that may be
2697 # useful for debugging bugs on bugzilla.
2698 #register_die_hook java-pkg_die
2699 if ! has java-pkg_die ${EBUILD_DEATH_HOOKS}; then
2700         EBUILD_DEATH_HOOKS="${EBUILD_DEATH_HOOKS} java-pkg_die"
2701 fi
2702
2703 java-pkg_die() {
2704         echo "!!! When you file a bug report, please include the following information:" >&2
2705         echo "GENTOO_VM=${GENTOO_VM}  CLASSPATH=\"${CLASSPATH}\" JAVA_HOME=\"${JAVA_HOME}\"" >&2
2706         echo "JAVACFLAGS=\"${JAVACFLAGS}\" COMPILER=\"${GENTOO_COMPILER}\"" >&2
2707         echo "and of course, the output of emerge --info =${P}" >&2
2708 }
2709
2710
2711 # TODO document
2712 # List jars in the source directory, ${S}
2713 java-pkg_jar-list() {
2714         if [[ -n "${JAVA_PKG_DEBUG}" ]]; then
2715                 einfo "Linked Jars"
2716                 find "${S}" -type l -name '*.jar' -print0 | xargs -0 -r -n 500 ls -ald | sed -e "s,${WORKDIR},\${WORKDIR},"
2717                 einfo "Jars"
2718                 find "${S}" -type f -name '*.jar' -print0 | xargs -0 -r -n 500 ls -ald | sed -e "s,${WORKDIR},\${WORKDIR},"
2719                 einfo "Classes"
2720                 find "${S}" -type f -name '*.class' -print0 | xargs -0 -r -n 500 ls -ald | sed -e "s,${WORKDIR},\${WORKDIR},"
2721         fi
2722 }
2723
2724 # @FUNCTION: java-pkg_verify-classes
2725 # @INTERNAL
2726 # @DESCRIPTION:
2727 # Verify that the classes were compiled for the right source / target. Dies if
2728 # not.
2729 # @CODE
2730 # $1 (optional) - the file to check, otherwise checks whole ${D}
2731 # @CODE
2732 java-pkg_verify-classes() {
2733         #$(find ${D} -type f -name '*.jar' -o -name '*.class')
2734
2735         local version_verify="/usr/bin/class-version-verify.py"
2736
2737         if [[ ! -x "${version_verify}" ]]; then
2738                 version_verify="/usr/$(get_libdir)/javatoolkit/bin/class-version-verify.py"
2739         fi
2740
2741         if [[ ! -x "${version_verify}" ]]; then
2742                 ewarn "Unable to perform class version checks as"
2743                 ewarn "class-version-verify.py is unavailable"
2744                 ewarn "Please install dev-java/javatoolkit."
2745                 return
2746         fi
2747
2748         local target=$(java-pkg_get-target)
2749         local result
2750         local log="${T}/class-version-verify.log"
2751         if [[ -n "${1}" ]]; then
2752                 ${version_verify} -v -t ${target} "${1}" > "${log}"
2753                 result=$?
2754         else
2755                 ebegin "Verifying java class versions (target: ${target})"
2756                 ${version_verify} -v -t ${target} -r "${D}" > "${log}"
2757                 result=$?
2758                 eend ${result}
2759         fi
2760         [[ -n ${JAVA_PKG_DEBUG} ]] && cat "${log}"
2761         if [[ ${result} != 0 ]]; then
2762                 eerror "Incorrect bytecode version found"
2763                 [[ -n "${1}" ]] && eerror "in file: ${1}"
2764                 eerror "See ${log} for more details."
2765                 die "Incorrect bytecode found"
2766         fi
2767 }
2768
2769 # @FUNCTION: java-pkg_ensure-dep
2770 # @INTERNAL
2771 # @DESCRIPTION:
2772 # Check that a package being used in jarfrom, getjars and getjar is contained
2773 # within DEPEND or RDEPEND with the correct SLOT. See this mail for details:
2774 # https://archives.gentoo.org/gentoo-dev/message/dcb644f89520f4bbb61cc7bbe45fdf6e
2775 # @CODE
2776 # Parameters:
2777 # $1 - empty - check both vars; "runtime" or "build" - check only
2778 #       RDEPEND, resp. DEPEND
2779 # $2 - Package name and slot.
2780 # @CODE
2781 java-pkg_ensure-dep() {
2782         debug-print-function ${FUNCNAME} $*
2783
2784         local limit_to="${1}"
2785         local target_pkg="${2}"
2786         local dev_error=""
2787
2788         # Transform into a regular expression to look for a matching package
2789         # and SLOT. SLOTs don't have to be numeric so foo-bar could either
2790         # mean foo-bar:0 or foo:bar. So you want to get your head around the
2791         # line below?
2792         #
2793         # * The target package first has any dots escaped, e.g. foo-1.2
2794         #   becomes foo-1\.2.
2795         #
2796         # * sed then looks at the component following the last - or :
2797         #   character, or the whole string if there is no - or :
2798         #   character. It uses this to build a new regexp with two
2799         #   significant branches.
2800         #
2801         # * The first checks for the whole target package string, optionally
2802         #   followed by a version number, and then :0.
2803         #
2804         # * The second checks for the first part of the target package
2805         #   string, optionally followed by a version number, followed by the
2806         #   aforementioned component, treating that as a SLOT.
2807         #
2808         local stripped_pkg=/$(sed -r 's/[-:]?([^-:]+)$/(\0(-[^:]+)?:0|(-[^:]+)?:\1)/' <<< "${target_pkg//./\\.}")\\b
2809
2810         debug-print "Matching against: ${stripped_pkg}"
2811
2812         # Uncomment the lines below once we've dealt with more of these
2813         # otherwise we'll be tempted to turn JAVA_PKG_STRICT off while
2814         # getting hit with a wave of bug reports. :(
2815
2816         if [[ ${limit_to} != runtime && ! ( "${DEPEND}" =~ $stripped_pkg ) ]]; then
2817                 dev_error="The ebuild is attempting to use ${target_pkg}, which is not "
2818                 dev_error+="declared with a SLOT in DEPEND."
2819 #               if is-java-strict; then
2820 #                       die "${dev_error}"
2821 #               else
2822                         eqawarn "java-pkg_ensure-dep: ${dev_error}"
2823 #                       eerror "Because you have ${target_pkg} installed,"
2824 #                       eerror "the package will build without problems, but please"
2825 #                       eerror "report this to https://bugs.gentoo.org."
2826 #               fi
2827         elif [[ ${limit_to} != build && ! ( "${RDEPEND}${PDEPEND}" =~ ${stripped_pkg} ) ]]; then
2828                 dev_error="The ebuild is attempting to use ${target_pkg}, which is not "
2829                 dev_error+="declared with a SLOT in [RP]DEPEND and --build-only wasn't given."
2830 #               if is-java-strict; then
2831 #                       die "${dev_error}"
2832 #               else
2833                         eqawarn "java-pkg_ensure-dep: ${dev_error}"
2834 #                       eerror "The package will build without problems, but may fail to run"
2835 #                       eerror "if you don't have ${target_pkg} installed,"
2836 #                       eerror "so please report this to https://bugs.gentoo.org."
2837 #               fi
2838         fi
2839 }
2840
2841 java-pkg_check-phase() {
2842         local phase=${1}
2843         local funcname=${FUNCNAME[1]}
2844         if [[ ${EBUILD_PHASE} != ${phase} ]]; then
2845                 local msg="${funcname} used outside of src_${phase}"
2846                 java-pkg_announce-qa-violation "${msg}"
2847         fi
2848 }
2849
2850 java-pkg_check-versioned-jar() {
2851         local jar=${1}
2852
2853         if [[ ${jar} =~ ${PV} ]]; then
2854                 java-pkg_announce-qa-violation "installing versioned jar '${jar}'"
2855         fi
2856 }
2857
2858 java-pkg_check-jikes() {
2859         if has jikes ${IUSE}; then
2860                 java-pkg_announce-qa-violation "deprecated USE flag 'jikes' in IUSE"
2861         fi
2862 }
2863
2864 java-pkg_announce-qa-violation() {
2865         local nodie
2866         if [[ ${1} == "--nodie" ]]; then
2867                 nodie="true"
2868                 shift
2869         fi
2870         echo "Java QA Notice: $@" >&2
2871         increment-qa-violations
2872         [[ -z "${nodie}" ]] && is-java-strict && die "${@}"
2873 }
2874
2875 increment-qa-violations() {
2876         let "JAVA_PKG_QA_VIOLATIONS+=1"
2877         export JAVA_PKG_QA_VIOLATIONS
2878 }
2879
2880 is-java-strict() {
2881         [[ -n ${JAVA_PKG_STRICT} ]]
2882         return $?
2883 }
2884
2885 # @FUNCTION: java-pkg_clean
2886 # @DESCRIPTION:
2887 # Java package cleaner function. This will remove all *.class and *.jar
2888 # files, removing any bundled dependencies.
2889 java-pkg_clean() {
2890         if [[ -z "${JAVA_PKG_NO_CLEAN}" ]]; then
2891                 find "${@}" '(' -name '*.class' -o -name '*.jar' ')' -type f -delete -print || die
2892         fi
2893 }
2894
2895 # @FUNCTION: java-pkg_gen-cp
2896 # @INTERNAL
2897 # @DESCRIPTION:
2898 # Java package generate classpath will create a classpath based on
2899 # special variable CP_DEPEND in the ebuild.
2900 #
2901 # @CODE
2902 # Parameters:
2903 # $1 - classpath variable either EANT_GENTOO_CLASSPATH or JAVA_GENTOO_CLASSPATH
2904 # @CODE
2905 java-pkg_gen-cp() {
2906         debug-print-function ${FUNCNAME} "${@}"
2907
2908         local atom
2909         for atom in ${CP_DEPEND}; do
2910                 if [[ ${atom} =~ /(([[:alnum:]+_-]+)-[0-9]+(\.[0-9]+)*[a-z]?(_[[:alnum:]]+)*(-r[0-9]*)?|[[:alnum:]+_-]+):([[:alnum:]+_.-]+) ]]; then
2911                         atom=${BASH_REMATCH[2]:-${BASH_REMATCH[1]}}
2912                         [[ ${BASH_REMATCH[6]} != 0 ]] && atom+=-${BASH_REMATCH[6]}
2913                         local regex="(^|\s|,)${atom}($|\s|,)"
2914                         [[ ${!1} =~ ${regex} ]] || declare -g ${1}+=${!1:+,}${atom}
2915                 else
2916                         die "Invalid CP_DEPEND atom ${atom}, ensure a SLOT is included"
2917                 fi
2918         done
2919 }