1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
5 # @ECLASS: java-vm-2.eclass
8 # @BLURB: Java Virtual Machine eclass
10 # This eclass provides functionality which assists with installing
11 # virtual machines, and ensures that they are recognized by java-config.
13 inherit eutils fdo-mime multilib pax-utils prefix
15 EXPORT_FUNCTIONS pkg_setup pkg_postinst pkg_prerm pkg_postrm
18 >=dev-java/java-config-2.2.0
19 app-eselect/eselect-java"
21 has "${EAPI}" 0 1 && DEPEND="${DEPEND} >=sys-apps/portage-2.1"
23 export WANT_JAVA_CONFIG=2
26 # @ECLASS-VARIABLE: JAVA_VM_CONFIG_DIR
29 # Where to place the vm env file.
30 JAVA_VM_CONFIG_DIR="/usr/share/java-config-2/vm"
32 # @ECLASS-VARIABLE: JAVA_VM_DIR
35 # Base directory for vm links.
36 JAVA_VM_DIR="/usr/lib/jvm"
38 # @ECLASS-VARIABLE: JAVA_VM_SYSTEM
42 JAVA_VM_SYSTEM="/etc/java-config-2/current-system-vm"
44 # @ECLASS-VARIABLE: JAVA_VM_BUILD_ONLY
46 # Set to YES to mark a vm as build-only.
47 JAVA_VM_BUILD_ONLY="${JAVA_VM_BUILD_ONLY:-FALSE}"
50 # @FUNCTION: java-vm-2_pkg_setup
54 # Initialize vm handle.
56 java-vm-2_pkg_setup() {
57 if [[ "${SLOT}" != "0" ]]; then
58 VMHANDLE=${PN}-${SLOT}
65 # @FUNCTION: java-vm-2_pkg_postinst
67 # default pkg_postinst
69 # Set the generation-2 system VM and Java plugin, if it isn't set or the
70 # setting is invalid. Also update mime database.
72 java-vm-2_pkg_postinst() {
73 has ${EAPI:-0} 0 1 2 && ! use prefix && EROOT=${ROOT}
74 # Note that we cannot rely on java-config here, as it will silently recognize
75 # e.g. icedtea6-bin as valid system VM if icedtea6 is set but invalid (e.g. due
76 # to the migration to icedtea-6)
77 if [[ ! -L "${EROOT}${JAVA_VM_SYSTEM}" ]]; then
80 local current_vm_path=$(readlink "${EROOT}${JAVA_VM_SYSTEM}")
81 local current_vm=$(basename "${ROOT}${current_vm_path}")
82 if [[ ! -L "${EROOT}${JAVA_VM_DIR}/${current_vm}" ]]; then
87 java-vm_check-nsplugin
89 fdo-mime_desktop_database_update
93 # @FUNCTION: java-vm_check-nsplugin
96 # Check if the nsplugin needs updating
98 java-vm_check-nsplugin() {
100 if [[ ${VMHANDLE} =~ emul-linux-x86 ]]; then
106 has ${EAPI:-0} 0 1 2 && ! use prefix && EPREFIX=
108 # Install a default nsplugin if we don't already have one
109 if in_iuse nsplugin && use nsplugin; then
110 if [[ ! -f "${ROOT}${EPREFIX}"/usr/${libdir}/nsbrowser/plugins/javaplugin.so ]]; then
111 einfo "No system nsplugin currently set."
114 einfo "System nsplugin is already set, not changing it."
116 einfo "You can change nsplugin with eselect java-nsplugin."
121 # @FUNCTION: java-vm_set-nsplugin
124 # Set the nsplugin implemetation.
126 java-vm_set-nsplugin() {
129 if [[ ${VMHANDLE} =~ emul-linux-x86 ]]; then
134 einfo "Setting ${extra_args} nsplugin to ${VMHANDLE}"
136 einfo "Setting nsplugin to ${VMHANDLE}..."
138 eselect java-nsplugin set ${extra_args} ${VMHANDLE}
142 # @FUNCTION: java-vm-2_pkg_prerm
146 # Warn user if removing system-vm.
148 java-vm-2_pkg_prerm() {
149 # Although REPLACED_BY_VERSION is EAPI=4, we shouldn't need to check EAPI for this use case
150 if [[ "$(GENTOO_VM="" java-config -f 2>/dev/null)" == "${VMHANDLE}" && -z "${REPLACED_BY_VERSION}" ]]; then
151 ewarn "It appears you are removing your system-vm!"
152 ewarn "Please run java-config -L to list available VMs,"
153 ewarn "then use java-config -S to set a new system-vm!"
158 # @FUNCTION: java-vm-2_pkg_postrm
162 # Update mime database.
164 java-vm-2_pkg_postrm() {
165 fdo-mime_desktop_database_update
169 # @FUNCTION: java_set_default_vm_
174 java_set_default_vm_() {
175 java-config-2 --set-system-vm="${VMHANDLE}"
177 einfo " ${P} set as the default system-vm."
181 # @FUNCTION: get_system_arch
183 # Get Java specific arch name.
187 sarch=$(echo ${ARCH} | sed -e s/[i]*.86/i386/ -e s/x86_64/amd64/ -e s/sun4u/sparc/ -e s/sparc64/sparc/ -e s/arm.*/arm/ -e s/sa110/arm/)
188 if [ -z "${sarch}" ]; then
189 sarch=$(uname -m | sed -e s/[i]*.86/i386/ -e s/x86_64/amd64/ -e s/sun4u/sparc/ -e s/sparc64/sparc/ -e s/arm.*/arm/ -e s/sa110/arm/)
195 # @FUNCTION: set_java_env
197 # Installs a vm env file.
199 # TODO rename to something more evident, like install_env_file
201 debug-print-function ${FUNCNAME} $*
203 if has ${EAPI:-0} 0 1 2 && ! use prefix ; then
208 local platform="$(get_system_arch)"
209 local env_file="${ED}${JAVA_VM_CONFIG_DIR}/${VMHANDLE}"
210 local old_env_file="${ED}/etc/env.d/java/20${P}"
212 local source_env_file="${1}"
214 local source_env_file="${FILESDIR}/${VMHANDLE}.env"
217 if [[ ! -f ${source_env_file} ]]; then
218 die "Unable to find the env file: ${source_env_file}"
221 dodir ${JAVA_VM_CONFIG_DIR}
224 -e "s/@PN@/${PN}/g" \
225 -e "s/@PV@/${PV}/g" \
226 -e "s/@PF@/${PF}/g" \
227 -e "s/@SLOT@/${SLOT}/g" \
228 -e "s/@PLATFORM@/${platform}/g" \
229 -e "s/@LIBDIR@/$(get_libdir)/g" \
230 -e "/^LDPATH=.*lib\\/\\\"/s|\"\\(.*\\)\"|\"\\1${platform}/:\\1${platform}/server/\"|" \
231 < "${source_env_file}" \
232 > "${env_file}" || die "sed failed"
235 echo "VMHANDLE=\"${VMHANDLE}\""
236 echo "BUILD_ONLY=\"${JAVA_VM_BUILD_ONLY}\""
239 eprefixify ${env_file}
241 [[ -n ${JAVA_PROVIDE} ]] && echo "PROVIDES=\"${JAVA_PROVIDE}\"" >> ${env_file}
243 local java_home=$(source "${env_file}"; echo ${JAVA_HOME})
244 [[ -z ${java_home} ]] && die "No JAVA_HOME defined in ${env_file}"
247 dodir "${JAVA_VM_DIR}"
248 dosym ${java_home#${EPREFIX}} ${JAVA_VM_DIR}/${VMHANDLE} \
249 || die "Failed to make VM symlink at ${JAVA_VM_DIR}/${VMHANDLE}"
253 # @FUNCTION: java-vm_set-pax-markings
255 # Set PaX markings on all JDK/JRE executables to allow code-generation on
256 # the heap by the JIT compiler.
258 # The markings need to be set prior to the first invocation of the the freshly
259 # built / installed VM. Be it before creating the Class Data Sharing archive or
260 # generating cacerts. Otherwise a PaX enabled kernel will kill the VM.
261 # Bug #215225 #389751
265 # $1 - JDK/JRE base directory.
268 # java-vm_set-pax-markings "${S}"
269 # java-vm_set-pax-markings "${ED}"/opt/${P}
272 java-vm_set-pax-markings() {
273 debug-print-function ${FUNCNAME} "$*"
274 [[ $# -ne 1 ]] && die "${FUNCNAME}: takes exactly one argument"
275 [[ ! -f "${1}"/bin/java ]] \
276 && die "${FUNCNAME}: argument needs to be JDK/JRE base directory"
278 local executables=( "${1}"/bin/* )
279 [[ -d "${1}"/jre ]] && executables+=( "${1}"/jre/bin/* )
281 # Ensure a PaX header is created.
282 local pax_markings="C"
283 # Usally disabeling MPROTECT is sufficent.
284 local pax_markings+="m"
285 # On x86 for heap sizes over 700MB disable SEGMEXEC and PAGEEXEC as well.
286 use x86 && pax_markings+="sp"
288 pax-mark ${pax_markings} $(list-paxables "${executables[@]}")
292 # @FUNCTION: java-vm_revdep-mask
294 # Installs a revdep-rebuild control file which SEARCH_DIR_MASK set to the path
295 # where the VM is installed. Prevents pointless rebuilds - see bug #177925.
296 # Also gives a notice to the user.
300 # $1 - Path of the VM (defaults to /opt/${P} if not set)
303 # java-vm_revdep-mask
304 # java-vm_revdep-mask /path/to/jdk/
308 java-vm_revdep-mask() {
309 if has ${EAPI:-0} 0 1 2 && ! use prefix; then
314 local VMROOT="${1-"${EPREFIX}"/opt/${P}}"
316 dodir /etc/revdep-rebuild/
317 echo "SEARCH_DIRS_MASK=\"${VMROOT}\""> "${ED}/etc/revdep-rebuild/61-${VMHANDLE}"
321 # @FUNCTION: java-vm_sandbox-predict
323 # Install a sandbox control file. Specified paths won't cause a sandbox
324 # violation if opened read write but no write takes place. See bug 388937#c1
328 # java-vm_sandbox-predict /dev/random /proc/self/coredump_filter
331 java-vm_sandbox-predict() {
332 debug-print-function ${FUNCNAME} "$*"
333 [[ -z "${1}" ]] && die "${FUNCNAME} takes at least one argument"
335 has ${EAPI:-0} 0 1 2 && ! use prefix && ED="${D}"
337 local path path_arr=("$@")
338 # subshell this to prevent IFS bleeding out dependant on bash version.
339 # could use local, which *should* work, but that requires a lot of testing.
340 path=$(IFS=":"; echo "${path_arr[*]}")
342 echo "SANDBOX_PREDICT=\"${path}\"" > "${ED}/etc/sandbox.d/20${VMHANDLE}" \
343 || die "Failed to write sandbox control file"
347 # @FUNCTION: java_get_plugin_dir_
350 # Get the java plugin dir.
352 java_get_plugin_dir_() {
353 has ${EAPI:-0} 0 1 2 && ! use prefix && EPREFIX=
354 echo "${EPREFIX}"/usr/$(get_libdir)/nsbrowser/plugins
358 # @FUNCTION: install_mozilla_plugin
360 # Register a netscape java-plugin.
362 install_mozilla_plugin() {
366 has ${EAPI:-0} 0 1 2 && ! use prefix && ED="${D}"
367 if [[ ! -f "${ED}/${plugin}" ]]; then
368 die "Cannot find mozilla plugin at ${ED}/${plugin}"
371 if [[ -n "${variant}" ]]; then
372 variant="-${variant}"
375 local plugin_dir="/usr/share/java-config-2/nsplugin"
376 dodir "${plugin_dir}"
377 dosym "${plugin}" "${plugin_dir}/${VMHANDLE}${variant}-javaplugin.so"
381 # @FUNCTION: java_mozilla_clean_
384 # Because previously some ebuilds installed symlinks outside of pkg_install
385 # and are left behind, which forces you to manualy remove them to select the
386 # jdk/jre you want to use for java
388 java_mozilla_clean_() {
389 local plugin_dir=$(java_get_plugin_dir_)
390 for file in ${plugin_dir}/javaplugin_*; do
393 for file in ${plugin_dir}/libjavaplugin*; do