blob: 7784d9f32883c766d981efc2322e6dea77a5a1fd [file] [log] [blame]
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -07001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -07002
Davide Pesavento3d01fa32021-10-03 17:13:38 -04003import platform
Alexander Afanasyev08d18742018-03-15 16:31:28 -04004from waflib import Configure, Logs, Utils
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -07005
Davide Pesaventoaf340ea2023-08-10 20:46:06 -04006
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -07007def options(opt):
Alexander Afanasyev08d18742018-03-15 16:31:28 -04008 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesavento3d01fa32021-10-03 17:13:38 -04009 help='Compile in debugging mode with minimal optimizations (-Og)')
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070010
Davide Pesaventoaf340ea2023-08-10 20:46:06 -040011
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070012def configure(conf):
Alexander Afanasyev08d18742018-03-15 16:31:28 -040013 conf.start_msg('Checking C++ compiler version')
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070014
Alexander Afanasyev08d18742018-03-15 16:31:28 -040015 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)
18 errmsg = ''
19 warnmsg = ''
20 if cxx == 'gcc':
Davide Pesavento7f27ec12022-03-10 20:10:54 -050021 if ccver < (7, 4, 0):
Alexander Afanasyev08d18742018-03-15 16:31:28 -040022 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesaventoaf340ea2023-08-10 20:46:06 -040023 '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 Afanasyev08d18742018-03-15 16:31:28 -040027 conf.flags = GccFlags()
28 elif cxx == 'clang':
Davide Pesavento7f27ec12022-03-10 20:10: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 Pesaventoaf340ea2023-08-10 20:46:06 -040032 '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 Pesavento7f27ec12022-03-10 20:10:54 -050035 'officially supported and may result in build failures.')
Davide Pesaventoaf340ea2023-08-10 20:46:06 -040036 elif ccver < (7, 0, 0):
Alexander Afanasyev08d18742018-03-15 16:31:28 -040037 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesaventoaf340ea2023-08-10 20:46:06 -040038 'The minimum supported clang version is 7.0.')
Alexander Afanasyev08d18742018-03-15 16:31:28 -040039 conf.flags = ClangFlags()
40 else:
Davide Pesavento423553e2022-08-19 20:46:06 -040041 warnmsg = f'{cxx} compiler is unsupported'
Alexander Afanasyev08d18742018-03-15 16:31:28 -040042 conf.flags = CompilerFlags()
43
44 if errmsg:
45 conf.end_msg(ccverstr, color='RED')
46 conf.fatal(errmsg)
47 elif warnmsg:
48 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento3d01fa32021-10-03 17:13:38 -040049 Logs.warn('WARNING: ' + warnmsg)
Alexander Afanasyev08d18742018-03-15 16:31:28 -040050 else:
51 conf.end_msg(ccverstr)
52
Davide Pesaventoaf340ea2023-08-10 20:46:06 -040053 conf.areCustomCxxflagsPresent = len(conf.env.CXXFLAGS) > 0
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080054
Davide Pesavento90034832018-05-30 10:10:31 -040055 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyev08d18742018-03-15 16:31:28 -040056 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080057 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
58 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
59 conf.env.DEFINES += generalFlags['DEFINES']
60
Davide Pesaventoaf340ea2023-08-10 20:46:06 -040061
Alexander Afanasyev08d18742018-03-15 16:31:28 -040062@Configure.conf
63def check_compiler_flags(conf):
Alexander Afanasyevc7c99002015-10-09 17:27:30 -070064 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080065 # corresponding environment variables are not set.
Alexander Afanasyevc7c99002015-10-09 17:27:30 -070066 # DEFINES are always applied.
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070067 if conf.options.debug:
Alexander Afanasyev08d18742018-03-15 16:31:28 -040068 extraFlags = conf.flags.getDebugFlags(conf)
69 if conf.areCustomCxxflagsPresent:
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080070 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev08d18742018-03-15 16:31:28 -040071 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 Afanasyev4ffcff22014-09-02 15:39:20 -070075 else:
Alexander Afanasyev08d18742018-03-15 16:31:28 -040076 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070077
Alexander Afanasyev08d18742018-03-15 16:31:28 -040078 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080079 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -080080 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080081
82 conf.env.DEFINES += extraFlags['DEFINES']
Shock Jiang2d8483c2014-10-30 10:15:37 -070083
Davide Pesaventoaf340ea2023-08-10 20:46:06 -040084
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070085@Configure.conf
86def add_supported_cxxflags(self, cxxflags):
87 """
Davide Pesaventoaf340ea2023-08-10 20:46:06 -040088 Check which cxxflags are supported by the active compiler and add them to env.CXXFLAGS variable.
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070089 """
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080090 if len(cxxflags) == 0:
91 return
92
Shock Jiang2d8483c2014-10-30 10:15:37 -070093 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070094
95 supportedFlags = []
Alexander Afanasyev08d18742018-03-15 16:31:28 -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 Afanasyev4ffcff22014-09-02 15:39:20 -0700100
101 self.end_msg(' '.join(supportedFlags))
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -0800102 self.env.prepend_value('CXXFLAGS', supportedFlags)
Shock Jiang2d8483c2014-10-30 10:15:37 -0700103
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400104
Shock Jiang2d8483c2014-10-30 10:15:37 -0700105@Configure.conf
106def add_supported_linkflags(self, linkflags):
107 """
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400108 Check which linkflags are supported by the active compiler and add them to env.LINKFLAGS variable.
Shock Jiang2d8483c2014-10-30 10:15:37 -0700109 """
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800110 if len(linkflags) == 0:
111 return
112
Shock Jiang2d8483c2014-10-30 10:15:37 -0700113 self.start_msg('Checking supported LINKFLAGS')
114
115 supportedFlags = []
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400116 for flags in linkflags:
117 flags = Utils.to_list(flags)
118 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
119 supportedFlags += flags
Shock Jiang2d8483c2014-10-30 10:15:37 -0700120
121 self.end_msg(' '.join(supportedFlags))
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -0800122 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800123
124
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400125class CompilerFlags:
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400126 def getCompilerVersion(self, conf):
127 return tuple(int(i) for i in conf.env.CC_VERSION)
128
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800129 def getGeneralFlags(self, conf):
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -0800130 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Davide Pesaventoce6f03b2024-02-10 17:04:11 -0500131 return {
132 'CXXFLAGS': [],
133 'LINKFLAGS': [],
134 'DEFINES': ['BOOST_FILESYSTEM_NO_DEPRECATED'],
135 }
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800136
137 def getDebugFlags(self, conf):
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -0800138 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Davide Pesaventoce6f03b2024-02-10 17:04:11 -0500139 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800140
141 def getOptimizedFlags(self, conf):
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -0800142 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800143 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
144
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400145
146class GccClangCommonFlags(CompilerFlags):
Alexander Afanasyevc7c99002015-10-09 17:27:30 -0700147 """
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400148 This class defines common flags that work for both gcc and clang compilers.
Alexander Afanasyevc7c99002015-10-09 17:27:30 -0700149 """
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400150
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400151 def getGeneralFlags(self, conf):
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400152 flags = super().getGeneralFlags(conf)
Davide Pesavento38fd3982022-04-18 22:22:02 -0400153 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesaventob2034db2022-11-16 17:21:22 -0500154 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesaventoc7888e62019-01-29 15:20:32 -0500155 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400156 return flags
157
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400158 __cxxFlags = [
159 '-fdiagnostics-color',
160 '-Wall',
161 '-Wextra',
162 '-Wpedantic',
163 '-Wenum-conversion',
164 '-Wextra-semi',
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400165 '-Wno-unused-parameter',
166 ]
167 __linkFlags = ['-Wl,-O1']
168
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800169 def getDebugFlags(self, conf):
Davide Pesaventoaf340ea2023-08-10 20:46:06 -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 Pesaventoce6f03b2024-02-10 17:04:11 -0500177 # Enable assertions in libstdc++
178 # https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
179 flags['DEFINES'] += ['_GLIBCXX_ASSERTIONS=1']
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800180 return flags
181
182 def getOptimizedFlags(self, conf):
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400183 flags = super().getOptimizedFlags(conf)
184 flags['CXXFLAGS'] += ['-O2', '-g1'] + self.__cxxFlags
185 flags['LINKFLAGS'] += self.__linkFlags
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800186 return flags
187
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400188
189class 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 Afanasyev83b7c2f2015-01-23 13:08:34 -0800200 def getDebugFlags(self, conf):
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400201 flags = super().getDebugFlags(conf)
202 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento7f27ec12022-03-10 20:10:54 -0500203 if platform.machine() == 'armv7l':
Davide Pesavento3d01fa32021-10-03 17:13:38 -0400204 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevc7c99002015-10-09 17:27:30 -0700205 return flags
206
207 def getOptimizedFlags(self, conf):
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400208 flags = super().getOptimizedFlags(conf)
209 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento7f27ec12022-03-10 20:10:54 -0500210 if platform.machine() == 'armv7l':
Davide Pesavento3d01fa32021-10-03 17:13:38 -0400211 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800212 return flags
213
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400214
215class ClangFlags(GccClangCommonFlags):
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800216 def getGeneralFlags(self, conf):
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400217 flags = super().getGeneralFlags(conf)
Davide Pesavento3d01fa32021-10-03 17:13:38 -0400218 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400219 # Bug #4296
Davide Pesavento423553e2022-08-19 20:46:06 -0400220 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400221 flags['CXXFLAGS'] += [
222 ['-isystem', f'{brewdir}/include'], # for Homebrew
223 ['-isystem', '/opt/local/include'], # for MacPorts
224 ]
Davide Pesavento3d01fa32021-10-03 17:13:38 -0400225 elif Utils.unversioned_sys_platform() == 'freebsd':
226 # Bug #4790
227 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Davide Pesaventoce6f03b2024-02-10 17:04:11 -0500228 if self.getCompilerVersion(conf) >= (18, 0, 0):
229 # Bug #5300
230 flags['CXXFLAGS'] += ['-Wno-enum-constexpr-conversion']
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800231 return flags
232
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400233 __cxxFlags = [
234 '-Wundefined-func-template',
235 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
236 ]
237
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800238 def getDebugFlags(self, conf):
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400239 flags = super().getDebugFlags(conf)
240 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventoce6f03b2024-02-10 17:04:11 -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']
Alexander Afanasyevc7c99002015-10-09 17:27:30 -0700248 return flags
249
250 def getOptimizedFlags(self, conf):
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400251 flags = super().getOptimizedFlags(conf)
252 flags['CXXFLAGS'] += self.__cxxFlags
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800253 return flags