blob: ef97f25725ba750871fd0fc01c09711bcbc2c81d [file] [log] [blame]
Yingdi Yu40cd1c32014-04-17 15:02:17 -07001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
Yingdi Yu40cd1c32014-04-17 15:02:17 -07002
Davide Pesavento7ae8b082021-10-12 21:45:47 -04003import platform
Alexander Afanasyev67758b12018-03-06 18:36:44 -05004from waflib import Configure, Logs, Utils
Yingdi Yu40cd1c32014-04-17 15:02:17 -07005
Davide Pesavento5110a8a2023-08-20 21:42:11 -04006
Yingdi Yu40cd1c32014-04-17 15:02:17 -07007def options(opt):
Alexander Afanasyev67758b12018-03-06 18:36:44 -05008 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesavento7ae8b082021-10-12 21:45:47 -04009 help='Compile in debugging mode with minimal optimizations (-Og)')
Yingdi Yu40cd1c32014-04-17 15:02:17 -070010
Davide Pesavento5110a8a2023-08-20 21:42:11 -040011
Yingdi Yu40cd1c32014-04-17 15:02:17 -070012def configure(conf):
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -040013 conf.start_msg('Checking C++ compiler version')
14
Alexander Afanasyev67758b12018-03-06 18:36:44 -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 Pesavento6ebfd7c2017-08-26 17:52:54 -040018 errmsg = ''
19 warnmsg = ''
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080020 if cxx == 'gcc':
Davide Pesaventod1f1df82022-03-12 16:40:37 -050021 if ccver < (7, 4, 0):
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -040022 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesavento5110a8a2023-08-20 21:42:11 -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 Afanasyev67758b12018-03-06 18:36:44 -050027 conf.flags = GccFlags()
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080028 elif cxx == 'clang':
Davide Pesaventod1f1df82022-03-12 16:40:37 -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 Pesavento5110a8a2023-08-20 21:42:11 -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 Pesaventod1f1df82022-03-12 16:40:37 -050035 'officially supported and may result in build failures.')
Davide Pesavento5110a8a2023-08-20 21:42:11 -040036 elif ccver < (7, 0, 0):
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -040037 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesavento5110a8a2023-08-20 21:42:11 -040038 'The minimum supported clang version is 7.0.')
Alexander Afanasyev67758b12018-03-06 18:36:44 -050039 conf.flags = ClangFlags()
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080040 else:
Davide Pesaventoede59632022-08-26 20:35:44 -040041 warnmsg = f'{cxx} compiler is unsupported'
Alexander Afanasyev67758b12018-03-06 18:36:44 -050042 conf.flags = CompilerFlags()
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -040043
44 if errmsg:
Alexander Afanasyev67758b12018-03-06 18:36:44 -050045 conf.end_msg(ccverstr, color='RED')
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -040046 conf.fatal(errmsg)
47 elif warnmsg:
Alexander Afanasyev67758b12018-03-06 18:36:44 -050048 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento7ae8b082021-10-12 21:45:47 -040049 Logs.warn('WARNING: ' + warnmsg)
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -040050 else:
Alexander Afanasyev67758b12018-03-06 18:36:44 -050051 conf.end_msg(ccverstr)
Yingdi Yu40cd1c32014-04-17 15:02:17 -070052
Davide Pesavento5110a8a2023-08-20 21:42:11 -040053 conf.areCustomCxxflagsPresent = len(conf.env.CXXFLAGS) > 0
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080054
Davide Pesaventoa08dc3f2018-05-24 00:40:28 -040055 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyev67758b12018-03-06 18:36:44 -050056 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080057 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
58 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
59 conf.env.DEFINES += generalFlags['DEFINES']
60
Davide Pesavento5110a8a2023-08-20 21:42:11 -040061
Alexander Afanasyev67758b12018-03-06 18:36:44 -050062@Configure.conf
63def check_compiler_flags(conf):
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080064 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
65 # corresponding environment variables are not set.
66 # DEFINES are always applied.
Yingdi Yu40cd1c32014-04-17 15:02:17 -070067 if conf.options.debug:
Alexander Afanasyev67758b12018-03-06 18:36:44 -050068 extraFlags = conf.flags.getDebugFlags(conf)
69 if conf.areCustomCxxflagsPresent:
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080070 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev67758b12018-03-06 18:36:44 -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))
Yingdi Yu40cd1c32014-04-17 15:02:17 -070075 else:
Alexander Afanasyev67758b12018-03-06 18:36:44 -050076 extraFlags = conf.flags.getOptimizedFlags(conf)
Yingdi Yu40cd1c32014-04-17 15:02:17 -070077
Alexander Afanasyev67758b12018-03-06 18:36:44 -050078 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080079 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
80 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
81
82 conf.env.DEFINES += extraFlags['DEFINES']
Vince Lehman0a7da612014-10-29 14:39:29 -050083
Davide Pesavento5110a8a2023-08-20 21:42:11 -040084
Yingdi Yu40cd1c32014-04-17 15:02:17 -070085@Configure.conf
86def add_supported_cxxflags(self, cxxflags):
87 """
Davide Pesavento5110a8a2023-08-20 21:42:11 -040088 Check which cxxflags are supported by the active compiler and add them to env.CXXFLAGS variable.
Yingdi Yu40cd1c32014-04-17 15:02:17 -070089 """
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080090 if len(cxxflags) == 0:
91 return
92
93 self.start_msg('Checking supported CXXFLAGS')
Yingdi Yu40cd1c32014-04-17 15:02:17 -070094
95 supportedFlags = []
Alexander Afanasyev67758b12018-03-06 18:36:44 -050096 for flags in cxxflags:
97 flags = Utils.to_list(flags)
98 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
99 supportedFlags += flags
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700100
101 self.end_msg(' '.join(supportedFlags))
Davide Pesaventoc58cc7f2017-08-08 16:51:28 -0500102 self.env.prepend_value('CXXFLAGS', supportedFlags)
Vince Lehman0a7da612014-10-29 14:39:29 -0500103
Davide Pesavento5110a8a2023-08-20 21:42:11 -0400104
Vince Lehman0a7da612014-10-29 14:39:29 -0500105@Configure.conf
106def add_supported_linkflags(self, linkflags):
107 """
Davide Pesavento5110a8a2023-08-20 21:42:11 -0400108 Check which linkflags are supported by the active compiler and add them to env.LINKFLAGS variable.
Vince Lehman0a7da612014-10-29 14:39:29 -0500109 """
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800110 if len(linkflags) == 0:
111 return
112
113 self.start_msg('Checking supported LINKFLAGS')
Vince Lehman0a7da612014-10-29 14:39:29 -0500114
115 supportedFlags = []
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500116 for flags in linkflags:
117 flags = Utils.to_list(flags)
118 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
119 supportedFlags += flags
Vince Lehman0a7da612014-10-29 14:39:29 -0500120
121 self.end_msg(' '.join(supportedFlags))
Davide Pesaventoc58cc7f2017-08-08 16:51:28 -0500122 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800123
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -0400124
Davide Pesaventoeed32092023-08-20 21:45:54 -0400125class CompilerFlags:
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500126 def getCompilerVersion(self, conf):
127 return tuple(int(i) for i in conf.env.CC_VERSION)
128
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800129 def getGeneralFlags(self, conf):
130 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Davide Pesavento5849ee72023-11-12 20:00:21 -0500131 return {
132 'CXXFLAGS': [],
133 'LINKFLAGS': [],
134 'DEFINES': ['BOOST_ASIO_NO_DEPRECATED', 'BOOST_FILESYSTEM_NO_DEPRECATED'],
135 }
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800136
137 def getDebugFlags(self, conf):
138 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Davide Pesaventoda43c612024-01-19 16:44:25 -0500139 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
Alexander Afanasyevf9f39102015-12-01 17:43:40 -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 Pesavento5110a8a2023-08-20 21:42:11 -0400145
Davide Pesaventoeed32092023-08-20 21:45:54 -0400146class GccClangCommonFlags(CompilerFlags):
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800147 """
Davide Pesaventoeed32092023-08-20 21:45:54 -0400148 This class defines common flags that work for both gcc and clang compilers.
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800149 """
Davide Pesaventoeed32092023-08-20 21:45:54 -0400150
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -0400151 def getGeneralFlags(self, conf):
Davide Pesaventoeed32092023-08-20 21:45:54 -0400152 flags = super().getGeneralFlags(conf)
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400153 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesavento65ee9922022-11-16 00:21:43 -0500154 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesaventoe5569912019-01-29 19:39:06 -0500155 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -0400156 return flags
157
Davide Pesaventoeed32092023-08-20 21:45:54 -0400158 __cxxFlags = [
159 '-fdiagnostics-color',
160 '-Wall',
161 '-Wextra',
162 '-Wpedantic',
163 '-Wenum-conversion',
164 '-Wextra-semi',
Davide Pesaventoeed32092023-08-20 21:45:54 -0400165 '-Wno-unused-parameter',
166 ]
167 __linkFlags = ['-Wl,-O1']
168
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800169 def getDebugFlags(self, conf):
Davide Pesaventoeed32092023-08-20 21:45:54 -0400170 flags = super().getDebugFlags(conf)
171 flags['CXXFLAGS'] += ['-Og', '-g'] + self.__cxxFlags + [
172 '-Werror',
173 '-Wno-error=deprecated-declarations', # Bug #3795
174 '-Wno-error=maybe-uninitialized', # Bug #1615
175 ]
176 flags['LINKFLAGS'] += self.__linkFlags
Davide Pesaventoda43c612024-01-19 16:44:25 -0500177 # Enable assertions in libstdc++
178 # https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
179 flags['DEFINES'] += ['_GLIBCXX_ASSERTIONS=1']
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800180 return flags
181
182 def getOptimizedFlags(self, conf):
Davide Pesaventoeed32092023-08-20 21:45:54 -0400183 flags = super().getOptimizedFlags(conf)
184 flags['CXXFLAGS'] += ['-O2', '-g1'] + self.__cxxFlags
185 flags['LINKFLAGS'] += self.__linkFlags
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800186 return flags
187
Davide Pesavento5110a8a2023-08-20 21:42:11 -0400188
Davide Pesaventoeed32092023-08-20 21:45:54 -0400189class GccFlags(GccClangCommonFlags):
190 __cxxFlags = [
191 '-Wcatch-value=2',
192 '-Wcomma-subscript', # enabled by default in C++20
193 '-Wduplicated-branches',
194 '-Wduplicated-cond',
195 '-Wlogical-op',
196 '-Wredundant-tags',
197 '-Wvolatile', # enabled by default in C++20
198 ]
199
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800200 def getDebugFlags(self, conf):
Davide Pesaventoeed32092023-08-20 21:45:54 -0400201 flags = super().getDebugFlags(conf)
202 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500203 if platform.machine() == 'armv7l':
Davide Pesavento7ae8b082021-10-12 21:45:47 -0400204 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800205 return flags
206
207 def getOptimizedFlags(self, conf):
Davide Pesaventoeed32092023-08-20 21:45:54 -0400208 flags = super().getOptimizedFlags(conf)
209 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500210 if platform.machine() == 'armv7l':
Davide Pesavento7ae8b082021-10-12 21:45:47 -0400211 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800212 return flags
213
Davide Pesavento5110a8a2023-08-20 21:42:11 -0400214
Davide Pesaventoeed32092023-08-20 21:45:54 -0400215class ClangFlags(GccClangCommonFlags):
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800216 def getGeneralFlags(self, conf):
Davide Pesaventoeed32092023-08-20 21:45:54 -0400217 flags = super().getGeneralFlags(conf)
Davide Pesavento7ae8b082021-10-12 21:45:47 -0400218 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500219 # Bug #4296
Davide Pesaventoede59632022-08-26 20:35:44 -0400220 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
Davide Pesaventoeed32092023-08-20 21:45:54 -0400221 flags['CXXFLAGS'] += [
222 ['-isystem', f'{brewdir}/include'], # for Homebrew
223 ['-isystem', '/opt/local/include'], # for MacPorts
224 ]
Davide Pesavento7ae8b082021-10-12 21:45:47 -0400225 elif Utils.unversioned_sys_platform() == 'freebsd':
226 # Bug #4790
227 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Davide Pesaventoda43c612024-01-19 16:44:25 -0500228 if self.getCompilerVersion(conf) >= (18, 0, 0):
229 # Bug #5300
230 flags['CXXFLAGS'] += ['-Wno-enum-constexpr-conversion']
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800231 return flags
232
Davide Pesaventoeed32092023-08-20 21:45:54 -0400233 __cxxFlags = [
234 '-Wundefined-func-template',
235 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
236 ]
237
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800238 def getDebugFlags(self, conf):
Davide Pesaventoeed32092023-08-20 21:45:54 -0400239 flags = super().getDebugFlags(conf)
240 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventoda43c612024-01-19 16:44:25 -0500241 # Enable assertions in libc++
242 if self.getCompilerVersion(conf) >= (18, 0, 0):
243 # https://libcxx.llvm.org/Hardening.html
244 flags['DEFINES'] += ['_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE']
245 elif self.getCompilerVersion(conf) >= (15, 0, 0):
246 # https://releases.llvm.org/15.0.0/projects/libcxx/docs/UsingLibcxx.html#enabling-the-safe-libc-mode
247 flags['DEFINES'] += ['_LIBCPP_ENABLE_ASSERTIONS=1']
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800248 return flags
249
250 def getOptimizedFlags(self, conf):
Davide Pesaventoeed32092023-08-20 21:45:54 -0400251 flags = super().getOptimizedFlags(conf)
252 flags['CXXFLAGS'] += self.__cxxFlags
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800253 return flags