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