blob: bba2b1e6942975f65095adeecbfb061f1b88e587 [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.')
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040020 conf.flags = GccFlags()
Junxiao Shif7191242015-03-19 05:53:41 -070021 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.')
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040025 conf.flags = ClangFlags()
Junxiao Shif7191242015-03-19 05:53:41 -070026 else:
Davide Pesaventoc0702702017-08-24 22:04:00 -040027 warnmsg = 'Note: %s compiler is unsupported' % cxx
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040028 conf.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
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040039 conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
Junxiao Shif7191242015-03-19 05:53:41 -070040
Junxiao Shic8a0a252015-10-05 07:25:02 -070041 # General flags are always applied (e.g., selecting C++11 mode)
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040042 generalFlags = conf.flags.getGeneralFlags(conf)
Junxiao Shif7191242015-03-19 05:53:41 -070043 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
44 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
45 conf.env.DEFINES += generalFlags['DEFINES']
46
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040047@Configure.conf
48def check_compiler_flags(conf):
Junxiao Shic8a0a252015-10-05 07:25:02 -070049 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Junxiao Shif7191242015-03-19 05:53:41 -070050 # corresponding environment variables are not set.
Junxiao Shic8a0a252015-10-05 07:25:02 -070051 # DEFINES are always applied.
Junxiao Shif7191242015-03-19 05:53:41 -070052 if conf.options.debug:
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040053 extraFlags = conf.flags.getDebugFlags(conf)
54 if conf.areCustomCxxflagsPresent:
Junxiao Shif7191242015-03-19 05:53:41 -070055 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
56 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))
60 else:
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040061 extraFlags = conf.flags.getOptimizedFlags(conf)
Junxiao Shif7191242015-03-19 05:53:41 -070062
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040063 if not conf.areCustomCxxflagsPresent:
Junxiao Shif7191242015-03-19 05:53:41 -070064 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Davide Pesavento3e79c9c2015-11-01 20:38:28 +010065 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Junxiao Shif7191242015-03-19 05:53:41 -070066
67 conf.env.DEFINES += extraFlags['DEFINES']
68
69@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 """
74 if len(cxxflags) == 0:
75 return
76
77 self.start_msg('Checking supported CXXFLAGS')
78
79 supportedFlags = []
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040080 for flags in cxxflags:
81 flags = Utils.to_list(flags)
82 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
83 supportedFlags += flags
Junxiao Shif7191242015-03-19 05:53:41 -070084
85 self.end_msg(' '.join(supportedFlags))
Davide Pesavento89d91752016-08-14 11:34:09 +020086 self.env.prepend_value('CXXFLAGS', supportedFlags)
Junxiao Shif7191242015-03-19 05:53:41 -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 """
93 if len(linkflags) == 0:
94 return
95
96 self.start_msg('Checking supported LINKFLAGS')
97
98 supportedFlags = []
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040099 for flags in linkflags:
100 flags = Utils.to_list(flags)
101 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
102 supportedFlags += flags
Junxiao Shif7191242015-03-19 05:53:41 -0700103
104 self.end_msg(' '.join(supportedFlags))
Davide Pesavento89d91752016-08-14 11:34:09 +0200105 self.env.prepend_value('LINKFLAGS', supportedFlags)
Junxiao Shif7191242015-03-19 05:53:41 -0700106
107
108class CompilerFlags(object):
109 def getGeneralFlags(self, conf):
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100110 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Junxiao Shif7191242015-03-19 05:53:41 -0700111 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
112
113 def getDebugFlags(self, conf):
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100114 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Junxiao Shif7191242015-03-19 05:53:41 -0700115 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
116
117 def getOptimizedFlags(self, conf):
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100118 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Junxiao Shif7191242015-03-19 05:53:41 -0700119 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
120
121class GccBasicFlags(CompilerFlags):
Junxiao Shic8a0a252015-10-05 07:25:02 -0700122 """
123 This class defines basic flags that work for both gcc and clang compilers
124 """
Davide Pesaventodd0e1952017-06-24 13:37:13 -0400125 def getGeneralFlags(self, conf):
126 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
127 flags['CXXFLAGS'] += ['-std=c++11']
128 return flags
129
Junxiao Shif7191242015-03-19 05:53:41 -0700130 def getDebugFlags(self, conf):
131 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100132 flags['CXXFLAGS'] += ['-O0',
Davide Pesaventodd0e1952017-06-24 13:37:13 -0400133 '-Og', # gcc >= 4.8, clang >= 4.0
Junxiao Shif7191242015-03-19 05:53:41 -0700134 '-g3',
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100135 '-pedantic',
136 '-Wall',
137 '-Wextra',
Junxiao Shif7191242015-03-19 05:53:41 -0700138 '-Werror',
Davide Pesavento887336c2017-09-19 15:25:23 -0400139 '-Wnon-virtual-dtor',
Alexander Afanasyevfc4aa0d2016-10-15 00:57:33 +0000140 '-Wno-error=deprecated-declarations', # Bug #3795
Davide Pesaventoc0702702017-08-24 22:04:00 -0400141 '-Wno-error=maybe-uninitialized', # Bug #1615
142 '-Wno-unused-parameter',
Junxiao Shic8a0a252015-10-05 07:25:02 -0700143 ]
Davide Pesaventodd0e1952017-06-24 13:37:13 -0400144 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Junxiao Shif7191242015-03-19 05:53:41 -0700145 return flags
146
147 def getOptimizedFlags(self, conf):
148 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100149 flags['CXXFLAGS'] += ['-O2',
Junxiao Shic8a0a252015-10-05 07:25:02 -0700150 '-g',
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100151 '-pedantic',
152 '-Wall',
153 '-Wextra',
Davide Pesavento887336c2017-09-19 15:25:23 -0400154 '-Wnon-virtual-dtor',
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100155 '-Wno-unused-parameter',
Junxiao Shic8a0a252015-10-05 07:25:02 -0700156 ]
Davide Pesaventodd0e1952017-06-24 13:37:13 -0400157 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Junxiao Shif7191242015-03-19 05:53:41 -0700158 return flags
159
160class GccFlags(GccBasicFlags):
Junxiao Shif7191242015-03-19 05:53:41 -0700161 def getDebugFlags(self, conf):
162 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesavento3e79c9c2015-11-01 20:38:28 +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 Pesaventodd0e1952017-06-24 13:37:13 -0400166 flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
Junxiao Shic8a0a252015-10-05 07:25:02 -0700167 return flags
168
169 def getOptimizedFlags(self, conf):
170 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesavento3e79c9c2015-11-01 20:38:28 +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']
Junxiao Shic8a0a252015-10-05 07:25:02 -0700174 flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
Junxiao Shif7191242015-03-19 05:53:41 -0700175 return flags
176
177class ClangFlags(GccBasicFlags):
178 def getGeneralFlags(self, conf):
179 flags = super(ClangFlags, self).getGeneralFlags(conf)
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -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
Junxiao Shif7191242015-03-19 05:53:41 -0700184 return flags
185
186 def getDebugFlags(self, conf):
187 flags = super(ClangFlags, self).getDebugFlags(conf)
Davide Pesaventoc0702702017-08-24 22:04:00 -0400188 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
189 '-Wextra-semi',
190 '-Wundefined-func-template',
191 '-Wno-error=deprecated-register',
192 '-Wno-error=infinite-recursion', # Bug #3358
193 '-Wno-error=keyword-macro', # Bug #3235
194 '-Wno-error=unneeded-internal-declaration', # Bug #1588
195 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
196 ]
Vince Lehman277ecf02016-02-10 16:37:48 -0600197 version = tuple(int(i) for i in conf.env['CC_VERSION'])
Davide Pesaventoc0702702017-08-24 22:04:00 -0400198 if version < (3, 9, 0) or (Utils.unversioned_sys_platform() == 'darwin' and version < (8, 1, 0)):
199 flags['CXXFLAGS'] += ['-Wno-unknown-pragmas']
Junxiao Shic8a0a252015-10-05 07:25:02 -0700200 return flags
201
202 def getOptimizedFlags(self, conf):
203 flags = super(ClangFlags, self).getOptimizedFlags(conf)
204 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesaventoc0702702017-08-24 22:04:00 -0400205 '-Wextra-semi',
206 '-Wundefined-func-template',
Junxiao Shic8a0a252015-10-05 07:25:02 -0700207 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
208 ]
Davide Pesaventoc0702702017-08-24 22:04:00 -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']
Junxiao Shif7191242015-03-19 05:53:41 -0700212 return flags