/[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.6 - (hide annotations) (download)
Tue Jul 18 14:17:10 2006 UTC (8 years ago) by nichoj
Branch: MAIN
Changes since 1.5: +4 -2 lines
Added a temporary fix for a bug in ejavac

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

  ViewVC Help
Powered by ViewVC 1.1.20