1 # Copyright 1999-2016 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.
15 *) die "EAPI=${EAPI} is not supported" ;;
18 inherit fdo-mime multilib pax-utils prefix
20 EXPORT_FUNCTIONS pkg_setup pkg_postinst pkg_prerm pkg_postrm
23 >=dev-java/java-config-2.2.0-r3
24 >=app-eselect/eselect-java-0.2.0"
27 export WANT_JAVA_CONFIG=2
30 # @ECLASS-VARIABLE: JAVA_VM_CONFIG_DIR
33 # Where to place the vm env file.
34 JAVA_VM_CONFIG_DIR="/usr/share/java-config-2/vm"
36 # @ECLASS-VARIABLE: JAVA_VM_DIR
39 # Base directory for vm links.
40 JAVA_VM_DIR="/usr/lib/jvm"
42 # @ECLASS-VARIABLE: JAVA_VM_SYSTEM
46 JAVA_VM_SYSTEM="/etc/java-config-2/current-system-vm"
48 # @ECLASS-VARIABLE: JAVA_VM_BUILD_ONLY
50 # Set to YES to mark a vm as build-only.
51 JAVA_VM_BUILD_ONLY="${JAVA_VM_BUILD_ONLY:-FALSE}"
54 # @FUNCTION: java-vm-2_pkg_setup
58 # Initialize vm handle.
60 java-vm-2_pkg_setup() {
61 if [[ "${SLOT}" != "0" ]]; then
62 VMHANDLE=${PN}-${SLOT}
69 # @FUNCTION: java-vm-2_pkg_postinst
71 # default pkg_postinst
73 # Set the generation-2 system VM, if it isn't set or the setting is
74 # invalid. Also update mime database.
76 java-vm-2_pkg_postinst() {
77 # Note that we cannot rely on java-config here, as it will silently recognize
78 # e.g. icedtea6-bin as valid system VM if icedtea6 is set but invalid (e.g. due
79 # to the migration to icedtea-6)
80 if [[ ! -L "${EROOT}${JAVA_VM_SYSTEM}" ]]; then
83 local current_vm_path=$(readlink "${EROOT}${JAVA_VM_SYSTEM}")
84 local current_vm=$(basename "${ROOT}${current_vm_path}")
85 if [[ ! -L "${EROOT}${JAVA_VM_DIR}/${current_vm}" ]]; then
90 fdo-mime_desktop_database_update
94 # @FUNCTION: java-vm-2_pkg_prerm
98 # Warn user if removing system-vm.
100 java-vm-2_pkg_prerm() {
101 if [[ "$(GENTOO_VM="" java-config -f 2>/dev/null)" == "${VMHANDLE}" && -z "${REPLACED_BY_VERSION}" ]]; then
102 ewarn "It appears you are removing your system-vm!"
103 ewarn "Please run java-config -L to list available VMs,"
104 ewarn "then use java-config -S to set a new system-vm!"
109 # @FUNCTION: java-vm-2_pkg_postrm
113 # Update mime database.
115 java-vm-2_pkg_postrm() {
116 fdo-mime_desktop_database_update
120 # @FUNCTION: java_set_default_vm_
125 java_set_default_vm_() {
126 java-config-2 --set-system-vm="${VMHANDLE}"
128 einfo " ${P} set as the default system-vm."
132 # @FUNCTION: get_system_arch
134 # Get Java specific arch name.
136 # NOTE the mips and sparc values are best guesses. Oracle uses sparcv9
137 # but does OpenJDK use sparc64? We don't support OpenJDK on sparc or any
138 # JVM on mips though so it doesn't matter much.
141 local abi=${1-${ABI}}
143 case $(get_abi_CHOST ${abi}) in
144 mips*l*) echo mipsel ;;
146 ppc64le*) echo ppc64le ;;
149 *_fbsd) get_system_arch ${abi%_fbsd} ;;
150 arm64) echo aarch64 ;;
152 sparc32) echo sparc ;;
153 sparc64) echo sparcv9 ;;
161 # @FUNCTION: set_java_env
163 # Installs a vm env file.
164 # DEPRECATED, use java-vm_install-env instead.
167 debug-print-function ${FUNCNAME} $*
169 local platform="$(get_system_arch)"
170 local env_file="${ED}${JAVA_VM_CONFIG_DIR}/${VMHANDLE}"
173 local source_env_file="${1}"
175 local source_env_file="${FILESDIR}/${VMHANDLE}.env"
178 if [[ ! -f ${source_env_file} ]]; then
179 die "Unable to find the env file: ${source_env_file}"
182 dodir ${JAVA_VM_CONFIG_DIR}
185 -e "s/@PN@/${PN}/g" \
186 -e "s/@PV@/${PV}/g" \
187 -e "s/@PF@/${PF}/g" \
188 -e "s/@SLOT@/${SLOT}/g" \
189 -e "s/@PLATFORM@/${platform}/g" \
190 -e "s/@LIBDIR@/$(get_libdir)/g" \
191 -e "/^LDPATH=.*lib\\/\\\"/s|\"\\(.*\\)\"|\"\\1${platform}/:\\1${platform}/server/\"|" \
192 < "${source_env_file}" \
193 > "${env_file}" || die "sed failed"
196 echo "VMHANDLE=\"${VMHANDLE}\""
197 echo "BUILD_ONLY=\"${JAVA_VM_BUILD_ONLY}\""
200 eprefixify ${env_file}
202 [[ -n ${JAVA_PROVIDE} ]] && echo "PROVIDES=\"${JAVA_PROVIDE}\"" >> ${env_file}
204 local java_home=$(source "${env_file}"; echo ${JAVA_HOME})
205 [[ -z ${java_home} ]] && die "No JAVA_HOME defined in ${env_file}"
208 dodir "${JAVA_VM_DIR}"
209 dosym ${java_home#${EPREFIX}} ${JAVA_VM_DIR}/${VMHANDLE}
213 # @FUNCTION: java-vm_install-env
216 # Installs a Java VM environment file. The source can be specified but
217 # defaults to ${FILESDIR}/${VMHANDLE}.env.sh.
219 # Environment variables within this file will be resolved. You should
220 # escape the $ when referring to variables that should be resolved later
221 # such as ${JAVA_HOME}. Subshells may be used but avoid using double
222 # quotes. See icedtea-bin.env.sh for a good example.
224 java-vm_install-env() {
225 debug-print-function ${FUNCNAME} "$*"
227 local env_file="${ED}${JAVA_VM_CONFIG_DIR}/${VMHANDLE}"
228 local source_env_file="${1-${FILESDIR}/${VMHANDLE}.env.sh}"
230 if [[ ! -f "${source_env_file}" ]]; then
231 die "Unable to find the env file: ${source_env_file}"
234 dodir "${JAVA_VM_CONFIG_DIR}"
236 # Here be dragons! ;) -- Chewi
237 eval echo "\"$(cat <<< "$(sed 's:":\\":g' "${source_env_file}")")\"" > "${env_file}" ||
238 die "failed to create Java env file"
241 echo "VMHANDLE=\"${VMHANDLE}\""
242 echo "BUILD_ONLY=\"${JAVA_VM_BUILD_ONLY}\""
243 [[ ${JAVA_PROVIDE} ]] && echo "PROVIDES=\"${JAVA_PROVIDE}\"" || true
244 ) >> "${env_file}" || die "failed to append to Java env file"
246 local java_home=$(unset JAVA_HOME; source "${env_file}"; echo ${JAVA_HOME})
247 [[ -z ${java_home} ]] && die "No JAVA_HOME defined in ${env_file}"
250 dodir "${JAVA_VM_DIR}"
251 dosym "${java_home#${EPREFIX}}" "${JAVA_VM_DIR}/${VMHANDLE}"
255 # @FUNCTION: java-vm_set-pax-markings
257 # Set PaX markings on all JDK/JRE executables to allow code-generation on
258 # the heap by the JIT compiler.
260 # The markings need to be set prior to the first invocation of the the freshly
261 # built / installed VM. Be it before creating the Class Data Sharing archive or
262 # generating cacerts. Otherwise a PaX enabled kernel will kill the VM.
263 # Bug #215225 #389751
267 # $1 - JDK/JRE base directory.
270 # java-vm_set-pax-markings "${S}"
271 # java-vm_set-pax-markings "${ED}"/opt/${P}
274 java-vm_set-pax-markings() {
275 debug-print-function ${FUNCNAME} "$*"
276 [[ $# -ne 1 ]] && die "${FUNCNAME}: takes exactly one argument"
277 [[ ! -f "${1}"/bin/java ]] \
278 && die "${FUNCNAME}: argument needs to be JDK/JRE base directory"
280 local executables=( "${1}"/bin/* )
281 [[ -d "${1}"/jre ]] && executables+=( "${1}"/jre/bin/* )
283 # Ensure a PaX header is created.
284 local pax_markings="C"
285 # Usally disabeling MPROTECT is sufficent.
286 local pax_markings+="m"
287 # On x86 for heap sizes over 700MB disable SEGMEXEC and PAGEEXEC as well.
288 use x86 && pax_markings+="sp"
290 pax-mark ${pax_markings} $(list-paxables "${executables[@]}")
294 # @FUNCTION: java-vm_revdep-mask
296 # Installs a revdep-rebuild control file which SEARCH_DIR_MASK set to the path
297 # where the VM is installed. Prevents pointless rebuilds - see bug #177925.
298 # Also gives a notice to the user.
302 # $1 - Path of the VM (defaults to /opt/${P} if not set)
305 # java-vm_revdep-mask
306 # java-vm_revdep-mask /path/to/jdk/
310 java-vm_revdep-mask() {
311 debug-print-function ${FUNCNAME} "$*"
313 local VMROOT="${1-"${EPREFIX}"/opt/${P}}"
315 dodir /etc/revdep-rebuild
316 echo "SEARCH_DIRS_MASK=\"${VMROOT}\"" >> "${ED}/etc/revdep-rebuild/61-${VMHANDLE}" \
317 || die "Failed to write revdep-rebuild mask file"
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 local path path_arr=("$@")
336 # subshell this to prevent IFS bleeding out dependant on bash version.
337 # could use local, which *should* work, but that requires a lot of testing.
338 path=$(IFS=":"; echo "${path_arr[*]}")
340 echo "SANDBOX_PREDICT=\"${path}\"" > "${ED}/etc/sandbox.d/20${VMHANDLE}" \
341 || die "Failed to write sandbox control file"