Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 1 | # -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*- |
| 2 | |
| 3 | from waflib import Logs, Configure, Utils |
| 4 | |
| 5 | def options(opt): |
| 6 | opt.add_option('--debug', '--with-debug', action='store_true', default=False, dest='debug', |
| 7 | help='''Compile in debugging mode without optimizations (-O0 or -Og)''') |
| 8 | |
| 9 | def configure(conf): |
| 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() |
Junxiao Shi | c8a0a25 | 2015-10-05 07:25:02 -0700 | [diff] [blame] | 17 | Logs.warn('The code has not yet been tested with %s compiler' % cxx) |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 18 | |
| 19 | areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0) |
| 20 | |
Junxiao Shi | c8a0a25 | 2015-10-05 07:25:02 -0700 | [diff] [blame] | 21 | # General flags are always applied (e.g., selecting C++11 mode) |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 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 | |
Junxiao Shi | c8a0a25 | 2015-10-05 07:25:02 -0700 | [diff] [blame] | 27 | # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 28 | # corresponding environment variables are not set. |
Junxiao Shi | c8a0a25 | 2015-10-05 07:25:02 -0700 | [diff] [blame] | 29 | # DEFINES are always applied. |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 30 | if conf.options.debug: |
| 31 | extraFlags = flags.getDebugFlags(conf) |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 32 | if areCustomCxxflagsPresent: |
| 33 | missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS] |
| 34 | if len(missingFlags) > 0: |
| 35 | Logs.warn("Selected debug mode, but CXXFLAGS is set to a custom value '%s'" |
| 36 | % " ".join(conf.env.CXXFLAGS)) |
| 37 | Logs.warn("Default flags '%s' are not activated" % " ".join(missingFlags)) |
| 38 | else: |
| 39 | extraFlags = flags.getOptimizedFlags(conf) |
| 40 | |
| 41 | if not areCustomCxxflagsPresent: |
| 42 | conf.add_supported_cxxflags(extraFlags['CXXFLAGS']) |
Davide Pesavento | 3e79c9c | 2015-11-01 20:38:28 +0100 | [diff] [blame^] | 43 | conf.add_supported_linkflags(extraFlags['LINKFLAGS']) |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 44 | |
| 45 | conf.env.DEFINES += extraFlags['DEFINES'] |
| 46 | |
| 47 | @Configure.conf |
| 48 | def add_supported_cxxflags(self, cxxflags): |
| 49 | """ |
| 50 | Check which cxxflags are supported by compiler and add them to env.CXXFLAGS variable |
| 51 | """ |
| 52 | if len(cxxflags) == 0: |
| 53 | return |
| 54 | |
| 55 | self.start_msg('Checking supported CXXFLAGS') |
| 56 | |
| 57 | supportedFlags = [] |
| 58 | for flag in cxxflags: |
| 59 | if self.check_cxx(cxxflags=['-Werror', flag], mandatory=False): |
| 60 | supportedFlags += [flag] |
| 61 | |
| 62 | self.end_msg(' '.join(supportedFlags)) |
| 63 | self.env.CXXFLAGS = supportedFlags + self.env.CXXFLAGS |
| 64 | |
| 65 | @Configure.conf |
| 66 | def add_supported_linkflags(self, linkflags): |
| 67 | """ |
| 68 | Check which linkflags are supported by compiler and add them to env.LINKFLAGS variable |
| 69 | """ |
| 70 | if len(linkflags) == 0: |
| 71 | return |
| 72 | |
| 73 | self.start_msg('Checking supported LINKFLAGS') |
| 74 | |
| 75 | supportedFlags = [] |
| 76 | for flag in linkflags: |
| 77 | if self.check_cxx(linkflags=['-Werror', flag], mandatory=False): |
| 78 | supportedFlags += [flag] |
| 79 | |
| 80 | self.end_msg(' '.join(supportedFlags)) |
| 81 | self.env.LINKFLAGS = supportedFlags + self.env.LINKFLAGS |
| 82 | |
| 83 | |
| 84 | class CompilerFlags(object): |
| 85 | def getGeneralFlags(self, conf): |
Davide Pesavento | 3e79c9c | 2015-11-01 20:38:28 +0100 | [diff] [blame^] | 86 | """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed""" |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 87 | return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []} |
| 88 | |
| 89 | def getDebugFlags(self, conf): |
Davide Pesavento | 3e79c9c | 2015-11-01 20:38:28 +0100 | [diff] [blame^] | 90 | """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode""" |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 91 | return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']} |
| 92 | |
| 93 | def getOptimizedFlags(self, conf): |
Davide Pesavento | 3e79c9c | 2015-11-01 20:38:28 +0100 | [diff] [blame^] | 94 | """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode""" |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 95 | return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']} |
| 96 | |
| 97 | class GccBasicFlags(CompilerFlags): |
Junxiao Shi | c8a0a25 | 2015-10-05 07:25:02 -0700 | [diff] [blame] | 98 | """ |
| 99 | This class defines basic flags that work for both gcc and clang compilers |
| 100 | """ |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 101 | def getDebugFlags(self, conf): |
| 102 | flags = super(GccBasicFlags, self).getDebugFlags(conf) |
Davide Pesavento | 3e79c9c | 2015-11-01 20:38:28 +0100 | [diff] [blame^] | 103 | flags['CXXFLAGS'] += ['-O0', |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 104 | '-g3', |
Davide Pesavento | 3e79c9c | 2015-11-01 20:38:28 +0100 | [diff] [blame^] | 105 | '-pedantic', |
| 106 | '-Wall', |
| 107 | '-Wextra', |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 108 | '-Werror', |
Davide Pesavento | 3e79c9c | 2015-11-01 20:38:28 +0100 | [diff] [blame^] | 109 | '-Wno-unused-parameter', |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 110 | '-Wno-error=maybe-uninitialized', # Bug #1615 |
Junxiao Shi | c8a0a25 | 2015-10-05 07:25:02 -0700 | [diff] [blame] | 111 | ] |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 112 | return flags |
| 113 | |
| 114 | def getOptimizedFlags(self, conf): |
| 115 | flags = super(GccBasicFlags, self).getOptimizedFlags(conf) |
Davide Pesavento | 3e79c9c | 2015-11-01 20:38:28 +0100 | [diff] [blame^] | 116 | flags['CXXFLAGS'] += ['-O2', |
Junxiao Shi | c8a0a25 | 2015-10-05 07:25:02 -0700 | [diff] [blame] | 117 | '-g', |
Davide Pesavento | 3e79c9c | 2015-11-01 20:38:28 +0100 | [diff] [blame^] | 118 | '-pedantic', |
| 119 | '-Wall', |
| 120 | '-Wextra', |
| 121 | '-Wno-unused-parameter', |
Junxiao Shi | c8a0a25 | 2015-10-05 07:25:02 -0700 | [diff] [blame] | 122 | ] |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 123 | return flags |
| 124 | |
| 125 | class GccFlags(GccBasicFlags): |
| 126 | def getGeneralFlags(self, conf): |
| 127 | flags = super(GccFlags, self).getGeneralFlags(conf) |
| 128 | version = tuple(int(i) for i in conf.env['CC_VERSION']) |
| 129 | if version < (4, 6, 0): |
| 130 | conf.fatal('The version of gcc you are using (%s) is too old.\n' % |
| 131 | '.'.join(conf.env['CC_VERSION']) + |
| 132 | 'The minimum supported gcc version is 4.6.0.') |
| 133 | elif version < (4, 7, 0): |
| 134 | flags['CXXFLAGS'] += ['-std=c++0x'] |
| 135 | else: |
| 136 | flags['CXXFLAGS'] += ['-std=c++11'] |
| 137 | if version < (4, 8, 0): |
| 138 | flags['DEFINES'] += ['_GLIBCXX_USE_NANOSLEEP'] # Bug #2499 |
| 139 | return flags |
| 140 | |
| 141 | def getDebugFlags(self, conf): |
| 142 | flags = super(GccFlags, self).getDebugFlags(conf) |
Davide Pesavento | 3e79c9c | 2015-11-01 20:38:28 +0100 | [diff] [blame^] | 143 | version = tuple(int(i) for i in conf.env['CC_VERSION']) |
| 144 | if version < (5, 1, 0): |
| 145 | flags['CXXFLAGS'] += ['-Wno-missing-field-initializers'] |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 146 | flags['CXXFLAGS'] += ['-Og', # gcc >= 4.8 |
| 147 | '-fdiagnostics-color', # gcc >= 4.9 |
Junxiao Shi | c8a0a25 | 2015-10-05 07:25:02 -0700 | [diff] [blame] | 148 | ] |
| 149 | return flags |
| 150 | |
| 151 | def getOptimizedFlags(self, conf): |
| 152 | flags = super(GccFlags, self).getOptimizedFlags(conf) |
Davide Pesavento | 3e79c9c | 2015-11-01 20:38:28 +0100 | [diff] [blame^] | 153 | version = tuple(int(i) for i in conf.env['CC_VERSION']) |
| 154 | if version < (5, 1, 0): |
| 155 | flags['CXXFLAGS'] += ['-Wno-missing-field-initializers'] |
Junxiao Shi | c8a0a25 | 2015-10-05 07:25:02 -0700 | [diff] [blame] | 156 | flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9 |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 157 | return flags |
| 158 | |
| 159 | class ClangFlags(GccBasicFlags): |
| 160 | def getGeneralFlags(self, conf): |
| 161 | flags = super(ClangFlags, self).getGeneralFlags(conf) |
Junxiao Shi | c8a0a25 | 2015-10-05 07:25:02 -0700 | [diff] [blame] | 162 | flags['CXXFLAGS'] += ['-std=c++11'] |
| 163 | if Utils.unversioned_sys_platform() == 'darwin': |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 164 | flags['CXXFLAGS'] += ['-stdlib=libc++'] |
| 165 | flags['LINKFLAGS'] += ['-stdlib=libc++'] |
| 166 | return flags |
| 167 | |
| 168 | def getDebugFlags(self, conf): |
| 169 | flags = super(ClangFlags, self).getDebugFlags(conf) |
Junxiao Shi | c8a0a25 | 2015-10-05 07:25:02 -0700 | [diff] [blame] | 170 | flags['CXXFLAGS'] += ['-fcolor-diagnostics', |
| 171 | '-Wno-unused-local-typedef', # Bugs #2657 and #3209 |
| 172 | '-Wno-error=unneeded-internal-declaration', # Bug #1588 |
| 173 | '-Wno-error=deprecated-register', |
| 174 | '-Wno-error=keyword-macro', # Bug #3235 |
| 175 | ] |
| 176 | return flags |
| 177 | |
| 178 | def getOptimizedFlags(self, conf): |
| 179 | flags = super(ClangFlags, self).getOptimizedFlags(conf) |
| 180 | flags['CXXFLAGS'] += ['-fcolor-diagnostics', |
| 181 | '-Wno-unused-local-typedef', # Bugs #2657 and #3209 |
| 182 | ] |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 183 | return flags |