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