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