blob: bac1893d5ee2c47a93624098bd61ddc9a55932fd [file] [log] [blame]
Davide Pesavento5f408ae2020-07-15 21:17:04 -04001import platform
Alexander Afanasyev40491df2018-03-09 16:29:52 -05002from waflib import Configure, Logs, Utils
Alexander Afanasyev7eb59112014-07-02 14:21:11 -07003
Davide Pesavento97741e72023-08-12 16:36:07 -04004
Alexander Afanasyev7eb59112014-07-02 14:21:11 -07005def options(opt):
Alexander Afanasyev40491df2018-03-09 16:29:52 -05006 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesavento5f408ae2020-07-15 21:17:04 -04007 help='Compile in debugging mode with minimal optimizations (-Og)')
Alexander Afanasyev7eb59112014-07-02 14:21:11 -07008
Davide Pesavento97741e72023-08-12 16:36:07 -04009
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070010def configure(conf):
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040011 conf.start_msg('Checking C++ compiler version')
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070012
Alexander Afanasyev40491df2018-03-09 16:29:52 -050013 cxx = conf.env.CXX_NAME # generic name of the compiler
Davide Pesavento2e56ec02025-03-25 00:32:31 -040014 ccver = get_compiler_ver(conf)
Alexander Afanasyev40491df2018-03-09 16:29:52 -050015 ccverstr = '.'.join(conf.env.CC_VERSION)
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040016 errmsg = ''
17 warnmsg = ''
18 if cxx == 'gcc':
Davide Pesavento6c4bb212024-12-14 15:30:21 -050019 if ccver < (9, 1, 0):
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040020 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesavento6c4bb212024-12-14 15:30:21 -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 Pesaventoed778702023-04-26 15:31:31 -040024 'officially supported and may result in build failures.')
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040025 conf.flags = GccFlags()
26 elif cxx == 'clang':
Davide Pesavento26a03262022-03-07 13:37:34 -050027 if Utils.unversioned_sys_platform() == 'darwin':
Davide Pesavento6c4bb212024-12-14 15:30:21 -050028 if ccver < (11, 0, 0):
Davide Pesavento26a03262022-03-07 13:37:34 -050029 errmsg = ('The version of Xcode you are using is too old.\n'
Davide Pesavento27b70922024-07-13 17:20:01 -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 Pesavento26a03262022-03-07 13:37:34 -050033 'officially supported and may result in build failures.')
Davide Pesaventoed778702023-04-26 15:31:31 -040034 elif ccver < (7, 0, 0):
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040035 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesavento6c4bb212024-12-14 15:30:21 -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 Afanasyev12d5faa2017-09-21 19:04:22 -040040 conf.flags = ClangFlags()
41 else:
Davide Pesavento15a74422022-08-19 15:48:45 -040042 warnmsg = f'{cxx} compiler is unsupported'
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040043 conf.flags = CompilerFlags()
44
45 if errmsg:
Alexander Afanasyev40491df2018-03-09 16:29:52 -050046 conf.end_msg(ccverstr, color='RED')
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040047 conf.fatal(errmsg)
48 elif warnmsg:
Alexander Afanasyev40491df2018-03-09 16:29:52 -050049 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento5f408ae2020-07-15 21:17:04 -040050 Logs.warn('WARNING: ' + warnmsg)
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040051 else:
Alexander Afanasyev40491df2018-03-09 16:29:52 -050052 conf.end_msg(ccverstr)
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040053
Davide Pesavento97741e72023-08-12 16:36:07 -040054 conf.areCustomCxxflagsPresent = len(conf.env.CXXFLAGS) > 0
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -080055
Davide Pesavento4a9395b2018-05-24 00:23:22 -040056 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040057 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -080058 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
59 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
60 conf.env.DEFINES += generalFlags['DEFINES']
61
Davide Pesavento97741e72023-08-12 16:36:07 -040062
Davide Pesavento2e56ec02025-03-25 00:32:31 -040063def get_compiler_ver(conf):
64 return tuple(int(i) for i in conf.env.CC_VERSION)
65
66
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040067@Configure.conf
68def check_compiler_flags(conf):
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -080069 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
70 # corresponding environment variables are not set.
71 # DEFINES are always applied.
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070072 if conf.options.debug:
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040073 extraFlags = conf.flags.getDebugFlags(conf)
74 if conf.areCustomCxxflagsPresent:
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -080075 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev40491df2018-03-09 16:29:52 -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 Afanasyev7eb59112014-07-02 14:21:11 -070080 else:
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040081 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070082
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040083 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -080084 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
85 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
86
87 conf.env.DEFINES += extraFlags['DEFINES']
Yingdi Yu906c2ea2014-10-31 11:24:50 -070088
Davide Pesavento97741e72023-08-12 16:36:07 -040089
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070090@Configure.conf
91def add_supported_cxxflags(self, cxxflags):
92 """
Davide Pesavento97741e72023-08-12 16:36:07 -040093 Check which cxxflags are supported by the active compiler and add them to env.CXXFLAGS variable.
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070094 """
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -080095 if len(cxxflags) == 0:
96 return
97
Yingdi Yu906c2ea2014-10-31 11:24:50 -070098 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070099
100 supportedFlags = []
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -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 Afanasyev7eb59112014-07-02 14:21:11 -0700105
106 self.end_msg(' '.join(supportedFlags))
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800107 self.env.prepend_value('CXXFLAGS', supportedFlags)
Yingdi Yu906c2ea2014-10-31 11:24:50 -0700108
Davide Pesavento97741e72023-08-12 16:36:07 -0400109
Yingdi Yu906c2ea2014-10-31 11:24:50 -0700110@Configure.conf
111def add_supported_linkflags(self, linkflags):
112 """
Davide Pesavento97741e72023-08-12 16:36:07 -0400113 Check which linkflags are supported by the active compiler and add them to env.LINKFLAGS variable.
Yingdi Yu906c2ea2014-10-31 11:24:50 -0700114 """
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800115 if len(linkflags) == 0:
116 return
117
Yingdi Yu906c2ea2014-10-31 11:24:50 -0700118 self.start_msg('Checking supported LINKFLAGS')
119
120 supportedFlags = []
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -0400121 for flags in linkflags:
122 flags = Utils.to_list(flags)
123 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
124 supportedFlags += flags
Yingdi Yu906c2ea2014-10-31 11:24:50 -0700125
126 self.end_msg(' '.join(supportedFlags))
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800127 self.env.prepend_value('LINKFLAGS', supportedFlags)
128
129
Davide Pesavento97741e72023-08-12 16:36:07 -0400130class CompilerFlags:
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800131 def getGeneralFlags(self, conf):
132 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Davide Pesavento7558ed22024-03-13 18:28:07 -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 Pesaventod45f8882023-11-11 17:32:23 -0500137 return {
138 'CXXFLAGS': [],
139 'LINKFLAGS': [],
Davide Pesavento39026112024-12-14 15:45:05 -0500140 'DEFINES': ['BOOST_ASIO_NO_DEPRECATED'],
Davide Pesaventod45f8882023-11-11 17:32:23 -0500141 }
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800142
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -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 Pesavento97741e72023-08-12 16:36:07 -0400147
148class GccClangCommonFlags(CompilerFlags):
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800149 """
Davide Pesavento97741e72023-08-12 16:36:07 -0400150 This class defines common flags that work for both gcc and clang compilers.
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800151 """
Davide Pesavento97741e72023-08-12 16:36:07 -0400152
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -0400153 def getGeneralFlags(self, conf):
Davide Pesavento97741e72023-08-12 16:36:07 -0400154 flags = super().getGeneralFlags(conf)
Davide Pesavento8663ed12022-07-23 03:04:27 -0400155 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesaventocda44892022-11-15 13:40:33 -0500156 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesaventofae9def2019-01-29 14:34:33 -0500157 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -0400158 return flags
159
Davide Pesavento97741e72023-08-12 16:36:07 -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 Afanasyevf3192eb2016-12-19 17:11:20 -0800172 def getDebugFlags(self, conf):
Davide Pesavento97741e72023-08-12 16:36:07 -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 Pesavento11c36382024-01-19 00:23:02 -0500180 # Enable assertions in libstdc++
181 # https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
182 flags['DEFINES'] += ['_GLIBCXX_ASSERTIONS=1']
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800183 return flags
184
185 def getOptimizedFlags(self, conf):
Davide Pesavento97741e72023-08-12 16:36:07 -0400186 flags = super().getOptimizedFlags(conf)
187 flags['CXXFLAGS'] += ['-O2', '-g1'] + self.__cxxFlags
188 flags['LINKFLAGS'] += self.__linkFlags
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800189 return flags
190
Davide Pesavento97741e72023-08-12 16:36:07 -0400191
192class 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 Afanasyevf3192eb2016-12-19 17:11:20 -0800203 def getDebugFlags(self, conf):
Davide Pesavento97741e72023-08-12 16:36:07 -0400204 flags = super().getDebugFlags(conf)
205 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento26a03262022-03-07 13:37:34 -0500206 if platform.machine() == 'armv7l':
Davide Pesavento5f408ae2020-07-15 21:17:04 -0400207 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800208 return flags
209
210 def getOptimizedFlags(self, conf):
Davide Pesavento97741e72023-08-12 16:36:07 -0400211 flags = super().getOptimizedFlags(conf)
212 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento26a03262022-03-07 13:37:34 -0500213 if platform.machine() == 'armv7l':
Davide Pesavento5f408ae2020-07-15 21:17:04 -0400214 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800215 return flags
216
Davide Pesavento97741e72023-08-12 16:36:07 -0400217
218class ClangFlags(GccClangCommonFlags):
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800219 def getGeneralFlags(self, conf):
Davide Pesavento97741e72023-08-12 16:36:07 -0400220 flags = super().getGeneralFlags(conf)
Davide Pesavento5f408ae2020-07-15 21:17:04 -0400221 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev40491df2018-03-09 16:29:52 -0500222 # Bug #4296
Davide Pesavento15a74422022-08-19 15:48:45 -0400223 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
Davide Pesavento97741e72023-08-12 16:36:07 -0400224 flags['CXXFLAGS'] += [
225 ['-isystem', f'{brewdir}/include'], # for Homebrew
226 ['-isystem', '/opt/local/include'], # for MacPorts
227 ]
Davide Pesavento5f408ae2020-07-15 21:17:04 -0400228 elif Utils.unversioned_sys_platform() == 'freebsd':
229 # Bug #4790
230 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Davide Pesavento2e56ec02025-03-25 00:32:31 -0400231 if get_compiler_ver(conf) >= (18, 0, 0) and get_compiler_ver(conf) < (20, 1, 0):
Davide Pesavento11c36382024-01-19 00:23:02 -0500232 # Bug #5300
233 flags['CXXFLAGS'] += ['-Wno-enum-constexpr-conversion']
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800234 return flags
235
Davide Pesavento97741e72023-08-12 16:36:07 -0400236 __cxxFlags = [
237 '-Wundefined-func-template',
238 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
239 ]
240
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800241 def getDebugFlags(self, conf):
Davide Pesavento97741e72023-08-12 16:36:07 -0400242 flags = super().getDebugFlags(conf)
243 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento11c36382024-01-19 00:23:02 -0500244 # Enable assertions in libc++
Davide Pesavento2e56ec02025-03-25 00:32:31 -0400245 if get_compiler_ver(conf) >= (18, 0, 0):
Davide Pesavento11c36382024-01-19 00:23:02 -0500246 # https://libcxx.llvm.org/Hardening.html
247 flags['DEFINES'] += ['_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE']
Davide Pesavento2e56ec02025-03-25 00:32:31 -0400248 elif get_compiler_ver(conf) >= (15, 0, 0):
Davide Pesavento11c36382024-01-19 00:23:02 -0500249 # https://releases.llvm.org/15.0.0/projects/libcxx/docs/UsingLibcxx.html#enabling-the-safe-libc-mode
250 flags['DEFINES'] += ['_LIBCPP_ENABLE_ASSERTIONS=1']
Davide Pesavento7558ed22024-03-13 18:28:07 -0400251 # Tell libc++ to avoid including transitive headers
252 # https://libcxx.llvm.org/DesignDocs/HeaderRemovalPolicy.html
253 flags['DEFINES'] += ['_LIBCPP_REMOVE_TRANSITIVE_INCLUDES=1']
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800254 return flags
255
256 def getOptimizedFlags(self, conf):
Davide Pesavento97741e72023-08-12 16:36:07 -0400257 flags = super().getOptimizedFlags(conf)
258 flags['CXXFLAGS'] += self.__cxxFlags
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800259 return flags