Alexander Afanasyev | 97e4cac | 2014-03-28 10:55:11 -0700 | [diff] [blame] | 1 | # -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*- |
Alexander Afanasyev | 97e4cac | 2014-03-28 10:55:11 -0700 | [diff] [blame] | 2 | |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 3 | from waflib import Logs, Configure, Utils |
Alexander Afanasyev | 97e4cac | 2014-03-28 10:55:11 -0700 | [diff] [blame] | 4 | |
| 5 | def options(opt): |
| 6 | opt.add_option('--debug', '--with-debug', action='store_true', default=False, dest='debug', |
Davide Pesavento | 8eb5f8f | 2017-06-24 02:19:42 -0400 | [diff] [blame] | 7 | help='''Compile in debugging mode with minimal optimizations (-O0 or -Og)''') |
Alexander Afanasyev | 97e4cac | 2014-03-28 10:55:11 -0700 | [diff] [blame] | 8 | |
| 9 | def configure(conf): |
Davide Pesavento | c0a5a39 | 2017-08-19 16:49:30 -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 | 8eb5f8f | 2017-06-24 02:19:42 -0400 | [diff] [blame] | 13 | ccver = tuple(int(i) for i in conf.env['CC_VERSION']) |
Davide Pesavento | c0a5a39 | 2017-08-19 16:49:30 -0400 | [diff] [blame] | 14 | errmsg = '' |
| 15 | warnmsg = '' |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 16 | if cxx == 'gcc': |
Davide Pesavento | 8eb5f8f | 2017-06-24 02:19:42 -0400 | [diff] [blame] | 17 | if ccver < (4, 8, 2): |
Davide Pesavento | c0a5a39 | 2017-08-19 16:49:30 -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 | b522070 | 2017-09-21 18:57:00 -0400 | [diff] [blame] | 20 | conf.flags = GccFlags() |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 21 | elif cxx == 'clang': |
Davide Pesavento | 8eb5f8f | 2017-06-24 02:19:42 -0400 | [diff] [blame] | 22 | if ccver < (3, 4, 0): |
Davide Pesavento | c0a5a39 | 2017-08-19 16:49:30 -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 | b522070 | 2017-09-21 18:57:00 -0400 | [diff] [blame] | 25 | conf.flags = ClangFlags() |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 26 | else: |
Davide Pesavento | c0a5a39 | 2017-08-19 16:49:30 -0400 | [diff] [blame] | 27 | warnmsg = 'Note: %s compiler is unsupported' % cxx |
Alexander Afanasyev | b522070 | 2017-09-21 18:57:00 -0400 | [diff] [blame] | 28 | conf.flags = CompilerFlags() |
Davide Pesavento | c0a5a39 | 2017-08-19 16:49:30 -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'])) |
Davide Pesavento | 1bdef28 | 2014-04-08 20:59:50 +0200 | [diff] [blame] | 38 | |
Alexander Afanasyev | b522070 | 2017-09-21 18:57:00 -0400 | [diff] [blame] | 39 | conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0) |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 40 | |
Davide Pesavento | f485d89 | 2015-10-02 02:00:54 +0200 | [diff] [blame] | 41 | # General flags are always applied (e.g., selecting C++11 mode) |
Alexander Afanasyev | b522070 | 2017-09-21 18:57:00 -0400 | [diff] [blame] | 42 | generalFlags = conf.flags.getGeneralFlags(conf) |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 43 | conf.add_supported_cxxflags(generalFlags['CXXFLAGS']) |
| 44 | conf.add_supported_linkflags(generalFlags['LINKFLAGS']) |
| 45 | conf.env.DEFINES += generalFlags['DEFINES'] |
| 46 | |
Alexander Afanasyev | b522070 | 2017-09-21 18:57:00 -0400 | [diff] [blame] | 47 | @Configure.conf |
| 48 | def check_compiler_flags(conf): |
Davide Pesavento | f485d89 | 2015-10-02 02:00:54 +0200 | [diff] [blame] | 49 | # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 50 | # corresponding environment variables are not set. |
Davide Pesavento | f485d89 | 2015-10-02 02:00:54 +0200 | [diff] [blame] | 51 | # DEFINES are always applied. |
Alexander Afanasyev | 97e4cac | 2014-03-28 10:55:11 -0700 | [diff] [blame] | 52 | if conf.options.debug: |
Alexander Afanasyev | b522070 | 2017-09-21 18:57:00 -0400 | [diff] [blame] | 53 | extraFlags = conf.flags.getDebugFlags(conf) |
| 54 | if conf.areCustomCxxflagsPresent: |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 55 | missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS] |
Alexander Afanasyev | 97e4cac | 2014-03-28 10:55:11 -0700 | [diff] [blame] | 56 | if len(missingFlags) > 0: |
| 57 | Logs.warn("Selected debug mode, but CXXFLAGS is set to a custom value '%s'" |
| 58 | % " ".join(conf.env.CXXFLAGS)) |
| 59 | Logs.warn("Default flags '%s' are not activated" % " ".join(missingFlags)) |
Alexander Afanasyev | 97e4cac | 2014-03-28 10:55:11 -0700 | [diff] [blame] | 60 | else: |
Alexander Afanasyev | b522070 | 2017-09-21 18:57:00 -0400 | [diff] [blame] | 61 | extraFlags = conf.flags.getOptimizedFlags(conf) |
Alexander Afanasyev | 97e4cac | 2014-03-28 10:55:11 -0700 | [diff] [blame] | 62 | |
Alexander Afanasyev | b522070 | 2017-09-21 18:57:00 -0400 | [diff] [blame] | 63 | if not conf.areCustomCxxflagsPresent: |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 64 | conf.add_supported_cxxflags(extraFlags['CXXFLAGS']) |
Davide Pesavento | ed89ccc | 2015-11-01 20:35:04 +0100 | [diff] [blame] | 65 | conf.add_supported_linkflags(extraFlags['LINKFLAGS']) |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 66 | |
| 67 | conf.env.DEFINES += extraFlags['DEFINES'] |
Davide Pesavento | ab1e8f2 | 2014-10-21 22:45:33 +0200 | [diff] [blame] | 68 | |
Alexander Afanasyev | 97e4cac | 2014-03-28 10:55:11 -0700 | [diff] [blame] | 69 | @Configure.conf |
| 70 | def add_supported_cxxflags(self, cxxflags): |
| 71 | """ |
| 72 | Check which cxxflags are supported by compiler and add them to env.CXXFLAGS variable |
| 73 | """ |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 74 | if len(cxxflags) == 0: |
| 75 | return |
| 76 | |
Davide Pesavento | ab1e8f2 | 2014-10-21 22:45:33 +0200 | [diff] [blame] | 77 | self.start_msg('Checking supported CXXFLAGS') |
Alexander Afanasyev | 97e4cac | 2014-03-28 10:55:11 -0700 | [diff] [blame] | 78 | |
| 79 | supportedFlags = [] |
Alexander Afanasyev | b522070 | 2017-09-21 18:57:00 -0400 | [diff] [blame] | 80 | for flags in cxxflags: |
| 81 | flags = Utils.to_list(flags) |
| 82 | if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False): |
| 83 | supportedFlags += flags |
Alexander Afanasyev | 97e4cac | 2014-03-28 10:55:11 -0700 | [diff] [blame] | 84 | |
Davide Pesavento | 1bdef28 | 2014-04-08 20:59:50 +0200 | [diff] [blame] | 85 | self.end_msg(' '.join(supportedFlags)) |
Davide Pesavento | 67f3027 | 2016-08-10 01:55:16 +0000 | [diff] [blame] | 86 | self.env.prepend_value('CXXFLAGS', supportedFlags) |
Davide Pesavento | ab1e8f2 | 2014-10-21 22:45:33 +0200 | [diff] [blame] | 87 | |
| 88 | @Configure.conf |
| 89 | def add_supported_linkflags(self, linkflags): |
| 90 | """ |
| 91 | Check which linkflags are supported by compiler and add them to env.LINKFLAGS variable |
| 92 | """ |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 93 | if len(linkflags) == 0: |
| 94 | return |
| 95 | |
Davide Pesavento | ab1e8f2 | 2014-10-21 22:45:33 +0200 | [diff] [blame] | 96 | self.start_msg('Checking supported LINKFLAGS') |
| 97 | |
| 98 | supportedFlags = [] |
Alexander Afanasyev | b522070 | 2017-09-21 18:57:00 -0400 | [diff] [blame] | 99 | for flags in linkflags: |
| 100 | flags = Utils.to_list(flags) |
| 101 | if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False): |
| 102 | supportedFlags += flags |
Davide Pesavento | ab1e8f2 | 2014-10-21 22:45:33 +0200 | [diff] [blame] | 103 | |
| 104 | self.end_msg(' '.join(supportedFlags)) |
Davide Pesavento | 67f3027 | 2016-08-10 01:55:16 +0000 | [diff] [blame] | 105 | self.env.prepend_value('LINKFLAGS', supportedFlags) |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 106 | |
| 107 | |
| 108 | class CompilerFlags(object): |
| 109 | def getGeneralFlags(self, conf): |
Davide Pesavento | ed89ccc | 2015-11-01 20:35:04 +0100 | [diff] [blame] | 110 | """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed""" |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 111 | return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []} |
| 112 | |
| 113 | def getDebugFlags(self, conf): |
Davide Pesavento | ed89ccc | 2015-11-01 20:35:04 +0100 | [diff] [blame] | 114 | """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode""" |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 115 | return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']} |
| 116 | |
| 117 | def getOptimizedFlags(self, conf): |
Davide Pesavento | ed89ccc | 2015-11-01 20:35:04 +0100 | [diff] [blame] | 118 | """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode""" |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 119 | return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']} |
| 120 | |
| 121 | class GccBasicFlags(CompilerFlags): |
Davide Pesavento | f485d89 | 2015-10-02 02:00:54 +0200 | [diff] [blame] | 122 | """ |
| 123 | This class defines basic flags that work for both gcc and clang compilers |
| 124 | """ |
Davide Pesavento | 8eb5f8f | 2017-06-24 02:19:42 -0400 | [diff] [blame] | 125 | def getGeneralFlags(self, conf): |
| 126 | flags = super(GccBasicFlags, self).getGeneralFlags(conf) |
| 127 | flags['CXXFLAGS'] += ['-std=c++11'] |
| 128 | return flags |
| 129 | |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 130 | def getDebugFlags(self, conf): |
| 131 | flags = super(GccBasicFlags, self).getDebugFlags(conf) |
Davide Pesavento | ed89ccc | 2015-11-01 20:35:04 +0100 | [diff] [blame] | 132 | flags['CXXFLAGS'] += ['-O0', |
Davide Pesavento | 8eb5f8f | 2017-06-24 02:19:42 -0400 | [diff] [blame] | 133 | '-Og', # gcc >= 4.8, clang >= 4.0 |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 134 | '-g3', |
Davide Pesavento | ed89ccc | 2015-11-01 20:35:04 +0100 | [diff] [blame] | 135 | '-pedantic', |
| 136 | '-Wall', |
| 137 | '-Wextra', |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 138 | '-Werror', |
Alexander Afanasyev | 847de40 | 2017-09-21 18:57:30 -0400 | [diff] [blame] | 139 | '-Wnon-virtual-dtor', |
Alexander Afanasyev | 436f3bd | 2016-10-11 16:37:45 -0700 | [diff] [blame] | 140 | '-Wno-error=deprecated-declarations', # Bug #3795 |
Davide Pesavento | c0a5a39 | 2017-08-19 16:49:30 -0400 | [diff] [blame] | 141 | '-Wno-error=maybe-uninitialized', # Bug #1615 |
| 142 | '-Wno-unused-parameter', |
Davide Pesavento | f485d89 | 2015-10-02 02:00:54 +0200 | [diff] [blame] | 143 | ] |
Davide Pesavento | 8eb5f8f | 2017-06-24 02:19:42 -0400 | [diff] [blame] | 144 | flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1'] |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 145 | return flags |
| 146 | |
| 147 | def getOptimizedFlags(self, conf): |
| 148 | flags = super(GccBasicFlags, self).getOptimizedFlags(conf) |
Davide Pesavento | ed89ccc | 2015-11-01 20:35:04 +0100 | [diff] [blame] | 149 | flags['CXXFLAGS'] += ['-O2', |
Davide Pesavento | f485d89 | 2015-10-02 02:00:54 +0200 | [diff] [blame] | 150 | '-g', |
Davide Pesavento | ed89ccc | 2015-11-01 20:35:04 +0100 | [diff] [blame] | 151 | '-pedantic', |
| 152 | '-Wall', |
| 153 | '-Wextra', |
Alexander Afanasyev | 847de40 | 2017-09-21 18:57:30 -0400 | [diff] [blame] | 154 | '-Wnon-virtual-dtor', |
Davide Pesavento | ed89ccc | 2015-11-01 20:35:04 +0100 | [diff] [blame] | 155 | '-Wno-unused-parameter', |
Davide Pesavento | f485d89 | 2015-10-02 02:00:54 +0200 | [diff] [blame] | 156 | ] |
Davide Pesavento | 8eb5f8f | 2017-06-24 02:19:42 -0400 | [diff] [blame] | 157 | flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1'] |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 158 | return flags |
| 159 | |
| 160 | class GccFlags(GccBasicFlags): |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 161 | def getDebugFlags(self, conf): |
| 162 | flags = super(GccFlags, self).getDebugFlags(conf) |
Davide Pesavento | ed89ccc | 2015-11-01 20:35:04 +0100 | [diff] [blame] | 163 | version = tuple(int(i) for i in conf.env['CC_VERSION']) |
| 164 | if version < (5, 1, 0): |
| 165 | flags['CXXFLAGS'] += ['-Wno-missing-field-initializers'] |
Davide Pesavento | 8eb5f8f | 2017-06-24 02:19:42 -0400 | [diff] [blame] | 166 | flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9 |
Davide Pesavento | f485d89 | 2015-10-02 02:00:54 +0200 | [diff] [blame] | 167 | return flags |
| 168 | |
| 169 | def getOptimizedFlags(self, conf): |
| 170 | flags = super(GccFlags, self).getOptimizedFlags(conf) |
Davide Pesavento | ed89ccc | 2015-11-01 20:35:04 +0100 | [diff] [blame] | 171 | version = tuple(int(i) for i in conf.env['CC_VERSION']) |
| 172 | if version < (5, 1, 0): |
| 173 | flags['CXXFLAGS'] += ['-Wno-missing-field-initializers'] |
Davide Pesavento | f485d89 | 2015-10-02 02:00:54 +0200 | [diff] [blame] | 174 | flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9 |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 175 | return flags |
| 176 | |
| 177 | class ClangFlags(GccBasicFlags): |
| 178 | def getGeneralFlags(self, conf): |
| 179 | flags = super(ClangFlags, self).getGeneralFlags(conf) |
Alexander Afanasyev | b522070 | 2017-09-21 18:57:00 -0400 | [diff] [blame] | 180 | version = tuple(int(i) for i in conf.env['CC_VERSION']) |
| 181 | if Utils.unversioned_sys_platform() == 'darwin' and version >= (9, 0, 0): # Bug #4296 |
| 182 | flags['CXXFLAGS'] += [['-isystem', '/usr/local/include'], # for Homebrew |
| 183 | ['-isystem', '/opt/local/include']] # for MacPorts |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 184 | return flags |
| 185 | |
| 186 | def getDebugFlags(self, conf): |
| 187 | flags = super(ClangFlags, self).getDebugFlags(conf) |
Davide Pesavento | f485d89 | 2015-10-02 02:00:54 +0200 | [diff] [blame] | 188 | flags['CXXFLAGS'] += ['-fcolor-diagnostics', |
Davide Pesavento | c0a5a39 | 2017-08-19 16:49:30 -0400 | [diff] [blame] | 189 | '-Wextra-semi', |
| 190 | '-Wundefined-func-template', |
Davide Pesavento | f485d89 | 2015-10-02 02:00:54 +0200 | [diff] [blame] | 191 | '-Wno-error=deprecated-register', |
Alexander Afanasyev | 0fc9748 | 2016-09-13 18:33:15 +0000 | [diff] [blame] | 192 | '-Wno-error=infinite-recursion', # Bug #3358 |
Davide Pesavento | c0a5a39 | 2017-08-19 16:49:30 -0400 | [diff] [blame] | 193 | '-Wno-error=keyword-macro', # Bug #3235 |
| 194 | '-Wno-error=unneeded-internal-declaration', # Bug #1588 |
| 195 | '-Wno-unused-local-typedef', # Bugs #2657 and #3209 |
Davide Pesavento | f485d89 | 2015-10-02 02:00:54 +0200 | [diff] [blame] | 196 | ] |
Davide Pesavento | c0a5a39 | 2017-08-19 16:49:30 -0400 | [diff] [blame] | 197 | version = tuple(int(i) for i in conf.env['CC_VERSION']) |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 198 | if version < (3, 9, 0) or (Utils.unversioned_sys_platform() == 'darwin' and version < (8, 1, 0)): |
Davide Pesavento | c0a5a39 | 2017-08-19 16:49:30 -0400 | [diff] [blame] | 199 | flags['CXXFLAGS'] += ['-Wno-unknown-pragmas'] |
Davide Pesavento | f485d89 | 2015-10-02 02:00:54 +0200 | [diff] [blame] | 200 | return flags |
| 201 | |
| 202 | def getOptimizedFlags(self, conf): |
| 203 | flags = super(ClangFlags, self).getOptimizedFlags(conf) |
| 204 | flags['CXXFLAGS'] += ['-fcolor-diagnostics', |
Davide Pesavento | c0a5a39 | 2017-08-19 16:49:30 -0400 | [diff] [blame] | 205 | '-Wextra-semi', |
| 206 | '-Wundefined-func-template', |
Davide Pesavento | f485d89 | 2015-10-02 02:00:54 +0200 | [diff] [blame] | 207 | '-Wno-unused-local-typedef', # Bugs #2657 and #3209 |
| 208 | ] |
Davide Pesavento | c0a5a39 | 2017-08-19 16:49:30 -0400 | [diff] [blame] | 209 | version = tuple(int(i) for i in conf.env['CC_VERSION']) |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 210 | if version < (3, 9, 0) or (Utils.unversioned_sys_platform() == 'darwin' and version < (8, 1, 0)): |
Davide Pesavento | c0a5a39 | 2017-08-19 16:49:30 -0400 | [diff] [blame] | 211 | flags['CXXFLAGS'] += ['-Wno-unknown-pragmas'] |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 212 | return flags |