/[gentoo-x86]/eclass/java-utils-2.eclass
Gentoo

Contents of /eclass/java-utils-2.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.8 - (show annotations) (download)
Mon Jul 31 03:32:53 2006 UTC (8 years, 5 months ago) by nichoj
Branch: MAIN
Changes since 1.7: +13 -0 lines
Added java-pkg_force-compiler

1 # Base eclass for Java packages
2 #
3 # Copyright (c) 2004-2005, Thomas Matthijs <axxo@gentoo.org>
4 # Copyright (c) 2004, Karl Trygve Kalleberg <karltk@gentoo.org>
5 # Copyright (c) 2004-2005, Gentoo Foundation
6 #
7 # Licensed under the GNU General Public License, v2
8 #
9
10
11 # -----------------------------------------------------------------------------
12 # @eclass-begin
13 # @eclass-shortdesc Java Utility eclass
14 # @eclass-maintainer java@gentoo.org
15 #
16 # This eclass provides functionality which is used by
17 # java-pkg.eclass and java-pkg-opt.eclass as well as from ebuilds.
18 #
19 # @warning
20 # You probably don't want to inherit this directly from an ebuild. Instead,
21 # you should inherit java-ant for Ant-based Java packages, java-pkg for other
22 # Java packages, or java-pkg-opt for packages that have optional Java support.
23 #
24 # -----------------------------------------------------------------------------
25
26 inherit eutils versionator multilib
27
28 # -----------------------------------------------------------------------------
29 # @section-begin variables
30 # @section-title Variables
31 #
32 # Summary of variables which control the behavior of building Java packges.
33 # -----------------------------------------------------------------------------
34
35 # Make sure we use java-config-2
36 export WANT_JAVA_CONFIG="2"
37
38 # TODO document
39 JAVA_PKG_PORTAGE_DEP=">=sys-apps/portage-2.1_pre1"
40
41 # -----------------------------------------------------------------------------
42 # @variable-internal JAVA_PKG_E_DEPEND
43 #
44 # This is a convience variable to be used from the other java eclasses. This is
45 # the version of java-config we want to use. We also need a recent version
46 # portage, that includes phase hooks.
47 # -----------------------------------------------------------------------------
48 JAVA_PKG_E_DEPEND=">=dev-java/java-config-2.0.19-r1 ${JAVA_PKG_PORTAGE_DEP}"
49
50 # -----------------------------------------------------------------------------
51 # @variable-external JAVA_PKG_ALLOW_VM_CHANGE
52 # @variable-default yes
53 #
54 # Allow this eclass to change the active VM?
55 # If your system VM isn't sufficient for the package, the build will fail.
56 # @note This is useful for testing specific VMs.
57 # -----------------------------------------------------------------------------
58 JAVA_PKG_ALLOW_VM_CHANGE=${JAVA_PKG_ALLOW_VM_CHANGE:="yes"}
59
60 # -----------------------------------------------------------------------------
61 # @variable-external JAVA_PKG_FORCE_VM
62 #
63 # Explicitly set a particular VM to use. If its not valid, it'll fall back to
64 # whatever /etc/java-config-2/build/jdk.conf would elect to use.
65 #
66 # Should only be used for testing and debugging.
67 #
68 # @example Use sun-jdk-1.5 to emerge foo
69 # JAVA_PKG_FORCE_VM=sun-jdk-1.5 emerge foo
70 #
71 # -----------------------------------------------------------------------------
72
73 # -----------------------------------------------------------------------------
74 # @variable-external JAVA_PKG_WANT_SOURCE
75 #
76 # Specify a specific VM version to compile for to use for -source.
77 # Normally this is determined from DEPEND.
78 # See java-pkg_get-source function below.
79 #
80 # Should only be used for testing and debugging.
81 #
82 # @seealso java-pkg_get-source
83 #
84 # @example Use 1.4 source to emerge baz
85 # JAVA_PKG_WANT_SOURCE=1.4 emerge baz
86 # -----------------------------------------------------------------------------
87
88 # -----------------------------------------------------------------------------
89 # @variable-external JAVA_PKG_WANT_TARGET
90 #
91 # Same as JAVA_PKG_WANT_SOURCE above but for -target.
92 # See java-pkg_get-target function below.
93 #
94 # Should only be used for testing and debugging.
95 #
96 # @seealso java-pkg_get-target
97 #
98 # @example emerge bar to be compatible with 1.3
99 # JAVA_PKG_WANT_TARGET=1.3 emerge bar
100 # -----------------------------------------------------------------------------
101
102 # -----------------------------------------------------------------------------
103 # @variable-internal JAVA_PKG_COMPILER_DIR
104 # @default /usr/share/java-config-2/compiler
105 #
106 # Directory where compiler settings are saved, without trailing slash.
107 # Probably shouldn't touch this variable.
108 # -----------------------------------------------------------------------------
109 JAVA_PKG_COMPILER_DIR=${JAVA_PKG_COMPILER_DIR:="/usr/share/java-config-2/compiler"}
110
111
112 # -----------------------------------------------------------------------------
113 # @variable-internal JAVA_PKG_COMPILERS_CONF
114 # @variable-default /etc/java-config-2/build/compilers.conf
115 #
116 # Path to file containing information about which compiler to use.
117 # Can be overloaded, but it should be overloaded for testing.
118 # -----------------------------------------------------------------------------
119 JAVA_PKG_COMPILERS_CONF=${JAVA_PKG_COMPILERS_CONF:="/etc/java-config-2/build/compilers.conf"}
120
121 # -----------------------------------------------------------------------------
122 # @variable-external JAVA_PKG_FORCE_COMPILER
123 #
124 # Explicitly set a list of compilers to use. This is normally read from
125 # JAVA_PKG_COMPILERS_CONF.
126 #
127 # @note This should only be used internally or for testing.
128 # @example Use jikes and javac, in that order
129 # JAVA_PKG_FORCE_COMPILER="jikes javac"
130 # -----------------------------------------------------------------------------
131
132 # TODO document me
133 JAVA_PKG_QA_VIOLATIONS=0
134
135 # -----------------------------------------------------------------------------
136 # @section-end variables
137 # -----------------------------------------------------------------------------
138
139
140 # -----------------------------------------------------------------------------
141 # @section-begin install
142 # @section-summary Install functions
143 #
144 # These are used to install Java-related things, such as jars, Javadocs, JNI
145 # libraries, etc.
146 # -----------------------------------------------------------------------------
147
148
149 # -----------------------------------------------------------------------------
150 # @ebuild-function java-pkg_dojar
151 #
152 # Installs any number of jars.
153 # Jar's will be installed into /usr/share/${PN}(-${SLOT})/lib/ by default.
154 # You can use java-pkg_jarinto to change this path.
155 # You should never install a jar with a package version in the filename.
156 # Instead, use java-pkg_newjar defined below.
157 #
158 # @example
159 # java-pkg_dojar dist/${PN}.jar dist/${PN}-core.jar
160 #
161 # @param $* - list of jars to install
162 # ------------------------------------------------------------------------------
163 java-pkg_dojar() {
164 debug-print-function ${FUNCNAME} $*
165
166 [[ ${#} -lt 1 ]] && die "At least one argument needed"
167
168 java-pkg_check-phase install
169 java-pkg_init_paths_
170
171 # Create JARDEST if it doesn't exist
172 dodir ${JAVA_PKG_JARDEST}
173
174 local jar
175 # for each jar
176 for jar in "$@"; do
177 local jar_basename=$(basename "${jar}")
178
179 java-pkg_check-versioned-jar ${jar_basename}
180
181 # check if it exists
182 if [[ -e "${jar}" ]] ; then
183 # install it into JARDEST if it's a non-symlink
184 if [[ ! -L "${jar}" ]] ; then
185 INSDESTTREE="${JAVA_PKG_JARDEST}" \
186 doins "${jar}" || die "failed to install ${jar}"
187 java-pkg_append_ JAVA_PKG_CLASSPATH "${JAVA_PKG_JARDEST}/${jar_basename}"
188 debug-print "installed ${jar} to ${D}${JAVA_PKG_JARDEST}"
189 # make a symlink to the original jar if it's symlink
190 else
191 # TODO use dosym, once we find something that could use it
192 # -nichoj
193 ln -s "$(readlink "${jar}")" "${D}${JAVA_PKG_JARDEST}/${jar_basename}"
194 debug-print "${jar} is a symlink, linking accordingly"
195 fi
196 else
197 die "${jar} does not exist"
198 fi
199 done
200
201 java-pkg_do_write_
202 }
203
204
205
206 # ------------------------------------------------------------------------------
207 # @ebuild-function java-pkg_regjar
208 #
209 # Records an already installed jar in the package.env
210 # This would mostly be used if the package has make or a custom script to
211 # install things.
212 #
213 # Example:
214 # java-pkg_regjar ${D}/opt/foo/lib/foo.jar
215 #
216 # WARNING:
217 # if you want to use shell expansion, you have to use ${D}/... as the for in
218 # this function will not be able to expand the path, here's an example:
219 #
220 # java-pkg_regjar /opt/my-java/lib/*.jar
221 #
222 # will not work, because:
223 # * the `for jar in "$@"` can't expand the path to jar file names, as they
224 # don't exist yet
225 # * all `if ...` inside for will fail - the file '/opt/my-java/lib/*.jar'
226 # doesn't exist
227 #
228 # you have to use it as:
229 #
230 # java-pkg_regjar ${D}/opt/my-java/lib/*.jar
231 #
232 # @param $@ - jars to record
233 # ------------------------------------------------------------------------------
234 # TODO fix me!
235 java-pkg_regjar() {
236 debug-print-function ${FUNCNAME} $*
237
238 java-pkg_check-phase install
239
240 [[ ${#} -lt 1 ]] && die "at least one argument needed"
241
242 java-pkg_init_paths_
243
244 local jar jar_dir jar_file
245 for jar in "$@"; do
246 # TODO use java-pkg_check-versioned-jar
247 if [[ -e "${jar}" ]]; then
248 # nelchael: we should strip ${D} in this case too, here's why:
249 # imagine such call:
250 # java-pkg_regjar ${D}/opt/java/*.jar
251 # such call will fall into this case (-e ${jar}) and will
252 # record paths with ${D} in package.env
253 java-pkg_append_ JAVA_PKG_CLASSPATH "${jar#${D}}"
254 elif [[ -e "${D}${jar}" ]]; then
255 java-pkg_append_ JAVA_PKG_CLASSPATH "${jar#${D}}"
256 else
257 die "${jar} does not exist"
258 fi
259 done
260
261 java-pkg_do_write_
262 }
263
264
265 # ------------------------------------------------------------------------------
266 # @ebuild-function java-pkg_newjar
267 #
268 # Installs a jar with a new name
269 #
270 # @example: install a versioned jar without the version
271 # java-pkg_newjar dist/${P}.jar ${PN}.jar
272 #
273 # @param $1 - jar to install
274 # @param $2 - new name for jar - defaults to ${PN}.jar if not specified
275 # ------------------------------------------------------------------------------
276 java-pkg_newjar() {
277 debug-print-function ${FUNCNAME} $*
278
279 local original_jar="${1}"
280 local new_jar="${2:-${PN}.jar}"
281 local new_jar_dest="${T}/${new_jar}"
282
283 [[ -z ${original_jar} ]] && die "Must specify a jar to install"
284 [[ ! -f ${original_jar} ]] && die "${original_jar} does not exist!"
285
286 rm -f "${new_jar_dest}" || die "Failed to remove ${new_jar_dest}"
287 cp "${original_jar}" "${new_jar_dest}" \
288 || die "Failed to copy ${original_jar} to ${new_jar_dest}"
289 java-pkg_dojar "${new_jar_dest}"
290 }
291
292
293 # ------------------------------------------------------------------------------
294 # @ebuild-function java-pkg_addcp
295 #
296 # Add something to the package's classpath. For jars, you should use dojar,
297 # newjar, or regjar. This is typically used to add directories to the classpath.
298 #
299 # TODO add example
300 # @param $@ - value to append to JAVA_PKG_CLASSPATH
301 # ------------------------------------------------------------------------------
302 java-pkg_addcp() {
303 java-pkg_append_ JAVA_PKG_CLASSPATH "${@}"
304 java-pkg_do_write_
305 }
306
307
308 # ------------------------------------------------------------------------------
309 # @ebuild-function java-pkg_doso
310 #
311 # Installs any number of JNI libraries
312 # They will be installed into /usr/lib by default, but java-pkg_sointo
313 # can be used change this path
314 #
315 # Example:
316 # java-pkg_doso *.so
317 #
318 # @param $@ - JNI libraries to install
319 # ------------------------------------------------------------------------------
320 java-pkg_doso() {
321 debug-print-function ${FUNCNAME} $*
322
323 [[ ${#} -lt 1 ]] && "At least one argument required for ${FUNCNAME}"
324 java-pkg_check-phase install
325
326 [[ ${#} -lt 1 ]] && die "At least one argument required for ${FUNCNAME}"
327
328 java-pkg_init_paths_
329
330 local lib
331 # for each lib
332 for lib in "$@" ; do
333 # if the lib exists...
334 if [[ -e "${lib}" ]] ; then
335 # install if it isn't a symlink
336 if [[ ! -L "${lib}" ]] ; then
337 INSDESTTREE="${JAVA_PKG_LIBDEST}" \
338 INSOPTIONS="${LIBOPTIONS}" \
339 doins "${lib}" || "failed to install ${lib}"
340 java-pkg_append_ JAVA_PKG_LIBRARY "${JAVA_PKG_LIBDEST}"
341 debug-print "Installing ${lib} to ${JAVA_PKG_LIBDEST}"
342 # otherwise make a symlink to the symlink's origin
343 else
344 # TODO use dosym
345 ln -s "$(readlink "${lib}")" "${D}${JAVA_PKG_LIBDEST}/$(basename "${lib}")"
346 debug-print "${lib} is a symlink, linking accordanly"
347 fi
348 # otherwise die
349 else
350 die "${lib} does not exist"
351 fi
352 done
353
354 java-pkg_do_write_
355 }
356
357 # ------------------------------------------------------------------------------
358 # @ebuild-function java-pkg_regso
359 #
360 # Registers an already JNI library in package.env.
361 #
362 # Example:
363 # java-pkg_regso *.so /path/*.so
364 #
365 # @param $@ - JNI libraries to register
366 # ------------------------------------------------------------------------------
367 java-pkg_regso() {
368 debug-print-function ${FUNCNAME} $*
369
370 java-pkg_check-phase install
371
372 [[ ${#} -lt 1 ]] && "at least one argument needed"
373
374 java-pkg_init_paths_
375
376 local lib target_dir
377 for lib in "$@" ; do
378 # Check the absolute path of the lib
379 if [[ -e "${lib}" ]] ; then
380 target_dir="$(java-pkg_expand_dir_ ${lib})"
381 java-pkg_append_ JAVA_PKG_LIBRARY "/${target_dir#${D}}"
382 # Check the path of the lib relative to ${D}
383 elif [[ -e "${D}${lib}" ]]; then
384 target_dir="$(java-pkg_expand_dir_ ${D}${lib})"
385 java-pkg_append_ JAVA_PKG_LIBRARY "${target_dir}"
386 else
387 die "${lib} does not exist"
388 fi
389 done
390
391 java-pkg_do_write_
392 }
393
394 # ------------------------------------------------------------------------------
395 # @ebuild-function java-pkg_jarinto
396 #
397 # Changes the path jars are installed into
398 #
399 # @param $1 - new location to install jars into.
400 # -----------------------------------------------------------------------------
401 java-pkg_jarinto() {
402 debug-print-function ${FUNCNAME} $*
403
404 JAVA_PKG_JARDEST="${1}"
405 }
406
407 # ------------------------------------------------------------------------------
408 # @ebuild-function java-pkg_sointo
409 #
410 # Changes the path that JNI libraries are installed into.
411 #
412 # @param $1 - new location to install JNI libraries into.
413 # ------------------------------------------------------------------------------
414 java-pkg_sointo() {
415 debug-print-function ${FUNCNAME} $*
416
417 JAVA_PKG_LIBDEST="${1}"
418 }
419
420 # ------------------------------------------------------------------------------
421 # @ebuild-function java-pkg_dohtml
422 #
423 # Install Javadoc HTML documentation
424 #
425 # @example
426 # java-pkg_dohtml dist/docs/
427 #
428 # ------------------------------------------------------------------------------
429 java-pkg_dohtml() {
430 debug-print-function ${FUNCNAME} $*
431
432 [[ ${#} -lt 1 ]] && die "At least one argument required for ${FUNCNAME}"
433 # TODO-nichoj find out what exactly -f package-list does
434 dohtml -f package-list "$@"
435 # this probably shouldn't be here but it provides
436 # a reasonable way to catch # docs for all of the
437 # old ebuilds.
438 java-pkg_recordjavadoc
439 }
440
441 # TODO document
442 java-pkg_dojavadoc() {
443 local dir="$1"
444
445 java-pkg_check-phase install
446
447 [[ -z "${dir}" ]] && die "Must specify a directory!"
448 [[ ! -d "${dir}" ]] && die "${dir} does not exist, or isn't a directory!"
449
450 local dir_to_install="${dir}"
451 if [[ "$(basename "${dir}")" != "api" ]]; then
452 dir_to_install="${T}/api"
453 # TODO use doins
454 cp -r "${dir}" "${dir_to_install}" || die "cp failed"
455 fi
456
457 java-pkg_dohtml -r ${dir_to_install}
458 }
459
460 # ------------------------------------------------------------------------------
461 # @ebuild-function java-pkg_dosrc
462 #
463 # Installs a zip containing the source for a package, so it can used in
464 # from IDEs like eclipse and netbeans.
465 #
466 # Ebuild needs to DEPEND on app-arch/zip to use this.
467 #
468 # It also should be controlled by USE=source.
469 #
470 # @example:
471 # java-pkg_dosrc src/*
472 #
473 # ------------------------------------------------------------------------------
474 # TODO change so it the arguments it takes are the base directories containing
475 # source -nichoj
476 # TODO should we be able to handle multiple calls to dosrc? -nichoj
477 # TODO maybe we can take an existing zip/jar? -nichoj
478 # FIXME apparently this fails if you give it an empty directories
479 java-pkg_dosrc() {
480 debug-print-function ${FUNCNAME} $*
481
482 [ ${#} -lt 1 ] && die "At least one argument needed"
483 if ! hasq source ${IUSE}; then
484 echo "Java QA Notice: ${FUNCNAME} called without source in IUSE"
485 fi
486
487 java-pkg_check-phase install
488
489 [[ ${#} -lt 1 ]] && die "At least one argument needed"
490
491 java-pkg_init_paths_
492
493 local zip_name="${PN}-src.zip"
494 local zip_path="${T}/${zip_name}"
495 local dir
496 for dir in ${@}; do
497 local dir_parent=$(dirname "${dir}")
498 local dir_name=$(basename "${dir}")
499 pushd ${dir_parent} > /dev/null || die "problem entering ${dir_parent}"
500 zip -q -r ${zip_path} ${dir_name} -i '*.java'
501 local result=$?
502 # 12 means zip has nothing to do
503 if [[ ${result} != 12 && ${result} != 0 ]]; then
504 die "failed to zip ${dir_name}"
505 fi
506 popd >/dev/null
507 done
508
509 # Install the zip
510 INSDESTTREE=${JAVA_PKG_SOURCESPATH} \
511 doins ${zip_path} || die "Failed to install source"
512
513 JAVA_SOURCES="${JAVA_PKG_SOURCESPATH}/${zip_name}"
514 java-pkg_do_write_
515 }
516
517 # ------------------------------------------------------------------------------
518 # @ebuild-function java-pkg_dolauncher
519 #
520 # Make a wrapper script to lauch/start this package
521 # If necessary, the wrapper will switch to the appropriate VM.
522 #
523 # @param $1 - filename of launcher to create
524 # @param $2 - options, as follows:
525 # --main the.main.class.too.start
526 # --jar /the/jar/too/launch.jar
527 # --java_args 'Extra arguments to pass too jave'
528 # --pkg_args 'extra arguments too pass too the package'
529 # --pwd
530 # -into
531 # -pre
532 # ------------------------------------------------------------------------------
533 java-pkg_dolauncher() {
534 debug-print-function ${FUNCNAME} $*
535
536 java-pkg_check-phase install
537
538 [[ ${#} -lt 1 ]] && die "Need at least one argument"
539
540 java-pkg_init_paths_
541
542 local name="${1}"
543 # TODO rename to launcher
544 local target="${T}/${name}"
545 local target_dir pre
546 shift
547
548 echo "#!/bin/bash" > "${target}"
549 while [[ -n "${1}" && -n "${2}" ]]; do
550 local var=${1} value=${2}
551 if [[ "${var:0:2}" == "--" ]]; then
552 echo "gjl_${var:2}=\"${value}\"" >> "${target}"
553 elif [[ "${var}" == "-into" ]]; then
554 target_dir="${value}"
555 elif [[ "${var}" == "-pre" ]]; then
556 pre="${value}"
557 fi
558 shift 2
559 done
560 echo "gjl_package=${JAVA_PKG_NAME}" >> "${target}"
561 [[ -n "${pre}" ]] && [[ -f "${pre}" ]] && cat "${pre}" >> "${target}"
562 echo "source /usr/share/java-config-2/launcher/launcher.bash" >> "${target}"
563
564 if [[ -n "${into}" ]]; then
565 DESTTREE="${target_dir}" dobin "${target}"
566 local ret=$?
567 return ${ret}
568 else
569 dobin "${target}"
570 fi
571 }
572
573 # ------------------------------------------------------------------------------
574 # Install war files.
575 # TODO document
576 # ------------------------------------------------------------------------------
577 java-pkg_dowar() {
578 debug-print-function ${FUNCNAME} $*
579
580 # Check for arguments
581 [[ ${#} -lt 1 ]] && die "At least one argument needed"
582 java-pkg_check-phase install
583
584 java-pkg_init_paths_
585
586 local war
587 for war in $* ; do
588 local warpath
589 # TODO evaluate if we want to handle symlinks differently -nichoj
590 # Check for symlink
591 if [[ -L "${war}" ]] ; then
592 cp "${war}" "${T}"
593 warpath="${T}$(basename "${war}")"
594 # Check for directory
595 # TODO evaluate if we want to handle directories differently -nichoj
596 elif [[ -d "${war}" ]] ; then
597 echo "dowar: warning, skipping directory ${war}"
598 continue
599 else
600 warpath="${war}"
601 fi
602
603 # Install those files like you mean it
604 INSOPTIONS="-m 0644" \
605 INSDESTTREE=${JAVA_PKG_WARDEST} \
606 doins ${warpath}
607 done
608 }
609
610 # ------------------------------------------------------------------------------
611 # @internal-function java-pkg_recordjavadoc
612 # Scan for JavaDocs, and record their existence in the package.env file
613 #
614 # TODO make sure this in the proper section
615 # ------------------------------------------------------------------------------
616 java-pkg_recordjavadoc()
617 {
618 debug-print-function ${FUNCNAME} $*
619 # the find statement is important
620 # as some packages include multiple trees of javadoc
621 JAVADOC_PATH="$(find ${D}/usr/share/doc/ -name allclasses-frame.html -printf '%h:')"
622 # remove $D - TODO: check this is ok with all cases of the above
623 JAVADOC_PATH="${JAVADOC_PATH//${D}}"
624 if [[ -n "${JAVADOC_PATH}" ]] ; then
625 debug-print "javadocs found in ${JAVADOC_PATH%:}"
626 java-pkg_do_write_
627 else
628 debug-print "No javadocs found"
629 fi
630 }
631
632 # ------------------------------------------------------------------------------
633 # @section-end install
634 # ------------------------------------------------------------------------------
635
636 # ------------------------------------------------------------------------------
637 # @begin-section query
638 # Use these to build the classpath for building a package.
639 # ------------------------------------------------------------------------------
640
641 # ------------------------------------------------------------------------------
642 # @ebuild-function java-pkg_jar-from
643 #
644 # Makes a symlink to a jar from a certain package
645 # A lot of java packages include dependencies in a lib/ directory
646 # You can use this function to replace these bundled dependencies.
647 #
648 # Example: get all jars from xerces slot 2
649 # java-pkg_jar-from xerces-2
650 # Example: get a specific jar from xerces slot 2
651 # java-pkg_jar-from xerces-2 xml-apis.jar
652 # Example get a specific jar from xerces slot 2, and name it diffrently
653 # java-pkg_jar-from xerces-2 xml-apis.jar xml.jar
654 #
655 # @param $1 - Package to get jars from.
656 # @param $2 - jar from package. If not specified, all jars will be used.
657 # @param $3 - When a single jar is specified, destination filename of the
658 # symlink. Defaults to the name of the jar.
659 # ------------------------------------------------------------------------------
660 # TODO could probably be cleaned up a little
661 java-pkg_jar-from() {
662 debug-print-function ${FUNCNAME} $*
663
664 local target_pkg="${1}" target_jar="${2}" destjar="${3}"
665
666 [[ -z ${target_pkg} ]] && die "Must specify a package"
667
668 # default destjar to the target jar
669 [[ -z "${destjar}" ]] && destjar="${target_jar}"
670
671 local classpath="$(java-config --classpath=${target_pkg})"
672 [[ $? != 0 ]] && die "There was a problem getting the classpath for ${target_pkg}"
673
674 local jar
675 for jar in ${classpath//:/ }; do
676 local jar_name=$(basename "${jar}")
677 if [[ ! -f "${jar}" ]] ; then
678 debug-print "${jar} from ${target_pkg} does not exist"
679 die "Installation problems with jars in ${target_pkg} - is it installed?"
680 fi
681 # If no specific target jar was indicated, link it
682 if [[ -z "${target_jar}" ]] ; then
683 [[ -f "${target_jar}" ]] && rm "${target_jar}"
684 ln -snf "${jar}" \
685 || die "Failed to make symlink from ${jar} to ${jar_name}"
686 java-pkg_record-jar_ "${target_pkg}" "${jar}"
687 # otherwise, if the current jar is the target jar, link it
688 elif [[ "${jar_name}" == "${target_jar}" ]] ; then
689 [[ -f "${destjar}" ]] && rm "${destjar}"
690 ln -snf "${jar}" "${destjar}" \
691 || die "Failed to make symlink from ${jar} to ${destjar}"
692 java-pkg_record-jar_ "${target_pkg}" "${jar}"
693 return 0
694 fi
695 done
696 # if no target was specified, we're ok
697 if [[ -z "${target_jar}" ]] ; then
698 return 0
699 # otherwise, die bitterly
700 else
701 die "Failed to find ${target_jar:-jar} in ${target_pkg}"
702 fi
703 }
704
705 # ------------------------------------------------------------------------------
706 # @ebuild-function java-pkg_jarfrom
707 #
708 # See java-pkg_jar-from
709 # ------------------------------------------------------------------------------
710 java-pkg_jarfrom() {
711 java-pkg_jar-from "$@"
712 }
713
714 # ------------------------------------------------------------------------------
715 # @ebuild-function java-pkg_getjars
716 #
717 # Get the classpath provided by any number of packages
718 # Among other things, this can be passed to 'javac -classpath' or 'ant -lib'.
719 #
720 # Example: Get the classpath for xerces-2,
721 # java-pkg_getjars xerces-2 xalan
722 # Example Return:
723 # /usr/share/xerces-2/lib/xml-apis.jar:/usr/share/xerces-2/lib/xmlParserAPIs.jar:/usr/share/xalan/lib/xalan.jar
724 #
725 # @param $@ - list of packages to get jars from
726 # ------------------------------------------------------------------------------
727 java-pkg_getjars() {
728 debug-print-function ${FUNCNAME} $*
729
730 [[ ${#} -lt 1 ]] && die "At least one argument needed"
731
732 # NOTE could probably just pass $@ to java-config --classpath. and return it
733 local classpath pkg
734 for pkg in ${@//,/ }; do
735 #for pkg in $(echo "$@" | tr ',' ' '); do
736 jars="$(java-config --classpath=${pkg})"
737 debug-print "${pkg}:${jars}"
738 # TODO should we ensure jars exist?
739 if [[ -z "${classpath}" ]]; then
740 classpath="${jars}"
741 else
742 classpath="${classpath}:${jars}"
743 fi
744 java-pkg_record-jar_ "${pkg}"
745 done
746 echo "${classpath}"
747 }
748
749 # ------------------------------------------------------------------------------
750 # @ebuild-function java-pkg_getjar
751 #
752 # Get the filename of a single jar from a package
753 #
754 # @example
755 # java-pkg_getjar xerces-2 xml-apis.jar
756 # @example-return
757 # /usr/share/xerces-2/lib/xml-apis.jar
758 #
759 # @param $1 - package to use
760 # @param $2 - jar to get
761 # ------------------------------------------------------------------------------
762 java-pkg_getjar() {
763 debug-print-function ${FUNCNAME} $*
764
765 local pkg="${1}" target_jar="${2}" jar
766 [[ -z ${pkg} ]] && die "Must specify package to get a jar from"
767 [[ -z ${target_jar} ]] && die "Must specify jar to get"
768
769 # TODO check that package is actually installed
770 local classpath=$(java-config --classpath=${pkg})
771 [[ $? != 0 ]] && die "There could not find classpath for ${pkg}. Are you sure its installed?"
772 for jar in ${classpath//:/ }; do
773 if [[ ! -f "${jar}" ]] ; then
774 die "Installation problems with jars in ${pkg} - is it installed?"
775 fi
776
777 if [[ "$(basename ${jar})" == "${target_jar}" ]] ; then
778 java-pkg_record-jar_ "${pkg}" "${jar}"
779 echo "${jar}"
780 return 0
781 fi
782 done
783
784 die "Could not find ${target_jar} in ${pkg}"
785 return 1
786 }
787
788 # This function reads stdin, and based on that input, figures out how to
789 # populate jars from the filesystem.
790 # Need to figure out a good way of making use of this, ie be able to use a
791 # string that was built instead of stdin
792 # NOTE: this isn't quite ready for primetime.
793 #java-pkg_populate-jars() {
794 # local line
795 #
796 # read line
797 # while [[ -n "${line}" ]]; do
798 # # Ignore comments
799 # [[ ${line%%#*} == "" ]] && continue
800 #
801 # # get rid of any spaces
802 # line="${line// /}"
803 #
804 # # format: path=jarinfo
805 # local path=${line%%=*}
806 # local jarinfo=${line##*=}
807 #
808 # # format: jar@package
809 # local jar=${jarinfo%%@*}.jar
810 # local package=${jarinfo##*@}
811 # if [[ -n ${replace_only} ]]; then
812 # [[ ! -f $path ]] && die "No jar exists at ${path}"
813 # fi
814 # if [[ -n ${create_parent} ]]; then
815 # local parent=$(dirname ${path})
816 # mkdir -p "${parent}"
817 # fi
818 # java-pkg_jar-from "${package}" "${jar}" "${path}"
819 #
820 # read line
821 # done
822 #}
823
824 # ------------------------------------------------------------------------------
825 # @section-end query
826 # ------------------------------------------------------------------------------
827
828 # ------------------------------------------------------------------------------
829 # @section-begin helper
830 # @section-summary Helper functions
831 #
832 # Various other functions to use from an ebuild
833 # ------------------------------------------------------------------------------
834
835 # ------------------------------------------------------------------------------
836 # @ebuild-function java-pkg_need
837 #
838 # Adds virtual dependencies, which can optionally be controlled by a USE flag.
839 # Currently supported virtuals are:
840 # javamail
841 # jdbc-stdext
842 # jaf
843 # jdbc-rowset
844 # jms
845 #
846 # @param $1 - Optionally indicate that the dependencies are controlled by
847 # a use flag by specifying '--use' Requires $2.
848 # @param $2 - USE flag which will enable the dependencies.
849 # @param $@ - virtual packages to add depenedencies for
850 # ------------------------------------------------------------------------------
851 # TODO rewrite to parse a line based declaration file instead -- karltk
852 #java-pkg_need() {
853 # debug-print-function ${FUNCNAME} $*
854 # local useflag
855 # if [[ ${1} == "--use" ]]; then
856 # useflag="${2}"
857 # shift 2
858 # fi
859 #
860 # if [[ -z ${1} ]]; then
861 # die "Must specify at least one virtual package."
862 # fi
863 #
864 # local depstr newdepstr
865 #
866 # for virtual in ${@}; do
867 # if has ${virtual} ${JAVA_PKG_VNEED}; then
868 # debug-print "Already registered virtual ${virtual}"
869 # continue
870 # fi
871 # case ${virtual} in
872 # javamail)
873 # debug-print "java-pkg_need: adding javamail dependencies"
874 # newdepstr="|| ( dev-java/gnu-javamail dev-java/sun-javamail-bin )"
875 # ;;
876 # jdbc-stdext)
877 # debug-print "java-pkg_need: adding jdbc-stdext dependencies"
878 # newdepstr="|| ( >=virtual/jdk-1.4 dev-java/jdbc2-stdext )"
879 # ;;
880 # jaf)
881 # debug-print "java-pkg_need: adding jaf dependencies"
882 # newdepstr="|| ( dev-java/gnu-jaf dev-java/sun-jaf-bin )"
883 # ;;
884 # jdbc-rowset)
885 # debug-print "java-pkg_need: adding jdbc-rowset dependencies"
886 # newdepstr="|| ( >=virtual/jdk-1.5 dev-java/sun-jdbc-rowset )"
887 # ;;
888 # jms)
889 # debug-print "java-pkg_need: adding jms dependencies"
890 # newdepstr="|| ( dev-java/sun-jms dev-java/openjms )"
891 # ;;
892 # *)
893 # die "Invalid virtual: ${virtual}"
894 # esac
895 #
896 # export JAVA_PKG_VNEED="${JAVA_PKG_VNEED} ${virtual}"
897 #
898 # if [[ -n ${useflag} ]]; then
899 # depstr="${depstr} ${useflag}? ( ${newdepstr} )"
900 # else
901 # depstr="${depstr} ${newdepstr}"
902 # fi
903 # done
904 #
905 # [[ -z ${JAVA_PKG_NV_DEPEND} ]] && export JAVA_PKG_NV_DEPEND="${DEPEND}"
906 # [[ -z ${JAVA_PKG_NV_RDEPEND} ]] && export JAVA_PKG_NV_RDEPEND="${RDEPEND}"
907 #
908 # export DEPEND="${DEPEND} ${depstr}"
909 # export RDEPEND="${RDEPEND} ${depstr}"
910 #}
911
912 # This should be used after S has been populated with symlinks to jars
913 # TODO document
914 java-pkg_ensure-no-bundled-jars() {
915 debug-print-function ${FUNCNAME} $*
916 pushd ${WORKDIR} >/dev/null 2>/dev/null
917
918 local bundled_jars=$(find . -name "*.jar" -type f)
919 if [[ -n ${bundled_jars} ]]; then
920 echo "Bundled jars found:"
921 local jar
922 for jar in ${bundled_jars}; do
923 echo $(pwd)${jar/./}
924 done
925 die "Bundled jars found!"
926
927 fi
928 popd >/dev/null 2>/dev/null
929 }
930
931 # ------------------------------------------------------------------------------
932 # @internal-function java-pkg_ensure-vm-version-sufficient
933 #
934 # Checks if we have a sufficient VM and dies if we don't.
935 #
936 # ------------------------------------------------------------------------------
937 java-pkg_ensure-vm-version-sufficient() {
938 debug-print-function ${FUNCNAME} $*
939
940 if ! java-pkg_is-vm-version-sufficient; then
941 debug-print "VM is not suffient"
942 eerror "Current Java VM cannot build this package"
943 einfo "Please use java-config -S to set the correct one"
944 die "Active Java VM cannot build this package"
945 fi
946 }
947
948 # ------------------------------------------------------------------------------
949 # @internal-function java-pkg_is-vm-version-sufficient
950 #
951 # @return zero - VM is sufficient
952 # @return non-zero - VM is not sufficient
953 # ------------------------------------------------------------------------------
954 java-pkg_is-vm-version-sufficient() {
955 debug-print-function ${FUNCNAME} $*
956
957 depend-java-query --is-sufficient "${DEPEND}" > /dev/null
958 return $?
959 }
960
961 # ------------------------------------------------------------------------------
962 # @internal-function java-pkg_ensure-vm-version-eq
963 #
964 # Die if the current VM is not equal to the argument passed.
965 #
966 # @param $@ - Desired VM version to ensure
967 # ------------------------------------------------------------------------------
968 java-pkg_ensure-vm-version-eq() {
969 debug-print-function ${FUNCNAME} $*
970
971 if ! java-pkg_is-vm-version-eq $@ ; then
972 debug-print "VM is not suffient"
973 eerror "This package requires a Java VM version = $@"
974 einfo "Please use java-config -S to set the correct one"
975 die "Active Java VM too old"
976 fi
977 }
978
979 # ------------------------------------------------------------------------------
980 # @internal-function java-pkg_is-vm-version-eq
981 #
982 # @param $@ - VM version to compare current VM to
983 # @return zero - VM versions are equal
984 # @return non-zero - VM version are not equal
985 # ------------------------------------------------------------------------------
986 java-pkg_is-vm-version-eq() {
987 debug-print-function ${FUNCNAME} $*
988
989 local needed_version="$@"
990
991 [[ -z "${needed_version}" ]] && die "need an argument"
992
993 local vm_version="$(java-pkg_get-vm-version)"
994
995 vm_version="$(get_version_component_range 1-2 "${vm_version}")"
996 needed_version="$(get_version_component_range 1-2 "${needed_version}")"
997
998 if [[ -z "${vm_version}" ]]; then
999 debug-print "Could not get JDK version from DEPEND"
1000 return 1
1001 else
1002 if [[ "${vm_version}" == "${needed_version}" ]]; then
1003 debug-print "Detected a JDK(${vm_version}) = ${needed_version}"
1004 return 0
1005 else
1006 debug-print "Detected a JDK(${vm_version}) != ${needed_version}"
1007 return 1
1008 fi
1009 fi
1010 }
1011
1012 # ------------------------------------------------------------------------------
1013 # @internal-function java-pkg_ensure-vm-version-ge
1014 #
1015 # Die if the current VM is not greater than the desired version
1016 #
1017 # @param $@ - VM version to compare current to
1018 # ------------------------------------------------------------------------------
1019 java-pkg_ensure-vm-version-ge() {
1020 debug-print-function ${FUNCNAME} $*
1021
1022 if ! java-pkg_is-vm-version-ge "$@" ; then
1023 debug-print "vm is not suffient"
1024 eerror "This package requires a Java VM version >= $@"
1025 einfo "Please use java-config -S to set the correct one"
1026 die "Active Java VM too old"
1027 fi
1028 }
1029
1030 # ------------------------------------------------------------------------------
1031 # @internal-function java-pkg_is-vm-version-ge
1032 #
1033 # @param $@ - VM version to compare current VM to
1034 # @return zero - current VM version is greater than checked version
1035 # @return non-zero - current VM version is not greater than checked version
1036 # ------------------------------------------------------------------------------
1037 java-pkg_is-vm-version-ge() {
1038 debug-print-function ${FUNCNAME} $*
1039
1040 local needed_version=$@
1041 local vm_version=$(java-pkg_get-vm-version)
1042 if [[ -z "${vm_version}" ]]; then
1043 debug-print "Could not get JDK version from DEPEND"
1044 return 1
1045 else
1046 if version_is_at_least "${needed_version}" "${vm_version}"; then
1047 debug-print "Detected a JDK(${vm_version}) >= ${needed_version}"
1048 return 0
1049 else
1050 debug-print "Detected a JDK(${vm_version}) < ${needed_version}"
1051 return 1
1052 fi
1053 fi
1054 }
1055
1056 # ------------------------------------------------------------------------------
1057 # @ebuild-function java-pkg_get-source
1058 #
1059 # Determines what source version should be used, for passing to -source.
1060 # Unless you want to break things you probably shouldn't set _WANT_SOURCE
1061 #
1062 # @return string - Either the lowest possible source, or JAVA_PKG_WANT_SOURCE
1063 # ------------------------------------------------------------------------------
1064 java-pkg_get-source() {
1065 echo ${JAVA_PKG_WANT_SOURCE:-$(depend-java-query --get-lowest "${DEPEND} ${RDEPEND}")}
1066 }
1067
1068 # ------------------------------------------------------------------------------
1069 # @ebuild-function java-pkg_get-target
1070 #
1071 # Determines what target version should be used, for passing to -target.
1072 # If you don't care about lower versions, you can set _WANT_TARGET to the
1073 # version of your JDK.
1074 # Remember doing this will mostly like cause things to break.
1075 # Doesn't allow it to be lower then the one in depend.
1076 # Doesn't allow it to be higher then the active vm.
1077 #
1078 # @return string - Either the lowest possible target, or JAVA_PKG_WANT_TARGET
1079 # ------------------------------------------------------------------------------
1080 java-pkg_get-target() {
1081 local min=$(depend-java-query --get-lowest "${DEPEND} ${RDEPEND}")
1082 if [[ -n "${JAVA_PKG_WANT_TARGET}" ]]; then
1083 local max="$(java-config --select-vm "${GENTOO_VM}" -g PROVIDES_VERSION)"
1084 if version_is_at_least "${min}" "${JAVA_PKG_WANT_TARGET}" && version_is_at_least "${JAVA_PKG_WANT_TARGET}" "${max}"; then
1085 echo ${JAVA_PKG_WANT_TARGET}
1086 else
1087 echo ${min}
1088 fi
1089 else
1090 echo ${min}
1091 fi
1092
1093 #echo ${JAVA_PKG_WANT_TARGET:-$(depend-java-query --get-lowest "${DEPEND}")}
1094 }
1095
1096 java-pkg_get-javac() {
1097 debug-print-function ${FUNCNAME} $*
1098
1099 java-pkg_init-compiler_
1100 local compiler="${GENTOO_COMPILER}"
1101
1102 local compiler_executable
1103 if [[ "${compiler}" = "javac" ]]; then
1104 # nothing fancy needs to be done for javac
1105 compiler_executable="javac"
1106 else
1107 # for everything else, try to determine from an env file
1108
1109 local compiler_env="/usr/share/java-config-2/compiler/${compiler}"
1110 if [[ -f ${compiler_env} ]]; then
1111 local old_javac=${JAVAC}
1112 unset JAVAC
1113 # try to get value of JAVAC
1114 compiler_executable="$(source ${compiler_env} 1>/dev/null 2>&1; echo ${JAVAC})"
1115 export JAVAC=${old_javac}
1116
1117 [[ -z ${compiler_executable} ]] && die "JAVAC is empty or undefined in ${compiler_env}"
1118
1119 # check that it's executable
1120 if [[ ! -x ${compiler_executable} ]]; then
1121 eerror "Could not find ${compiler_executable}!"
1122 die "${compiler_executable} doesn't exist, or isn't executable"
1123 fi
1124 else
1125 eerror "Could not find environment file for ${compiler}"
1126 die "Could not find ${compiler_env}"
1127 fi
1128 fi
1129 echo ${compiler_executable}
1130 }
1131
1132 # ------------------------------------------------------------------------------
1133 # @ebuild-function java-pkg_javac-args
1134 #
1135 # If an ebuild uses javac directly, instead of using ejavac, it should call this
1136 # to know what -source/-target to use.
1137 #
1138 # @return string - arguments to pass to javac, complete with -target and -source
1139 # ------------------------------------------------------------------------------
1140 java-pkg_javac-args() {
1141 debug-print-function ${FUNCNAME} $*
1142
1143 local want_source="$(java-pkg_get-source)"
1144 local want_target="$(java-pkg_get-target)"
1145
1146 local source_str="-source ${want_source}"
1147 local target_str="-target ${want_target}"
1148
1149 debug-print "want source: ${want_source}"
1150 debug-print "want target: ${want_target}"
1151
1152 if [[ -z "${want_source}" || -z "${want_target}" ]]; then
1153 debug-print "could not find valid -source/-target values"
1154 die "Could not find valid -source/-target values"
1155 else
1156 if java-pkg_is-vm-version-ge "1.4"; then
1157 echo "${source_str} ${target_str}"
1158 else
1159 echo "${target_str}"
1160 fi
1161 fi
1162 }
1163
1164 # TODO document
1165 java-pkg_get-jni-cflags() {
1166 local flags="-I${JAVA_HOME}/include"
1167
1168 # TODO figure out how to cope with other things than linux...
1169 flags="${flags} -I${JAVA_HOME}/include/linux"
1170
1171 echo ${flags}
1172 }
1173
1174 # ------------------------------------------------------------------------------
1175 # @section-end helper
1176 # ------------------------------------------------------------------------------
1177
1178 # ------------------------------------------------------------------------------
1179 # @section-begin build
1180 # @section-summary Build functions
1181 #
1182 # These are some functions for building a package. In particular, it consists of
1183 # wrappers for javac and ant.
1184 # ------------------------------------------------------------------------------
1185
1186 # ------------------------------------------------------------------------------
1187 # @ebuild-function eant
1188 #
1189 # Ant wrapper function. Will use the appropriate compiler, based on user-defined
1190 # compiler.
1191 #
1192 # ------------------------------------------------------------------------------
1193 eant() {
1194 debug-print-function ${FUNCNAME} $*
1195
1196 local antflags
1197 java-pkg_init-compiler_
1198 local compiler="${GENTOO_COMPILER}"
1199
1200 local compiler_env="${JAVA_PKG_COMPILER_DIR}/${compiler}"
1201
1202 local build_compiler="$(source ${compiler_env} 1>/dev/null 2>&1; echo ${ANT_BUILD_COMPILER})"
1203 if [[ "${compiler}" != "javac" && -z "${build_compiler}" ]]; then
1204 die "ANT_BUILD_COMPILER undefined in ${compiler_env}"
1205 fi
1206
1207 if [[ ${compiler} != "javac" ]]; then
1208 antflags="-Dbuild.compiler=${build_compiler}"
1209 # Figure out any extra stuff to put on the classpath for compilers aside
1210 # from javac
1211 # ANT_BUILD_COMPILER_DEPS should be something that could be passed to
1212 # java-config -p
1213 local build_compiler_deps="$(source ${JAVA_PKG_COMPILER_DIR}/${compiler} 1>/dev/null 2>&1; echo ${ANT_BUILD_COMPILER_DEPS})"
1214 if [[ -n ${build_compiler_deps} ]]; then
1215 antflags="${antflags} -lib $(java-config -p ${build_compiler_deps})"
1216 fi
1217 fi
1218
1219 if [[ -n ${JAVA_PKG_DEBUG} ]]; then
1220 antflags="${antflags} -debug"
1221 fi
1222
1223 [[ -n ${JAVA_PKG_DEBUG} ]] && echo ant ${antflags} "${@}"
1224 ant ${antflags} "${@}" || die "eant failed"
1225
1226 }
1227
1228 # ------------------------------------------------------------------------------
1229 # @ebuild-function ejavac
1230 #
1231 # Javac wrapper function. Will use the appropriate compiler, based on
1232 # /etc/java-config/compilers.conf
1233 #
1234 # @param $@ - Arguments to be passed to the compiler
1235 # ------------------------------------------------------------------------------
1236 ejavac() {
1237 debug-print-function ${FUNCNAME} $*
1238
1239 # FIXME using get-javac ends up printing stuff with einfo
1240 # local compiler_executable=$(java-pkg_get-javac)
1241 local compiler_executable="javac"
1242
1243 [[ -n ${JAVA_PKG_DEBUG} ]] && echo ${compiler_executable} $(java-pkg_javac-args) "${@}"
1244 ${compiler_executable} $(java-pkg_javac-args) "${@}" || die "ejavac failed"
1245 }
1246
1247 # ------------------------------------------------------------------------------
1248 # @ebuild-function java-pkg_filter-compiler
1249 #
1250 # Used to prevent the use of some compilers. Should be used in src_compile.
1251 # Basically, it just appends onto JAVA_PKG_FILTER_COMPILER
1252 #
1253 # @param $@ - compilers to filter
1254 # ------------------------------------------------------------------------------
1255 java-pkg_filter-compiler() {
1256 JAVA_PKG_FILTER_COMPILER="${JAVA_PKG_FILTER_COMPILER} $@"
1257 }
1258
1259 # ------------------------------------------------------------------------------
1260 # @ebuild-function java-pkg_force-compiler
1261 #
1262 # Used to force the use of particular compilers. Should be used in src_compile.
1263 # A common use of this would be to force ecj-3.1 to be used on amd64, to avoid
1264 # OutOfMemoryErrors that may come up.
1265 #
1266 # @param $@ - compilers to force
1267 # ------------------------------------------------------------------------------
1268 java-pkg_force-compiler() {
1269 JAVA_PKG_FORCE_COMPILER="$@"
1270 }
1271
1272 # ------------------------------------------------------------------------------
1273 # @ebuild-function use_doc
1274 #
1275 # Helper function for getting ant to build javadocs. If the user has USE=doc,
1276 # then 'javadoc' or the argument are returned. Otherwise, there is no return.
1277 #
1278 # The output of this should be passed to ant.
1279 #
1280 # Example: build javadocs by calling 'javadoc' target
1281 # eant $(use_doc)
1282 # Example: build javadocs by calling 'apidoc' target
1283 # eant $(use_doc apidoc)
1284 #
1285 # @param $@ - Option value to return. Defaults to 'javadoc'
1286 # @return string - Name of the target to create javadocs
1287 # ------------------------------------------------------------------------------
1288 use_doc() {
1289 use doc && echo ${@:-javadoc}
1290 }
1291
1292 # ------------------------------------------------------------------------------
1293 # @section-end build
1294 # ------------------------------------------------------------------------------
1295
1296 # ------------------------------------------------------------------------------
1297 # @section-begin internal
1298 # @section-summary Internal functions
1299 #
1300 # Do __NOT__ use any of these from an ebuild! These are only to be used from
1301 # within the java eclasses.
1302 # ------------------------------------------------------------------------------
1303
1304 # -----------------------------------------------------------------------------
1305 # @function-internal java-pkg_init
1306 #
1307 # The purpose of this function, as the name might imply, is to initialize the
1308 # Java environment. It ensures that that there aren't any environment variables
1309 # that'll muss things up. It initializes some variables, which are used
1310 # internally. And most importantly, it'll switch the VM if necessary.
1311 #
1312 # This shouldn't be used directly. Instead, java-pkg and java-pkg-opt will
1313 # call it during each of the phases of the merge process.
1314 #
1315 # -----------------------------------------------------------------------------
1316 java-pkg_init() {
1317 unset JAVAC
1318 unset JAVA_HOME
1319 java-pkg_init_paths_
1320 java-pkg_switch-vm
1321 PATH=${JAVA_HOME}/bin:${PATH}
1322
1323 # TODO we will probably want to set JAVAC and JAVACFLAGS
1324
1325 # Do some QA checks
1326 java-pkg_check-jikes
1327
1328 # When users have crazy classpaths some packages can fail to compile.
1329 # and everything should work with empty CLASSPATH.
1330 # This also helps prevent unexpected dependencies on random things
1331 # from the CLASSPATH.
1332 unset CLASSPATH
1333 }
1334
1335 # ------------------------------------------------------------------------------
1336 # @function-internal java-pkg-init-compiler_
1337 #
1338 # This function attempts to figure out what compiler should be used. It does
1339 # this by reading the file at JAVA_PKG_COMPILERS_CONF, and checking the
1340 # COMPILERS variable defined there.
1341 # This can be overridden by a list in JAVA_PKG_FORCE_COMPILER
1342 #
1343 # It will go through the list of compilers, and verify that it supports the
1344 # target and source that are needed. If it is not suitable, then the next
1345 # compiler is checked. When JAVA_PKG_FORCE_COMPILER is defined, this checking
1346 # isn't done.
1347 #
1348 # Once the which compiler to use has been figured out, it is set to
1349 # GENTOO_COMPILER.
1350 #
1351 # If you hadn't guessed, JAVA_PKG_FORCE_COMPILER is for testing only.
1352 #
1353 # If the user doesn't defined anything in JAVA_PKG_COMPILERS_CONF, or no
1354 # suitable compiler was found there, then the default is to use javac provided
1355 # by the current VM.
1356 #
1357 #
1358 # @return name of the compiler to use
1359 # ------------------------------------------------------------------------------
1360 java-pkg_init-compiler_() {
1361 debug-print-function ${FUNCNAME} $*
1362
1363 if [[ -n ${GENTOO_COMPILER} ]]; then
1364 debug-print "GENTOO_COMPILER already set"
1365 return
1366 fi
1367
1368 local compilers;
1369 if [[ -z ${JAVA_PKG_FORCE_COMPILER} ]]; then
1370 compilers="$(source ${JAVA_PKG_COMPILERS_CONF} 1>/dev/null 2>&1; echo ${COMPILERS})"
1371 else
1372 compilers=${JAVA_PKG_FORCE_COMPILER}
1373 fi
1374
1375 debug-print "Read \"${compilers}\" from ${JAVA_PKG_COMPILERS_CONF}"
1376
1377 # Figure out if we should announce what compiler we're using
1378 local compiler
1379 for compiler in ${compilers}; do
1380 debug-print "Checking ${compiler}..."
1381 # javac should always be alright
1382 if [[ ${compiler} = "javac" ]]; then
1383 debug-print "Found javac... breaking"
1384 export GENTOO_COMPILER="javac"
1385 break
1386 fi
1387
1388 if has ${compiler} ${JAVA_PKG_FILTER_COMPILER}; then
1389 if [[ -z ${JAVA_PKG_FORCE_COMPILER} ]]; then
1390 einfo "Filtering ${compiler}"
1391 continue
1392 fi
1393 fi
1394
1395 # for non-javac, we need to make sure it supports the right target and
1396 # source
1397 local compiler_env="${JAVA_PKG_COMPILER_DIR}/${compiler}"
1398 if [[ -f ${compiler_env} ]]; then
1399 local desired_target="$(java-pkg_get-target)"
1400 local desired_source="$(java-pkg_get-source)"
1401
1402
1403 # Verify that the compiler supports target
1404 local supported_target=$(source ${compiler_env} 1>/dev/null 2>&1; echo ${SUPPORTED_TARGET})
1405 if ! has ${desired_target} ${supported_target}; then
1406 ewarn "${compiler} does not support -target ${desired_target}, skipping"
1407 continue
1408 fi
1409
1410 # -source was introduced in 1.3, so only check 1.3 and on
1411 if version_is_at_least "${desired_soure}" "1.3"; then
1412 # Verify that the compiler supports source
1413 local supported_source=$(source ${compiler_env} 1>/dev/null 2>&1; echo ${SUPPORTED_SOURCE})
1414 if ! has ${desired_source} ${supported_source}; then
1415 ewarn "${compiler} does not support -source ${desired_source}, skipping"
1416 continue
1417 fi
1418 fi
1419
1420 # if you get here, then the compiler should be good to go
1421 export GENTOO_COMPILER="${compiler}"
1422 break
1423 else
1424 ewarn "Could not find configuration for ${compiler}, skipping"
1425 ewarn "Perhaps it is not installed?"
1426 continue
1427 fi
1428 done
1429
1430 # If it hasn't been defined already, default to javac
1431 if [[ -z ${GENTOO_COMPILER} ]]; then
1432 if [[ -n ${compilers} ]]; then
1433 einfo "No suitable compiler found: defaulting javac for compilation"
1434 else
1435 # probably don't need to notify users about the default.
1436 :;#einfo "Defaulting to javac for compilation"
1437 fi
1438 export GENTOO_COMPILER=javac
1439 else
1440 einfo "Using ${GENTOO_COMPILER} for compilation"
1441 fi
1442
1443 }
1444
1445 # ------------------------------------------------------------------------------
1446 # @internal-function init_paths_
1447 #
1448 # Initializes some variables that will be used. These variables are mostly used
1449 # to determine where things will eventually get installed.
1450 # ------------------------------------------------------------------------------
1451 java-pkg_init_paths_() {
1452 debug-print-function ${FUNCNAME} $*
1453
1454 local pkg_name
1455 if [[ "$SLOT" == "0" ]] ; then
1456 JAVA_PKG_NAME="${PN}"
1457 else
1458 JAVA_PKG_NAME="${PN}-${SLOT}"
1459 fi
1460
1461 JAVA_PKG_SHAREPATH="${DESTTREE}/share/${JAVA_PKG_NAME}"
1462 JAVA_PKG_SOURCESPATH="${JAVA_PKG_SHAREPATH}/sources/"
1463 JAVA_PKG_ENV="${D}${JAVA_PKG_SHAREPATH}/package.env"
1464
1465 [[ -z "${JAVA_PKG_JARDEST}" ]] && JAVA_PKG_JARDEST="${JAVA_PKG_SHAREPATH}/lib"
1466 [[ -z "${JAVA_PKG_LIBDEST}" ]] && JAVA_PKG_LIBDEST="${DESTTREE}/$(get_libdir)/${JAVA_PKG_NAME}"
1467 [[ -z "${JAVA_PKG_WARDEST}" ]] && JAVA_PKG_WARDEST="${JAVA_PKG_SHAREPATH}/webapps"
1468
1469
1470 # TODO maybe only print once?
1471 debug-print "JAVA_PKG_SHAREPATH: ${JAVA_PKG_SHAREPATH}"
1472 debug-print "JAVA_PKG_ENV: ${JAVA_PKG_ENV}"
1473 debug-print "JAVA_PKG_JARDEST: ${JAVA_PKG_JARDEST}"
1474 debug-print "JAVA_PKG_LIBDEST: ${JAVA_PKG_LIBDEST}"
1475 debug-print "JAVA_PKG_WARDEST: ${JAVA_PKG_WARDEST}"
1476 }
1477
1478 # ------------------------------------------------------------------------------
1479 # @internal-function java-pkg_do_write_
1480 #
1481 # Writes the package.env out to disk.
1482 #
1483 # ------------------------------------------------------------------------------
1484 # TODO change to do-write, to match everything else
1485 java-pkg_do_write_() {
1486 # Create directory for package.env
1487 dodir "${JAVA_PKG_SHAREPATH}"
1488 if [[ -n "${JAVA_PKG_CLASSPATH}" || -n "${JAVA_PKG_LIBRARY}" || -f "${JAVA_PKG_DEPEND}" ]]; then
1489 # Create package.env
1490 (
1491 echo "DESCRIPTION=\"${DESCRIPTION}\""
1492 echo "GENERATION=\"2\""
1493
1494 [[ -n "${JAVA_PKG_CLASSPATH}" ]] && echo "CLASSPATH=\"${JAVA_PKG_CLASSPATH}\""
1495 [[ -n "${JAVA_PKG_LIBRARY}" ]] && echo "LIBRARY_PATH=\"${JAVA_PKG_LIBRARY}\""
1496 [[ -n "${JAVA_PROVIDE}" ]] && echo "PROVIDES=\"${JAVA_PROVIDE}\""
1497 [[ -f "${JAVA_PKG_DEPEND}" ]] && echo "DEPEND=\"$(cat ${JAVA_PKG_DEPEND} | uniq | tr '\n' ':')\""
1498 echo "VM=\"$(echo ${RDEPEND} ${DEPEND} | sed -e 's/ /\n/g' | sed -n -e '/virtual\/\(jre\|jdk\)/ { p;q }')\"" # TODO cleanup !
1499 ) > "${JAVA_PKG_ENV}"
1500
1501 # register target/source
1502 local target="$(java-pkg_get-target)"
1503 local source="$(java-pkg_get-source)"
1504 [[ -n ${target} ]] && echo "TARGET=\"${target}\"" >> "${JAVA_PKG_ENV}"
1505 [[ -n ${source} ]] && echo "SOURCE=\"${source}\"" >> "${JAVA_PKG_ENV}"
1506
1507 # register javadoc info
1508 [[ -n ${JAVADOC_PATH} ]] && echo "JAVADOC_PATH=\"${JAVADOC_PATH}\"" \
1509 >> ${JAVA_PKG_ENV}
1510 # register source archives
1511 [[ -n ${JAVA_SOURCES} ]] && echo "JAVA_SOURCES=\"${JAVA_SOURCES}\"" \
1512 >> ${JAVA_PKG_ENV}
1513
1514
1515 echo "MERGE_VM=\"${GENTOO_VM}\"" >> "${JAVA_PKG_ENV}"
1516 [[ -n ${GENTOO_COMPILER} ]] && echo "MERGE_COMPILER=\"${GENTOO_COMPILER}\"" >> "${JAVA_PKG_ENV}"
1517
1518 # Strip unnecessary leading and trailing colons
1519 # TODO try to cleanup if possible
1520 sed -e "s/=\":/=\"/" -e "s/:\"$/\"/" -i "${JAVA_PKG_ENV}" || die "Did you forget to call java_init ?"
1521 fi
1522 }
1523
1524
1525 # ------------------------------------------------------------------------------
1526 # @internal-function java-pkg_record-jar_
1527 #
1528 # Record a dependency to the package.env
1529 #
1530 # ------------------------------------------------------------------------------
1531 JAVA_PKG_DEPEND="${T}/java-pkg-depend"
1532
1533 java-pkg_record-jar_() {
1534 debug-print-function ${FUNCNAME} $*
1535
1536 local pkg=${1} jar=${2} append
1537 if [[ -z "${jar}" ]]; then
1538 append="${pkg}"
1539 else
1540 append="$(basename ${jar})@${pkg}"
1541 fi
1542
1543 echo ${append} >> ${JAVA_PKG_DEPEND}
1544 }
1545
1546 # ------------------------------------------------------------------------------
1547 # @internal-function java-pkg_append_
1548 #
1549 # Appends a value to a variable
1550 #
1551 # Example: java-pkg_append_ CLASSPATH foo.jar
1552 # @param $1 variable name to modify
1553 # @param $2 value to append
1554 # ------------------------------------------------------------------------------
1555 java-pkg_append_() {
1556 debug-print-function ${FUNCNAME} $*
1557
1558 local var="${1}" value="${2}"
1559 if [[ -z "${!var}" ]] ; then
1560 export ${var}="${value}"
1561 else
1562 local oldIFS=${IFS} cur haveit
1563 IFS=':'
1564 for cur in ${!var}; do
1565 if [[ ${cur} == ${value} ]]; then
1566 haveit="yes"
1567 break
1568 fi
1569 done
1570 [[ -z ${haveit} ]] && export ${var}="${!var}:${value}"
1571 IFS=${oldIFS}
1572 fi
1573 }
1574
1575 # ------------------------------------------------------------------------------
1576 # @internal-function java-pkg_expand_dir_
1577 #
1578 # Gets the full path of the file/directory's parent.
1579 # @param $1 - file/directory to find parent directory for
1580 # @return - path to $1's parent directory
1581 # ------------------------------------------------------------------------------
1582 java-pkg_expand_dir_() {
1583 pushd "$(dirname "${1}")" >/dev/null 2>&1
1584 pwd
1585 popd >/dev/null 2>&1
1586 }
1587
1588 # ------------------------------------------------------------------------------
1589 # @internal-function java-pkg_func-exists
1590 #
1591 # Does the indicated function exist?
1592 #
1593 # @return 0 - function is declared
1594 # @return 1 - function is undeclared
1595 # ------------------------------------------------------------------------------
1596 java-pkg_func-exists() {
1597 if [[ -n "$(declare -f ${1})" ]]; then
1598 return 0
1599 else
1600 return 1
1601 fi
1602 }
1603
1604 # ------------------------------------------------------------------------------
1605 # @internal-function java-pkg_setup-vm
1606 #
1607 # Sets up the environment for a specific VM
1608 #
1609 # ------------------------------------------------------------------------------
1610 java-pkg_setup-vm() {
1611 debug-print-function ${FUNCNAME} $*
1612
1613 local vendor="$(java-pkg_get-vm-vendor)"
1614 if [[ "${vendor}" == "sun" ]] && java-pkg_is-vm-version-ge 1 5; then
1615 addpredict "/dev/random"
1616 elif [[ "${vendor}" == "ibm" ]]; then
1617 addpredict "/proc/self/maps"
1618 addpredict "/proc/cpuinfo"
1619 export LANG="C" LC_ALL="C"
1620 elif [[ "${vendor}" == "jrockit" ]]; then
1621 addpredict "/proc/cpuinfo"
1622 fi
1623 }
1624
1625 # ------------------------------------------------------------------------------
1626 # @internal-function java-pkg_needs-vm
1627 #
1628 # Does the current package depend on virtual/jdk?
1629 #
1630 # @return 0 - Package depends on virtual/jdk
1631 # @return 1 - Package does not depend on virtual/jdk
1632 # ------------------------------------------------------------------------------
1633 java-pkg_needs-vm() {
1634 debug-print-function ${FUNCNAME} $*
1635
1636 if [[ -n "$(echo ${DEPEND} | sed -e '\:virtual/jdk:!d')" ]]; then
1637 return 0
1638 fi
1639
1640 return 1
1641 }
1642
1643 # ------------------------------------------------------------------------------
1644 # @internal-function java-pkg_get-current-vm
1645 #
1646 # @return - The current VM being used
1647 # ------------------------------------------------------------------------------
1648 java-pkg_get-current-vm() {
1649 java-config -f
1650 }
1651
1652 # ------------------------------------------------------------------------------
1653 # @internal-function java-pkg_get-vm-vendor
1654 #
1655 # @return - The vendor of the current VM
1656 # ------------------------------------------------------------------------------
1657 java-pkg_get-vm-vendor() {
1658 debug-print-function ${FUNCNAME} $*
1659
1660 local vm="$(java-pkg_get-current-vm)"
1661 vm="${vm/-*/}"
1662 echo "${vm}"
1663 }
1664
1665 # ------------------------------------------------------------------------------
1666 # @internal-function java-pkg_get-vm-version
1667 #
1668 # @return - The version of the current VM
1669 # ------------------------------------------------------------------------------
1670 java-pkg_get-vm-version() {
1671 debug-print-function ${FUNCNAME} $*
1672
1673 java-pkg_get-current-vm | sed -e "s/.*-\([0-9.]\+\).*/\1/"
1674 }
1675
1676 # ------------------------------------------------------------------------------
1677 # @internal-function java-pkg_switch-vm
1678 #
1679 # Switch VM if we're allowed to (controlled by JAVA_PKG_ALLOW_VM_CHANGE), and
1680 # verify that the current VM is sufficient.
1681 # Setup the environment for the VM being used.
1682 # ------------------------------------------------------------------------------
1683 java-pkg_switch-vm() {
1684 if java-pkg_needs-vm; then
1685 # Use the VM specified by JAVA_PKG_FORCE_VM
1686 if [[ -n ${JAVA_PKG_FORCE_VM} ]]; then
1687 # If you're forcing the VM, I hope you know what your doing...
1688 export GENTOO_VM="${JAVA_PKG_FORCE_VM}"
1689 # if we're allowed to switch the vm...
1690 elif [[ "${JAVA_PKG_ALLOW_VM_CHANGE}" == "yes" ]]; then
1691 debug-print "depend-java-query: NV_DEPEND: ${JAVA_PKG_NV_DEPEND:-${DEPEND}} VNEED: ${JAVA_PKG_VNEED}"
1692 if [[ -n ${JAVA_PKG_VNEED} ]]; then
1693 export GENTOO_VM="$(depend-java-query --need-virtual "${JAVA_PKG_VNEED}" --get-vm "${JAVA_PKG_NV_DEPEND:-${DEPEND}}")"
1694 else
1695 export GENTOO_VM="$(depend-java-query --get-vm "${JAVA_PKG_NV_DEPEND:-${DEPEND}}")"
1696 fi
1697 # otherwise just make sure the current VM is sufficient
1698 else
1699 java-pkg_ensure-vm-version-sufficient
1700 fi
1701 debug-print "Using: $(java-config -f)"
1702
1703 java-pkg_setup-vm
1704
1705 export JAVA=$(java-config --java)
1706 export JAVAC=$(java-config --javac)
1707 export JAVACFLAGS="$(java-pkg_javac-args)"
1708 [[ -n ${JAVACFLAGS_EXTRA} ]] && export JAVACFLAGS="${JAVACFLAGS_EXTRA} ${JAVACFLAGS}"
1709
1710 export JAVA_HOME="$(java-config -g JAVA_HOME)"
1711 export JDK_HOME=${JAVA_HOME}
1712
1713 #TODO If you know a better solution let us know.
1714 java-pkg_append_ LD_LIBRARY_PATH "$(java-config -g LDPATH)"
1715
1716 local tann="${T}/announced-vm"
1717 if [[ -n "${JAVA_PKG_DEBUG}" ]] || [[ ! -f "${tann}" ]] ; then
1718 # Add a check for setup/preinst phase... to avoid duplicate outputs
1719 # for when FEATURES=buildpkg
1720 if [[ ${EBUILD_PHASE} != "setup" && ${EBUILD_PHASE} != "preinst" && ${EBUILD_PHASE} != "postinst" ]];
1721 then
1722 einfo "Using: $(java-config -f)"
1723 [[ ! -f "${tann}" ]] && touch "${tann}"
1724 fi
1725 fi
1726
1727 else
1728 [[ -n "${JAVA_PKG_DEBUG}" ]] && ewarn "!!! This package inherits java-pkg but doesn't depend on a JDK. -bin or broken dependency!!!"
1729 fi
1730 }
1731
1732 # ------------------------------------------------------------------------------
1733 # @internal-function java-pkg_die
1734 #
1735 # Enhanced die for Java packages, which displays some information that may be
1736 # useful for debugging bugs on bugzilla.
1737 # ------------------------------------------------------------------------------
1738 #register_die_hook java-pkg_die
1739 if ! hasq java-pkg_die ${EBUILD_DEATH_HOOKS}; then
1740 EBUILD_DEATH_HOOKS="${EBUILD_DEATH_HOOKS} java-pkg_die"
1741 fi
1742
1743 java-pkg_die() {
1744 echo "!!! When you file a bug report, please include the following information:" >&2
1745 echo "GENTOO_VM=${GENTOO_VM} CLASSPATH=\"${CLASSPATH}\" JAVA_HOME=\"${JAVA_HOME}\"" >&2
1746 echo "JAVACFLAGS=\"${JAVACFLAGS}\" COMPILER=\"${GENTOO_COMPILER}\"" >&2
1747 echo "and of course, the output of emerge --info" >&2
1748 }
1749
1750
1751 # TODO document
1752 # List jars in the source directory, ${S}
1753 java-pkg_jar-list() {
1754 if [[ -n "${JAVA_PKG_DEBUG}" ]]; then
1755 einfo "Linked Jars"
1756 find "${S}" -type l -name '*.jar' -print0 | xargs -0 -r -n 500 ls -ald | sed -e "s,${WORKDIR},\${WORKDIR},"
1757 einfo "Jars"
1758 find "${S}" -type f -name '*.jar' -print0 | xargs -0 -r -n 500 ls -ald | sed -e "s,${WORKDIR},\${WORKDIR},"
1759 einfo "Classes"
1760 find "${S}" -type f -name '*.class' -print0 | xargs -0 -r -n 500 ls -ald | sed -e "s,${WORKDIR},\${WORKDIR},"
1761 fi
1762 }
1763
1764 # TODO document
1765 # Verify that the classes were compiled for the right source / target
1766 java-pkg_verify-classes() {
1767 ebegin "Verifying java class versions"
1768 #$(find ${D} -type f -name '*.jar' -o -name '*.class')
1769 class-version-verify.py -t $(java-pkg_get-target) -r ${D}
1770 result=$?
1771 eend ${result}
1772 if [[ ${result} == 0 ]]; then
1773 einfo "All good"
1774 else
1775 ewarn "Possible problem"
1776 die "Bad class files found"
1777 fi
1778 }
1779
1780
1781 # ------------------------------------------------------------------------------
1782 # @section-end internal
1783 # ------------------------------------------------------------------------------
1784
1785 java-pkg_check-phase() {
1786 local phase=${1}
1787 local funcname=${2}
1788 # TODO add check for java-stricter
1789 if [[ ${EBUILD_PHASE} != ${phase} ]]; then
1790 java-pkg_announce-qa-violation \
1791 "${funcname} used outside of src_${phase}"
1792 fi
1793 }
1794
1795 java-pkg_check-versioned-jar() {
1796 local jar=${1}
1797
1798 if [[ ${jar} =~ ${PV} ]]; then
1799 java-pkg_announce-qa-violation "installing versioned jar '${jar}'"
1800 fi
1801 }
1802
1803 java-pkg_check-jikes() {
1804 if hasq jikes ${IUSE}; then
1805 java-pkg_announce-qa-violation "deprecated USE flag 'jikes' in IUSE"
1806 fi
1807 }
1808
1809 java-pkg_announce-qa-violation() {
1810 if hasq java-strict ${FEATURES}; then
1811 echo "Java QA Notice: $@" >&2
1812 increment-qa-violations
1813 fi
1814 }
1815
1816 increment-qa-violations() {
1817 let "JAVA_PKG_QA_VIOLATIONS+=1"
1818 export JAVA_PKG_QA_VIOLATIONS
1819 }
1820
1821 # ------------------------------------------------------------------------------
1822 # @eclass-end
1823 # ------------------------------------------------------------------------------

  ViewVC Help
Powered by ViewVC 1.1.20