blob: 562d1927f092b4c49ffff915b41efecf26da06e0 [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))
63 self.env.CXXFLAGS = supportedFlags + self.env.CXXFLAGS
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))
81 self.env.LINKFLAGS = supportedFlags + self.env.LINKFLAGS
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080082
83
84class CompilerFlags(object):
85 def getGeneralFlags(self, conf):
86 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
87 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
88
89 def getDebugFlags(self, conf):
90 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
91 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
92
93 def getOptimizedFlags(self, conf):
94 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
95 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
96
97class GccBasicFlags(CompilerFlags):
98 """
99 This class defines basic flags that work for both gcc and clang compilers
100 """
101 def getDebugFlags(self, conf):
102 flags = super(GccBasicFlags, self).getDebugFlags(conf)
103 flags['CXXFLAGS'] += ['-O0',
104 '-g3',
105 '-pedantic',
106 '-Wall',
107 '-Wextra',
108 '-Werror',
109 '-Wno-unused-parameter',
110 '-Wno-error=maybe-uninitialized', # Bug #1615
Alexander Afanasyev96bf2f02016-10-11 16:37:45 -0700111 '-Wno-error=deprecated-declarations', # Bug #3795
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800112 ]
113 return flags
114
115 def getOptimizedFlags(self, conf):
116 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
117 flags['CXXFLAGS'] += ['-O2',
118 '-g',
119 '-pedantic',
120 '-Wall',
121 '-Wextra',
122 '-Wno-unused-parameter',
123 ]
124 return flags
125
126class GccFlags(GccBasicFlags):
127 def getGeneralFlags(self, conf):
128 flags = super(GccFlags, self).getGeneralFlags(conf)
129 version = tuple(int(i) for i in conf.env['CC_VERSION'])
130 if version < (4, 6, 0):
131 conf.fatal('The version of gcc you are using (%s) is too old.\n' %
132 '.'.join(conf.env['CC_VERSION']) +
133 'The minimum supported gcc version is 4.6.0.')
134 elif version < (4, 7, 0):
135 flags['CXXFLAGS'] += ['-std=c++0x']
136 else:
137 flags['CXXFLAGS'] += ['-std=c++11']
138 if version < (4, 8, 0):
139 flags['DEFINES'] += ['_GLIBCXX_USE_NANOSLEEP'] # Bug #2499
140 return flags
141
142 def getDebugFlags(self, conf):
143 flags = super(GccFlags, self).getDebugFlags(conf)
144 version = tuple(int(i) for i in conf.env['CC_VERSION'])
145 if version < (5, 1, 0):
146 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
147 flags['CXXFLAGS'] += ['-Og', # gcc >= 4.8
148 '-fdiagnostics-color', # gcc >= 4.9
149 ]
150 return flags
151
152 def getOptimizedFlags(self, conf):
153 flags = super(GccFlags, self).getOptimizedFlags(conf)
154 version = tuple(int(i) for i in conf.env['CC_VERSION'])
155 if version < (5, 1, 0):
156 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
157 flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
158 return flags
159
160class ClangFlags(GccBasicFlags):
161 def getGeneralFlags(self, conf):
162 flags = super(ClangFlags, self).getGeneralFlags(conf)
163 flags['CXXFLAGS'] += ['-std=c++11']
164 if Utils.unversioned_sys_platform() == 'darwin':
165 flags['CXXFLAGS'] += ['-stdlib=libc++']
166 flags['LINKFLAGS'] += ['-stdlib=libc++']
167 return flags
168
169 def getDebugFlags(self, conf):
170 flags = super(ClangFlags, self).getDebugFlags(conf)
171 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
172 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
173 '-Wno-error=unneeded-internal-declaration', # Bug #1588
174 '-Wno-error=deprecated-register',
175 '-Wno-error=keyword-macro', # Bug #3235
176 '-Wno-nested-anon-types',
177 '-Wno-sign-compare',
178 '-Wno-keyword-macro',
179 '-Wno-error=ignored-qualifiers',
180 '-Wno-error=infinite-recursion',
181 ]
182 return flags
183
184 def getOptimizedFlags(self, conf):
185 flags = super(ClangFlags, self).getOptimizedFlags(conf)
186 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
187 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
188 '-Wno-nested-anon-types',
189 '-Wno-sign-compare',
190 '-Wno-keyword-macro',
191 ]
192 return flags