compiler_cxx.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. # Matthias Jahn jahn dôt matthias ât freenet dôt de 2007 (pmarat)
  4. """
  5. Try to detect a C++ compiler from the list of supported compilers (g++, msvc, etc)::
  6. def options(opt):
  7. opt.load('compiler_cxx')
  8. def configure(cnf):
  9. cnf.load('compiler_cxx')
  10. def build(bld):
  11. bld.program(source='main.cpp', target='app')
  12. The compilers are associated to platforms in :py:attr:`waflib.Tools.compiler_cxx.cxx_compiler`. To register
  13. a new C++ compiler named *cfoo* (assuming the tool ``waflib/extras/cfoo.py`` exists), use::
  14. from waflib.Tools.compiler_cxx import cxx_compiler
  15. cxx_compiler['win32'] = ['cfoo', 'msvc', 'gcc']
  16. def options(opt):
  17. opt.load('compiler_cxx')
  18. def configure(cnf):
  19. cnf.load('compiler_cxx')
  20. def build(bld):
  21. bld.program(source='main.c', target='app')
  22. Not all compilers need to have a specific tool. For example, the clang compilers can be detected by the gcc tools when using::
  23. $ CXX=clang waf configure
  24. """
  25. import re
  26. from waflib.Tools import ccroot
  27. from waflib import Utils
  28. from waflib.Logs import debug
  29. cxx_compiler = {
  30. 'win32': ['msvc', 'g++', 'clang++'],
  31. 'cygwin': ['g++'],
  32. 'darwin': ['clang++', 'g++'],
  33. 'aix': ['xlc++', 'g++', 'clang++'],
  34. 'linux': ['g++', 'clang++', 'icpc'],
  35. 'sunos': ['sunc++', 'g++'],
  36. 'irix': ['g++'],
  37. 'hpux': ['g++'],
  38. 'osf1V': ['g++'],
  39. 'gnu': ['g++', 'clang++'],
  40. 'java': ['g++', 'msvc', 'clang++', 'icpc'],
  41. 'default': ['clang++', 'g++']
  42. }
  43. """
  44. Dict mapping the platform names to Waf tools finding specific C++ compilers::
  45. from waflib.Tools.compiler_cxx import cxx_compiler
  46. cxx_compiler['linux'] = ['gxx', 'icpc', 'suncxx']
  47. """
  48. def default_compilers():
  49. build_platform = Utils.unversioned_sys_platform()
  50. possible_compiler_list = cxx_compiler.get(build_platform, cxx_compiler['default'])
  51. return ' '.join(possible_compiler_list)
  52. def configure(conf):
  53. """
  54. Detects a suitable C++ compiler
  55. :raises: :py:class:`waflib.Errors.ConfigurationError` when no suitable compiler is found
  56. """
  57. try:
  58. test_for_compiler = conf.options.check_cxx_compiler or default_compilers()
  59. except AttributeError:
  60. conf.fatal("Add options(opt): opt.load('compiler_cxx')")
  61. for compiler in re.split('[ ,]+', test_for_compiler):
  62. conf.env.stash()
  63. conf.start_msg('Checking for %r (C++ compiler)' % compiler)
  64. try:
  65. conf.load(compiler)
  66. except conf.errors.ConfigurationError as e:
  67. conf.env.revert()
  68. conf.end_msg(False)
  69. debug('compiler_cxx: %r', e)
  70. else:
  71. if conf.env.CXX:
  72. conf.end_msg(conf.env.get_flat('CXX'))
  73. conf.env.COMPILER_CXX = compiler
  74. conf.env.commit()
  75. break
  76. conf.env.revert()
  77. conf.end_msg(False)
  78. else:
  79. conf.fatal('could not configure a C++ compiler!')
  80. def options(opt):
  81. """
  82. This is how to provide compiler preferences on the command-line::
  83. $ waf configure --check-cxx-compiler=gxx
  84. """
  85. test_for_compiler = default_compilers()
  86. opt.load_special_tools('cxx_*.py')
  87. cxx_compiler_opts = opt.add_option_group('Configuration options')
  88. cxx_compiler_opts.add_option('--check-cxx-compiler', default=None,
  89. help='list of C++ compilers to try [%s]' % test_for_compiler,
  90. dest="check_cxx_compiler")
  91. for x in test_for_compiler.split():
  92. opt.load('%s' % x)