blob: f1d23d6bc00bb2f02ced77f418d1532d4ab4e4de [file] [log] [blame]
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -07001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
2
Alexander Afanasyevd352cce2015-11-20 14:15:11 -05003from waflib import Logs, Configure, Utils
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -07004
Shuo Chen478204c2014-03-18 18:27:04 -07005def options(opt):
Shuo Chen478204c2014-03-18 18:27:04 -07006 opt.add_option('--debug', '--with-debug', action='store_true', default=False, dest='debug',
Junxiao Shie1801312017-07-22 19:16:49 +00007 help='''Compile in debugging mode with minimal optimizations (-O0 or -Og)''')
Shuo Chen478204c2014-03-18 18:27:04 -07008
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -07009def configure(conf):
Davide Pesaventob545aac2017-09-22 23:54:10 -040010 conf.start_msg('Checking C++ compiler version')
11
12 cxx = conf.env['CXX_NAME'] # CXX_NAME is the generic name of the compiler
Junxiao Shie1801312017-07-22 19:16:49 +000013 ccver = tuple(int(i) for i in conf.env['CC_VERSION'])
Davide Pesaventob545aac2017-09-22 23:54:10 -040014 errmsg = ''
15 warnmsg = ''
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050016 if cxx == 'gcc':
Junxiao Shie1801312017-07-22 19:16:49 +000017 if ccver < (4, 8, 2):
Davide Pesaventob545aac2017-09-22 23:54:10 -040018 errmsg = ('The version of gcc you are using is too old.\n'
19 'The minimum supported gcc version is 4.8.2.')
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050020 flags = GccFlags()
21 elif cxx == 'clang':
Junxiao Shie1801312017-07-22 19:16:49 +000022 if ccver < (3, 4, 0):
Davide Pesaventob545aac2017-09-22 23:54:10 -040023 errmsg = ('The version of clang you are using is too old.\n'
24 'The minimum supported clang version is 3.4.0.')
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050025 flags = ClangFlags()
26 else:
Davide Pesaventob545aac2017-09-22 23:54:10 -040027 warnmsg = 'Note: %s compiler is unsupported' % cxx
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050028 flags = CompilerFlags()
Davide Pesaventob545aac2017-09-22 23:54:10 -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']))
Shuo Chen478204c2014-03-18 18:27:04 -070038
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050039 areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
40
41 # General flags are always applied (e.g., selecting C++11 mode)
42 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
47 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
48 # corresponding environment variables are not set.
49 # DEFINES are always applied.
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070050 if conf.options.debug:
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050051 extraFlags = flags.getDebugFlags(conf)
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070052 if areCustomCxxflagsPresent:
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050053 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070054 if len(missingFlags) > 0:
55 Logs.warn("Selected debug mode, but CXXFLAGS is set to a custom value '%s'"
Shuo Chen478204c2014-03-18 18:27:04 -070056 % " ".join(conf.env.CXXFLAGS))
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070057 Logs.warn("Default flags '%s' are not activated" % " ".join(missingFlags))
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070058 else:
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050059 extraFlags = flags.getOptimizedFlags(conf)
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070060
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050061 if not areCustomCxxflagsPresent:
62 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
63 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
64
65 conf.env.DEFINES += extraFlags['DEFINES']
Wentao Shanga8f3c402014-10-30 14:03:27 -070066
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -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 Afanasyevd352cce2015-11-20 14:15:11 -050072 if len(cxxflags) == 0:
73 return
74
Wentao Shanga8f3c402014-10-30 14:03:27 -070075 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070076
77 supportedFlags = []
78 for flag in cxxflags:
Wentao Shanga8f3c402014-10-30 14:03:27 -070079 if self.check_cxx(cxxflags=['-Werror', flag], mandatory=False):
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070080 supportedFlags += [flag]
81
Shuo Chen478204c2014-03-18 18:27:04 -070082 self.end_msg(' '.join(supportedFlags))
Junxiao Shie1801312017-07-22 19:16:49 +000083 self.env.prepend_value('CXXFLAGS', supportedFlags)
Wentao Shanga8f3c402014-10-30 14:03:27 -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 """
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050090 if len(linkflags) == 0:
91 return
92
Wentao Shanga8f3c402014-10-30 14:03:27 -070093 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))
Junxiao Shie1801312017-07-22 19:16:49 +0000101 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500102
103
104class CompilerFlags(object):
105 def getGeneralFlags(self, conf):
106 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
107 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
108
109 def getDebugFlags(self, conf):
110 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
111 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
112
113 def getOptimizedFlags(self, conf):
114 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
115 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
116
117class GccBasicFlags(CompilerFlags):
118 """
119 This class defines basic flags that work for both gcc and clang compilers
120 """
Junxiao Shie1801312017-07-22 19:16:49 +0000121 def getGeneralFlags(self, conf):
122 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
123 flags['CXXFLAGS'] += ['-std=c++11']
124 return flags
125
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500126 def getDebugFlags(self, conf):
127 flags = super(GccBasicFlags, self).getDebugFlags(conf)
128 flags['CXXFLAGS'] += ['-O0',
Junxiao Shie1801312017-07-22 19:16:49 +0000129 '-Og', # gcc >= 4.8, clang >= 4.0
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500130 '-g3',
131 '-pedantic',
132 '-Wall',
133 '-Wextra',
134 '-Werror',
Junxiao Shie1801312017-07-22 19:16:49 +0000135 '-Wnon-virtual-dtor',
Junxiao Shie1801312017-07-22 19:16:49 +0000136 '-Wno-error=deprecated-declarations', # Bug #3795
Davide Pesaventob545aac2017-09-22 23:54:10 -0400137 '-Wno-error=maybe-uninitialized', # Bug #1615
138 '-Wno-unused-parameter',
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500139 ]
Junxiao Shie1801312017-07-22 19:16:49 +0000140 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500141 return flags
142
143 def getOptimizedFlags(self, conf):
144 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
145 flags['CXXFLAGS'] += ['-O2',
146 '-g',
147 '-pedantic',
148 '-Wall',
149 '-Wextra',
Junxiao Shie1801312017-07-22 19:16:49 +0000150 '-Wnon-virtual-dtor',
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500151 '-Wno-unused-parameter',
152 ]
Junxiao Shie1801312017-07-22 19:16:49 +0000153 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500154 return flags
155
156class GccFlags(GccBasicFlags):
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500157 def getDebugFlags(self, conf):
158 flags = super(GccFlags, self).getDebugFlags(conf)
159 version = tuple(int(i) for i in conf.env['CC_VERSION'])
160 if version < (5, 1, 0):
161 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
Junxiao Shie1801312017-07-22 19:16:49 +0000162 flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500163 return flags
164
165 def getOptimizedFlags(self, conf):
166 flags = super(GccFlags, self).getOptimizedFlags(conf)
167 version = tuple(int(i) for i in conf.env['CC_VERSION'])
168 if version < (5, 1, 0):
169 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
170 flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
171 return flags
172
173class ClangFlags(GccBasicFlags):
174 def getGeneralFlags(self, conf):
175 flags = super(ClangFlags, self).getGeneralFlags(conf)
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500176 if Utils.unversioned_sys_platform() == 'darwin':
177 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)
183 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500184 '-Wno-error=deprecated-register',
Junxiao Shie1801312017-07-22 19:16:49 +0000185 '-Wno-error=infinite-recursion', # Bug #3358
Davide Pesaventob545aac2017-09-22 23:54:10 -0400186 '-Wno-error=keyword-macro', # Bug #3235
187 '-Wno-error=unneeded-internal-declaration', # Bug #1588
188 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500189 ]
190 return flags
191
192 def getOptimizedFlags(self, conf):
193 flags = super(ClangFlags, self).getOptimizedFlags(conf)
194 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
195 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
196 ]
197 return flags