blob: 7544cec6acc658492ef9916631c2ec7461da929e [file] [log] [blame]
Yingdi Yu40cd1c32014-04-17 15:02:17 -07001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
Yingdi Yu40cd1c32014-04-17 15:02:17 -07002
Alexander Afanasyevf9f39102015-12-01 17:43:40 -08003from waflib import Logs, Configure, Utils
Yingdi Yu40cd1c32014-04-17 15:02:17 -07004
5def options(opt):
Yingdi Yu40cd1c32014-04-17 15:02:17 -07006 opt.add_option('--debug', '--with-debug', action='store_true', default=False, dest='debug',
Vince Lehman0a7da612014-10-29 14:39:29 -05007 help='''Compile in debugging mode without optimizations (-O0 or -Og)''')
Yingdi Yu40cd1c32014-04-17 15:02:17 -07008
9def configure(conf):
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080010 cxx = conf.env['CXX_NAME'] # CXX_NAME represents generic name of the compiler
11 if cxx == 'gcc':
12 flags = GccFlags()
13 elif cxx == 'clang':
14 flags = ClangFlags()
15 else:
16 flags = CompilerFlags()
17 Logs.warn('The code has not yet been tested with %s compiler' % cxx)
Yingdi Yu40cd1c32014-04-17 15:02:17 -070018
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080019 areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
20
21 # General flags are always applied (e.g., selecting C++11 mode)
22 generalFlags = flags.getGeneralFlags(conf)
23 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
24 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
25 conf.env.DEFINES += generalFlags['DEFINES']
26
27 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
28 # corresponding environment variables are not set.
29 # DEFINES are always applied.
Yingdi Yu40cd1c32014-04-17 15:02:17 -070030 if conf.options.debug:
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080031 extraFlags = flags.getDebugFlags(conf)
Yingdi Yu40cd1c32014-04-17 15:02:17 -070032 if areCustomCxxflagsPresent:
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080033 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Yingdi Yu40cd1c32014-04-17 15:02:17 -070034 if len(missingFlags) > 0:
35 Logs.warn("Selected debug mode, but CXXFLAGS is set to a custom value '%s'"
36 % " ".join(conf.env.CXXFLAGS))
37 Logs.warn("Default flags '%s' are not activated" % " ".join(missingFlags))
Yingdi Yu40cd1c32014-04-17 15:02:17 -070038 else:
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080039 extraFlags = flags.getOptimizedFlags(conf)
Yingdi Yu40cd1c32014-04-17 15:02:17 -070040
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080041 if not areCustomCxxflagsPresent:
42 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
43 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
44
45 conf.env.DEFINES += extraFlags['DEFINES']
Vince Lehman0a7da612014-10-29 14:39:29 -050046
Yingdi Yu40cd1c32014-04-17 15:02:17 -070047@Configure.conf
48def add_supported_cxxflags(self, cxxflags):
49 """
50 Check which cxxflags are supported by compiler and add them to env.CXXFLAGS variable
51 """
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080052 if len(cxxflags) == 0:
53 return
54
55 self.start_msg('Checking supported CXXFLAGS')
Yingdi Yu40cd1c32014-04-17 15:02:17 -070056
57 supportedFlags = []
58 for flag in cxxflags:
Vince Lehman0a7da612014-10-29 14:39:29 -050059 if self.check_cxx(cxxflags=['-Werror', flag], mandatory=False):
Yingdi Yu40cd1c32014-04-17 15:02:17 -070060 supportedFlags += [flag]
61
62 self.end_msg(' '.join(supportedFlags))
Davide Pesaventoc58cc7f2017-08-08 16:51:28 -050063 self.env.prepend_value('CXXFLAGS', supportedFlags)
Vince Lehman0a7da612014-10-29 14:39:29 -050064
65@Configure.conf
66def add_supported_linkflags(self, linkflags):
67 """
68 Check which linkflags are supported by compiler and add them to env.LINKFLAGS variable
69 """
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080070 if len(linkflags) == 0:
71 return
72
73 self.start_msg('Checking supported LINKFLAGS')
Vince Lehman0a7da612014-10-29 14:39:29 -050074
75 supportedFlags = []
76 for flag in linkflags:
77 if self.check_cxx(linkflags=['-Werror', flag], mandatory=False):
78 supportedFlags += [flag]
79
80 self.end_msg(' '.join(supportedFlags))
Davide Pesaventoc58cc7f2017-08-08 16:51:28 -050081 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080082
83class CompilerFlags(object):
84 def getGeneralFlags(self, conf):
85 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
86 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
87
88 def getDebugFlags(self, conf):
89 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
90 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
91
92 def getOptimizedFlags(self, conf):
93 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
94 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
95
96class GccBasicFlags(CompilerFlags):
97 """
98 This class defines basic flags that work for both gcc and clang compilers
99 """
100 def getDebugFlags(self, conf):
101 flags = super(GccBasicFlags, self).getDebugFlags(conf)
102 flags['CXXFLAGS'] += ['-O0',
103 '-g3',
104 '-pedantic',
105 '-Wall',
106 '-Wextra',
107 '-Werror',
108 '-Wno-unused-parameter',
109 '-Wno-error=maybe-uninitialized', # Bug #1615
Alexander Afanasyev96bf2f02016-10-11 16:37:45 -0700110 '-Wno-error=deprecated-declarations', # Bug #3795
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800111 ]
112 return flags
113
114 def getOptimizedFlags(self, conf):
115 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
116 flags['CXXFLAGS'] += ['-O2',
117 '-g',
118 '-pedantic',
119 '-Wall',
120 '-Wextra',
121 '-Wno-unused-parameter',
122 ]
123 return flags
124
125class GccFlags(GccBasicFlags):
126 def getGeneralFlags(self, conf):
127 flags = super(GccFlags, self).getGeneralFlags(conf)
128 version = tuple(int(i) for i in conf.env['CC_VERSION'])
129 if version < (4, 6, 0):
130 conf.fatal('The version of gcc you are using (%s) is too old.\n' %
131 '.'.join(conf.env['CC_VERSION']) +
132 'The minimum supported gcc version is 4.6.0.')
133 elif version < (4, 7, 0):
134 flags['CXXFLAGS'] += ['-std=c++0x']
135 else:
136 flags['CXXFLAGS'] += ['-std=c++11']
137 if version < (4, 8, 0):
138 flags['DEFINES'] += ['_GLIBCXX_USE_NANOSLEEP'] # Bug #2499
139 return flags
140
141 def getDebugFlags(self, conf):
142 flags = super(GccFlags, self).getDebugFlags(conf)
143 version = tuple(int(i) for i in conf.env['CC_VERSION'])
144 if version < (5, 1, 0):
145 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
146 flags['CXXFLAGS'] += ['-Og', # gcc >= 4.8
147 '-fdiagnostics-color', # gcc >= 4.9
148 ]
149 return flags
150
151 def getOptimizedFlags(self, conf):
152 flags = super(GccFlags, self).getOptimizedFlags(conf)
153 version = tuple(int(i) for i in conf.env['CC_VERSION'])
154 if version < (5, 1, 0):
155 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
156 flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
157 return flags
158
159class ClangFlags(GccBasicFlags):
160 def getGeneralFlags(self, conf):
161 flags = super(ClangFlags, self).getGeneralFlags(conf)
162 flags['CXXFLAGS'] += ['-std=c++11']
163 if Utils.unversioned_sys_platform() == 'darwin':
164 flags['CXXFLAGS'] += ['-stdlib=libc++']
165 flags['LINKFLAGS'] += ['-stdlib=libc++']
166 return flags
167
168 def getDebugFlags(self, conf):
169 flags = super(ClangFlags, self).getDebugFlags(conf)
170 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
171 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
172 '-Wno-error=unneeded-internal-declaration', # Bug #1588
173 '-Wno-error=deprecated-register',
174 '-Wno-error=keyword-macro', # Bug #3235
175 '-Wno-nested-anon-types',
176 '-Wno-sign-compare',
177 '-Wno-keyword-macro',
178 '-Wno-error=ignored-qualifiers',
179 '-Wno-error=infinite-recursion',
180 ]
181 return flags
182
183 def getOptimizedFlags(self, conf):
184 flags = super(ClangFlags, self).getOptimizedFlags(conf)
185 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
186 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
187 '-Wno-nested-anon-types',
188 '-Wno-sign-compare',
189 '-Wno-keyword-macro',
190 ]
191 return flags