blob: f3be6e7bf841b3eb83173308ea7b1f877c0cfac1 [file] [log] [blame]
Yingdi Yu40cd1c32014-04-17 15:02:17 -07001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
Yingdi Yu40cd1c32014-04-17 15:02:17 -07002
Davide Pesavento7ae8b082021-10-12 21:45:47 -04003import platform
Alexander Afanasyev67758b12018-03-06 18:36:44 -05004from waflib import Configure, Logs, Utils
Yingdi Yu40cd1c32014-04-17 15:02:17 -07005
6def options(opt):
Alexander Afanasyev67758b12018-03-06 18:36:44 -05007 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesavento7ae8b082021-10-12 21:45:47 -04008 help='Compile in debugging mode with minimal optimizations (-Og)')
Yingdi Yu40cd1c32014-04-17 15:02:17 -07009
10def configure(conf):
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -040011 conf.start_msg('Checking C++ compiler version')
12
Alexander Afanasyev67758b12018-03-06 18:36:44 -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 Pesavento6ebfd7c2017-08-26 17:52:54 -040016 errmsg = ''
17 warnmsg = ''
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080018 if cxx == 'gcc':
Davide Pesaventoa08dc3f2018-05-24 00:40:28 -040019 if ccver < (5, 3, 0):
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -040020 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesavento7ae8b082021-10-12 21:45:47 -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 Afanasyev67758b12018-03-06 18:36:44 -050025 conf.flags = GccFlags()
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080026 elif cxx == 'clang':
Davide Pesavento7ae8b082021-10-12 21:45:47 -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 Pesavento6ebfd7c2017-08-26 17:52:54 -040031 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesavento7ae8b082021-10-12 21:45:47 -040032 'The minimum supported clang version is 4.0.')
Alexander Afanasyev67758b12018-03-06 18:36:44 -050033 conf.flags = ClangFlags()
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080034 else:
Davide Pesavento7ae8b082021-10-12 21:45:47 -040035 warnmsg = '%s compiler is unsupported' % cxx
Alexander Afanasyev67758b12018-03-06 18:36:44 -050036 conf.flags = CompilerFlags()
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -040037
38 if errmsg:
Alexander Afanasyev67758b12018-03-06 18:36:44 -050039 conf.end_msg(ccverstr, color='RED')
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -040040 conf.fatal(errmsg)
41 elif warnmsg:
Alexander Afanasyev67758b12018-03-06 18:36:44 -050042 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento7ae8b082021-10-12 21:45:47 -040043 Logs.warn('WARNING: ' + warnmsg)
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -040044 else:
Alexander Afanasyev67758b12018-03-06 18:36:44 -050045 conf.end_msg(ccverstr)
Yingdi Yu40cd1c32014-04-17 15:02:17 -070046
Alexander Afanasyev67758b12018-03-06 18:36:44 -050047 conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080048
Davide Pesaventoa08dc3f2018-05-24 00:40:28 -040049 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyev67758b12018-03-06 18:36:44 -050050 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080051 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
52 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
53 conf.env.DEFINES += generalFlags['DEFINES']
54
Alexander Afanasyev67758b12018-03-06 18:36:44 -050055@Configure.conf
56def check_compiler_flags(conf):
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080057 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
58 # corresponding environment variables are not set.
59 # DEFINES are always applied.
Yingdi Yu40cd1c32014-04-17 15:02:17 -070060 if conf.options.debug:
Alexander Afanasyev67758b12018-03-06 18:36:44 -050061 extraFlags = conf.flags.getDebugFlags(conf)
62 if conf.areCustomCxxflagsPresent:
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080063 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev67758b12018-03-06 18:36:44 -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))
Yingdi Yu40cd1c32014-04-17 15:02:17 -070068 else:
Alexander Afanasyev67758b12018-03-06 18:36:44 -050069 extraFlags = conf.flags.getOptimizedFlags(conf)
Yingdi Yu40cd1c32014-04-17 15:02:17 -070070
Alexander Afanasyev67758b12018-03-06 18:36:44 -050071 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080072 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
73 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
74
75 conf.env.DEFINES += extraFlags['DEFINES']
Vince Lehman0a7da612014-10-29 14:39:29 -050076
Yingdi Yu40cd1c32014-04-17 15:02:17 -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 Afanasyevf9f39102015-12-01 17:43:40 -080082 if len(cxxflags) == 0:
83 return
84
85 self.start_msg('Checking supported CXXFLAGS')
Yingdi Yu40cd1c32014-04-17 15:02:17 -070086
87 supportedFlags = []
Alexander Afanasyev67758b12018-03-06 18:36:44 -050088 for flags in cxxflags:
89 flags = Utils.to_list(flags)
90 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
91 supportedFlags += flags
Yingdi Yu40cd1c32014-04-17 15:02:17 -070092
93 self.end_msg(' '.join(supportedFlags))
Davide Pesaventoc58cc7f2017-08-08 16:51:28 -050094 self.env.prepend_value('CXXFLAGS', supportedFlags)
Vince Lehman0a7da612014-10-29 14:39:29 -050095
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 Afanasyevf9f39102015-12-01 17:43:40 -0800101 if len(linkflags) == 0:
102 return
103
104 self.start_msg('Checking supported LINKFLAGS')
Vince Lehman0a7da612014-10-29 14:39:29 -0500105
106 supportedFlags = []
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500107 for flags in linkflags:
108 flags = Utils.to_list(flags)
109 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
110 supportedFlags += flags
Vince Lehman0a7da612014-10-29 14:39:29 -0500111
112 self.end_msg(' '.join(supportedFlags))
Davide Pesaventoc58cc7f2017-08-08 16:51:28 -0500113 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800114
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -0400115
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800116class CompilerFlags(object):
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500117 def getCompilerVersion(self, conf):
118 return tuple(int(i) for i in conf.env.CC_VERSION)
119
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800120 def getGeneralFlags(self, conf):
121 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
122 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
123
124 def getDebugFlags(self, conf):
125 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
126 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
127
128 def getOptimizedFlags(self, conf):
129 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
130 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
131
132class GccBasicFlags(CompilerFlags):
133 """
134 This class defines basic flags that work for both gcc and clang compilers
135 """
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -0400136 def getGeneralFlags(self, conf):
137 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
Davide Pesaventoa08dc3f2018-05-24 00:40:28 -0400138 flags['CXXFLAGS'] += ['-std=c++14']
Davide Pesaventoe5569912019-01-29 19:39:06 -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 Pesavento6ebfd7c2017-08-26 17:52:54 -0400143 return flags
144
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800145 def getDebugFlags(self, conf):
146 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Davide Pesavento7ae8b082021-10-12 21:45:47 -0400147 flags['CXXFLAGS'] += ['-Og',
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800148 '-g3',
149 '-pedantic',
150 '-Wall',
151 '-Wextra',
152 '-Werror',
Davide Pesavento7ae8b082021-10-12 21:45:47 -0400153 '-Wcatch-value=2',
154 '-Wextra-semi',
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500155 '-Wnon-virtual-dtor',
Alexander Afanasyev96bf2f02016-10-11 16:37:45 -0700156 '-Wno-error=deprecated-declarations', # Bug #3795
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -0400157 '-Wno-error=maybe-uninitialized', # Bug #1615
158 '-Wno-unused-parameter',
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800159 ]
Davide Pesaventoe5569912019-01-29 19:39:06 -0500160 flags['LINKFLAGS'] += ['-Wl,-O1']
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800161 return flags
162
163 def getOptimizedFlags(self, conf):
164 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
165 flags['CXXFLAGS'] += ['-O2',
166 '-g',
167 '-pedantic',
168 '-Wall',
169 '-Wextra',
Davide Pesavento7ae8b082021-10-12 21:45:47 -0400170 '-Wcatch-value=2',
171 '-Wextra-semi',
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500172 '-Wnon-virtual-dtor',
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800173 '-Wno-unused-parameter',
174 ]
Davide Pesaventoe5569912019-01-29 19:39:06 -0500175 flags['LINKFLAGS'] += ['-Wl,-O1']
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800176 return flags
177
178class GccFlags(GccBasicFlags):
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800179 def getDebugFlags(self, conf):
180 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesavento7ae8b082021-10-12 21:45:47 -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 Afanasyevf9f39102015-12-01 17:43:40 -0800186 return flags
187
188 def getOptimizedFlags(self, conf):
189 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesavento7ae8b082021-10-12 21:45:47 -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 Afanasyevf9f39102015-12-01 17:43:40 -0800195 return flags
196
197class ClangFlags(GccBasicFlags):
198 def getGeneralFlags(self, conf):
199 flags = super(ClangFlags, self).getGeneralFlags(conf)
Davide Pesavento7ae8b082021-10-12 21:45:47 -0400200 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500201 # Bug #4296
202 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include'], # for Homebrew
203 ['-isystem', '/opt/local/include']] # for MacPorts
Davide Pesavento7ae8b082021-10-12 21:45:47 -0400204 elif Utils.unversioned_sys_platform() == 'freebsd':
205 # Bug #4790
206 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800207 return flags
208
209 def getDebugFlags(self, conf):
210 flags = super(ClangFlags, self).getDebugFlags(conf)
211 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500212 '-Wundefined-func-template',
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -0400213 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800214 ]
Davide Pesavento7ae8b082021-10-12 21:45:47 -0400215 if self.getCompilerVersion(conf) < (6, 0, 0):
Davide Pesaventoe5569912019-01-29 19:39:06 -0500216 flags['CXXFLAGS'] += ['-Wno-missing-braces'] # Bug #4721
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800217 return flags
218
219 def getOptimizedFlags(self, conf):
220 flags = super(ClangFlags, self).getOptimizedFlags(conf)
221 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500222 '-Wundefined-func-template',
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -0400223 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800224 ]
Davide Pesavento7ae8b082021-10-12 21:45:47 -0400225 if self.getCompilerVersion(conf) < (6, 0, 0):
Davide Pesaventoe5569912019-01-29 19:39:06 -0500226 flags['CXXFLAGS'] += ['-Wno-missing-braces'] # Bug #4721
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800227 return flags