java.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # Copyright 2012-2017 The Meson development team
  2. # Licensed under the Apache License, Version 2.0 (the "License");
  3. # you may not use this file except in compliance with the License.
  4. # You may obtain a copy of the License at
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. # Unless required by applicable law or agreed to in writing, software
  7. # distributed under the License is distributed on an "AS IS" BASIS,
  8. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. # See the License for the specific language governing permissions and
  10. # limitations under the License.
  11. import os.path
  12. import shutil
  13. import subprocess
  14. import typing as T
  15. from ..mesonlib import EnvironmentException, MachineChoice
  16. from .compilers import Compiler, java_buildtype_args
  17. from .mixins.islinker import BasicLinkerIsCompilerMixin
  18. if T.TYPE_CHECKING:
  19. from ..envconfig import MachineInfo
  20. class JavaCompiler(BasicLinkerIsCompilerMixin, Compiler):
  21. language = 'java'
  22. def __init__(self, exelist, version, for_machine: MachineChoice,
  23. info: 'MachineInfo'):
  24. super().__init__(exelist, version, for_machine, info)
  25. self.id = 'unknown'
  26. self.is_cross = False
  27. self.javarunner = 'java'
  28. def get_werror_args(self):
  29. return ['-Werror']
  30. def split_shlib_to_parts(self, fname):
  31. return None, fname
  32. def get_dependency_gen_args(self, outtarget, outfile):
  33. return []
  34. def get_compile_only_args(self):
  35. return []
  36. def get_output_args(self, subdir):
  37. if subdir == '':
  38. subdir = './'
  39. return ['-d', subdir, '-s', subdir]
  40. def get_coverage_args(self):
  41. return []
  42. def get_std_exe_link_args(self):
  43. return []
  44. def get_include_args(self, path):
  45. return []
  46. def get_pic_args(self):
  47. return []
  48. def name_string(self):
  49. return ' '.join(self.exelist)
  50. def get_pch_use_args(self, pch_dir, header):
  51. return []
  52. def get_pch_name(self, header_name):
  53. return ''
  54. def get_buildtype_args(self, buildtype):
  55. return java_buildtype_args[buildtype]
  56. def compute_parameters_with_absolute_paths(self, parameter_list, build_dir):
  57. for idx, i in enumerate(parameter_list):
  58. if i in ['-cp', '-classpath', '-sourcepath'] and idx + 1 < len(parameter_list):
  59. path_list = parameter_list[idx + 1].split(os.pathsep)
  60. path_list = [os.path.normpath(os.path.join(build_dir, x)) for x in path_list]
  61. parameter_list[idx + 1] = os.pathsep.join(path_list)
  62. return parameter_list
  63. def sanity_check(self, work_dir, environment):
  64. src = 'SanityCheck.java'
  65. obj = 'SanityCheck'
  66. source_name = os.path.join(work_dir, src)
  67. with open(source_name, 'w') as ofile:
  68. ofile.write('''class SanityCheck {
  69. public static void main(String[] args) {
  70. int i;
  71. }
  72. }
  73. ''')
  74. pc = subprocess.Popen(self.exelist + [src], cwd=work_dir)
  75. pc.wait()
  76. if pc.returncode != 0:
  77. raise EnvironmentException('Java compiler %s can not compile programs.' % self.name_string())
  78. runner = shutil.which(self.javarunner)
  79. if runner:
  80. cmdlist = [runner, obj]
  81. pe = subprocess.Popen(cmdlist, cwd=work_dir)
  82. pe.wait()
  83. if pe.returncode != 0:
  84. raise EnvironmentException('Executables created by Java compiler %s are not runnable.' % self.name_string())
  85. else:
  86. m = "Java Virtual Machine wasn't found, but it's needed by Meson. " \
  87. "Please install a JRE.\nIf you have specific needs where this " \
  88. "requirement doesn't make sense, please open a bug at " \
  89. "https://github.com/mesonbuild/meson/issues/new and tell us " \
  90. "all about it."
  91. raise EnvironmentException(m)
  92. def needs_static_linker(self):
  93. return False