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