depend.apache.eclass: Let APXS point to the new location of the binary.
[gentoo.git] / eclass / java-vm-2.eclass
1 # Copyright 1999-2016 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 case ${EAPI:-0} in
14         5|6) ;;
15         *) die "EAPI=${EAPI} is not supported" ;;
16 esac
17
18 inherit fdo-mime multilib pax-utils prefix
19
20 EXPORT_FUNCTIONS pkg_setup pkg_postinst pkg_prerm pkg_postrm
21
22 RDEPEND="
23         >=dev-java/java-config-2.2.0-r3
24         >=app-eselect/eselect-java-0.2.0"
25 DEPEND="${RDEPEND}"
26
27 export WANT_JAVA_CONFIG=2
28
29
30 # @ECLASS-VARIABLE: JAVA_VM_CONFIG_DIR
31 # @INTERNAL
32 # @DESCRIPTION:
33 # Where to place the vm env file.
34 JAVA_VM_CONFIG_DIR="/usr/share/java-config-2/vm"
35
36 # @ECLASS-VARIABLE: JAVA_VM_DIR
37 # @INTERNAL
38 # @DESCRIPTION:
39 # Base directory for vm links.
40 JAVA_VM_DIR="/usr/lib/jvm"
41
42 # @ECLASS-VARIABLE: JAVA_VM_SYSTEM
43 # @INTERNAL
44 # @DESCRIPTION:
45 # Link for system-vm
46 JAVA_VM_SYSTEM="/etc/java-config-2/current-system-vm"
47
48 # @ECLASS-VARIABLE: JAVA_VM_BUILD_ONLY
49 # @DESCRIPTION:
50 # Set to YES to mark a vm as build-only.
51 JAVA_VM_BUILD_ONLY="${JAVA_VM_BUILD_ONLY:-FALSE}"
52
53
54 # @FUNCTION: java-vm-2_pkg_setup
55 # @DESCRIPTION:
56 # default pkg_setup
57 #
58 # Initialize vm handle.
59
60 java-vm-2_pkg_setup() {
61         if [[ "${SLOT}" != "0" ]]; then
62                 VMHANDLE=${PN}-${SLOT}
63         else
64                 VMHANDLE=${PN}
65         fi
66 }
67
68
69 # @FUNCTION: java-vm-2_pkg_postinst
70 # @DESCRIPTION:
71 # default pkg_postinst
72 #
73 # Set the generation-2 system VM, if it isn't set or the setting is
74 # invalid. Also update mime database.
75
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
81                 java_set_default_vm_
82         else
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
86                         java_set_default_vm_
87                 fi
88         fi
89
90         fdo-mime_desktop_database_update
91 }
92
93
94 # @FUNCTION: java-vm-2_pkg_prerm
95 # @DESCRIPTION:
96 # default pkg_prerm
97 #
98 # Warn user if removing system-vm.
99
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!"
105         fi
106 }
107
108
109 # @FUNCTION: java-vm-2_pkg_postrm
110 # @DESCRIPTION:
111 # default pkg_postrm
112 #
113 # Update mime database.
114
115 java-vm-2_pkg_postrm() {
116         fdo-mime_desktop_database_update
117 }
118
119
120 # @FUNCTION: java_set_default_vm_
121 # @INTERNAL
122 # @DESCRIPTION:
123 # Set system-vm.
124
125 java_set_default_vm_() {
126         java-config-2 --set-system-vm="${VMHANDLE}"
127
128         einfo " ${P} set as the default system-vm."
129 }
130
131
132 # @FUNCTION: get_system_arch
133 # @DESCRIPTION:
134 # Get Java specific arch name.
135 #
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.
139
140 get_system_arch() {
141         local abi=${1-${ABI}}
142
143         case $(get_abi_CHOST ${abi}) in
144                 mips*l*) echo mipsel ;;
145                 mips*) echo mips ;;
146                 ppc64le*) echo ppc64le ;;
147                 *)
148                         case ${abi} in
149                                 *_fbsd) get_system_arch ${abi%_fbsd} ;;
150                                 arm64) echo aarch64 ;;
151                                 hppa) echo parisc ;;
152                                 sparc32) echo sparc ;;
153                                 sparc64) echo sparcv9 ;;
154                                 x86*) echo i386 ;;
155                                 *) echo ${abi} ;;
156                         esac ;;
157         esac
158 }
159
160
161 # @FUNCTION: set_java_env
162 # @DESCRIPTION:
163 # Installs a vm env file.
164 # DEPRECATED, use java-vm_install-env instead.
165
166 set_java_env() {
167         debug-print-function ${FUNCNAME} $*
168
169         local platform="$(get_system_arch)"
170         local env_file="${ED}${JAVA_VM_CONFIG_DIR}/${VMHANDLE}"
171
172         if [[ ${1} ]]; then
173                 local source_env_file="${1}"
174         else
175                 local source_env_file="${FILESDIR}/${VMHANDLE}.env"
176         fi
177
178         if [[ ! -f ${source_env_file} ]]; then
179                 die "Unable to find the env file: ${source_env_file}"
180         fi
181
182         dodir ${JAVA_VM_CONFIG_DIR}
183         sed \
184                 -e "s/@P@/${P}/g" \
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"
194
195         (
196                 echo "VMHANDLE=\"${VMHANDLE}\""
197                 echo "BUILD_ONLY=\"${JAVA_VM_BUILD_ONLY}\""
198         ) >> "${env_file}"
199
200         eprefixify ${env_file}
201
202         [[ -n ${JAVA_PROVIDE} ]] && echo "PROVIDES=\"${JAVA_PROVIDE}\"" >> ${env_file}
203
204         local java_home=$(source "${env_file}"; echo ${JAVA_HOME})
205         [[ -z ${java_home} ]] && die "No JAVA_HOME defined in ${env_file}"
206
207         # Make the symlink
208         dodir "${JAVA_VM_DIR}"
209         dosym ${java_home#${EPREFIX}} ${JAVA_VM_DIR}/${VMHANDLE}
210 }
211
212
213 # @FUNCTION: java-vm_install-env
214 # @DESCRIPTION:
215 #
216 # Installs a Java VM environment file. The source can be specified but
217 # defaults to ${FILESDIR}/${VMHANDLE}.env.sh.
218 #
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.
223
224 java-vm_install-env() {
225         debug-print-function ${FUNCNAME} "$*"
226
227         local env_file="${ED}${JAVA_VM_CONFIG_DIR}/${VMHANDLE}"
228         local source_env_file="${1-${FILESDIR}/${VMHANDLE}.env.sh}"
229
230         if [[ ! -f "${source_env_file}" ]]; then
231                 die "Unable to find the env file: ${source_env_file}"
232         fi
233
234         dodir "${JAVA_VM_CONFIG_DIR}"
235
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"
239
240         (
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"
245
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}"
248
249         # Make the symlink
250         dodir "${JAVA_VM_DIR}"
251         dosym "${java_home#${EPREFIX}}" "${JAVA_VM_DIR}/${VMHANDLE}"
252 }
253
254
255 # @FUNCTION: java-vm_set-pax-markings
256 # @DESCRIPTION:
257 # Set PaX markings on all JDK/JRE executables to allow code-generation on
258 # the heap by the JIT compiler.
259 #
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
264 #
265 # @CODE
266 #   Parameters:
267 #     $1 - JDK/JRE base directory.
268 #
269 #   Examples:
270 #     java-vm_set-pax-markings "${S}"
271 #     java-vm_set-pax-markings "${ED}"/opt/${P}
272 # @CODE
273
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"
279
280         local executables=( "${1}"/bin/* )
281         [[ -d "${1}"/jre ]] && executables+=( "${1}"/jre/bin/* )
282
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"
289
290         pax-mark ${pax_markings} $(list-paxables "${executables[@]}")
291 }
292
293
294 # @FUNCTION: java-vm_revdep-mask
295 # @DESCRIPTION:
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.
299 #
300 # @CODE
301 #   Parameters:
302 #     $1 - Path of the VM (defaults to /opt/${P} if not set)
303 #
304 #   Examples:
305 #     java-vm_revdep-mask
306 #     java-vm_revdep-mask /path/to/jdk/
307 #
308 # @CODE
309
310 java-vm_revdep-mask() {
311         debug-print-function ${FUNCNAME} "$*"
312
313         local VMROOT="${1-"${EPREFIX}"/opt/${P}}"
314
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"
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         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[*]}")
339         dodir /etc/sandbox.d
340         echo "SANDBOX_PREDICT=\"${path}\"" > "${ED}/etc/sandbox.d/20${VMHANDLE}" \
341                 || die "Failed to write sandbox control file"
342 }