java-vm-2.eclass 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. # Copyright 1999-2017 Gentoo Foundation
  2. # Distributed under the terms of the GNU General Public License v2
  3. # @ECLASS: java-vm-2.eclass
  4. # @MAINTAINER:
  5. # java@gentoo.org
  6. # @BLURB: Java Virtual Machine eclass
  7. # @DESCRIPTION:
  8. # This eclass provides functionality which assists with installing
  9. # virtual machines, and ensures that they are recognized by java-config.
  10. case ${EAPI:-0} in
  11. 5|6) ;;
  12. *) die "EAPI=${EAPI} is not supported" ;;
  13. esac
  14. inherit fdo-mime multilib pax-utils prefix
  15. EXPORT_FUNCTIONS pkg_setup pkg_postinst pkg_prerm pkg_postrm
  16. RDEPEND="
  17. >=dev-java/java-config-2.2.0-r3
  18. >=app-eselect/eselect-java-0.2.0"
  19. DEPEND="${RDEPEND}"
  20. export WANT_JAVA_CONFIG=2
  21. # @ECLASS-VARIABLE: JAVA_VM_CONFIG_DIR
  22. # @INTERNAL
  23. # @DESCRIPTION:
  24. # Where to place the vm env file.
  25. JAVA_VM_CONFIG_DIR="/usr/share/java-config-2/vm"
  26. # @ECLASS-VARIABLE: JAVA_VM_DIR
  27. # @INTERNAL
  28. # @DESCRIPTION:
  29. # Base directory for vm links.
  30. JAVA_VM_DIR="/usr/lib/jvm"
  31. # @ECLASS-VARIABLE: JAVA_VM_SYSTEM
  32. # @INTERNAL
  33. # @DESCRIPTION:
  34. # Link for system-vm
  35. JAVA_VM_SYSTEM="/etc/java-config-2/current-system-vm"
  36. # @ECLASS-VARIABLE: JAVA_VM_BUILD_ONLY
  37. # @DESCRIPTION:
  38. # Set to YES to mark a vm as build-only.
  39. JAVA_VM_BUILD_ONLY="${JAVA_VM_BUILD_ONLY:-FALSE}"
  40. # @FUNCTION: java-vm-2_pkg_setup
  41. # @DESCRIPTION:
  42. # default pkg_setup
  43. #
  44. # Initialize vm handle.
  45. java-vm-2_pkg_setup() {
  46. if [[ "${SLOT}" != "0" ]]; then
  47. VMHANDLE=${PN}-${SLOT}
  48. else
  49. VMHANDLE=${PN}
  50. fi
  51. }
  52. # @FUNCTION: java-vm-2_pkg_postinst
  53. # @DESCRIPTION:
  54. # default pkg_postinst
  55. #
  56. # Set the generation-2 system VM, if it isn't set or the setting is
  57. # invalid. Also update mime database.
  58. java-vm-2_pkg_postinst() {
  59. # Note that we cannot rely on java-config here, as it will silently recognize
  60. # e.g. icedtea6-bin as valid system VM if icedtea6 is set but invalid (e.g. due
  61. # to the migration to icedtea-6)
  62. if [[ ! -L "${EROOT}${JAVA_VM_SYSTEM}" ]]; then
  63. java_set_default_vm_
  64. else
  65. local current_vm_path=$(readlink "${EROOT}${JAVA_VM_SYSTEM}")
  66. local current_vm=$(basename "${ROOT}${current_vm_path}")
  67. if [[ ! -L "${EROOT}${JAVA_VM_DIR}/${current_vm}" ]]; then
  68. java_set_default_vm_
  69. fi
  70. fi
  71. fdo-mime_desktop_database_update
  72. }
  73. # @FUNCTION: java-vm-2_pkg_prerm
  74. # @DESCRIPTION:
  75. # default pkg_prerm
  76. #
  77. # Warn user if removing system-vm.
  78. java-vm-2_pkg_prerm() {
  79. if [[ "$(GENTOO_VM="" java-config -f 2>/dev/null)" == "${VMHANDLE}" && -z "${REPLACED_BY_VERSION}" ]]; then
  80. ewarn "It appears you are removing your system-vm!"
  81. ewarn "Please run java-config -L to list available VMs,"
  82. ewarn "then use java-config -S to set a new system-vm!"
  83. fi
  84. }
  85. # @FUNCTION: java-vm-2_pkg_postrm
  86. # @DESCRIPTION:
  87. # default pkg_postrm
  88. #
  89. # Update mime database.
  90. java-vm-2_pkg_postrm() {
  91. fdo-mime_desktop_database_update
  92. }
  93. # @FUNCTION: java_set_default_vm_
  94. # @INTERNAL
  95. # @DESCRIPTION:
  96. # Set system-vm.
  97. java_set_default_vm_() {
  98. java-config-2 --set-system-vm="${VMHANDLE}"
  99. einfo " ${P} set as the default system-vm."
  100. }
  101. # @FUNCTION: get_system_arch
  102. # @DESCRIPTION:
  103. # Get Java specific arch name.
  104. #
  105. # NOTE the mips and sparc values are best guesses. Oracle uses sparcv9
  106. # but does OpenJDK use sparc64? We don't support OpenJDK on sparc or any
  107. # JVM on mips though so it doesn't matter much.
  108. get_system_arch() {
  109. local abi=${1-${ABI}}
  110. case $(get_abi_CHOST ${abi}) in
  111. mips*l*) echo mipsel ;;
  112. mips*) echo mips ;;
  113. ppc64le*) echo ppc64le ;;
  114. *)
  115. case ${abi} in
  116. *_fbsd) get_system_arch ${abi%_fbsd} ;;
  117. arm64) echo aarch64 ;;
  118. hppa) echo parisc ;;
  119. sparc32) echo sparc ;;
  120. sparc64) echo sparcv9 ;;
  121. x86*) echo i386 ;;
  122. *) echo ${abi} ;;
  123. esac ;;
  124. esac
  125. }
  126. # @FUNCTION: set_java_env
  127. # @DESCRIPTION:
  128. # Installs a vm env file.
  129. # DEPRECATED, use java-vm_install-env instead.
  130. set_java_env() {
  131. debug-print-function ${FUNCNAME} $*
  132. local platform="$(get_system_arch)"
  133. local env_file="${ED}${JAVA_VM_CONFIG_DIR}/${VMHANDLE}"
  134. if [[ ${1} ]]; then
  135. local source_env_file="${1}"
  136. else
  137. local source_env_file="${FILESDIR}/${VMHANDLE}.env"
  138. fi
  139. if [[ ! -f ${source_env_file} ]]; then
  140. die "Unable to find the env file: ${source_env_file}"
  141. fi
  142. dodir ${JAVA_VM_CONFIG_DIR}
  143. sed \
  144. -e "s/@P@/${P}/g" \
  145. -e "s/@PN@/${PN}/g" \
  146. -e "s/@PV@/${PV}/g" \
  147. -e "s/@PF@/${PF}/g" \
  148. -e "s/@SLOT@/${SLOT}/g" \
  149. -e "s/@PLATFORM@/${platform}/g" \
  150. -e "s/@LIBDIR@/$(get_libdir)/g" \
  151. -e "/^LDPATH=.*lib\\/\\\"/s|\"\\(.*\\)\"|\"\\1${platform}/:\\1${platform}/server/\"|" \
  152. < "${source_env_file}" \
  153. > "${env_file}" || die "sed failed"
  154. (
  155. echo "VMHANDLE=\"${VMHANDLE}\""
  156. echo "BUILD_ONLY=\"${JAVA_VM_BUILD_ONLY}\""
  157. ) >> "${env_file}"
  158. eprefixify ${env_file}
  159. [[ -n ${JAVA_PROVIDE} ]] && echo "PROVIDES=\"${JAVA_PROVIDE}\"" >> ${env_file}
  160. local java_home=$(source "${env_file}"; echo ${JAVA_HOME})
  161. [[ -z ${java_home} ]] && die "No JAVA_HOME defined in ${env_file}"
  162. # Make the symlink
  163. dodir "${JAVA_VM_DIR}"
  164. dosym ${java_home#${EPREFIX}} ${JAVA_VM_DIR}/${VMHANDLE}
  165. }
  166. # @FUNCTION: java-vm_install-env
  167. # @DESCRIPTION:
  168. #
  169. # Installs a Java VM environment file. The source can be specified but
  170. # defaults to ${FILESDIR}/${VMHANDLE}.env.sh.
  171. #
  172. # Environment variables within this file will be resolved. You should
  173. # escape the $ when referring to variables that should be resolved later
  174. # such as ${JAVA_HOME}. Subshells may be used but avoid using double
  175. # quotes. See icedtea-bin.env.sh for a good example.
  176. java-vm_install-env() {
  177. debug-print-function ${FUNCNAME} "$*"
  178. local env_file="${ED}${JAVA_VM_CONFIG_DIR}/${VMHANDLE}"
  179. local source_env_file="${1-${FILESDIR}/${VMHANDLE}.env.sh}"
  180. if [[ ! -f "${source_env_file}" ]]; then
  181. die "Unable to find the env file: ${source_env_file}"
  182. fi
  183. dodir "${JAVA_VM_CONFIG_DIR}"
  184. # Here be dragons! ;) -- Chewi
  185. eval echo "\"$(cat <<< "$(sed 's:":\\":g' "${source_env_file}")")\"" > "${env_file}" ||
  186. die "failed to create Java env file"
  187. (
  188. echo "VMHANDLE=\"${VMHANDLE}\""
  189. echo "BUILD_ONLY=\"${JAVA_VM_BUILD_ONLY}\""
  190. [[ ${JAVA_PROVIDE} ]] && echo "PROVIDES=\"${JAVA_PROVIDE}\"" || true
  191. ) >> "${env_file}" || die "failed to append to Java env file"
  192. local java_home=$(unset JAVA_HOME; source "${env_file}"; echo ${JAVA_HOME})
  193. [[ -z ${java_home} ]] && die "No JAVA_HOME defined in ${env_file}"
  194. # Make the symlink
  195. dodir "${JAVA_VM_DIR}"
  196. dosym "${java_home#${EPREFIX}}" "${JAVA_VM_DIR}/${VMHANDLE}"
  197. }
  198. # @FUNCTION: java-vm_set-pax-markings
  199. # @DESCRIPTION:
  200. # Set PaX markings on all JDK/JRE executables to allow code-generation on
  201. # the heap by the JIT compiler.
  202. #
  203. # The markings need to be set prior to the first invocation of the the freshly
  204. # built / installed VM. Be it before creating the Class Data Sharing archive or
  205. # generating cacerts. Otherwise a PaX enabled kernel will kill the VM.
  206. # Bug #215225 #389751
  207. #
  208. # @CODE
  209. # Parameters:
  210. # $1 - JDK/JRE base directory.
  211. #
  212. # Examples:
  213. # java-vm_set-pax-markings "${S}"
  214. # java-vm_set-pax-markings "${ED}"/opt/${P}
  215. # @CODE
  216. java-vm_set-pax-markings() {
  217. debug-print-function ${FUNCNAME} "$*"
  218. [[ $# -ne 1 ]] && die "${FUNCNAME}: takes exactly one argument"
  219. [[ ! -f "${1}"/bin/java ]] \
  220. && die "${FUNCNAME}: argument needs to be JDK/JRE base directory"
  221. local executables=( "${1}"/bin/* )
  222. [[ -d "${1}"/jre ]] && executables+=( "${1}"/jre/bin/* )
  223. # Usually disabling MPROTECT is sufficient.
  224. local pax_markings="m"
  225. # On x86 for heap sizes over 700MB disable SEGMEXEC and PAGEEXEC as well.
  226. use x86 && pax_markings+="sp"
  227. pax-mark ${pax_markings} $(list-paxables "${executables[@]}")
  228. }
  229. # @FUNCTION: java-vm_revdep-mask
  230. # @DESCRIPTION:
  231. # Installs a revdep-rebuild control file which SEARCH_DIR_MASK set to the path
  232. # where the VM is installed. Prevents pointless rebuilds - see bug #177925.
  233. # Also gives a notice to the user.
  234. #
  235. # @CODE
  236. # Parameters:
  237. # $1 - Path of the VM (defaults to /opt/${P} if not set)
  238. #
  239. # Examples:
  240. # java-vm_revdep-mask
  241. # java-vm_revdep-mask /path/to/jdk/
  242. #
  243. # @CODE
  244. java-vm_revdep-mask() {
  245. debug-print-function ${FUNCNAME} "$*"
  246. local VMROOT="${1-"${EPREFIX}"/opt/${P}}"
  247. dodir /etc/revdep-rebuild
  248. echo "SEARCH_DIRS_MASK=\"${VMROOT}\"" >> "${ED}/etc/revdep-rebuild/61-${VMHANDLE}" \
  249. || die "Failed to write revdep-rebuild mask file"
  250. }
  251. # @FUNCTION: java-vm_sandbox-predict
  252. # @DESCRIPTION:
  253. # Install a sandbox control file. Specified paths won't cause a sandbox
  254. # violation if opened read write but no write takes place. See bug 388937#c1
  255. #
  256. # @CODE
  257. # Examples:
  258. # java-vm_sandbox-predict /dev/random /proc/self/coredump_filter
  259. # @CODE
  260. java-vm_sandbox-predict() {
  261. debug-print-function ${FUNCNAME} "$*"
  262. [[ -z "${1}" ]] && die "${FUNCNAME} takes at least one argument"
  263. local path path_arr=("$@")
  264. # subshell this to prevent IFS bleeding out dependant on bash version.
  265. # could use local, which *should* work, but that requires a lot of testing.
  266. path=$(IFS=":"; echo "${path_arr[*]}")
  267. dodir /etc/sandbox.d
  268. echo "SANDBOX_PREDICT=\"${path}\"" > "${ED}/etc/sandbox.d/20${VMHANDLE}" \
  269. || die "Failed to write sandbox control file"
  270. }