blob: ef97f25725ba750871fd0fc01c09711bcbc2c81d [file] [log] [blame]
Alexander Afanasyev740812e2014-10-30 15:37:45 -07001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
2
Davide Pesaventoab6ac5d2021-10-05 23:23:53 -04003import platform
Davide Pesavento64cafaa2018-05-24 00:50:41 -04004from waflib import Configure, Logs, Utils
Alexander Afanasyev740812e2014-10-30 15:37:45 -07005
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -04006
Alexander Afanasyev740812e2014-10-30 15:37:45 -07007def options(opt):
Davide Pesavento64cafaa2018-05-24 00:50:41 -04008 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesaventoab6ac5d2021-10-05 23:23:53 -04009 help='Compile in debugging mode with minimal optimizations (-Og)')
Alexander Afanasyev740812e2014-10-30 15:37:45 -070010
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -040011
Alexander Afanasyev740812e2014-10-30 15:37:45 -070012def configure(conf):
Davide Pesavento29db0fd2017-08-29 13:32:00 -040013 conf.start_msg('Checking C++ compiler version')
Alexander Afanasyev740812e2014-10-30 15:37:45 -070014
Davide Pesavento64cafaa2018-05-24 00:50:41 -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)
Davide Pesavento29db0fd2017-08-29 13:32:00 -040018 errmsg = ''
19 warnmsg = ''
20 if cxx == 'gcc':
Davide Pesavento92669062022-03-10 19:09:44 -050021 if ccver < (7, 4, 0):
Davide Pesavento29db0fd2017-08-29 13:32:00 -040022 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesavento08208492023-04-17 01:25:01 -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.')
Davide Pesavento64cafaa2018-05-24 00:50:41 -040027 conf.flags = GccFlags()
Davide Pesavento29db0fd2017-08-29 13:32:00 -040028 elif cxx == 'clang':
Davide Pesavento92669062022-03-10 19:09:44 -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 Pesavento08208492023-04-17 01:25:01 -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 Pesavento92669062022-03-10 19:09:44 -050035 'officially supported and may result in build failures.')
Davide Pesavento08208492023-04-17 01:25:01 -040036 elif ccver < (7, 0, 0):
Davide Pesavento29db0fd2017-08-29 13:32:00 -040037 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesavento08208492023-04-17 01:25:01 -040038 'The minimum supported clang version is 7.0.')
Davide Pesavento64cafaa2018-05-24 00:50:41 -040039 conf.flags = ClangFlags()
Davide Pesavento29db0fd2017-08-29 13:32:00 -040040 else:
Davide Pesaventof5006be2022-08-19 17:36:36 -040041 warnmsg = f'{cxx} compiler is unsupported'
Davide Pesavento64cafaa2018-05-24 00:50:41 -040042 conf.flags = CompilerFlags()
Davide Pesavento29db0fd2017-08-29 13:32:00 -040043
44 if errmsg:
Davide Pesavento64cafaa2018-05-24 00:50:41 -040045 conf.end_msg(ccverstr, color='RED')
Davide Pesavento29db0fd2017-08-29 13:32:00 -040046 conf.fatal(errmsg)
47 elif warnmsg:
Davide Pesavento64cafaa2018-05-24 00:50:41 -040048 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesaventoab6ac5d2021-10-05 23:23:53 -040049 Logs.warn('WARNING: ' + warnmsg)
Davide Pesavento29db0fd2017-08-29 13:32:00 -040050 else:
Davide Pesavento64cafaa2018-05-24 00:50:41 -040051 conf.end_msg(ccverstr)
Davide Pesavento29db0fd2017-08-29 13:32:00 -040052
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -040053 conf.areCustomCxxflagsPresent = len(conf.env.CXXFLAGS) > 0
Davide Pesavento29db0fd2017-08-29 13:32:00 -040054
Davide Pesavento64cafaa2018-05-24 00:50:41 -040055 # General flags are always applied (e.g., selecting C++ language standard)
56 generalFlags = conf.flags.getGeneralFlags(conf)
Davide Pesavento29db0fd2017-08-29 13:32:00 -040057 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
58 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
59 conf.env.DEFINES += generalFlags['DEFINES']
60
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -040061
Davide Pesavento64cafaa2018-05-24 00:50:41 -040062@Configure.conf
63def check_compiler_flags(conf):
Davide Pesavento29db0fd2017-08-29 13:32:00 -040064 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
65 # corresponding environment variables are not set.
66 # DEFINES are always applied.
Alexander Afanasyev740812e2014-10-30 15:37:45 -070067 if conf.options.debug:
Davide Pesavento64cafaa2018-05-24 00:50:41 -040068 extraFlags = conf.flags.getDebugFlags(conf)
69 if conf.areCustomCxxflagsPresent:
Davide Pesavento29db0fd2017-08-29 13:32:00 -040070 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Davide Pesavento64cafaa2018-05-24 00:50:41 -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 Afanasyev740812e2014-10-30 15:37:45 -070075 else:
Davide Pesavento64cafaa2018-05-24 00:50:41 -040076 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev740812e2014-10-30 15:37:45 -070077
Davide Pesavento64cafaa2018-05-24 00:50:41 -040078 if not conf.areCustomCxxflagsPresent:
Davide Pesavento29db0fd2017-08-29 13:32:00 -040079 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
80 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
81
82 conf.env.DEFINES += extraFlags['DEFINES']
Alexander Afanasyev740812e2014-10-30 15:37:45 -070083
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -040084
Alexander Afanasyev740812e2014-10-30 15:37:45 -070085@Configure.conf
86def add_supported_cxxflags(self, cxxflags):
87 """
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -040088 Check which cxxflags are supported by the active compiler and add them to env.CXXFLAGS variable.
Alexander Afanasyev740812e2014-10-30 15:37:45 -070089 """
Davide Pesavento29db0fd2017-08-29 13:32:00 -040090 if len(cxxflags) == 0:
91 return
92
Alexander Afanasyev740812e2014-10-30 15:37:45 -070093 self.start_msg('Checking supported CXXFLAGS')
94
95 supportedFlags = []
Davide Pesavento64cafaa2018-05-24 00:50:41 -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 Afanasyev740812e2014-10-30 15:37:45 -0700100
101 self.end_msg(' '.join(supportedFlags))
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400102 self.env.prepend_value('CXXFLAGS', supportedFlags)
Alexander Afanasyev740812e2014-10-30 15:37:45 -0700103
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -0400104
Alexander Afanasyev740812e2014-10-30 15:37:45 -0700105@Configure.conf
106def add_supported_linkflags(self, linkflags):
107 """
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -0400108 Check which linkflags are supported by the active compiler and add them to env.LINKFLAGS variable.
Alexander Afanasyev740812e2014-10-30 15:37:45 -0700109 """
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400110 if len(linkflags) == 0:
111 return
112
Alexander Afanasyev740812e2014-10-30 15:37:45 -0700113 self.start_msg('Checking supported LINKFLAGS')
114
115 supportedFlags = []
Davide Pesavento64cafaa2018-05-24 00:50:41 -0400116 for flags in linkflags:
117 flags = Utils.to_list(flags)
118 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
119 supportedFlags += flags
Alexander Afanasyev740812e2014-10-30 15:37:45 -0700120
121 self.end_msg(' '.join(supportedFlags))
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400122 self.env.prepend_value('LINKFLAGS', supportedFlags)
123
124
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -0400125class CompilerFlags:
Davide Pesavento64cafaa2018-05-24 00:50:41 -0400126 def getCompilerVersion(self, conf):
127 return tuple(int(i) for i in conf.env.CC_VERSION)
128
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400129 def getGeneralFlags(self, conf):
130 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Davide Pesavento06784b42023-11-11 18:10:43 -0500131 return {
132 'CXXFLAGS': [],
133 'LINKFLAGS': [],
134 'DEFINES': ['BOOST_ASIO_NO_DEPRECATED', 'BOOST_FILESYSTEM_NO_DEPRECATED'],
135 }
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400136
137 def getDebugFlags(self, conf):
138 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Davide Pesavento4c24cf72024-01-19 15:53:53 -0500139 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400140
141 def getOptimizedFlags(self, conf):
142 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
143 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
144
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -0400145
146class GccClangCommonFlags(CompilerFlags):
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400147 """
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -0400148 This class defines common flags that work for both gcc and clang compilers.
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400149 """
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -0400150
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400151 def getGeneralFlags(self, conf):
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -0400152 flags = super().getGeneralFlags(conf)
Davide Pesaventoef064892022-04-05 02:26:03 -0400153 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesaventof855ec12022-11-16 16:08:35 -0500154 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesavento7eaed5c2019-01-09 20:03:43 -0500155 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400156 return flags
157
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -0400158 __cxxFlags = [
159 '-fdiagnostics-color',
160 '-Wall',
161 '-Wextra',
162 '-Wpedantic',
163 '-Wenum-conversion',
164 '-Wextra-semi',
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -0400165 '-Wno-unused-parameter',
166 ]
167 __linkFlags = ['-Wl,-O1']
168
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400169 def getDebugFlags(self, conf):
Davide Pesaventoc1a7fbc2023-08-09 13:22: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 Pesavento4c24cf72024-01-19 15:53:53 -0500177 # Enable assertions in libstdc++
178 # https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
179 flags['DEFINES'] += ['_GLIBCXX_ASSERTIONS=1']
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400180 return flags
181
182 def getOptimizedFlags(self, conf):
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -0400183 flags = super().getOptimizedFlags(conf)
184 flags['CXXFLAGS'] += ['-O2', '-g1'] + self.__cxxFlags
185 flags['LINKFLAGS'] += self.__linkFlags
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400186 return flags
187
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -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
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400200 def getDebugFlags(self, conf):
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -0400201 flags = super().getDebugFlags(conf)
202 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento92669062022-03-10 19:09:44 -0500203 if platform.machine() == 'armv7l':
Davide Pesaventoab6ac5d2021-10-05 23:23:53 -0400204 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400205 return flags
206
207 def getOptimizedFlags(self, conf):
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -0400208 flags = super().getOptimizedFlags(conf)
209 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento92669062022-03-10 19:09:44 -0500210 if platform.machine() == 'armv7l':
Davide Pesaventoab6ac5d2021-10-05 23:23:53 -0400211 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400212 return flags
213
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -0400214
215class ClangFlags(GccClangCommonFlags):
Davide Pesavento64cafaa2018-05-24 00:50:41 -0400216 def getGeneralFlags(self, conf):
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -0400217 flags = super().getGeneralFlags(conf)
Davide Pesaventoab6ac5d2021-10-05 23:23:53 -0400218 if Utils.unversioned_sys_platform() == 'darwin':
Davide Pesavento64cafaa2018-05-24 00:50:41 -0400219 # Bug #4296
Davide Pesaventof5006be2022-08-19 17:36:36 -0400220 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -0400221 flags['CXXFLAGS'] += [
222 ['-isystem', f'{brewdir}/include'], # for Homebrew
223 ['-isystem', '/opt/local/include'], # for MacPorts
224 ]
Davide Pesaventoab6ac5d2021-10-05 23:23:53 -0400225 elif Utils.unversioned_sys_platform() == 'freebsd':
226 # Bug #4790
227 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Davide Pesavento4c24cf72024-01-19 15:53:53 -0500228 if self.getCompilerVersion(conf) >= (18, 0, 0):
229 # Bug #5300
230 flags['CXXFLAGS'] += ['-Wno-enum-constexpr-conversion']
Davide Pesavento64cafaa2018-05-24 00:50:41 -0400231 return flags
232
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -0400233 __cxxFlags = [
234 '-Wundefined-func-template',
235 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
236 ]
237
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400238 def getDebugFlags(self, conf):
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -0400239 flags = super().getDebugFlags(conf)
240 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento4c24cf72024-01-19 15:53:53 -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 Pesavento29db0fd2017-08-29 13:32:00 -0400248 return flags
249
250 def getOptimizedFlags(self, conf):
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -0400251 flags = super().getOptimizedFlags(conf)
252 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400253 return flags