blob: 8c718717e3da2f62156c901c59a903bf6b4d7160 [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 Pesavento762548b2023-03-05 11:02:02 -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 Afanasyevb82d8c32017-09-21 11:35:07 -040025 conf.flags = GccFlags()
Alexander Afanasyev01515792014-12-16 14:20:01 -080026 elif cxx == 'clang':
Davide Pesavento541a8222022-03-01 15:08:42 -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 Pesavento762548b2023-03-05 11:02:02 -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 Pesavento541a8222022-03-01 15:08:42 -050033 'officially supported and may result in build failures.')
Davide Pesavento762548b2023-03-05 11:02:02 -050034 elif ccver < (7, 0, 0):
Davide Pesavento88a0d812017-08-19 21:31:42 -040035 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesavento762548b2023-03-05 11:02:02 -050036 'The minimum supported clang version is 7.0.')
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040037 conf.flags = ClangFlags()
Alexander Afanasyev01515792014-12-16 14:20:01 -080038 else:
Davide Pesaventof6625002022-07-31 17:15:02 -040039 warnmsg = f'{cxx} compiler is unsupported'
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040040 conf.flags = CompilerFlags()
Davide Pesavento88a0d812017-08-19 21:31:42 -040041
42 if errmsg:
Alexander Afanasyev5560fd42018-03-07 17:03:03 -050043 conf.end_msg(ccverstr, color='RED')
Davide Pesavento88a0d812017-08-19 21:31:42 -040044 conf.fatal(errmsg)
45 elif warnmsg:
Alexander Afanasyev5560fd42018-03-07 17:03:03 -050046 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento71c622b2020-04-24 01:39:01 -040047 Logs.warn('WARNING: ' + warnmsg)
Davide Pesavento88a0d812017-08-19 21:31:42 -040048 else:
Alexander Afanasyev5560fd42018-03-07 17:03:03 -050049 conf.end_msg(ccverstr)
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -070050
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040051 conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
Alexander Afanasyev01515792014-12-16 14:20:01 -080052
Davide Pesavento1fd00242018-05-20 00:11:01 -040053 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040054 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyev01515792014-12-16 14:20:01 -080055 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
56 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
57 conf.env.DEFINES += generalFlags['DEFINES']
58
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040059@Configure.conf
60def check_compiler_flags(conf):
Davide Pesaventod06ea052015-10-02 01:48:24 +020061 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Alexander Afanasyev01515792014-12-16 14:20:01 -080062 # corresponding environment variables are not set.
Davide Pesaventod06ea052015-10-02 01:48:24 +020063 # DEFINES are always applied.
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070064 if conf.options.debug:
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040065 extraFlags = conf.flags.getDebugFlags(conf)
66 if conf.areCustomCxxflagsPresent:
Alexander Afanasyev01515792014-12-16 14:20:01 -080067 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev5560fd42018-03-07 17:03:03 -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))
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070072 else:
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040073 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070074
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040075 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyev01515792014-12-16 14:20:01 -080076 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Davide Pesavento7c02b472015-10-28 20:50:17 +010077 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Alexander Afanasyev01515792014-12-16 14:20:01 -080078
79 conf.env.DEFINES += extraFlags['DEFINES']
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020080
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070081@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 """
Alexander Afanasyev01515792014-12-16 14:20:01 -080086 if len(cxxflags) == 0:
87 return
88
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020089 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070090
91 supportedFlags = []
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040092 for flags in cxxflags:
93 flags = Utils.to_list(flags)
94 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
95 supportedFlags += flags
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070096
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -070097 self.end_msg(' '.join(supportedFlags))
Davide Pesavento1349d2d2016-08-09 06:43:57 +020098 self.env.prepend_value('CXXFLAGS', supportedFlags)
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020099
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 """
Alexander Afanasyev01515792014-12-16 14:20:01 -0800105 if len(linkflags) == 0:
106 return
107
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +0200108 self.start_msg('Checking supported LINKFLAGS')
109
110 supportedFlags = []
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -0400111 for flags in linkflags:
112 flags = Utils.to_list(flags)
113 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
114 supportedFlags += flags
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +0200115
116 self.end_msg(' '.join(supportedFlags))
Davide Pesavento1349d2d2016-08-09 06:43:57 +0200117 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyev01515792014-12-16 14:20:01 -0800118
119
120class CompilerFlags(object):
Alexander Afanasyev5560fd42018-03-07 17:03:03 -0500121 def getCompilerVersion(self, conf):
122 return tuple(int(i) for i in conf.env.CC_VERSION)
123
Alexander Afanasyev01515792014-12-16 14:20:01 -0800124 def getGeneralFlags(self, conf):
Davide Pesavento7c02b472015-10-28 20:50:17 +0100125 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Alexander Afanasyev01515792014-12-16 14:20:01 -0800126 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
127
128 def getDebugFlags(self, conf):
Davide Pesavento7c02b472015-10-28 20:50:17 +0100129 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Alexander Afanasyev01515792014-12-16 14:20:01 -0800130 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
131
132 def getOptimizedFlags(self, conf):
Davide Pesavento7c02b472015-10-28 20:50:17 +0100133 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Alexander Afanasyev01515792014-12-16 14:20:01 -0800134 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
135
136class GccBasicFlags(CompilerFlags):
Davide Pesaventod06ea052015-10-02 01:48:24 +0200137 """
138 This class defines basic flags that work for both gcc and clang compilers
139 """
Davide Pesavento724d65a2017-06-23 17:14:08 -0400140 def getGeneralFlags(self, conf):
141 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
Davide Pesaventofcd3e442023-03-10 18:44:11 -0500142 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesaventoea5ce492022-09-30 20:25:25 -0400143 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesavento64ae55d2018-12-19 00:25:47 -0500144 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Davide Pesavento724d65a2017-06-23 17:14:08 -0400145 return flags
146
Alexander Afanasyev01515792014-12-16 14:20:01 -0800147 def getDebugFlags(self, conf):
148 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Davide Pesavento71c622b2020-04-24 01:39:01 -0400149 flags['CXXFLAGS'] += ['-Og',
Alexander Afanasyev01515792014-12-16 14:20:01 -0800150 '-g3',
Davide Pesavento7c02b472015-10-28 20:50:17 +0100151 '-Wall',
152 '-Wextra',
Davide Pesavento81de5d92022-12-30 01:08:05 -0500153 '-Wpedantic',
Alexander Afanasyev01515792014-12-16 14:20:01 -0800154 '-Werror',
Davide Pesavento71c622b2020-04-24 01:39:01 -0400155 '-Wcatch-value=2',
156 '-Wextra-semi',
Davide Pesavento7f20d6e2017-01-16 14:43:58 -0500157 '-Wnon-virtual-dtor',
Alexander Afanasyev465c9732016-10-11 16:35:32 -0700158 '-Wno-error=deprecated-declarations', # Bug #3795
Davide Pesavento88a0d812017-08-19 21:31:42 -0400159 '-Wno-error=maybe-uninitialized', # Bug #1615
160 '-Wno-unused-parameter',
Davide Pesavento1aafba42015-09-17 17:22:31 -0700161 ]
Davide Pesavento64ae55d2018-12-19 00:25:47 -0500162 flags['LINKFLAGS'] += ['-Wl,-O1']
Alexander Afanasyev01515792014-12-16 14:20:01 -0800163 return flags
164
165 def getOptimizedFlags(self, conf):
166 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
Davide Pesavento7c02b472015-10-28 20:50:17 +0100167 flags['CXXFLAGS'] += ['-O2',
Davide Pesavento1aafba42015-09-17 17:22:31 -0700168 '-g',
Davide Pesavento7c02b472015-10-28 20:50:17 +0100169 '-Wall',
170 '-Wextra',
Davide Pesavento81de5d92022-12-30 01:08:05 -0500171 '-Wpedantic',
Davide Pesavento71c622b2020-04-24 01:39:01 -0400172 '-Wcatch-value=2',
173 '-Wextra-semi',
Davide Pesavento7f20d6e2017-01-16 14:43:58 -0500174 '-Wnon-virtual-dtor',
Davide Pesavento7c02b472015-10-28 20:50:17 +0100175 '-Wno-unused-parameter',
Davide Pesavento1aafba42015-09-17 17:22:31 -0700176 ]
Davide Pesavento64ae55d2018-12-19 00:25:47 -0500177 flags['LINKFLAGS'] += ['-Wl,-O1']
Alexander Afanasyev01515792014-12-16 14:20:01 -0800178 return flags
179
180class GccFlags(GccBasicFlags):
Alexander Afanasyev01515792014-12-16 14:20:01 -0800181 def getDebugFlags(self, conf):
182 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesavento71c622b2020-04-24 01:39:01 -0400183 flags['CXXFLAGS'] += ['-fdiagnostics-color',
184 '-Wredundant-tags',
185 ]
Davide Pesavento541a8222022-03-01 15:08:42 -0500186 if platform.machine() == 'armv7l':
Davide Pesavento71c622b2020-04-24 01:39:01 -0400187 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyev01515792014-12-16 14:20:01 -0800188 return flags
189
Davide Pesaventod06ea052015-10-02 01:48:24 +0200190 def getOptimizedFlags(self, conf):
191 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesavento71c622b2020-04-24 01:39:01 -0400192 flags['CXXFLAGS'] += ['-fdiagnostics-color',
193 '-Wredundant-tags',
194 ]
Davide Pesavento541a8222022-03-01 15:08:42 -0500195 if platform.machine() == 'armv7l':
Davide Pesavento71c622b2020-04-24 01:39:01 -0400196 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Davide Pesaventod06ea052015-10-02 01:48:24 +0200197 return flags
198
Alexander Afanasyev01515792014-12-16 14:20:01 -0800199class ClangFlags(GccBasicFlags):
200 def getGeneralFlags(self, conf):
201 flags = super(ClangFlags, self).getGeneralFlags(conf)
Davide Pesavento71c622b2020-04-24 01:39:01 -0400202 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev5560fd42018-03-07 17:03:03 -0500203 # Bug #4296
Davide Pesaventof6625002022-07-31 17:15:02 -0400204 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
205 flags['CXXFLAGS'] += [['-isystem', f'{brewdir}/include'], # for Homebrew
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -0400206 ['-isystem', '/opt/local/include']] # for MacPorts
Davide Pesavento71c622b2020-04-24 01:39:01 -0400207 elif Utils.unversioned_sys_platform() == 'freebsd':
208 # Bug #4790
209 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Alexander Afanasyev01515792014-12-16 14:20:01 -0800210 return flags
211
212 def getDebugFlags(self, conf):
213 flags = super(ClangFlags, self).getDebugFlags(conf)
Davide Pesavento1aafba42015-09-17 17:22:31 -0700214 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesavento88a0d812017-08-19 21:31:42 -0400215 '-Wundefined-func-template',
Davide Pesavento88a0d812017-08-19 21:31:42 -0400216 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Davide Pesaventod06ea052015-10-02 01:48:24 +0200217 ]
218 return flags
219
220 def getOptimizedFlags(self, conf):
221 flags = super(ClangFlags, self).getOptimizedFlags(conf)
222 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesavento88a0d812017-08-19 21:31:42 -0400223 '-Wundefined-func-template',
Davide Pesaventod06ea052015-10-02 01:48:24 +0200224 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Davide Pesavento1aafba42015-09-17 17:22:31 -0700225 ]
Alexander Afanasyev01515792014-12-16 14:20:01 -0800226 return flags