dev-libs/libusb: stable 1.0.21 for sparc, bug #630342
[gentoo.git] / eclass / java-vm-2.eclass
1 # Copyright 1999-2017 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: java-vm-2.eclass
5 # @MAINTAINER:
6 # java@gentoo.org
7 # @BLURB: Java Virtual Machine eclass
8 # @DESCRIPTION:
9 # This eclass provides functionality which assists with installing
10 # virtual machines, and ensures that they are recognized by java-config.
11
12 case ${EAPI:-0} in
13         5|6) ;;
14         *) die "EAPI=${EAPI} is not supported" ;;
15 esac
16
17 inherit fdo-mime multilib pax-utils prefix
18
19 EXPORT_FUNCTIONS pkg_setup pkg_postinst pkg_prerm pkg_postrm
20
21 RDEPEND="
22         >=dev-java/java-config-2.2.0-r3
23         >=app-eselect/eselect-java-0.2.0"
24 DEPEND="${RDEPEND}"
25
26 export WANT_JAVA_CONFIG=2
27
28
29 # @ECLASS-VARIABLE: JAVA_VM_CONFIG_DIR
30 # @INTERNAL
31 # @DESCRIPTION:
32 # Where to place the vm env file.
33 JAVA_VM_CONFIG_DIR="/usr/share/java-config-2/vm"
34
35 # @ECLASS-VARIABLE: JAVA_VM_DIR
36 # @INTERNAL
37 # @DESCRIPTION:
38 # Base directory for vm links.
39 JAVA_VM_DIR="/usr/lib/jvm"
40
41 # @ECLASS-VARIABLE: JAVA_VM_SYSTEM
42 # @INTERNAL
43 # @DESCRIPTION:
44 # Link for system-vm
45 JAVA_VM_SYSTEM="/etc/java-config-2/current-system-vm"
46
47 # @ECLASS-VARIABLE: JAVA_VM_BUILD_ONLY
48 # @DESCRIPTION:
49 # Set to YES to mark a vm as build-only.
50 JAVA_VM_BUILD_ONLY="${JAVA_VM_BUILD_ONLY:-FALSE}"
51
52
53 # @FUNCTION: java-vm-2_pkg_setup
54 # @DESCRIPTION:
55 # default pkg_setup
56 #
57 # Initialize vm handle.
58
59 java-vm-2_pkg_setup() {
60         if [[ "${SLOT}" != "0" ]]; then
61                 VMHANDLE=${PN}-${SLOT}
62         else
63                 VMHANDLE=${PN}
64         fi
65 }
66
67
68 # @FUNCTION: java-vm-2_pkg_postinst
69 # @DESCRIPTION:
70 # default pkg_postinst
71 #
72 # Set the generation-2 system VM, if it isn't set or the setting is
73 # invalid. Also update mime database.
74
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
80                 java_set_default_vm_
81         else
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
85                         java_set_default_vm_
86                 fi
87         fi
88
89         fdo-mime_desktop_database_update
90 }
91
92
93 # @FUNCTION: java-vm-2_pkg_prerm
94 # @DESCRIPTION:
95 # default pkg_prerm
96 #
97 # Warn user if removing system-vm.
98
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!"
104         fi
105 }
106
107
108 # @FUNCTION: java-vm-2_pkg_postrm
109 # @DESCRIPTION:
110 # default pkg_postrm
111 #
112 # Update mime database.
113
114 java-vm-2_pkg_postrm() {
115         fdo-mime_desktop_database_update
116 }
117
118
119 # @FUNCTION: java_set_default_vm_
120 # @INTERNAL
121 # @DESCRIPTION:
122 # Set system-vm.
123
124 java_set_default_vm_() {
125         java-config-2 --set-system-vm="${VMHANDLE}"
126
127         einfo " ${P} set as the default system-vm."
128 }
129
130
131 # @FUNCTION: get_system_arch
132 # @DESCRIPTION:
133 # Get Java specific arch name.
134 #
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.
138
139 get_system_arch() {
140         local abi=${1-${ABI}}
141
142         case $(get_abi_CHOST ${abi}) in
143                 mips*l*) echo mipsel ;;
144                 mips*) echo mips ;;
145                 ppc64le*) echo ppc64le ;;
146                 *)
147                         case ${abi} in
148                                 *_fbsd) get_system_arch ${abi%_fbsd} ;;
149                                 arm64) echo aarch64 ;;
150                                 hppa) echo parisc ;;
151                                 sparc32) echo sparc ;;
152                                 sparc64) echo sparcv9 ;;
153                                 x86*) echo i386 ;;
154                                 *) echo ${abi} ;;
155                         esac ;;
156         esac
157 }
158
159
160 # @FUNCTION: set_java_env
161 # @DESCRIPTION:
162 # Installs a vm env file.
163 # DEPRECATED, use java-vm_install-env instead.
164
165 set_java_env() {
166         debug-print-function ${FUNCNAME} $*
167
168         local platform="$(get_system_arch)"
169         local env_file="${ED}${JAVA_VM_CONFIG_DIR}/${VMHANDLE}"
170
171         if [[ ${1} ]]; then
172                 local source_env_file="${1}"
173         else
174                 local source_env_file="${FILESDIR}/${VMHANDLE}.env"
175         fi
176
177         if [[ ! -f ${source_env_file} ]]; then
178                 die "Unable to find the env file: ${source_env_file}"
179         fi
180
181         dodir ${JAVA_VM_CONFIG_DIR}
182         sed \
183                 -e "s/@P@/${P}/g" \
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"
193
194         (
195                 echo "VMHANDLE=\"${VMHANDLE}\""
196                 echo "BUILD_ONLY=\"${JAVA_VM_BUILD_ONLY}\""
197         ) >> "${env_file}"
198
199         eprefixify ${env_file}
200
201         [[ -n ${JAVA_PROVIDE} ]] && echo "PROVIDES=\"${JAVA_PROVIDE}\"" >> ${env_file}
202
203         local java_home=$(source "${env_file}"; echo ${JAVA_HOME})
204         [[ -z ${java_home} ]] && die "No JAVA_HOME defined in ${env_file}"
205
206         # Make the symlink
207         dodir "${JAVA_VM_DIR}"
208         dosym ${java_home#${EPREFIX}} ${JAVA_VM_DIR}/${VMHANDLE}
209 }
210
211
212 # @FUNCTION: java-vm_install-env
213 # @DESCRIPTION:
214 #
215 # Installs a Java VM environment file. The source can be specified but
216 # defaults to ${FILESDIR}/${VMHANDLE}.env.sh.
217 #
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.
222
223 java-vm_install-env() {
224         debug-print-function ${FUNCNAME} "$*"
225
226         local env_file="${ED}${JAVA_VM_CONFIG_DIR}/${VMHANDLE}"
227         local source_env_file="${1-${FILESDIR}/${VMHANDLE}.env.sh}"
228
229         if [[ ! -f "${source_env_file}" ]]; then
230                 die "Unable to find the env file: ${source_env_file}"
231         fi
232
233         dodir "${JAVA_VM_CONFIG_DIR}"
234
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"
238
239         (
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"
244
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}"
247
248         # Make the symlink
249         dodir "${JAVA_VM_DIR}"
250         dosym "${java_home#${EPREFIX}}" "${JAVA_VM_DIR}/${VMHANDLE}"
251 }
252
253
254 # @FUNCTION: java-vm_set-pax-markings
255 # @DESCRIPTION:
256 # Set PaX markings on all JDK/JRE executables to allow code-generation on
257 # the heap by the JIT compiler.
258 #
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
263 #
264 # @CODE
265 #   Parameters:
266 #     $1 - JDK/JRE base directory.
267 #
268 #   Examples:
269 #     java-vm_set-pax-markings "${S}"
270 #     java-vm_set-pax-markings "${ED}"/opt/${P}
271 # @CODE
272
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"
278
279         local executables=( "${1}"/bin/* )
280         [[ -d "${1}"/jre ]] && executables+=( "${1}"/jre/bin/* )
281
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"
286
287         pax-mark ${pax_markings} $(list-paxables "${executables[@]}")
288 }
289
290
291 # @FUNCTION: java-vm_revdep-mask
292 # @DESCRIPTION:
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.
296 #
297 # @CODE
298 #   Parameters:
299 #     $1 - Path of the VM (defaults to /opt/${P} if not set)
300 #
301 #   Examples:
302 #     java-vm_revdep-mask
303 #     java-vm_revdep-mask /path/to/jdk/
304 #
305 # @CODE
306
307 java-vm_revdep-mask() {
308         debug-print-function ${FUNCNAME} "$*"
309
310         local VMROOT="${1-"${EPREFIX}"/opt/${P}}"
311
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"
315 }
316
317
318 # @FUNCTION: java-vm_sandbox-predict
319 # @DESCRIPTION:
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
322 #
323 # @CODE
324 #   Examples:
325 #     java-vm_sandbox-predict /dev/random /proc/self/coredump_filter
326 # @CODE
327
328 java-vm_sandbox-predict() {
329         debug-print-function ${FUNCNAME} "$*"
330         [[ -z "${1}" ]] && die "${FUNCNAME} takes at least one argument"
331
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[*]}")
336         dodir /etc/sandbox.d
337         echo "SANDBOX_PREDICT=\"${path}\"" > "${ED}/etc/sandbox.d/20${VMHANDLE}" \
338                 || die "Failed to write sandbox control file"
339 }