blob: 6869097f4337af5f2b804b317271f651b0a78564 [file] [log] [blame]
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -07001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -07002
Davide Pesavento71c622b2020-04-24 01:39:01 -04003import platform
Alexander Afanasyev5560fd42018-03-07 17:03:03 -05004from waflib import Configure, Logs, Utils
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -07005
Davide Pesavento12b8aaa2023-08-06 19:18:51 -04006
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -07007def options(opt):
Alexander Afanasyev5560fd42018-03-07 17:03:03 -05008 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesavento71c622b2020-04-24 01:39:01 -04009 help='Compile in debugging mode with minimal optimizations (-Og)')
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070010
Davide Pesavento12b8aaa2023-08-06 19:18:51 -040011
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070012def configure(conf):
Davide Pesavento88a0d812017-08-19 21:31:42 -040013 conf.start_msg('Checking C++ compiler version')
14
Alexander Afanasyev5560fd42018-03-07 17:03:03 -050015 cxx = conf.env.CXX_NAME # generic name of the compiler
16 ccver = tuple(int(i) for i in conf.env.CC_VERSION)
17 ccverstr = '.'.join(conf.env.CC_VERSION)
Davide Pesavento88a0d812017-08-19 21:31:42 -040018 errmsg = ''
19 warnmsg = ''
Alexander Afanasyev01515792014-12-16 14:20:01 -080020 if cxx == 'gcc':
Davide Pesavento541a8222022-03-01 15:08:42 -050021 if ccver < (7, 4, 0):
Davide Pesavento88a0d812017-08-19 21:31:42 -040022 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesavento762548b2023-03-05 11:02:02 -050023 'The minimum supported gcc version is 9.3.')
24 elif ccver < (9, 3, 0):
25 warnmsg = ('Using a version of gcc older than 9.3 is not '
26 'officially supported and may result in build failures.')
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040027 conf.flags = GccFlags()
Alexander Afanasyev01515792014-12-16 14:20:01 -080028 elif cxx == 'clang':
Davide Pesavento541a8222022-03-01 15:08:42 -050029 if Utils.unversioned_sys_platform() == 'darwin':
30 if ccver < (10, 0, 0):
31 errmsg = ('The version of Xcode you are using is too old.\n'
Davide Pesavento762548b2023-03-05 11:02:02 -050032 'The minimum supported Xcode version is 12.4.')
33 elif ccver < (12, 0, 0):
34 warnmsg = ('Using a version of Xcode older than 12.4 is not '
Davide Pesavento541a8222022-03-01 15:08:42 -050035 'officially supported and may result in build failures.')
Davide Pesavento762548b2023-03-05 11:02:02 -050036 elif ccver < (7, 0, 0):
Davide Pesavento88a0d812017-08-19 21:31:42 -040037 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesavento762548b2023-03-05 11:02:02 -050038 'The minimum supported clang version is 7.0.')
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040039 conf.flags = ClangFlags()
Alexander Afanasyev01515792014-12-16 14:20:01 -080040 else:
Davide Pesaventof6625002022-07-31 17:15:02 -040041 warnmsg = f'{cxx} compiler is unsupported'
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040042 conf.flags = CompilerFlags()
Davide Pesavento88a0d812017-08-19 21:31:42 -040043
44 if errmsg:
Alexander Afanasyev5560fd42018-03-07 17:03:03 -050045 conf.end_msg(ccverstr, color='RED')
Davide Pesavento88a0d812017-08-19 21:31:42 -040046 conf.fatal(errmsg)
47 elif warnmsg:
Alexander Afanasyev5560fd42018-03-07 17:03:03 -050048 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento71c622b2020-04-24 01:39:01 -040049 Logs.warn('WARNING: ' + warnmsg)
Davide Pesavento88a0d812017-08-19 21:31:42 -040050 else:
Alexander Afanasyev5560fd42018-03-07 17:03:03 -050051 conf.end_msg(ccverstr)
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -070052
Davide Pesavento12b8aaa2023-08-06 19:18:51 -040053 conf.areCustomCxxflagsPresent = len(conf.env.CXXFLAGS) > 0
Alexander Afanasyev01515792014-12-16 14:20:01 -080054
Davide Pesavento1fd00242018-05-20 00:11:01 -040055 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040056 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyev01515792014-12-16 14:20:01 -080057 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
58 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
59 conf.env.DEFINES += generalFlags['DEFINES']
60
Davide Pesavento12b8aaa2023-08-06 19:18:51 -040061
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040062@Configure.conf
63def check_compiler_flags(conf):
Davide Pesaventod06ea052015-10-02 01:48:24 +020064 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Alexander Afanasyev01515792014-12-16 14:20:01 -080065 # corresponding environment variables are not set.
Davide Pesaventod06ea052015-10-02 01:48:24 +020066 # DEFINES are always applied.
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070067 if conf.options.debug:
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040068 extraFlags = conf.flags.getDebugFlags(conf)
69 if conf.areCustomCxxflagsPresent:
Alexander Afanasyev01515792014-12-16 14:20:01 -080070 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev5560fd42018-03-07 17:03:03 -050071 if missingFlags:
72 Logs.warn('Selected debug mode, but CXXFLAGS is set to a custom value "%s"'
73 % ' '.join(conf.env.CXXFLAGS))
74 Logs.warn('Default flags "%s" will not be used' % ' '.join(missingFlags))
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070075 else:
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040076 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070077
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040078 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyev01515792014-12-16 14:20:01 -080079 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Davide Pesavento7c02b472015-10-28 20:50:17 +010080 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Alexander Afanasyev01515792014-12-16 14:20:01 -080081
82 conf.env.DEFINES += extraFlags['DEFINES']
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020083
Davide Pesavento12b8aaa2023-08-06 19:18:51 -040084
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070085@Configure.conf
86def add_supported_cxxflags(self, cxxflags):
87 """
Davide Pesavento12b8aaa2023-08-06 19:18:51 -040088 Check which cxxflags are supported by the active compiler and add them to env.CXXFLAGS variable.
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070089 """
Alexander Afanasyev01515792014-12-16 14:20:01 -080090 if len(cxxflags) == 0:
91 return
92
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020093 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070094
95 supportedFlags = []
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040096 for flags in cxxflags:
97 flags = Utils.to_list(flags)
98 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
99 supportedFlags += flags
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -0700100
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -0700101 self.end_msg(' '.join(supportedFlags))
Davide Pesavento1349d2d2016-08-09 06:43:57 +0200102 self.env.prepend_value('CXXFLAGS', supportedFlags)
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +0200103
Davide Pesavento12b8aaa2023-08-06 19:18:51 -0400104
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +0200105@Configure.conf
106def add_supported_linkflags(self, linkflags):
107 """
Davide Pesavento12b8aaa2023-08-06 19:18:51 -0400108 Check which linkflags are supported by the active compiler and add them to env.LINKFLAGS variable.
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +0200109 """
Alexander Afanasyev01515792014-12-16 14:20:01 -0800110 if len(linkflags) == 0:
111 return
112
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +0200113 self.start_msg('Checking supported LINKFLAGS')
114
115 supportedFlags = []
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -0400116 for flags in linkflags:
117 flags = Utils.to_list(flags)
118 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
119 supportedFlags += flags
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +0200120
121 self.end_msg(' '.join(supportedFlags))
Davide Pesavento1349d2d2016-08-09 06:43:57 +0200122 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyev01515792014-12-16 14:20:01 -0800123
124
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400125class CompilerFlags:
Alexander Afanasyev5560fd42018-03-07 17:03:03 -0500126 def getCompilerVersion(self, conf):
127 return tuple(int(i) for i in conf.env.CC_VERSION)
128
Alexander Afanasyev01515792014-12-16 14:20:01 -0800129 def getGeneralFlags(self, conf):
Davide Pesavento7c02b472015-10-28 20:50:17 +0100130 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Davide Pesavento2f46d652023-11-09 23:40:01 -0500131 return {
132 'CXXFLAGS': [],
133 'LINKFLAGS': [],
134 'DEFINES': ['BOOST_ASIO_NO_DEPRECATED', 'BOOST_FILESYSTEM_NO_DEPRECATED'],
135 }
Alexander Afanasyev01515792014-12-16 14:20:01 -0800136
137 def getDebugFlags(self, conf):
Davide Pesavento7c02b472015-10-28 20:50:17 +0100138 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Davide Pesaventoc5003932024-01-14 20:33:02 -0500139 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
Alexander Afanasyev01515792014-12-16 14:20:01 -0800140
141 def getOptimizedFlags(self, conf):
Davide Pesavento7c02b472015-10-28 20:50:17 +0100142 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Alexander Afanasyev01515792014-12-16 14:20:01 -0800143 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
144
Davide Pesavento12b8aaa2023-08-06 19:18:51 -0400145
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400146class GccClangCommonFlags(CompilerFlags):
Davide Pesaventod06ea052015-10-02 01:48:24 +0200147 """
Davide Pesavento12b8aaa2023-08-06 19:18:51 -0400148 This class defines common flags that work for both gcc and clang compilers.
Davide Pesaventod06ea052015-10-02 01:48:24 +0200149 """
Davide Pesavento12b8aaa2023-08-06 19:18:51 -0400150
Davide Pesavento724d65a2017-06-23 17:14:08 -0400151 def getGeneralFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400152 flags = super().getGeneralFlags(conf)
Davide Pesaventofcd3e442023-03-10 18:44:11 -0500153 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesaventoea5ce492022-09-30 20:25:25 -0400154 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesavento64ae55d2018-12-19 00:25:47 -0500155 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Davide Pesavento724d65a2017-06-23 17:14:08 -0400156 return flags
157
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400158 __cxxFlags = [
159 '-fdiagnostics-color',
160 '-Wall',
161 '-Wextra',
162 '-Wpedantic',
163 '-Wenum-conversion',
164 '-Wextra-semi',
165 '-Wnon-virtual-dtor',
166 '-Wno-unused-parameter',
167 ]
168 __linkFlags = ['-Wl,-O1']
169
Alexander Afanasyev01515792014-12-16 14:20:01 -0800170 def getDebugFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400171 flags = super().getDebugFlags(conf)
172 flags['CXXFLAGS'] += ['-Og', '-g'] + self.__cxxFlags + [
173 '-Werror',
174 '-Wno-error=deprecated-declarations', # Bug #3795
175 '-Wno-error=maybe-uninitialized', # Bug #1615
176 ]
177 flags['LINKFLAGS'] += self.__linkFlags
Davide Pesavento5fdab132024-01-14 16:30:06 -0500178 # Enable assertions in libstdc++
179 # https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
180 flags['DEFINES'] += ['_GLIBCXX_ASSERTIONS=1']
Alexander Afanasyev01515792014-12-16 14:20:01 -0800181 return flags
182
183 def getOptimizedFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400184 flags = super().getOptimizedFlags(conf)
185 flags['CXXFLAGS'] += ['-O2', '-g1'] + self.__cxxFlags
186 flags['LINKFLAGS'] += self.__linkFlags
Alexander Afanasyev01515792014-12-16 14:20:01 -0800187 return flags
188
Davide Pesavento12b8aaa2023-08-06 19:18:51 -0400189
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400190class GccFlags(GccClangCommonFlags):
191 __cxxFlags = [
192 '-Wcatch-value=2',
193 '-Wcomma-subscript', # enabled by default in C++20
194 '-Wduplicated-branches',
195 '-Wduplicated-cond',
196 '-Wlogical-op',
197 '-Wredundant-tags',
198 '-Wvolatile', # enabled by default in C++20
199 ]
200
Alexander Afanasyev01515792014-12-16 14:20:01 -0800201 def getDebugFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400202 flags = super().getDebugFlags(conf)
203 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento541a8222022-03-01 15:08:42 -0500204 if platform.machine() == 'armv7l':
Davide Pesavento71c622b2020-04-24 01:39:01 -0400205 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyev01515792014-12-16 14:20:01 -0800206 return flags
207
Davide Pesaventod06ea052015-10-02 01:48:24 +0200208 def getOptimizedFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400209 flags = super().getOptimizedFlags(conf)
210 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento541a8222022-03-01 15:08:42 -0500211 if platform.machine() == 'armv7l':
Davide Pesavento71c622b2020-04-24 01:39:01 -0400212 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Davide Pesaventod06ea052015-10-02 01:48:24 +0200213 return flags
214
Davide Pesavento12b8aaa2023-08-06 19:18:51 -0400215
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400216class ClangFlags(GccClangCommonFlags):
Alexander Afanasyev01515792014-12-16 14:20:01 -0800217 def getGeneralFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400218 flags = super().getGeneralFlags(conf)
Davide Pesavento71c622b2020-04-24 01:39:01 -0400219 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev5560fd42018-03-07 17:03:03 -0500220 # Bug #4296
Davide Pesaventof6625002022-07-31 17:15:02 -0400221 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400222 flags['CXXFLAGS'] += [
223 ['-isystem', f'{brewdir}/include'], # for Homebrew
224 ['-isystem', '/opt/local/include'], # for MacPorts
225 ]
Davide Pesavento71c622b2020-04-24 01:39:01 -0400226 elif Utils.unversioned_sys_platform() == 'freebsd':
227 # Bug #4790
228 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Davide Pesaventoecfb2172024-01-13 18:26:18 -0500229 if self.getCompilerVersion(conf) >= (18, 0, 0):
230 # Bug #5300
231 flags['CXXFLAGS'] += ['-Wno-enum-constexpr-conversion']
Alexander Afanasyev01515792014-12-16 14:20:01 -0800232 return flags
233
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400234 __cxxFlags = [
235 '-Wundefined-func-template',
236 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
237 ]
238
Alexander Afanasyev01515792014-12-16 14:20:01 -0800239 def getDebugFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400240 flags = super().getDebugFlags(conf)
241 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento5fdab132024-01-14 16:30:06 -0500242 # Enable assertions in libc++
243 if self.getCompilerVersion(conf) >= (18, 0, 0):
244 # https://libcxx.llvm.org/Hardening.html
245 flags['DEFINES'] += ['_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE']
246 elif self.getCompilerVersion(conf) >= (15, 0, 0):
247 # https://releases.llvm.org/15.0.0/projects/libcxx/docs/UsingLibcxx.html#enabling-the-safe-libc-mode
248 flags['DEFINES'] += ['_LIBCPP_ENABLE_ASSERTIONS=1']
Davide Pesaventod06ea052015-10-02 01:48:24 +0200249 return flags
250
251 def getOptimizedFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400252 flags = super().getOptimizedFlags(conf)
253 flags['CXXFLAGS'] += self.__cxxFlags
Alexander Afanasyev01515792014-12-16 14:20:01 -0800254 return flags