dev-qt/qtopengl: stable 5.14.2 for ppc, bug #719732
[gentoo.git] / eclass / java-vm-2.eclass
1 # Copyright 1999-2018 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 # @SUPPORTED_EAPIS: 5 6
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 multilib pax-utils prefix xdg-utils
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.4.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         if [[ ! -d ${EROOT}${JAVA_VM_SYSTEM} ]]; then
78                 eselect java-vm set system "${VMHANDLE}"
79                 einfo "${P} set as the default system-vm."
80         fi
81
82         xdg_desktop_database_update
83 }
84
85
86 # @FUNCTION: java-vm-2_pkg_prerm
87 # @DESCRIPTION:
88 # default pkg_prerm
89 #
90 # Warn user if removing system-vm.
91
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!"
97         fi
98 }
99
100
101 # @FUNCTION: java-vm-2_pkg_postrm
102 # @DESCRIPTION:
103 # default pkg_postrm
104 #
105 # Update mime database.
106
107 java-vm-2_pkg_postrm() {
108         xdg_desktop_database_update
109 }
110
111
112 # @FUNCTION: get_system_arch
113 # @DESCRIPTION:
114 # Get Java specific arch name.
115 #
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.
119
120 get_system_arch() {
121         local abi=${1-${ABI}}
122
123         case $(get_abi_CHOST ${abi}) in
124                 mips*l*) echo mipsel ;;
125                 mips*) echo mips ;;
126                 powerpc64le*) echo ppc64le ;;
127                 *)
128                         case ${abi} in
129                                 *_fbsd) get_system_arch ${abi%_fbsd} ;;
130                                 arm64) echo aarch64 ;;
131                                 hppa) echo parisc ;;
132                                 sparc32) echo sparc ;;
133                                 sparc64) echo sparcv9 ;;
134                                 x86*) echo i386 ;;
135                                 *) echo ${abi} ;;
136                         esac ;;
137         esac
138 }
139
140
141 # @FUNCTION: set_java_env
142 # @DESCRIPTION:
143 # Installs a vm env file.
144 # DEPRECATED, use java-vm_install-env instead.
145
146 set_java_env() {
147         debug-print-function ${FUNCNAME} $*
148
149         local platform="$(get_system_arch)"
150         local env_file="${ED}${JAVA_VM_CONFIG_DIR}/${VMHANDLE}"
151
152         if [[ ${1} ]]; then
153                 local source_env_file="${1}"
154         else
155                 local source_env_file="${FILESDIR}/${VMHANDLE}.env"
156         fi
157
158         if [[ ! -f ${source_env_file} ]]; then
159                 die "Unable to find the env file: ${source_env_file}"
160         fi
161
162         dodir ${JAVA_VM_CONFIG_DIR}
163         sed \
164                 -e "s/@P@/${P}/g" \
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"
174
175         (
176                 echo "VMHANDLE=\"${VMHANDLE}\""
177                 echo "BUILD_ONLY=\"${JAVA_VM_BUILD_ONLY}\""
178         ) >> "${env_file}"
179
180         eprefixify ${env_file}
181
182         [[ -n ${JAVA_PROVIDE} ]] && echo "PROVIDES=\"${JAVA_PROVIDE}\"" >> ${env_file}
183
184         local java_home=$(source "${env_file}"; echo ${JAVA_HOME})
185         [[ -z ${java_home} ]] && die "No JAVA_HOME defined in ${env_file}"
186
187         # Make the symlink
188         dodir "${JAVA_VM_DIR}"
189         dosym ${java_home#${EPREFIX}} ${JAVA_VM_DIR}/${VMHANDLE}
190 }
191
192
193 # @FUNCTION: java-vm_install-env
194 # @DESCRIPTION:
195 #
196 # Installs a Java VM environment file. The source can be specified but
197 # defaults to ${FILESDIR}/${VMHANDLE}.env.sh.
198 #
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.
203
204 java-vm_install-env() {
205         debug-print-function ${FUNCNAME} "$*"
206
207         local env_file="${ED}${JAVA_VM_CONFIG_DIR}/${VMHANDLE}"
208         local source_env_file="${1-${FILESDIR}/${VMHANDLE}.env.sh}"
209
210         if [[ ! -f "${source_env_file}" ]]; then
211                 die "Unable to find the env file: ${source_env_file}"
212         fi
213
214         dodir "${JAVA_VM_CONFIG_DIR}"
215
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"
219
220         (
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"
225
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}"
228
229         # Make the symlink
230         dodir "${JAVA_VM_DIR}"
231         dosym "${java_home#${EPREFIX}}" "${JAVA_VM_DIR}/${VMHANDLE}"
232 }
233
234
235 # @FUNCTION: java-vm_set-pax-markings
236 # @DESCRIPTION:
237 # Set PaX markings on all JDK/JRE executables to allow code-generation on
238 # the heap by the JIT compiler.
239 #
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
244 #
245 # @CODE
246 #   Parameters:
247 #     $1 - JDK/JRE base directory.
248 #
249 #   Examples:
250 #     java-vm_set-pax-markings "${S}"
251 #     java-vm_set-pax-markings "${ED}"/opt/${P}
252 # @CODE
253
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"
259
260         local executables=( "${1}"/bin/* )
261         [[ -d "${1}"/jre ]] && executables+=( "${1}"/jre/bin/* )
262
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"
267
268         pax-mark ${pax_markings} $(list-paxables "${executables[@]}")
269 }
270
271
272 # @FUNCTION: java-vm_revdep-mask
273 # @DESCRIPTION:
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.
277 #
278 # @CODE
279 #   Parameters:
280 #     $1 - Path of the VM (defaults to /opt/${P} if not set)
281 #
282 #   Examples:
283 #     java-vm_revdep-mask
284 #     java-vm_revdep-mask /path/to/jdk/
285 #
286 # @CODE
287
288 java-vm_revdep-mask() {
289         debug-print-function ${FUNCNAME} "$*"
290
291         local VMROOT="${1-"${EPREFIX}"/opt/${P}}"
292
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"
296 }
297
298
299 # @FUNCTION: java-vm_sandbox-predict
300 # @DESCRIPTION:
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
303 #
304 # @CODE
305 #   Examples:
306 #     java-vm_sandbox-predict /dev/random /proc/self/coredump_filter
307 # @CODE
308
309 java-vm_sandbox-predict() {
310         debug-print-function ${FUNCNAME} "$*"
311         [[ -z "${1}" ]] && die "${FUNCNAME} takes at least one argument"
312
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[*]}")
317         dodir /etc/sandbox.d
318         echo "SANDBOX_PREDICT=\"${path}\"" > "${ED}/etc/sandbox.d/20${VMHANDLE}" \
319                 || die "Failed to write sandbox control file"
320 }