blob: f3be6e7bf841b3eb83173308ea7b1f877c0cfac1 [file] [log] [blame]
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -07001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -07002
Davide Pesavento17521592020-05-14 19:01:32 -04003import platform
Davide Pesavento0064c1d2018-03-03 18:43:53 -05004from waflib import Configure, Logs, Utils
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -07005
6def options(opt):
Davide Pesavento0064c1d2018-03-03 18:43:53 -05007 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesavento17521592020-05-14 19:01:32 -04008 help='Compile in debugging mode with minimal optimizations (-Og)')
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -07009
10def configure(conf):
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040011 conf.start_msg('Checking C++ compiler version')
12
Davide Pesavento0064c1d2018-03-03 18:43:53 -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 Pesaventoc0a5a392017-08-19 16:49:30 -040016 errmsg = ''
17 warnmsg = ''
Alexander Afanasyev76818632015-01-26 21:30:45 -080018 if cxx == 'gcc':
Davide Pesavento5f35f642018-05-10 19:36:03 -040019 if ccver < (5, 3, 0):
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040020 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesavento17521592020-05-14 19:01:32 -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 Afanasyevb5220702017-09-21 18:57:00 -040025 conf.flags = GccFlags()
Alexander Afanasyev76818632015-01-26 21:30:45 -080026 elif cxx == 'clang':
Davide Pesavento17521592020-05-14 19:01:32 -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 Pesaventoc0a5a392017-08-19 16:49:30 -040031 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesavento17521592020-05-14 19:01:32 -040032 'The minimum supported clang version is 4.0.')
Alexander Afanasyevb5220702017-09-21 18:57:00 -040033 conf.flags = ClangFlags()
Alexander Afanasyev76818632015-01-26 21:30:45 -080034 else:
Davide Pesavento17521592020-05-14 19:01:32 -040035 warnmsg = '%s compiler is unsupported' % cxx
Alexander Afanasyevb5220702017-09-21 18:57:00 -040036 conf.flags = CompilerFlags()
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040037
38 if errmsg:
Davide Pesavento0064c1d2018-03-03 18:43:53 -050039 conf.end_msg(ccverstr, color='RED')
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040040 conf.fatal(errmsg)
41 elif warnmsg:
Davide Pesavento0064c1d2018-03-03 18:43:53 -050042 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento17521592020-05-14 19:01:32 -040043 Logs.warn('WARNING: ' + warnmsg)
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040044 else:
Davide Pesavento0064c1d2018-03-03 18:43:53 -050045 conf.end_msg(ccverstr)
Davide Pesavento1bdef282014-04-08 20:59:50 +020046
Alexander Afanasyevb5220702017-09-21 18:57:00 -040047 conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
Alexander Afanasyev76818632015-01-26 21:30:45 -080048
Davide Pesaventoc0822fa2018-05-10 21:54:10 -040049 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyevb5220702017-09-21 18:57:00 -040050 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyev76818632015-01-26 21:30:45 -080051 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
52 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
53 conf.env.DEFINES += generalFlags['DEFINES']
54
Alexander Afanasyevb5220702017-09-21 18:57:00 -040055@Configure.conf
56def check_compiler_flags(conf):
Davide Pesaventof485d892015-10-02 02:00:54 +020057 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Alexander Afanasyev76818632015-01-26 21:30:45 -080058 # corresponding environment variables are not set.
Davide Pesaventof485d892015-10-02 02:00:54 +020059 # DEFINES are always applied.
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070060 if conf.options.debug:
Alexander Afanasyevb5220702017-09-21 18:57:00 -040061 extraFlags = conf.flags.getDebugFlags(conf)
62 if conf.areCustomCxxflagsPresent:
Alexander Afanasyev76818632015-01-26 21:30:45 -080063 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Davide Pesavento0064c1d2018-03-03 18:43:53 -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 Afanasyev97e4cac2014-03-28 10:55:11 -070068 else:
Alexander Afanasyevb5220702017-09-21 18:57:00 -040069 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070070
Alexander Afanasyevb5220702017-09-21 18:57:00 -040071 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyev76818632015-01-26 21:30:45 -080072 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Davide Pesaventoed89ccc2015-11-01 20:35:04 +010073 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Alexander Afanasyev76818632015-01-26 21:30:45 -080074
75 conf.env.DEFINES += extraFlags['DEFINES']
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020076
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -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 Afanasyev76818632015-01-26 21:30:45 -080082 if len(cxxflags) == 0:
83 return
84
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020085 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070086
87 supportedFlags = []
Alexander Afanasyevb5220702017-09-21 18:57:00 -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 Afanasyev97e4cac2014-03-28 10:55:11 -070092
Davide Pesavento1bdef282014-04-08 20:59:50 +020093 self.end_msg(' '.join(supportedFlags))
Davide Pesavento67f30272016-08-10 01:55:16 +000094 self.env.prepend_value('CXXFLAGS', supportedFlags)
Davide Pesaventoab1e8f22014-10-21 22:45:33 +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 Afanasyev76818632015-01-26 21:30:45 -0800101 if len(linkflags) == 0:
102 return
103
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200104 self.start_msg('Checking supported LINKFLAGS')
105
106 supportedFlags = []
Alexander Afanasyevb5220702017-09-21 18:57:00 -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 Pesaventoab1e8f22014-10-21 22:45:33 +0200111
112 self.end_msg(' '.join(supportedFlags))
Davide Pesavento67f30272016-08-10 01:55:16 +0000113 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyev76818632015-01-26 21:30:45 -0800114
115
116class CompilerFlags(object):
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500117 def getCompilerVersion(self, conf):
118 return tuple(int(i) for i in conf.env.CC_VERSION)
119
Alexander Afanasyev76818632015-01-26 21:30:45 -0800120 def getGeneralFlags(self, conf):
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100121 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Alexander Afanasyev76818632015-01-26 21:30:45 -0800122 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
123
124 def getDebugFlags(self, conf):
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100125 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Alexander Afanasyev76818632015-01-26 21:30:45 -0800126 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
127
128 def getOptimizedFlags(self, conf):
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100129 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Alexander Afanasyev76818632015-01-26 21:30:45 -0800130 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
131
132class GccBasicFlags(CompilerFlags):
Davide Pesaventof485d892015-10-02 02:00:54 +0200133 """
134 This class defines basic flags that work for both gcc and clang compilers
135 """
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -0400136 def getGeneralFlags(self, conf):
137 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
Davide Pesaventoc0822fa2018-05-10 21:54:10 -0400138 flags['CXXFLAGS'] += ['-std=c++14']
Davide Pesaventodfbeda22018-12-19 01:15:48 -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 Pesavento8eb5f8f2017-06-24 02:19:42 -0400143 return flags
144
Alexander Afanasyev76818632015-01-26 21:30:45 -0800145 def getDebugFlags(self, conf):
146 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Davide Pesavento17521592020-05-14 19:01:32 -0400147 flags['CXXFLAGS'] += ['-Og',
Alexander Afanasyev76818632015-01-26 21:30:45 -0800148 '-g3',
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100149 '-pedantic',
150 '-Wall',
151 '-Wextra',
Alexander Afanasyev76818632015-01-26 21:30:45 -0800152 '-Werror',
Davide Pesavento17521592020-05-14 19:01:32 -0400153 '-Wcatch-value=2',
154 '-Wextra-semi',
Alexander Afanasyev847de402017-09-21 18:57:30 -0400155 '-Wnon-virtual-dtor',
Alexander Afanasyev436f3bd2016-10-11 16:37:45 -0700156 '-Wno-error=deprecated-declarations', # Bug #3795
Davide Pesaventoc0a5a392017-08-19 16:49:30 -0400157 '-Wno-error=maybe-uninitialized', # Bug #1615
158 '-Wno-unused-parameter',
Davide Pesaventof485d892015-10-02 02:00:54 +0200159 ]
Davide Pesaventodfbeda22018-12-19 01:15:48 -0500160 flags['LINKFLAGS'] += ['-Wl,-O1']
Alexander Afanasyev76818632015-01-26 21:30:45 -0800161 return flags
162
163 def getOptimizedFlags(self, conf):
164 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100165 flags['CXXFLAGS'] += ['-O2',
Davide Pesaventof485d892015-10-02 02:00:54 +0200166 '-g',
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100167 '-pedantic',
168 '-Wall',
169 '-Wextra',
Davide Pesavento17521592020-05-14 19:01:32 -0400170 '-Wcatch-value=2',
171 '-Wextra-semi',
Alexander Afanasyev847de402017-09-21 18:57:30 -0400172 '-Wnon-virtual-dtor',
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100173 '-Wno-unused-parameter',
Davide Pesaventof485d892015-10-02 02:00:54 +0200174 ]
Davide Pesaventodfbeda22018-12-19 01:15:48 -0500175 flags['LINKFLAGS'] += ['-Wl,-O1']
Alexander Afanasyev76818632015-01-26 21:30:45 -0800176 return flags
177
178class GccFlags(GccBasicFlags):
Alexander Afanasyev76818632015-01-26 21:30:45 -0800179 def getDebugFlags(self, conf):
180 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesavento17521592020-05-14 19:01:32 -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
Davide Pesaventof485d892015-10-02 02:00:54 +0200186 return flags
187
188 def getOptimizedFlags(self, conf):
189 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesavento17521592020-05-14 19:01:32 -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
Alexander Afanasyev76818632015-01-26 21:30:45 -0800195 return flags
196
197class ClangFlags(GccBasicFlags):
198 def getGeneralFlags(self, conf):
199 flags = super(ClangFlags, self).getGeneralFlags(conf)
Davide Pesavento17521592020-05-14 19:01:32 -0400200 if Utils.unversioned_sys_platform() == 'darwin':
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500201 # Bug #4296
Alexander Afanasyevb5220702017-09-21 18:57:00 -0400202 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include'], # for Homebrew
203 ['-isystem', '/opt/local/include']] # for MacPorts
Davide Pesavento17521592020-05-14 19:01:32 -0400204 elif Utils.unversioned_sys_platform() == 'freebsd':
205 # Bug #4790
206 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Alexander Afanasyev76818632015-01-26 21:30:45 -0800207 return flags
208
209 def getDebugFlags(self, conf):
210 flags = super(ClangFlags, self).getDebugFlags(conf)
Davide Pesaventof485d892015-10-02 02:00:54 +0200211 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesaventoc0a5a392017-08-19 16:49:30 -0400212 '-Wundefined-func-template',
Davide Pesaventoc0a5a392017-08-19 16:49:30 -0400213 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Davide Pesaventof485d892015-10-02 02:00:54 +0200214 ]
Davide Pesavento17521592020-05-14 19:01:32 -0400215 if self.getCompilerVersion(conf) < (6, 0, 0):
Davide Pesavento69692ab2018-09-11 18:11:23 -0400216 flags['CXXFLAGS'] += ['-Wno-missing-braces'] # Bug #4721
Davide Pesaventof485d892015-10-02 02:00:54 +0200217 return flags
218
219 def getOptimizedFlags(self, conf):
220 flags = super(ClangFlags, self).getOptimizedFlags(conf)
221 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesaventoc0a5a392017-08-19 16:49:30 -0400222 '-Wundefined-func-template',
Davide Pesaventof485d892015-10-02 02:00:54 +0200223 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
224 ]
Davide Pesavento17521592020-05-14 19:01:32 -0400225 if self.getCompilerVersion(conf) < (6, 0, 0):
Davide Pesavento69692ab2018-09-11 18:11:23 -0400226 flags['CXXFLAGS'] += ['-Wno-missing-braces'] # Bug #4721
Alexander Afanasyev76818632015-01-26 21:30:45 -0800227 return flags