blob: e412692782f29c5be5dcb1c31698055ff4466fe8 [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 Pesavento85a73d22022-03-06 16:00:01 -050019 if ccver < (7, 4, 0):
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050020 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesavento88b7bbd2023-04-26 15:48:02 -040021 '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.')
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':
28 if ccver < (10, 0, 0):
29 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 Pesavento88b7bbd2023-04-26 15:48:02 -040036 'The minimum supported clang version is 7.0.')
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050037 conf.flags = ClangFlags()
38 else:
Davide Pesaventoda1a4d32022-08-19 19:03:24 -040039 warnmsg = f'{cxx} compiler is unsupported'
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050040 conf.flags = CompilerFlags()
41
42 if errmsg:
43 conf.end_msg(ccverstr, color='RED')
44 conf.fatal(errmsg)
45 elif warnmsg:
46 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento042dfb32020-07-23 21:07:16 -040047 Logs.warn('WARNING: ' + warnmsg)
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050048 else:
49 conf.end_msg(ccverstr)
50
Davide Pesavento822c5792023-08-16 15:06:23 -040051 conf.areCustomCxxflagsPresent = len(conf.env.CXXFLAGS) > 0
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050052
53 # General flags are always applied (e.g., selecting C++ language standard)
54 generalFlags = conf.flags.getGeneralFlags(conf)
55 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
56 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
57 conf.env.DEFINES += generalFlags['DEFINES']
58
Davide Pesavento822c5792023-08-16 15:06:23 -040059
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050060@Configure.conf
61def check_compiler_flags(conf):
62 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
63 # corresponding environment variables are not set.
64 # DEFINES are always applied.
65 if conf.options.debug:
66 extraFlags = conf.flags.getDebugFlags(conf)
67 if conf.areCustomCxxflagsPresent:
68 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
69 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))
73 else:
74 extraFlags = conf.flags.getOptimizedFlags(conf)
75
76 if not conf.areCustomCxxflagsPresent:
77 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
78 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
79
80 conf.env.DEFINES += extraFlags['DEFINES']
81
Davide Pesavento822c5792023-08-16 15:06:23 -040082
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050083@Configure.conf
84def add_supported_cxxflags(self, cxxflags):
85 """
Davide Pesavento822c5792023-08-16 15:06:23 -040086 Check which cxxflags are supported by the active compiler and add them to env.CXXFLAGS variable.
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050087 """
88 if len(cxxflags) == 0:
89 return
90
91 self.start_msg('Checking supported CXXFLAGS')
92
93 supportedFlags = []
94 for flags in cxxflags:
95 flags = Utils.to_list(flags)
96 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
97 supportedFlags += flags
98
99 self.end_msg(' '.join(supportedFlags))
100 self.env.prepend_value('CXXFLAGS', supportedFlags)
101
Davide Pesavento822c5792023-08-16 15:06:23 -0400102
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500103@Configure.conf
104def add_supported_linkflags(self, linkflags):
105 """
Davide Pesavento822c5792023-08-16 15:06:23 -0400106 Check which linkflags are supported by the active compiler and add them to env.LINKFLAGS variable.
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500107 """
108 if len(linkflags) == 0:
109 return
110
111 self.start_msg('Checking supported LINKFLAGS')
112
113 supportedFlags = []
114 for flags in linkflags:
115 flags = Utils.to_list(flags)
116 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
117 supportedFlags += flags
118
119 self.end_msg(' '.join(supportedFlags))
120 self.env.prepend_value('LINKFLAGS', supportedFlags)
121
122
Davide Pesavento822c5792023-08-16 15:06:23 -0400123class CompilerFlags:
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500124 def getCompilerVersion(self, conf):
125 return tuple(int(i) for i in conf.env.CC_VERSION)
126
127 def getGeneralFlags(self, conf):
128 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Davide Pesavento8c7c2282024-03-13 18:32:32 -0400129 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 Pesavento60e50432023-11-11 18:35:05 -0500133 return {
134 'CXXFLAGS': [],
135 'LINKFLAGS': [],
136 'DEFINES': ['BOOST_ASIO_NO_DEPRECATED', 'BOOST_FILESYSTEM_NO_DEPRECATED'],
137 }
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500138
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500139 def getOptimizedFlags(self, conf):
140 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
141 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
142
Davide Pesavento822c5792023-08-16 15:06:23 -0400143
144class GccClangCommonFlags(CompilerFlags):
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500145 """
Davide Pesavento822c5792023-08-16 15:06:23 -0400146 This class defines common flags that work for both gcc and clang compilers.
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500147 """
Davide Pesavento822c5792023-08-16 15:06:23 -0400148
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500149 def getGeneralFlags(self, conf):
Davide Pesavento822c5792023-08-16 15:06:23 -0400150 flags = super().getGeneralFlags(conf)
Davide Pesaventoc407dee2022-07-21 23:56:05 -0400151 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesaventob68f2842022-11-17 19:07:04 -0500152 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesaventoda278492019-01-29 15:00:49 -0500153 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500154 return flags
155
Davide Pesavento822c5792023-08-16 15:06:23 -0400156 __cxxFlags = [
157 '-fdiagnostics-color',
158 '-Wall',
159 '-Wextra',
160 '-Wpedantic',
161 '-Wenum-conversion',
162 '-Wextra-semi',
163 '-Wnon-virtual-dtor',
164 '-Wno-unused-parameter',
165 ]
166 __linkFlags = ['-Wl,-O1']
167
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500168 def getDebugFlags(self, conf):
Davide Pesavento822c5792023-08-16 15:06:23 -0400169 flags = super().getDebugFlags(conf)
170 flags['CXXFLAGS'] += ['-Og', '-g'] + self.__cxxFlags + [
171 '-Werror',
172 '-Wno-error=deprecated-declarations', # Bug #3795
173 '-Wno-error=maybe-uninitialized', # Bug #1615
174 ]
175 flags['LINKFLAGS'] += self.__linkFlags
Davide Pesaventoac63e322024-01-19 00:30:21 -0500176 # Enable assertions in libstdc++
177 # https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
178 flags['DEFINES'] += ['_GLIBCXX_ASSERTIONS=1']
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500179 return flags
180
181 def getOptimizedFlags(self, conf):
Davide Pesavento822c5792023-08-16 15:06:23 -0400182 flags = super().getOptimizedFlags(conf)
183 flags['CXXFLAGS'] += ['-O2', '-g1'] + self.__cxxFlags
184 flags['LINKFLAGS'] += self.__linkFlags
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500185 return flags
186
Davide Pesavento822c5792023-08-16 15:06:23 -0400187
188class GccFlags(GccClangCommonFlags):
189 __cxxFlags = [
190 '-Wcatch-value=2',
191 '-Wcomma-subscript', # enabled by default in C++20
192 '-Wduplicated-branches',
193 '-Wduplicated-cond',
194 '-Wlogical-op',
195 '-Wredundant-tags',
196 '-Wvolatile', # enabled by default in C++20
197 ]
198
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500199 def getDebugFlags(self, conf):
Davide Pesavento822c5792023-08-16 15:06:23 -0400200 flags = super().getDebugFlags(conf)
201 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento85a73d22022-03-06 16:00:01 -0500202 if platform.machine() == 'armv7l':
Davide Pesavento042dfb32020-07-23 21:07:16 -0400203 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500204 return flags
205
206 def getOptimizedFlags(self, conf):
Davide Pesavento822c5792023-08-16 15:06:23 -0400207 flags = super().getOptimizedFlags(conf)
208 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento85a73d22022-03-06 16:00:01 -0500209 if platform.machine() == 'armv7l':
Davide Pesavento042dfb32020-07-23 21:07:16 -0400210 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500211 return flags
212
Davide Pesavento822c5792023-08-16 15:06:23 -0400213
214class ClangFlags(GccClangCommonFlags):
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500215 def getGeneralFlags(self, conf):
Davide Pesavento822c5792023-08-16 15:06:23 -0400216 flags = super().getGeneralFlags(conf)
Davide Pesavento042dfb32020-07-23 21:07:16 -0400217 if Utils.unversioned_sys_platform() == 'darwin':
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500218 # Bug #4296
Davide Pesaventoda1a4d32022-08-19 19:03:24 -0400219 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
Davide Pesavento822c5792023-08-16 15:06:23 -0400220 flags['CXXFLAGS'] += [
221 ['-isystem', f'{brewdir}/include'], # for Homebrew
222 ['-isystem', '/opt/local/include'], # for MacPorts
223 ]
Davide Pesavento042dfb32020-07-23 21:07:16 -0400224 elif Utils.unversioned_sys_platform() == 'freebsd':
225 # Bug #4790
226 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Davide Pesaventoac63e322024-01-19 00:30:21 -0500227 if self.getCompilerVersion(conf) >= (18, 0, 0):
228 # Bug #5300
229 flags['CXXFLAGS'] += ['-Wno-enum-constexpr-conversion']
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500230 return flags
231
Davide Pesavento822c5792023-08-16 15:06:23 -0400232 __cxxFlags = [
233 '-Wundefined-func-template',
234 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
235 ]
236
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500237 def getDebugFlags(self, conf):
Davide Pesavento822c5792023-08-16 15:06:23 -0400238 flags = super().getDebugFlags(conf)
239 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventoac63e322024-01-19 00:30:21 -0500240 # Enable assertions in libc++
241 if self.getCompilerVersion(conf) >= (18, 0, 0):
242 # https://libcxx.llvm.org/Hardening.html
243 flags['DEFINES'] += ['_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE']
244 elif self.getCompilerVersion(conf) >= (15, 0, 0):
245 # https://releases.llvm.org/15.0.0/projects/libcxx/docs/UsingLibcxx.html#enabling-the-safe-libc-mode
246 flags['DEFINES'] += ['_LIBCPP_ENABLE_ASSERTIONS=1']
Davide Pesavento8c7c2282024-03-13 18:32:32 -0400247 # Tell libc++ to avoid including transitive headers
248 # https://libcxx.llvm.org/DesignDocs/HeaderRemovalPolicy.html
249 flags['DEFINES'] += ['_LIBCPP_REMOVE_TRANSITIVE_INCLUDES=1']
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500250 return flags
251
252 def getOptimizedFlags(self, conf):
Davide Pesavento822c5792023-08-16 15:06:23 -0400253 flags = super().getOptimizedFlags(conf)
254 flags['CXXFLAGS'] += self.__cxxFlags
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500255 return flags