| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2012-2017 The Meson development team |
| 3 |
|
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
import os |
| 7 |
import os.path |
| 8 |
import shutil |
| 9 |
import textwrap |
| 10 |
import typing as T |
| 11 |
|
| 12 |
from ..mesonlib import EnvironmentException |
| 13 |
from .compilers import Compiler |
| 14 |
from .mixins.islinker import BasicLinkerIsCompilerMixin |
| 15 |
|
| 16 |
if T.TYPE_CHECKING: |
| 17 |
from ..environment import Environment |
| 18 |
from ..mesonlib import MachineChoice |
| 19 |
|
| 20 |
|
| 21 |
java_debug_args: T.Dict[bool, T.List[str]] = { |
| 22 |
False: ['-g:none'], |
| 23 |
True: ['-g'] |
| 24 |
} |
| 25 |
|
| 26 |
class JavaCompiler(BasicLinkerIsCompilerMixin, Compiler): |
| 27 |
|
| 28 |
language = 'java' |
| 29 |
id = 'unknown' |
| 30 |
|
| 31 |
_WARNING_LEVELS: T.Dict[str, T.List[str]] = { |
| 32 |
'0': ['-nowarn'], |
| 33 |
'1': ['-Xlint:all'], |
| 34 |
'2': ['-Xlint:all', '-Xdoclint:all'], |
| 35 |
'3': ['-Xlint:all', '-Xdoclint:all'], |
| 36 |
} |
| 37 |
|
| 38 |
def __init__(self, exelist: T.List[str], version: str, for_machine: MachineChoice, |
| 39 |
env: Environment, full_version: T.Optional[str] = None): |
| 40 |
super().__init__([], exelist, version, for_machine, env, full_version=full_version) |
| 41 |
self.javarunner = 'java' |
| 42 |
|
| 43 |
def get_warn_args(self, level: str) -> T.List[str]: |
| 44 |
return self._WARNING_LEVELS[level] |
| 45 |
|
| 46 |
def get_werror_args(self) -> T.List[str]: |
| 47 |
return ['-Werror'] |
| 48 |
|
| 49 |
def get_output_args(self, outputname: str) -> T.List[str]: |
| 50 |
if outputname == '': |
| 51 |
outputname = './' |
| 52 |
return ['-d', outputname, '-s', outputname] |
| 53 |
|
| 54 |
def get_pic_args(self) -> T.List[str]: |
| 55 |
return [] |
| 56 |
|
| 57 |
def get_pch_use_args(self, pch_dir: str, header: str) -> T.List[str]: |
| 58 |
return [] |
| 59 |
|
| 60 |
def get_pch_name(self, name: str) -> str: |
| 61 |
return '' |
| 62 |
|
| 63 |
def compute_parameters_with_absolute_paths(self, parameter_list: T.List[str], |
| 64 |
build_dir: str) -> T.List[str]: |
| 65 |
for idx, i in enumerate(parameter_list): |
| 66 |
if i in {'-cp', '-classpath', '-sourcepath'} and idx + 1 < len(parameter_list): |
| 67 |
path_list = parameter_list[idx + 1].split(os.pathsep) |
| 68 |
path_list = [os.path.normpath(os.path.join(build_dir, x)) for x in path_list] |
| 69 |
parameter_list[idx + 1] = os.pathsep.join(path_list) |
| 70 |
|
| 71 |
return parameter_list |
| 72 |
|
| 73 |
def _sanity_check_filenames(self) -> T.Tuple[str, T.Optional[str], str]: |
| 74 |
sup = super()._sanity_check_filenames() |
| 75 |
return sup[0], None, 'SanityCheck' |
| 76 |
|
| 77 |
def _sanity_check_run_with_exe_wrapper(self, command: T.List[str]) -> T.List[str]: |
| 78 |
runner = shutil.which(self.javarunner) |
| 79 |
if runner is None: |
| 80 |
m = "Java Virtual Machine wasn't found, but it's needed by Meson. " \ |
| 81 |
"Please install a JRE.\nIf you have specific needs where this " \ |
| 82 |
"requirement doesn't make sense, please open a bug at " \ |
| 83 |
"https://github.com/mesonbuild/meson/issues/new and tell us " \ |
| 84 |
"all about it." |
| 85 |
raise EnvironmentException(m) |
| 86 |
basedir = os.path.basename(command[0]) |
| 87 |
return [runner, '-cp', basedir, basedir] |
| 88 |
|
| 89 |
def _sanity_check_source_code(self) -> str: |
| 90 |
return textwrap.dedent( |
| 91 |
'''class SanityCheck { |
| 92 |
public static void main(String[] args) { |
| 93 |
int i; |
| 94 |
} |
| 95 |
} |
| 96 |
''') |
| 97 |
|
| 98 |
def sanity_check(self, work_dir: str) -> None: |
| 99 |
# Older versions of Java (At least 1.8), don't create this directory and |
| 100 |
# error when it doesn't exist. Newer versions (11 at least), doesn't have |
| 101 |
# this issue. |
| 102 |
fname = self._sanity_check_filenames()[2] |
| 103 |
os.makedirs(os.path.join(work_dir, fname), exist_ok=True) |
| 104 |
return super().sanity_check(work_dir) |
| 105 |
|
| 106 |
def needs_static_linker(self) -> bool: |
| 107 |
return False |
| 108 |
|
| 109 |
def get_optimization_args(self, optimization_level: str) -> T.List[str]: |
| 110 |
return [] |
| 111 |
|
| 112 |
def get_debug_args(self, is_debug: bool) -> T.List[str]: |
| 113 |
return java_debug_args[is_debug] |