blob: 8c718717e3da2f62156c901c59a903bf6b4d7160 [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
6def options(opt):
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -05007 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesaventob07d7a92020-05-13 23:30:07 -04008 help='Compile in debugging mode with minimal optimizations (-Og)')
Junxiao Shif7191242015-03-19 05:53:41 -07009
10def 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 Pesaventob04c52b2022-02-20 15:20:02 -050019 if ccver < (7, 4, 0):
Davide Pesaventoc0702702017-08-24 22:04:00 -040020 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesaventoad266072023-03-07 21:10:55 -050021 'The minimum supported gcc version is 9.3.')
22 elif ccver < (9, 3, 0):
23 warnmsg = ('Using a version of gcc older than 9.3 is not '
24 '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':
28 if ccver < (10, 0, 0):
29 errmsg = ('The version of Xcode you are using is too old.\n'
Davide Pesaventoad266072023-03-07 21:10:55 -050030 'The minimum supported Xcode version is 12.4.')
31 elif ccver < (12, 0, 0):
32 warnmsg = ('Using a version of Xcode older than 12.4 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 Pesaventoad266072023-03-07 21:10:55 -050036 'The minimum supported clang version is 7.0.')
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040037 conf.flags = ClangFlags()
Junxiao Shif7191242015-03-19 05:53:41 -070038 else:
Davide Pesavento423e58a2022-08-12 15:51:42 -040039 warnmsg = f'{cxx} compiler is unsupported'
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040040 conf.flags = CompilerFlags()
Davide Pesaventoc0702702017-08-24 22:04:00 -040041
42 if errmsg:
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -050043 conf.end_msg(ccverstr, color='RED')
Davide Pesaventoc0702702017-08-24 22:04:00 -040044 conf.fatal(errmsg)
45 elif warnmsg:
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -050046 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesaventob07d7a92020-05-13 23:30:07 -040047 Logs.warn('WARNING: ' + warnmsg)
Davide Pesaventoc0702702017-08-24 22:04:00 -040048 else:
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -050049 conf.end_msg(ccverstr)
Junxiao Shif7191242015-03-19 05:53:41 -070050
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040051 conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
Junxiao Shif7191242015-03-19 05:53:41 -070052
Davide Pesavento60f8cc12018-05-10 22:05:21 -040053 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040054 generalFlags = conf.flags.getGeneralFlags(conf)
Junxiao Shif7191242015-03-19 05:53:41 -070055 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
56 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
57 conf.env.DEFINES += generalFlags['DEFINES']
58
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040059@Configure.conf
60def check_compiler_flags(conf):
Junxiao Shic8a0a252015-10-05 07:25:02 -070061 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Junxiao Shif7191242015-03-19 05:53:41 -070062 # corresponding environment variables are not set.
Junxiao Shic8a0a252015-10-05 07:25:02 -070063 # DEFINES are always applied.
Junxiao Shif7191242015-03-19 05:53:41 -070064 if conf.options.debug:
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040065 extraFlags = conf.flags.getDebugFlags(conf)
66 if conf.areCustomCxxflagsPresent:
Junxiao Shif7191242015-03-19 05:53:41 -070067 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -050068 if missingFlags:
69 Logs.warn('Selected debug mode, but CXXFLAGS is set to a custom value "%s"'
70 % ' '.join(conf.env.CXXFLAGS))
71 Logs.warn('Default flags "%s" will not be used' % ' '.join(missingFlags))
Junxiao Shif7191242015-03-19 05:53:41 -070072 else:
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040073 extraFlags = conf.flags.getOptimizedFlags(conf)
Junxiao Shif7191242015-03-19 05:53:41 -070074
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040075 if not conf.areCustomCxxflagsPresent:
Junxiao Shif7191242015-03-19 05:53:41 -070076 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Davide Pesavento3e79c9c2015-11-01 20:38:28 +010077 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Junxiao Shif7191242015-03-19 05:53:41 -070078
79 conf.env.DEFINES += extraFlags['DEFINES']
80
81@Configure.conf
82def add_supported_cxxflags(self, cxxflags):
83 """
84 Check which cxxflags are supported by compiler and add them to env.CXXFLAGS variable
85 """
86 if len(cxxflags) == 0:
87 return
88
89 self.start_msg('Checking supported CXXFLAGS')
90
91 supportedFlags = []
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040092 for flags in cxxflags:
93 flags = Utils.to_list(flags)
94 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
95 supportedFlags += flags
Junxiao Shif7191242015-03-19 05:53:41 -070096
97 self.end_msg(' '.join(supportedFlags))
Davide Pesavento89d91752016-08-14 11:34:09 +020098 self.env.prepend_value('CXXFLAGS', supportedFlags)
Junxiao Shif7191242015-03-19 05:53:41 -070099
100@Configure.conf
101def add_supported_linkflags(self, linkflags):
102 """
103 Check which linkflags are supported by compiler and add them to env.LINKFLAGS variable
104 """
105 if len(linkflags) == 0:
106 return
107
108 self.start_msg('Checking supported LINKFLAGS')
109
110 supportedFlags = []
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -0400111 for flags in linkflags:
112 flags = Utils.to_list(flags)
113 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
114 supportedFlags += flags
Junxiao Shif7191242015-03-19 05:53:41 -0700115
116 self.end_msg(' '.join(supportedFlags))
Davide Pesavento89d91752016-08-14 11:34:09 +0200117 self.env.prepend_value('LINKFLAGS', supportedFlags)
Junxiao Shif7191242015-03-19 05:53:41 -0700118
119
120class CompilerFlags(object):
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -0500121 def getCompilerVersion(self, conf):
122 return tuple(int(i) for i in conf.env.CC_VERSION)
123
Junxiao Shif7191242015-03-19 05:53:41 -0700124 def getGeneralFlags(self, conf):
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100125 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Junxiao Shif7191242015-03-19 05:53:41 -0700126 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
127
128 def getDebugFlags(self, conf):
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100129 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Junxiao Shif7191242015-03-19 05:53:41 -0700130 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
131
132 def getOptimizedFlags(self, conf):
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100133 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Junxiao Shif7191242015-03-19 05:53:41 -0700134 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
135
136class GccBasicFlags(CompilerFlags):
Junxiao Shic8a0a252015-10-05 07:25:02 -0700137 """
138 This class defines basic flags that work for both gcc and clang compilers
139 """
Davide Pesaventodd0e1952017-06-24 13:37:13 -0400140 def getGeneralFlags(self, conf):
141 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
Davide Pesaventob3570c62022-02-19 19:19:00 -0500142 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesavento3d7b0332022-10-05 15:30:02 -0400143 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesaventoe1b18a62018-12-19 01:17:42 -0500144 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Davide Pesaventodd0e1952017-06-24 13:37:13 -0400145 return flags
146
Junxiao Shif7191242015-03-19 05:53:41 -0700147 def getDebugFlags(self, conf):
148 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400149 flags['CXXFLAGS'] += ['-Og',
Junxiao Shif7191242015-03-19 05:53:41 -0700150 '-g3',
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100151 '-Wall',
152 '-Wextra',
Davide Pesavento3b4ee2f2022-12-31 01:55:06 -0500153 '-Wpedantic',
Junxiao Shif7191242015-03-19 05:53:41 -0700154 '-Werror',
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400155 '-Wcatch-value=2',
156 '-Wextra-semi',
Davide Pesavento887336c2017-09-19 15:25:23 -0400157 '-Wnon-virtual-dtor',
Alexander Afanasyevfc4aa0d2016-10-15 00:57:33 +0000158 '-Wno-error=deprecated-declarations', # Bug #3795
Davide Pesaventoc0702702017-08-24 22:04:00 -0400159 '-Wno-error=maybe-uninitialized', # Bug #1615
160 '-Wno-unused-parameter',
Junxiao Shic8a0a252015-10-05 07:25:02 -0700161 ]
Davide Pesaventoe1b18a62018-12-19 01:17:42 -0500162 flags['LINKFLAGS'] += ['-Wl,-O1']
Junxiao Shif7191242015-03-19 05:53:41 -0700163 return flags
164
165 def getOptimizedFlags(self, conf):
166 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100167 flags['CXXFLAGS'] += ['-O2',
Junxiao Shic8a0a252015-10-05 07:25:02 -0700168 '-g',
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100169 '-Wall',
170 '-Wextra',
Davide Pesavento3b4ee2f2022-12-31 01:55:06 -0500171 '-Wpedantic',
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400172 '-Wcatch-value=2',
173 '-Wextra-semi',
Davide Pesavento887336c2017-09-19 15:25:23 -0400174 '-Wnon-virtual-dtor',
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100175 '-Wno-unused-parameter',
Junxiao Shic8a0a252015-10-05 07:25:02 -0700176 ]
Davide Pesaventoe1b18a62018-12-19 01:17:42 -0500177 flags['LINKFLAGS'] += ['-Wl,-O1']
Junxiao Shif7191242015-03-19 05:53:41 -0700178 return flags
179
180class GccFlags(GccBasicFlags):
Junxiao Shif7191242015-03-19 05:53:41 -0700181 def getDebugFlags(self, conf):
182 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400183 flags['CXXFLAGS'] += ['-fdiagnostics-color',
184 '-Wredundant-tags',
185 ]
Davide Pesaventob04c52b2022-02-20 15:20:02 -0500186 if platform.machine() == 'armv7l':
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400187 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Junxiao Shic8a0a252015-10-05 07:25:02 -0700188 return flags
189
190 def getOptimizedFlags(self, conf):
191 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400192 flags['CXXFLAGS'] += ['-fdiagnostics-color',
193 '-Wredundant-tags',
194 ]
Davide Pesaventob04c52b2022-02-20 15:20:02 -0500195 if platform.machine() == 'armv7l':
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400196 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Junxiao Shif7191242015-03-19 05:53:41 -0700197 return flags
198
199class ClangFlags(GccBasicFlags):
200 def getGeneralFlags(self, conf):
201 flags = super(ClangFlags, self).getGeneralFlags(conf)
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400202 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -0500203 # Bug #4296
Davide Pesavento423e58a2022-08-12 15:51:42 -0400204 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
205 flags['CXXFLAGS'] += [['-isystem', f'{brewdir}/include'], # for Homebrew
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -0400206 ['-isystem', '/opt/local/include']] # for MacPorts
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400207 elif Utils.unversioned_sys_platform() == 'freebsd':
208 # Bug #4790
209 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Junxiao Shif7191242015-03-19 05:53:41 -0700210 return flags
211
212 def getDebugFlags(self, conf):
213 flags = super(ClangFlags, self).getDebugFlags(conf)
Davide Pesaventoc0702702017-08-24 22:04:00 -0400214 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesaventoc0702702017-08-24 22:04:00 -0400215 '-Wundefined-func-template',
Davide Pesaventoc0702702017-08-24 22:04:00 -0400216 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
217 ]
Junxiao Shic8a0a252015-10-05 07:25:02 -0700218 return flags
219
220 def getOptimizedFlags(self, conf):
221 flags = super(ClangFlags, self).getOptimizedFlags(conf)
222 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesaventoc0702702017-08-24 22:04:00 -0400223 '-Wundefined-func-template',
Junxiao Shic8a0a252015-10-05 07:25:02 -0700224 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
225 ]
Junxiao Shif7191242015-03-19 05:53:41 -0700226 return flags