Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 1 | # -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*- |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 2 | |
Davide Pesavento | 7ae8b08 | 2021-10-12 21:45:47 -0400 | [diff] [blame] | 3 | import platform |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame] | 4 | from waflib import Configure, Logs, Utils |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 5 | |
| 6 | def options(opt): |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame] | 7 | opt.add_option('--debug', '--with-debug', action='store_true', default=False, |
Davide Pesavento | 7ae8b08 | 2021-10-12 21:45:47 -0400 | [diff] [blame] | 8 | help='Compile in debugging mode with minimal optimizations (-Og)') |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 9 | |
| 10 | def configure(conf): |
Davide Pesavento | 6ebfd7c | 2017-08-26 17:52:54 -0400 | [diff] [blame] | 11 | conf.start_msg('Checking C++ compiler version') |
| 12 | |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -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) |
Davide Pesavento | 6ebfd7c | 2017-08-26 17:52:54 -0400 | [diff] [blame] | 16 | errmsg = '' |
| 17 | warnmsg = '' |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 18 | if cxx == 'gcc': |
Davide Pesavento | a08dc3f | 2018-05-24 00:40:28 -0400 | [diff] [blame] | 19 | if ccver < (5, 3, 0): |
Davide Pesavento | 6ebfd7c | 2017-08-26 17:52:54 -0400 | [diff] [blame] | 20 | errmsg = ('The version of gcc you are using is too old.\n' |
Davide Pesavento | 7ae8b08 | 2021-10-12 21:45:47 -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 | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame] | 25 | conf.flags = GccFlags() |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 26 | elif cxx == 'clang': |
Davide Pesavento | 7ae8b08 | 2021-10-12 21:45:47 -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): |
Davide Pesavento | 6ebfd7c | 2017-08-26 17:52:54 -0400 | [diff] [blame] | 31 | errmsg = ('The version of clang you are using is too old.\n' |
Davide Pesavento | 7ae8b08 | 2021-10-12 21:45:47 -0400 | [diff] [blame] | 32 | 'The minimum supported clang version is 4.0.') |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame] | 33 | conf.flags = ClangFlags() |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 34 | else: |
Davide Pesavento | 7ae8b08 | 2021-10-12 21:45:47 -0400 | [diff] [blame] | 35 | warnmsg = '%s compiler is unsupported' % cxx |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame] | 36 | conf.flags = CompilerFlags() |
Davide Pesavento | 6ebfd7c | 2017-08-26 17:52:54 -0400 | [diff] [blame] | 37 | |
| 38 | if errmsg: |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame] | 39 | conf.end_msg(ccverstr, color='RED') |
Davide Pesavento | 6ebfd7c | 2017-08-26 17:52:54 -0400 | [diff] [blame] | 40 | conf.fatal(errmsg) |
| 41 | elif warnmsg: |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame] | 42 | conf.end_msg(ccverstr, color='YELLOW') |
Davide Pesavento | 7ae8b08 | 2021-10-12 21:45:47 -0400 | [diff] [blame] | 43 | Logs.warn('WARNING: ' + warnmsg) |
Davide Pesavento | 6ebfd7c | 2017-08-26 17:52:54 -0400 | [diff] [blame] | 44 | else: |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame] | 45 | conf.end_msg(ccverstr) |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 46 | |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame] | 47 | conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0) |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 48 | |
Davide Pesavento | a08dc3f | 2018-05-24 00:40:28 -0400 | [diff] [blame] | 49 | # General flags are always applied (e.g., selecting C++ language standard) |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame] | 50 | generalFlags = conf.flags.getGeneralFlags(conf) |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -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 | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame] | 55 | @Configure.conf |
| 56 | def check_compiler_flags(conf): |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -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. |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 60 | if conf.options.debug: |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame] | 61 | extraFlags = conf.flags.getDebugFlags(conf) |
| 62 | if conf.areCustomCxxflagsPresent: |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 63 | missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS] |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -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)) |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 68 | else: |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame] | 69 | extraFlags = conf.flags.getOptimizedFlags(conf) |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 70 | |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame] | 71 | if not conf.areCustomCxxflagsPresent: |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 72 | conf.add_supported_cxxflags(extraFlags['CXXFLAGS']) |
| 73 | conf.add_supported_linkflags(extraFlags['LINKFLAGS']) |
| 74 | |
| 75 | conf.env.DEFINES += extraFlags['DEFINES'] |
Vince Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame] | 76 | |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -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 | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 82 | if len(cxxflags) == 0: |
| 83 | return |
| 84 | |
| 85 | self.start_msg('Checking supported CXXFLAGS') |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 86 | |
| 87 | supportedFlags = [] |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [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 |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 92 | |
| 93 | self.end_msg(' '.join(supportedFlags)) |
Davide Pesavento | c58cc7f | 2017-08-08 16:51:28 -0500 | [diff] [blame] | 94 | self.env.prepend_value('CXXFLAGS', supportedFlags) |
Vince Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [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 | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 101 | if len(linkflags) == 0: |
| 102 | return |
| 103 | |
| 104 | self.start_msg('Checking supported LINKFLAGS') |
Vince Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame] | 105 | |
| 106 | supportedFlags = [] |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [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 |
Vince Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame] | 111 | |
| 112 | self.end_msg(' '.join(supportedFlags)) |
Davide Pesavento | c58cc7f | 2017-08-08 16:51:28 -0500 | [diff] [blame] | 113 | self.env.prepend_value('LINKFLAGS', supportedFlags) |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 114 | |
Davide Pesavento | 6ebfd7c | 2017-08-26 17:52:54 -0400 | [diff] [blame] | 115 | |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 116 | class CompilerFlags(object): |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame] | 117 | def getCompilerVersion(self, conf): |
| 118 | return tuple(int(i) for i in conf.env.CC_VERSION) |
| 119 | |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -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 | """ |
Davide Pesavento | 6ebfd7c | 2017-08-26 17:52:54 -0400 | [diff] [blame] | 136 | def getGeneralFlags(self, conf): |
| 137 | flags = super(GccBasicFlags, self).getGeneralFlags(conf) |
Davide Pesavento | a08dc3f | 2018-05-24 00:40:28 -0400 | [diff] [blame] | 138 | flags['CXXFLAGS'] += ['-std=c++14'] |
Davide Pesavento | e556991 | 2019-01-29 19:39:06 -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'] |
Davide Pesavento | 6ebfd7c | 2017-08-26 17:52:54 -0400 | [diff] [blame] | 143 | return flags |
| 144 | |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 145 | def getDebugFlags(self, conf): |
| 146 | flags = super(GccBasicFlags, self).getDebugFlags(conf) |
Davide Pesavento | 7ae8b08 | 2021-10-12 21:45:47 -0400 | [diff] [blame] | 147 | flags['CXXFLAGS'] += ['-Og', |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 148 | '-g3', |
| 149 | '-pedantic', |
| 150 | '-Wall', |
| 151 | '-Wextra', |
| 152 | '-Werror', |
Davide Pesavento | 7ae8b08 | 2021-10-12 21:45:47 -0400 | [diff] [blame] | 153 | '-Wcatch-value=2', |
| 154 | '-Wextra-semi', |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame] | 155 | '-Wnon-virtual-dtor', |
Alexander Afanasyev | 96bf2f0 | 2016-10-11 16:37:45 -0700 | [diff] [blame] | 156 | '-Wno-error=deprecated-declarations', # Bug #3795 |
Davide Pesavento | 6ebfd7c | 2017-08-26 17:52:54 -0400 | [diff] [blame] | 157 | '-Wno-error=maybe-uninitialized', # Bug #1615 |
| 158 | '-Wno-unused-parameter', |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 159 | ] |
Davide Pesavento | e556991 | 2019-01-29 19:39:06 -0500 | [diff] [blame] | 160 | flags['LINKFLAGS'] += ['-Wl,-O1'] |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -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 | 7ae8b08 | 2021-10-12 21:45:47 -0400 | [diff] [blame] | 170 | '-Wcatch-value=2', |
| 171 | '-Wextra-semi', |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame] | 172 | '-Wnon-virtual-dtor', |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 173 | '-Wno-unused-parameter', |
| 174 | ] |
Davide Pesavento | e556991 | 2019-01-29 19:39:06 -0500 | [diff] [blame] | 175 | flags['LINKFLAGS'] += ['-Wl,-O1'] |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 176 | return flags |
| 177 | |
| 178 | class GccFlags(GccBasicFlags): |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 179 | def getDebugFlags(self, conf): |
| 180 | flags = super(GccFlags, self).getDebugFlags(conf) |
Davide Pesavento | 7ae8b08 | 2021-10-12 21:45:47 -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 | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 186 | return flags |
| 187 | |
| 188 | def getOptimizedFlags(self, conf): |
| 189 | flags = super(GccFlags, self).getOptimizedFlags(conf) |
Davide Pesavento | 7ae8b08 | 2021-10-12 21:45:47 -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 | f9f3910 | 2015-12-01 17:43:40 -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 | 7ae8b08 | 2021-10-12 21:45:47 -0400 | [diff] [blame] | 200 | if Utils.unversioned_sys_platform() == 'darwin': |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame] | 201 | # Bug #4296 |
| 202 | flags['CXXFLAGS'] += [['-isystem', '/usr/local/include'], # for Homebrew |
| 203 | ['-isystem', '/opt/local/include']] # for MacPorts |
Davide Pesavento | 7ae8b08 | 2021-10-12 21:45:47 -0400 | [diff] [blame] | 204 | elif Utils.unversioned_sys_platform() == 'freebsd': |
| 205 | # Bug #4790 |
| 206 | flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']] |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -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 | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame] | 212 | '-Wundefined-func-template', |
Davide Pesavento | 6ebfd7c | 2017-08-26 17:52:54 -0400 | [diff] [blame] | 213 | '-Wno-unused-local-typedef', # Bugs #2657 and #3209 |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 214 | ] |
Davide Pesavento | 7ae8b08 | 2021-10-12 21:45:47 -0400 | [diff] [blame] | 215 | if self.getCompilerVersion(conf) < (6, 0, 0): |
Davide Pesavento | e556991 | 2019-01-29 19:39:06 -0500 | [diff] [blame] | 216 | flags['CXXFLAGS'] += ['-Wno-missing-braces'] # Bug #4721 |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -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 | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame] | 222 | '-Wundefined-func-template', |
Davide Pesavento | 6ebfd7c | 2017-08-26 17:52:54 -0400 | [diff] [blame] | 223 | '-Wno-unused-local-typedef', # Bugs #2657 and #3209 |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 224 | ] |
Davide Pesavento | 7ae8b08 | 2021-10-12 21:45:47 -0400 | [diff] [blame] | 225 | if self.getCompilerVersion(conf) < (6, 0, 0): |
Davide Pesavento | e556991 | 2019-01-29 19:39:06 -0500 | [diff] [blame] | 226 | flags['CXXFLAGS'] += ['-Wno-missing-braces'] # Bug #4721 |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 227 | return flags |