blob: bba2b1e6942975f65095adeecbfb061f1b88e587 [file] [log] [blame]
Alexander Afanasyev7eb59112014-07-02 14:21:11 -07001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
Alexander Afanasyev7eb59112014-07-02 14:21:11 -07002
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -08003from waflib import Logs, Configure, Utils
Alexander Afanasyev7eb59112014-07-02 14:21:11 -07004
5def options(opt):
6 opt.add_option('--debug', '--with-debug', action='store_true', default=False, dest='debug',
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -04007 help='''Compile in debugging mode with minimal optimizations (-O0 or -Og)''')
Alexander Afanasyev7eb59112014-07-02 14:21:11 -07008
9def configure(conf):
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040010 conf.start_msg('Checking C++ compiler version')
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070011
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040012 cxx = conf.env['CXX_NAME'] # CXX_NAME is the generic name of the compiler
13 ccver = tuple(int(i) for i in conf.env['CC_VERSION'])
14 errmsg = ''
15 warnmsg = ''
16 if cxx == 'gcc':
17 if ccver < (4, 8, 2):
18 errmsg = ('The version of gcc you are using is too old.\n'
19 'The minimum supported gcc version is 4.8.2.')
20 conf.flags = GccFlags()
21 elif cxx == 'clang':
22 if ccver < (3, 4, 0):
23 errmsg = ('The version of clang you are using is too old.\n'
24 'The minimum supported clang version is 3.4.0.')
25 conf.flags = ClangFlags()
26 else:
27 warnmsg = 'Note: %s compiler is unsupported' % cxx
28 conf.flags = CompilerFlags()
29
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']))
38
39 conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -080040
41 # General flags are always applied (e.g., selecting C++11 mode)
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040042 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -080043 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
44 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
45 conf.env.DEFINES += generalFlags['DEFINES']
46
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040047@Configure.conf
48def check_compiler_flags(conf):
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -080049 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
50 # corresponding environment variables are not set.
51 # DEFINES are always applied.
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070052 if conf.options.debug:
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040053 extraFlags = conf.flags.getDebugFlags(conf)
54 if conf.areCustomCxxflagsPresent:
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -080055 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070056 if len(missingFlags) > 0:
57 Logs.warn("Selected debug mode, but CXXFLAGS is set to a custom value '%s'"
58 % " ".join(conf.env.CXXFLAGS))
59 Logs.warn("Default flags '%s' are not activated" % " ".join(missingFlags))
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070060 else:
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040061 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070062
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040063 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -080064 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
65 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
66
67 conf.env.DEFINES += extraFlags['DEFINES']
Yingdi Yu906c2ea2014-10-31 11:24:50 -070068
Alexander Afanasyev7eb59112014-07-02 14:21:11 -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 Afanasyevf3192eb2016-12-19 17:11:20 -080074 if len(cxxflags) == 0:
75 return
76
Yingdi Yu906c2ea2014-10-31 11:24:50 -070077 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070078
79 supportedFlags = []
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -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 Afanasyev7eb59112014-07-02 14:21:11 -070084
85 self.end_msg(' '.join(supportedFlags))
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -080086 self.env.prepend_value('CXXFLAGS', supportedFlags)
Yingdi Yu906c2ea2014-10-31 11:24:50 -070087
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 Afanasyevf3192eb2016-12-19 17:11:20 -080093 if len(linkflags) == 0:
94 return
95
Yingdi Yu906c2ea2014-10-31 11:24:50 -070096 self.start_msg('Checking supported LINKFLAGS')
97
98 supportedFlags = []
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040099 for flags in linkflags:
100 flags = Utils.to_list(flags)
101 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
102 supportedFlags += flags
Yingdi Yu906c2ea2014-10-31 11:24:50 -0700103
104 self.end_msg(' '.join(supportedFlags))
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800105 self.env.prepend_value('LINKFLAGS', supportedFlags)
106
107
108class CompilerFlags(object):
109 def getGeneralFlags(self, conf):
110 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
111 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
112
113 def getDebugFlags(self, conf):
114 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
115 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
116
117 def getOptimizedFlags(self, conf):
118 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
119 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
120
121class GccBasicFlags(CompilerFlags):
122 """
123 This class defines basic flags that work for both gcc and clang compilers
124 """
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -0400125 def getGeneralFlags(self, conf):
126 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
127 flags['CXXFLAGS'] += ['-std=c++11']
128 return flags
129
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800130 def getDebugFlags(self, conf):
131 flags = super(GccBasicFlags, self).getDebugFlags(conf)
132 flags['CXXFLAGS'] += ['-O0',
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -0400133 '-Og', # gcc >= 4.8, clang >= 4.0
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800134 '-g3',
135 '-pedantic',
136 '-Wall',
137 '-Wextra',
138 '-Werror',
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -0400139 '-Wnon-virtual-dtor',
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800140 '-Wno-error=deprecated-declarations', # Bug #3795
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -0400141 '-Wno-error=maybe-uninitialized', # Bug #1615
142 '-Wno-unused-parameter',
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800143 ]
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -0400144 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800145 return flags
146
147 def getOptimizedFlags(self, conf):
148 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
149 flags['CXXFLAGS'] += ['-O2',
150 '-g',
151 '-pedantic',
152 '-Wall',
153 '-Wextra',
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -0400154 '-Wnon-virtual-dtor',
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800155 '-Wno-unused-parameter',
156 ]
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -0400157 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800158 return flags
159
160class GccFlags(GccBasicFlags):
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800161 def getDebugFlags(self, conf):
162 flags = super(GccFlags, self).getDebugFlags(conf)
163 version = tuple(int(i) for i in conf.env['CC_VERSION'])
164 if version < (5, 1, 0):
165 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -0400166 flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800167 return flags
168
169 def getOptimizedFlags(self, conf):
170 flags = super(GccFlags, self).getOptimizedFlags(conf)
171 version = tuple(int(i) for i in conf.env['CC_VERSION'])
172 if version < (5, 1, 0):
173 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
174 flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
175 return flags
176
177class ClangFlags(GccBasicFlags):
178 def getGeneralFlags(self, conf):
179 flags = super(ClangFlags, self).getGeneralFlags(conf)
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -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 Afanasyevf3192eb2016-12-19 17:11:20 -0800184 return flags
185
186 def getDebugFlags(self, conf):
187 flags = super(ClangFlags, self).getDebugFlags(conf)
188 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -0400189 '-Wextra-semi',
190 '-Wundefined-func-template',
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800191 '-Wno-error=deprecated-register',
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800192 '-Wno-error=infinite-recursion', # Bug #3358
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -0400193 '-Wno-error=keyword-macro', # Bug #3235
194 '-Wno-error=unneeded-internal-declaration', # Bug #1588
195 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800196 ]
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -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']
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800200 return flags
201
202 def getOptimizedFlags(self, conf):
203 flags = super(ClangFlags, self).getOptimizedFlags(conf)
204 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -0400205 '-Wextra-semi',
206 '-Wundefined-func-template',
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800207 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
208 ]
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -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 Afanasyevf3192eb2016-12-19 17:11:20 -0800212 return flags