1 # Copyright 1999-2017 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: java-vm-2.eclass
7 # @BLURB: Java Virtual Machine eclass
9 # This eclass provides functionality which assists with installing
10 # virtual machines, and ensures that they are recognized by java-config.
14 *) die "EAPI=${EAPI} is not supported" ;;
17 inherit fdo-mime multilib pax-utils prefix
19 EXPORT_FUNCTIONS pkg_setup pkg_postinst pkg_prerm pkg_postrm
22 >=dev-java/java-config-2.2.0-r3
23 >=app-eselect/eselect-java-0.2.0"
26 export WANT_JAVA_CONFIG=2
29 # @ECLASS-VARIABLE: JAVA_VM_CONFIG_DIR
32 # Where to place the vm env file.
33 JAVA_VM_CONFIG_DIR="/usr/share/java-config-2/vm"
35 # @ECLASS-VARIABLE: JAVA_VM_DIR
38 # Base directory for vm links.
39 JAVA_VM_DIR="/usr/lib/jvm"
41 # @ECLASS-VARIABLE: JAVA_VM_SYSTEM
45 JAVA_VM_SYSTEM="/etc/java-config-2/current-system-vm"
47 # @ECLASS-VARIABLE: JAVA_VM_BUILD_ONLY
49 # Set to YES to mark a vm as build-only.
50 JAVA_VM_BUILD_ONLY="${JAVA_VM_BUILD_ONLY:-FALSE}"
53 # @FUNCTION: java-vm-2_pkg_setup
57 # Initialize vm handle.
59 java-vm-2_pkg_setup() {
60 if [[ "${SLOT}" != "0" ]]; then
61 VMHANDLE=${PN}-${SLOT}
68 # @FUNCTION: java-vm-2_pkg_postinst
70 # default pkg_postinst
72 # Set the generation-2 system VM, if it isn't set or the setting is
73 # invalid. Also update mime database.
75 java-vm-2_pkg_postinst() {
76 # Note that we cannot rely on java-config here, as it will silently recognize
77 # e.g. icedtea6-bin as valid system VM if icedtea6 is set but invalid (e.g. due
78 # to the migration to icedtea-6)
79 if [[ ! -L "${EROOT}${JAVA_VM_SYSTEM}" ]]; then
82 local current_vm_path=$(readlink "${EROOT}${JAVA_VM_SYSTEM}")
83 local current_vm=$(basename "${ROOT}${current_vm_path}")
84 if [[ ! -L "${EROOT}${JAVA_VM_DIR}/${current_vm}" ]]; then
89 fdo-mime_desktop_database_update
93 # @FUNCTION: java-vm-2_pkg_prerm
97 # Warn user if removing system-vm.
99 java-vm-2_pkg_prerm() {
100 if [[ "$(GENTOO_VM="" java-config -f 2>/dev/null)" == "${VMHANDLE}" && -z "${REPLACED_BY_VERSION}" ]]; then
101 ewarn "It appears you are removing your system-vm!"
102 ewarn "Please run java-config -L to list available VMs,"
103 ewarn "then use java-config -S to set a new system-vm!"
108 # @FUNCTION: java-vm-2_pkg_postrm
112 # Update mime database.
114 java-vm-2_pkg_postrm() {
115 fdo-mime_desktop_database_update
119 # @FUNCTION: java_set_default_vm_
124 java_set_default_vm_() {
125 java-config-2 --set-system-vm="${VMHANDLE}"
127 einfo " ${P} set as the default system-vm."
131 # @FUNCTION: get_system_arch
133 # Get Java specific arch name.
135 # NOTE the mips and sparc values are best guesses. Oracle uses sparcv9
136 # but does OpenJDK use sparc64? We don't support OpenJDK on sparc or any
137 # JVM on mips though so it doesn't matter much.
140 local abi=${1-${ABI}}
142 case $(get_abi_CHOST ${abi}) in
143 mips*l*) echo mipsel ;;
145 ppc64le*) echo ppc64le ;;
148 *_fbsd) get_system_arch ${abi%_fbsd} ;;
149 arm64) echo aarch64 ;;
151 sparc32) echo sparc ;;
152 sparc64) echo sparcv9 ;;
160 # @FUNCTION: set_java_env
162 # Installs a vm env file.
163 # DEPRECATED, use java-vm_install-env instead.
166 debug-print-function ${FUNCNAME} $*
168 local platform="$(get_system_arch)"
169 local env_file="${ED}${JAVA_VM_CONFIG_DIR}/${VMHANDLE}"
172 local source_env_file="${1}"
174 local source_env_file="${FILESDIR}/${VMHANDLE}.env"
177 if [[ ! -f ${source_env_file} ]]; then
178 die "Unable to find the env file: ${source_env_file}"
181 dodir ${JAVA_VM_CONFIG_DIR}
184 -e "s/@PN@/${PN}/g" \
185 -e "s/@PV@/${PV}/g" \
186 -e "s/@PF@/${PF}/g" \
187 -e "s/@SLOT@/${SLOT}/g" \
188 -e "s/@PLATFORM@/${platform}/g" \
189 -e "s/@LIBDIR@/$(get_libdir)/g" \
190 -e "/^LDPATH=.*lib\\/\\\"/s|\"\\(.*\\)\"|\"\\1${platform}/:\\1${platform}/server/\"|" \
191 < "${source_env_file}" \
192 > "${env_file}" || die "sed failed"
195 echo "VMHANDLE=\"${VMHANDLE}\""
196 echo "BUILD_ONLY=\"${JAVA_VM_BUILD_ONLY}\""
199 eprefixify ${env_file}
201 [[ -n ${JAVA_PROVIDE} ]] && echo "PROVIDES=\"${JAVA_PROVIDE}\"" >> ${env_file}
203 local java_home=$(source "${env_file}"; echo ${JAVA_HOME})
204 [[ -z ${java_home} ]] && die "No JAVA_HOME defined in ${env_file}"
207 dodir "${JAVA_VM_DIR}"
208 dosym ${java_home#${EPREFIX}} ${JAVA_VM_DIR}/${VMHANDLE}
212 # @FUNCTION: java-vm_install-env
215 # Installs a Java VM environment file. The source can be specified but
216 # defaults to ${FILESDIR}/${VMHANDLE}.env.sh.
218 # Environment variables within this file will be resolved. You should
219 # escape the $ when referring to variables that should be resolved later
220 # such as ${JAVA_HOME}. Subshells may be used but avoid using double
221 # quotes. See icedtea-bin.env.sh for a good example.
223 java-vm_install-env() {
224 debug-print-function ${FUNCNAME} "$*"
226 local env_file="${ED}${JAVA_VM_CONFIG_DIR}/${VMHANDLE}"
227 local source_env_file="${1-${FILESDIR}/${VMHANDLE}.env.sh}"
229 if [[ ! -f "${source_env_file}" ]]; then
230 die "Unable to find the env file: ${source_env_file}"
233 dodir "${JAVA_VM_CONFIG_DIR}"
235 # Here be dragons! ;) -- Chewi
236 eval echo "\"$(cat <<< "$(sed 's:":\\":g' "${source_env_file}")")\"" > "${env_file}" ||
237 die "failed to create Java env file"
240 echo "VMHANDLE=\"${VMHANDLE}\""
241 echo "BUILD_ONLY=\"${JAVA_VM_BUILD_ONLY}\""
242 [[ ${JAVA_PROVIDE} ]] && echo "PROVIDES=\"${JAVA_PROVIDE}\"" || true
243 ) >> "${env_file}" || die "failed to append to Java env file"
245 local java_home=$(unset JAVA_HOME; source "${env_file}"; echo ${JAVA_HOME})
246 [[ -z ${java_home} ]] && die "No JAVA_HOME defined in ${env_file}"
249 dodir "${JAVA_VM_DIR}"
250 dosym "${java_home#${EPREFIX}}" "${JAVA_VM_DIR}/${VMHANDLE}"
254 # @FUNCTION: java-vm_set-pax-markings
256 # Set PaX markings on all JDK/JRE executables to allow code-generation on
257 # the heap by the JIT compiler.
259 # The markings need to be set prior to the first invocation of the the freshly
260 # built / installed VM. Be it before creating the Class Data Sharing archive or
261 # generating cacerts. Otherwise a PaX enabled kernel will kill the VM.
262 # Bug #215225 #389751
266 # $1 - JDK/JRE base directory.
269 # java-vm_set-pax-markings "${S}"
270 # java-vm_set-pax-markings "${ED}"/opt/${P}
273 java-vm_set-pax-markings() {
274 debug-print-function ${FUNCNAME} "$*"
275 [[ $# -ne 1 ]] && die "${FUNCNAME}: takes exactly one argument"
276 [[ ! -f "${1}"/bin/java ]] \
277 && die "${FUNCNAME}: argument needs to be JDK/JRE base directory"
279 local executables=( "${1}"/bin/* )
280 [[ -d "${1}"/jre ]] && executables+=( "${1}"/jre/bin/* )
282 # Usually disabling MPROTECT is sufficient.
283 local pax_markings="m"
284 # On x86 for heap sizes over 700MB disable SEGMEXEC and PAGEEXEC as well.
285 use x86 && pax_markings+="sp"
287 pax-mark ${pax_markings} $(list-paxables "${executables[@]}")
291 # @FUNCTION: java-vm_revdep-mask
293 # Installs a revdep-rebuild control file which SEARCH_DIR_MASK set to the path
294 # where the VM is installed. Prevents pointless rebuilds - see bug #177925.
295 # Also gives a notice to the user.
299 # $1 - Path of the VM (defaults to /opt/${P} if not set)
302 # java-vm_revdep-mask
303 # java-vm_revdep-mask /path/to/jdk/
307 java-vm_revdep-mask() {
308 debug-print-function ${FUNCNAME} "$*"
310 local VMROOT="${1-"${EPREFIX}"/opt/${P}}"
312 dodir /etc/revdep-rebuild
313 echo "SEARCH_DIRS_MASK=\"${VMROOT}\"" >> "${ED}/etc/revdep-rebuild/61-${VMHANDLE}" \
314 || die "Failed to write revdep-rebuild mask file"
318 # @FUNCTION: java-vm_sandbox-predict
320 # Install a sandbox control file. Specified paths won't cause a sandbox
321 # violation if opened read write but no write takes place. See bug 388937#c1
325 # java-vm_sandbox-predict /dev/random /proc/self/coredump_filter
328 java-vm_sandbox-predict() {
329 debug-print-function ${FUNCNAME} "$*"
330 [[ -z "${1}" ]] && die "${FUNCNAME} takes at least one argument"
332 local path path_arr=("$@")
333 # subshell this to prevent IFS bleeding out dependant on bash version.
334 # could use local, which *should* work, but that requires a lot of testing.
335 path=$(IFS=":"; echo "${path_arr[*]}")
337 echo "SANDBOX_PREDICT=\"${path}\"" > "${ED}/etc/sandbox.d/20${VMHANDLE}" \
338 || die "Failed to write sandbox control file"