1 # Copyright 1999-2018 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: java-vm-2.eclass
7 # @SUPPORTED_EAPIS: 5 6
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 multilib pax-utils prefix xdg-utils
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.4.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 if [[ ! -d ${EROOT}${JAVA_VM_SYSTEM} ]]; then
78 eselect java-vm set system "${VMHANDLE}"
79 einfo "${P} set as the default system-vm."
82 xdg_desktop_database_update
86 # @FUNCTION: java-vm-2_pkg_prerm
90 # Warn user if removing system-vm.
92 java-vm-2_pkg_prerm() {
93 if [[ $(GENTOO_VM= java-config -f 2>/dev/null) == ${VMHANDLE} && -z ${REPLACED_BY_VERSION} ]]; then
94 ewarn "It appears you are removing your system-vm! Please run"
95 ewarn "\"eselect java-vm list\" to list available VMs, then use"
96 ewarn "\"eselect java-vm set system\" to set a new system-vm!"
101 # @FUNCTION: java-vm-2_pkg_postrm
105 # Update mime database.
107 java-vm-2_pkg_postrm() {
108 xdg_desktop_database_update
112 # @FUNCTION: get_system_arch
114 # Get Java specific arch name.
116 # NOTE the mips and sparc values are best guesses. Oracle uses sparcv9
117 # but does OpenJDK use sparc64? We don't support OpenJDK on sparc or any
118 # JVM on mips though so it doesn't matter much.
121 local abi=${1-${ABI}}
123 case $(get_abi_CHOST ${abi}) in
124 mips*l*) echo mipsel ;;
126 powerpc64le*) echo ppc64le ;;
129 *_fbsd) get_system_arch ${abi%_fbsd} ;;
130 arm64) echo aarch64 ;;
132 sparc32) echo sparc ;;
133 sparc64) echo sparcv9 ;;
141 # @FUNCTION: set_java_env
143 # Installs a vm env file.
144 # DEPRECATED, use java-vm_install-env instead.
147 debug-print-function ${FUNCNAME} $*
149 local platform="$(get_system_arch)"
150 local env_file="${ED}${JAVA_VM_CONFIG_DIR}/${VMHANDLE}"
153 local source_env_file="${1}"
155 local source_env_file="${FILESDIR}/${VMHANDLE}.env"
158 if [[ ! -f ${source_env_file} ]]; then
159 die "Unable to find the env file: ${source_env_file}"
162 dodir ${JAVA_VM_CONFIG_DIR}
165 -e "s/@PN@/${PN}/g" \
166 -e "s/@PV@/${PV}/g" \
167 -e "s/@PF@/${PF}/g" \
168 -e "s/@SLOT@/${SLOT}/g" \
169 -e "s/@PLATFORM@/${platform}/g" \
170 -e "s/@LIBDIR@/$(get_libdir)/g" \
171 -e "/^LDPATH=.*lib\\/\\\"/s|\"\\(.*\\)\"|\"\\1${platform}/:\\1${platform}/server/\"|" \
172 < "${source_env_file}" \
173 > "${env_file}" || die "sed failed"
176 echo "VMHANDLE=\"${VMHANDLE}\""
177 echo "BUILD_ONLY=\"${JAVA_VM_BUILD_ONLY}\""
180 eprefixify ${env_file}
182 [[ -n ${JAVA_PROVIDE} ]] && echo "PROVIDES=\"${JAVA_PROVIDE}\"" >> ${env_file}
184 local java_home=$(source "${env_file}"; echo ${JAVA_HOME})
185 [[ -z ${java_home} ]] && die "No JAVA_HOME defined in ${env_file}"
188 dodir "${JAVA_VM_DIR}"
189 dosym ${java_home#${EPREFIX}} ${JAVA_VM_DIR}/${VMHANDLE}
193 # @FUNCTION: java-vm_install-env
196 # Installs a Java VM environment file. The source can be specified but
197 # defaults to ${FILESDIR}/${VMHANDLE}.env.sh.
199 # Environment variables within this file will be resolved. You should
200 # escape the $ when referring to variables that should be resolved later
201 # such as ${JAVA_HOME}. Subshells may be used but avoid using double
202 # quotes. See icedtea-bin.env.sh for a good example.
204 java-vm_install-env() {
205 debug-print-function ${FUNCNAME} "$*"
207 local env_file="${ED}${JAVA_VM_CONFIG_DIR}/${VMHANDLE}"
208 local source_env_file="${1-${FILESDIR}/${VMHANDLE}.env.sh}"
210 if [[ ! -f "${source_env_file}" ]]; then
211 die "Unable to find the env file: ${source_env_file}"
214 dodir "${JAVA_VM_CONFIG_DIR}"
216 # Here be dragons! ;) -- Chewi
217 eval echo "\"$(cat <<< "$(sed 's:":\\":g' "${source_env_file}")")\"" > "${env_file}" ||
218 die "failed to create Java env file"
221 echo "VMHANDLE=\"${VMHANDLE}\""
222 echo "BUILD_ONLY=\"${JAVA_VM_BUILD_ONLY}\""
223 [[ ${JAVA_PROVIDE} ]] && echo "PROVIDES=\"${JAVA_PROVIDE}\"" || true
224 ) >> "${env_file}" || die "failed to append to Java env file"
226 local java_home=$(unset JAVA_HOME; source "${env_file}"; echo ${JAVA_HOME})
227 [[ -z ${java_home} ]] && die "No JAVA_HOME defined in ${env_file}"
230 dodir "${JAVA_VM_DIR}"
231 dosym "${java_home#${EPREFIX}}" "${JAVA_VM_DIR}/${VMHANDLE}"
235 # @FUNCTION: java-vm_set-pax-markings
237 # Set PaX markings on all JDK/JRE executables to allow code-generation on
238 # the heap by the JIT compiler.
240 # The markings need to be set prior to the first invocation of the the freshly
241 # built / installed VM. Be it before creating the Class Data Sharing archive or
242 # generating cacerts. Otherwise a PaX enabled kernel will kill the VM.
243 # Bug #215225 #389751
247 # $1 - JDK/JRE base directory.
250 # java-vm_set-pax-markings "${S}"
251 # java-vm_set-pax-markings "${ED}"/opt/${P}
254 java-vm_set-pax-markings() {
255 debug-print-function ${FUNCNAME} "$*"
256 [[ $# -ne 1 ]] && die "${FUNCNAME}: takes exactly one argument"
257 [[ ! -f "${1}"/bin/java ]] \
258 && die "${FUNCNAME}: argument needs to be JDK/JRE base directory"
260 local executables=( "${1}"/bin/* )
261 [[ -d "${1}"/jre ]] && executables+=( "${1}"/jre/bin/* )
263 # Usually disabling MPROTECT is sufficient.
264 local pax_markings="m"
265 # On x86 for heap sizes over 700MB disable SEGMEXEC and PAGEEXEC as well.
266 use x86 && pax_markings+="sp"
268 pax-mark ${pax_markings} $(list-paxables "${executables[@]}")
272 # @FUNCTION: java-vm_revdep-mask
274 # Installs a revdep-rebuild control file which SEARCH_DIR_MASK set to the path
275 # where the VM is installed. Prevents pointless rebuilds - see bug #177925.
276 # Also gives a notice to the user.
280 # $1 - Path of the VM (defaults to /opt/${P} if not set)
283 # java-vm_revdep-mask
284 # java-vm_revdep-mask /path/to/jdk/
288 java-vm_revdep-mask() {
289 debug-print-function ${FUNCNAME} "$*"
291 local VMROOT="${1-"${EPREFIX}"/opt/${P}}"
293 dodir /etc/revdep-rebuild
294 echo "SEARCH_DIRS_MASK=\"${VMROOT}\"" >> "${ED}/etc/revdep-rebuild/61-${VMHANDLE}" \
295 || die "Failed to write revdep-rebuild mask file"
299 # @FUNCTION: java-vm_sandbox-predict
301 # Install a sandbox control file. Specified paths won't cause a sandbox
302 # violation if opened read write but no write takes place. See bug 388937#c1
306 # java-vm_sandbox-predict /dev/random /proc/self/coredump_filter
309 java-vm_sandbox-predict() {
310 debug-print-function ${FUNCNAME} "$*"
311 [[ -z "${1}" ]] && die "${FUNCNAME} takes at least one argument"
313 local path path_arr=("$@")
314 # subshell this to prevent IFS bleeding out dependant on bash version.
315 # could use local, which *should* work, but that requires a lot of testing.
316 path=$(IFS=":"; echo "${path_arr[*]}")
318 echo "SANDBOX_PREDICT=\"${path}\"" > "${ED}/etc/sandbox.d/20${VMHANDLE}" \
319 || die "Failed to write sandbox control file"