blob: c3a87e556c04ff6ae825ffd8e1647c2c9e815119 [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
3from waflib import Logs, Configure, Utils
4
5def options(opt):
6 opt.add_option('--debug', '--with-debug', action='store_true', default=False, dest='debug',
Davide Pesaventodd0e1952017-06-24 13:37:13 -04007 help='''Compile in debugging mode with minimal optimizations (-O0 or -Og)''')
Junxiao Shif7191242015-03-19 05:53:41 -07008
9def configure(conf):
Davide Pesaventoc0702702017-08-24 22:04:00 -040010 conf.start_msg('Checking C++ compiler version')
11
12 cxx = conf.env['CXX_NAME'] # CXX_NAME is the generic name of the compiler
Davide Pesaventodd0e1952017-06-24 13:37:13 -040013 ccver = tuple(int(i) for i in conf.env['CC_VERSION'])
Davide Pesaventoc0702702017-08-24 22:04:00 -040014 errmsg = ''
15 warnmsg = ''
Junxiao Shif7191242015-03-19 05:53:41 -070016 if cxx == 'gcc':
Davide Pesaventodd0e1952017-06-24 13:37:13 -040017 if ccver < (4, 8, 2):
Davide Pesaventoc0702702017-08-24 22:04:00 -040018 errmsg = ('The version of gcc you are using is too old.\n'
19 'The minimum supported gcc version is 4.8.2.')
Junxiao Shif7191242015-03-19 05:53:41 -070020 flags = GccFlags()
21 elif cxx == 'clang':
Davide Pesaventodd0e1952017-06-24 13:37:13 -040022 if ccver < (3, 4, 0):
Davide Pesaventoc0702702017-08-24 22:04:00 -040023 errmsg = ('The version of clang you are using is too old.\n'
24 'The minimum supported clang version is 3.4.0.')
Junxiao Shif7191242015-03-19 05:53:41 -070025 flags = ClangFlags()
26 else:
Davide Pesaventoc0702702017-08-24 22:04:00 -040027 warnmsg = 'Note: %s compiler is unsupported' % cxx
Junxiao Shif7191242015-03-19 05:53:41 -070028 flags = CompilerFlags()
Davide Pesaventoc0702702017-08-24 22:04:00 -040029
30 if errmsg:
31 conf.end_msg('.'.join(conf.env['CC_VERSION']), color='RED')
32 conf.fatal(errmsg)
33 elif warnmsg:
34 conf.end_msg('.'.join(conf.env['CC_VERSION']), color='YELLOW')
35 Logs.warn(warnmsg)
36 else:
37 conf.end_msg('.'.join(conf.env['CC_VERSION']))
Junxiao Shif7191242015-03-19 05:53:41 -070038
39 areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
40
Junxiao Shic8a0a252015-10-05 07:25:02 -070041 # General flags are always applied (e.g., selecting C++11 mode)
Junxiao Shif7191242015-03-19 05:53:41 -070042 generalFlags = flags.getGeneralFlags(conf)
43 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
44 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
45 conf.env.DEFINES += generalFlags['DEFINES']
46
Junxiao Shic8a0a252015-10-05 07:25:02 -070047 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Junxiao Shif7191242015-03-19 05:53:41 -070048 # corresponding environment variables are not set.
Junxiao Shic8a0a252015-10-05 07:25:02 -070049 # DEFINES are always applied.
Junxiao Shif7191242015-03-19 05:53:41 -070050 if conf.options.debug:
51 extraFlags = flags.getDebugFlags(conf)
Junxiao Shif7191242015-03-19 05:53:41 -070052 if areCustomCxxflagsPresent:
53 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
54 if len(missingFlags) > 0:
55 Logs.warn("Selected debug mode, but CXXFLAGS is set to a custom value '%s'"
56 % " ".join(conf.env.CXXFLAGS))
57 Logs.warn("Default flags '%s' are not activated" % " ".join(missingFlags))
58 else:
59 extraFlags = flags.getOptimizedFlags(conf)
60
61 if not areCustomCxxflagsPresent:
62 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Davide Pesavento3e79c9c2015-11-01 20:38:28 +010063 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Junxiao Shif7191242015-03-19 05:53:41 -070064
65 conf.env.DEFINES += extraFlags['DEFINES']
66
67@Configure.conf
68def add_supported_cxxflags(self, cxxflags):
69 """
70 Check which cxxflags are supported by compiler and add them to env.CXXFLAGS variable
71 """
72 if len(cxxflags) == 0:
73 return
74
75 self.start_msg('Checking supported CXXFLAGS')
76
77 supportedFlags = []
78 for flag in cxxflags:
79 if self.check_cxx(cxxflags=['-Werror', flag], mandatory=False):
80 supportedFlags += [flag]
81
82 self.end_msg(' '.join(supportedFlags))
Davide Pesavento89d91752016-08-14 11:34:09 +020083 self.env.prepend_value('CXXFLAGS', supportedFlags)
Junxiao Shif7191242015-03-19 05:53:41 -070084
85@Configure.conf
86def add_supported_linkflags(self, linkflags):
87 """
88 Check which linkflags are supported by compiler and add them to env.LINKFLAGS variable
89 """
90 if len(linkflags) == 0:
91 return
92
93 self.start_msg('Checking supported LINKFLAGS')
94
95 supportedFlags = []
96 for flag in linkflags:
97 if self.check_cxx(linkflags=['-Werror', flag], mandatory=False):
98 supportedFlags += [flag]
99
100 self.end_msg(' '.join(supportedFlags))
Davide Pesavento89d91752016-08-14 11:34:09 +0200101 self.env.prepend_value('LINKFLAGS', supportedFlags)
Junxiao Shif7191242015-03-19 05:53:41 -0700102
103
104class CompilerFlags(object):
105 def getGeneralFlags(self, conf):
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100106 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Junxiao Shif7191242015-03-19 05:53:41 -0700107 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
108
109 def getDebugFlags(self, conf):
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100110 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Junxiao Shif7191242015-03-19 05:53:41 -0700111 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
112
113 def getOptimizedFlags(self, conf):
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100114 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Junxiao Shif7191242015-03-19 05:53:41 -0700115 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
116
117class GccBasicFlags(CompilerFlags):
Junxiao Shic8a0a252015-10-05 07:25:02 -0700118 """
119 This class defines basic flags that work for both gcc and clang compilers
120 """
Davide Pesaventodd0e1952017-06-24 13:37:13 -0400121 def getGeneralFlags(self, conf):
122 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
123 flags['CXXFLAGS'] += ['-std=c++11']
124 return flags
125
Junxiao Shif7191242015-03-19 05:53:41 -0700126 def getDebugFlags(self, conf):
127 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100128 flags['CXXFLAGS'] += ['-O0',
Davide Pesaventodd0e1952017-06-24 13:37:13 -0400129 '-Og', # gcc >= 4.8, clang >= 4.0
Junxiao Shif7191242015-03-19 05:53:41 -0700130 '-g3',
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100131 '-pedantic',
132 '-Wall',
133 '-Wextra',
Junxiao Shif7191242015-03-19 05:53:41 -0700134 '-Werror',
Davide Pesavento887336c2017-09-19 15:25:23 -0400135 '-Wnon-virtual-dtor',
Alexander Afanasyevfc4aa0d2016-10-15 00:57:33 +0000136 '-Wno-error=deprecated-declarations', # Bug #3795
Davide Pesaventoc0702702017-08-24 22:04:00 -0400137 '-Wno-error=maybe-uninitialized', # Bug #1615
138 '-Wno-unused-parameter',
Junxiao Shic8a0a252015-10-05 07:25:02 -0700139 ]
Davide Pesaventodd0e1952017-06-24 13:37:13 -0400140 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Junxiao Shif7191242015-03-19 05:53:41 -0700141 return flags
142
143 def getOptimizedFlags(self, conf):
144 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100145 flags['CXXFLAGS'] += ['-O2',
Junxiao Shic8a0a252015-10-05 07:25:02 -0700146 '-g',
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100147 '-pedantic',
148 '-Wall',
149 '-Wextra',
Davide Pesavento887336c2017-09-19 15:25:23 -0400150 '-Wnon-virtual-dtor',
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100151 '-Wno-unused-parameter',
Junxiao Shic8a0a252015-10-05 07:25:02 -0700152 ]
Davide Pesaventodd0e1952017-06-24 13:37:13 -0400153 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Junxiao Shif7191242015-03-19 05:53:41 -0700154 return flags
155
156class GccFlags(GccBasicFlags):
Junxiao Shif7191242015-03-19 05:53:41 -0700157 def getDebugFlags(self, conf):
158 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100159 version = tuple(int(i) for i in conf.env['CC_VERSION'])
160 if version < (5, 1, 0):
161 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
Davide Pesaventodd0e1952017-06-24 13:37:13 -0400162 flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
Junxiao Shic8a0a252015-10-05 07:25:02 -0700163 return flags
164
165 def getOptimizedFlags(self, conf):
166 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100167 version = tuple(int(i) for i in conf.env['CC_VERSION'])
168 if version < (5, 1, 0):
169 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
Junxiao Shic8a0a252015-10-05 07:25:02 -0700170 flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
Junxiao Shif7191242015-03-19 05:53:41 -0700171 return flags
172
173class ClangFlags(GccBasicFlags):
174 def getGeneralFlags(self, conf):
175 flags = super(ClangFlags, self).getGeneralFlags(conf)
Junxiao Shic8a0a252015-10-05 07:25:02 -0700176 if Utils.unversioned_sys_platform() == 'darwin':
Junxiao Shif7191242015-03-19 05:53:41 -0700177 flags['CXXFLAGS'] += ['-stdlib=libc++']
178 flags['LINKFLAGS'] += ['-stdlib=libc++']
179 return flags
180
181 def getDebugFlags(self, conf):
182 flags = super(ClangFlags, self).getDebugFlags(conf)
Davide Pesaventoc0702702017-08-24 22:04:00 -0400183 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
184 '-Wextra-semi',
185 '-Wundefined-func-template',
186 '-Wno-error=deprecated-register',
187 '-Wno-error=infinite-recursion', # Bug #3358
188 '-Wno-error=keyword-macro', # Bug #3235
189 '-Wno-error=unneeded-internal-declaration', # Bug #1588
190 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
191 ]
Vince Lehman277ecf02016-02-10 16:37:48 -0600192 version = tuple(int(i) for i in conf.env['CC_VERSION'])
Davide Pesaventoc0702702017-08-24 22:04:00 -0400193 if version < (3, 9, 0) or (Utils.unversioned_sys_platform() == 'darwin' and version < (8, 1, 0)):
194 flags['CXXFLAGS'] += ['-Wno-unknown-pragmas']
Vince Lehman277ecf02016-02-10 16:37:48 -0600195 if Utils.unversioned_sys_platform() == 'darwin' and version < (7, 0, 0):
196 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
Junxiao Shic8a0a252015-10-05 07:25:02 -0700197 return flags
198
199 def getOptimizedFlags(self, conf):
200 flags = super(ClangFlags, self).getOptimizedFlags(conf)
201 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesaventoc0702702017-08-24 22:04:00 -0400202 '-Wextra-semi',
203 '-Wundefined-func-template',
Junxiao Shic8a0a252015-10-05 07:25:02 -0700204 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
205 ]
Davide Pesaventoc0702702017-08-24 22:04:00 -0400206 version = tuple(int(i) for i in conf.env['CC_VERSION'])
207 if version < (3, 9, 0) or (Utils.unversioned_sys_platform() == 'darwin' and version < (8, 1, 0)):
208 flags['CXXFLAGS'] += ['-Wno-unknown-pragmas']
209 if Utils.unversioned_sys_platform() == 'darwin' and version < (7, 0, 0):
210 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
Junxiao Shif7191242015-03-19 05:53:41 -0700211 return flags