blob: d70575d225a2ae68d38274be1520bee09b251753 [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 Pesaventoc52cd5e2022-03-05 20:40:54 -050019 if ccver < (7, 4, 0):
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040020 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesaventoc5e3e7a2023-03-07 21:25:27 -050021 'The minimum supported gcc version is 9.3.')
22 elif ccver < (9, 3, 0):
23 warnmsg = ('Using a version of gcc older than 9.3 is not '
24 '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':
28 if ccver < (10, 0, 0):
29 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 Pesaventoc5e3e7a2023-03-07 21:25:27 -050036 'The minimum supported clang version is 7.0.')
Alexander Afanasyevb5220702017-09-21 18:57:00 -040037 conf.flags = ClangFlags()
Alexander Afanasyev76818632015-01-26 21:30:45 -080038 else:
Davide Pesaventoe541d1b2022-08-17 15:10:32 -040039 warnmsg = f'{cxx} compiler is unsupported'
Alexander Afanasyevb5220702017-09-21 18:57:00 -040040 conf.flags = CompilerFlags()
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040041
42 if errmsg:
Davide Pesavento0064c1d2018-03-03 18:43:53 -050043 conf.end_msg(ccverstr, color='RED')
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040044 conf.fatal(errmsg)
45 elif warnmsg:
Davide Pesavento0064c1d2018-03-03 18:43:53 -050046 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento17521592020-05-14 19:01:32 -040047 Logs.warn('WARNING: ' + warnmsg)
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040048 else:
Davide Pesavento0064c1d2018-03-03 18:43:53 -050049 conf.end_msg(ccverstr)
Davide Pesavento1bdef282014-04-08 20:59:50 +020050
Davide Pesavento5b15e2f2023-08-07 16:35:35 -040051 conf.areCustomCxxflagsPresent = len(conf.env.CXXFLAGS) > 0
Alexander Afanasyev76818632015-01-26 21:30:45 -080052
Davide Pesaventoc0822fa2018-05-10 21:54:10 -040053 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyevb5220702017-09-21 18:57:00 -040054 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyev76818632015-01-26 21:30:45 -080055 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
56 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
57 conf.env.DEFINES += generalFlags['DEFINES']
58
Davide Pesavento5b15e2f2023-08-07 16:35:35 -040059
Alexander Afanasyevb5220702017-09-21 18:57:00 -040060@Configure.conf
61def check_compiler_flags(conf):
Davide Pesaventof485d892015-10-02 02:00:54 +020062 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Alexander Afanasyev76818632015-01-26 21:30:45 -080063 # corresponding environment variables are not set.
Davide Pesaventof485d892015-10-02 02:00:54 +020064 # DEFINES are always applied.
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070065 if conf.options.debug:
Alexander Afanasyevb5220702017-09-21 18:57:00 -040066 extraFlags = conf.flags.getDebugFlags(conf)
67 if conf.areCustomCxxflagsPresent:
Alexander Afanasyev76818632015-01-26 21:30:45 -080068 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Davide Pesavento0064c1d2018-03-03 18:43:53 -050069 if missingFlags:
70 Logs.warn('Selected debug mode, but CXXFLAGS is set to a custom value "%s"'
71 % ' '.join(conf.env.CXXFLAGS))
72 Logs.warn('Default flags "%s" will not be used' % ' '.join(missingFlags))
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070073 else:
Alexander Afanasyevb5220702017-09-21 18:57:00 -040074 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070075
Alexander Afanasyevb5220702017-09-21 18:57:00 -040076 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyev76818632015-01-26 21:30:45 -080077 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Davide Pesaventoed89ccc2015-11-01 20:35:04 +010078 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Alexander Afanasyev76818632015-01-26 21:30:45 -080079
80 conf.env.DEFINES += extraFlags['DEFINES']
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020081
Davide Pesavento5b15e2f2023-08-07 16:35:35 -040082
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070083@Configure.conf
84def add_supported_cxxflags(self, cxxflags):
85 """
Davide Pesavento5b15e2f2023-08-07 16:35:35 -040086 Check which cxxflags are supported by the active compiler and add them to env.CXXFLAGS variable.
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070087 """
Alexander Afanasyev76818632015-01-26 21:30:45 -080088 if len(cxxflags) == 0:
89 return
90
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020091 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070092
93 supportedFlags = []
Alexander Afanasyevb5220702017-09-21 18:57:00 -040094 for flags in cxxflags:
95 flags = Utils.to_list(flags)
96 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
97 supportedFlags += flags
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070098
Davide Pesavento1bdef282014-04-08 20:59:50 +020099 self.end_msg(' '.join(supportedFlags))
Davide Pesavento67f30272016-08-10 01:55:16 +0000100 self.env.prepend_value('CXXFLAGS', supportedFlags)
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200101
Davide Pesavento5b15e2f2023-08-07 16:35:35 -0400102
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200103@Configure.conf
104def add_supported_linkflags(self, linkflags):
105 """
Davide Pesavento5b15e2f2023-08-07 16:35:35 -0400106 Check which linkflags are supported by the active compiler and add them to env.LINKFLAGS variable.
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200107 """
Alexander Afanasyev76818632015-01-26 21:30:45 -0800108 if len(linkflags) == 0:
109 return
110
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200111 self.start_msg('Checking supported LINKFLAGS')
112
113 supportedFlags = []
Alexander Afanasyevb5220702017-09-21 18:57:00 -0400114 for flags in linkflags:
115 flags = Utils.to_list(flags)
116 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
117 supportedFlags += flags
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200118
119 self.end_msg(' '.join(supportedFlags))
Davide Pesavento67f30272016-08-10 01:55:16 +0000120 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyev76818632015-01-26 21:30:45 -0800121
122
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400123class CompilerFlags:
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500124 def getCompilerVersion(self, conf):
125 return tuple(int(i) for i in conf.env.CC_VERSION)
126
Alexander Afanasyev76818632015-01-26 21:30:45 -0800127 def getGeneralFlags(self, conf):
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100128 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Davide Pesavento936205d2024-02-27 21:42:44 -0500129 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
130
131 def getDebugFlags(self, conf):
132 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Davide Pesaventoe277f8b2023-11-11 17:14:02 -0500133 return {
134 'CXXFLAGS': [],
135 'LINKFLAGS': [],
136 'DEFINES': ['BOOST_FILESYSTEM_NO_DEPRECATED'],
137 }
Alexander Afanasyev76818632015-01-26 21:30:45 -0800138
Alexander Afanasyev76818632015-01-26 21:30:45 -0800139 def getOptimizedFlags(self, conf):
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100140 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Alexander Afanasyev76818632015-01-26 21:30:45 -0800141 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
142
Davide Pesavento5b15e2f2023-08-07 16:35:35 -0400143
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400144class GccClangCommonFlags(CompilerFlags):
Davide Pesaventof485d892015-10-02 02:00:54 +0200145 """
Davide Pesavento5b15e2f2023-08-07 16:35:35 -0400146 This class defines common flags that work for both gcc and clang compilers.
Davide Pesaventof485d892015-10-02 02:00:54 +0200147 """
Davide Pesavento5b15e2f2023-08-07 16:35:35 -0400148
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -0400149 def getGeneralFlags(self, conf):
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400150 flags = super().getGeneralFlags(conf)
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400151 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesavento9bfdaef2022-10-05 17:38:44 -0400152 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesaventodfbeda22018-12-19 01:15:48 -0500153 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -0400154 return flags
155
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400156 __cxxFlags = [
157 '-fdiagnostics-color',
158 '-Wall',
159 '-Wextra',
160 '-Wpedantic',
161 '-Wenum-conversion',
162 '-Wextra-semi',
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400163 '-Wno-unused-parameter',
164 ]
165 __linkFlags = ['-Wl,-O1']
166
Alexander Afanasyev76818632015-01-26 21:30:45 -0800167 def getDebugFlags(self, conf):
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400168 flags = super().getDebugFlags(conf)
169 flags['CXXFLAGS'] += ['-Og', '-g'] + self.__cxxFlags + [
170 '-Werror',
171 '-Wno-error=deprecated-declarations', # Bug #3795
172 '-Wno-error=maybe-uninitialized', # Bug #1615
173 ]
174 flags['LINKFLAGS'] += self.__linkFlags
Davide Pesavento8a932842024-01-15 00:35:35 -0500175 # Enable assertions in libstdc++
176 # https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
177 flags['DEFINES'] += ['_GLIBCXX_ASSERTIONS=1']
Alexander Afanasyev76818632015-01-26 21:30:45 -0800178 return flags
179
180 def getOptimizedFlags(self, conf):
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400181 flags = super().getOptimizedFlags(conf)
182 flags['CXXFLAGS'] += ['-O2', '-g1'] + self.__cxxFlags
183 flags['LINKFLAGS'] += self.__linkFlags
Alexander Afanasyev76818632015-01-26 21:30:45 -0800184 return flags
185
Davide Pesavento5b15e2f2023-08-07 16:35:35 -0400186
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400187class GccFlags(GccClangCommonFlags):
188 __cxxFlags = [
189 '-Wcatch-value=2',
190 '-Wcomma-subscript', # enabled by default in C++20
191 '-Wduplicated-branches',
192 '-Wduplicated-cond',
193 '-Wlogical-op',
194 '-Wredundant-tags',
195 '-Wvolatile', # enabled by default in C++20
196 ]
197
Alexander Afanasyev76818632015-01-26 21:30:45 -0800198 def getDebugFlags(self, conf):
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400199 flags = super().getDebugFlags(conf)
200 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventoc52cd5e2022-03-05 20:40:54 -0500201 if platform.machine() == 'armv7l':
Davide Pesavento17521592020-05-14 19:01:32 -0400202 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Davide Pesaventof485d892015-10-02 02:00:54 +0200203 return flags
204
205 def getOptimizedFlags(self, conf):
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400206 flags = super().getOptimizedFlags(conf)
207 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventoc52cd5e2022-03-05 20:40:54 -0500208 if platform.machine() == 'armv7l':
Davide Pesavento17521592020-05-14 19:01:32 -0400209 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyev76818632015-01-26 21:30:45 -0800210 return flags
211
Davide Pesavento5b15e2f2023-08-07 16:35:35 -0400212
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400213class ClangFlags(GccClangCommonFlags):
Alexander Afanasyev76818632015-01-26 21:30:45 -0800214 def getGeneralFlags(self, conf):
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400215 flags = super().getGeneralFlags(conf)
Davide Pesavento17521592020-05-14 19:01:32 -0400216 if Utils.unversioned_sys_platform() == 'darwin':
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500217 # Bug #4296
Davide Pesaventoe541d1b2022-08-17 15:10:32 -0400218 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400219 flags['CXXFLAGS'] += [
220 ['-isystem', f'{brewdir}/include'], # for Homebrew
221 ['-isystem', '/opt/local/include'], # for MacPorts
222 ]
Davide Pesavento17521592020-05-14 19:01:32 -0400223 elif Utils.unversioned_sys_platform() == 'freebsd':
224 # Bug #4790
225 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Davide Pesaventoafcf75d2024-01-13 21:51:51 -0500226 if self.getCompilerVersion(conf) >= (18, 0, 0):
227 # Bug #5300
228 flags['CXXFLAGS'] += ['-Wno-enum-constexpr-conversion']
Alexander Afanasyev76818632015-01-26 21:30:45 -0800229 return flags
230
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400231 __cxxFlags = [
232 '-Wundefined-func-template',
233 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
234 ]
235
Alexander Afanasyev76818632015-01-26 21:30:45 -0800236 def getDebugFlags(self, conf):
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400237 flags = super().getDebugFlags(conf)
238 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento8a932842024-01-15 00:35:35 -0500239 # Enable assertions in libc++
240 if self.getCompilerVersion(conf) >= (18, 0, 0):
241 # https://libcxx.llvm.org/Hardening.html
242 flags['DEFINES'] += ['_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE']
243 elif self.getCompilerVersion(conf) >= (15, 0, 0):
244 # https://releases.llvm.org/15.0.0/projects/libcxx/docs/UsingLibcxx.html#enabling-the-safe-libc-mode
245 flags['DEFINES'] += ['_LIBCPP_ENABLE_ASSERTIONS=1']
Davide Pesavento936205d2024-02-27 21:42:44 -0500246 # Tell libc++ to avoid including transitive headers
247 # https://libcxx.llvm.org/DesignDocs/HeaderRemovalPolicy.html
248 flags['DEFINES'] += ['_LIBCPP_REMOVE_TRANSITIVE_INCLUDES=1']
Davide Pesaventof485d892015-10-02 02:00:54 +0200249 return flags
250
251 def getOptimizedFlags(self, conf):
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400252 flags = super().getOptimizedFlags(conf)
253 flags['CXXFLAGS'] += self.__cxxFlags
Alexander Afanasyev76818632015-01-26 21:30:45 -0800254 return flags