cpp.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  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 copy
  12. import functools
  13. import os.path
  14. import typing as T
  15. from .. import coredata
  16. from .. import mlog
  17. from ..mesonlib import MesonException, MachineChoice, version_compare
  18. from ..linkers import LinkerEnvVarsMixin
  19. from .compilers import (
  20. gnu_winlibs,
  21. msvc_winlibs,
  22. Compiler,
  23. )
  24. from .c_function_attributes import CXX_FUNC_ATTRIBUTES, C_FUNC_ATTRIBUTES
  25. from .mixins.clike import CLikeCompiler
  26. from .mixins.ccrx import CcrxCompiler
  27. from .mixins.c2000 import C2000Compiler
  28. from .mixins.arm import ArmCompiler, ArmclangCompiler
  29. from .mixins.visualstudio import MSVCCompiler, ClangClCompiler
  30. from .mixins.gnu import GnuCompiler
  31. from .mixins.intel import IntelGnuLikeCompiler, IntelVisualStudioLikeCompiler
  32. from .mixins.clang import ClangCompiler
  33. from .mixins.elbrus import ElbrusCompiler
  34. from .mixins.pgi import PGICompiler
  35. from .mixins.emscripten import EmscriptenMixin
  36. if T.TYPE_CHECKING:
  37. from ..envconfig import MachineInfo
  38. def non_msvc_eh_options(eh, args):
  39. if eh == 'none':
  40. args.append('-fno-exceptions')
  41. elif eh == 's' or eh == 'c':
  42. mlog.warning('non-MSVC compilers do not support ' + eh + ' exception handling.' +
  43. 'You may want to set eh to \'default\'.')
  44. class CPPCompiler(CLikeCompiler, Compiler):
  45. @classmethod
  46. def attribute_check_func(cls, name):
  47. try:
  48. return CXX_FUNC_ATTRIBUTES.get(name, C_FUNC_ATTRIBUTES[name])
  49. except KeyError:
  50. raise MesonException('Unknown function attribute "{}"'.format(name))
  51. language = 'cpp'
  52. def __init__(self, exelist, version, for_machine: MachineChoice, is_cross: bool,
  53. info: 'MachineInfo', exe_wrap: T.Optional[str] = None, **kwargs):
  54. # If a child ObjCPP class has already set it, don't set it ourselves
  55. Compiler.__init__(self, exelist, version, for_machine, info, **kwargs)
  56. CLikeCompiler.__init__(self, is_cross, exe_wrap)
  57. @staticmethod
  58. def get_display_language():
  59. return 'C++'
  60. def get_no_stdinc_args(self):
  61. return ['-nostdinc++']
  62. def sanity_check(self, work_dir, environment):
  63. code = 'class breakCCompiler;int main(void) { return 0; }\n'
  64. return self.sanity_check_impl(work_dir, environment, 'sanitycheckcpp.cc', code)
  65. def get_compiler_check_args(self):
  66. # -fpermissive allows non-conforming code to compile which is necessary
  67. # for many C++ checks. Particularly, the has_header_symbol check is
  68. # too strict without this and always fails.
  69. return super().get_compiler_check_args() + ['-fpermissive']
  70. def has_header_symbol(self, hname, symbol, prefix, env, *, extra_args=None, dependencies=None):
  71. # Check if it's a C-like symbol
  72. found, cached = super().has_header_symbol(hname, symbol, prefix, env,
  73. extra_args=extra_args,
  74. dependencies=dependencies)
  75. if found:
  76. return True, cached
  77. # Check if it's a class or a template
  78. if extra_args is None:
  79. extra_args = []
  80. fargs = {'prefix': prefix, 'header': hname, 'symbol': symbol}
  81. t = '''{prefix}
  82. #include <{header}>
  83. using {symbol};
  84. int main(void) {{ return 0; }}'''
  85. return self.compiles(t.format(**fargs), env, extra_args=extra_args,
  86. dependencies=dependencies)
  87. def _test_cpp_std_arg(self, cpp_std_value):
  88. # Test whether the compiler understands a -std=XY argument
  89. assert(cpp_std_value.startswith('-std='))
  90. # This test does not use has_multi_arguments() for two reasons:
  91. # 1. has_multi_arguments() requires an env argument, which the compiler
  92. # object does not have at this point.
  93. # 2. even if it did have an env object, that might contain another more
  94. # recent -std= argument, which might lead to a cascaded failure.
  95. CPP_TEST = 'int i = static_cast<int>(0);'
  96. with self.compile(CPP_TEST, extra_args=[cpp_std_value], mode='compile') as p:
  97. if p.returncode == 0:
  98. mlog.debug('Compiler accepts {}:'.format(cpp_std_value), 'YES')
  99. return True
  100. else:
  101. mlog.debug('Compiler accepts {}:'.format(cpp_std_value), 'NO')
  102. return False
  103. @functools.lru_cache()
  104. def _find_best_cpp_std(self, cpp_std):
  105. # The initial version mapping approach to make falling back
  106. # from '-std=c++14' to '-std=c++1y' was too brittle. For instance,
  107. # Apple's Clang uses a different versioning scheme to upstream LLVM,
  108. # making the whole detection logic awfully brittle. Instead, let's
  109. # just see if feeding GCC or Clang our '-std=' setting works, and
  110. # if not, try the fallback argument.
  111. CPP_FALLBACKS = {
  112. 'c++11': 'c++0x',
  113. 'gnu++11': 'gnu++0x',
  114. 'c++14': 'c++1y',
  115. 'gnu++14': 'gnu++1y',
  116. 'c++17': 'c++1z',
  117. 'gnu++17': 'gnu++1z'
  118. }
  119. # Currently, remapping is only supported for Clang, Elbrus and GCC
  120. assert(self.id in frozenset(['clang', 'lcc', 'gcc', 'emscripten']))
  121. if cpp_std not in CPP_FALLBACKS:
  122. # 'c++03' and 'c++98' don't have fallback types
  123. return '-std=' + cpp_std
  124. for i in (cpp_std, CPP_FALLBACKS[cpp_std]):
  125. cpp_std_value = '-std=' + i
  126. if self._test_cpp_std_arg(cpp_std_value):
  127. return cpp_std_value
  128. raise MesonException('C++ Compiler does not support -std={}'.format(cpp_std))
  129. class ClangCPPCompiler(ClangCompiler, CPPCompiler):
  130. def __init__(self, exelist, version, for_machine: MachineChoice,
  131. is_cross, info: 'MachineInfo', exe_wrapper=None, **kwargs):
  132. CPPCompiler.__init__(self, exelist, version, for_machine, is_cross,
  133. info, exe_wrapper, **kwargs)
  134. ClangCompiler.__init__(self)
  135. default_warn_args = ['-Wall', '-Winvalid-pch', '-Wnon-virtual-dtor']
  136. self.warn_args = {'0': [],
  137. '1': default_warn_args,
  138. '2': default_warn_args + ['-Wextra'],
  139. '3': default_warn_args + ['-Wextra', '-Wpedantic']}
  140. def get_options(self):
  141. opts = CPPCompiler.get_options(self)
  142. opts.update({
  143. 'eh': coredata.UserComboOption(
  144. 'C++ exception handling type.',
  145. ['none', 'default', 'a', 's', 'sc'],
  146. 'default',
  147. ),
  148. 'rtti': coredata.UserBooleanOption('Enable RTTI', True),
  149. 'std': coredata.UserComboOption(
  150. 'C++ language standard to use',
  151. ['none', 'c++98', 'c++03', 'c++11', 'c++14', 'c++17', 'c++1z', 'c++2a',
  152. 'gnu++11', 'gnu++14', 'gnu++17', 'gnu++1z', 'gnu++2a'],
  153. 'none',
  154. ),
  155. })
  156. if self.info.is_windows() or self.info.is_cygwin():
  157. opts.update({
  158. 'winlibs': coredata.UserArrayOption(
  159. 'Standard Win libraries to link against',
  160. gnu_winlibs,
  161. ),
  162. })
  163. return opts
  164. def get_option_compile_args(self, options):
  165. args = []
  166. std = options['std']
  167. if std.value != 'none':
  168. args.append(self._find_best_cpp_std(std.value))
  169. non_msvc_eh_options(options['eh'].value, args)
  170. if not options['rtti'].value:
  171. args.append('-fno-rtti')
  172. return args
  173. def get_option_link_args(self, options):
  174. if self.info.is_windows() or self.info.is_cygwin():
  175. return options['winlibs'].value[:]
  176. return []
  177. def language_stdlib_only_link_flags(self):
  178. return ['-lstdc++']
  179. class AppleClangCPPCompiler(ClangCPPCompiler):
  180. def language_stdlib_only_link_flags(self):
  181. return ['-lc++']
  182. class EmscriptenCPPCompiler(EmscriptenMixin, LinkerEnvVarsMixin, ClangCPPCompiler):
  183. def __init__(self, exelist, version, for_machine: MachineChoice,
  184. is_cross: bool, info: 'MachineInfo', exe_wrapper=None, **kwargs):
  185. if not is_cross:
  186. raise MesonException('Emscripten compiler can only be used for cross compilation.')
  187. ClangCPPCompiler.__init__(self, exelist=exelist, version=version,
  188. for_machine=for_machine, is_cross=is_cross,
  189. info=info, exe_wrapper=exe_wrapper, **kwargs)
  190. self.id = 'emscripten'
  191. def get_option_compile_args(self, options):
  192. args = []
  193. std = options['std']
  194. if std.value != 'none':
  195. args.append(self._find_best_cpp_std(std.value))
  196. return args
  197. class ArmclangCPPCompiler(ArmclangCompiler, CPPCompiler):
  198. def __init__(self, exelist, version, for_machine: MachineChoice,
  199. is_cross, info: 'MachineInfo', exe_wrapper=None, **kwargs):
  200. CPPCompiler.__init__(self, exelist, version, for_machine, is_cross, exe_wrapper, **kwargs)
  201. ArmclangCompiler.__init__(self)
  202. default_warn_args = ['-Wall', '-Winvalid-pch', '-Wnon-virtual-dtor']
  203. self.warn_args = {'0': [],
  204. '1': default_warn_args,
  205. '2': default_warn_args + ['-Wextra'],
  206. '3': default_warn_args + ['-Wextra', '-Wpedantic']}
  207. def get_options(self):
  208. opts = CPPCompiler.get_options(self)
  209. opts.update({
  210. 'eh': coredata.UserComboOption(
  211. 'C++ exception handling type.',
  212. ['none', 'default', 'a', 's', 'sc'],
  213. 'default',
  214. ),
  215. 'std': coredata.UserComboOption(
  216. 'C++ language standard to use',
  217. [
  218. 'none', 'c++98', 'c++03', 'c++11', 'c++14', 'c++17',
  219. 'gnu++98', 'gnu++03', 'gnu++11', 'gnu++14', 'gnu++17',
  220. ],
  221. 'none',
  222. ),
  223. })
  224. return opts
  225. def get_option_compile_args(self, options):
  226. args = []
  227. std = options['std']
  228. if std.value != 'none':
  229. args.append('-std=' + std.value)
  230. non_msvc_eh_options(options['eh'].value, args)
  231. return args
  232. def get_option_link_args(self, options):
  233. return []
  234. class GnuCPPCompiler(GnuCompiler, CPPCompiler):
  235. def __init__(self, exelist, version, for_machine: MachineChoice,
  236. is_cross, info: 'MachineInfo', exe_wrap, defines, **kwargs):
  237. CPPCompiler.__init__(self, exelist, version, for_machine, is_cross, info, exe_wrap, **kwargs)
  238. GnuCompiler.__init__(self, defines)
  239. default_warn_args = ['-Wall', '-Winvalid-pch', '-Wnon-virtual-dtor']
  240. self.warn_args = {'0': [],
  241. '1': default_warn_args,
  242. '2': default_warn_args + ['-Wextra'],
  243. '3': default_warn_args + ['-Wextra', '-Wpedantic']}
  244. def get_options(self):
  245. opts = CPPCompiler.get_options(self)
  246. opts.update({
  247. 'eh': coredata.UserComboOption(
  248. 'C++ exception handling type.',
  249. ['none', 'default', 'a', 's', 'sc'],
  250. 'default',
  251. ),
  252. 'rtti': coredata.UserBooleanOption('Enable RTTI', True),
  253. 'std': coredata.UserComboOption(
  254. 'C++ language standard to use',
  255. ['none', 'c++98', 'c++03', 'c++11', 'c++14', 'c++17', 'c++1z', 'c++2a',
  256. 'gnu++03', 'gnu++11', 'gnu++14', 'gnu++17', 'gnu++1z', 'gnu++2a'],
  257. 'none',
  258. ),
  259. 'debugstl': coredata.UserBooleanOption(
  260. 'STL debug mode',
  261. False,
  262. )
  263. })
  264. if self.info.is_windows() or self.info.is_cygwin():
  265. opts.update({
  266. 'winlibs': coredata.UserArrayOption(
  267. 'Standard Win libraries to link against',
  268. gnu_winlibs,
  269. ),
  270. })
  271. return opts
  272. def get_option_compile_args(self, options):
  273. args = []
  274. std = options['std']
  275. if std.value != 'none':
  276. args.append(self._find_best_cpp_std(std.value))
  277. non_msvc_eh_options(options['eh'].value, args)
  278. if not options['rtti'].value:
  279. args.append('-fno-rtti')
  280. if options['debugstl'].value:
  281. args.append('-D_GLIBCXX_DEBUG=1')
  282. return args
  283. def get_option_link_args(self, options):
  284. if self.info.is_windows() or self.info.is_cygwin():
  285. return options['winlibs'].value[:]
  286. return []
  287. def get_pch_use_args(self, pch_dir, header):
  288. return ['-fpch-preprocess', '-include', os.path.basename(header)]
  289. def language_stdlib_only_link_flags(self):
  290. return ['-lstdc++']
  291. class PGICPPCompiler(PGICompiler, CPPCompiler):
  292. def __init__(self, exelist, version, for_machine: MachineChoice,
  293. is_cross, info: 'MachineInfo', exe_wrapper=None, **kwargs):
  294. CPPCompiler.__init__(self, exelist, version, for_machine, is_cross, info, exe_wrapper, **kwargs)
  295. PGICompiler.__init__(self)
  296. class ElbrusCPPCompiler(GnuCPPCompiler, ElbrusCompiler):
  297. def __init__(self, exelist, version, for_machine: MachineChoice,
  298. is_cross, info: 'MachineInfo', exe_wrapper=None,
  299. defines=None, **kwargs):
  300. GnuCPPCompiler.__init__(self, exelist, version, for_machine,
  301. is_cross, info, exe_wrapper, defines,
  302. **kwargs)
  303. ElbrusCompiler.__init__(self)
  304. # It does not support c++/gnu++ 17 and 1z, but still does support 0x, 1y, and gnu++98.
  305. def get_options(self):
  306. opts = CPPCompiler.get_options(self)
  307. opts.update({
  308. 'eh': coredata.UserComboOption(
  309. 'C++ exception handling type.',
  310. ['none', 'default', 'a', 's', 'sc'],
  311. 'default',
  312. ),
  313. 'std': coredata.UserComboOption(
  314. 'C++ language standard to use',
  315. [
  316. 'none', 'c++98', 'c++03', 'c++0x', 'c++11', 'c++14', 'c++1y',
  317. 'gnu++98', 'gnu++03', 'gnu++0x', 'gnu++11', 'gnu++14', 'gnu++1y',
  318. ],
  319. 'none',
  320. ),
  321. 'debugstl': coredata.UserBooleanOption(
  322. 'STL debug mode',
  323. False,
  324. ),
  325. })
  326. return opts
  327. # Elbrus C++ compiler does not have lchmod, but there is only linker warning, not compiler error.
  328. # So we should explicitly fail at this case.
  329. def has_function(self, funcname, prefix, env, *, extra_args=None, dependencies=None):
  330. if funcname == 'lchmod':
  331. return False, False
  332. else:
  333. return super().has_function(funcname, prefix, env,
  334. extra_args=extra_args,
  335. dependencies=dependencies)
  336. # Elbrus C++ compiler does not support RTTI, so don't check for it.
  337. def get_option_compile_args(self, options):
  338. args = []
  339. std = options['std']
  340. if std.value != 'none':
  341. args.append(self._find_best_cpp_std(std.value))
  342. non_msvc_eh_options(options['eh'].value, args)
  343. if options['debugstl'].value:
  344. args.append('-D_GLIBCXX_DEBUG=1')
  345. return args
  346. class IntelCPPCompiler(IntelGnuLikeCompiler, CPPCompiler):
  347. def __init__(self, exelist, version, for_machine: MachineChoice,
  348. is_cross, info: 'MachineInfo', exe_wrap, **kwargs):
  349. CPPCompiler.__init__(self, exelist, version, for_machine, is_cross,
  350. info, exe_wrap, **kwargs)
  351. IntelGnuLikeCompiler.__init__(self)
  352. self.lang_header = 'c++-header'
  353. default_warn_args = ['-Wall', '-w3', '-diag-disable:remark',
  354. '-Wpch-messages', '-Wnon-virtual-dtor']
  355. self.warn_args = {'0': [],
  356. '1': default_warn_args,
  357. '2': default_warn_args + ['-Wextra'],
  358. '3': default_warn_args + ['-Wextra']}
  359. def get_options(self):
  360. opts = CPPCompiler.get_options(self)
  361. # Every Unix compiler under the sun seems to accept -std=c++03,
  362. # with the exception of ICC. Instead of preventing the user from
  363. # globally requesting C++03, we transparently remap it to C++98
  364. c_stds = ['c++98', 'c++03']
  365. g_stds = ['gnu++98', 'gnu++03']
  366. if version_compare(self.version, '>=15.0.0'):
  367. c_stds += ['c++11', 'c++14']
  368. g_stds += ['gnu++11']
  369. if version_compare(self.version, '>=16.0.0'):
  370. c_stds += ['c++17']
  371. if version_compare(self.version, '>=17.0.0'):
  372. g_stds += ['gnu++14']
  373. opts.update({
  374. 'eh': coredata.UserComboOption(
  375. 'C++ exception handling type.',
  376. ['none', 'default', 'a', 's', 'sc'],
  377. 'default',
  378. ),
  379. 'rtti': coredata.UserBooleanOption('Enable RTTI', True),
  380. 'std': coredata.UserComboOption(
  381. 'C++ language standard to use',
  382. ['none'] + c_stds + g_stds,
  383. 'none',
  384. ),
  385. 'debugstl': coredata.UserBooleanOption('STL debug mode', False),
  386. })
  387. return opts
  388. def get_option_compile_args(self, options):
  389. args = []
  390. std = options['std']
  391. if std.value != 'none':
  392. remap_cpp03 = {
  393. 'c++03': 'c++98',
  394. 'gnu++03': 'gnu++98'
  395. }
  396. args.append('-std=' + remap_cpp03.get(std.value, std.value))
  397. if options['eh'].value == 'none':
  398. args.append('-fno-exceptions')
  399. if not options['rtti'].value:
  400. args.append('-fno-rtti')
  401. if options['debugstl'].value:
  402. args.append('-D_GLIBCXX_DEBUG=1')
  403. return args
  404. def get_option_link_args(self, options):
  405. return []
  406. class VisualStudioLikeCPPCompilerMixin:
  407. """Mixin for C++ specific method overrides in MSVC-like compilers."""
  408. VC_VERSION_MAP = {
  409. 'none': (True, None),
  410. 'vc++11': (True, 11),
  411. 'vc++14': (True, 14),
  412. 'vc++17': (True, 17),
  413. 'vc++latest': (True, "latest"),
  414. 'c++11': (False, 11),
  415. 'c++14': (False, 14),
  416. 'c++17': (False, 17),
  417. 'c++latest': (False, "latest"),
  418. }
  419. def get_option_link_args(self, options):
  420. return options['winlibs'].value[:]
  421. def _get_options_impl(self, opts, cpp_stds: T.List[str]):
  422. opts.update({
  423. 'eh': coredata.UserComboOption(
  424. 'C++ exception handling type.',
  425. ['none', 'default', 'a', 's', 'sc'],
  426. 'default',
  427. ),
  428. 'rtti': coredata.UserBooleanOption('Enable RTTI', True),
  429. 'std': coredata.UserComboOption(
  430. 'C++ language standard to use',
  431. cpp_stds,
  432. 'none',
  433. ),
  434. 'winlibs': coredata.UserArrayOption(
  435. 'Windows libs to link against.',
  436. msvc_winlibs,
  437. ),
  438. })
  439. return opts
  440. def get_option_compile_args(self, options):
  441. args = []
  442. eh = options['eh']
  443. if eh.value == 'default':
  444. args.append('/EHsc')
  445. elif eh.value == 'none':
  446. args.append('/EHs-c-')
  447. else:
  448. args.append('/EH' + eh.value)
  449. if not options['rtti'].value:
  450. args.append('/GR-')
  451. permissive, ver = self.VC_VERSION_MAP[options['std'].value]
  452. if ver is not None:
  453. args.append('/std:c++{}'.format(ver))
  454. if not permissive:
  455. args.append('/permissive-')
  456. return args
  457. def get_compiler_check_args(self):
  458. # XXX: this is a hack because so much GnuLike stuff is in the base CPPCompiler class.
  459. return CLikeCompiler.get_compiler_check_args(self)
  460. class CPP11AsCPP14Mixin:
  461. """Mixin class for VisualStudio and ClangCl to replace C++11 std with C++14.
  462. This is a limitation of Clang and MSVC that ICL doesn't share.
  463. """
  464. def get_option_compile_args(self, options):
  465. # Note: there is no explicit flag for supporting C++11; we attempt to do the best we can
  466. # which means setting the C++ standard version to C++14, in compilers that support it
  467. # (i.e., after VS2015U3)
  468. # if one is using anything before that point, one cannot set the standard.
  469. if options['std'].value in {'vc++11', 'c++11'}:
  470. mlog.warning(self.id, 'does not support C++11;',
  471. 'attempting best effort; setting the standard to C++14', once=True)
  472. # Don't mutate anything we're going to change, we need to use
  473. # deepcopy since we're messing with members, and we can't simply
  474. # copy the members because the option proxy doesn't support it.
  475. options = copy.deepcopy(options)
  476. if options['std'].value == 'vc++11':
  477. options['std'].value = 'vc++14'
  478. else:
  479. options['std'].value = 'c++14'
  480. return super().get_option_compile_args(options)
  481. class VisualStudioCPPCompiler(CPP11AsCPP14Mixin, VisualStudioLikeCPPCompilerMixin, MSVCCompiler, CPPCompiler):
  482. def __init__(self, exelist, version, for_machine: MachineChoice,
  483. is_cross: bool, info: 'MachineInfo', exe_wrap, target, **kwargs):
  484. CPPCompiler.__init__(self, exelist, version, for_machine, is_cross, info, exe_wrap, **kwargs)
  485. MSVCCompiler.__init__(self, target)
  486. self.base_options = ['b_pch', 'b_vscrt'] # FIXME add lto, pgo and the like
  487. self.id = 'msvc'
  488. def get_options(self):
  489. cpp_stds = ['none', 'c++11', 'vc++11']
  490. # Visual Studio 2015 and later
  491. if version_compare(self.version, '>=19'):
  492. cpp_stds.extend(['c++14', 'c++latest', 'vc++latest'])
  493. # Visual Studio 2017 and later
  494. if version_compare(self.version, '>=19.11'):
  495. cpp_stds.extend(['vc++14', 'c++17', 'vc++17'])
  496. return self._get_options_impl(super().get_options(), cpp_stds)
  497. def get_option_compile_args(self, options):
  498. if options['std'].value != 'none' and version_compare(self.version, '<19.00.24210'):
  499. mlog.warning('This version of MSVC does not support cpp_std arguments')
  500. options = copy.copy(options)
  501. options['std'].value = 'none'
  502. args = super().get_option_compile_args(options)
  503. if version_compare(self.version, '<19.11'):
  504. try:
  505. i = args.index('/permissive-')
  506. except ValueError:
  507. return args
  508. del args[i]
  509. return args
  510. class ClangClCPPCompiler(CPP11AsCPP14Mixin, VisualStudioLikeCPPCompilerMixin, ClangClCompiler, CPPCompiler):
  511. def __init__(self, exelist, version, for_machine: MachineChoice,
  512. is_cross, info: 'MachineInfo', exe_wrap, target, **kwargs):
  513. CPPCompiler.__init__(self, exelist, version, for_machine, is_cross,
  514. info, exe_wrap, **kwargs)
  515. ClangClCompiler.__init__(self, target)
  516. self.id = 'clang-cl'
  517. def get_options(self):
  518. cpp_stds = ['none', 'c++11', 'vc++11', 'c++14', 'vc++14', 'c++17', 'vc++17', 'c++latest']
  519. return self._get_options_impl(super().get_options(), cpp_stds)
  520. class IntelClCPPCompiler(VisualStudioLikeCPPCompilerMixin, IntelVisualStudioLikeCompiler, CPPCompiler):
  521. def __init__(self, exelist, version, for_machine: MachineChoice,
  522. is_cross, info: 'MachineInfo', exe_wrap, target, **kwargs):
  523. CPPCompiler.__init__(self, exelist, version, for_machine, is_cross,
  524. info, exe_wrap, **kwargs)
  525. IntelVisualStudioLikeCompiler.__init__(self, target)
  526. def get_options(self):
  527. # This has only been tested with version 19.0,
  528. cpp_stds = ['none', 'c++11', 'vc++11', 'c++14', 'vc++14', 'c++17', 'vc++17', 'c++latest']
  529. return self._get_options_impl(super().get_options(), cpp_stds)
  530. class ArmCPPCompiler(ArmCompiler, CPPCompiler):
  531. def __init__(self, exelist, version, for_machine: MachineChoice,
  532. is_cross, info: 'MachineInfo', exe_wrap=None, **kwargs):
  533. CPPCompiler.__init__(self, exelist, version, for_machine, is_cross,
  534. info, exe_wrap, **kwargs)
  535. ArmCompiler.__init__(self)
  536. def get_options(self):
  537. opts = CPPCompiler.get_options(self)
  538. opts.update({
  539. 'std': coredata.UserComboOption(
  540. 'C++ language standard to use',
  541. ['none', 'c++03', 'c++11'],
  542. 'none',
  543. ),
  544. })
  545. return opts
  546. def get_option_compile_args(self, options):
  547. args = []
  548. std = options['std']
  549. if std.value == 'c++11':
  550. args.append('--cpp11')
  551. elif std.value == 'c++03':
  552. args.append('--cpp')
  553. return args
  554. def get_option_link_args(self, options):
  555. return []
  556. def get_compiler_check_args(self):
  557. return []
  558. class CcrxCPPCompiler(CcrxCompiler, CPPCompiler):
  559. def __init__(self, exelist, version, for_machine: MachineChoice,
  560. is_cross, info: 'MachineInfo', exe_wrap=None, **kwargs):
  561. CPPCompiler.__init__(self, exelist, version, for_machine, is_cross,
  562. info, exe_wrap, **kwargs)
  563. CcrxCompiler.__init__(self)
  564. # Override CCompiler.get_always_args
  565. def get_always_args(self):
  566. return ['-nologo', '-lang=cpp']
  567. def get_option_compile_args(self, options):
  568. return []
  569. def get_compile_only_args(self):
  570. return []
  571. def get_output_args(self, target):
  572. return ['-output=obj=%s' % target]
  573. def get_option_link_args(self, options):
  574. return []
  575. def get_compiler_check_args(self):
  576. return []
  577. class C2000CPPCompiler(C2000Compiler, CPPCompiler):
  578. def __init__(self, exelist, version, for_machine: MachineChoice,
  579. is_cross, info: 'MachineInfo', exe_wrap=None, **kwargs):
  580. CPPCompiler.__init__(self, exelist, version, for_machine, is_cross,
  581. info, exe_wrap, **kwargs)
  582. C2000Compiler.__init__(self)
  583. def get_options(self):
  584. opts = CPPCompiler.get_options(self)
  585. opts.update({'cpp_std': coredata.UserComboOption('C++ language standard to use',
  586. ['none', 'c++03'],
  587. 'none')})
  588. return opts
  589. def get_always_args(self):
  590. return ['-nologo', '-lang=cpp']
  591. def get_option_compile_args(self, options):
  592. return []
  593. def get_compile_only_args(self):
  594. return []
  595. def get_output_args(self, target):
  596. return ['-output=obj=%s' % target]
  597. def get_option_link_args(self, options):
  598. return []
  599. def get_compiler_check_args(self):
  600. return []