blob: f3be6e7bf841b3eb83173308ea7b1f877c0cfac1 [file] [log] [blame]
Junxiao Shif7191242015-03-19 05:53:41 -07001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
2
Davide Pesaventob07d7a92020-05-13 23:30:07 -04003import platform
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -05004from waflib import Configure, Logs, Utils
Junxiao Shif7191242015-03-19 05:53:41 -07005
6def options(opt):
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -05007 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesaventob07d7a92020-05-13 23:30:07 -04008 help='Compile in debugging mode with minimal optimizations (-Og)')
Junxiao Shif7191242015-03-19 05:53:41 -07009
10def configure(conf):
Davide Pesaventoc0702702017-08-24 22:04:00 -040011 conf.start_msg('Checking C++ compiler version')
12
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -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 Pesaventoc0702702017-08-24 22:04:00 -040016 errmsg = ''
17 warnmsg = ''
Junxiao Shif7191242015-03-19 05:53:41 -070018 if cxx == 'gcc':
Davide Pesavento26ea1ac2018-05-10 20:20:03 -040019 if ccver < (5, 3, 0):
Davide Pesaventoc0702702017-08-24 22:04:00 -040020 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesaventob07d7a92020-05-13 23:30:07 -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 Afanasyev11e74eb2017-09-21 19:01:54 -040025 conf.flags = GccFlags()
Junxiao Shif7191242015-03-19 05:53:41 -070026 elif cxx == 'clang':
Davide Pesaventob07d7a92020-05-13 23:30:07 -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 Pesaventoc0702702017-08-24 22:04:00 -040031 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesaventob07d7a92020-05-13 23:30:07 -040032 'The minimum supported clang version is 4.0.')
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040033 conf.flags = ClangFlags()
Junxiao Shif7191242015-03-19 05:53:41 -070034 else:
Davide Pesaventob07d7a92020-05-13 23:30:07 -040035 warnmsg = '%s compiler is unsupported' % cxx
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040036 conf.flags = CompilerFlags()
Davide Pesaventoc0702702017-08-24 22:04:00 -040037
38 if errmsg:
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -050039 conf.end_msg(ccverstr, color='RED')
Davide Pesaventoc0702702017-08-24 22:04:00 -040040 conf.fatal(errmsg)
41 elif warnmsg:
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -050042 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesaventob07d7a92020-05-13 23:30:07 -040043 Logs.warn('WARNING: ' + warnmsg)
Davide Pesaventoc0702702017-08-24 22:04:00 -040044 else:
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -050045 conf.end_msg(ccverstr)
Junxiao Shif7191242015-03-19 05:53:41 -070046
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040047 conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
Junxiao Shif7191242015-03-19 05:53:41 -070048
Davide Pesavento60f8cc12018-05-10 22:05:21 -040049 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040050 generalFlags = conf.flags.getGeneralFlags(conf)
Junxiao Shif7191242015-03-19 05:53:41 -070051 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
52 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
53 conf.env.DEFINES += generalFlags['DEFINES']
54
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040055@Configure.conf
56def check_compiler_flags(conf):
Junxiao Shic8a0a252015-10-05 07:25:02 -070057 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Junxiao Shif7191242015-03-19 05:53:41 -070058 # corresponding environment variables are not set.
Junxiao Shic8a0a252015-10-05 07:25:02 -070059 # DEFINES are always applied.
Junxiao Shif7191242015-03-19 05:53:41 -070060 if conf.options.debug:
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040061 extraFlags = conf.flags.getDebugFlags(conf)
62 if conf.areCustomCxxflagsPresent:
Junxiao Shif7191242015-03-19 05:53:41 -070063 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -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))
Junxiao Shif7191242015-03-19 05:53:41 -070068 else:
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040069 extraFlags = conf.flags.getOptimizedFlags(conf)
Junxiao Shif7191242015-03-19 05:53:41 -070070
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040071 if not conf.areCustomCxxflagsPresent:
Junxiao Shif7191242015-03-19 05:53:41 -070072 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Davide Pesavento3e79c9c2015-11-01 20:38:28 +010073 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Junxiao Shif7191242015-03-19 05:53:41 -070074
75 conf.env.DEFINES += extraFlags['DEFINES']
76
77@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 """
82 if len(cxxflags) == 0:
83 return
84
85 self.start_msg('Checking supported CXXFLAGS')
86
87 supportedFlags = []
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040088 for flags in cxxflags:
89 flags = Utils.to_list(flags)
90 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
91 supportedFlags += flags
Junxiao Shif7191242015-03-19 05:53:41 -070092
93 self.end_msg(' '.join(supportedFlags))
Davide Pesavento89d91752016-08-14 11:34:09 +020094 self.env.prepend_value('CXXFLAGS', supportedFlags)
Junxiao Shif7191242015-03-19 05:53:41 -070095
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 """
101 if len(linkflags) == 0:
102 return
103
104 self.start_msg('Checking supported LINKFLAGS')
105
106 supportedFlags = []
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -0400107 for flags in linkflags:
108 flags = Utils.to_list(flags)
109 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
110 supportedFlags += flags
Junxiao Shif7191242015-03-19 05:53:41 -0700111
112 self.end_msg(' '.join(supportedFlags))
Davide Pesavento89d91752016-08-14 11:34:09 +0200113 self.env.prepend_value('LINKFLAGS', supportedFlags)
Junxiao Shif7191242015-03-19 05:53:41 -0700114
115
116class CompilerFlags(object):
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -0500117 def getCompilerVersion(self, conf):
118 return tuple(int(i) for i in conf.env.CC_VERSION)
119
Junxiao Shif7191242015-03-19 05:53:41 -0700120 def getGeneralFlags(self, conf):
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100121 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Junxiao Shif7191242015-03-19 05:53:41 -0700122 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
123
124 def getDebugFlags(self, conf):
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100125 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Junxiao Shif7191242015-03-19 05:53:41 -0700126 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
127
128 def getOptimizedFlags(self, conf):
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100129 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Junxiao Shif7191242015-03-19 05:53:41 -0700130 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
131
132class GccBasicFlags(CompilerFlags):
Junxiao Shic8a0a252015-10-05 07:25:02 -0700133 """
134 This class defines basic flags that work for both gcc and clang compilers
135 """
Davide Pesaventodd0e1952017-06-24 13:37:13 -0400136 def getGeneralFlags(self, conf):
137 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
Davide Pesavento60f8cc12018-05-10 22:05:21 -0400138 flags['CXXFLAGS'] += ['-std=c++14']
Davide Pesaventoe1b18a62018-12-19 01:17:42 -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 Pesaventodd0e1952017-06-24 13:37:13 -0400143 return flags
144
Junxiao Shif7191242015-03-19 05:53:41 -0700145 def getDebugFlags(self, conf):
146 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400147 flags['CXXFLAGS'] += ['-Og',
Junxiao Shif7191242015-03-19 05:53:41 -0700148 '-g3',
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100149 '-pedantic',
150 '-Wall',
151 '-Wextra',
Junxiao Shif7191242015-03-19 05:53:41 -0700152 '-Werror',
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400153 '-Wcatch-value=2',
154 '-Wextra-semi',
Davide Pesavento887336c2017-09-19 15:25:23 -0400155 '-Wnon-virtual-dtor',
Alexander Afanasyevfc4aa0d2016-10-15 00:57:33 +0000156 '-Wno-error=deprecated-declarations', # Bug #3795
Davide Pesaventoc0702702017-08-24 22:04:00 -0400157 '-Wno-error=maybe-uninitialized', # Bug #1615
158 '-Wno-unused-parameter',
Junxiao Shic8a0a252015-10-05 07:25:02 -0700159 ]
Davide Pesaventoe1b18a62018-12-19 01:17:42 -0500160 flags['LINKFLAGS'] += ['-Wl,-O1']
Junxiao Shif7191242015-03-19 05:53:41 -0700161 return flags
162
163 def getOptimizedFlags(self, conf):
164 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100165 flags['CXXFLAGS'] += ['-O2',
Junxiao Shic8a0a252015-10-05 07:25:02 -0700166 '-g',
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100167 '-pedantic',
168 '-Wall',
169 '-Wextra',
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400170 '-Wcatch-value=2',
171 '-Wextra-semi',
Davide Pesavento887336c2017-09-19 15:25:23 -0400172 '-Wnon-virtual-dtor',
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100173 '-Wno-unused-parameter',
Junxiao Shic8a0a252015-10-05 07:25:02 -0700174 ]
Davide Pesaventoe1b18a62018-12-19 01:17:42 -0500175 flags['LINKFLAGS'] += ['-Wl,-O1']
Junxiao Shif7191242015-03-19 05:53:41 -0700176 return flags
177
178class GccFlags(GccBasicFlags):
Junxiao Shif7191242015-03-19 05:53:41 -0700179 def getDebugFlags(self, conf):
180 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesaventob07d7a92020-05-13 23:30:07 -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
Junxiao Shic8a0a252015-10-05 07:25:02 -0700186 return flags
187
188 def getOptimizedFlags(self, conf):
189 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesaventob07d7a92020-05-13 23:30:07 -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
Junxiao Shif7191242015-03-19 05:53:41 -0700195 return flags
196
197class ClangFlags(GccBasicFlags):
198 def getGeneralFlags(self, conf):
199 flags = super(ClangFlags, self).getGeneralFlags(conf)
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400200 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -0500201 # Bug #4296
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -0400202 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include'], # for Homebrew
203 ['-isystem', '/opt/local/include']] # for MacPorts
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400204 elif Utils.unversioned_sys_platform() == 'freebsd':
205 # Bug #4790
206 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Junxiao Shif7191242015-03-19 05:53:41 -0700207 return flags
208
209 def getDebugFlags(self, conf):
210 flags = super(ClangFlags, self).getDebugFlags(conf)
Davide Pesaventoc0702702017-08-24 22:04:00 -0400211 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesaventoc0702702017-08-24 22:04:00 -0400212 '-Wundefined-func-template',
Davide Pesaventoc0702702017-08-24 22:04:00 -0400213 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
214 ]
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400215 if self.getCompilerVersion(conf) < (6, 0, 0):
Davide Pesavento88beb4d2018-09-11 18:14:18 -0400216 flags['CXXFLAGS'] += ['-Wno-missing-braces'] # Bug #4721
Junxiao Shic8a0a252015-10-05 07:25:02 -0700217 return flags
218
219 def getOptimizedFlags(self, conf):
220 flags = super(ClangFlags, self).getOptimizedFlags(conf)
221 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesaventoc0702702017-08-24 22:04:00 -0400222 '-Wundefined-func-template',
Junxiao Shic8a0a252015-10-05 07:25:02 -0700223 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
224 ]
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400225 if self.getCompilerVersion(conf) < (6, 0, 0):
Davide Pesavento88beb4d2018-09-11 18:14:18 -0400226 flags['CXXFLAGS'] += ['-Wno-missing-braces'] # Bug #4721
Junxiao Shif7191242015-03-19 05:53:41 -0700227 return flags