blob: bac1893d5ee2c47a93624098bd61ddc9a55932fd [file] [log] [blame]
Davide Pesavento80b50fb2021-10-07 02:04:11 -04001import platform
Alexander Afanasyev6e64ac92018-06-14 17:25:38 -04002from waflib import Configure, Logs, Utils
3
Davide Pesavento9e7520f2023-08-11 17:35:35 -04004
Alexander Afanasyev6e64ac92018-06-14 17:25:38 -04005def options(opt):
6 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesavento80b50fb2021-10-07 02:04:11 -04007 help='Compile in debugging mode with minimal optimizations (-Og)')
Alexander Afanasyev6e64ac92018-06-14 17:25:38 -04008
Davide Pesavento9e7520f2023-08-11 17:35:35 -04009
Alexander Afanasyev6e64ac92018-06-14 17:25:38 -040010def configure(conf):
11 conf.start_msg('Checking C++ compiler version')
12
13 cxx = conf.env.CXX_NAME # generic name of the compiler
Davide Pesavento28f045a2025-03-19 19:38:45 -040014 ccver = get_compiler_ver(conf)
Alexander Afanasyev6e64ac92018-06-14 17:25:38 -040015 ccverstr = '.'.join(conf.env.CC_VERSION)
16 errmsg = ''
17 warnmsg = ''
18 if cxx == 'gcc':
Davide Pesavento52dc9a62024-12-11 18:14:14 -050019 if ccver < (9, 1, 0):
Alexander Afanasyev6e64ac92018-06-14 17:25:38 -040020 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesavento52dc9a62024-12-11 18:14:14 -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 Pesavento46ba1e52023-04-17 02:36:33 -040024 'officially supported and may result in build failures.')
Alexander Afanasyev6e64ac92018-06-14 17:25:38 -040025 conf.flags = GccFlags()
26 elif cxx == 'clang':
Davide Pesaventoa0ae15d2022-03-09 18:10:23 -050027 if Utils.unversioned_sys_platform() == 'darwin':
Davide Pesavento52dc9a62024-12-11 18:14:14 -050028 if ccver < (11, 0, 0):
Davide Pesaventoa0ae15d2022-03-09 18:10:23 -050029 errmsg = ('The version of Xcode you are using is too old.\n'
Davide Pesaventofb0d7cd2024-07-21 14:06:02 -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 Pesaventoa0ae15d2022-03-09 18:10:23 -050033 'officially supported and may result in build failures.')
Davide Pesavento46ba1e52023-04-17 02:36:33 -040034 elif ccver < (7, 0, 0):
Alexander Afanasyev6e64ac92018-06-14 17:25:38 -040035 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesavento52dc9a62024-12-11 18:14:14 -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.')
Alexander Afanasyev6e64ac92018-06-14 17:25:38 -040040 conf.flags = ClangFlags()
41 else:
Davide Pesavento27dd70c2022-08-19 16:24:28 -040042 warnmsg = f'{cxx} compiler is unsupported'
Alexander Afanasyev6e64ac92018-06-14 17:25:38 -040043 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 Pesavento80b50fb2021-10-07 02:04:11 -040050 Logs.warn('WARNING: ' + warnmsg)
Alexander Afanasyev6e64ac92018-06-14 17:25:38 -040051 else:
52 conf.end_msg(ccverstr)
53
Davide Pesavento9e7520f2023-08-11 17:35:35 -040054 conf.areCustomCxxflagsPresent = len(conf.env.CXXFLAGS) > 0
Alexander Afanasyev6e64ac92018-06-14 17:25:38 -040055
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 Pesavento9e7520f2023-08-11 17:35:35 -040062
Davide Pesavento28f045a2025-03-19 19:38:45 -040063def get_compiler_ver(conf):
64 return tuple(int(i) for i in conf.env.CC_VERSION)
65
66
Alexander Afanasyev6e64ac92018-06-14 17:25:38 -040067@Configure.conf
68def check_compiler_flags(conf):
69 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
70 # corresponding environment variables are not set.
71 # DEFINES are always applied.
72 if conf.options.debug:
73 extraFlags = conf.flags.getDebugFlags(conf)
74 if conf.areCustomCxxflagsPresent:
75 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
76 if missingFlags:
77 Logs.warn('Selected debug mode, but CXXFLAGS is set to a custom value "%s"'
78 % ' '.join(conf.env.CXXFLAGS))
79 Logs.warn('Default flags "%s" will not be used' % ' '.join(missingFlags))
80 else:
81 extraFlags = conf.flags.getOptimizedFlags(conf)
82
83 if not conf.areCustomCxxflagsPresent:
84 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
85 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
86
87 conf.env.DEFINES += extraFlags['DEFINES']
88
Davide Pesavento9e7520f2023-08-11 17:35:35 -040089
Alexander Afanasyev6e64ac92018-06-14 17:25:38 -040090@Configure.conf
91def add_supported_cxxflags(self, cxxflags):
92 """
Davide Pesavento9e7520f2023-08-11 17:35:35 -040093 Check which cxxflags are supported by the active compiler and add them to env.CXXFLAGS variable.
Alexander Afanasyev6e64ac92018-06-14 17:25:38 -040094 """
95 if len(cxxflags) == 0:
96 return
97
98 self.start_msg('Checking supported CXXFLAGS')
99
100 supportedFlags = []
101 for flags in cxxflags:
102 flags = Utils.to_list(flags)
103 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
104 supportedFlags += flags
105
106 self.end_msg(' '.join(supportedFlags))
107 self.env.prepend_value('CXXFLAGS', supportedFlags)
108
Davide Pesavento9e7520f2023-08-11 17:35:35 -0400109
Alexander Afanasyev6e64ac92018-06-14 17:25:38 -0400110@Configure.conf
111def add_supported_linkflags(self, linkflags):
112 """
Davide Pesavento9e7520f2023-08-11 17:35:35 -0400113 Check which linkflags are supported by the active compiler and add them to env.LINKFLAGS variable.
Alexander Afanasyev6e64ac92018-06-14 17:25:38 -0400114 """
115 if len(linkflags) == 0:
116 return
117
118 self.start_msg('Checking supported LINKFLAGS')
119
120 supportedFlags = []
121 for flags in linkflags:
122 flags = Utils.to_list(flags)
123 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
124 supportedFlags += flags
125
126 self.end_msg(' '.join(supportedFlags))
127 self.env.prepend_value('LINKFLAGS', supportedFlags)
128
129
Davide Pesavento9e7520f2023-08-11 17:35:35 -0400130class CompilerFlags:
Alexander Afanasyev6e64ac92018-06-14 17:25:38 -0400131 def getGeneralFlags(self, conf):
132 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Davide Pesaventoecb2db12024-03-13 19:00:03 -0400133 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
134
135 def getDebugFlags(self, conf):
136 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Davide Pesavento74b3a7e2023-11-11 17:40:03 -0500137 return {
138 'CXXFLAGS': [],
139 'LINKFLAGS': [],
Davide Pesavento28f045a2025-03-19 19:38:45 -0400140 'DEFINES': ['BOOST_ASIO_NO_DEPRECATED'],
Davide Pesavento74b3a7e2023-11-11 17:40:03 -0500141 }
Alexander Afanasyev6e64ac92018-06-14 17:25:38 -0400142
Alexander Afanasyev6e64ac92018-06-14 17:25:38 -0400143 def getOptimizedFlags(self, conf):
144 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
145 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
146
Davide Pesavento9e7520f2023-08-11 17:35:35 -0400147
148class GccClangCommonFlags(CompilerFlags):
Alexander Afanasyev6e64ac92018-06-14 17:25:38 -0400149 """
Davide Pesavento9e7520f2023-08-11 17:35:35 -0400150 This class defines common flags that work for both gcc and clang compilers.
Alexander Afanasyev6e64ac92018-06-14 17:25:38 -0400151 """
Davide Pesavento9e7520f2023-08-11 17:35:35 -0400152
Alexander Afanasyev6e64ac92018-06-14 17:25:38 -0400153 def getGeneralFlags(self, conf):
Davide Pesavento9e7520f2023-08-11 17:35:35 -0400154 flags = super().getGeneralFlags(conf)
Davide Pesaventobde084f2022-04-17 00:21:35 -0400155 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesaventof5248012022-11-15 14:25:34 -0500156 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesavento0f4ff532019-01-29 15:38:25 -0500157 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Alexander Afanasyev6e64ac92018-06-14 17:25:38 -0400158 return flags
159
Davide Pesavento9e7520f2023-08-11 17:35:35 -0400160 __cxxFlags = [
161 '-fdiagnostics-color',
162 '-Wall',
163 '-Wextra',
164 '-Wpedantic',
165 '-Wenum-conversion',
166 '-Wextra-semi',
167 '-Wnon-virtual-dtor',
168 '-Wno-unused-parameter',
169 ]
170 __linkFlags = ['-Wl,-O1']
171
Alexander Afanasyev6e64ac92018-06-14 17:25:38 -0400172 def getDebugFlags(self, conf):
Davide Pesavento9e7520f2023-08-11 17:35:35 -0400173 flags = super().getDebugFlags(conf)
174 flags['CXXFLAGS'] += ['-Og', '-g'] + self.__cxxFlags + [
175 '-Werror',
176 '-Wno-error=deprecated-declarations', # Bug #3795
177 '-Wno-error=maybe-uninitialized', # Bug #1615
178 ]
179 flags['LINKFLAGS'] += self.__linkFlags
Davide Pesaventofad12642024-01-19 00:38:07 -0500180 # Enable assertions in libstdc++
181 # https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
182 flags['DEFINES'] += ['_GLIBCXX_ASSERTIONS=1']
Alexander Afanasyev6e64ac92018-06-14 17:25:38 -0400183 return flags
184
185 def getOptimizedFlags(self, conf):
Davide Pesavento9e7520f2023-08-11 17:35:35 -0400186 flags = super().getOptimizedFlags(conf)
187 flags['CXXFLAGS'] += ['-O2', '-g1'] + self.__cxxFlags
188 flags['LINKFLAGS'] += self.__linkFlags
Alexander Afanasyev6e64ac92018-06-14 17:25:38 -0400189 return flags
190
Davide Pesavento9e7520f2023-08-11 17:35:35 -0400191
192class GccFlags(GccClangCommonFlags):
193 __cxxFlags = [
194 '-Wcatch-value=2',
195 '-Wcomma-subscript', # enabled by default in C++20
196 '-Wduplicated-branches',
197 '-Wduplicated-cond',
198 '-Wlogical-op',
199 '-Wredundant-tags',
200 '-Wvolatile', # enabled by default in C++20
201 ]
202
Alexander Afanasyev6e64ac92018-06-14 17:25:38 -0400203 def getDebugFlags(self, conf):
Davide Pesavento9e7520f2023-08-11 17:35:35 -0400204 flags = super().getDebugFlags(conf)
205 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventoa0ae15d2022-03-09 18:10:23 -0500206 if platform.machine() == 'armv7l':
Davide Pesavento80b50fb2021-10-07 02:04:11 -0400207 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyev6e64ac92018-06-14 17:25:38 -0400208 return flags
209
210 def getOptimizedFlags(self, conf):
Davide Pesavento9e7520f2023-08-11 17:35:35 -0400211 flags = super().getOptimizedFlags(conf)
212 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventoa0ae15d2022-03-09 18:10:23 -0500213 if platform.machine() == 'armv7l':
Davide Pesavento80b50fb2021-10-07 02:04:11 -0400214 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyev6e64ac92018-06-14 17:25:38 -0400215 return flags
216
Davide Pesavento9e7520f2023-08-11 17:35:35 -0400217
218class ClangFlags(GccClangCommonFlags):
Alexander Afanasyev6e64ac92018-06-14 17:25:38 -0400219 def getGeneralFlags(self, conf):
Davide Pesavento9e7520f2023-08-11 17:35:35 -0400220 flags = super().getGeneralFlags(conf)
Davide Pesavento80b50fb2021-10-07 02:04:11 -0400221 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev6e64ac92018-06-14 17:25:38 -0400222 # Bug #4296
Davide Pesavento27dd70c2022-08-19 16:24:28 -0400223 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
Davide Pesavento9e7520f2023-08-11 17:35:35 -0400224 flags['CXXFLAGS'] += [
225 ['-isystem', f'{brewdir}/include'], # for Homebrew
226 ['-isystem', '/opt/local/include'], # for MacPorts
227 ]
Davide Pesavento80b50fb2021-10-07 02:04:11 -0400228 elif Utils.unversioned_sys_platform() == 'freebsd':
Davide Pesavento3c7f6452021-10-02 04:06:26 -0400229 # Bug #4790
230 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Davide Pesavento28f045a2025-03-19 19:38:45 -0400231 if get_compiler_ver(conf) >= (18, 0, 0) and get_compiler_ver(conf) < (20, 1, 0):
Davide Pesaventofad12642024-01-19 00:38:07 -0500232 # Bug #5300
233 flags['CXXFLAGS'] += ['-Wno-enum-constexpr-conversion']
Alexander Afanasyev6e64ac92018-06-14 17:25:38 -0400234 return flags
235
Davide Pesavento9e7520f2023-08-11 17:35:35 -0400236 __cxxFlags = [
237 '-Wundefined-func-template',
238 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
239 ]
240
Alexander Afanasyev6e64ac92018-06-14 17:25:38 -0400241 def getDebugFlags(self, conf):
Davide Pesavento9e7520f2023-08-11 17:35:35 -0400242 flags = super().getDebugFlags(conf)
243 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventofad12642024-01-19 00:38:07 -0500244 # Enable assertions in libc++
Davide Pesavento28f045a2025-03-19 19:38:45 -0400245 if get_compiler_ver(conf) >= (18, 0, 0):
Davide Pesaventofad12642024-01-19 00:38:07 -0500246 # https://libcxx.llvm.org/Hardening.html
247 flags['DEFINES'] += ['_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE']
Davide Pesavento28f045a2025-03-19 19:38:45 -0400248 elif get_compiler_ver(conf) >= (15, 0, 0):
Davide Pesaventofad12642024-01-19 00:38:07 -0500249 # https://releases.llvm.org/15.0.0/projects/libcxx/docs/UsingLibcxx.html#enabling-the-safe-libc-mode
250 flags['DEFINES'] += ['_LIBCPP_ENABLE_ASSERTIONS=1']
Davide Pesaventoecb2db12024-03-13 19:00:03 -0400251 # Tell libc++ to avoid including transitive headers
252 # https://libcxx.llvm.org/DesignDocs/HeaderRemovalPolicy.html
253 flags['DEFINES'] += ['_LIBCPP_REMOVE_TRANSITIVE_INCLUDES=1']
Alexander Afanasyev6e64ac92018-06-14 17:25:38 -0400254 return flags
255
256 def getOptimizedFlags(self, conf):
Davide Pesavento9e7520f2023-08-11 17:35:35 -0400257 flags = super().getOptimizedFlags(conf)
258 flags['CXXFLAGS'] += self.__cxxFlags
Alexander Afanasyev6e64ac92018-06-14 17:25:38 -0400259 return flags