blob: 280165cdbc45e4229732102f67d843e43b96532e [file] [log] [blame]
Davide Pesavento71c622b2020-04-24 01:39:01 -04001import platform
Alexander Afanasyev5560fd42018-03-07 17:03:03 -05002from waflib import Configure, Logs, Utils
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -07003
Davide Pesavento12b8aaa2023-08-06 19:18:51 -04004
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -07005def options(opt):
Alexander Afanasyev5560fd42018-03-07 17:03:03 -05006 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesavento71c622b2020-04-24 01:39:01 -04007 help='Compile in debugging mode with minimal optimizations (-Og)')
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -07008
Davide Pesavento12b8aaa2023-08-06 19:18:51 -04009
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070010def configure(conf):
Davide Pesavento88a0d812017-08-19 21:31:42 -040011 conf.start_msg('Checking C++ compiler version')
12
Alexander Afanasyev5560fd42018-03-07 17:03:03 -050013 cxx = conf.env.CXX_NAME # generic name of the compiler
Davide Pesavento8a8afa22025-03-12 17:42:20 -040014 ccver = get_compiler_ver(conf)
Alexander Afanasyev5560fd42018-03-07 17:03:03 -050015 ccverstr = '.'.join(conf.env.CC_VERSION)
Davide Pesavento88a0d812017-08-19 21:31:42 -040016 errmsg = ''
17 warnmsg = ''
Alexander Afanasyev01515792014-12-16 14:20:01 -080018 if cxx == 'gcc':
Davide Pesaventoe913e3a2024-12-10 20:25:04 -050019 if ccver < (9, 1, 0):
Davide Pesavento88a0d812017-08-19 21:31:42 -040020 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesaventoe913e3a2024-12-10 20:25:04 -050021 'The minimum supported gcc version is 10.2.')
22 elif ccver < (10, 2, 0):
23 warnmsg = ('Using a version of gcc older than 10.2 is not '
Davide Pesavento762548b2023-03-05 11:02:02 -050024 'officially supported and may result in build failures.')
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040025 conf.flags = GccFlags()
Alexander Afanasyev01515792014-12-16 14:20:01 -080026 elif cxx == 'clang':
Davide Pesavento541a8222022-03-01 15:08:42 -050027 if Utils.unversioned_sys_platform() == 'darwin':
Davide Pesaventoe913e3a2024-12-10 20:25:04 -050028 if ccver < (11, 0, 0):
Davide Pesavento541a8222022-03-01 15:08:42 -050029 errmsg = ('The version of Xcode you are using is too old.\n'
Davide Pesavento2be774b2024-06-07 17:25:31 -040030 'The minimum supported Xcode version is 13.0.')
31 elif ccver < (13, 0, 0):
32 warnmsg = ('Using a version of Xcode older than 13.0 is not '
Davide Pesavento541a8222022-03-01 15:08:42 -050033 'officially supported and may result in build failures.')
Davide Pesavento762548b2023-03-05 11:02:02 -050034 elif ccver < (7, 0, 0):
Davide Pesavento88a0d812017-08-19 21:31:42 -040035 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesaventoe913e3a2024-12-10 20:25:04 -050036 'The minimum supported clang version is 10.0.')
37 elif ccver < (10, 0, 0):
38 warnmsg = ('Using a version of clang older than 10.0 is not '
39 'officially supported and may result in build failures.')
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040040 conf.flags = ClangFlags()
Alexander Afanasyev01515792014-12-16 14:20:01 -080041 else:
Davide Pesaventof6625002022-07-31 17:15:02 -040042 warnmsg = f'{cxx} compiler is unsupported'
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040043 conf.flags = CompilerFlags()
Davide Pesavento88a0d812017-08-19 21:31:42 -040044
45 if errmsg:
Alexander Afanasyev5560fd42018-03-07 17:03:03 -050046 conf.end_msg(ccverstr, color='RED')
Davide Pesavento88a0d812017-08-19 21:31:42 -040047 conf.fatal(errmsg)
48 elif warnmsg:
Alexander Afanasyev5560fd42018-03-07 17:03:03 -050049 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento71c622b2020-04-24 01:39:01 -040050 Logs.warn('WARNING: ' + warnmsg)
Davide Pesavento88a0d812017-08-19 21:31:42 -040051 else:
Alexander Afanasyev5560fd42018-03-07 17:03:03 -050052 conf.end_msg(ccverstr)
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -070053
Davide Pesavento12b8aaa2023-08-06 19:18:51 -040054 conf.areCustomCxxflagsPresent = len(conf.env.CXXFLAGS) > 0
Alexander Afanasyev01515792014-12-16 14:20:01 -080055
Davide Pesavento1fd00242018-05-20 00:11:01 -040056 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040057 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyev01515792014-12-16 14:20:01 -080058 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
59 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
60 conf.env.DEFINES += generalFlags['DEFINES']
61
Davide Pesavento12b8aaa2023-08-06 19:18:51 -040062
Davide Pesavento8a8afa22025-03-12 17:42:20 -040063def get_compiler_ver(conf):
64 return tuple(int(i) for i in conf.env.CC_VERSION)
65
66
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040067@Configure.conf
68def check_compiler_flags(conf):
Davide Pesaventod06ea052015-10-02 01:48:24 +020069 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Alexander Afanasyev01515792014-12-16 14:20:01 -080070 # corresponding environment variables are not set.
Davide Pesaventod06ea052015-10-02 01:48:24 +020071 # DEFINES are always applied.
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070072 if conf.options.debug:
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040073 extraFlags = conf.flags.getDebugFlags(conf)
74 if conf.areCustomCxxflagsPresent:
Alexander Afanasyev01515792014-12-16 14:20:01 -080075 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev5560fd42018-03-07 17:03:03 -050076 if missingFlags:
77 Logs.warn('Selected debug mode, but CXXFLAGS is set to a custom value "%s"'
78 % ' '.join(conf.env.CXXFLAGS))
79 Logs.warn('Default flags "%s" will not be used' % ' '.join(missingFlags))
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070080 else:
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040081 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070082
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040083 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyev01515792014-12-16 14:20:01 -080084 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Davide Pesavento7c02b472015-10-28 20:50:17 +010085 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Alexander Afanasyev01515792014-12-16 14:20:01 -080086
87 conf.env.DEFINES += extraFlags['DEFINES']
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020088
Davide Pesavento12b8aaa2023-08-06 19:18:51 -040089
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070090@Configure.conf
91def add_supported_cxxflags(self, cxxflags):
92 """
Davide Pesavento12b8aaa2023-08-06 19:18:51 -040093 Check which cxxflags are supported by the active compiler and add them to env.CXXFLAGS variable.
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070094 """
Alexander Afanasyev01515792014-12-16 14:20:01 -080095 if len(cxxflags) == 0:
96 return
97
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020098 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070099
100 supportedFlags = []
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -0400101 for flags in cxxflags:
102 flags = Utils.to_list(flags)
103 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
104 supportedFlags += flags
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -0700105
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -0700106 self.end_msg(' '.join(supportedFlags))
Davide Pesavento1349d2d2016-08-09 06:43:57 +0200107 self.env.prepend_value('CXXFLAGS', supportedFlags)
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +0200108
Davide Pesavento12b8aaa2023-08-06 19:18:51 -0400109
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +0200110@Configure.conf
111def add_supported_linkflags(self, linkflags):
112 """
Davide Pesavento12b8aaa2023-08-06 19:18:51 -0400113 Check which linkflags are supported by the active compiler and add them to env.LINKFLAGS variable.
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +0200114 """
Alexander Afanasyev01515792014-12-16 14:20:01 -0800115 if len(linkflags) == 0:
116 return
117
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +0200118 self.start_msg('Checking supported LINKFLAGS')
119
120 supportedFlags = []
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -0400121 for flags in linkflags:
122 flags = Utils.to_list(flags)
123 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
124 supportedFlags += flags
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +0200125
126 self.end_msg(' '.join(supportedFlags))
Davide Pesavento1349d2d2016-08-09 06:43:57 +0200127 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyev01515792014-12-16 14:20:01 -0800128
129
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400130class CompilerFlags:
Alexander Afanasyev01515792014-12-16 14:20:01 -0800131 def getGeneralFlags(self, conf):
Davide Pesavento7c02b472015-10-28 20:50:17 +0100132 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Davide Pesavento682b2af2024-02-23 21:25:41 -0500133 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
134
135 def getDebugFlags(self, conf):
136 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Davide Pesavento2f46d652023-11-09 23:40:01 -0500137 return {
138 'CXXFLAGS': [],
139 'LINKFLAGS': [],
Davide Pesavento51974f62024-12-21 20:42:45 -0500140 'DEFINES': ['BOOST_ASIO_NO_DEPRECATED'],
Davide Pesavento2f46d652023-11-09 23:40:01 -0500141 }
Alexander Afanasyev01515792014-12-16 14:20:01 -0800142
Alexander Afanasyev01515792014-12-16 14:20:01 -0800143 def getOptimizedFlags(self, conf):
Davide Pesavento7c02b472015-10-28 20:50:17 +0100144 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Alexander Afanasyev01515792014-12-16 14:20:01 -0800145 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
146
Davide Pesavento12b8aaa2023-08-06 19:18:51 -0400147
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400148class GccClangCommonFlags(CompilerFlags):
Davide Pesaventod06ea052015-10-02 01:48:24 +0200149 """
Davide Pesavento12b8aaa2023-08-06 19:18:51 -0400150 This class defines common flags that work for both gcc and clang compilers.
Davide Pesaventod06ea052015-10-02 01:48:24 +0200151 """
Davide Pesavento12b8aaa2023-08-06 19:18:51 -0400152
Davide Pesavento724d65a2017-06-23 17:14:08 -0400153 def getGeneralFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400154 flags = super().getGeneralFlags(conf)
Davide Pesaventofcd3e442023-03-10 18:44:11 -0500155 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesaventoea5ce492022-09-30 20:25:25 -0400156 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesavento64ae55d2018-12-19 00:25:47 -0500157 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Davide Pesavento724d65a2017-06-23 17:14:08 -0400158 return flags
159
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400160 __cxxFlags = [
161 '-fdiagnostics-color',
162 '-Wall',
163 '-Wextra',
164 '-Wpedantic',
165 '-Wenum-conversion',
166 '-Wextra-semi',
167 '-Wnon-virtual-dtor',
168 '-Wno-unused-parameter',
169 ]
170 __linkFlags = ['-Wl,-O1']
171
Alexander Afanasyev01515792014-12-16 14:20:01 -0800172 def getDebugFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400173 flags = super().getDebugFlags(conf)
174 flags['CXXFLAGS'] += ['-Og', '-g'] + self.__cxxFlags + [
175 '-Werror',
176 '-Wno-error=deprecated-declarations', # Bug #3795
177 '-Wno-error=maybe-uninitialized', # Bug #1615
178 ]
179 flags['LINKFLAGS'] += self.__linkFlags
Davide Pesavento5fdab132024-01-14 16:30:06 -0500180 # Enable assertions in libstdc++
181 # https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
182 flags['DEFINES'] += ['_GLIBCXX_ASSERTIONS=1']
Alexander Afanasyev01515792014-12-16 14:20:01 -0800183 return flags
184
185 def getOptimizedFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400186 flags = super().getOptimizedFlags(conf)
187 flags['CXXFLAGS'] += ['-O2', '-g1'] + self.__cxxFlags
188 flags['LINKFLAGS'] += self.__linkFlags
Alexander Afanasyev01515792014-12-16 14:20:01 -0800189 return flags
190
Davide Pesavento12b8aaa2023-08-06 19:18:51 -0400191
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400192class GccFlags(GccClangCommonFlags):
193 __cxxFlags = [
194 '-Wcatch-value=2',
195 '-Wcomma-subscript', # enabled by default in C++20
196 '-Wduplicated-branches',
197 '-Wduplicated-cond',
198 '-Wlogical-op',
199 '-Wredundant-tags',
200 '-Wvolatile', # enabled by default in C++20
201 ]
202
Alexander Afanasyev01515792014-12-16 14:20:01 -0800203 def getDebugFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400204 flags = super().getDebugFlags(conf)
205 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento541a8222022-03-01 15:08:42 -0500206 if platform.machine() == 'armv7l':
Davide Pesavento71c622b2020-04-24 01:39:01 -0400207 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyev01515792014-12-16 14:20:01 -0800208 return flags
209
Davide Pesaventod06ea052015-10-02 01:48:24 +0200210 def getOptimizedFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400211 flags = super().getOptimizedFlags(conf)
212 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento541a8222022-03-01 15:08:42 -0500213 if platform.machine() == 'armv7l':
Davide Pesavento71c622b2020-04-24 01:39:01 -0400214 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Davide Pesaventod06ea052015-10-02 01:48:24 +0200215 return flags
216
Davide Pesavento12b8aaa2023-08-06 19:18:51 -0400217
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400218class ClangFlags(GccClangCommonFlags):
Alexander Afanasyev01515792014-12-16 14:20:01 -0800219 def getGeneralFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400220 flags = super().getGeneralFlags(conf)
Davide Pesavento71c622b2020-04-24 01:39:01 -0400221 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev5560fd42018-03-07 17:03:03 -0500222 # Bug #4296
Davide Pesaventof6625002022-07-31 17:15:02 -0400223 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400224 flags['CXXFLAGS'] += [
225 ['-isystem', f'{brewdir}/include'], # for Homebrew
226 ['-isystem', '/opt/local/include'], # for MacPorts
227 ]
Davide Pesavento1208aa12025-04-03 03:42:02 -0400228 else:
229 if Utils.unversioned_sys_platform() == 'freebsd':
230 # Bug #4790
231 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
232 if get_compiler_ver(conf) >= (18, 0, 0) and get_compiler_ver(conf) < (20, 1, 0):
233 # Bug #5300
234 flags['CXXFLAGS'] += ['-Wno-enum-constexpr-conversion']
Alexander Afanasyev01515792014-12-16 14:20:01 -0800235 return flags
236
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400237 __cxxFlags = [
238 '-Wundefined-func-template',
239 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
240 ]
241
Alexander Afanasyev01515792014-12-16 14:20:01 -0800242 def getDebugFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400243 flags = super().getDebugFlags(conf)
244 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento1208aa12025-04-03 03:42:02 -0400245 ccver = get_compiler_ver(conf)
246 darwin = Utils.unversioned_sys_platform() == 'darwin'
Davide Pesavento5fdab132024-01-14 16:30:06 -0500247 # Enable assertions in libc++
Davide Pesavento1208aa12025-04-03 03:42:02 -0400248 if (darwin and ccver >= (17, 0, 0)) or (not darwin and ccver >= (18, 0, 0)):
Davide Pesavento5fdab132024-01-14 16:30:06 -0500249 # https://libcxx.llvm.org/Hardening.html
250 flags['DEFINES'] += ['_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE']
Davide Pesavento1208aa12025-04-03 03:42:02 -0400251 elif ccver >= (15, 0, 0):
Davide Pesavento5fdab132024-01-14 16:30:06 -0500252 # https://releases.llvm.org/15.0.0/projects/libcxx/docs/UsingLibcxx.html#enabling-the-safe-libc-mode
253 flags['DEFINES'] += ['_LIBCPP_ENABLE_ASSERTIONS=1']
Davide Pesavento682b2af2024-02-23 21:25:41 -0500254 # Tell libc++ to avoid including transitive headers
255 # https://libcxx.llvm.org/DesignDocs/HeaderRemovalPolicy.html
256 flags['DEFINES'] += ['_LIBCPP_REMOVE_TRANSITIVE_INCLUDES=1']
Davide Pesaventod06ea052015-10-02 01:48:24 +0200257 return flags
258
259 def getOptimizedFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400260 flags = super().getOptimizedFlags(conf)
261 flags['CXXFLAGS'] += self.__cxxFlags
Alexander Afanasyev01515792014-12-16 14:20:01 -0800262 return flags