blob: ac5cdfdcd9f1a9d0068c13b0537022c7c0770a6e [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
Davide Pesaventofa9e4342025-03-25 13:40:11 -040014 ccver = get_compiler_ver(conf)
Alexander Afanasyev67758b12018-03-06 18:36:44 -050015 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
Davide Pesaventofa9e4342025-03-25 13:40:11 -040063def get_compiler_ver(conf):
64 return tuple(int(i) for i in conf.env.CC_VERSION)
65
66
Alexander Afanasyev67758b12018-03-06 18:36:44 -050067@Configure.conf
68def check_compiler_flags(conf):
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080069 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
70 # corresponding environment variables are not set.
71 # DEFINES are always applied.
Yingdi Yu40cd1c32014-04-17 15:02:17 -070072 if conf.options.debug:
Alexander Afanasyev67758b12018-03-06 18:36:44 -050073 extraFlags = conf.flags.getDebugFlags(conf)
74 if conf.areCustomCxxflagsPresent:
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080075 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev67758b12018-03-06 18:36:44 -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))
Yingdi Yu40cd1c32014-04-17 15:02:17 -070080 else:
Alexander Afanasyev67758b12018-03-06 18:36:44 -050081 extraFlags = conf.flags.getOptimizedFlags(conf)
Yingdi Yu40cd1c32014-04-17 15:02:17 -070082
Alexander Afanasyev67758b12018-03-06 18:36:44 -050083 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080084 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
85 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
86
87 conf.env.DEFINES += extraFlags['DEFINES']
Vince Lehman0a7da612014-10-29 14:39:29 -050088
Davide Pesavento5110a8a2023-08-20 21:42:11 -040089
Yingdi Yu40cd1c32014-04-17 15:02:17 -070090@Configure.conf
91def add_supported_cxxflags(self, cxxflags):
92 """
Davide Pesavento5110a8a2023-08-20 21:42:11 -040093 Check which cxxflags are supported by the active compiler and add them to env.CXXFLAGS variable.
Yingdi Yu40cd1c32014-04-17 15:02:17 -070094 """
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080095 if len(cxxflags) == 0:
96 return
97
98 self.start_msg('Checking supported CXXFLAGS')
Yingdi Yu40cd1c32014-04-17 15:02:17 -070099
100 supportedFlags = []
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500101 for flags in cxxflags:
102 flags = Utils.to_list(flags)
103 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
104 supportedFlags += flags
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700105
106 self.end_msg(' '.join(supportedFlags))
Davide Pesaventoc58cc7f2017-08-08 16:51:28 -0500107 self.env.prepend_value('CXXFLAGS', supportedFlags)
Vince Lehman0a7da612014-10-29 14:39:29 -0500108
Davide Pesavento5110a8a2023-08-20 21:42:11 -0400109
Vince Lehman0a7da612014-10-29 14:39:29 -0500110@Configure.conf
111def add_supported_linkflags(self, linkflags):
112 """
Davide Pesavento5110a8a2023-08-20 21:42:11 -0400113 Check which linkflags are supported by the active compiler and add them to env.LINKFLAGS variable.
Vince Lehman0a7da612014-10-29 14:39:29 -0500114 """
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800115 if len(linkflags) == 0:
116 return
117
118 self.start_msg('Checking supported LINKFLAGS')
Vince Lehman0a7da612014-10-29 14:39:29 -0500119
120 supportedFlags = []
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500121 for flags in linkflags:
122 flags = Utils.to_list(flags)
123 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
124 supportedFlags += flags
Vince Lehman0a7da612014-10-29 14:39:29 -0500125
126 self.end_msg(' '.join(supportedFlags))
Davide Pesaventoc58cc7f2017-08-08 16:51:28 -0500127 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800128
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -0400129
Davide Pesaventoeed32092023-08-20 21:45:54 -0400130class CompilerFlags:
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800131 def getGeneralFlags(self, conf):
132 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Davide Pesavento5a1d77a2024-03-13 19:24:11 -0400133 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 Pesavento5849ee72023-11-12 20:00:21 -0500137 return {
138 'CXXFLAGS': [],
139 'LINKFLAGS': [],
Davide Pesaventob0716542024-12-16 19:12:11 -0500140 'DEFINES': ['BOOST_ASIO_NO_DEPRECATED'],
Davide Pesavento5849ee72023-11-12 20:00:21 -0500141 }
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800142
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800143 def getOptimizedFlags(self, conf):
144 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
145 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
146
Davide Pesavento5110a8a2023-08-20 21:42:11 -0400147
Davide Pesaventoeed32092023-08-20 21:45:54 -0400148class GccClangCommonFlags(CompilerFlags):
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800149 """
Davide Pesaventoeed32092023-08-20 21:45:54 -0400150 This class defines common flags that work for both gcc and clang compilers.
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800151 """
Davide Pesaventoeed32092023-08-20 21:45:54 -0400152
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -0400153 def getGeneralFlags(self, conf):
Davide Pesaventoeed32092023-08-20 21:45:54 -0400154 flags = super().getGeneralFlags(conf)
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400155 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesavento65ee9922022-11-16 00:21:43 -0500156 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesaventoe5569912019-01-29 19:39:06 -0500157 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -0400158 return flags
159
Davide Pesaventoeed32092023-08-20 21:45:54 -0400160 __cxxFlags = [
161 '-fdiagnostics-color',
162 '-Wall',
163 '-Wextra',
164 '-Wpedantic',
165 '-Wenum-conversion',
166 '-Wextra-semi',
Davide Pesaventoeed32092023-08-20 21:45:54 -0400167 '-Wno-unused-parameter',
168 ]
169 __linkFlags = ['-Wl,-O1']
170
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800171 def getDebugFlags(self, conf):
Davide Pesaventoeed32092023-08-20 21:45:54 -0400172 flags = super().getDebugFlags(conf)
173 flags['CXXFLAGS'] += ['-Og', '-g'] + self.__cxxFlags + [
174 '-Werror',
175 '-Wno-error=deprecated-declarations', # Bug #3795
176 '-Wno-error=maybe-uninitialized', # Bug #1615
177 ]
178 flags['LINKFLAGS'] += self.__linkFlags
Davide Pesaventoda43c612024-01-19 16:44:25 -0500179 # Enable assertions in libstdc++
180 # https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
181 flags['DEFINES'] += ['_GLIBCXX_ASSERTIONS=1']
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800182 return flags
183
184 def getOptimizedFlags(self, conf):
Davide Pesaventoeed32092023-08-20 21:45:54 -0400185 flags = super().getOptimizedFlags(conf)
186 flags['CXXFLAGS'] += ['-O2', '-g1'] + self.__cxxFlags
187 flags['LINKFLAGS'] += self.__linkFlags
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800188 return flags
189
Davide Pesavento5110a8a2023-08-20 21:42:11 -0400190
Davide Pesaventoeed32092023-08-20 21:45:54 -0400191class GccFlags(GccClangCommonFlags):
192 __cxxFlags = [
193 '-Wcatch-value=2',
194 '-Wcomma-subscript', # enabled by default in C++20
195 '-Wduplicated-branches',
196 '-Wduplicated-cond',
197 '-Wlogical-op',
198 '-Wredundant-tags',
199 '-Wvolatile', # enabled by default in C++20
200 ]
201
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800202 def getDebugFlags(self, conf):
Davide Pesaventoeed32092023-08-20 21:45:54 -0400203 flags = super().getDebugFlags(conf)
204 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500205 if platform.machine() == 'armv7l':
Davide Pesavento7ae8b082021-10-12 21:45:47 -0400206 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800207 return flags
208
209 def getOptimizedFlags(self, conf):
Davide Pesaventoeed32092023-08-20 21:45:54 -0400210 flags = super().getOptimizedFlags(conf)
211 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500212 if platform.machine() == 'armv7l':
Davide Pesavento7ae8b082021-10-12 21:45:47 -0400213 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800214 return flags
215
Davide Pesavento5110a8a2023-08-20 21:42:11 -0400216
Davide Pesaventoeed32092023-08-20 21:45:54 -0400217class ClangFlags(GccClangCommonFlags):
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800218 def getGeneralFlags(self, conf):
Davide Pesaventoeed32092023-08-20 21:45:54 -0400219 flags = super().getGeneralFlags(conf)
Davide Pesavento7ae8b082021-10-12 21:45:47 -0400220 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500221 # Bug #4296
Davide Pesaventoede59632022-08-26 20:35:44 -0400222 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
Davide Pesaventoeed32092023-08-20 21:45:54 -0400223 flags['CXXFLAGS'] += [
224 ['-isystem', f'{brewdir}/include'], # for Homebrew
225 ['-isystem', '/opt/local/include'], # for MacPorts
226 ]
Davide Pesaventoa8e2beb2025-04-03 18:05:11 -0400227 else:
228 if Utils.unversioned_sys_platform() == 'freebsd':
229 # Bug #4790
230 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
231 if get_compiler_ver(conf) >= (18, 0, 0) and get_compiler_ver(conf) < (20, 1, 0):
232 # Bug #5300
233 flags['CXXFLAGS'] += ['-Wno-enum-constexpr-conversion']
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800234 return flags
235
Davide Pesaventoeed32092023-08-20 21:45:54 -0400236 __cxxFlags = [
237 '-Wundefined-func-template',
238 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
239 ]
240
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800241 def getDebugFlags(self, conf):
Davide Pesaventoeed32092023-08-20 21:45:54 -0400242 flags = super().getDebugFlags(conf)
243 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventoa8e2beb2025-04-03 18:05:11 -0400244 ccver = get_compiler_ver(conf)
245 darwin = Utils.unversioned_sys_platform() == 'darwin'
Davide Pesaventoda43c612024-01-19 16:44:25 -0500246 # Enable assertions in libc++
Davide Pesaventoa8e2beb2025-04-03 18:05:11 -0400247 if (darwin and ccver >= (17, 0, 0)) or (not darwin and ccver >= (18, 0, 0)):
Davide Pesaventoda43c612024-01-19 16:44:25 -0500248 # https://libcxx.llvm.org/Hardening.html
249 flags['DEFINES'] += ['_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE']
Davide Pesaventoa8e2beb2025-04-03 18:05:11 -0400250 elif ccver >= (15, 0, 0):
Davide Pesaventoda43c612024-01-19 16:44:25 -0500251 # https://releases.llvm.org/15.0.0/projects/libcxx/docs/UsingLibcxx.html#enabling-the-safe-libc-mode
252 flags['DEFINES'] += ['_LIBCPP_ENABLE_ASSERTIONS=1']
Davide Pesavento5a1d77a2024-03-13 19:24:11 -0400253 # Tell libc++ to avoid including transitive headers
254 # https://libcxx.llvm.org/DesignDocs/HeaderRemovalPolicy.html
255 flags['DEFINES'] += ['_LIBCPP_REMOVE_TRANSITIVE_INCLUDES=1']
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800256 return flags
257
258 def getOptimizedFlags(self, conf):
Davide Pesaventoeed32092023-08-20 21:45:54 -0400259 flags = super().getOptimizedFlags(conf)
260 flags['CXXFLAGS'] += self.__cxxFlags
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800261 return flags