blob: 9899d05b0b2c38eca520c67024e9816b5509a51f [file] [log] [blame]
Davide Pesaventob07d7a92020-05-13 23:30:07 -04001import platform
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -05002from waflib import Configure, Logs, Utils
Junxiao Shif7191242015-03-19 05:53:41 -07003
Davide Pesaventodd808b02023-08-06 16:00:02 -04004
Junxiao Shif7191242015-03-19 05:53:41 -07005def options(opt):
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -05006 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesaventob07d7a92020-05-13 23:30:07 -04007 help='Compile in debugging mode with minimal optimizations (-Og)')
Junxiao Shif7191242015-03-19 05:53:41 -07008
Davide Pesaventodd808b02023-08-06 16:00:02 -04009
Junxiao Shif7191242015-03-19 05:53:41 -070010def configure(conf):
Davide Pesaventoc0702702017-08-24 22:04:00 -040011 conf.start_msg('Checking C++ compiler version')
12
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -050013 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)
Davide Pesaventoc0702702017-08-24 22:04:00 -040016 errmsg = ''
17 warnmsg = ''
Junxiao Shif7191242015-03-19 05:53:41 -070018 if cxx == 'gcc':
Davide Pesavento8357bbe2024-12-10 20:44:04 -050019 if ccver < (9, 1, 0):
Davide Pesaventoc0702702017-08-24 22:04:00 -040020 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesavento8357bbe2024-12-10 20:44:04 -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 Pesaventoad266072023-03-07 21:10:55 -050024 'officially supported and may result in build failures.')
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040025 conf.flags = GccFlags()
Junxiao Shif7191242015-03-19 05:53:41 -070026 elif cxx == 'clang':
Davide Pesaventob04c52b2022-02-20 15:20:02 -050027 if Utils.unversioned_sys_platform() == 'darwin':
Davide Pesavento8357bbe2024-12-10 20:44:04 -050028 if ccver < (11, 0, 0):
Davide Pesaventob04c52b2022-02-20 15:20:02 -050029 errmsg = ('The version of Xcode you are using is too old.\n'
Davide Pesavento5b71cfa2024-06-07 17:44:06 -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 Pesaventob04c52b2022-02-20 15:20:02 -050033 'officially supported and may result in build failures.')
Davide Pesaventoad266072023-03-07 21:10:55 -050034 elif ccver < (7, 0, 0):
Davide Pesaventoc0702702017-08-24 22:04:00 -040035 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesavento8357bbe2024-12-10 20:44:04 -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 Afanasyev11e74eb2017-09-21 19:01:54 -040040 conf.flags = ClangFlags()
Junxiao Shif7191242015-03-19 05:53:41 -070041 else:
Davide Pesavento423e58a2022-08-12 15:51:42 -040042 warnmsg = f'{cxx} compiler is unsupported'
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040043 conf.flags = CompilerFlags()
Davide Pesaventoc0702702017-08-24 22:04:00 -040044
45 if errmsg:
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -050046 conf.end_msg(ccverstr, color='RED')
Davide Pesaventoc0702702017-08-24 22:04:00 -040047 conf.fatal(errmsg)
48 elif warnmsg:
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -050049 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesaventob07d7a92020-05-13 23:30:07 -040050 Logs.warn('WARNING: ' + warnmsg)
Davide Pesaventoc0702702017-08-24 22:04:00 -040051 else:
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -050052 conf.end_msg(ccverstr)
Junxiao Shif7191242015-03-19 05:53:41 -070053
Davide Pesaventodd808b02023-08-06 16:00:02 -040054 conf.areCustomCxxflagsPresent = len(conf.env.CXXFLAGS) > 0
Junxiao Shif7191242015-03-19 05:53:41 -070055
Davide Pesavento60f8cc12018-05-10 22:05:21 -040056 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040057 generalFlags = conf.flags.getGeneralFlags(conf)
Junxiao Shif7191242015-03-19 05:53:41 -070058 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
59 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
60 conf.env.DEFINES += generalFlags['DEFINES']
61
Davide Pesaventodd808b02023-08-06 16:00:02 -040062
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040063@Configure.conf
64def check_compiler_flags(conf):
Junxiao Shic8a0a252015-10-05 07:25:02 -070065 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Junxiao Shif7191242015-03-19 05:53:41 -070066 # corresponding environment variables are not set.
Junxiao Shic8a0a252015-10-05 07:25:02 -070067 # DEFINES are always applied.
Junxiao Shif7191242015-03-19 05:53:41 -070068 if conf.options.debug:
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040069 extraFlags = conf.flags.getDebugFlags(conf)
70 if conf.areCustomCxxflagsPresent:
Junxiao Shif7191242015-03-19 05:53:41 -070071 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -050072 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))
Junxiao Shif7191242015-03-19 05:53:41 -070076 else:
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040077 extraFlags = conf.flags.getOptimizedFlags(conf)
Junxiao Shif7191242015-03-19 05:53:41 -070078
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040079 if not conf.areCustomCxxflagsPresent:
Junxiao Shif7191242015-03-19 05:53:41 -070080 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Davide Pesavento3e79c9c2015-11-01 20:38:28 +010081 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Junxiao Shif7191242015-03-19 05:53:41 -070082
83 conf.env.DEFINES += extraFlags['DEFINES']
84
Davide Pesaventodd808b02023-08-06 16:00:02 -040085
Junxiao Shif7191242015-03-19 05:53:41 -070086@Configure.conf
87def add_supported_cxxflags(self, cxxflags):
88 """
Davide Pesavento8148cd42023-08-06 14:01:32 -040089 Check which cxxflags are supported by the active compiler and add them to env.CXXFLAGS variable.
Junxiao Shif7191242015-03-19 05:53:41 -070090 """
91 if len(cxxflags) == 0:
92 return
93
94 self.start_msg('Checking supported CXXFLAGS')
95
96 supportedFlags = []
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040097 for flags in cxxflags:
98 flags = Utils.to_list(flags)
99 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
100 supportedFlags += flags
Junxiao Shif7191242015-03-19 05:53:41 -0700101
102 self.end_msg(' '.join(supportedFlags))
Davide Pesavento89d91752016-08-14 11:34:09 +0200103 self.env.prepend_value('CXXFLAGS', supportedFlags)
Junxiao Shif7191242015-03-19 05:53:41 -0700104
Davide Pesaventodd808b02023-08-06 16:00:02 -0400105
Junxiao Shif7191242015-03-19 05:53:41 -0700106@Configure.conf
107def add_supported_linkflags(self, linkflags):
108 """
Davide Pesavento8148cd42023-08-06 14:01:32 -0400109 Check which linkflags are supported by the active compiler and add them to env.LINKFLAGS variable.
Junxiao Shif7191242015-03-19 05:53:41 -0700110 """
111 if len(linkflags) == 0:
112 return
113
114 self.start_msg('Checking supported LINKFLAGS')
115
116 supportedFlags = []
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -0400117 for flags in linkflags:
118 flags = Utils.to_list(flags)
119 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
120 supportedFlags += flags
Junxiao Shif7191242015-03-19 05:53:41 -0700121
122 self.end_msg(' '.join(supportedFlags))
Davide Pesavento89d91752016-08-14 11:34:09 +0200123 self.env.prepend_value('LINKFLAGS', supportedFlags)
Junxiao Shif7191242015-03-19 05:53:41 -0700124
125
Davide Pesaventodd808b02023-08-06 16:00:02 -0400126class CompilerFlags:
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -0500127 def getCompilerVersion(self, conf):
128 return tuple(int(i) for i in conf.env.CC_VERSION)
129
Junxiao Shif7191242015-03-19 05:53:41 -0700130 def getGeneralFlags(self, conf):
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100131 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Davide Pesaventof6d75992024-02-27 19:56:06 -0500132 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 Pesavento7e9d7e42023-11-11 15:00:03 -0500136 return {
137 'CXXFLAGS': [],
138 'LINKFLAGS': [],
139 'DEFINES': ['BOOST_ASIO_NO_DEPRECATED', 'BOOST_FILESYSTEM_NO_DEPRECATED'],
140 }
Junxiao Shif7191242015-03-19 05:53:41 -0700141
Junxiao Shif7191242015-03-19 05:53:41 -0700142 def getOptimizedFlags(self, conf):
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100143 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Junxiao Shif7191242015-03-19 05:53:41 -0700144 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
145
Davide Pesaventodd808b02023-08-06 16:00:02 -0400146
147class GccClangCommonFlags(CompilerFlags):
Junxiao Shic8a0a252015-10-05 07:25:02 -0700148 """
Davide Pesavento8148cd42023-08-06 14:01:32 -0400149 This class defines common flags that work for both gcc and clang compilers.
Junxiao Shic8a0a252015-10-05 07:25:02 -0700150 """
Davide Pesaventodd808b02023-08-06 16:00:02 -0400151
Davide Pesaventodd0e1952017-06-24 13:37:13 -0400152 def getGeneralFlags(self, conf):
Davide Pesaventodd808b02023-08-06 16:00:02 -0400153 flags = super().getGeneralFlags(conf)
Davide Pesaventob3570c62022-02-19 19:19:00 -0500154 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesavento3d7b0332022-10-05 15:30:02 -0400155 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesaventoe1b18a62018-12-19 01:17:42 -0500156 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Davide Pesaventodd0e1952017-06-24 13:37:13 -0400157 return flags
158
Davide Pesaventodd808b02023-08-06 16:00:02 -0400159 __cxxFlags = [
160 '-fdiagnostics-color',
161 '-Wall',
162 '-Wextra',
163 '-Wpedantic',
164 '-Wenum-conversion',
165 '-Wextra-semi',
Davide Pesaventodd808b02023-08-06 16:00:02 -0400166 '-Wno-unused-parameter',
167 ]
168 __linkFlags = ['-Wl,-O1']
169
Junxiao Shif7191242015-03-19 05:53:41 -0700170 def getDebugFlags(self, conf):
Davide Pesaventodd808b02023-08-06 16:00:02 -0400171 flags = super().getDebugFlags(conf)
172 flags['CXXFLAGS'] += ['-Og', '-g'] + self.__cxxFlags + [
173 '-Werror',
174 '-Wno-error=deprecated-declarations', # Bug #3795
175 '-Wno-error=maybe-uninitialized', # Bug #1615
176 ]
177 flags['LINKFLAGS'] += self.__linkFlags
Davide Pesaventoa9d7db12024-01-15 00:30:05 -0500178 # Enable assertions in libstdc++
179 # https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
180 flags['DEFINES'] += ['_GLIBCXX_ASSERTIONS=1']
Junxiao Shif7191242015-03-19 05:53:41 -0700181 return flags
182
183 def getOptimizedFlags(self, conf):
Davide Pesaventodd808b02023-08-06 16:00:02 -0400184 flags = super().getOptimizedFlags(conf)
185 flags['CXXFLAGS'] += ['-O2', '-g1'] + self.__cxxFlags
186 flags['LINKFLAGS'] += self.__linkFlags
Junxiao Shif7191242015-03-19 05:53:41 -0700187 return flags
188
Davide Pesaventodd808b02023-08-06 16:00:02 -0400189
190class GccFlags(GccClangCommonFlags):
191 __cxxFlags = [
192 '-Wcatch-value=2',
193 '-Wcomma-subscript', # enabled by default in C++20
194 '-Wduplicated-branches',
195 '-Wduplicated-cond',
196 '-Wlogical-op',
197 '-Wredundant-tags',
198 '-Wvolatile', # enabled by default in C++20
199 ]
200
Junxiao Shif7191242015-03-19 05:53:41 -0700201 def getDebugFlags(self, conf):
Davide Pesaventodd808b02023-08-06 16:00:02 -0400202 flags = super().getDebugFlags(conf)
203 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventob04c52b2022-02-20 15:20:02 -0500204 if platform.machine() == 'armv7l':
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400205 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Junxiao Shic8a0a252015-10-05 07:25:02 -0700206 return flags
207
208 def getOptimizedFlags(self, conf):
Davide Pesaventodd808b02023-08-06 16:00:02 -0400209 flags = super().getOptimizedFlags(conf)
210 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventob04c52b2022-02-20 15:20:02 -0500211 if platform.machine() == 'armv7l':
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400212 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Junxiao Shif7191242015-03-19 05:53:41 -0700213 return flags
214
Davide Pesaventodd808b02023-08-06 16:00:02 -0400215
216class ClangFlags(GccClangCommonFlags):
Junxiao Shif7191242015-03-19 05:53:41 -0700217 def getGeneralFlags(self, conf):
Davide Pesaventodd808b02023-08-06 16:00:02 -0400218 flags = super().getGeneralFlags(conf)
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400219 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -0500220 # Bug #4296
Davide Pesavento423e58a2022-08-12 15:51:42 -0400221 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
Davide Pesaventodd808b02023-08-06 16:00:02 -0400222 flags['CXXFLAGS'] += [
223 ['-isystem', f'{brewdir}/include'], # for Homebrew
224 ['-isystem', '/opt/local/include'], # for MacPorts
225 ]
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400226 elif Utils.unversioned_sys_platform() == 'freebsd':
227 # Bug #4790
228 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Davide Pesavento0a675562024-01-13 21:54:52 -0500229 if self.getCompilerVersion(conf) >= (18, 0, 0):
230 # Bug #5300
231 flags['CXXFLAGS'] += ['-Wno-enum-constexpr-conversion']
Junxiao Shif7191242015-03-19 05:53:41 -0700232 return flags
233
Davide Pesaventodd808b02023-08-06 16:00:02 -0400234 __cxxFlags = [
235 '-Wundefined-func-template',
236 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
237 ]
238
Junxiao Shif7191242015-03-19 05:53:41 -0700239 def getDebugFlags(self, conf):
Davide Pesaventodd808b02023-08-06 16:00:02 -0400240 flags = super().getDebugFlags(conf)
241 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventoa9d7db12024-01-15 00:30:05 -0500242 # Enable assertions in libc++
243 if self.getCompilerVersion(conf) >= (18, 0, 0):
244 # https://libcxx.llvm.org/Hardening.html
245 flags['DEFINES'] += ['_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE']
246 elif self.getCompilerVersion(conf) >= (15, 0, 0):
247 # https://releases.llvm.org/15.0.0/projects/libcxx/docs/UsingLibcxx.html#enabling-the-safe-libc-mode
248 flags['DEFINES'] += ['_LIBCPP_ENABLE_ASSERTIONS=1']
Davide Pesaventof6d75992024-02-27 19:56:06 -0500249 # Tell libc++ to avoid including transitive headers
250 # https://libcxx.llvm.org/DesignDocs/HeaderRemovalPolicy.html
251 flags['DEFINES'] += ['_LIBCPP_REMOVE_TRANSITIVE_INCLUDES=1']
Junxiao Shic8a0a252015-10-05 07:25:02 -0700252 return flags
253
254 def getOptimizedFlags(self, conf):
Davide Pesaventodd808b02023-08-06 16:00:02 -0400255 flags = super().getOptimizedFlags(conf)
256 flags['CXXFLAGS'] += self.__cxxFlags
Junxiao Shif7191242015-03-19 05:53:41 -0700257 return flags