Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 1 | # -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*- |
| 2 | |
| 3 | import platform |
| 4 | from waflib import Configure, Logs, Utils |
| 5 | |
| 6 | def options(opt): |
| 7 | opt.add_option('--debug', '--with-debug', action='store_true', default=False, |
| 8 | help='Compile in debugging mode with minimal optimizations (-Og)') |
| 9 | |
| 10 | def configure(conf): |
| 11 | conf.start_msg('Checking C++ compiler version') |
| 12 | |
| 13 | cxx = conf.env.CXX_NAME # generic name of the compiler |
| 14 | ccver = tuple(int(i) for i in conf.env.CC_VERSION) |
| 15 | ccverstr = '.'.join(conf.env.CC_VERSION) |
| 16 | errmsg = '' |
| 17 | warnmsg = '' |
| 18 | if cxx == 'gcc': |
| 19 | if ccver < (5, 3, 0): |
| 20 | errmsg = ('The version of gcc you are using is too old.\n' |
| 21 | 'The minimum supported gcc version is 7.4.0.') |
| 22 | elif ccver < (7, 4, 0): |
| 23 | warnmsg = ('Using a version of gcc older than 7.4.0 is not ' |
| 24 | 'officially supported and may result in build failures.') |
| 25 | conf.flags = GccFlags() |
| 26 | elif cxx == 'clang': |
| 27 | if Utils.unversioned_sys_platform() == 'darwin' and ccver < (9, 0, 0): |
| 28 | errmsg = ('The version of Xcode you are using is too old.\n' |
| 29 | 'The minimum supported Xcode version is 9.0.') |
| 30 | elif ccver < (4, 0, 0): |
| 31 | errmsg = ('The version of clang you are using is too old.\n' |
| 32 | 'The minimum supported clang version is 4.0.') |
| 33 | conf.flags = ClangFlags() |
| 34 | else: |
| 35 | warnmsg = '%s compiler is unsupported' % cxx |
| 36 | conf.flags = CompilerFlags() |
| 37 | |
| 38 | if errmsg: |
| 39 | conf.end_msg(ccverstr, color='RED') |
| 40 | conf.fatal(errmsg) |
| 41 | elif warnmsg: |
| 42 | conf.end_msg(ccverstr, color='YELLOW') |
| 43 | Logs.warn('WARNING: ' + warnmsg) |
| 44 | else: |
| 45 | conf.end_msg(ccverstr) |
| 46 | |
| 47 | conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0) |
| 48 | |
| 49 | # General flags are always applied (e.g., selecting C++ language standard) |
| 50 | generalFlags = conf.flags.getGeneralFlags(conf) |
| 51 | conf.add_supported_cxxflags(generalFlags['CXXFLAGS']) |
| 52 | conf.add_supported_linkflags(generalFlags['LINKFLAGS']) |
| 53 | conf.env.DEFINES += generalFlags['DEFINES'] |
| 54 | |
| 55 | @Configure.conf |
| 56 | def check_compiler_flags(conf): |
| 57 | # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the |
| 58 | # corresponding environment variables are not set. |
| 59 | # DEFINES are always applied. |
| 60 | if conf.options.debug: |
| 61 | extraFlags = conf.flags.getDebugFlags(conf) |
| 62 | if conf.areCustomCxxflagsPresent: |
| 63 | missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS] |
| 64 | if missingFlags: |
| 65 | Logs.warn('Selected debug mode, but CXXFLAGS is set to a custom value "%s"' |
| 66 | % ' '.join(conf.env.CXXFLAGS)) |
| 67 | Logs.warn('Default flags "%s" will not be used' % ' '.join(missingFlags)) |
| 68 | else: |
| 69 | extraFlags = conf.flags.getOptimizedFlags(conf) |
| 70 | |
| 71 | if not conf.areCustomCxxflagsPresent: |
| 72 | conf.add_supported_cxxflags(extraFlags['CXXFLAGS']) |
| 73 | conf.add_supported_linkflags(extraFlags['LINKFLAGS']) |
| 74 | |
| 75 | conf.env.DEFINES += extraFlags['DEFINES'] |
| 76 | |
| 77 | @Configure.conf |
| 78 | def add_supported_cxxflags(self, cxxflags): |
| 79 | """ |
| 80 | Check which cxxflags are supported by compiler and add them to env.CXXFLAGS variable |
| 81 | """ |
| 82 | if len(cxxflags) == 0: |
| 83 | return |
| 84 | |
| 85 | self.start_msg('Checking supported CXXFLAGS') |
| 86 | |
| 87 | supportedFlags = [] |
| 88 | for flags in cxxflags: |
| 89 | flags = Utils.to_list(flags) |
| 90 | if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False): |
| 91 | supportedFlags += flags |
| 92 | |
| 93 | self.end_msg(' '.join(supportedFlags)) |
| 94 | self.env.prepend_value('CXXFLAGS', supportedFlags) |
| 95 | |
| 96 | @Configure.conf |
| 97 | def add_supported_linkflags(self, linkflags): |
| 98 | """ |
| 99 | Check which linkflags are supported by compiler and add them to env.LINKFLAGS variable |
| 100 | """ |
| 101 | if len(linkflags) == 0: |
| 102 | return |
| 103 | |
| 104 | self.start_msg('Checking supported LINKFLAGS') |
| 105 | |
| 106 | supportedFlags = [] |
| 107 | for flags in linkflags: |
| 108 | flags = Utils.to_list(flags) |
| 109 | if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False): |
| 110 | supportedFlags += flags |
| 111 | |
| 112 | self.end_msg(' '.join(supportedFlags)) |
| 113 | self.env.prepend_value('LINKFLAGS', supportedFlags) |
| 114 | |
| 115 | |
| 116 | class CompilerFlags(object): |
| 117 | def getCompilerVersion(self, conf): |
| 118 | return tuple(int(i) for i in conf.env.CC_VERSION) |
| 119 | |
| 120 | def getGeneralFlags(self, conf): |
| 121 | """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed""" |
| 122 | return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []} |
| 123 | |
| 124 | def getDebugFlags(self, conf): |
| 125 | """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode""" |
| 126 | return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']} |
| 127 | |
| 128 | def getOptimizedFlags(self, conf): |
| 129 | """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode""" |
| 130 | return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']} |
| 131 | |
| 132 | class GccBasicFlags(CompilerFlags): |
| 133 | """ |
| 134 | This class defines basic flags that work for both gcc and clang compilers |
| 135 | """ |
| 136 | def getGeneralFlags(self, conf): |
| 137 | flags = super(GccBasicFlags, self).getGeneralFlags(conf) |
| 138 | flags['CXXFLAGS'] += ['-std=c++14'] |
| 139 | if Utils.unversioned_sys_platform() == 'linux': |
| 140 | flags['LINKFLAGS'] += ['-fuse-ld=gold'] |
| 141 | elif Utils.unversioned_sys_platform() == 'freebsd': |
| 142 | flags['LINKFLAGS'] += ['-fuse-ld=lld'] |
| 143 | return flags |
| 144 | |
| 145 | def getDebugFlags(self, conf): |
| 146 | flags = super(GccBasicFlags, self).getDebugFlags(conf) |
| 147 | flags['CXXFLAGS'] += ['-Og', |
| 148 | '-g3', |
| 149 | '-pedantic', |
| 150 | '-Wall', |
| 151 | '-Wextra', |
| 152 | '-Werror', |
| 153 | '-Wcatch-value=2', |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 154 | #'-Wextra-semi', # Qt5 |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 155 | '-Wnon-virtual-dtor', |
| 156 | '-Wno-error=deprecated-declarations', # Bug #3795 |
| 157 | '-Wno-error=maybe-uninitialized', # Bug #1615 |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 158 | '-Wno-deprecated-copy', # Qt5 |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 159 | '-Wno-unused-parameter', |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 160 | ] |
| 161 | flags['LINKFLAGS'] += ['-Wl,-O1'] |
| 162 | return flags |
| 163 | |
| 164 | def getOptimizedFlags(self, conf): |
| 165 | flags = super(GccBasicFlags, self).getOptimizedFlags(conf) |
| 166 | flags['CXXFLAGS'] += ['-O2', |
| 167 | '-g', |
| 168 | '-pedantic', |
| 169 | '-Wall', |
| 170 | '-Wextra', |
| 171 | '-Wcatch-value=2', |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 172 | #'-Wextra-semi', # Qt5 |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 173 | '-Wnon-virtual-dtor', |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 174 | '-Wno-deprecated-copy', # Qt5 |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 175 | '-Wno-unused-parameter', |
| 176 | ] |
| 177 | flags['LINKFLAGS'] += ['-Wl,-O1'] |
| 178 | return flags |
| 179 | |
| 180 | class GccFlags(GccBasicFlags): |
| 181 | def getDebugFlags(self, conf): |
| 182 | flags = super(GccFlags, self).getDebugFlags(conf) |
| 183 | flags['CXXFLAGS'] += ['-fdiagnostics-color', |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 184 | #'-Wredundant-tags', # Qt5 |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 185 | ] |
| 186 | if platform.machine() == 'armv7l' and self.getCompilerVersion(conf) >= (7, 1, 0): |
| 187 | flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106 |
| 188 | return flags |
| 189 | |
| 190 | def getOptimizedFlags(self, conf): |
| 191 | flags = super(GccFlags, self).getOptimizedFlags(conf) |
| 192 | flags['CXXFLAGS'] += ['-fdiagnostics-color', |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 193 | #'-Wredundant-tags', # Qt5 |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 194 | ] |
| 195 | if platform.machine() == 'armv7l' and self.getCompilerVersion(conf) >= (7, 1, 0): |
| 196 | flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106 |
| 197 | return flags |
| 198 | |
| 199 | class ClangFlags(GccBasicFlags): |
| 200 | def getGeneralFlags(self, conf): |
| 201 | flags = super(ClangFlags, self).getGeneralFlags(conf) |
| 202 | if Utils.unversioned_sys_platform() == 'darwin': |
| 203 | # Bug #4296 |
| 204 | flags['CXXFLAGS'] += [['-isystem', '/usr/local/include'], # for Homebrew |
| 205 | ['-isystem', '/opt/local/include']] # for MacPorts |
| 206 | elif Utils.unversioned_sys_platform() == 'freebsd': |
| 207 | # Bug #4790 |
| 208 | flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']] |
| 209 | return flags |
| 210 | |
| 211 | def getDebugFlags(self, conf): |
| 212 | flags = super(ClangFlags, self).getDebugFlags(conf) |
| 213 | flags['CXXFLAGS'] += ['-fcolor-diagnostics', |
| 214 | '-Wundefined-func-template', |
| 215 | '-Wno-unused-local-typedef', # Bugs #2657 and #3209 |
| 216 | ] |
| 217 | if self.getCompilerVersion(conf) < (6, 0, 0): |
| 218 | flags['CXXFLAGS'] += ['-Wno-missing-braces'] # Bug #4721 |
| 219 | return flags |
| 220 | |
| 221 | def getOptimizedFlags(self, conf): |
| 222 | flags = super(ClangFlags, self).getOptimizedFlags(conf) |
| 223 | flags['CXXFLAGS'] += ['-fcolor-diagnostics', |
| 224 | '-Wundefined-func-template', |
| 225 | '-Wno-unused-local-typedef', # Bugs #2657 and #3209 |
| 226 | ] |
| 227 | if self.getCompilerVersion(conf) < (6, 0, 0): |
| 228 | flags['CXXFLAGS'] += ['-Wno-missing-braces'] # Bug #4721 |
Davide Pesavento | 7676b56 | 2020-12-14 00:41:26 -0500 | [diff] [blame] | 229 | return flags |