blob: 7746db935ffbc52b216c604e7688b21635c3ca3b [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"""
Junxiao Shif7191242015-03-19 05:53:41 -0700131 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
132
133 def getDebugFlags(self, conf):
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100134 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Junxiao Shif7191242015-03-19 05:53:41 -0700135 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
136
137 def getOptimizedFlags(self, conf):
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100138 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Junxiao Shif7191242015-03-19 05:53:41 -0700139 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
140
Davide Pesaventodd808b02023-08-06 16:00:02 -0400141
142class GccClangCommonFlags(CompilerFlags):
Junxiao Shic8a0a252015-10-05 07:25:02 -0700143 """
Davide Pesavento8148cd42023-08-06 14:01:32 -0400144 This class defines common flags that work for both gcc and clang compilers.
Junxiao Shic8a0a252015-10-05 07:25:02 -0700145 """
Davide Pesaventodd808b02023-08-06 16:00:02 -0400146
Davide Pesaventodd0e1952017-06-24 13:37:13 -0400147 def getGeneralFlags(self, conf):
Davide Pesaventodd808b02023-08-06 16:00:02 -0400148 flags = super().getGeneralFlags(conf)
Davide Pesaventob3570c62022-02-19 19:19:00 -0500149 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesavento3d7b0332022-10-05 15:30:02 -0400150 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesaventoe1b18a62018-12-19 01:17:42 -0500151 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Davide Pesaventodd0e1952017-06-24 13:37:13 -0400152 return flags
153
Davide Pesaventodd808b02023-08-06 16:00:02 -0400154 __cxxFlags = [
155 '-fdiagnostics-color',
156 '-Wall',
157 '-Wextra',
158 '-Wpedantic',
159 '-Wenum-conversion',
160 '-Wextra-semi',
Davide Pesaventodd808b02023-08-06 16:00:02 -0400161 '-Wno-unused-parameter',
162 ]
163 __linkFlags = ['-Wl,-O1']
164
Junxiao Shif7191242015-03-19 05:53:41 -0700165 def getDebugFlags(self, conf):
Davide Pesaventodd808b02023-08-06 16:00:02 -0400166 flags = super().getDebugFlags(conf)
167 flags['CXXFLAGS'] += ['-Og', '-g'] + self.__cxxFlags + [
168 '-Werror',
169 '-Wno-error=deprecated-declarations', # Bug #3795
170 '-Wno-error=maybe-uninitialized', # Bug #1615
171 ]
172 flags['LINKFLAGS'] += self.__linkFlags
Junxiao Shif7191242015-03-19 05:53:41 -0700173 return flags
174
175 def getOptimizedFlags(self, conf):
Davide Pesaventodd808b02023-08-06 16:00:02 -0400176 flags = super().getOptimizedFlags(conf)
177 flags['CXXFLAGS'] += ['-O2', '-g1'] + self.__cxxFlags
178 flags['LINKFLAGS'] += self.__linkFlags
Junxiao Shif7191242015-03-19 05:53:41 -0700179 return flags
180
Davide Pesaventodd808b02023-08-06 16:00:02 -0400181
182class GccFlags(GccClangCommonFlags):
183 __cxxFlags = [
184 '-Wcatch-value=2',
185 '-Wcomma-subscript', # enabled by default in C++20
186 '-Wduplicated-branches',
187 '-Wduplicated-cond',
188 '-Wlogical-op',
189 '-Wredundant-tags',
190 '-Wvolatile', # enabled by default in C++20
191 ]
192
Junxiao Shif7191242015-03-19 05:53:41 -0700193 def getDebugFlags(self, conf):
Davide Pesaventodd808b02023-08-06 16:00:02 -0400194 flags = super().getDebugFlags(conf)
195 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventob04c52b2022-02-20 15:20:02 -0500196 if platform.machine() == 'armv7l':
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400197 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Junxiao Shic8a0a252015-10-05 07:25:02 -0700198 return flags
199
200 def getOptimizedFlags(self, conf):
Davide Pesaventodd808b02023-08-06 16:00:02 -0400201 flags = super().getOptimizedFlags(conf)
202 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventob04c52b2022-02-20 15:20:02 -0500203 if platform.machine() == 'armv7l':
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400204 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Junxiao Shif7191242015-03-19 05:53:41 -0700205 return flags
206
Davide Pesaventodd808b02023-08-06 16:00:02 -0400207
208class ClangFlags(GccClangCommonFlags):
Junxiao Shif7191242015-03-19 05:53:41 -0700209 def getGeneralFlags(self, conf):
Davide Pesaventodd808b02023-08-06 16:00:02 -0400210 flags = super().getGeneralFlags(conf)
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400211 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -0500212 # Bug #4296
Davide Pesavento423e58a2022-08-12 15:51:42 -0400213 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
Davide Pesaventodd808b02023-08-06 16:00:02 -0400214 flags['CXXFLAGS'] += [
215 ['-isystem', f'{brewdir}/include'], # for Homebrew
216 ['-isystem', '/opt/local/include'], # for MacPorts
217 ]
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400218 elif Utils.unversioned_sys_platform() == 'freebsd':
219 # Bug #4790
220 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Junxiao Shif7191242015-03-19 05:53:41 -0700221 return flags
222
Davide Pesaventodd808b02023-08-06 16:00:02 -0400223 __cxxFlags = [
224 '-Wundefined-func-template',
225 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
226 ]
227
Junxiao Shif7191242015-03-19 05:53:41 -0700228 def getDebugFlags(self, conf):
Davide Pesaventodd808b02023-08-06 16:00:02 -0400229 flags = super().getDebugFlags(conf)
230 flags['CXXFLAGS'] += self.__cxxFlags
Junxiao Shic8a0a252015-10-05 07:25:02 -0700231 return flags
232
233 def getOptimizedFlags(self, conf):
Davide Pesaventodd808b02023-08-06 16:00:02 -0400234 flags = super().getOptimizedFlags(conf)
235 flags['CXXFLAGS'] += self.__cxxFlags
Junxiao Shif7191242015-03-19 05:53:41 -0700236 return flags