blob: 3c724fe4eacb16b339af61e176c40641c3bb618d [file] [log] [blame]
Davide Pesavento71c622b2020-04-24 01:39:01 -04001import platform
Alexander Afanasyev5560fd42018-03-07 17:03:03 -05002from waflib import Configure, Logs, Utils
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -07003
Davide Pesavento12b8aaa2023-08-06 19:18:51 -04004
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -07005def options(opt):
Alexander Afanasyev5560fd42018-03-07 17:03:03 -05006 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesavento71c622b2020-04-24 01:39:01 -04007 help='Compile in debugging mode with minimal optimizations (-Og)')
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -07008
Davide Pesavento12b8aaa2023-08-06 19:18:51 -04009
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070010def configure(conf):
Davide Pesavento88a0d812017-08-19 21:31:42 -040011 conf.start_msg('Checking C++ compiler version')
12
Alexander Afanasyev5560fd42018-03-07 17:03:03 -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 Pesavento88a0d812017-08-19 21:31:42 -040016 errmsg = ''
17 warnmsg = ''
Alexander Afanasyev01515792014-12-16 14:20:01 -080018 if cxx == 'gcc':
Davide Pesaventoe913e3a2024-12-10 20:25:04 -050019 if ccver < (9, 1, 0):
Davide Pesavento88a0d812017-08-19 21:31:42 -040020 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesaventoe913e3a2024-12-10 20:25: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 Pesavento762548b2023-03-05 11:02:02 -050024 'officially supported and may result in build failures.')
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040025 conf.flags = GccFlags()
Alexander Afanasyev01515792014-12-16 14:20:01 -080026 elif cxx == 'clang':
Davide Pesavento541a8222022-03-01 15:08:42 -050027 if Utils.unversioned_sys_platform() == 'darwin':
Davide Pesaventoe913e3a2024-12-10 20:25:04 -050028 if ccver < (11, 0, 0):
Davide Pesavento541a8222022-03-01 15:08:42 -050029 errmsg = ('The version of Xcode you are using is too old.\n'
Davide Pesavento2be774b2024-06-07 17:25:31 -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 Pesavento541a8222022-03-01 15:08:42 -050033 'officially supported and may result in build failures.')
Davide Pesavento762548b2023-03-05 11:02:02 -050034 elif ccver < (7, 0, 0):
Davide Pesavento88a0d812017-08-19 21:31:42 -040035 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesaventoe913e3a2024-12-10 20:25: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 Afanasyevb82d8c32017-09-21 11:35:07 -040040 conf.flags = ClangFlags()
Alexander Afanasyev01515792014-12-16 14:20:01 -080041 else:
Davide Pesaventof6625002022-07-31 17:15:02 -040042 warnmsg = f'{cxx} compiler is unsupported'
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040043 conf.flags = CompilerFlags()
Davide Pesavento88a0d812017-08-19 21:31:42 -040044
45 if errmsg:
Alexander Afanasyev5560fd42018-03-07 17:03:03 -050046 conf.end_msg(ccverstr, color='RED')
Davide Pesavento88a0d812017-08-19 21:31:42 -040047 conf.fatal(errmsg)
48 elif warnmsg:
Alexander Afanasyev5560fd42018-03-07 17:03:03 -050049 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento71c622b2020-04-24 01:39:01 -040050 Logs.warn('WARNING: ' + warnmsg)
Davide Pesavento88a0d812017-08-19 21:31:42 -040051 else:
Alexander Afanasyev5560fd42018-03-07 17:03:03 -050052 conf.end_msg(ccverstr)
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -070053
Davide Pesavento12b8aaa2023-08-06 19:18:51 -040054 conf.areCustomCxxflagsPresent = len(conf.env.CXXFLAGS) > 0
Alexander Afanasyev01515792014-12-16 14:20:01 -080055
Davide Pesavento1fd00242018-05-20 00:11:01 -040056 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040057 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyev01515792014-12-16 14:20:01 -080058 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
59 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
60 conf.env.DEFINES += generalFlags['DEFINES']
61
Davide Pesavento12b8aaa2023-08-06 19:18:51 -040062
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040063@Configure.conf
64def check_compiler_flags(conf):
Davide Pesaventod06ea052015-10-02 01:48:24 +020065 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Alexander Afanasyev01515792014-12-16 14:20:01 -080066 # corresponding environment variables are not set.
Davide Pesaventod06ea052015-10-02 01:48:24 +020067 # DEFINES are always applied.
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070068 if conf.options.debug:
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040069 extraFlags = conf.flags.getDebugFlags(conf)
70 if conf.areCustomCxxflagsPresent:
Alexander Afanasyev01515792014-12-16 14:20:01 -080071 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev5560fd42018-03-07 17:03:03 -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))
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070076 else:
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040077 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070078
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040079 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyev01515792014-12-16 14:20:01 -080080 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Davide Pesavento7c02b472015-10-28 20:50:17 +010081 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Alexander Afanasyev01515792014-12-16 14:20:01 -080082
83 conf.env.DEFINES += extraFlags['DEFINES']
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020084
Davide Pesavento12b8aaa2023-08-06 19:18:51 -040085
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070086@Configure.conf
87def add_supported_cxxflags(self, cxxflags):
88 """
Davide Pesavento12b8aaa2023-08-06 19:18:51 -040089 Check which cxxflags are supported by the active compiler and add them to env.CXXFLAGS variable.
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070090 """
Alexander Afanasyev01515792014-12-16 14:20:01 -080091 if len(cxxflags) == 0:
92 return
93
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020094 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070095
96 supportedFlags = []
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040097 for flags in cxxflags:
98 flags = Utils.to_list(flags)
99 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
100 supportedFlags += flags
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -0700101
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -0700102 self.end_msg(' '.join(supportedFlags))
Davide Pesavento1349d2d2016-08-09 06:43:57 +0200103 self.env.prepend_value('CXXFLAGS', supportedFlags)
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +0200104
Davide Pesavento12b8aaa2023-08-06 19:18:51 -0400105
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +0200106@Configure.conf
107def add_supported_linkflags(self, linkflags):
108 """
Davide Pesavento12b8aaa2023-08-06 19:18:51 -0400109 Check which linkflags are supported by the active compiler and add them to env.LINKFLAGS variable.
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +0200110 """
Alexander Afanasyev01515792014-12-16 14:20:01 -0800111 if len(linkflags) == 0:
112 return
113
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +0200114 self.start_msg('Checking supported LINKFLAGS')
115
116 supportedFlags = []
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -0400117 for flags in linkflags:
118 flags = Utils.to_list(flags)
119 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
120 supportedFlags += flags
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +0200121
122 self.end_msg(' '.join(supportedFlags))
Davide Pesavento1349d2d2016-08-09 06:43:57 +0200123 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyev01515792014-12-16 14:20:01 -0800124
125
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400126class CompilerFlags:
Alexander Afanasyev5560fd42018-03-07 17:03:03 -0500127 def getCompilerVersion(self, conf):
128 return tuple(int(i) for i in conf.env.CC_VERSION)
129
Alexander Afanasyev01515792014-12-16 14:20:01 -0800130 def getGeneralFlags(self, conf):
Davide Pesavento7c02b472015-10-28 20:50:17 +0100131 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Davide Pesavento682b2af2024-02-23 21:25:41 -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 Pesavento2f46d652023-11-09 23:40:01 -0500136 return {
137 'CXXFLAGS': [],
138 'LINKFLAGS': [],
Davide Pesavento51974f62024-12-21 20:42:45 -0500139 'DEFINES': ['BOOST_ASIO_NO_DEPRECATED'],
Davide Pesavento2f46d652023-11-09 23:40:01 -0500140 }
Alexander Afanasyev01515792014-12-16 14:20:01 -0800141
Alexander Afanasyev01515792014-12-16 14:20:01 -0800142 def getOptimizedFlags(self, conf):
Davide Pesavento7c02b472015-10-28 20:50:17 +0100143 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Alexander Afanasyev01515792014-12-16 14:20:01 -0800144 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
145
Davide Pesavento12b8aaa2023-08-06 19:18:51 -0400146
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400147class GccClangCommonFlags(CompilerFlags):
Davide Pesaventod06ea052015-10-02 01:48:24 +0200148 """
Davide Pesavento12b8aaa2023-08-06 19:18:51 -0400149 This class defines common flags that work for both gcc and clang compilers.
Davide Pesaventod06ea052015-10-02 01:48:24 +0200150 """
Davide Pesavento12b8aaa2023-08-06 19:18:51 -0400151
Davide Pesavento724d65a2017-06-23 17:14:08 -0400152 def getGeneralFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400153 flags = super().getGeneralFlags(conf)
Davide Pesaventofcd3e442023-03-10 18:44:11 -0500154 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesaventoea5ce492022-09-30 20:25:25 -0400155 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesavento64ae55d2018-12-19 00:25:47 -0500156 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Davide Pesavento724d65a2017-06-23 17:14:08 -0400157 return flags
158
Davide Pesavento03fdfb12023-08-06 19:35:10 -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
Alexander Afanasyev01515792014-12-16 14:20:01 -0800171 def getDebugFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -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 Pesavento5fdab132024-01-14 16:30:06 -0500179 # Enable assertions in libstdc++
180 # https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
181 flags['DEFINES'] += ['_GLIBCXX_ASSERTIONS=1']
Alexander Afanasyev01515792014-12-16 14:20:01 -0800182 return flags
183
184 def getOptimizedFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400185 flags = super().getOptimizedFlags(conf)
186 flags['CXXFLAGS'] += ['-O2', '-g1'] + self.__cxxFlags
187 flags['LINKFLAGS'] += self.__linkFlags
Alexander Afanasyev01515792014-12-16 14:20:01 -0800188 return flags
189
Davide Pesavento12b8aaa2023-08-06 19:18:51 -0400190
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400191class 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
Alexander Afanasyev01515792014-12-16 14:20:01 -0800202 def getDebugFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400203 flags = super().getDebugFlags(conf)
204 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento541a8222022-03-01 15:08:42 -0500205 if platform.machine() == 'armv7l':
Davide Pesavento71c622b2020-04-24 01:39:01 -0400206 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyev01515792014-12-16 14:20:01 -0800207 return flags
208
Davide Pesaventod06ea052015-10-02 01:48:24 +0200209 def getOptimizedFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400210 flags = super().getOptimizedFlags(conf)
211 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento541a8222022-03-01 15:08:42 -0500212 if platform.machine() == 'armv7l':
Davide Pesavento71c622b2020-04-24 01:39:01 -0400213 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Davide Pesaventod06ea052015-10-02 01:48:24 +0200214 return flags
215
Davide Pesavento12b8aaa2023-08-06 19:18:51 -0400216
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400217class ClangFlags(GccClangCommonFlags):
Alexander Afanasyev01515792014-12-16 14:20:01 -0800218 def getGeneralFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400219 flags = super().getGeneralFlags(conf)
Davide Pesavento71c622b2020-04-24 01:39:01 -0400220 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev5560fd42018-03-07 17:03:03 -0500221 # Bug #4296
Davide Pesaventof6625002022-07-31 17:15:02 -0400222 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400223 flags['CXXFLAGS'] += [
224 ['-isystem', f'{brewdir}/include'], # for Homebrew
225 ['-isystem', '/opt/local/include'], # for MacPorts
226 ]
Davide Pesavento71c622b2020-04-24 01:39:01 -0400227 elif Utils.unversioned_sys_platform() == 'freebsd':
228 # Bug #4790
229 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Davide Pesaventoecfb2172024-01-13 18:26:18 -0500230 if self.getCompilerVersion(conf) >= (18, 0, 0):
231 # Bug #5300
232 flags['CXXFLAGS'] += ['-Wno-enum-constexpr-conversion']
Alexander Afanasyev01515792014-12-16 14:20:01 -0800233 return flags
234
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400235 __cxxFlags = [
236 '-Wundefined-func-template',
237 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
238 ]
239
Alexander Afanasyev01515792014-12-16 14:20:01 -0800240 def getDebugFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400241 flags = super().getDebugFlags(conf)
242 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento5fdab132024-01-14 16:30:06 -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 Pesavento682b2af2024-02-23 21:25:41 -0500250 # Tell libc++ to avoid including transitive headers
251 # https://libcxx.llvm.org/DesignDocs/HeaderRemovalPolicy.html
252 flags['DEFINES'] += ['_LIBCPP_REMOVE_TRANSITIVE_INCLUDES=1']
Davide Pesaventod06ea052015-10-02 01:48:24 +0200253 return flags
254
255 def getOptimizedFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400256 flags = super().getOptimizedFlags(conf)
257 flags['CXXFLAGS'] += self.__cxxFlags
Alexander Afanasyev01515792014-12-16 14:20:01 -0800258 return flags