blob: aab0f92c1fbe593306b0a0ee5319f1332fb684a1 [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
14 ccver = tuple(int(i) for i in conf.env.CC_VERSION)
15 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
Alexander Afanasyevb5220702017-09-21 18:57:00 -040063@Configure.conf
64def check_compiler_flags(conf):
Davide Pesaventof485d892015-10-02 02:00:54 +020065 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Alexander Afanasyev76818632015-01-26 21:30:45 -080066 # corresponding environment variables are not set.
Davide Pesaventof485d892015-10-02 02:00:54 +020067 # DEFINES are always applied.
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070068 if conf.options.debug:
Alexander Afanasyevb5220702017-09-21 18:57:00 -040069 extraFlags = conf.flags.getDebugFlags(conf)
70 if conf.areCustomCxxflagsPresent:
Alexander Afanasyev76818632015-01-26 21:30:45 -080071 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Davide Pesavento0064c1d2018-03-03 18:43:53 -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))
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070076 else:
Alexander Afanasyevb5220702017-09-21 18:57:00 -040077 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070078
Alexander Afanasyevb5220702017-09-21 18:57:00 -040079 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyev76818632015-01-26 21:30:45 -080080 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Davide Pesaventoed89ccc2015-11-01 20:35:04 +010081 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Alexander Afanasyev76818632015-01-26 21:30:45 -080082
83 conf.env.DEFINES += extraFlags['DEFINES']
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020084
Davide Pesavento5b15e2f2023-08-07 16:35:35 -040085
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070086@Configure.conf
87def add_supported_cxxflags(self, cxxflags):
88 """
Davide Pesavento5b15e2f2023-08-07 16:35:35 -040089 Check which cxxflags are supported by the active compiler and add them to env.CXXFLAGS variable.
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070090 """
Alexander Afanasyev76818632015-01-26 21:30:45 -080091 if len(cxxflags) == 0:
92 return
93
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020094 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070095
96 supportedFlags = []
Alexander Afanasyevb5220702017-09-21 18:57:00 -040097 for flags in cxxflags:
98 flags = Utils.to_list(flags)
99 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
100 supportedFlags += flags
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -0700101
Davide Pesavento1bdef282014-04-08 20:59:50 +0200102 self.end_msg(' '.join(supportedFlags))
Davide Pesavento67f30272016-08-10 01:55:16 +0000103 self.env.prepend_value('CXXFLAGS', supportedFlags)
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200104
Davide Pesavento5b15e2f2023-08-07 16:35:35 -0400105
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200106@Configure.conf
107def add_supported_linkflags(self, linkflags):
108 """
Davide Pesavento5b15e2f2023-08-07 16:35:35 -0400109 Check which linkflags are supported by the active compiler and add them to env.LINKFLAGS variable.
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200110 """
Alexander Afanasyev76818632015-01-26 21:30:45 -0800111 if len(linkflags) == 0:
112 return
113
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200114 self.start_msg('Checking supported LINKFLAGS')
115
116 supportedFlags = []
Alexander Afanasyevb5220702017-09-21 18:57:00 -0400117 for flags in linkflags:
118 flags = Utils.to_list(flags)
119 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
120 supportedFlags += flags
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200121
122 self.end_msg(' '.join(supportedFlags))
Davide Pesavento67f30272016-08-10 01:55:16 +0000123 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyev76818632015-01-26 21:30:45 -0800124
125
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400126class CompilerFlags:
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500127 def getCompilerVersion(self, conf):
128 return tuple(int(i) for i in conf.env.CC_VERSION)
129
Alexander Afanasyev76818632015-01-26 21:30:45 -0800130 def getGeneralFlags(self, conf):
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100131 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Davide Pesavento936205d2024-02-27 21:42:44 -0500132 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 Pesaventoe277f8b2023-11-11 17:14:02 -0500136 return {
137 'CXXFLAGS': [],
138 'LINKFLAGS': [],
Davide Pesavento401d1a42024-12-19 21:10:22 -0500139 'DEFINES': [],
Davide Pesaventoe277f8b2023-11-11 17:14:02 -0500140 }
Alexander Afanasyev76818632015-01-26 21:30:45 -0800141
Alexander Afanasyev76818632015-01-26 21:30:45 -0800142 def getOptimizedFlags(self, conf):
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100143 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Alexander Afanasyev76818632015-01-26 21:30:45 -0800144 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
145
Davide Pesavento5b15e2f2023-08-07 16:35:35 -0400146
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400147class GccClangCommonFlags(CompilerFlags):
Davide Pesaventof485d892015-10-02 02:00:54 +0200148 """
Davide Pesavento5b15e2f2023-08-07 16:35:35 -0400149 This class defines common flags that work for both gcc and clang compilers.
Davide Pesaventof485d892015-10-02 02:00:54 +0200150 """
Davide Pesavento5b15e2f2023-08-07 16:35:35 -0400151
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -0400152 def getGeneralFlags(self, conf):
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400153 flags = super().getGeneralFlags(conf)
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400154 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesavento9bfdaef2022-10-05 17:38:44 -0400155 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesaventodfbeda22018-12-19 01:15:48 -0500156 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -0400157 return flags
158
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400159 __cxxFlags = [
160 '-fdiagnostics-color',
161 '-Wall',
162 '-Wextra',
163 '-Wpedantic',
164 '-Wenum-conversion',
165 '-Wextra-semi',
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400166 '-Wno-unused-parameter',
167 ]
168 __linkFlags = ['-Wl,-O1']
169
Alexander Afanasyev76818632015-01-26 21:30:45 -0800170 def getDebugFlags(self, conf):
Davide Pesaventobf2e67f2023-08-07 17:06:02 -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 Pesavento8a932842024-01-15 00:35:35 -0500178 # Enable assertions in libstdc++
179 # https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
180 flags['DEFINES'] += ['_GLIBCXX_ASSERTIONS=1']
Alexander Afanasyev76818632015-01-26 21:30:45 -0800181 return flags
182
183 def getOptimizedFlags(self, conf):
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400184 flags = super().getOptimizedFlags(conf)
185 flags['CXXFLAGS'] += ['-O2', '-g1'] + self.__cxxFlags
186 flags['LINKFLAGS'] += self.__linkFlags
Alexander Afanasyev76818632015-01-26 21:30:45 -0800187 return flags
188
Davide Pesavento5b15e2f2023-08-07 16:35:35 -0400189
Davide Pesaventobf2e67f2023-08-07 17:06:02 -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 Afanasyev76818632015-01-26 21:30:45 -0800201 def getDebugFlags(self, conf):
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400202 flags = super().getDebugFlags(conf)
203 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventoc52cd5e2022-03-05 20:40:54 -0500204 if platform.machine() == 'armv7l':
Davide Pesavento17521592020-05-14 19:01:32 -0400205 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Davide Pesaventof485d892015-10-02 02:00:54 +0200206 return flags
207
208 def getOptimizedFlags(self, conf):
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400209 flags = super().getOptimizedFlags(conf)
210 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventoc52cd5e2022-03-05 20:40:54 -0500211 if platform.machine() == 'armv7l':
Davide Pesavento17521592020-05-14 19:01:32 -0400212 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyev76818632015-01-26 21:30:45 -0800213 return flags
214
Davide Pesavento5b15e2f2023-08-07 16:35:35 -0400215
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400216class ClangFlags(GccClangCommonFlags):
Alexander Afanasyev76818632015-01-26 21:30:45 -0800217 def getGeneralFlags(self, conf):
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400218 flags = super().getGeneralFlags(conf)
Davide Pesavento17521592020-05-14 19:01:32 -0400219 if Utils.unversioned_sys_platform() == 'darwin':
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500220 # Bug #4296
Davide Pesaventoe541d1b2022-08-17 15:10:32 -0400221 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400222 flags['CXXFLAGS'] += [
223 ['-isystem', f'{brewdir}/include'], # for Homebrew
224 ['-isystem', '/opt/local/include'], # for MacPorts
225 ]
Davide Pesavento17521592020-05-14 19:01:32 -0400226 elif Utils.unversioned_sys_platform() == 'freebsd':
227 # Bug #4790
228 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Davide Pesaventoafcf75d2024-01-13 21:51:51 -0500229 if self.getCompilerVersion(conf) >= (18, 0, 0):
230 # Bug #5300
231 flags['CXXFLAGS'] += ['-Wno-enum-constexpr-conversion']
Alexander Afanasyev76818632015-01-26 21:30:45 -0800232 return flags
233
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400234 __cxxFlags = [
235 '-Wundefined-func-template',
236 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
237 ]
238
Alexander Afanasyev76818632015-01-26 21:30:45 -0800239 def getDebugFlags(self, conf):
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400240 flags = super().getDebugFlags(conf)
241 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento8a932842024-01-15 00:35:35 -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 Pesavento936205d2024-02-27 21:42:44 -0500249 # Tell libc++ to avoid including transitive headers
250 # https://libcxx.llvm.org/DesignDocs/HeaderRemovalPolicy.html
251 flags['DEFINES'] += ['_LIBCPP_REMOVE_TRANSITIVE_INCLUDES=1']
Davide Pesaventof485d892015-10-02 02:00:54 +0200252 return flags
253
254 def getOptimizedFlags(self, conf):
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400255 flags = super().getOptimizedFlags(conf)
256 flags['CXXFLAGS'] += self.__cxxFlags
Alexander Afanasyev76818632015-01-26 21:30:45 -0800257 return flags