blob: f3be6e7bf841b3eb83173308ea7b1f877c0cfac1 [file] [log] [blame]
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -07001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -07002
Davide Pesavento71c622b2020-04-24 01:39:01 -04003import platform
Alexander Afanasyev5560fd42018-03-07 17:03:03 -05004from waflib import Configure, Logs, Utils
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -07005
6def options(opt):
Alexander Afanasyev5560fd42018-03-07 17:03:03 -05007 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesavento71c622b2020-04-24 01:39:01 -04008 help='Compile in debugging mode with minimal optimizations (-Og)')
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -07009
10def 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 Pesavento844b0932018-05-07 01:00:16 -040019 if ccver < (5, 3, 0):
Davide Pesavento88a0d812017-08-19 21:31:42 -040020 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesavento71c622b2020-04-24 01:39:01 -040021 'The minimum supported gcc version is 7.4.0.')
22 elif ccver < (7, 4, 0):
23 warnmsg = ('Using a version of gcc older than 7.4.0 is not '
24 '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 Pesavento71c622b2020-04-24 01:39:01 -040027 if Utils.unversioned_sys_platform() == 'darwin' and ccver < (9, 0, 0):
28 errmsg = ('The version of Xcode you are using is too old.\n'
29 'The minimum supported Xcode version is 9.0.')
30 elif ccver < (4, 0, 0):
Davide Pesavento88a0d812017-08-19 21:31:42 -040031 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesavento71c622b2020-04-24 01:39:01 -040032 'The minimum supported clang version is 4.0.')
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040033 conf.flags = ClangFlags()
Alexander Afanasyev01515792014-12-16 14:20:01 -080034 else:
Davide Pesavento71c622b2020-04-24 01:39:01 -040035 warnmsg = '%s compiler is unsupported' % cxx
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040036 conf.flags = CompilerFlags()
Davide Pesavento88a0d812017-08-19 21:31:42 -040037
38 if errmsg:
Alexander Afanasyev5560fd42018-03-07 17:03:03 -050039 conf.end_msg(ccverstr, color='RED')
Davide Pesavento88a0d812017-08-19 21:31:42 -040040 conf.fatal(errmsg)
41 elif warnmsg:
Alexander Afanasyev5560fd42018-03-07 17:03:03 -050042 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento71c622b2020-04-24 01:39:01 -040043 Logs.warn('WARNING: ' + warnmsg)
Davide Pesavento88a0d812017-08-19 21:31:42 -040044 else:
Alexander Afanasyev5560fd42018-03-07 17:03:03 -050045 conf.end_msg(ccverstr)
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -070046
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040047 conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
Alexander Afanasyev01515792014-12-16 14:20:01 -080048
Davide Pesavento1fd00242018-05-20 00:11:01 -040049 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040050 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyev01515792014-12-16 14:20:01 -080051 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
52 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
53 conf.env.DEFINES += generalFlags['DEFINES']
54
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040055@Configure.conf
56def check_compiler_flags(conf):
Davide Pesaventod06ea052015-10-02 01:48:24 +020057 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Alexander Afanasyev01515792014-12-16 14:20:01 -080058 # corresponding environment variables are not set.
Davide Pesaventod06ea052015-10-02 01:48:24 +020059 # DEFINES are always applied.
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070060 if conf.options.debug:
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040061 extraFlags = conf.flags.getDebugFlags(conf)
62 if conf.areCustomCxxflagsPresent:
Alexander Afanasyev01515792014-12-16 14:20:01 -080063 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev5560fd42018-03-07 17:03:03 -050064 if missingFlags:
65 Logs.warn('Selected debug mode, but CXXFLAGS is set to a custom value "%s"'
66 % ' '.join(conf.env.CXXFLAGS))
67 Logs.warn('Default flags "%s" will not be used' % ' '.join(missingFlags))
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070068 else:
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040069 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070070
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040071 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyev01515792014-12-16 14:20:01 -080072 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Davide Pesavento7c02b472015-10-28 20:50:17 +010073 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Alexander Afanasyev01515792014-12-16 14:20:01 -080074
75 conf.env.DEFINES += extraFlags['DEFINES']
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020076
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070077@Configure.conf
78def add_supported_cxxflags(self, cxxflags):
79 """
80 Check which cxxflags are supported by compiler and add them to env.CXXFLAGS variable
81 """
Alexander Afanasyev01515792014-12-16 14:20:01 -080082 if len(cxxflags) == 0:
83 return
84
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020085 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070086
87 supportedFlags = []
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040088 for flags in cxxflags:
89 flags = Utils.to_list(flags)
90 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
91 supportedFlags += flags
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070092
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -070093 self.end_msg(' '.join(supportedFlags))
Davide Pesavento1349d2d2016-08-09 06:43:57 +020094 self.env.prepend_value('CXXFLAGS', supportedFlags)
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020095
96@Configure.conf
97def add_supported_linkflags(self, linkflags):
98 """
99 Check which linkflags are supported by compiler and add them to env.LINKFLAGS variable
100 """
Alexander Afanasyev01515792014-12-16 14:20:01 -0800101 if len(linkflags) == 0:
102 return
103
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +0200104 self.start_msg('Checking supported LINKFLAGS')
105
106 supportedFlags = []
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -0400107 for flags in linkflags:
108 flags = Utils.to_list(flags)
109 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
110 supportedFlags += flags
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +0200111
112 self.end_msg(' '.join(supportedFlags))
Davide Pesavento1349d2d2016-08-09 06:43:57 +0200113 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyev01515792014-12-16 14:20:01 -0800114
115
116class CompilerFlags(object):
Alexander Afanasyev5560fd42018-03-07 17:03:03 -0500117 def getCompilerVersion(self, conf):
118 return tuple(int(i) for i in conf.env.CC_VERSION)
119
Alexander Afanasyev01515792014-12-16 14:20:01 -0800120 def getGeneralFlags(self, conf):
Davide Pesavento7c02b472015-10-28 20:50:17 +0100121 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Alexander Afanasyev01515792014-12-16 14:20:01 -0800122 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
123
124 def getDebugFlags(self, conf):
Davide Pesavento7c02b472015-10-28 20:50:17 +0100125 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Alexander Afanasyev01515792014-12-16 14:20:01 -0800126 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
127
128 def getOptimizedFlags(self, conf):
Davide Pesavento7c02b472015-10-28 20:50:17 +0100129 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Alexander Afanasyev01515792014-12-16 14:20:01 -0800130 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
131
132class GccBasicFlags(CompilerFlags):
Davide Pesaventod06ea052015-10-02 01:48:24 +0200133 """
134 This class defines basic flags that work for both gcc and clang compilers
135 """
Davide Pesavento724d65a2017-06-23 17:14:08 -0400136 def getGeneralFlags(self, conf):
137 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
Davide Pesavento1fd00242018-05-20 00:11:01 -0400138 flags['CXXFLAGS'] += ['-std=c++14']
Davide Pesavento64ae55d2018-12-19 00:25:47 -0500139 if Utils.unversioned_sys_platform() == 'linux':
140 flags['LINKFLAGS'] += ['-fuse-ld=gold']
141 elif Utils.unversioned_sys_platform() == 'freebsd':
142 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Davide Pesavento724d65a2017-06-23 17:14:08 -0400143 return flags
144
Alexander Afanasyev01515792014-12-16 14:20:01 -0800145 def getDebugFlags(self, conf):
146 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Davide Pesavento71c622b2020-04-24 01:39:01 -0400147 flags['CXXFLAGS'] += ['-Og',
Alexander Afanasyev01515792014-12-16 14:20:01 -0800148 '-g3',
Davide Pesavento7c02b472015-10-28 20:50:17 +0100149 '-pedantic',
150 '-Wall',
151 '-Wextra',
Alexander Afanasyev01515792014-12-16 14:20:01 -0800152 '-Werror',
Davide Pesavento71c622b2020-04-24 01:39:01 -0400153 '-Wcatch-value=2',
154 '-Wextra-semi',
Davide Pesavento7f20d6e2017-01-16 14:43:58 -0500155 '-Wnon-virtual-dtor',
Alexander Afanasyev465c9732016-10-11 16:35:32 -0700156 '-Wno-error=deprecated-declarations', # Bug #3795
Davide Pesavento88a0d812017-08-19 21:31:42 -0400157 '-Wno-error=maybe-uninitialized', # Bug #1615
158 '-Wno-unused-parameter',
Davide Pesavento1aafba42015-09-17 17:22:31 -0700159 ]
Davide Pesavento64ae55d2018-12-19 00:25:47 -0500160 flags['LINKFLAGS'] += ['-Wl,-O1']
Alexander Afanasyev01515792014-12-16 14:20:01 -0800161 return flags
162
163 def getOptimizedFlags(self, conf):
164 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
Davide Pesavento7c02b472015-10-28 20:50:17 +0100165 flags['CXXFLAGS'] += ['-O2',
Davide Pesavento1aafba42015-09-17 17:22:31 -0700166 '-g',
Davide Pesavento7c02b472015-10-28 20:50:17 +0100167 '-pedantic',
168 '-Wall',
169 '-Wextra',
Davide Pesavento71c622b2020-04-24 01:39:01 -0400170 '-Wcatch-value=2',
171 '-Wextra-semi',
Davide Pesavento7f20d6e2017-01-16 14:43:58 -0500172 '-Wnon-virtual-dtor',
Davide Pesavento7c02b472015-10-28 20:50:17 +0100173 '-Wno-unused-parameter',
Davide Pesavento1aafba42015-09-17 17:22:31 -0700174 ]
Davide Pesavento64ae55d2018-12-19 00:25:47 -0500175 flags['LINKFLAGS'] += ['-Wl,-O1']
Alexander Afanasyev01515792014-12-16 14:20:01 -0800176 return flags
177
178class GccFlags(GccBasicFlags):
Alexander Afanasyev01515792014-12-16 14:20:01 -0800179 def getDebugFlags(self, conf):
180 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesavento71c622b2020-04-24 01:39:01 -0400181 flags['CXXFLAGS'] += ['-fdiagnostics-color',
182 '-Wredundant-tags',
183 ]
184 if platform.machine() == 'armv7l' and self.getCompilerVersion(conf) >= (7, 1, 0):
185 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyev01515792014-12-16 14:20:01 -0800186 return flags
187
Davide Pesaventod06ea052015-10-02 01:48:24 +0200188 def getOptimizedFlags(self, conf):
189 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesavento71c622b2020-04-24 01:39:01 -0400190 flags['CXXFLAGS'] += ['-fdiagnostics-color',
191 '-Wredundant-tags',
192 ]
193 if platform.machine() == 'armv7l' and self.getCompilerVersion(conf) >= (7, 1, 0):
194 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Davide Pesaventod06ea052015-10-02 01:48:24 +0200195 return flags
196
Alexander Afanasyev01515792014-12-16 14:20:01 -0800197class ClangFlags(GccBasicFlags):
198 def getGeneralFlags(self, conf):
199 flags = super(ClangFlags, self).getGeneralFlags(conf)
Davide Pesavento71c622b2020-04-24 01:39:01 -0400200 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev5560fd42018-03-07 17:03:03 -0500201 # Bug #4296
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -0400202 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include'], # for Homebrew
203 ['-isystem', '/opt/local/include']] # for MacPorts
Davide Pesavento71c622b2020-04-24 01:39:01 -0400204 elif Utils.unversioned_sys_platform() == 'freebsd':
205 # Bug #4790
206 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Alexander Afanasyev01515792014-12-16 14:20:01 -0800207 return flags
208
209 def getDebugFlags(self, conf):
210 flags = super(ClangFlags, self).getDebugFlags(conf)
Davide Pesavento1aafba42015-09-17 17:22:31 -0700211 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesavento88a0d812017-08-19 21:31:42 -0400212 '-Wundefined-func-template',
Davide Pesavento88a0d812017-08-19 21:31:42 -0400213 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Davide Pesaventod06ea052015-10-02 01:48:24 +0200214 ]
Davide Pesavento71c622b2020-04-24 01:39:01 -0400215 if self.getCompilerVersion(conf) < (6, 0, 0):
Davide Pesavento52bc4e92018-08-23 19:08:19 -0400216 flags['CXXFLAGS'] += ['-Wno-missing-braces'] # Bug #4721
Davide Pesaventod06ea052015-10-02 01:48:24 +0200217 return flags
218
219 def getOptimizedFlags(self, conf):
220 flags = super(ClangFlags, self).getOptimizedFlags(conf)
221 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesavento88a0d812017-08-19 21:31:42 -0400222 '-Wundefined-func-template',
Davide Pesaventod06ea052015-10-02 01:48:24 +0200223 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Davide Pesavento1aafba42015-09-17 17:22:31 -0700224 ]
Davide Pesavento71c622b2020-04-24 01:39:01 -0400225 if self.getCompilerVersion(conf) < (6, 0, 0):
Davide Pesavento52bc4e92018-08-23 19:08:19 -0400226 flags['CXXFLAGS'] += ['-Wno-missing-braces'] # Bug #4721
Alexander Afanasyev01515792014-12-16 14:20:01 -0800227 return flags