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() |
Davide Pesavento | d06ea05 | 2015-10-02 01:48:24 +0200 | [diff] [blame] | 17 | Logs.warn('The code has not yet been 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 | |
Davide Pesavento | d06ea05 | 2015-10-02 01:48:24 +0200 | [diff] [blame] | 21 | # General flags are always applied (e.g., selecting C++11 mode) |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [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 | |
Davide Pesavento | d06ea05 | 2015-10-02 01:48:24 +0200 | [diff] [blame] | 27 | # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 28 | # corresponding environment variables are not set. |
Davide Pesavento | d06ea05 | 2015-10-02 01:48:24 +0200 | [diff] [blame] | 29 | # DEFINES are 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) |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 32 | if areCustomCxxflagsPresent: |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 33 | 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] | 34 | if len(missingFlags) > 0: |
| 35 | 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] | 36 | % " ".join(conf.env.CXXFLAGS)) |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 37 | Logs.warn("Default flags '%s' are not activated" % " ".join(missingFlags)) |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 38 | else: |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 39 | extraFlags = flags.getOptimizedFlags(conf) |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 40 | |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 41 | if not areCustomCxxflagsPresent: |
| 42 | conf.add_supported_cxxflags(extraFlags['CXXFLAGS']) |
Davide Pesavento | 7c02b47 | 2015-10-28 20:50:17 +0100 | [diff] [blame] | 43 | conf.add_supported_linkflags(extraFlags['LINKFLAGS']) |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 44 | |
| 45 | conf.env.DEFINES += extraFlags['DEFINES'] |
Davide Pesavento | dfe9c6b | 2014-08-25 21:17:10 +0200 | [diff] [blame] | 46 | |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 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 | """ |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 52 | if len(cxxflags) == 0: |
| 53 | return |
| 54 | |
Davide Pesavento | dfe9c6b | 2014-08-25 21:17:10 +0200 | [diff] [blame] | 55 | self.start_msg('Checking supported CXXFLAGS') |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 56 | |
| 57 | supportedFlags = [] |
| 58 | for flag in cxxflags: |
Alexander Afanasyev | 3e6a98e | 2014-04-25 12:02:13 -0700 | [diff] [blame] | 59 | if self.check_cxx(cxxflags=['-Werror', flag], mandatory=False): |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 60 | supportedFlags += [flag] |
| 61 | |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 62 | self.end_msg(' '.join(supportedFlags)) |
Davide Pesavento | 1349d2d | 2016-08-09 06:43:57 +0200 | [diff] [blame] | 63 | self.env.prepend_value('CXXFLAGS', supportedFlags) |
Davide Pesavento | dfe9c6b | 2014-08-25 21:17:10 +0200 | [diff] [blame] | 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 | """ |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 70 | if len(linkflags) == 0: |
| 71 | return |
| 72 | |
Davide Pesavento | dfe9c6b | 2014-08-25 21:17:10 +0200 | [diff] [blame] | 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)) |
Davide Pesavento | 1349d2d | 2016-08-09 06:43:57 +0200 | [diff] [blame] | 81 | self.env.prepend_value('LINKFLAGS', supportedFlags) |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 82 | |
| 83 | |
| 84 | class CompilerFlags(object): |
| 85 | def getGeneralFlags(self, conf): |
Davide Pesavento | 7c02b47 | 2015-10-28 20:50:17 +0100 | [diff] [blame] | 86 | """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed""" |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 87 | return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []} |
| 88 | |
| 89 | def getDebugFlags(self, conf): |
Davide Pesavento | 7c02b47 | 2015-10-28 20:50:17 +0100 | [diff] [blame] | 90 | """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode""" |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 91 | return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']} |
| 92 | |
| 93 | def getOptimizedFlags(self, conf): |
Davide Pesavento | 7c02b47 | 2015-10-28 20:50:17 +0100 | [diff] [blame] | 94 | """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode""" |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 95 | return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']} |
| 96 | |
| 97 | class GccBasicFlags(CompilerFlags): |
Davide Pesavento | d06ea05 | 2015-10-02 01:48:24 +0200 | [diff] [blame] | 98 | """ |
| 99 | This class defines basic flags that work for both gcc and clang compilers |
| 100 | """ |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 101 | def getDebugFlags(self, conf): |
| 102 | flags = super(GccBasicFlags, self).getDebugFlags(conf) |
Davide Pesavento | 7c02b47 | 2015-10-28 20:50:17 +0100 | [diff] [blame] | 103 | flags['CXXFLAGS'] += ['-O0', |
Davide Pesavento | f714c1f | 2017-04-12 23:53:40 -0400 | [diff] [blame] | 104 | '-Og', # gcc >= 4.8, clang >= 4.0 |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 105 | '-g3', |
Davide Pesavento | 7c02b47 | 2015-10-28 20:50:17 +0100 | [diff] [blame] | 106 | '-pedantic', |
| 107 | '-Wall', |
| 108 | '-Wextra', |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 109 | '-Werror', |
Davide Pesavento | 7f20d6e | 2017-01-16 14:43:58 -0500 | [diff] [blame] | 110 | '-Wnon-virtual-dtor', |
Davide Pesavento | 7c02b47 | 2015-10-28 20:50:17 +0100 | [diff] [blame] | 111 | '-Wno-unused-parameter', |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 112 | '-Wno-error=maybe-uninitialized', # Bug #1615 |
Alexander Afanasyev | 465c973 | 2016-10-11 16:35:32 -0700 | [diff] [blame] | 113 | '-Wno-error=deprecated-declarations', # Bug #3795 |
Davide Pesavento | 1aafba4 | 2015-09-17 17:22:31 -0700 | [diff] [blame] | 114 | ] |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 115 | return flags |
| 116 | |
| 117 | def getOptimizedFlags(self, conf): |
| 118 | flags = super(GccBasicFlags, self).getOptimizedFlags(conf) |
Davide Pesavento | 7c02b47 | 2015-10-28 20:50:17 +0100 | [diff] [blame] | 119 | flags['CXXFLAGS'] += ['-O2', |
Davide Pesavento | 1aafba4 | 2015-09-17 17:22:31 -0700 | [diff] [blame] | 120 | '-g', |
Davide Pesavento | 7c02b47 | 2015-10-28 20:50:17 +0100 | [diff] [blame] | 121 | '-pedantic', |
| 122 | '-Wall', |
| 123 | '-Wextra', |
Davide Pesavento | 7f20d6e | 2017-01-16 14:43:58 -0500 | [diff] [blame] | 124 | '-Wnon-virtual-dtor', |
Davide Pesavento | 7c02b47 | 2015-10-28 20:50:17 +0100 | [diff] [blame] | 125 | '-Wno-unused-parameter', |
Davide Pesavento | 1aafba4 | 2015-09-17 17:22:31 -0700 | [diff] [blame] | 126 | ] |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 127 | return flags |
| 128 | |
| 129 | class GccFlags(GccBasicFlags): |
| 130 | def getGeneralFlags(self, conf): |
| 131 | flags = super(GccFlags, self).getGeneralFlags(conf) |
| 132 | version = tuple(int(i) for i in conf.env['CC_VERSION']) |
Davide Pesavento | e6e6fde | 2016-04-16 14:44:45 +0200 | [diff] [blame] | 133 | if version < (4, 8, 2): |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 134 | 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] | 135 | '.'.join(conf.env['CC_VERSION']) + |
Davide Pesavento | e6e6fde | 2016-04-16 14:44:45 +0200 | [diff] [blame] | 136 | 'The minimum supported gcc version is 4.8.2.') |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 137 | else: |
| 138 | flags['CXXFLAGS'] += ['-std=c++11'] |
| 139 | return flags |
| 140 | |
| 141 | def getDebugFlags(self, conf): |
| 142 | flags = super(GccFlags, self).getDebugFlags(conf) |
Davide Pesavento | 7c02b47 | 2015-10-28 20:50:17 +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'] |
Davide Pesavento | f714c1f | 2017-04-12 23:53:40 -0400 | [diff] [blame] | 146 | flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9 |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 147 | return flags |
| 148 | |
Davide Pesavento | d06ea05 | 2015-10-02 01:48:24 +0200 | [diff] [blame] | 149 | def getOptimizedFlags(self, conf): |
| 150 | flags = super(GccFlags, self).getOptimizedFlags(conf) |
Davide Pesavento | 7c02b47 | 2015-10-28 20:50:17 +0100 | [diff] [blame] | 151 | version = tuple(int(i) for i in conf.env['CC_VERSION']) |
| 152 | if version < (5, 1, 0): |
| 153 | flags['CXXFLAGS'] += ['-Wno-missing-field-initializers'] |
Davide Pesavento | d06ea05 | 2015-10-02 01:48:24 +0200 | [diff] [blame] | 154 | flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9 |
| 155 | return flags |
| 156 | |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 157 | class ClangFlags(GccBasicFlags): |
| 158 | def getGeneralFlags(self, conf): |
| 159 | flags = super(ClangFlags, self).getGeneralFlags(conf) |
Davide Pesavento | 1aafba4 | 2015-09-17 17:22:31 -0700 | [diff] [blame] | 160 | flags['CXXFLAGS'] += ['-std=c++11'] |
Davide Pesavento | d06ea05 | 2015-10-02 01:48:24 +0200 | [diff] [blame] | 161 | if Utils.unversioned_sys_platform() == 'darwin': |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 162 | flags['CXXFLAGS'] += ['-stdlib=libc++'] |
| 163 | flags['LINKFLAGS'] += ['-stdlib=libc++'] |
| 164 | return flags |
| 165 | |
| 166 | def getDebugFlags(self, conf): |
| 167 | flags = super(ClangFlags, self).getDebugFlags(conf) |
Davide Pesavento | 1aafba4 | 2015-09-17 17:22:31 -0700 | [diff] [blame] | 168 | flags['CXXFLAGS'] += ['-fcolor-diagnostics', |
Davide Pesavento | d06ea05 | 2015-10-02 01:48:24 +0200 | [diff] [blame] | 169 | '-Wno-unused-local-typedef', # Bugs #2657 and #3209 |
Davide Pesavento | 1aafba4 | 2015-09-17 17:22:31 -0700 | [diff] [blame] | 170 | '-Wno-error=unneeded-internal-declaration', # Bug #1588 |
| 171 | '-Wno-error=deprecated-register', |
Junxiao Shi | b1ba3e0 | 2015-10-04 16:56:37 -0700 | [diff] [blame] | 172 | '-Wno-error=keyword-macro', # Bug #3235 |
Alexander Afanasyev | efa22d6 | 2015-12-04 13:39:17 -0800 | [diff] [blame] | 173 | '-Wno-error=infinite-recursion', # Bug #3358 |
Davide Pesavento | d06ea05 | 2015-10-02 01:48:24 +0200 | [diff] [blame] | 174 | ] |
| 175 | return flags |
| 176 | |
| 177 | def getOptimizedFlags(self, conf): |
| 178 | flags = super(ClangFlags, self).getOptimizedFlags(conf) |
| 179 | flags['CXXFLAGS'] += ['-fcolor-diagnostics', |
| 180 | '-Wno-unused-local-typedef', # Bugs #2657 and #3209 |
Davide Pesavento | 1aafba4 | 2015-09-17 17:22:31 -0700 | [diff] [blame] | 181 | ] |
Alexander Afanasyev | 0151579 | 2014-12-16 14:20:01 -0800 | [diff] [blame] | 182 | return flags |