perl-functions.eclass: should 'just work' in EAPI=6
[gentoo.git] / eclass / java-vm-2.eclass
1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Id$
4
5 # @ECLASS: java-vm-2.eclass
6 # @MAINTAINER:
7 # java@gentoo.org
8 # @BLURB: Java Virtual Machine eclass
9 # @DESCRIPTION:
10 # This eclass provides functionality which assists with installing
11 # virtual machines, and ensures that they are recognized by java-config.
12
13 inherit eutils fdo-mime multilib pax-utils prefix
14
15 EXPORT_FUNCTIONS pkg_setup pkg_postinst pkg_prerm pkg_postrm
16
17 RDEPEND="
18         >=dev-java/java-config-2.2.0
19         app-eselect/eselect-java"
20 DEPEND="${RDEPEND}"
21 has "${EAPI}" 0 1 && DEPEND="${DEPEND} >=sys-apps/portage-2.1"
22
23 export WANT_JAVA_CONFIG=2
24
25
26 # @ECLASS-VARIABLE: JAVA_VM_CONFIG_DIR
27 # @INTERNAL
28 # @DESCRIPTION:
29 # Where to place the vm env file.
30 JAVA_VM_CONFIG_DIR="/usr/share/java-config-2/vm"
31
32 # @ECLASS-VARIABLE: JAVA_VM_DIR
33 # @INTERNAL
34 # @DESCRIPTION:
35 # Base directory for vm links.
36 JAVA_VM_DIR="/usr/lib/jvm"
37
38 # @ECLASS-VARIABLE: JAVA_VM_SYSTEM
39 # @INTERNAL
40 # @DESCRIPTION:
41 # Link for system-vm
42 JAVA_VM_SYSTEM="/etc/java-config-2/current-system-vm"
43
44 # @ECLASS-VARIABLE: JAVA_VM_BUILD_ONLY
45 # @DESCRIPTION:
46 # Set to YES to mark a vm as build-only.
47 JAVA_VM_BUILD_ONLY="${JAVA_VM_BUILD_ONLY:-FALSE}"
48
49
50 # @FUNCTION: java-vm-2_pkg_setup
51 # @DESCRIPTION:
52 # default pkg_setup
53 #
54 # Initialize vm handle.
55
56 java-vm-2_pkg_setup() {
57         if [[ "${SLOT}" != "0" ]]; then
58                 VMHANDLE=${PN}-${SLOT}
59         else
60                 VMHANDLE=${PN}
61         fi
62 }
63
64
65 # @FUNCTION: java-vm-2_pkg_postinst
66 # @DESCRIPTION:
67 # default pkg_postinst
68 #
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.
71
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
78                 java_set_default_vm_
79         else
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
83                         java_set_default_vm_
84                 fi
85         fi
86
87         java-vm_check-nsplugin
88         java_mozilla_clean_
89         fdo-mime_desktop_database_update
90 }
91
92
93 # @FUNCTION: java-vm_check-nsplugin
94 # @INTERNAL
95 # @DESCRIPTION:
96 # Check if the nsplugin needs updating
97
98 java-vm_check-nsplugin() {
99         local libdir
100         if [[ ${VMHANDLE} =~ emul-linux-x86 ]]; then
101                 libdir=lib32
102         else
103                 libdir=lib
104         fi
105
106         has ${EAPI:-0} 0 1 2 && ! use prefix && EPREFIX=
107
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."
112                         java-vm_set-nsplugin
113                 else
114                         einfo "System nsplugin is already set, not changing it."
115                 fi
116                 einfo "You can change nsplugin with eselect java-nsplugin."
117         fi
118 }
119
120
121 # @FUNCTION: java-vm_set-nsplugin
122 # @INTERNAL
123 # @DESCRIPTION:
124 # Set the nsplugin implemetation.
125
126 java-vm_set-nsplugin() {
127         local extra_args
128         if use amd64; then
129                 if [[ ${VMHANDLE} =~ emul-linux-x86 ]]; then
130                         extra_args="32bit"
131                 else
132                         extra_args="64bit"
133                 fi
134                 einfo "Setting ${extra_args} nsplugin to ${VMHANDLE}"
135         else
136                 einfo "Setting nsplugin to ${VMHANDLE}..."
137         fi
138         eselect java-nsplugin set ${extra_args} ${VMHANDLE}
139 }
140
141
142 # @FUNCTION: java-vm-2_pkg_prerm
143 # @DESCRIPTION:
144 # default pkg_prerm
145 #
146 # Warn user if removing system-vm.
147
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!"
154         fi
155 }
156
157
158 # @FUNCTION: java-vm-2_pkg_postrm
159 # @DESCRIPTION:
160 # default pkg_postrm
161 #
162 # Update mime database.
163
164 java-vm-2_pkg_postrm() {
165         fdo-mime_desktop_database_update
166 }
167
168
169 # @FUNCTION: java_set_default_vm_
170 # @INTERNAL
171 # @DESCRIPTION:
172 # Set system-vm.
173
174 java_set_default_vm_() {
175         java-config-2 --set-system-vm="${VMHANDLE}"
176
177         einfo " ${P} set as the default system-vm."
178 }
179
180
181 # @FUNCTION: get_system_arch
182 # @DESCRIPTION:
183 # Get Java specific arch name.
184
185 get_system_arch() {
186         local sarch
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/)
190         fi
191         echo ${sarch}
192 }
193
194
195 # @FUNCTION: set_java_env
196 # @DESCRIPTION:
197 # Installs a vm env file.
198
199 # TODO rename to something more evident, like install_env_file
200 set_java_env() {
201         debug-print-function ${FUNCNAME} $*
202
203         if has ${EAPI:-0} 0 1 2 && ! use prefix ; then
204                 ED="${D}"
205                 EPREFIX=""
206         fi
207
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}"
211         if [[ ${1} ]]; then
212                 local source_env_file="${1}"
213         else
214                 local source_env_file="${FILESDIR}/${VMHANDLE}.env"
215         fi
216
217         if [[ ! -f ${source_env_file} ]]; then
218                 die "Unable to find the env file: ${source_env_file}"
219         fi
220
221         dodir ${JAVA_VM_CONFIG_DIR}
222         sed \
223                 -e "s/@P@/${P}/g" \
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"
233
234         (
235                 echo "VMHANDLE=\"${VMHANDLE}\""
236                 echo "BUILD_ONLY=\"${JAVA_VM_BUILD_ONLY}\""
237         ) >> "${env_file}"
238
239         eprefixify ${env_file}
240
241         [[ -n ${JAVA_PROVIDE} ]] && echo "PROVIDES=\"${JAVA_PROVIDE}\"" >> ${env_file}
242
243         local java_home=$(source "${env_file}"; echo ${JAVA_HOME})
244         [[ -z ${java_home} ]] && die "No JAVA_HOME defined in ${env_file}"
245
246         # Make the symlink
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}"
250 }
251
252
253 # @FUNCTION: java-vm_set-pax-markings
254 # @DESCRIPTION:
255 # Set PaX markings on all JDK/JRE executables to allow code-generation on
256 # the heap by the JIT compiler.
257 #
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
262 #
263 # @CODE
264 #   Parameters:
265 #     $1 - JDK/JRE base directory.
266 #
267 #   Examples:
268 #     java-vm_set-pax-markings "${S}"
269 #     java-vm_set-pax-markings "${ED}"/opt/${P}
270 # @CODE
271
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"
277
278         local executables=( "${1}"/bin/* )
279         [[ -d "${1}"/jre ]] && executables+=( "${1}"/jre/bin/* )
280
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"
287
288         pax-mark ${pax_markings} $(list-paxables "${executables[@]}")
289 }
290
291
292 # @FUNCTION: java-vm_revdep-mask
293 # @DESCRIPTION:
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.
297 #
298 # @CODE
299 #   Parameters:
300 #     $1 - Path of the VM (defaults to /opt/${P} if not set)
301 #
302 #   Examples:
303 #     java-vm_revdep-mask
304 #     java-vm_revdep-mask /path/to/jdk/
305 #
306 # @CODE
307
308 java-vm_revdep-mask() {
309         if has ${EAPI:-0} 0 1 2 && ! use prefix; then
310                 ED="${D}"
311                 EPREFIX=
312         fi
313
314         local VMROOT="${1-"${EPREFIX}"/opt/${P}}"
315
316         dodir /etc/revdep-rebuild/
317         echo "SEARCH_DIRS_MASK=\"${VMROOT}\""> "${ED}/etc/revdep-rebuild/61-${VMHANDLE}"
318 }
319
320
321 # @FUNCTION: java-vm_sandbox-predict
322 # @DESCRIPTION:
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
325 #
326 # @CODE
327 #   Examples:
328 #     java-vm_sandbox-predict /dev/random /proc/self/coredump_filter
329 # @CODE
330
331 java-vm_sandbox-predict() {
332         debug-print-function ${FUNCNAME} "$*"
333         [[ -z "${1}" ]] && die "${FUNCNAME} takes at least one argument"
334
335         has ${EAPI:-0} 0 1 2 && ! use prefix && ED="${D}"
336
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[*]}")
341         dodir /etc/sandbox.d
342         echo "SANDBOX_PREDICT=\"${path}\"" > "${ED}/etc/sandbox.d/20${VMHANDLE}" \
343                 || die "Failed to write sandbox control file"
344 }
345
346
347 # @FUNCTION: java_get_plugin_dir_
348 # @INTERNAL
349 # @DESCRIPTION:
350 # Get the java plugin dir.
351
352 java_get_plugin_dir_() {
353         has ${EAPI:-0} 0 1 2 && ! use prefix && EPREFIX=
354         echo "${EPREFIX}"/usr/$(get_libdir)/nsbrowser/plugins
355 }
356
357
358 # @FUNCTION: install_mozilla_plugin
359 # @DESCRIPTION:
360 # Register a netscape java-plugin.
361
362 install_mozilla_plugin() {
363         local plugin="${1}"
364         local variant="${2}"
365
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}"
369         fi
370
371         if [[ -n "${variant}" ]]; then
372                 variant="-${variant}"
373         fi
374
375         local plugin_dir="/usr/share/java-config-2/nsplugin"
376         dodir "${plugin_dir}"
377         dosym "${plugin}" "${plugin_dir}/${VMHANDLE}${variant}-javaplugin.so"
378 }
379
380
381 # @FUNCTION: java_mozilla_clean_
382 # @INTERNAL
383 # @DESCRIPTION:
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
387
388 java_mozilla_clean_() {
389         local plugin_dir=$(java_get_plugin_dir_)
390         for file in ${plugin_dir}/javaplugin_*; do
391                 rm -f ${file}
392         done
393         for file in ${plugin_dir}/libjavaplugin*; do
394                 rm -f ${file}
395         done
396 }