blob: 6869097f4337af5f2b804b317271f651b0a78564 [file] [log] [blame]
Alexander Afanasyev7eb59112014-07-02 14:21:11 -07001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
Alexander Afanasyev7eb59112014-07-02 14:21:11 -07002
Davide Pesavento5f408ae2020-07-15 21:17:04 -04003import platform
Alexander Afanasyev40491df2018-03-09 16:29:52 -05004from waflib import Configure, Logs, Utils
Alexander Afanasyev7eb59112014-07-02 14:21:11 -07005
Davide Pesavento97741e72023-08-12 16:36:07 -04006
Alexander Afanasyev7eb59112014-07-02 14:21:11 -07007def options(opt):
Alexander Afanasyev40491df2018-03-09 16:29:52 -05008 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesavento5f408ae2020-07-15 21:17:04 -04009 help='Compile in debugging mode with minimal optimizations (-Og)')
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070010
Davide Pesavento97741e72023-08-12 16:36:07 -040011
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070012def configure(conf):
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040013 conf.start_msg('Checking C++ compiler version')
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070014
Alexander Afanasyev40491df2018-03-09 16:29:52 -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)
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040018 errmsg = ''
19 warnmsg = ''
20 if cxx == 'gcc':
Davide Pesavento26a03262022-03-07 13:37:34 -050021 if ccver < (7, 4, 0):
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040022 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesaventoed778702023-04-26 15:31:31 -040023 '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 Afanasyev12d5faa2017-09-21 19:04:22 -040027 conf.flags = GccFlags()
28 elif cxx == 'clang':
Davide Pesavento26a03262022-03-07 13:37:34 -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 Pesaventoed778702023-04-26 15:31:31 -040032 '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 Pesavento26a03262022-03-07 13:37:34 -050035 'officially supported and may result in build failures.')
Davide Pesaventoed778702023-04-26 15:31:31 -040036 elif ccver < (7, 0, 0):
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040037 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesaventoed778702023-04-26 15:31:31 -040038 'The minimum supported clang version is 7.0.')
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040039 conf.flags = ClangFlags()
40 else:
Davide Pesavento15a74422022-08-19 15:48:45 -040041 warnmsg = f'{cxx} compiler is unsupported'
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040042 conf.flags = CompilerFlags()
43
44 if errmsg:
Alexander Afanasyev40491df2018-03-09 16:29:52 -050045 conf.end_msg(ccverstr, color='RED')
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040046 conf.fatal(errmsg)
47 elif warnmsg:
Alexander Afanasyev40491df2018-03-09 16:29:52 -050048 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento5f408ae2020-07-15 21:17:04 -040049 Logs.warn('WARNING: ' + warnmsg)
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040050 else:
Alexander Afanasyev40491df2018-03-09 16:29:52 -050051 conf.end_msg(ccverstr)
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040052
Davide Pesavento97741e72023-08-12 16:36:07 -040053 conf.areCustomCxxflagsPresent = len(conf.env.CXXFLAGS) > 0
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -080054
Davide Pesavento4a9395b2018-05-24 00:23:22 -040055 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040056 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -080057 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
58 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
59 conf.env.DEFINES += generalFlags['DEFINES']
60
Davide Pesavento97741e72023-08-12 16:36:07 -040061
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040062@Configure.conf
63def check_compiler_flags(conf):
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -080064 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
65 # corresponding environment variables are not set.
66 # DEFINES are always applied.
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070067 if conf.options.debug:
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040068 extraFlags = conf.flags.getDebugFlags(conf)
69 if conf.areCustomCxxflagsPresent:
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -080070 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev40491df2018-03-09 16:29:52 -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 Afanasyev7eb59112014-07-02 14:21:11 -070075 else:
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040076 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070077
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040078 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -080079 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
80 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
81
82 conf.env.DEFINES += extraFlags['DEFINES']
Yingdi Yu906c2ea2014-10-31 11:24:50 -070083
Davide Pesavento97741e72023-08-12 16:36:07 -040084
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070085@Configure.conf
86def add_supported_cxxflags(self, cxxflags):
87 """
Davide Pesavento97741e72023-08-12 16:36:07 -040088 Check which cxxflags are supported by the active compiler and add them to env.CXXFLAGS variable.
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070089 """
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -080090 if len(cxxflags) == 0:
91 return
92
Yingdi Yu906c2ea2014-10-31 11:24:50 -070093 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070094
95 supportedFlags = []
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -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 Afanasyev7eb59112014-07-02 14:21:11 -0700100
101 self.end_msg(' '.join(supportedFlags))
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800102 self.env.prepend_value('CXXFLAGS', supportedFlags)
Yingdi Yu906c2ea2014-10-31 11:24:50 -0700103
Davide Pesavento97741e72023-08-12 16:36:07 -0400104
Yingdi Yu906c2ea2014-10-31 11:24:50 -0700105@Configure.conf
106def add_supported_linkflags(self, linkflags):
107 """
Davide Pesavento97741e72023-08-12 16:36:07 -0400108 Check which linkflags are supported by the active compiler and add them to env.LINKFLAGS variable.
Yingdi Yu906c2ea2014-10-31 11:24:50 -0700109 """
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800110 if len(linkflags) == 0:
111 return
112
Yingdi Yu906c2ea2014-10-31 11:24:50 -0700113 self.start_msg('Checking supported LINKFLAGS')
114
115 supportedFlags = []
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -0400116 for flags in linkflags:
117 flags = Utils.to_list(flags)
118 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
119 supportedFlags += flags
Yingdi Yu906c2ea2014-10-31 11:24:50 -0700120
121 self.end_msg(' '.join(supportedFlags))
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800122 self.env.prepend_value('LINKFLAGS', supportedFlags)
123
124
Davide Pesavento97741e72023-08-12 16:36:07 -0400125class CompilerFlags:
Alexander Afanasyev40491df2018-03-09 16:29:52 -0500126 def getCompilerVersion(self, conf):
127 return tuple(int(i) for i in conf.env.CC_VERSION)
128
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800129 def getGeneralFlags(self, conf):
130 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Davide Pesaventod45f8882023-11-11 17:32:23 -0500131 return {
132 'CXXFLAGS': [],
133 'LINKFLAGS': [],
134 'DEFINES': ['BOOST_ASIO_NO_DEPRECATED', 'BOOST_FILESYSTEM_NO_DEPRECATED'],
135 }
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800136
137 def getDebugFlags(self, conf):
138 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Davide Pesavento11c36382024-01-19 00:23:02 -0500139 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800140
141 def getOptimizedFlags(self, conf):
142 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
143 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
144
Davide Pesavento97741e72023-08-12 16:36:07 -0400145
146class GccClangCommonFlags(CompilerFlags):
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800147 """
Davide Pesavento97741e72023-08-12 16:36:07 -0400148 This class defines common flags that work for both gcc and clang compilers.
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800149 """
Davide Pesavento97741e72023-08-12 16:36:07 -0400150
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -0400151 def getGeneralFlags(self, conf):
Davide Pesavento97741e72023-08-12 16:36:07 -0400152 flags = super().getGeneralFlags(conf)
Davide Pesavento8663ed12022-07-23 03:04:27 -0400153 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesaventocda44892022-11-15 13:40:33 -0500154 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesaventofae9def2019-01-29 14:34:33 -0500155 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -0400156 return flags
157
Davide Pesavento97741e72023-08-12 16:36:07 -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 Afanasyevf3192eb2016-12-19 17:11:20 -0800170 def getDebugFlags(self, conf):
Davide Pesavento97741e72023-08-12 16:36:07 -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 Pesavento11c36382024-01-19 00:23:02 -0500178 # Enable assertions in libstdc++
179 # https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
180 flags['DEFINES'] += ['_GLIBCXX_ASSERTIONS=1']
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800181 return flags
182
183 def getOptimizedFlags(self, conf):
Davide Pesavento97741e72023-08-12 16:36:07 -0400184 flags = super().getOptimizedFlags(conf)
185 flags['CXXFLAGS'] += ['-O2', '-g1'] + self.__cxxFlags
186 flags['LINKFLAGS'] += self.__linkFlags
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800187 return flags
188
Davide Pesavento97741e72023-08-12 16:36:07 -0400189
190class 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 Afanasyevf3192eb2016-12-19 17:11:20 -0800201 def getDebugFlags(self, conf):
Davide Pesavento97741e72023-08-12 16:36:07 -0400202 flags = super().getDebugFlags(conf)
203 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento26a03262022-03-07 13:37:34 -0500204 if platform.machine() == 'armv7l':
Davide Pesavento5f408ae2020-07-15 21:17:04 -0400205 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800206 return flags
207
208 def getOptimizedFlags(self, conf):
Davide Pesavento97741e72023-08-12 16:36:07 -0400209 flags = super().getOptimizedFlags(conf)
210 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento26a03262022-03-07 13:37:34 -0500211 if platform.machine() == 'armv7l':
Davide Pesavento5f408ae2020-07-15 21:17:04 -0400212 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800213 return flags
214
Davide Pesavento97741e72023-08-12 16:36:07 -0400215
216class ClangFlags(GccClangCommonFlags):
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800217 def getGeneralFlags(self, conf):
Davide Pesavento97741e72023-08-12 16:36:07 -0400218 flags = super().getGeneralFlags(conf)
Davide Pesavento5f408ae2020-07-15 21:17:04 -0400219 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev40491df2018-03-09 16:29:52 -0500220 # Bug #4296
Davide Pesavento15a74422022-08-19 15:48:45 -0400221 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
Davide Pesavento97741e72023-08-12 16:36:07 -0400222 flags['CXXFLAGS'] += [
223 ['-isystem', f'{brewdir}/include'], # for Homebrew
224 ['-isystem', '/opt/local/include'], # for MacPorts
225 ]
Davide Pesavento5f408ae2020-07-15 21:17:04 -0400226 elif Utils.unversioned_sys_platform() == 'freebsd':
227 # Bug #4790
228 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Davide Pesavento11c36382024-01-19 00:23:02 -0500229 if self.getCompilerVersion(conf) >= (18, 0, 0):
230 # Bug #5300
231 flags['CXXFLAGS'] += ['-Wno-enum-constexpr-conversion']
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800232 return flags
233
Davide Pesavento97741e72023-08-12 16:36:07 -0400234 __cxxFlags = [
235 '-Wundefined-func-template',
236 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
237 ]
238
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800239 def getDebugFlags(self, conf):
Davide Pesavento97741e72023-08-12 16:36:07 -0400240 flags = super().getDebugFlags(conf)
241 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento11c36382024-01-19 00:23:02 -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']
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800249 return flags
250
251 def getOptimizedFlags(self, conf):
Davide Pesavento97741e72023-08-12 16:36:07 -0400252 flags = super().getOptimizedFlags(conf)
253 flags['CXXFLAGS'] += self.__cxxFlags
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800254 return flags