blob: 6ba0635e46f4dcc0860782d2b7afdafc918f4740 [file] [log] [blame]
Davide Pesavento17521592020-05-14 19:01:32 -04001import platform
Davide Pesavento0064c1d2018-03-03 18:43:53 -05002from waflib import Configure, Logs, Utils
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -07003
Davide Pesavento5b15e2f2023-08-07 16:35:35 -04004
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -07005def options(opt):
Davide Pesavento0064c1d2018-03-03 18:43:53 -05006 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesavento17521592020-05-14 19:01:32 -04007 help='Compile in debugging mode with minimal optimizations (-Og)')
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -07008
Davide Pesavento5b15e2f2023-08-07 16:35:35 -04009
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070010def configure(conf):
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040011 conf.start_msg('Checking C++ compiler version')
12
Davide Pesavento0064c1d2018-03-03 18:43:53 -050013 cxx = conf.env.CXX_NAME # generic name of the compiler
Davide Pesaventoe11fa9a2025-03-18 02:42:03 -040014 ccver = get_compiler_ver(conf)
Davide Pesavento0064c1d2018-03-03 18:43:53 -050015 ccverstr = '.'.join(conf.env.CC_VERSION)
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040016 errmsg = ''
17 warnmsg = ''
Alexander Afanasyev76818632015-01-26 21:30:45 -080018 if cxx == 'gcc':
Davide Pesavento2073dce2024-12-10 21:24:41 -050019 if ccver < (9, 1, 0):
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040020 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesavento2073dce2024-12-10 21:24:41 -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 Pesaventoc5e3e7a2023-03-07 21:25:27 -050024 'officially supported and may result in build failures.')
Alexander Afanasyevb5220702017-09-21 18:57:00 -040025 conf.flags = GccFlags()
Alexander Afanasyev76818632015-01-26 21:30:45 -080026 elif cxx == 'clang':
Davide Pesaventoc52cd5e2022-03-05 20:40:54 -050027 if Utils.unversioned_sys_platform() == 'darwin':
Davide Pesavento2073dce2024-12-10 21:24:41 -050028 if ccver < (11, 0, 0):
Davide Pesaventoc52cd5e2022-03-05 20:40:54 -050029 errmsg = ('The version of Xcode you are using is too old.\n'
Davide Pesavento4b7cffb2024-06-07 17:35:53 -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 Pesaventoc52cd5e2022-03-05 20:40:54 -050033 'officially supported and may result in build failures.')
Davide Pesaventoc5e3e7a2023-03-07 21:25:27 -050034 elif ccver < (7, 0, 0):
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040035 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesavento2073dce2024-12-10 21:24:41 -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 Afanasyevb5220702017-09-21 18:57:00 -040040 conf.flags = ClangFlags()
Alexander Afanasyev76818632015-01-26 21:30:45 -080041 else:
Davide Pesaventoe541d1b2022-08-17 15:10:32 -040042 warnmsg = f'{cxx} compiler is unsupported'
Alexander Afanasyevb5220702017-09-21 18:57:00 -040043 conf.flags = CompilerFlags()
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040044
45 if errmsg:
Davide Pesavento0064c1d2018-03-03 18:43:53 -050046 conf.end_msg(ccverstr, color='RED')
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040047 conf.fatal(errmsg)
48 elif warnmsg:
Davide Pesavento0064c1d2018-03-03 18:43:53 -050049 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento17521592020-05-14 19:01:32 -040050 Logs.warn('WARNING: ' + warnmsg)
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040051 else:
Davide Pesavento0064c1d2018-03-03 18:43:53 -050052 conf.end_msg(ccverstr)
Davide Pesavento1bdef282014-04-08 20:59:50 +020053
Davide Pesavento5b15e2f2023-08-07 16:35:35 -040054 conf.areCustomCxxflagsPresent = len(conf.env.CXXFLAGS) > 0
Alexander Afanasyev76818632015-01-26 21:30:45 -080055
Davide Pesaventoc0822fa2018-05-10 21:54:10 -040056 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyevb5220702017-09-21 18:57:00 -040057 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyev76818632015-01-26 21:30:45 -080058 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
59 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
60 conf.env.DEFINES += generalFlags['DEFINES']
61
Davide Pesavento5b15e2f2023-08-07 16:35:35 -040062
Davide Pesaventoe11fa9a2025-03-18 02:42:03 -040063def get_compiler_ver(conf):
64 return tuple(int(i) for i in conf.env.CC_VERSION)
65
66
Alexander Afanasyevb5220702017-09-21 18:57:00 -040067@Configure.conf
68def check_compiler_flags(conf):
Davide Pesaventof485d892015-10-02 02:00:54 +020069 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Alexander Afanasyev76818632015-01-26 21:30:45 -080070 # corresponding environment variables are not set.
Davide Pesaventof485d892015-10-02 02:00:54 +020071 # DEFINES are always applied.
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070072 if conf.options.debug:
Alexander Afanasyevb5220702017-09-21 18:57:00 -040073 extraFlags = conf.flags.getDebugFlags(conf)
74 if conf.areCustomCxxflagsPresent:
Alexander Afanasyev76818632015-01-26 21:30:45 -080075 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Davide Pesavento0064c1d2018-03-03 18:43:53 -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 Afanasyev97e4cac2014-03-28 10:55:11 -070080 else:
Alexander Afanasyevb5220702017-09-21 18:57:00 -040081 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070082
Alexander Afanasyevb5220702017-09-21 18:57:00 -040083 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyev76818632015-01-26 21:30:45 -080084 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Davide Pesaventoed89ccc2015-11-01 20:35:04 +010085 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Alexander Afanasyev76818632015-01-26 21:30:45 -080086
87 conf.env.DEFINES += extraFlags['DEFINES']
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020088
Davide Pesavento5b15e2f2023-08-07 16:35:35 -040089
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070090@Configure.conf
91def add_supported_cxxflags(self, cxxflags):
92 """
Davide Pesavento5b15e2f2023-08-07 16:35:35 -040093 Check which cxxflags are supported by the active compiler and add them to env.CXXFLAGS variable.
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070094 """
Alexander Afanasyev76818632015-01-26 21:30:45 -080095 if len(cxxflags) == 0:
96 return
97
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020098 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070099
100 supportedFlags = []
Alexander Afanasyevb5220702017-09-21 18:57:00 -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 Afanasyev97e4cac2014-03-28 10:55:11 -0700105
Davide Pesavento1bdef282014-04-08 20:59:50 +0200106 self.end_msg(' '.join(supportedFlags))
Davide Pesavento67f30272016-08-10 01:55:16 +0000107 self.env.prepend_value('CXXFLAGS', supportedFlags)
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200108
Davide Pesavento5b15e2f2023-08-07 16:35:35 -0400109
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200110@Configure.conf
111def add_supported_linkflags(self, linkflags):
112 """
Davide Pesavento5b15e2f2023-08-07 16:35:35 -0400113 Check which linkflags are supported by the active compiler and add them to env.LINKFLAGS variable.
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200114 """
Alexander Afanasyev76818632015-01-26 21:30:45 -0800115 if len(linkflags) == 0:
116 return
117
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200118 self.start_msg('Checking supported LINKFLAGS')
119
120 supportedFlags = []
Alexander Afanasyevb5220702017-09-21 18:57:00 -0400121 for flags in linkflags:
122 flags = Utils.to_list(flags)
123 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
124 supportedFlags += flags
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200125
126 self.end_msg(' '.join(supportedFlags))
Davide Pesavento67f30272016-08-10 01:55:16 +0000127 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyev76818632015-01-26 21:30:45 -0800128
129
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400130class CompilerFlags:
Alexander Afanasyev76818632015-01-26 21:30:45 -0800131 def getGeneralFlags(self, conf):
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100132 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Davide Pesavento936205d2024-02-27 21:42:44 -0500133 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 Pesaventoe277f8b2023-11-11 17:14:02 -0500137 return {
138 'CXXFLAGS': [],
139 'LINKFLAGS': [],
Davide Pesavento401d1a42024-12-19 21:10:22 -0500140 'DEFINES': [],
Davide Pesaventoe277f8b2023-11-11 17:14:02 -0500141 }
Alexander Afanasyev76818632015-01-26 21:30:45 -0800142
Alexander Afanasyev76818632015-01-26 21:30:45 -0800143 def getOptimizedFlags(self, conf):
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100144 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Alexander Afanasyev76818632015-01-26 21:30:45 -0800145 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
146
Davide Pesavento5b15e2f2023-08-07 16:35:35 -0400147
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400148class GccClangCommonFlags(CompilerFlags):
Davide Pesaventof485d892015-10-02 02:00:54 +0200149 """
Davide Pesavento5b15e2f2023-08-07 16:35:35 -0400150 This class defines common flags that work for both gcc and clang compilers.
Davide Pesaventof485d892015-10-02 02:00:54 +0200151 """
Davide Pesavento5b15e2f2023-08-07 16:35:35 -0400152
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -0400153 def getGeneralFlags(self, conf):
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400154 flags = super().getGeneralFlags(conf)
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400155 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesavento9bfdaef2022-10-05 17:38:44 -0400156 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesaventodfbeda22018-12-19 01:15:48 -0500157 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -0400158 return flags
159
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400160 __cxxFlags = [
161 '-fdiagnostics-color',
162 '-Wall',
163 '-Wextra',
164 '-Wpedantic',
165 '-Wenum-conversion',
166 '-Wextra-semi',
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400167 '-Wno-unused-parameter',
168 ]
169 __linkFlags = ['-Wl,-O1']
170
Alexander Afanasyev76818632015-01-26 21:30:45 -0800171 def getDebugFlags(self, conf):
Davide Pesaventobf2e67f2023-08-07 17:06:02 -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 Pesavento8a932842024-01-15 00:35:35 -0500179 # Enable assertions in libstdc++
180 # https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
181 flags['DEFINES'] += ['_GLIBCXX_ASSERTIONS=1']
Alexander Afanasyev76818632015-01-26 21:30:45 -0800182 return flags
183
184 def getOptimizedFlags(self, conf):
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400185 flags = super().getOptimizedFlags(conf)
186 flags['CXXFLAGS'] += ['-O2', '-g1'] + self.__cxxFlags
187 flags['LINKFLAGS'] += self.__linkFlags
Alexander Afanasyev76818632015-01-26 21:30:45 -0800188 return flags
189
Davide Pesavento5b15e2f2023-08-07 16:35:35 -0400190
Davide Pesaventobf2e67f2023-08-07 17:06:02 -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 Afanasyev76818632015-01-26 21:30:45 -0800202 def getDebugFlags(self, conf):
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400203 flags = super().getDebugFlags(conf)
204 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventoc52cd5e2022-03-05 20:40:54 -0500205 if platform.machine() == 'armv7l':
Davide Pesavento17521592020-05-14 19:01:32 -0400206 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Davide Pesaventof485d892015-10-02 02:00:54 +0200207 return flags
208
209 def getOptimizedFlags(self, conf):
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400210 flags = super().getOptimizedFlags(conf)
211 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventoc52cd5e2022-03-05 20:40:54 -0500212 if platform.machine() == 'armv7l':
Davide Pesavento17521592020-05-14 19:01:32 -0400213 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyev76818632015-01-26 21:30:45 -0800214 return flags
215
Davide Pesavento5b15e2f2023-08-07 16:35:35 -0400216
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400217class ClangFlags(GccClangCommonFlags):
Alexander Afanasyev76818632015-01-26 21:30:45 -0800218 def getGeneralFlags(self, conf):
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400219 flags = super().getGeneralFlags(conf)
Davide Pesavento17521592020-05-14 19:01:32 -0400220 if Utils.unversioned_sys_platform() == 'darwin':
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500221 # Bug #4296
Davide Pesaventoe541d1b2022-08-17 15:10:32 -0400222 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400223 flags['CXXFLAGS'] += [
224 ['-isystem', f'{brewdir}/include'], # for Homebrew
225 ['-isystem', '/opt/local/include'], # for MacPorts
226 ]
Davide Pesavento17521592020-05-14 19:01:32 -0400227 elif Utils.unversioned_sys_platform() == 'freebsd':
228 # Bug #4790
229 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Davide Pesaventoe11fa9a2025-03-18 02:42:03 -0400230 if get_compiler_ver(conf) >= (18, 0, 0) and get_compiler_ver(conf) < (20, 1, 0):
Davide Pesaventoafcf75d2024-01-13 21:51:51 -0500231 # Bug #5300
232 flags['CXXFLAGS'] += ['-Wno-enum-constexpr-conversion']
Alexander Afanasyev76818632015-01-26 21:30:45 -0800233 return flags
234
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400235 __cxxFlags = [
236 '-Wundefined-func-template',
237 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
238 ]
239
Alexander Afanasyev76818632015-01-26 21:30:45 -0800240 def getDebugFlags(self, conf):
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400241 flags = super().getDebugFlags(conf)
242 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento8a932842024-01-15 00:35:35 -0500243 # Enable assertions in libc++
Davide Pesaventoe11fa9a2025-03-18 02:42:03 -0400244 if get_compiler_ver(conf) >= (18, 0, 0):
Davide Pesavento8a932842024-01-15 00:35:35 -0500245 # https://libcxx.llvm.org/Hardening.html
246 flags['DEFINES'] += ['_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE']
Davide Pesaventoe11fa9a2025-03-18 02:42:03 -0400247 elif get_compiler_ver(conf) >= (15, 0, 0):
Davide Pesavento8a932842024-01-15 00:35:35 -0500248 # https://releases.llvm.org/15.0.0/projects/libcxx/docs/UsingLibcxx.html#enabling-the-safe-libc-mode
249 flags['DEFINES'] += ['_LIBCPP_ENABLE_ASSERTIONS=1']
Davide Pesavento936205d2024-02-27 21:42:44 -0500250 # Tell libc++ to avoid including transitive headers
251 # https://libcxx.llvm.org/DesignDocs/HeaderRemovalPolicy.html
252 flags['DEFINES'] += ['_LIBCPP_REMOVE_TRANSITIVE_INCLUDES=1']
Davide Pesaventof485d892015-10-02 02:00:54 +0200253 return flags
254
255 def getOptimizedFlags(self, conf):
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400256 flags = super().getOptimizedFlags(conf)
257 flags['CXXFLAGS'] += self.__cxxFlags
Alexander Afanasyev76818632015-01-26 21:30:45 -0800258 return flags