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