Alexander Afanasyev | 7eb5911 | 2014-07-02 14:21:11 -0700 | [diff] [blame] | 1 | # -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*- |
Alexander Afanasyev | 7eb5911 | 2014-07-02 14:21:11 -0700 | [diff] [blame] | 2 | |
Davide Pesavento | 5f408ae | 2020-07-15 21:17:04 -0400 | [diff] [blame^] | 3 | import platform |
Alexander Afanasyev | 40491df | 2018-03-09 16:29:52 -0500 | [diff] [blame] | 4 | from waflib import Configure, Logs, Utils |
Alexander Afanasyev | 7eb5911 | 2014-07-02 14:21:11 -0700 | [diff] [blame] | 5 | |
| 6 | def options(opt): |
Alexander Afanasyev | 40491df | 2018-03-09 16:29:52 -0500 | [diff] [blame] | 7 | opt.add_option('--debug', '--with-debug', action='store_true', default=False, |
Davide Pesavento | 5f408ae | 2020-07-15 21:17:04 -0400 | [diff] [blame^] | 8 | help='Compile in debugging mode with minimal optimizations (-Og)') |
Alexander Afanasyev | 7eb5911 | 2014-07-02 14:21:11 -0700 | [diff] [blame] | 9 | |
| 10 | def configure(conf): |
Alexander Afanasyev | 12d5faa | 2017-09-21 19:04:22 -0400 | [diff] [blame] | 11 | conf.start_msg('Checking C++ compiler version') |
Alexander Afanasyev | 7eb5911 | 2014-07-02 14:21:11 -0700 | [diff] [blame] | 12 | |
Alexander Afanasyev | 40491df | 2018-03-09 16:29:52 -0500 | [diff] [blame] | 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) |
Alexander Afanasyev | 12d5faa | 2017-09-21 19:04:22 -0400 | [diff] [blame] | 16 | errmsg = '' |
| 17 | warnmsg = '' |
| 18 | if cxx == 'gcc': |
Davide Pesavento | 4a9395b | 2018-05-24 00:23:22 -0400 | [diff] [blame] | 19 | if ccver < (5, 3, 0): |
Alexander Afanasyev | 12d5faa | 2017-09-21 19:04:22 -0400 | [diff] [blame] | 20 | errmsg = ('The version of gcc you are using is too old.\n' |
Davide Pesavento | 5f408ae | 2020-07-15 21:17:04 -0400 | [diff] [blame^] | 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.') |
Alexander Afanasyev | 12d5faa | 2017-09-21 19:04:22 -0400 | [diff] [blame] | 25 | conf.flags = GccFlags() |
| 26 | elif cxx == 'clang': |
Davide Pesavento | 5f408ae | 2020-07-15 21:17:04 -0400 | [diff] [blame^] | 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): |
Alexander Afanasyev | 12d5faa | 2017-09-21 19:04:22 -0400 | [diff] [blame] | 31 | errmsg = ('The version of clang you are using is too old.\n' |
Davide Pesavento | 5f408ae | 2020-07-15 21:17:04 -0400 | [diff] [blame^] | 32 | 'The minimum supported clang version is 4.0.') |
Alexander Afanasyev | 12d5faa | 2017-09-21 19:04:22 -0400 | [diff] [blame] | 33 | conf.flags = ClangFlags() |
| 34 | else: |
Davide Pesavento | 5f408ae | 2020-07-15 21:17:04 -0400 | [diff] [blame^] | 35 | warnmsg = '%s compiler is unsupported' % cxx |
Alexander Afanasyev | 12d5faa | 2017-09-21 19:04:22 -0400 | [diff] [blame] | 36 | conf.flags = CompilerFlags() |
| 37 | |
| 38 | if errmsg: |
Alexander Afanasyev | 40491df | 2018-03-09 16:29:52 -0500 | [diff] [blame] | 39 | conf.end_msg(ccverstr, color='RED') |
Alexander Afanasyev | 12d5faa | 2017-09-21 19:04:22 -0400 | [diff] [blame] | 40 | conf.fatal(errmsg) |
| 41 | elif warnmsg: |
Alexander Afanasyev | 40491df | 2018-03-09 16:29:52 -0500 | [diff] [blame] | 42 | conf.end_msg(ccverstr, color='YELLOW') |
Davide Pesavento | 5f408ae | 2020-07-15 21:17:04 -0400 | [diff] [blame^] | 43 | Logs.warn('WARNING: ' + warnmsg) |
Alexander Afanasyev | 12d5faa | 2017-09-21 19:04:22 -0400 | [diff] [blame] | 44 | else: |
Alexander Afanasyev | 40491df | 2018-03-09 16:29:52 -0500 | [diff] [blame] | 45 | conf.end_msg(ccverstr) |
Alexander Afanasyev | 12d5faa | 2017-09-21 19:04:22 -0400 | [diff] [blame] | 46 | |
| 47 | conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0) |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 48 | |
Davide Pesavento | 4a9395b | 2018-05-24 00:23:22 -0400 | [diff] [blame] | 49 | # General flags are always applied (e.g., selecting C++ language standard) |
Alexander Afanasyev | 12d5faa | 2017-09-21 19:04:22 -0400 | [diff] [blame] | 50 | generalFlags = conf.flags.getGeneralFlags(conf) |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 51 | conf.add_supported_cxxflags(generalFlags['CXXFLAGS']) |
| 52 | conf.add_supported_linkflags(generalFlags['LINKFLAGS']) |
| 53 | conf.env.DEFINES += generalFlags['DEFINES'] |
| 54 | |
Alexander Afanasyev | 12d5faa | 2017-09-21 19:04:22 -0400 | [diff] [blame] | 55 | @Configure.conf |
| 56 | def check_compiler_flags(conf): |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 57 | # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the |
| 58 | # corresponding environment variables are not set. |
| 59 | # DEFINES are always applied. |
Alexander Afanasyev | 7eb5911 | 2014-07-02 14:21:11 -0700 | [diff] [blame] | 60 | if conf.options.debug: |
Alexander Afanasyev | 12d5faa | 2017-09-21 19:04:22 -0400 | [diff] [blame] | 61 | extraFlags = conf.flags.getDebugFlags(conf) |
| 62 | if conf.areCustomCxxflagsPresent: |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 63 | missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS] |
Alexander Afanasyev | 40491df | 2018-03-09 16:29:52 -0500 | [diff] [blame] | 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)) |
Alexander Afanasyev | 7eb5911 | 2014-07-02 14:21:11 -0700 | [diff] [blame] | 68 | else: |
Alexander Afanasyev | 12d5faa | 2017-09-21 19:04:22 -0400 | [diff] [blame] | 69 | extraFlags = conf.flags.getOptimizedFlags(conf) |
Alexander Afanasyev | 7eb5911 | 2014-07-02 14:21:11 -0700 | [diff] [blame] | 70 | |
Alexander Afanasyev | 12d5faa | 2017-09-21 19:04:22 -0400 | [diff] [blame] | 71 | if not conf.areCustomCxxflagsPresent: |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 72 | conf.add_supported_cxxflags(extraFlags['CXXFLAGS']) |
| 73 | conf.add_supported_linkflags(extraFlags['LINKFLAGS']) |
| 74 | |
| 75 | conf.env.DEFINES += extraFlags['DEFINES'] |
Yingdi Yu | 906c2ea | 2014-10-31 11:24:50 -0700 | [diff] [blame] | 76 | |
Alexander Afanasyev | 7eb5911 | 2014-07-02 14:21:11 -0700 | [diff] [blame] | 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 | """ |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 82 | if len(cxxflags) == 0: |
| 83 | return |
| 84 | |
Yingdi Yu | 906c2ea | 2014-10-31 11:24:50 -0700 | [diff] [blame] | 85 | self.start_msg('Checking supported CXXFLAGS') |
Alexander Afanasyev | 7eb5911 | 2014-07-02 14:21:11 -0700 | [diff] [blame] | 86 | |
| 87 | supportedFlags = [] |
Alexander Afanasyev | 12d5faa | 2017-09-21 19:04:22 -0400 | [diff] [blame] | 88 | for flags in cxxflags: |
| 89 | flags = Utils.to_list(flags) |
| 90 | if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False): |
| 91 | supportedFlags += flags |
Alexander Afanasyev | 7eb5911 | 2014-07-02 14:21:11 -0700 | [diff] [blame] | 92 | |
| 93 | self.end_msg(' '.join(supportedFlags)) |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 94 | self.env.prepend_value('CXXFLAGS', supportedFlags) |
Yingdi Yu | 906c2ea | 2014-10-31 11:24:50 -0700 | [diff] [blame] | 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 | """ |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 101 | if len(linkflags) == 0: |
| 102 | return |
| 103 | |
Yingdi Yu | 906c2ea | 2014-10-31 11:24:50 -0700 | [diff] [blame] | 104 | self.start_msg('Checking supported LINKFLAGS') |
| 105 | |
| 106 | supportedFlags = [] |
Alexander Afanasyev | 12d5faa | 2017-09-21 19:04:22 -0400 | [diff] [blame] | 107 | for flags in linkflags: |
| 108 | flags = Utils.to_list(flags) |
| 109 | if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False): |
| 110 | supportedFlags += flags |
Yingdi Yu | 906c2ea | 2014-10-31 11:24:50 -0700 | [diff] [blame] | 111 | |
| 112 | self.end_msg(' '.join(supportedFlags)) |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 113 | self.env.prepend_value('LINKFLAGS', supportedFlags) |
| 114 | |
| 115 | |
| 116 | class CompilerFlags(object): |
Alexander Afanasyev | 40491df | 2018-03-09 16:29:52 -0500 | [diff] [blame] | 117 | def getCompilerVersion(self, conf): |
| 118 | return tuple(int(i) for i in conf.env.CC_VERSION) |
| 119 | |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 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 | """ |
Alexander Afanasyev | 12d5faa | 2017-09-21 19:04:22 -0400 | [diff] [blame] | 136 | def getGeneralFlags(self, conf): |
| 137 | flags = super(GccBasicFlags, self).getGeneralFlags(conf) |
Davide Pesavento | 4a9395b | 2018-05-24 00:23:22 -0400 | [diff] [blame] | 138 | flags['CXXFLAGS'] += ['-std=c++14'] |
Davide Pesavento | fae9def | 2019-01-29 14:34:33 -0500 | [diff] [blame] | 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'] |
Alexander Afanasyev | 12d5faa | 2017-09-21 19:04:22 -0400 | [diff] [blame] | 143 | return flags |
| 144 | |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 145 | def getDebugFlags(self, conf): |
| 146 | flags = super(GccBasicFlags, self).getDebugFlags(conf) |
Davide Pesavento | 5f408ae | 2020-07-15 21:17:04 -0400 | [diff] [blame^] | 147 | flags['CXXFLAGS'] += ['-Og', |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 148 | '-g3', |
| 149 | '-pedantic', |
| 150 | '-Wall', |
| 151 | '-Wextra', |
| 152 | '-Werror', |
Davide Pesavento | 5f408ae | 2020-07-15 21:17:04 -0400 | [diff] [blame^] | 153 | '-Wcatch-value=2', |
| 154 | '-Wextra-semi', |
Alexander Afanasyev | 12d5faa | 2017-09-21 19:04:22 -0400 | [diff] [blame] | 155 | '-Wnon-virtual-dtor', |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 156 | '-Wno-error=deprecated-declarations', # Bug #3795 |
Alexander Afanasyev | 12d5faa | 2017-09-21 19:04:22 -0400 | [diff] [blame] | 157 | '-Wno-error=maybe-uninitialized', # Bug #1615 |
| 158 | '-Wno-unused-parameter', |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 159 | ] |
Davide Pesavento | fae9def | 2019-01-29 14:34:33 -0500 | [diff] [blame] | 160 | flags['LINKFLAGS'] += ['-Wl,-O1'] |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 161 | return flags |
| 162 | |
| 163 | def getOptimizedFlags(self, conf): |
| 164 | flags = super(GccBasicFlags, self).getOptimizedFlags(conf) |
| 165 | flags['CXXFLAGS'] += ['-O2', |
| 166 | '-g', |
| 167 | '-pedantic', |
| 168 | '-Wall', |
| 169 | '-Wextra', |
Davide Pesavento | 5f408ae | 2020-07-15 21:17:04 -0400 | [diff] [blame^] | 170 | '-Wcatch-value=2', |
| 171 | '-Wextra-semi', |
Alexander Afanasyev | 12d5faa | 2017-09-21 19:04:22 -0400 | [diff] [blame] | 172 | '-Wnon-virtual-dtor', |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 173 | '-Wno-unused-parameter', |
| 174 | ] |
Davide Pesavento | fae9def | 2019-01-29 14:34:33 -0500 | [diff] [blame] | 175 | flags['LINKFLAGS'] += ['-Wl,-O1'] |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 176 | return flags |
| 177 | |
| 178 | class GccFlags(GccBasicFlags): |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 179 | def getDebugFlags(self, conf): |
| 180 | flags = super(GccFlags, self).getDebugFlags(conf) |
Davide Pesavento | 5f408ae | 2020-07-15 21:17:04 -0400 | [diff] [blame^] | 181 | flags['CXXFLAGS'] += ['-fdiagnostics-color', |
| 182 | '-Wredundant-tags', |
| 183 | ] |
| 184 | if platform.machine() == 'armv7l' and self.getCompilerVersion(conf) >= (7, 1, 0): |
| 185 | flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106 |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 186 | return flags |
| 187 | |
| 188 | def getOptimizedFlags(self, conf): |
| 189 | flags = super(GccFlags, self).getOptimizedFlags(conf) |
Davide Pesavento | 5f408ae | 2020-07-15 21:17:04 -0400 | [diff] [blame^] | 190 | flags['CXXFLAGS'] += ['-fdiagnostics-color', |
| 191 | '-Wredundant-tags', |
| 192 | ] |
| 193 | if platform.machine() == 'armv7l' and self.getCompilerVersion(conf) >= (7, 1, 0): |
| 194 | flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106 |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 195 | return flags |
| 196 | |
| 197 | class ClangFlags(GccBasicFlags): |
| 198 | def getGeneralFlags(self, conf): |
| 199 | flags = super(ClangFlags, self).getGeneralFlags(conf) |
Davide Pesavento | 5f408ae | 2020-07-15 21:17:04 -0400 | [diff] [blame^] | 200 | if Utils.unversioned_sys_platform() == 'darwin': |
Alexander Afanasyev | 40491df | 2018-03-09 16:29:52 -0500 | [diff] [blame] | 201 | # Bug #4296 |
Alexander Afanasyev | 12d5faa | 2017-09-21 19:04:22 -0400 | [diff] [blame] | 202 | flags['CXXFLAGS'] += [['-isystem', '/usr/local/include'], # for Homebrew |
| 203 | ['-isystem', '/opt/local/include']] # for MacPorts |
Davide Pesavento | 5f408ae | 2020-07-15 21:17:04 -0400 | [diff] [blame^] | 204 | elif Utils.unversioned_sys_platform() == 'freebsd': |
| 205 | # Bug #4790 |
| 206 | flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']] |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 207 | return flags |
| 208 | |
| 209 | def getDebugFlags(self, conf): |
| 210 | flags = super(ClangFlags, self).getDebugFlags(conf) |
| 211 | flags['CXXFLAGS'] += ['-fcolor-diagnostics', |
Alexander Afanasyev | 12d5faa | 2017-09-21 19:04:22 -0400 | [diff] [blame] | 212 | '-Wundefined-func-template', |
Alexander Afanasyev | 12d5faa | 2017-09-21 19:04:22 -0400 | [diff] [blame] | 213 | '-Wno-unused-local-typedef', # Bugs #2657 and #3209 |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 214 | ] |
Davide Pesavento | 5f408ae | 2020-07-15 21:17:04 -0400 | [diff] [blame^] | 215 | if self.getCompilerVersion(conf) < (6, 0, 0): |
Davide Pesavento | fae9def | 2019-01-29 14:34:33 -0500 | [diff] [blame] | 216 | flags['CXXFLAGS'] += ['-Wno-missing-braces'] # Bug #4721 |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 217 | return flags |
| 218 | |
| 219 | def getOptimizedFlags(self, conf): |
| 220 | flags = super(ClangFlags, self).getOptimizedFlags(conf) |
| 221 | flags['CXXFLAGS'] += ['-fcolor-diagnostics', |
Alexander Afanasyev | 12d5faa | 2017-09-21 19:04:22 -0400 | [diff] [blame] | 222 | '-Wundefined-func-template', |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 223 | '-Wno-unused-local-typedef', # Bugs #2657 and #3209 |
| 224 | ] |
Davide Pesavento | 5f408ae | 2020-07-15 21:17:04 -0400 | [diff] [blame^] | 225 | if self.getCompilerVersion(conf) < (6, 0, 0): |
Davide Pesavento | fae9def | 2019-01-29 14:34:33 -0500 | [diff] [blame] | 226 | flags['CXXFLAGS'] += ['-Wno-missing-braces'] # Bug #4721 |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 227 | return flags |