blob: 3c724fe4eacb16b339af61e176c40641c3bb618d [file] [log] [blame]
Davide Pesavento042dfb32020-07-23 21:07:16 -04001import platform
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -05002from waflib import Configure, Logs, Utils
3
Davide Pesavento822c5792023-08-16 15:06:23 -04004
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -05005def options(opt):
6 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesavento042dfb32020-07-23 21:07:16 -04007 help='Compile in debugging mode with minimal optimizations (-Og)')
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -05008
Davide Pesavento822c5792023-08-16 15:06:23 -04009
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050010def configure(conf):
11 conf.start_msg('Checking C++ compiler version')
12
13 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)
16 errmsg = ''
17 warnmsg = ''
18 if cxx == 'gcc':
Davide Pesaventof4571e82024-12-14 15:15:34 -050019 if ccver < (9, 1, 0):
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050020 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesaventof4571e82024-12-14 15:15:34 -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 Pesavento88b7bbd2023-04-26 15:48:02 -040024 'officially supported and may result in build failures.')
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050025 conf.flags = GccFlags()
26 elif cxx == 'clang':
Davide Pesavento85a73d22022-03-06 16:00:01 -050027 if Utils.unversioned_sys_platform() == 'darwin':
Davide Pesaventof4571e82024-12-14 15:15:34 -050028 if ccver < (11, 0, 0):
Davide Pesavento85a73d22022-03-06 16:00:01 -050029 errmsg = ('The version of Xcode you are using is too old.\n'
Davide Pesavento78926bd2024-07-13 17:21:26 -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 Pesavento85a73d22022-03-06 16:00:01 -050033 'officially supported and may result in build failures.')
Davide Pesavento88b7bbd2023-04-26 15:48:02 -040034 elif ccver < (7, 0, 0):
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050035 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesaventof4571e82024-12-14 15:15:34 -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.')
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050040 conf.flags = ClangFlags()
41 else:
Davide Pesaventoda1a4d32022-08-19 19:03:24 -040042 warnmsg = f'{cxx} compiler is unsupported'
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050043 conf.flags = CompilerFlags()
44
45 if errmsg:
46 conf.end_msg(ccverstr, color='RED')
47 conf.fatal(errmsg)
48 elif warnmsg:
49 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento042dfb32020-07-23 21:07:16 -040050 Logs.warn('WARNING: ' + warnmsg)
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050051 else:
52 conf.end_msg(ccverstr)
53
Davide Pesavento822c5792023-08-16 15:06:23 -040054 conf.areCustomCxxflagsPresent = len(conf.env.CXXFLAGS) > 0
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050055
56 # General flags are always applied (e.g., selecting C++ language standard)
57 generalFlags = conf.flags.getGeneralFlags(conf)
58 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
59 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
60 conf.env.DEFINES += generalFlags['DEFINES']
61
Davide Pesavento822c5792023-08-16 15:06:23 -040062
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050063@Configure.conf
64def check_compiler_flags(conf):
65 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
66 # corresponding environment variables are not set.
67 # DEFINES are always applied.
68 if conf.options.debug:
69 extraFlags = conf.flags.getDebugFlags(conf)
70 if conf.areCustomCxxflagsPresent:
71 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
72 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))
76 else:
77 extraFlags = conf.flags.getOptimizedFlags(conf)
78
79 if not conf.areCustomCxxflagsPresent:
80 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
81 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
82
83 conf.env.DEFINES += extraFlags['DEFINES']
84
Davide Pesavento822c5792023-08-16 15:06:23 -040085
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050086@Configure.conf
87def add_supported_cxxflags(self, cxxflags):
88 """
Davide Pesavento822c5792023-08-16 15:06:23 -040089 Check which cxxflags are supported by the active compiler and add them to env.CXXFLAGS variable.
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050090 """
91 if len(cxxflags) == 0:
92 return
93
94 self.start_msg('Checking supported CXXFLAGS')
95
96 supportedFlags = []
97 for flags in cxxflags:
98 flags = Utils.to_list(flags)
99 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
100 supportedFlags += flags
101
102 self.end_msg(' '.join(supportedFlags))
103 self.env.prepend_value('CXXFLAGS', supportedFlags)
104
Davide Pesavento822c5792023-08-16 15:06:23 -0400105
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500106@Configure.conf
107def add_supported_linkflags(self, linkflags):
108 """
Davide Pesavento822c5792023-08-16 15:06:23 -0400109 Check which linkflags are supported by the active compiler and add them to env.LINKFLAGS variable.
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500110 """
111 if len(linkflags) == 0:
112 return
113
114 self.start_msg('Checking supported LINKFLAGS')
115
116 supportedFlags = []
117 for flags in linkflags:
118 flags = Utils.to_list(flags)
119 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
120 supportedFlags += flags
121
122 self.end_msg(' '.join(supportedFlags))
123 self.env.prepend_value('LINKFLAGS', supportedFlags)
124
125
Davide Pesavento822c5792023-08-16 15:06:23 -0400126class CompilerFlags:
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500127 def getCompilerVersion(self, conf):
128 return tuple(int(i) for i in conf.env.CC_VERSION)
129
130 def getGeneralFlags(self, conf):
131 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Davide Pesavento8c7c2282024-03-13 18:32:32 -0400132 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 Pesavento60e50432023-11-11 18:35:05 -0500136 return {
137 'CXXFLAGS': [],
138 'LINKFLAGS': [],
Davide Pesaventof4571e82024-12-14 15:15:34 -0500139 'DEFINES': ['BOOST_ASIO_NO_DEPRECATED'],
Davide Pesavento60e50432023-11-11 18:35:05 -0500140 }
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500141
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500142 def getOptimizedFlags(self, conf):
143 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
144 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
145
Davide Pesavento822c5792023-08-16 15:06:23 -0400146
147class GccClangCommonFlags(CompilerFlags):
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500148 """
Davide Pesavento822c5792023-08-16 15:06:23 -0400149 This class defines common flags that work for both gcc and clang compilers.
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500150 """
Davide Pesavento822c5792023-08-16 15:06:23 -0400151
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500152 def getGeneralFlags(self, conf):
Davide Pesavento822c5792023-08-16 15:06:23 -0400153 flags = super().getGeneralFlags(conf)
Davide Pesaventoc407dee2022-07-21 23:56:05 -0400154 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesaventob68f2842022-11-17 19:07:04 -0500155 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesaventoda278492019-01-29 15:00:49 -0500156 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500157 return flags
158
Davide Pesavento822c5792023-08-16 15:06:23 -0400159 __cxxFlags = [
160 '-fdiagnostics-color',
161 '-Wall',
162 '-Wextra',
163 '-Wpedantic',
164 '-Wenum-conversion',
165 '-Wextra-semi',
166 '-Wnon-virtual-dtor',
167 '-Wno-unused-parameter',
168 ]
169 __linkFlags = ['-Wl,-O1']
170
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500171 def getDebugFlags(self, conf):
Davide Pesavento822c5792023-08-16 15:06:23 -0400172 flags = super().getDebugFlags(conf)
173 flags['CXXFLAGS'] += ['-Og', '-g'] + self.__cxxFlags + [
174 '-Werror',
175 '-Wno-error=deprecated-declarations', # Bug #3795
176 '-Wno-error=maybe-uninitialized', # Bug #1615
177 ]
178 flags['LINKFLAGS'] += self.__linkFlags
Davide Pesaventoac63e322024-01-19 00:30:21 -0500179 # Enable assertions in libstdc++
180 # https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
181 flags['DEFINES'] += ['_GLIBCXX_ASSERTIONS=1']
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500182 return flags
183
184 def getOptimizedFlags(self, conf):
Davide Pesavento822c5792023-08-16 15:06:23 -0400185 flags = super().getOptimizedFlags(conf)
186 flags['CXXFLAGS'] += ['-O2', '-g1'] + self.__cxxFlags
187 flags['LINKFLAGS'] += self.__linkFlags
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500188 return flags
189
Davide Pesavento822c5792023-08-16 15:06:23 -0400190
191class GccFlags(GccClangCommonFlags):
192 __cxxFlags = [
193 '-Wcatch-value=2',
194 '-Wcomma-subscript', # enabled by default in C++20
195 '-Wduplicated-branches',
196 '-Wduplicated-cond',
197 '-Wlogical-op',
198 '-Wredundant-tags',
199 '-Wvolatile', # enabled by default in C++20
200 ]
201
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500202 def getDebugFlags(self, conf):
Davide Pesavento822c5792023-08-16 15:06:23 -0400203 flags = super().getDebugFlags(conf)
204 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento85a73d22022-03-06 16:00:01 -0500205 if platform.machine() == 'armv7l':
Davide Pesavento042dfb32020-07-23 21:07:16 -0400206 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500207 return flags
208
209 def getOptimizedFlags(self, conf):
Davide Pesavento822c5792023-08-16 15:06:23 -0400210 flags = super().getOptimizedFlags(conf)
211 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento85a73d22022-03-06 16:00:01 -0500212 if platform.machine() == 'armv7l':
Davide Pesavento042dfb32020-07-23 21:07:16 -0400213 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500214 return flags
215
Davide Pesavento822c5792023-08-16 15:06:23 -0400216
217class ClangFlags(GccClangCommonFlags):
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500218 def getGeneralFlags(self, conf):
Davide Pesavento822c5792023-08-16 15:06:23 -0400219 flags = super().getGeneralFlags(conf)
Davide Pesavento042dfb32020-07-23 21:07:16 -0400220 if Utils.unversioned_sys_platform() == 'darwin':
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500221 # Bug #4296
Davide Pesaventoda1a4d32022-08-19 19:03:24 -0400222 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
Davide Pesavento822c5792023-08-16 15:06:23 -0400223 flags['CXXFLAGS'] += [
224 ['-isystem', f'{brewdir}/include'], # for Homebrew
225 ['-isystem', '/opt/local/include'], # for MacPorts
226 ]
Davide Pesavento042dfb32020-07-23 21:07:16 -0400227 elif Utils.unversioned_sys_platform() == 'freebsd':
228 # Bug #4790
229 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Davide Pesaventoac63e322024-01-19 00:30:21 -0500230 if self.getCompilerVersion(conf) >= (18, 0, 0):
231 # Bug #5300
232 flags['CXXFLAGS'] += ['-Wno-enum-constexpr-conversion']
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500233 return flags
234
Davide Pesavento822c5792023-08-16 15:06:23 -0400235 __cxxFlags = [
236 '-Wundefined-func-template',
237 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
238 ]
239
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500240 def getDebugFlags(self, conf):
Davide Pesavento822c5792023-08-16 15:06:23 -0400241 flags = super().getDebugFlags(conf)
242 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventoac63e322024-01-19 00:30:21 -0500243 # Enable assertions in libc++
244 if self.getCompilerVersion(conf) >= (18, 0, 0):
245 # https://libcxx.llvm.org/Hardening.html
246 flags['DEFINES'] += ['_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE']
247 elif self.getCompilerVersion(conf) >= (15, 0, 0):
248 # https://releases.llvm.org/15.0.0/projects/libcxx/docs/UsingLibcxx.html#enabling-the-safe-libc-mode
249 flags['DEFINES'] += ['_LIBCPP_ENABLE_ASSERTIONS=1']
Davide Pesavento8c7c2282024-03-13 18:32:32 -0400250 # Tell libc++ to avoid including transitive headers
251 # https://libcxx.llvm.org/DesignDocs/HeaderRemovalPolicy.html
252 flags['DEFINES'] += ['_LIBCPP_REMOVE_TRANSITIVE_INCLUDES=1']
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500253 return flags
254
255 def getOptimizedFlags(self, conf):
Davide Pesavento822c5792023-08-16 15:06:23 -0400256 flags = super().getOptimizedFlags(conf)
257 flags['CXXFLAGS'] += self.__cxxFlags
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500258 return flags