blob: f159fdeb6b05760bde8c70edc8e08126c694e795 [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 Pesavento541a8222022-03-01 15:08:42 -050019 if ccver < (7, 4, 0):
Davide Pesavento88a0d812017-08-19 21:31:42 -040020 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesavento541a8222022-03-01 15:08:42 -050021 'The minimum supported gcc version is 7.4.')
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040022 conf.flags = GccFlags()
Alexander Afanasyev01515792014-12-16 14:20:01 -080023 elif cxx == 'clang':
Davide Pesavento541a8222022-03-01 15:08:42 -050024 if Utils.unversioned_sys_platform() == 'darwin':
25 if ccver < (10, 0, 0):
26 errmsg = ('The version of Xcode you are using is too old.\n'
27 'The minimum supported Xcode version is 11.3.')
28 elif ccver < (11, 0, 0):
29 warnmsg = ('Using a version of Xcode older than 11.3 is not '
30 'officially supported and may result in build failures.')
31 elif ccver < (6, 0, 0):
Davide Pesavento88a0d812017-08-19 21:31:42 -040032 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesavento541a8222022-03-01 15:08:42 -050033 'The minimum supported clang version is 6.0.')
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040034 conf.flags = ClangFlags()
Alexander Afanasyev01515792014-12-16 14:20:01 -080035 else:
Davide Pesaventof6625002022-07-31 17:15:02 -040036 warnmsg = f'{cxx} compiler is unsupported'
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040037 conf.flags = CompilerFlags()
Davide Pesavento88a0d812017-08-19 21:31:42 -040038
39 if errmsg:
Alexander Afanasyev5560fd42018-03-07 17:03:03 -050040 conf.end_msg(ccverstr, color='RED')
Davide Pesavento88a0d812017-08-19 21:31:42 -040041 conf.fatal(errmsg)
42 elif warnmsg:
Alexander Afanasyev5560fd42018-03-07 17:03:03 -050043 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento71c622b2020-04-24 01:39:01 -040044 Logs.warn('WARNING: ' + warnmsg)
Davide Pesavento88a0d812017-08-19 21:31:42 -040045 else:
Alexander Afanasyev5560fd42018-03-07 17:03:03 -050046 conf.end_msg(ccverstr)
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -070047
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040048 conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
Alexander Afanasyev01515792014-12-16 14:20:01 -080049
Davide Pesavento1fd00242018-05-20 00:11:01 -040050 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040051 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyev01515792014-12-16 14:20:01 -080052 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
53 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
54 conf.env.DEFINES += generalFlags['DEFINES']
55
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040056@Configure.conf
57def check_compiler_flags(conf):
Davide Pesaventod06ea052015-10-02 01:48:24 +020058 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Alexander Afanasyev01515792014-12-16 14:20:01 -080059 # corresponding environment variables are not set.
Davide Pesaventod06ea052015-10-02 01:48:24 +020060 # DEFINES are always applied.
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070061 if conf.options.debug:
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040062 extraFlags = conf.flags.getDebugFlags(conf)
63 if conf.areCustomCxxflagsPresent:
Alexander Afanasyev01515792014-12-16 14:20:01 -080064 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev5560fd42018-03-07 17:03:03 -050065 if missingFlags:
66 Logs.warn('Selected debug mode, but CXXFLAGS is set to a custom value "%s"'
67 % ' '.join(conf.env.CXXFLAGS))
68 Logs.warn('Default flags "%s" will not be used' % ' '.join(missingFlags))
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070069 else:
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040070 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070071
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040072 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyev01515792014-12-16 14:20:01 -080073 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Davide Pesavento7c02b472015-10-28 20:50:17 +010074 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Alexander Afanasyev01515792014-12-16 14:20:01 -080075
76 conf.env.DEFINES += extraFlags['DEFINES']
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020077
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070078@Configure.conf
79def add_supported_cxxflags(self, cxxflags):
80 """
81 Check which cxxflags are supported by compiler and add them to env.CXXFLAGS variable
82 """
Alexander Afanasyev01515792014-12-16 14:20:01 -080083 if len(cxxflags) == 0:
84 return
85
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020086 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070087
88 supportedFlags = []
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040089 for flags in cxxflags:
90 flags = Utils.to_list(flags)
91 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
92 supportedFlags += flags
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070093
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -070094 self.end_msg(' '.join(supportedFlags))
Davide Pesavento1349d2d2016-08-09 06:43:57 +020095 self.env.prepend_value('CXXFLAGS', supportedFlags)
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020096
97@Configure.conf
98def add_supported_linkflags(self, linkflags):
99 """
100 Check which linkflags are supported by compiler and add them to env.LINKFLAGS variable
101 """
Alexander Afanasyev01515792014-12-16 14:20:01 -0800102 if len(linkflags) == 0:
103 return
104
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +0200105 self.start_msg('Checking supported LINKFLAGS')
106
107 supportedFlags = []
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -0400108 for flags in linkflags:
109 flags = Utils.to_list(flags)
110 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
111 supportedFlags += flags
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +0200112
113 self.end_msg(' '.join(supportedFlags))
Davide Pesavento1349d2d2016-08-09 06:43:57 +0200114 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyev01515792014-12-16 14:20:01 -0800115
116
117class CompilerFlags(object):
Alexander Afanasyev5560fd42018-03-07 17:03:03 -0500118 def getCompilerVersion(self, conf):
119 return tuple(int(i) for i in conf.env.CC_VERSION)
120
Alexander Afanasyev01515792014-12-16 14:20:01 -0800121 def getGeneralFlags(self, conf):
Davide Pesavento7c02b472015-10-28 20:50:17 +0100122 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Alexander Afanasyev01515792014-12-16 14:20:01 -0800123 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
124
125 def getDebugFlags(self, conf):
Davide Pesavento7c02b472015-10-28 20:50:17 +0100126 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Alexander Afanasyev01515792014-12-16 14:20:01 -0800127 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
128
129 def getOptimizedFlags(self, conf):
Davide Pesavento7c02b472015-10-28 20:50:17 +0100130 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Alexander Afanasyev01515792014-12-16 14:20:01 -0800131 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
132
133class GccBasicFlags(CompilerFlags):
Davide Pesaventod06ea052015-10-02 01:48:24 +0200134 """
135 This class defines basic flags that work for both gcc and clang compilers
136 """
Davide Pesavento724d65a2017-06-23 17:14:08 -0400137 def getGeneralFlags(self, conf):
138 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
Davide Pesavento1fd00242018-05-20 00:11:01 -0400139 flags['CXXFLAGS'] += ['-std=c++14']
Davide Pesaventoea5ce492022-09-30 20:25:25 -0400140 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesavento64ae55d2018-12-19 00:25:47 -0500141 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Davide Pesavento724d65a2017-06-23 17:14:08 -0400142 return flags
143
Alexander Afanasyev01515792014-12-16 14:20:01 -0800144 def getDebugFlags(self, conf):
145 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Davide Pesavento71c622b2020-04-24 01:39:01 -0400146 flags['CXXFLAGS'] += ['-Og',
Alexander Afanasyev01515792014-12-16 14:20:01 -0800147 '-g3',
Davide Pesavento7c02b472015-10-28 20:50:17 +0100148 '-Wall',
149 '-Wextra',
Davide Pesavento81de5d92022-12-30 01:08:05 -0500150 '-Wpedantic',
Alexander Afanasyev01515792014-12-16 14:20:01 -0800151 '-Werror',
Davide Pesavento71c622b2020-04-24 01:39:01 -0400152 '-Wcatch-value=2',
153 '-Wextra-semi',
Davide Pesavento7f20d6e2017-01-16 14:43:58 -0500154 '-Wnon-virtual-dtor',
Alexander Afanasyev465c9732016-10-11 16:35:32 -0700155 '-Wno-error=deprecated-declarations', # Bug #3795
Davide Pesavento88a0d812017-08-19 21:31:42 -0400156 '-Wno-error=maybe-uninitialized', # Bug #1615
157 '-Wno-unused-parameter',
Davide Pesavento1aafba42015-09-17 17:22:31 -0700158 ]
Davide Pesavento64ae55d2018-12-19 00:25:47 -0500159 flags['LINKFLAGS'] += ['-Wl,-O1']
Alexander Afanasyev01515792014-12-16 14:20:01 -0800160 return flags
161
162 def getOptimizedFlags(self, conf):
163 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
Davide Pesavento7c02b472015-10-28 20:50:17 +0100164 flags['CXXFLAGS'] += ['-O2',
Davide Pesavento1aafba42015-09-17 17:22:31 -0700165 '-g',
Davide Pesavento7c02b472015-10-28 20:50:17 +0100166 '-Wall',
167 '-Wextra',
Davide Pesavento81de5d92022-12-30 01:08:05 -0500168 '-Wpedantic',
Davide Pesavento71c622b2020-04-24 01:39:01 -0400169 '-Wcatch-value=2',
170 '-Wextra-semi',
Davide Pesavento7f20d6e2017-01-16 14:43:58 -0500171 '-Wnon-virtual-dtor',
Davide Pesavento7c02b472015-10-28 20:50:17 +0100172 '-Wno-unused-parameter',
Davide Pesavento1aafba42015-09-17 17:22:31 -0700173 ]
Davide Pesavento64ae55d2018-12-19 00:25:47 -0500174 flags['LINKFLAGS'] += ['-Wl,-O1']
Alexander Afanasyev01515792014-12-16 14:20:01 -0800175 return flags
176
177class GccFlags(GccBasicFlags):
Alexander Afanasyev01515792014-12-16 14:20:01 -0800178 def getDebugFlags(self, conf):
179 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesavento71c622b2020-04-24 01:39:01 -0400180 flags['CXXFLAGS'] += ['-fdiagnostics-color',
181 '-Wredundant-tags',
182 ]
Davide Pesavento541a8222022-03-01 15:08:42 -0500183 if platform.machine() == 'armv7l':
Davide Pesavento71c622b2020-04-24 01:39:01 -0400184 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyev01515792014-12-16 14:20:01 -0800185 return flags
186
Davide Pesaventod06ea052015-10-02 01:48:24 +0200187 def getOptimizedFlags(self, conf):
188 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesavento71c622b2020-04-24 01:39:01 -0400189 flags['CXXFLAGS'] += ['-fdiagnostics-color',
190 '-Wredundant-tags',
191 ]
Davide Pesavento541a8222022-03-01 15:08:42 -0500192 if platform.machine() == 'armv7l':
Davide Pesavento71c622b2020-04-24 01:39:01 -0400193 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Davide Pesaventod06ea052015-10-02 01:48:24 +0200194 return flags
195
Alexander Afanasyev01515792014-12-16 14:20:01 -0800196class ClangFlags(GccBasicFlags):
197 def getGeneralFlags(self, conf):
198 flags = super(ClangFlags, self).getGeneralFlags(conf)
Davide Pesavento71c622b2020-04-24 01:39:01 -0400199 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev5560fd42018-03-07 17:03:03 -0500200 # Bug #4296
Davide Pesaventof6625002022-07-31 17:15:02 -0400201 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
202 flags['CXXFLAGS'] += [['-isystem', f'{brewdir}/include'], # for Homebrew
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -0400203 ['-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 ]
215 return flags
216
217 def getOptimizedFlags(self, conf):
218 flags = super(ClangFlags, self).getOptimizedFlags(conf)
219 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesavento88a0d812017-08-19 21:31:42 -0400220 '-Wundefined-func-template',
Davide Pesaventod06ea052015-10-02 01:48:24 +0200221 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Davide Pesavento1aafba42015-09-17 17:22:31 -0700222 ]
Alexander Afanasyev01515792014-12-16 14:20:01 -0800223 return flags