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