blob: 38468e86913cf28777ea67c2b82b975b67edca80 [file] [log] [blame]
Alexander Afanasyeve3342152016-03-20 11:23:57 -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',
7 help='''Compile in debugging mode without optimizations (-O0 or -Og)''')
8
9def configure(conf):
10 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)
18
19 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.
30 if conf.options.debug:
31 extraFlags = flags.getDebugFlags(conf)
32 if areCustomCxxflagsPresent:
33 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
34 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))
38 else:
39 extraFlags = flags.getOptimizedFlags(conf)
40
41 if not areCustomCxxflagsPresent:
42 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
43 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
44
45 conf.env.DEFINES += extraFlags['DEFINES']
46
47@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 """
52 if len(cxxflags) == 0:
53 return
54
55 self.start_msg('Checking supported CXXFLAGS')
56
57 supportedFlags = []
58 for flag in cxxflags:
59 if self.check_cxx(cxxflags=['-Werror', flag], mandatory=False):
60 supportedFlags += [flag]
61
62 self.end_msg(' '.join(supportedFlags))
63 self.env.CXXFLAGS = supportedFlags + self.env.CXXFLAGS
64
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 """
70 if len(linkflags) == 0:
71 return
72
73 self.start_msg('Checking supported LINKFLAGS')
74
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
82
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
susmit90b71822016-03-20 13:31:23 -0600111 '-fPIC',
Alexander Afanasyeve3342152016-03-20 11:23:57 -0700112 ]
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',
susmit90b71822016-03-20 13:31:23 -0600123 '-fPIC',
Alexander Afanasyeve3342152016-03-20 11:23:57 -0700124 ]
125 return flags
126
127class GccFlags(GccBasicFlags):
128 def getGeneralFlags(self, conf):
129 flags = super(GccFlags, self).getGeneralFlags(conf)
130 version = tuple(int(i) for i in conf.env['CC_VERSION'])
131 if version < (4, 6, 0):
132 conf.fatal('The version of gcc you are using (%s) is too old.\n' %
133 '.'.join(conf.env['CC_VERSION']) +
134 'The minimum supported gcc version is 4.6.0.')
135 elif version < (4, 7, 0):
136 flags['CXXFLAGS'] += ['-std=c++0x']
137 else:
138 flags['CXXFLAGS'] += ['-std=c++11']
139 if version < (4, 8, 0):
140 flags['DEFINES'] += ['_GLIBCXX_USE_NANOSLEEP'] # Bug #2499
141 return flags
142
143 def getDebugFlags(self, conf):
144 flags = super(GccFlags, self).getDebugFlags(conf)
145 version = tuple(int(i) for i in conf.env['CC_VERSION'])
146 if version < (5, 1, 0):
147 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
148 flags['CXXFLAGS'] += ['-Og', # gcc >= 4.8
149 '-fdiagnostics-color', # gcc >= 4.9
150 ]
151 return flags
152
153 def getOptimizedFlags(self, conf):
154 flags = super(GccFlags, self).getOptimizedFlags(conf)
155 version = tuple(int(i) for i in conf.env['CC_VERSION'])
156 if version < (5, 1, 0):
157 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
158 flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
159 return flags
160
161class ClangFlags(GccBasicFlags):
162 def getGeneralFlags(self, conf):
163 flags = super(ClangFlags, self).getGeneralFlags(conf)
164 flags['CXXFLAGS'] += ['-std=c++11']
165 if Utils.unversioned_sys_platform() == 'darwin':
166 flags['CXXFLAGS'] += ['-stdlib=libc++']
167 flags['LINKFLAGS'] += ['-stdlib=libc++']
168 return flags
169
170 def getDebugFlags(self, conf):
171 flags = super(ClangFlags, self).getDebugFlags(conf)
172 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
173 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
174 '-Wno-error=unneeded-internal-declaration', # Bug #1588
175 '-Wno-error=deprecated-register',
176 '-Wno-error=keyword-macro', # Bug #3235
177 ]
178 return flags
179
180 def getOptimizedFlags(self, conf):
181 flags = super(ClangFlags, self).getOptimizedFlags(conf)
182 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
183 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
184 ]
185 return flags