blob: bba2b1e6942975f65095adeecbfb061f1b88e587 [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 Afanasyevb82d8c32017-09-21 11:35:07 -040020 conf.flags = GccFlags()
Alexander Afanasyev01515792014-12-16 14:20:01 -080021 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 Afanasyevb82d8c32017-09-21 11:35:07 -040025 conf.flags = ClangFlags()
Alexander Afanasyev01515792014-12-16 14:20:01 -080026 else:
Davide Pesavento88a0d812017-08-19 21:31:42 -040027 warnmsg = 'Note: %s compiler is unsupported' % cxx
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040028 conf.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 Afanasyevb82d8c32017-09-21 11:35:07 -040039 conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
Alexander Afanasyev01515792014-12-16 14:20:01 -080040
Davide Pesaventod06ea052015-10-02 01:48:24 +020041 # General flags are always applied (e.g., selecting C++11 mode)
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040042 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyev01515792014-12-16 14:20:01 -080043 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
44 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
45 conf.env.DEFINES += generalFlags['DEFINES']
46
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040047@Configure.conf
48def check_compiler_flags(conf):
Davide Pesaventod06ea052015-10-02 01:48:24 +020049 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Alexander Afanasyev01515792014-12-16 14:20:01 -080050 # corresponding environment variables are not set.
Davide Pesaventod06ea052015-10-02 01:48:24 +020051 # DEFINES are always applied.
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070052 if conf.options.debug:
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040053 extraFlags = conf.flags.getDebugFlags(conf)
54 if conf.areCustomCxxflagsPresent:
Alexander Afanasyev01515792014-12-16 14:20:01 -080055 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070056 if len(missingFlags) > 0:
57 Logs.warn("Selected debug mode, but CXXFLAGS is set to a custom value '%s'"
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -070058 % " ".join(conf.env.CXXFLAGS))
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070059 Logs.warn("Default flags '%s' are not activated" % " ".join(missingFlags))
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070060 else:
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040061 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070062
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040063 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyev01515792014-12-16 14:20:01 -080064 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Davide Pesavento7c02b472015-10-28 20:50:17 +010065 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Alexander Afanasyev01515792014-12-16 14:20:01 -080066
67 conf.env.DEFINES += extraFlags['DEFINES']
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020068
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070069@Configure.conf
70def add_supported_cxxflags(self, cxxflags):
71 """
72 Check which cxxflags are supported by compiler and add them to env.CXXFLAGS variable
73 """
Alexander Afanasyev01515792014-12-16 14:20:01 -080074 if len(cxxflags) == 0:
75 return
76
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020077 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070078
79 supportedFlags = []
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040080 for flags in cxxflags:
81 flags = Utils.to_list(flags)
82 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
83 supportedFlags += flags
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070084
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -070085 self.end_msg(' '.join(supportedFlags))
Davide Pesavento1349d2d2016-08-09 06:43:57 +020086 self.env.prepend_value('CXXFLAGS', supportedFlags)
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020087
88@Configure.conf
89def add_supported_linkflags(self, linkflags):
90 """
91 Check which linkflags are supported by compiler and add them to env.LINKFLAGS variable
92 """
Alexander Afanasyev01515792014-12-16 14:20:01 -080093 if len(linkflags) == 0:
94 return
95
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020096 self.start_msg('Checking supported LINKFLAGS')
97
98 supportedFlags = []
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040099 for flags in linkflags:
100 flags = Utils.to_list(flags)
101 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
102 supportedFlags += flags
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +0200103
104 self.end_msg(' '.join(supportedFlags))
Davide Pesavento1349d2d2016-08-09 06:43:57 +0200105 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyev01515792014-12-16 14:20:01 -0800106
107
108class CompilerFlags(object):
109 def getGeneralFlags(self, conf):
Davide Pesavento7c02b472015-10-28 20:50:17 +0100110 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Alexander Afanasyev01515792014-12-16 14:20:01 -0800111 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
112
113 def getDebugFlags(self, conf):
Davide Pesavento7c02b472015-10-28 20:50:17 +0100114 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Alexander Afanasyev01515792014-12-16 14:20:01 -0800115 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
116
117 def getOptimizedFlags(self, conf):
Davide Pesavento7c02b472015-10-28 20:50:17 +0100118 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Alexander Afanasyev01515792014-12-16 14:20:01 -0800119 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
120
121class GccBasicFlags(CompilerFlags):
Davide Pesaventod06ea052015-10-02 01:48:24 +0200122 """
123 This class defines basic flags that work for both gcc and clang compilers
124 """
Davide Pesavento724d65a2017-06-23 17:14:08 -0400125 def getGeneralFlags(self, conf):
126 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
127 flags['CXXFLAGS'] += ['-std=c++11']
128 return flags
129
Alexander Afanasyev01515792014-12-16 14:20:01 -0800130 def getDebugFlags(self, conf):
131 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Davide Pesavento7c02b472015-10-28 20:50:17 +0100132 flags['CXXFLAGS'] += ['-O0',
Davide Pesaventof714c1f2017-04-12 23:53:40 -0400133 '-Og', # gcc >= 4.8, clang >= 4.0
Alexander Afanasyev01515792014-12-16 14:20:01 -0800134 '-g3',
Davide Pesavento7c02b472015-10-28 20:50:17 +0100135 '-pedantic',
136 '-Wall',
137 '-Wextra',
Alexander Afanasyev01515792014-12-16 14:20:01 -0800138 '-Werror',
Davide Pesavento7f20d6e2017-01-16 14:43:58 -0500139 '-Wnon-virtual-dtor',
Alexander Afanasyev465c9732016-10-11 16:35:32 -0700140 '-Wno-error=deprecated-declarations', # Bug #3795
Davide Pesavento88a0d812017-08-19 21:31:42 -0400141 '-Wno-error=maybe-uninitialized', # Bug #1615
142 '-Wno-unused-parameter',
Davide Pesavento1aafba42015-09-17 17:22:31 -0700143 ]
Davide Pesavento37271c52017-06-23 00:58:50 -0400144 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Alexander Afanasyev01515792014-12-16 14:20:01 -0800145 return flags
146
147 def getOptimizedFlags(self, conf):
148 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
Davide Pesavento7c02b472015-10-28 20:50:17 +0100149 flags['CXXFLAGS'] += ['-O2',
Davide Pesavento1aafba42015-09-17 17:22:31 -0700150 '-g',
Davide Pesavento7c02b472015-10-28 20:50:17 +0100151 '-pedantic',
152 '-Wall',
153 '-Wextra',
Davide Pesavento7f20d6e2017-01-16 14:43:58 -0500154 '-Wnon-virtual-dtor',
Davide Pesavento7c02b472015-10-28 20:50:17 +0100155 '-Wno-unused-parameter',
Davide Pesavento1aafba42015-09-17 17:22:31 -0700156 ]
Davide Pesavento37271c52017-06-23 00:58:50 -0400157 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Alexander Afanasyev01515792014-12-16 14:20:01 -0800158 return flags
159
160class GccFlags(GccBasicFlags):
Alexander Afanasyev01515792014-12-16 14:20:01 -0800161 def getDebugFlags(self, conf):
162 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesavento7c02b472015-10-28 20:50:17 +0100163 version = tuple(int(i) for i in conf.env['CC_VERSION'])
164 if version < (5, 1, 0):
165 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
Davide Pesaventof714c1f2017-04-12 23:53:40 -0400166 flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
Alexander Afanasyev01515792014-12-16 14:20:01 -0800167 return flags
168
Davide Pesaventod06ea052015-10-02 01:48:24 +0200169 def getOptimizedFlags(self, conf):
170 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesavento7c02b472015-10-28 20:50:17 +0100171 version = tuple(int(i) for i in conf.env['CC_VERSION'])
172 if version < (5, 1, 0):
173 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
Davide Pesaventod06ea052015-10-02 01:48:24 +0200174 flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
175 return flags
176
Alexander Afanasyev01515792014-12-16 14:20:01 -0800177class ClangFlags(GccBasicFlags):
178 def getGeneralFlags(self, conf):
179 flags = super(ClangFlags, self).getGeneralFlags(conf)
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -0400180 version = tuple(int(i) for i in conf.env['CC_VERSION'])
181 if Utils.unversioned_sys_platform() == 'darwin' and version >= (9, 0, 0): # Bug #4296
182 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include'], # for Homebrew
183 ['-isystem', '/opt/local/include']] # for MacPorts
Alexander Afanasyev01515792014-12-16 14:20:01 -0800184 return flags
185
186 def getDebugFlags(self, conf):
187 flags = super(ClangFlags, self).getDebugFlags(conf)
Davide Pesavento1aafba42015-09-17 17:22:31 -0700188 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesavento88a0d812017-08-19 21:31:42 -0400189 '-Wextra-semi',
190 '-Wundefined-func-template',
Davide Pesavento1aafba42015-09-17 17:22:31 -0700191 '-Wno-error=deprecated-register',
Alexander Afanasyevefa22d62015-12-04 13:39:17 -0800192 '-Wno-error=infinite-recursion', # Bug #3358
Davide Pesavento88a0d812017-08-19 21:31:42 -0400193 '-Wno-error=keyword-macro', # Bug #3235
194 '-Wno-error=unneeded-internal-declaration', # Bug #1588
195 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Davide Pesaventod06ea052015-10-02 01:48:24 +0200196 ]
Davide Pesavento88a0d812017-08-19 21:31:42 -0400197 version = tuple(int(i) for i in conf.env['CC_VERSION'])
198 if version < (3, 9, 0) or (Utils.unversioned_sys_platform() == 'darwin' and version < (8, 1, 0)):
199 flags['CXXFLAGS'] += ['-Wno-unknown-pragmas']
Davide Pesaventod06ea052015-10-02 01:48:24 +0200200 return flags
201
202 def getOptimizedFlags(self, conf):
203 flags = super(ClangFlags, self).getOptimizedFlags(conf)
204 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesavento88a0d812017-08-19 21:31:42 -0400205 '-Wextra-semi',
206 '-Wundefined-func-template',
Davide Pesaventod06ea052015-10-02 01:48:24 +0200207 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Davide Pesavento1aafba42015-09-17 17:22:31 -0700208 ]
Davide Pesavento88a0d812017-08-19 21:31:42 -0400209 version = tuple(int(i) for i in conf.env['CC_VERSION'])
210 if version < (3, 9, 0) or (Utils.unversioned_sys_platform() == 'darwin' and version < (8, 1, 0)):
211 flags['CXXFLAGS'] += ['-Wno-unknown-pragmas']
Alexander Afanasyev01515792014-12-16 14:20:01 -0800212 return flags