blob: 9899d05b0b2c38eca520c67024e9816b5509a51f [file] [log] [blame]
Davide Pesavento7ae8b082021-10-12 21:45:47 -04001import platform
Alexander Afanasyev67758b12018-03-06 18:36:44 -05002from waflib import Configure, Logs, Utils
Yingdi Yu40cd1c32014-04-17 15:02:17 -07003
Davide Pesavento5110a8a2023-08-20 21:42:11 -04004
Yingdi Yu40cd1c32014-04-17 15:02:17 -07005def options(opt):
Alexander Afanasyev67758b12018-03-06 18:36:44 -05006 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesavento7ae8b082021-10-12 21:45:47 -04007 help='Compile in debugging mode with minimal optimizations (-Og)')
Yingdi Yu40cd1c32014-04-17 15:02:17 -07008
Davide Pesavento5110a8a2023-08-20 21:42:11 -04009
Yingdi Yu40cd1c32014-04-17 15:02:17 -070010def configure(conf):
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -040011 conf.start_msg('Checking C++ compiler version')
12
Alexander Afanasyev67758b12018-03-06 18:36:44 -050013 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 Pesavento6ebfd7c2017-08-26 17:52:54 -040016 errmsg = ''
17 warnmsg = ''
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080018 if cxx == 'gcc':
Davide Pesaventod04267f2024-12-15 13:48:42 -050019 if ccver < (9, 1, 0):
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -040020 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesaventod04267f2024-12-15 13:48:42 -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 Pesavento5110a8a2023-08-20 21:42:11 -040024 'officially supported and may result in build failures.')
Alexander Afanasyev67758b12018-03-06 18:36:44 -050025 conf.flags = GccFlags()
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080026 elif cxx == 'clang':
Davide Pesaventod1f1df82022-03-12 16:40:37 -050027 if Utils.unversioned_sys_platform() == 'darwin':
Davide Pesaventod04267f2024-12-15 13:48:42 -050028 if ccver < (11, 0, 0):
Davide Pesaventod1f1df82022-03-12 16:40:37 -050029 errmsg = ('The version of Xcode you are using is too old.\n'
Davide Pesaventod96b63a2024-06-08 20:18:05 -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 Pesaventod1f1df82022-03-12 16:40:37 -050033 'officially supported and may result in build failures.')
Davide Pesavento5110a8a2023-08-20 21:42:11 -040034 elif ccver < (7, 0, 0):
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -040035 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesaventod04267f2024-12-15 13:48:42 -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 Afanasyev67758b12018-03-06 18:36:44 -050040 conf.flags = ClangFlags()
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080041 else:
Davide Pesaventoede59632022-08-26 20:35:44 -040042 warnmsg = f'{cxx} compiler is unsupported'
Alexander Afanasyev67758b12018-03-06 18:36:44 -050043 conf.flags = CompilerFlags()
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -040044
45 if errmsg:
Alexander Afanasyev67758b12018-03-06 18:36:44 -050046 conf.end_msg(ccverstr, color='RED')
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -040047 conf.fatal(errmsg)
48 elif warnmsg:
Alexander Afanasyev67758b12018-03-06 18:36:44 -050049 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento7ae8b082021-10-12 21:45:47 -040050 Logs.warn('WARNING: ' + warnmsg)
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -040051 else:
Alexander Afanasyev67758b12018-03-06 18:36:44 -050052 conf.end_msg(ccverstr)
Yingdi Yu40cd1c32014-04-17 15:02:17 -070053
Davide Pesavento5110a8a2023-08-20 21:42:11 -040054 conf.areCustomCxxflagsPresent = len(conf.env.CXXFLAGS) > 0
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080055
Davide Pesaventoa08dc3f2018-05-24 00:40:28 -040056 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyev67758b12018-03-06 18:36:44 -050057 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080058 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
59 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
60 conf.env.DEFINES += generalFlags['DEFINES']
61
Davide Pesavento5110a8a2023-08-20 21:42:11 -040062
Alexander Afanasyev67758b12018-03-06 18:36:44 -050063@Configure.conf
64def check_compiler_flags(conf):
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080065 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
66 # corresponding environment variables are not set.
67 # DEFINES are always applied.
Yingdi Yu40cd1c32014-04-17 15:02:17 -070068 if conf.options.debug:
Alexander Afanasyev67758b12018-03-06 18:36:44 -050069 extraFlags = conf.flags.getDebugFlags(conf)
70 if conf.areCustomCxxflagsPresent:
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080071 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev67758b12018-03-06 18:36:44 -050072 if missingFlags:
73 Logs.warn('Selected debug mode, but CXXFLAGS is set to a custom value "%s"'
74 % ' '.join(conf.env.CXXFLAGS))
75 Logs.warn('Default flags "%s" will not be used' % ' '.join(missingFlags))
Yingdi Yu40cd1c32014-04-17 15:02:17 -070076 else:
Alexander Afanasyev67758b12018-03-06 18:36:44 -050077 extraFlags = conf.flags.getOptimizedFlags(conf)
Yingdi Yu40cd1c32014-04-17 15:02:17 -070078
Alexander Afanasyev67758b12018-03-06 18:36:44 -050079 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080080 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
81 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
82
83 conf.env.DEFINES += extraFlags['DEFINES']
Vince Lehman0a7da612014-10-29 14:39:29 -050084
Davide Pesavento5110a8a2023-08-20 21:42:11 -040085
Yingdi Yu40cd1c32014-04-17 15:02:17 -070086@Configure.conf
87def add_supported_cxxflags(self, cxxflags):
88 """
Davide Pesavento5110a8a2023-08-20 21:42:11 -040089 Check which cxxflags are supported by the active compiler and add them to env.CXXFLAGS variable.
Yingdi Yu40cd1c32014-04-17 15:02:17 -070090 """
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080091 if len(cxxflags) == 0:
92 return
93
94 self.start_msg('Checking supported CXXFLAGS')
Yingdi Yu40cd1c32014-04-17 15:02:17 -070095
96 supportedFlags = []
Alexander Afanasyev67758b12018-03-06 18:36:44 -050097 for flags in cxxflags:
98 flags = Utils.to_list(flags)
99 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
100 supportedFlags += flags
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700101
102 self.end_msg(' '.join(supportedFlags))
Davide Pesaventoc58cc7f2017-08-08 16:51:28 -0500103 self.env.prepend_value('CXXFLAGS', supportedFlags)
Vince Lehman0a7da612014-10-29 14:39:29 -0500104
Davide Pesavento5110a8a2023-08-20 21:42:11 -0400105
Vince Lehman0a7da612014-10-29 14:39:29 -0500106@Configure.conf
107def add_supported_linkflags(self, linkflags):
108 """
Davide Pesavento5110a8a2023-08-20 21:42:11 -0400109 Check which linkflags are supported by the active compiler and add them to env.LINKFLAGS variable.
Vince Lehman0a7da612014-10-29 14:39:29 -0500110 """
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800111 if len(linkflags) == 0:
112 return
113
114 self.start_msg('Checking supported LINKFLAGS')
Vince Lehman0a7da612014-10-29 14:39:29 -0500115
116 supportedFlags = []
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500117 for flags in linkflags:
118 flags = Utils.to_list(flags)
119 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
120 supportedFlags += flags
Vince Lehman0a7da612014-10-29 14:39:29 -0500121
122 self.end_msg(' '.join(supportedFlags))
Davide Pesaventoc58cc7f2017-08-08 16:51:28 -0500123 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800124
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -0400125
Davide Pesaventoeed32092023-08-20 21:45:54 -0400126class CompilerFlags:
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500127 def getCompilerVersion(self, conf):
128 return tuple(int(i) for i in conf.env.CC_VERSION)
129
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800130 def getGeneralFlags(self, conf):
131 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Davide Pesavento5a1d77a2024-03-13 19:24:11 -0400132 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
133
134 def getDebugFlags(self, conf):
135 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Davide Pesavento5849ee72023-11-12 20:00:21 -0500136 return {
137 'CXXFLAGS': [],
138 'LINKFLAGS': [],
139 'DEFINES': ['BOOST_ASIO_NO_DEPRECATED', 'BOOST_FILESYSTEM_NO_DEPRECATED'],
140 }
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800141
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800142 def getOptimizedFlags(self, conf):
143 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
144 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
145
Davide Pesavento5110a8a2023-08-20 21:42:11 -0400146
Davide Pesaventoeed32092023-08-20 21:45:54 -0400147class GccClangCommonFlags(CompilerFlags):
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800148 """
Davide Pesaventoeed32092023-08-20 21:45:54 -0400149 This class defines common flags that work for both gcc and clang compilers.
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800150 """
Davide Pesaventoeed32092023-08-20 21:45:54 -0400151
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -0400152 def getGeneralFlags(self, conf):
Davide Pesaventoeed32092023-08-20 21:45:54 -0400153 flags = super().getGeneralFlags(conf)
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400154 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesavento65ee9922022-11-16 00:21:43 -0500155 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesaventoe5569912019-01-29 19:39:06 -0500156 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -0400157 return flags
158
Davide Pesaventoeed32092023-08-20 21:45:54 -0400159 __cxxFlags = [
160 '-fdiagnostics-color',
161 '-Wall',
162 '-Wextra',
163 '-Wpedantic',
164 '-Wenum-conversion',
165 '-Wextra-semi',
Davide Pesaventoeed32092023-08-20 21:45:54 -0400166 '-Wno-unused-parameter',
167 ]
168 __linkFlags = ['-Wl,-O1']
169
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800170 def getDebugFlags(self, conf):
Davide Pesaventoeed32092023-08-20 21:45:54 -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 Pesaventoda43c612024-01-19 16:44:25 -0500178 # Enable assertions in libstdc++
179 # https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
180 flags['DEFINES'] += ['_GLIBCXX_ASSERTIONS=1']
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800181 return flags
182
183 def getOptimizedFlags(self, conf):
Davide Pesaventoeed32092023-08-20 21:45:54 -0400184 flags = super().getOptimizedFlags(conf)
185 flags['CXXFLAGS'] += ['-O2', '-g1'] + self.__cxxFlags
186 flags['LINKFLAGS'] += self.__linkFlags
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800187 return flags
188
Davide Pesavento5110a8a2023-08-20 21:42:11 -0400189
Davide Pesaventoeed32092023-08-20 21:45:54 -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 Afanasyevf9f39102015-12-01 17:43:40 -0800201 def getDebugFlags(self, conf):
Davide Pesaventoeed32092023-08-20 21:45:54 -0400202 flags = super().getDebugFlags(conf)
203 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500204 if platform.machine() == 'armv7l':
Davide Pesavento7ae8b082021-10-12 21:45:47 -0400205 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800206 return flags
207
208 def getOptimizedFlags(self, conf):
Davide Pesaventoeed32092023-08-20 21:45:54 -0400209 flags = super().getOptimizedFlags(conf)
210 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500211 if platform.machine() == 'armv7l':
Davide Pesavento7ae8b082021-10-12 21:45:47 -0400212 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800213 return flags
214
Davide Pesavento5110a8a2023-08-20 21:42:11 -0400215
Davide Pesaventoeed32092023-08-20 21:45:54 -0400216class ClangFlags(GccClangCommonFlags):
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800217 def getGeneralFlags(self, conf):
Davide Pesaventoeed32092023-08-20 21:45:54 -0400218 flags = super().getGeneralFlags(conf)
Davide Pesavento7ae8b082021-10-12 21:45:47 -0400219 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500220 # Bug #4296
Davide Pesaventoede59632022-08-26 20:35:44 -0400221 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
Davide Pesaventoeed32092023-08-20 21:45:54 -0400222 flags['CXXFLAGS'] += [
223 ['-isystem', f'{brewdir}/include'], # for Homebrew
224 ['-isystem', '/opt/local/include'], # for MacPorts
225 ]
Davide Pesavento7ae8b082021-10-12 21:45:47 -0400226 elif Utils.unversioned_sys_platform() == 'freebsd':
227 # Bug #4790
228 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Davide Pesaventoda43c612024-01-19 16:44:25 -0500229 if self.getCompilerVersion(conf) >= (18, 0, 0):
230 # Bug #5300
231 flags['CXXFLAGS'] += ['-Wno-enum-constexpr-conversion']
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800232 return flags
233
Davide Pesaventoeed32092023-08-20 21:45:54 -0400234 __cxxFlags = [
235 '-Wundefined-func-template',
236 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
237 ]
238
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800239 def getDebugFlags(self, conf):
Davide Pesaventoeed32092023-08-20 21:45:54 -0400240 flags = super().getDebugFlags(conf)
241 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventoda43c612024-01-19 16:44:25 -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 Pesavento5a1d77a2024-03-13 19:24:11 -0400249 # Tell libc++ to avoid including transitive headers
250 # https://libcxx.llvm.org/DesignDocs/HeaderRemovalPolicy.html
251 flags['DEFINES'] += ['_LIBCPP_REMOVE_TRANSITIVE_INCLUDES=1']
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800252 return flags
253
254 def getOptimizedFlags(self, conf):
Davide Pesaventoeed32092023-08-20 21:45:54 -0400255 flags = super().getOptimizedFlags(conf)
256 flags['CXXFLAGS'] += self.__cxxFlags
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800257 return flags