blob: 82632d64e20af757ec1cd4b42a39e3f559e2005f [file] [log] [blame]
Junxiao Shif7191242015-03-19 05:53:41 -07001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
2
Davide Pesaventob07d7a92020-05-13 23:30:07 -04003import platform
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -05004from waflib import Configure, Logs, Utils
Junxiao Shif7191242015-03-19 05:53:41 -07005
Davide Pesaventodd808b02023-08-06 16:00:02 -04006
Junxiao Shif7191242015-03-19 05:53:41 -07007def options(opt):
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -05008 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesaventob07d7a92020-05-13 23:30:07 -04009 help='Compile in debugging mode with minimal optimizations (-Og)')
Junxiao Shif7191242015-03-19 05:53:41 -070010
Davide Pesaventodd808b02023-08-06 16:00:02 -040011
Junxiao Shif7191242015-03-19 05:53:41 -070012def configure(conf):
Davide Pesaventoc0702702017-08-24 22:04:00 -040013 conf.start_msg('Checking C++ compiler version')
14
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -050015 cxx = conf.env.CXX_NAME # generic name of the compiler
16 ccver = tuple(int(i) for i in conf.env.CC_VERSION)
17 ccverstr = '.'.join(conf.env.CC_VERSION)
Davide Pesaventoc0702702017-08-24 22:04:00 -040018 errmsg = ''
19 warnmsg = ''
Junxiao Shif7191242015-03-19 05:53:41 -070020 if cxx == 'gcc':
Davide Pesaventob04c52b2022-02-20 15:20:02 -050021 if ccver < (7, 4, 0):
Davide Pesaventoc0702702017-08-24 22:04:00 -040022 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesaventoad266072023-03-07 21:10:55 -050023 'The minimum supported gcc version is 9.3.')
24 elif ccver < (9, 3, 0):
25 warnmsg = ('Using a version of gcc older than 9.3 is not '
26 'officially supported and may result in build failures.')
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040027 conf.flags = GccFlags()
Junxiao Shif7191242015-03-19 05:53:41 -070028 elif cxx == 'clang':
Davide Pesaventob04c52b2022-02-20 15:20:02 -050029 if Utils.unversioned_sys_platform() == 'darwin':
30 if ccver < (10, 0, 0):
31 errmsg = ('The version of Xcode you are using is too old.\n'
Davide Pesaventoad266072023-03-07 21:10:55 -050032 'The minimum supported Xcode version is 12.4.')
33 elif ccver < (12, 0, 0):
34 warnmsg = ('Using a version of Xcode older than 12.4 is not '
Davide Pesaventob04c52b2022-02-20 15:20:02 -050035 'officially supported and may result in build failures.')
Davide Pesaventoad266072023-03-07 21:10:55 -050036 elif ccver < (7, 0, 0):
Davide Pesaventoc0702702017-08-24 22:04:00 -040037 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesaventoad266072023-03-07 21:10:55 -050038 'The minimum supported clang version is 7.0.')
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040039 conf.flags = ClangFlags()
Junxiao Shif7191242015-03-19 05:53:41 -070040 else:
Davide Pesavento423e58a2022-08-12 15:51:42 -040041 warnmsg = f'{cxx} compiler is unsupported'
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040042 conf.flags = CompilerFlags()
Davide Pesaventoc0702702017-08-24 22:04:00 -040043
44 if errmsg:
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -050045 conf.end_msg(ccverstr, color='RED')
Davide Pesaventoc0702702017-08-24 22:04:00 -040046 conf.fatal(errmsg)
47 elif warnmsg:
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -050048 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesaventob07d7a92020-05-13 23:30:07 -040049 Logs.warn('WARNING: ' + warnmsg)
Davide Pesaventoc0702702017-08-24 22:04:00 -040050 else:
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -050051 conf.end_msg(ccverstr)
Junxiao Shif7191242015-03-19 05:53:41 -070052
Davide Pesaventodd808b02023-08-06 16:00:02 -040053 conf.areCustomCxxflagsPresent = len(conf.env.CXXFLAGS) > 0
Junxiao Shif7191242015-03-19 05:53:41 -070054
Davide Pesavento60f8cc12018-05-10 22:05:21 -040055 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040056 generalFlags = conf.flags.getGeneralFlags(conf)
Junxiao Shif7191242015-03-19 05:53:41 -070057 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
58 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
59 conf.env.DEFINES += generalFlags['DEFINES']
60
Davide Pesaventodd808b02023-08-06 16:00:02 -040061
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040062@Configure.conf
63def check_compiler_flags(conf):
Junxiao Shic8a0a252015-10-05 07:25:02 -070064 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Junxiao Shif7191242015-03-19 05:53:41 -070065 # corresponding environment variables are not set.
Junxiao Shic8a0a252015-10-05 07:25:02 -070066 # DEFINES are always applied.
Junxiao Shif7191242015-03-19 05:53:41 -070067 if conf.options.debug:
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040068 extraFlags = conf.flags.getDebugFlags(conf)
69 if conf.areCustomCxxflagsPresent:
Junxiao Shif7191242015-03-19 05:53:41 -070070 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -050071 if missingFlags:
72 Logs.warn('Selected debug mode, but CXXFLAGS is set to a custom value "%s"'
73 % ' '.join(conf.env.CXXFLAGS))
74 Logs.warn('Default flags "%s" will not be used' % ' '.join(missingFlags))
Junxiao Shif7191242015-03-19 05:53:41 -070075 else:
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040076 extraFlags = conf.flags.getOptimizedFlags(conf)
Junxiao Shif7191242015-03-19 05:53:41 -070077
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040078 if not conf.areCustomCxxflagsPresent:
Junxiao Shif7191242015-03-19 05:53:41 -070079 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Davide Pesavento3e79c9c2015-11-01 20:38:28 +010080 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Junxiao Shif7191242015-03-19 05:53:41 -070081
82 conf.env.DEFINES += extraFlags['DEFINES']
83
Davide Pesaventodd808b02023-08-06 16:00:02 -040084
Junxiao Shif7191242015-03-19 05:53:41 -070085@Configure.conf
86def add_supported_cxxflags(self, cxxflags):
87 """
Davide Pesavento8148cd42023-08-06 14:01:32 -040088 Check which cxxflags are supported by the active compiler and add them to env.CXXFLAGS variable.
Junxiao Shif7191242015-03-19 05:53:41 -070089 """
90 if len(cxxflags) == 0:
91 return
92
93 self.start_msg('Checking supported CXXFLAGS')
94
95 supportedFlags = []
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040096 for flags in cxxflags:
97 flags = Utils.to_list(flags)
98 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
99 supportedFlags += flags
Junxiao Shif7191242015-03-19 05:53:41 -0700100
101 self.end_msg(' '.join(supportedFlags))
Davide Pesavento89d91752016-08-14 11:34:09 +0200102 self.env.prepend_value('CXXFLAGS', supportedFlags)
Junxiao Shif7191242015-03-19 05:53:41 -0700103
Davide Pesaventodd808b02023-08-06 16:00:02 -0400104
Junxiao Shif7191242015-03-19 05:53:41 -0700105@Configure.conf
106def add_supported_linkflags(self, linkflags):
107 """
Davide Pesavento8148cd42023-08-06 14:01:32 -0400108 Check which linkflags are supported by the active compiler and add them to env.LINKFLAGS variable.
Junxiao Shif7191242015-03-19 05:53:41 -0700109 """
110 if len(linkflags) == 0:
111 return
112
113 self.start_msg('Checking supported LINKFLAGS')
114
115 supportedFlags = []
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -0400116 for flags in linkflags:
117 flags = Utils.to_list(flags)
118 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
119 supportedFlags += flags
Junxiao Shif7191242015-03-19 05:53:41 -0700120
121 self.end_msg(' '.join(supportedFlags))
Davide Pesavento89d91752016-08-14 11:34:09 +0200122 self.env.prepend_value('LINKFLAGS', supportedFlags)
Junxiao Shif7191242015-03-19 05:53:41 -0700123
124
Davide Pesaventodd808b02023-08-06 16:00:02 -0400125class CompilerFlags:
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -0500126 def getCompilerVersion(self, conf):
127 return tuple(int(i) for i in conf.env.CC_VERSION)
128
Junxiao Shif7191242015-03-19 05:53:41 -0700129 def getGeneralFlags(self, conf):
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100130 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Davide Pesavento7e9d7e42023-11-11 15:00:03 -0500131 return {
132 'CXXFLAGS': [],
133 'LINKFLAGS': [],
134 'DEFINES': ['BOOST_ASIO_NO_DEPRECATED', 'BOOST_FILESYSTEM_NO_DEPRECATED'],
135 }
Junxiao Shif7191242015-03-19 05:53:41 -0700136
137 def getDebugFlags(self, conf):
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100138 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Junxiao Shif7191242015-03-19 05:53:41 -0700139 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
140
141 def getOptimizedFlags(self, conf):
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100142 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Junxiao Shif7191242015-03-19 05:53:41 -0700143 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
144
Davide Pesaventodd808b02023-08-06 16:00:02 -0400145
146class GccClangCommonFlags(CompilerFlags):
Junxiao Shic8a0a252015-10-05 07:25:02 -0700147 """
Davide Pesavento8148cd42023-08-06 14:01:32 -0400148 This class defines common flags that work for both gcc and clang compilers.
Junxiao Shic8a0a252015-10-05 07:25:02 -0700149 """
Davide Pesaventodd808b02023-08-06 16:00:02 -0400150
Davide Pesaventodd0e1952017-06-24 13:37:13 -0400151 def getGeneralFlags(self, conf):
Davide Pesaventodd808b02023-08-06 16:00:02 -0400152 flags = super().getGeneralFlags(conf)
Davide Pesaventob3570c62022-02-19 19:19:00 -0500153 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesavento3d7b0332022-10-05 15:30:02 -0400154 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesaventoe1b18a62018-12-19 01:17:42 -0500155 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Davide Pesaventodd0e1952017-06-24 13:37:13 -0400156 return flags
157
Davide Pesaventodd808b02023-08-06 16:00:02 -0400158 __cxxFlags = [
159 '-fdiagnostics-color',
160 '-Wall',
161 '-Wextra',
162 '-Wpedantic',
163 '-Wenum-conversion',
164 '-Wextra-semi',
Davide Pesaventodd808b02023-08-06 16:00:02 -0400165 '-Wno-unused-parameter',
166 ]
167 __linkFlags = ['-Wl,-O1']
168
Junxiao Shif7191242015-03-19 05:53:41 -0700169 def getDebugFlags(self, conf):
Davide Pesaventodd808b02023-08-06 16:00:02 -0400170 flags = super().getDebugFlags(conf)
171 flags['CXXFLAGS'] += ['-Og', '-g'] + self.__cxxFlags + [
172 '-Werror',
173 '-Wno-error=deprecated-declarations', # Bug #3795
174 '-Wno-error=maybe-uninitialized', # Bug #1615
175 ]
176 flags['LINKFLAGS'] += self.__linkFlags
Junxiao Shif7191242015-03-19 05:53:41 -0700177 return flags
178
179 def getOptimizedFlags(self, conf):
Davide Pesaventodd808b02023-08-06 16:00:02 -0400180 flags = super().getOptimizedFlags(conf)
181 flags['CXXFLAGS'] += ['-O2', '-g1'] + self.__cxxFlags
182 flags['LINKFLAGS'] += self.__linkFlags
Junxiao Shif7191242015-03-19 05:53:41 -0700183 return flags
184
Davide Pesaventodd808b02023-08-06 16:00:02 -0400185
186class GccFlags(GccClangCommonFlags):
187 __cxxFlags = [
188 '-Wcatch-value=2',
189 '-Wcomma-subscript', # enabled by default in C++20
190 '-Wduplicated-branches',
191 '-Wduplicated-cond',
192 '-Wlogical-op',
193 '-Wredundant-tags',
194 '-Wvolatile', # enabled by default in C++20
195 ]
196
Junxiao Shif7191242015-03-19 05:53:41 -0700197 def getDebugFlags(self, conf):
Davide Pesaventodd808b02023-08-06 16:00:02 -0400198 flags = super().getDebugFlags(conf)
199 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventob04c52b2022-02-20 15:20:02 -0500200 if platform.machine() == 'armv7l':
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400201 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Junxiao Shic8a0a252015-10-05 07:25:02 -0700202 return flags
203
204 def getOptimizedFlags(self, conf):
Davide Pesaventodd808b02023-08-06 16:00:02 -0400205 flags = super().getOptimizedFlags(conf)
206 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventob04c52b2022-02-20 15:20:02 -0500207 if platform.machine() == 'armv7l':
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400208 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Junxiao Shif7191242015-03-19 05:53:41 -0700209 return flags
210
Davide Pesaventodd808b02023-08-06 16:00:02 -0400211
212class ClangFlags(GccClangCommonFlags):
Junxiao Shif7191242015-03-19 05:53:41 -0700213 def getGeneralFlags(self, conf):
Davide Pesaventodd808b02023-08-06 16:00:02 -0400214 flags = super().getGeneralFlags(conf)
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400215 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -0500216 # Bug #4296
Davide Pesavento423e58a2022-08-12 15:51:42 -0400217 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
Davide Pesaventodd808b02023-08-06 16:00:02 -0400218 flags['CXXFLAGS'] += [
219 ['-isystem', f'{brewdir}/include'], # for Homebrew
220 ['-isystem', '/opt/local/include'], # for MacPorts
221 ]
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400222 elif Utils.unversioned_sys_platform() == 'freebsd':
223 # Bug #4790
224 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Davide Pesavento0a675562024-01-13 21:54:52 -0500225 if self.getCompilerVersion(conf) >= (18, 0, 0):
226 # Bug #5300
227 flags['CXXFLAGS'] += ['-Wno-enum-constexpr-conversion']
Junxiao Shif7191242015-03-19 05:53:41 -0700228 return flags
229
Davide Pesaventodd808b02023-08-06 16:00:02 -0400230 __cxxFlags = [
231 '-Wundefined-func-template',
232 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
233 ]
234
Junxiao Shif7191242015-03-19 05:53:41 -0700235 def getDebugFlags(self, conf):
Davide Pesaventodd808b02023-08-06 16:00:02 -0400236 flags = super().getDebugFlags(conf)
237 flags['CXXFLAGS'] += self.__cxxFlags
Junxiao Shic8a0a252015-10-05 07:25:02 -0700238 return flags
239
240 def getOptimizedFlags(self, conf):
Davide Pesaventodd808b02023-08-06 16:00:02 -0400241 flags = super().getOptimizedFlags(conf)
242 flags['CXXFLAGS'] += self.__cxxFlags
Junxiao Shif7191242015-03-19 05:53:41 -0700243 return flags