blob: 23873feba2db62fd2c2a6db59b6c9048b5286039 [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
Alexander Afanasyev01515792014-12-16 14:20:01 -08003from waflib import Logs, Configure, Utils
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -07004
5def options(opt):
6 opt.add_option('--debug', '--with-debug', action='store_true', default=False, dest='debug',
Davide Pesavento37271c52017-06-23 00:58:50 -04007 help='''Compile in debugging mode with minimal optimizations (-O0 or -Og)''')
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -07008
9def configure(conf):
Davide Pesavento88a0d812017-08-19 21:31:42 -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 Pesavento724d65a2017-06-23 17:14:08 -040013 ccver = tuple(int(i) for i in conf.env['CC_VERSION'])
Davide Pesavento88a0d812017-08-19 21:31:42 -040014 errmsg = ''
15 warnmsg = ''
Alexander Afanasyev01515792014-12-16 14:20:01 -080016 if cxx == 'gcc':
Davide Pesavento724d65a2017-06-23 17:14:08 -040017 if ccver < (4, 8, 2):
Davide Pesavento88a0d812017-08-19 21:31:42 -040018 errmsg = ('The version of gcc you are using is too old.\n'
19 'The minimum supported gcc version is 4.8.2.')
Alexander Afanasyev01515792014-12-16 14:20:01 -080020 flags = GccFlags()
21 elif cxx == 'clang':
Davide Pesavento724d65a2017-06-23 17:14:08 -040022 if ccver < (3, 4, 0):
Davide Pesavento88a0d812017-08-19 21:31:42 -040023 errmsg = ('The version of clang you are using is too old.\n'
24 'The minimum supported clang version is 3.4.0.')
Alexander Afanasyev01515792014-12-16 14:20:01 -080025 flags = ClangFlags()
26 else:
Davide Pesavento88a0d812017-08-19 21:31:42 -040027 warnmsg = 'Note: %s compiler is unsupported' % cxx
Alexander Afanasyev01515792014-12-16 14:20:01 -080028 flags = CompilerFlags()
Davide Pesavento88a0d812017-08-19 21:31:42 -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']))
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -070038
Alexander Afanasyev01515792014-12-16 14:20:01 -080039 areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
40
Davide Pesaventod06ea052015-10-02 01:48:24 +020041 # General flags are always applied (e.g., selecting C++11 mode)
Alexander Afanasyev01515792014-12-16 14:20:01 -080042 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
Davide Pesaventod06ea052015-10-02 01:48:24 +020047 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Alexander Afanasyev01515792014-12-16 14:20:01 -080048 # corresponding environment variables are not set.
Davide Pesaventod06ea052015-10-02 01:48:24 +020049 # DEFINES are always applied.
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070050 if conf.options.debug:
Alexander Afanasyev01515792014-12-16 14:20:01 -080051 extraFlags = flags.getDebugFlags(conf)
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070052 if areCustomCxxflagsPresent:
Alexander Afanasyev01515792014-12-16 14:20:01 -080053 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070054 if len(missingFlags) > 0:
55 Logs.warn("Selected debug mode, but CXXFLAGS is set to a custom value '%s'"
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -070056 % " ".join(conf.env.CXXFLAGS))
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070057 Logs.warn("Default flags '%s' are not activated" % " ".join(missingFlags))
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070058 else:
Alexander Afanasyev01515792014-12-16 14:20:01 -080059 extraFlags = flags.getOptimizedFlags(conf)
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070060
Alexander Afanasyev01515792014-12-16 14:20:01 -080061 if not areCustomCxxflagsPresent:
62 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Davide Pesavento7c02b472015-10-28 20:50:17 +010063 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Alexander Afanasyev01515792014-12-16 14:20:01 -080064
65 conf.env.DEFINES += extraFlags['DEFINES']
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020066
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070067@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 """
Alexander Afanasyev01515792014-12-16 14:20:01 -080072 if len(cxxflags) == 0:
73 return
74
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020075 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070076
77 supportedFlags = []
78 for flag in cxxflags:
Alexander Afanasyev3e6a98e2014-04-25 12:02:13 -070079 if self.check_cxx(cxxflags=['-Werror', flag], mandatory=False):
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070080 supportedFlags += [flag]
81
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -070082 self.end_msg(' '.join(supportedFlags))
Davide Pesavento1349d2d2016-08-09 06:43:57 +020083 self.env.prepend_value('CXXFLAGS', supportedFlags)
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020084
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 """
Alexander Afanasyev01515792014-12-16 14:20:01 -080090 if len(linkflags) == 0:
91 return
92
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020093 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 Pesavento1349d2d2016-08-09 06:43:57 +0200101 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyev01515792014-12-16 14:20:01 -0800102
103
104class CompilerFlags(object):
105 def getGeneralFlags(self, conf):
Davide Pesavento7c02b472015-10-28 20:50:17 +0100106 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Alexander Afanasyev01515792014-12-16 14:20:01 -0800107 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
108
109 def getDebugFlags(self, conf):
Davide Pesavento7c02b472015-10-28 20:50:17 +0100110 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Alexander Afanasyev01515792014-12-16 14:20:01 -0800111 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
112
113 def getOptimizedFlags(self, conf):
Davide Pesavento7c02b472015-10-28 20:50:17 +0100114 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Alexander Afanasyev01515792014-12-16 14:20:01 -0800115 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
116
117class GccBasicFlags(CompilerFlags):
Davide Pesaventod06ea052015-10-02 01:48:24 +0200118 """
119 This class defines basic flags that work for both gcc and clang compilers
120 """
Davide Pesavento724d65a2017-06-23 17:14:08 -0400121 def getGeneralFlags(self, conf):
122 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
123 flags['CXXFLAGS'] += ['-std=c++11']
124 return flags
125
Alexander Afanasyev01515792014-12-16 14:20:01 -0800126 def getDebugFlags(self, conf):
127 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Davide Pesavento7c02b472015-10-28 20:50:17 +0100128 flags['CXXFLAGS'] += ['-O0',
Davide Pesaventof714c1f2017-04-12 23:53:40 -0400129 '-Og', # gcc >= 4.8, clang >= 4.0
Alexander Afanasyev01515792014-12-16 14:20:01 -0800130 '-g3',
Davide Pesavento7c02b472015-10-28 20:50:17 +0100131 '-pedantic',
132 '-Wall',
133 '-Wextra',
Alexander Afanasyev01515792014-12-16 14:20:01 -0800134 '-Werror',
Davide Pesavento7f20d6e2017-01-16 14:43:58 -0500135 '-Wnon-virtual-dtor',
Alexander Afanasyev465c9732016-10-11 16:35:32 -0700136 '-Wno-error=deprecated-declarations', # Bug #3795
Davide Pesavento88a0d812017-08-19 21:31:42 -0400137 '-Wno-error=maybe-uninitialized', # Bug #1615
138 '-Wno-unused-parameter',
Davide Pesavento1aafba42015-09-17 17:22:31 -0700139 ]
Davide Pesavento37271c52017-06-23 00:58:50 -0400140 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Alexander Afanasyev01515792014-12-16 14:20:01 -0800141 return flags
142
143 def getOptimizedFlags(self, conf):
144 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
Davide Pesavento7c02b472015-10-28 20:50:17 +0100145 flags['CXXFLAGS'] += ['-O2',
Davide Pesavento1aafba42015-09-17 17:22:31 -0700146 '-g',
Davide Pesavento7c02b472015-10-28 20:50:17 +0100147 '-pedantic',
148 '-Wall',
149 '-Wextra',
Davide Pesavento7f20d6e2017-01-16 14:43:58 -0500150 '-Wnon-virtual-dtor',
Davide Pesavento7c02b472015-10-28 20:50:17 +0100151 '-Wno-unused-parameter',
Davide Pesavento1aafba42015-09-17 17:22:31 -0700152 ]
Davide Pesavento37271c52017-06-23 00:58:50 -0400153 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Alexander Afanasyev01515792014-12-16 14:20:01 -0800154 return flags
155
156class GccFlags(GccBasicFlags):
Alexander Afanasyev01515792014-12-16 14:20:01 -0800157 def getDebugFlags(self, conf):
158 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesavento7c02b472015-10-28 20:50:17 +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 Pesaventof714c1f2017-04-12 23:53:40 -0400162 flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
Alexander Afanasyev01515792014-12-16 14:20:01 -0800163 return flags
164
Davide Pesaventod06ea052015-10-02 01:48:24 +0200165 def getOptimizedFlags(self, conf):
166 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesavento7c02b472015-10-28 20:50:17 +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']
Davide Pesaventod06ea052015-10-02 01:48:24 +0200170 flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
171 return flags
172
Alexander Afanasyev01515792014-12-16 14:20:01 -0800173class ClangFlags(GccBasicFlags):
174 def getGeneralFlags(self, conf):
175 flags = super(ClangFlags, self).getGeneralFlags(conf)
Davide Pesaventod06ea052015-10-02 01:48:24 +0200176 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev01515792014-12-16 14:20:01 -0800177 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 Pesavento1aafba42015-09-17 17:22:31 -0700183 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesavento88a0d812017-08-19 21:31:42 -0400184 '-Wextra-semi',
185 '-Wundefined-func-template',
Davide Pesavento1aafba42015-09-17 17:22:31 -0700186 '-Wno-error=deprecated-register',
Alexander Afanasyevefa22d62015-12-04 13:39:17 -0800187 '-Wno-error=infinite-recursion', # Bug #3358
Davide Pesavento88a0d812017-08-19 21:31:42 -0400188 '-Wno-error=keyword-macro', # Bug #3235
189 '-Wno-error=unneeded-internal-declaration', # Bug #1588
190 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Davide Pesaventod06ea052015-10-02 01:48:24 +0200191 ]
Davide Pesavento88a0d812017-08-19 21:31:42 -0400192 version = tuple(int(i) for i in conf.env['CC_VERSION'])
193 if version < (3, 9, 0) or (Utils.unversioned_sys_platform() == 'darwin' and version < (8, 1, 0)):
194 flags['CXXFLAGS'] += ['-Wno-unknown-pragmas']
Davide Pesaventod06ea052015-10-02 01:48:24 +0200195 return flags
196
197 def getOptimizedFlags(self, conf):
198 flags = super(ClangFlags, self).getOptimizedFlags(conf)
199 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesavento88a0d812017-08-19 21:31:42 -0400200 '-Wextra-semi',
201 '-Wundefined-func-template',
Davide Pesaventod06ea052015-10-02 01:48:24 +0200202 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Davide Pesavento1aafba42015-09-17 17:22:31 -0700203 ]
Davide Pesavento88a0d812017-08-19 21:31:42 -0400204 version = tuple(int(i) for i in conf.env['CC_VERSION'])
205 if version < (3, 9, 0) or (Utils.unversioned_sys_platform() == 'darwin' and version < (8, 1, 0)):
206 flags['CXXFLAGS'] += ['-Wno-unknown-pragmas']
Alexander Afanasyev01515792014-12-16 14:20:01 -0800207 return flags