Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 1 | # -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*- |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 2 | |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 3 | from waflib import Logs, Configure, Utils |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 4 | |
| 5 | def options(opt): |
| 6 | opt.add_option('--debug', '--with-debug', action='store_true', default=False, dest='debug', |
Davide Pesavento | dfe9c6b | 2014-08-25 21:17:10 +0200 | [diff] [blame] | 7 | help='''Compile in debugging mode without optimizations (-O0 or -Og)''') |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 8 | |
| 9 | def configure(conf): |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 10 | cxx = conf.env['CXX_NAME'] # CXX_NAME represents generic name of the compiler |
| 11 | if cxx == 'gcc': |
| 12 | flags = GccFlags() |
| 13 | elif cxx == 'clang': |
| 14 | flags = ClangFlags() |
| 15 | else: |
| 16 | flags = CompilerFlags() |
| 17 | Logs.warn('The code has not been yet tested with %s compiler' % cxx) |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 18 | |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 19 | areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0) |
| 20 | |
| 21 | # General flags will alway be applied (e.g., selecting C++11 mode) |
| 22 | generalFlags = flags.getGeneralFlags(conf) |
| 23 | conf.add_supported_cxxflags(generalFlags['CXXFLAGS']) |
| 24 | conf.add_supported_linkflags(generalFlags['LINKFLAGS']) |
| 25 | conf.env.DEFINES += generalFlags['DEFINES'] |
| 26 | |
| 27 | # Debug or optimization CXXFLAGS and LINKFLAGS will be applied only if the |
| 28 | # corresponding environment variables are not set. |
| 29 | # DEFINES will be always applied |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 30 | if conf.options.debug: |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 31 | extraFlags = flags.getDebugFlags(conf) |
| 32 | |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 33 | if areCustomCxxflagsPresent: |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 34 | missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS] |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 35 | if len(missingFlags) > 0: |
| 36 | Logs.warn("Selected debug mode, but CXXFLAGS is set to a custom value '%s'" |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 37 | % " ".join(conf.env.CXXFLAGS)) |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 38 | Logs.warn("Default flags '%s' are not activated" % " ".join(missingFlags)) |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 39 | else: |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 40 | extraFlags = flags.getOptimizedFlags(conf) |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 41 | |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 42 | if not areCustomCxxflagsPresent: |
| 43 | conf.add_supported_cxxflags(extraFlags['CXXFLAGS']) |
| 44 | conf.add_supported_cxxflags(extraFlags['LINKFLAGS']) |
| 45 | |
| 46 | conf.env.DEFINES += extraFlags['DEFINES'] |
Davide Pesavento | dfe9c6b | 2014-08-25 21:17:10 +0200 | [diff] [blame] | 47 | |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 48 | @Configure.conf |
| 49 | def add_supported_cxxflags(self, cxxflags): |
| 50 | """ |
| 51 | Check which cxxflags are supported by compiler and add them to env.CXXFLAGS variable |
| 52 | """ |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 53 | if len(cxxflags) == 0: |
| 54 | return |
| 55 | |
Davide Pesavento | dfe9c6b | 2014-08-25 21:17:10 +0200 | [diff] [blame] | 56 | self.start_msg('Checking supported CXXFLAGS') |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 57 | |
| 58 | supportedFlags = [] |
| 59 | for flag in cxxflags: |
Alexander Afanasyev | 3e6a98e | 2014-04-25 12:02:13 -0700 | [diff] [blame] | 60 | if self.check_cxx(cxxflags=['-Werror', flag], mandatory=False): |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 61 | supportedFlags += [flag] |
| 62 | |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 63 | self.end_msg(' '.join(supportedFlags)) |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 64 | self.env.CXXFLAGS = supportedFlags + self.env.CXXFLAGS |
Davide Pesavento | dfe9c6b | 2014-08-25 21:17:10 +0200 | [diff] [blame] | 65 | |
| 66 | @Configure.conf |
| 67 | def add_supported_linkflags(self, linkflags): |
| 68 | """ |
| 69 | Check which linkflags are supported by compiler and add them to env.LINKFLAGS variable |
| 70 | """ |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 71 | if len(linkflags) == 0: |
| 72 | return |
| 73 | |
Davide Pesavento | dfe9c6b | 2014-08-25 21:17:10 +0200 | [diff] [blame] | 74 | self.start_msg('Checking supported LINKFLAGS') |
| 75 | |
| 76 | supportedFlags = [] |
| 77 | for flag in linkflags: |
| 78 | if self.check_cxx(linkflags=['-Werror', flag], mandatory=False): |
| 79 | supportedFlags += [flag] |
| 80 | |
| 81 | self.end_msg(' '.join(supportedFlags)) |
| 82 | self.env.LINKFLAGS = supportedFlags + self.env.LINKFLAGS |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 83 | |
| 84 | |
| 85 | class CompilerFlags(object): |
| 86 | def getGeneralFlags(self, conf): |
| 87 | """Get dict {'CXXFLAGS':[...], LINKFLAGS:[...], DEFINES:[...]} that are always needed""" |
| 88 | return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []} |
| 89 | |
| 90 | def getDebugFlags(self, conf): |
| 91 | """Get tuple {CXXFLAGS, LINKFLAGS, DEFINES} that are needed in debug mode""" |
| 92 | return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']} |
| 93 | |
| 94 | def getOptimizedFlags(self, conf): |
| 95 | """Get tuple {CXXFLAGS, LINKFLAGS, DEFINES} that are needed in optimized mode""" |
| 96 | return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']} |
| 97 | |
| 98 | class GccBasicFlags(CompilerFlags): |
| 99 | """This class defines base flags that work for gcc and clang compiler""" |
| 100 | def getDebugFlags(self, conf): |
| 101 | flags = super(GccBasicFlags, self).getDebugFlags(conf) |
| 102 | flags['CXXFLAGS'] += ['-pedantic', '-Wall', |
| 103 | '-O0', |
| 104 | '-g3', |
| 105 | '-Werror', |
| 106 | '-Wno-error=maybe-uninitialized', # Bug #1615 |
| 107 | ] |
| 108 | return flags |
| 109 | |
| 110 | def getOptimizedFlags(self, conf): |
| 111 | flags = super(GccBasicFlags, self).getOptimizedFlags(conf) |
| 112 | flags['CXXFLAGS'] += ['-pedantic', '-Wall', '-O2', '-g'] |
| 113 | return flags |
| 114 | |
| 115 | class GccFlags(GccBasicFlags): |
| 116 | def getGeneralFlags(self, conf): |
| 117 | flags = super(GccFlags, self).getGeneralFlags(conf) |
| 118 | version = tuple(int(i) for i in conf.env['CC_VERSION']) |
| 119 | if version < (4, 6, 0): |
| 120 | conf.fatal('The version of gcc you are using (%s) is too old.\n' % |
Stephen McQuistin | e3d6266 | 2015-01-15 15:08:34 +0000 | [diff] [blame] | 121 | '.'.join(conf.env['CC_VERSION']) + |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 122 | 'The minimum supported gcc version is 4.6.0.') |
| 123 | elif version < (4, 7, 0): |
| 124 | flags['CXXFLAGS'] += ['-std=c++0x'] |
| 125 | else: |
| 126 | flags['CXXFLAGS'] += ['-std=c++11'] |
Alexander Afanasyev | 3e7d2ac | 2015-02-10 19:39:28 -0800 | [diff] [blame] | 127 | if version < (4, 8, 0): |
| 128 | flags['DEFINES'] += ['_GLIBCXX_USE_NANOSLEEP'] # Bug #2499 |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 129 | return flags |
| 130 | |
| 131 | def getDebugFlags(self, conf): |
| 132 | flags = super(GccFlags, self).getDebugFlags(conf) |
| 133 | flags['CXXFLAGS'] += ['-Og', # gcc >= 4.8 |
| 134 | '-fdiagnostics-color', # gcc >= 4.9 |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 135 | ] |
| 136 | return flags |
| 137 | |
| 138 | class ClangFlags(GccBasicFlags): |
| 139 | def getGeneralFlags(self, conf): |
| 140 | flags = super(ClangFlags, self).getGeneralFlags(conf) |
Alexander Afanasyev | c940f4b | 2015-01-22 13:36:15 -0800 | [diff] [blame] | 141 | flags['CXXFLAGS'] += ['-std=c++11', |
| 142 | '-Wno-error=unneeded-internal-declaration', # Bug #1588 |
| 143 | '-Wno-error=deprecated-register', |
Alexander Afanasyev | 0dcb6a2 | 2015-03-20 14:43:53 -0700 | [diff] [blame] | 144 | '-Wno-error=unused-local-typedef', # Bug #2657 |
Alexander Afanasyev | c940f4b | 2015-01-22 13:36:15 -0800 | [diff] [blame] | 145 | ] |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 146 | if Utils.unversioned_sys_platform() == "darwin": |
| 147 | flags['CXXFLAGS'] += ['-stdlib=libc++'] |
| 148 | flags['LINKFLAGS'] += ['-stdlib=libc++'] |
| 149 | return flags |
| 150 | |
| 151 | def getDebugFlags(self, conf): |
| 152 | flags = super(ClangFlags, self).getDebugFlags(conf) |
| 153 | flags['CXXFLAGS'] += ['-fcolor-diagnostics'] |
| 154 | return flags |