blob: 7784d9f32883c766d981efc2322e6dea77a5a1fd [file] [log] [blame]
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -07001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -07002
Davide Pesavento17521592020-05-14 19:01:32 -04003import platform
Davide Pesavento0064c1d2018-03-03 18:43:53 -05004from waflib import Configure, Logs, Utils
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -07005
Davide Pesavento5b15e2f2023-08-07 16:35:35 -04006
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -07007def options(opt):
Davide Pesavento0064c1d2018-03-03 18:43:53 -05008 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesavento17521592020-05-14 19:01:32 -04009 help='Compile in debugging mode with minimal optimizations (-Og)')
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070010
Davide Pesavento5b15e2f2023-08-07 16:35:35 -040011
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070012def configure(conf):
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040013 conf.start_msg('Checking C++ compiler version')
14
Davide Pesavento0064c1d2018-03-03 18:43:53 -050015 cxx = conf.env.CXX_NAME # generic name of the compiler
16 ccver = tuple(int(i) for i in conf.env.CC_VERSION)
17 ccverstr = '.'.join(conf.env.CC_VERSION)
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040018 errmsg = ''
19 warnmsg = ''
Alexander Afanasyev76818632015-01-26 21:30:45 -080020 if cxx == 'gcc':
Davide Pesaventoc52cd5e2022-03-05 20:40:54 -050021 if ccver < (7, 4, 0):
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040022 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesaventoc5e3e7a2023-03-07 21:25:27 -050023 'The minimum supported gcc version is 9.3.')
24 elif ccver < (9, 3, 0):
25 warnmsg = ('Using a version of gcc older than 9.3 is not '
26 'officially supported and may result in build failures.')
Alexander Afanasyevb5220702017-09-21 18:57:00 -040027 conf.flags = GccFlags()
Alexander Afanasyev76818632015-01-26 21:30:45 -080028 elif cxx == 'clang':
Davide Pesaventoc52cd5e2022-03-05 20:40:54 -050029 if Utils.unversioned_sys_platform() == 'darwin':
30 if ccver < (10, 0, 0):
31 errmsg = ('The version of Xcode you are using is too old.\n'
Davide Pesaventoc5e3e7a2023-03-07 21:25:27 -050032 'The minimum supported Xcode version is 12.4.')
33 elif ccver < (12, 0, 0):
34 warnmsg = ('Using a version of Xcode older than 12.4 is not '
Davide Pesaventoc52cd5e2022-03-05 20:40:54 -050035 'officially supported and may result in build failures.')
Davide Pesaventoc5e3e7a2023-03-07 21:25:27 -050036 elif ccver < (7, 0, 0):
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040037 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesaventoc5e3e7a2023-03-07 21:25:27 -050038 'The minimum supported clang version is 7.0.')
Alexander Afanasyevb5220702017-09-21 18:57:00 -040039 conf.flags = ClangFlags()
Alexander Afanasyev76818632015-01-26 21:30:45 -080040 else:
Davide Pesaventoe541d1b2022-08-17 15:10:32 -040041 warnmsg = f'{cxx} compiler is unsupported'
Alexander Afanasyevb5220702017-09-21 18:57:00 -040042 conf.flags = CompilerFlags()
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040043
44 if errmsg:
Davide Pesavento0064c1d2018-03-03 18:43:53 -050045 conf.end_msg(ccverstr, color='RED')
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040046 conf.fatal(errmsg)
47 elif warnmsg:
Davide Pesavento0064c1d2018-03-03 18:43:53 -050048 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento17521592020-05-14 19:01:32 -040049 Logs.warn('WARNING: ' + warnmsg)
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040050 else:
Davide Pesavento0064c1d2018-03-03 18:43:53 -050051 conf.end_msg(ccverstr)
Davide Pesavento1bdef282014-04-08 20:59:50 +020052
Davide Pesavento5b15e2f2023-08-07 16:35:35 -040053 conf.areCustomCxxflagsPresent = len(conf.env.CXXFLAGS) > 0
Alexander Afanasyev76818632015-01-26 21:30:45 -080054
Davide Pesaventoc0822fa2018-05-10 21:54:10 -040055 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyevb5220702017-09-21 18:57:00 -040056 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyev76818632015-01-26 21:30:45 -080057 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
58 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
59 conf.env.DEFINES += generalFlags['DEFINES']
60
Davide Pesavento5b15e2f2023-08-07 16:35:35 -040061
Alexander Afanasyevb5220702017-09-21 18:57:00 -040062@Configure.conf
63def check_compiler_flags(conf):
Davide Pesaventof485d892015-10-02 02:00:54 +020064 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Alexander Afanasyev76818632015-01-26 21:30:45 -080065 # corresponding environment variables are not set.
Davide Pesaventof485d892015-10-02 02:00:54 +020066 # DEFINES are always applied.
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070067 if conf.options.debug:
Alexander Afanasyevb5220702017-09-21 18:57:00 -040068 extraFlags = conf.flags.getDebugFlags(conf)
69 if conf.areCustomCxxflagsPresent:
Alexander Afanasyev76818632015-01-26 21:30:45 -080070 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Davide Pesavento0064c1d2018-03-03 18:43:53 -050071 if missingFlags:
72 Logs.warn('Selected debug mode, but CXXFLAGS is set to a custom value "%s"'
73 % ' '.join(conf.env.CXXFLAGS))
74 Logs.warn('Default flags "%s" will not be used' % ' '.join(missingFlags))
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070075 else:
Alexander Afanasyevb5220702017-09-21 18:57:00 -040076 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070077
Alexander Afanasyevb5220702017-09-21 18:57:00 -040078 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyev76818632015-01-26 21:30:45 -080079 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Davide Pesaventoed89ccc2015-11-01 20:35:04 +010080 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Alexander Afanasyev76818632015-01-26 21:30:45 -080081
82 conf.env.DEFINES += extraFlags['DEFINES']
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020083
Davide Pesavento5b15e2f2023-08-07 16:35:35 -040084
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070085@Configure.conf
86def add_supported_cxxflags(self, cxxflags):
87 """
Davide Pesavento5b15e2f2023-08-07 16:35:35 -040088 Check which cxxflags are supported by the active compiler and add them to env.CXXFLAGS variable.
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070089 """
Alexander Afanasyev76818632015-01-26 21:30:45 -080090 if len(cxxflags) == 0:
91 return
92
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020093 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070094
95 supportedFlags = []
Alexander Afanasyevb5220702017-09-21 18:57:00 -040096 for flags in cxxflags:
97 flags = Utils.to_list(flags)
98 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
99 supportedFlags += flags
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -0700100
Davide Pesavento1bdef282014-04-08 20:59:50 +0200101 self.end_msg(' '.join(supportedFlags))
Davide Pesavento67f30272016-08-10 01:55:16 +0000102 self.env.prepend_value('CXXFLAGS', supportedFlags)
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200103
Davide Pesavento5b15e2f2023-08-07 16:35:35 -0400104
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200105@Configure.conf
106def add_supported_linkflags(self, linkflags):
107 """
Davide Pesavento5b15e2f2023-08-07 16:35:35 -0400108 Check which linkflags are supported by the active compiler and add them to env.LINKFLAGS variable.
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200109 """
Alexander Afanasyev76818632015-01-26 21:30:45 -0800110 if len(linkflags) == 0:
111 return
112
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200113 self.start_msg('Checking supported LINKFLAGS')
114
115 supportedFlags = []
Alexander Afanasyevb5220702017-09-21 18:57:00 -0400116 for flags in linkflags:
117 flags = Utils.to_list(flags)
118 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
119 supportedFlags += flags
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200120
121 self.end_msg(' '.join(supportedFlags))
Davide Pesavento67f30272016-08-10 01:55:16 +0000122 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyev76818632015-01-26 21:30:45 -0800123
124
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400125class CompilerFlags:
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500126 def getCompilerVersion(self, conf):
127 return tuple(int(i) for i in conf.env.CC_VERSION)
128
Alexander Afanasyev76818632015-01-26 21:30:45 -0800129 def getGeneralFlags(self, conf):
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100130 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Davide Pesaventoe277f8b2023-11-11 17:14:02 -0500131 return {
132 'CXXFLAGS': [],
133 'LINKFLAGS': [],
134 'DEFINES': ['BOOST_FILESYSTEM_NO_DEPRECATED'],
135 }
Alexander Afanasyev76818632015-01-26 21:30:45 -0800136
137 def getDebugFlags(self, conf):
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100138 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Davide Pesavento91c15c82024-01-15 17:14:23 -0500139 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
Alexander Afanasyev76818632015-01-26 21:30:45 -0800140
141 def getOptimizedFlags(self, conf):
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100142 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Alexander Afanasyev76818632015-01-26 21:30:45 -0800143 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
144
Davide Pesavento5b15e2f2023-08-07 16:35:35 -0400145
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400146class GccClangCommonFlags(CompilerFlags):
Davide Pesaventof485d892015-10-02 02:00:54 +0200147 """
Davide Pesavento5b15e2f2023-08-07 16:35:35 -0400148 This class defines common flags that work for both gcc and clang compilers.
Davide Pesaventof485d892015-10-02 02:00:54 +0200149 """
Davide Pesavento5b15e2f2023-08-07 16:35:35 -0400150
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -0400151 def getGeneralFlags(self, conf):
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400152 flags = super().getGeneralFlags(conf)
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400153 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesavento9bfdaef2022-10-05 17:38:44 -0400154 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesaventodfbeda22018-12-19 01:15:48 -0500155 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -0400156 return flags
157
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400158 __cxxFlags = [
159 '-fdiagnostics-color',
160 '-Wall',
161 '-Wextra',
162 '-Wpedantic',
163 '-Wenum-conversion',
164 '-Wextra-semi',
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400165 '-Wno-unused-parameter',
166 ]
167 __linkFlags = ['-Wl,-O1']
168
Alexander Afanasyev76818632015-01-26 21:30:45 -0800169 def getDebugFlags(self, conf):
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400170 flags = super().getDebugFlags(conf)
171 flags['CXXFLAGS'] += ['-Og', '-g'] + self.__cxxFlags + [
172 '-Werror',
173 '-Wno-error=deprecated-declarations', # Bug #3795
174 '-Wno-error=maybe-uninitialized', # Bug #1615
175 ]
176 flags['LINKFLAGS'] += self.__linkFlags
Davide Pesavento8a932842024-01-15 00:35:35 -0500177 # Enable assertions in libstdc++
178 # https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
179 flags['DEFINES'] += ['_GLIBCXX_ASSERTIONS=1']
Alexander Afanasyev76818632015-01-26 21:30:45 -0800180 return flags
181
182 def getOptimizedFlags(self, conf):
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400183 flags = super().getOptimizedFlags(conf)
184 flags['CXXFLAGS'] += ['-O2', '-g1'] + self.__cxxFlags
185 flags['LINKFLAGS'] += self.__linkFlags
Alexander Afanasyev76818632015-01-26 21:30:45 -0800186 return flags
187
Davide Pesavento5b15e2f2023-08-07 16:35:35 -0400188
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400189class GccFlags(GccClangCommonFlags):
190 __cxxFlags = [
191 '-Wcatch-value=2',
192 '-Wcomma-subscript', # enabled by default in C++20
193 '-Wduplicated-branches',
194 '-Wduplicated-cond',
195 '-Wlogical-op',
196 '-Wredundant-tags',
197 '-Wvolatile', # enabled by default in C++20
198 ]
199
Alexander Afanasyev76818632015-01-26 21:30:45 -0800200 def getDebugFlags(self, conf):
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400201 flags = super().getDebugFlags(conf)
202 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventoc52cd5e2022-03-05 20:40:54 -0500203 if platform.machine() == 'armv7l':
Davide Pesavento17521592020-05-14 19:01:32 -0400204 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Davide Pesaventof485d892015-10-02 02:00:54 +0200205 return flags
206
207 def getOptimizedFlags(self, conf):
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400208 flags = super().getOptimizedFlags(conf)
209 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventoc52cd5e2022-03-05 20:40:54 -0500210 if platform.machine() == 'armv7l':
Davide Pesavento17521592020-05-14 19:01:32 -0400211 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyev76818632015-01-26 21:30:45 -0800212 return flags
213
Davide Pesavento5b15e2f2023-08-07 16:35:35 -0400214
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400215class ClangFlags(GccClangCommonFlags):
Alexander Afanasyev76818632015-01-26 21:30:45 -0800216 def getGeneralFlags(self, conf):
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400217 flags = super().getGeneralFlags(conf)
Davide Pesavento17521592020-05-14 19:01:32 -0400218 if Utils.unversioned_sys_platform() == 'darwin':
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500219 # Bug #4296
Davide Pesaventoe541d1b2022-08-17 15:10:32 -0400220 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400221 flags['CXXFLAGS'] += [
222 ['-isystem', f'{brewdir}/include'], # for Homebrew
223 ['-isystem', '/opt/local/include'], # for MacPorts
224 ]
Davide Pesavento17521592020-05-14 19:01:32 -0400225 elif Utils.unversioned_sys_platform() == 'freebsd':
226 # Bug #4790
227 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Davide Pesaventoafcf75d2024-01-13 21:51:51 -0500228 if self.getCompilerVersion(conf) >= (18, 0, 0):
229 # Bug #5300
230 flags['CXXFLAGS'] += ['-Wno-enum-constexpr-conversion']
Alexander Afanasyev76818632015-01-26 21:30:45 -0800231 return flags
232
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400233 __cxxFlags = [
234 '-Wundefined-func-template',
235 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
236 ]
237
Alexander Afanasyev76818632015-01-26 21:30:45 -0800238 def getDebugFlags(self, conf):
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400239 flags = super().getDebugFlags(conf)
240 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento8a932842024-01-15 00:35:35 -0500241 # Enable assertions in libc++
242 if self.getCompilerVersion(conf) >= (18, 0, 0):
243 # https://libcxx.llvm.org/Hardening.html
244 flags['DEFINES'] += ['_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE']
245 elif self.getCompilerVersion(conf) >= (15, 0, 0):
246 # https://releases.llvm.org/15.0.0/projects/libcxx/docs/UsingLibcxx.html#enabling-the-safe-libc-mode
247 flags['DEFINES'] += ['_LIBCPP_ENABLE_ASSERTIONS=1']
Davide Pesaventof485d892015-10-02 02:00:54 +0200248 return flags
249
250 def getOptimizedFlags(self, conf):
Davide Pesaventobf2e67f2023-08-07 17:06:02 -0400251 flags = super().getOptimizedFlags(conf)
252 flags['CXXFLAGS'] += self.__cxxFlags
Alexander Afanasyev76818632015-01-26 21:30:45 -0800253 return flags