blob: 968e1d8d4f6cc0fb36232b3a5ee87497ed6418dc [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',
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()
Junxiao Shic8a0a252015-10-05 07:25:02 -070017 Logs.warn('The code has not yet been tested with %s compiler' % cxx)
Junxiao Shif7191242015-03-19 05:53:41 -070018
19 areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
20
Junxiao Shic8a0a252015-10-05 07:25:02 -070021 # General flags are always applied (e.g., selecting C++11 mode)
Junxiao Shif7191242015-03-19 05:53:41 -070022 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
Junxiao Shic8a0a252015-10-05 07:25:02 -070027 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Junxiao Shif7191242015-03-19 05:53:41 -070028 # corresponding environment variables are not set.
Junxiao Shic8a0a252015-10-05 07:25:02 -070029 # DEFINES are always applied.
Junxiao Shif7191242015-03-19 05:53:41 -070030 if conf.options.debug:
31 extraFlags = flags.getDebugFlags(conf)
Junxiao Shif7191242015-03-19 05:53:41 -070032 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'])
Davide Pesavento3e79c9c2015-11-01 20:38:28 +010043 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Junxiao Shif7191242015-03-19 05:53:41 -070044
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))
Davide Pesavento89d91752016-08-14 11:34:09 +020063 self.env.prepend_value('CXXFLAGS', supportedFlags)
Junxiao Shif7191242015-03-19 05:53:41 -070064
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))
Davide Pesavento89d91752016-08-14 11:34:09 +020081 self.env.prepend_value('LINKFLAGS', supportedFlags)
Junxiao Shif7191242015-03-19 05:53:41 -070082
83
84class CompilerFlags(object):
85 def getGeneralFlags(self, conf):
Davide Pesavento3e79c9c2015-11-01 20:38:28 +010086 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Junxiao Shif7191242015-03-19 05:53:41 -070087 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
88
89 def getDebugFlags(self, conf):
Davide Pesavento3e79c9c2015-11-01 20:38:28 +010090 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Junxiao Shif7191242015-03-19 05:53:41 -070091 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
92
93 def getOptimizedFlags(self, conf):
Davide Pesavento3e79c9c2015-11-01 20:38:28 +010094 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Junxiao Shif7191242015-03-19 05:53:41 -070095 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
96
97class GccBasicFlags(CompilerFlags):
Junxiao Shic8a0a252015-10-05 07:25:02 -070098 """
99 This class defines basic flags that work for both gcc and clang compilers
100 """
Junxiao Shif7191242015-03-19 05:53:41 -0700101 def getDebugFlags(self, conf):
102 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100103 flags['CXXFLAGS'] += ['-O0',
Junxiao Shif7191242015-03-19 05:53:41 -0700104 '-g3',
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100105 '-pedantic',
106 '-Wall',
107 '-Wextra',
Junxiao Shif7191242015-03-19 05:53:41 -0700108 '-Werror',
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100109 '-Wno-unused-parameter',
Junxiao Shif7191242015-03-19 05:53:41 -0700110 '-Wno-error=maybe-uninitialized', # Bug #1615
Alexander Afanasyevfc4aa0d2016-10-15 00:57:33 +0000111 '-Wno-error=deprecated-declarations', # Bug #3795
Junxiao Shic8a0a252015-10-05 07:25:02 -0700112 ]
Junxiao Shif7191242015-03-19 05:53:41 -0700113 return flags
114
115 def getOptimizedFlags(self, conf):
116 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100117 flags['CXXFLAGS'] += ['-O2',
Junxiao Shic8a0a252015-10-05 07:25:02 -0700118 '-g',
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100119 '-pedantic',
120 '-Wall',
121 '-Wextra',
122 '-Wno-unused-parameter',
Junxiao Shic8a0a252015-10-05 07:25:02 -0700123 ]
Junxiao Shif7191242015-03-19 05:53:41 -0700124 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'])
Davide Pesavento52401ce2016-05-03 18:16:59 +0200130 if version < (4, 8, 2):
Junxiao Shif7191242015-03-19 05:53:41 -0700131 conf.fatal('The version of gcc you are using (%s) is too old.\n' %
132 '.'.join(conf.env['CC_VERSION']) +
Davide Pesavento52401ce2016-05-03 18:16:59 +0200133 'The minimum supported gcc version is 4.8.2.')
Junxiao Shif7191242015-03-19 05:53:41 -0700134 else:
135 flags['CXXFLAGS'] += ['-std=c++11']
Junxiao Shif7191242015-03-19 05:53:41 -0700136 return flags
137
138 def getDebugFlags(self, conf):
139 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100140 version = tuple(int(i) for i in conf.env['CC_VERSION'])
141 if version < (5, 1, 0):
142 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
Junxiao Shif7191242015-03-19 05:53:41 -0700143 flags['CXXFLAGS'] += ['-Og', # gcc >= 4.8
144 '-fdiagnostics-color', # gcc >= 4.9
Junxiao Shic8a0a252015-10-05 07:25:02 -0700145 ]
146 return flags
147
148 def getOptimizedFlags(self, conf):
149 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100150 version = tuple(int(i) for i in conf.env['CC_VERSION'])
151 if version < (5, 1, 0):
152 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
Junxiao Shic8a0a252015-10-05 07:25:02 -0700153 flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
Junxiao Shif7191242015-03-19 05:53:41 -0700154 return flags
155
156class ClangFlags(GccBasicFlags):
157 def getGeneralFlags(self, conf):
158 flags = super(ClangFlags, self).getGeneralFlags(conf)
Junxiao Shic8a0a252015-10-05 07:25:02 -0700159 flags['CXXFLAGS'] += ['-std=c++11']
160 if Utils.unversioned_sys_platform() == 'darwin':
Junxiao Shif7191242015-03-19 05:53:41 -0700161 flags['CXXFLAGS'] += ['-stdlib=libc++']
162 flags['LINKFLAGS'] += ['-stdlib=libc++']
163 return flags
164
165 def getDebugFlags(self, conf):
166 flags = super(ClangFlags, self).getDebugFlags(conf)
Vince Lehman277ecf02016-02-10 16:37:48 -0600167 version = tuple(int(i) for i in conf.env['CC_VERSION'])
168 if Utils.unversioned_sys_platform() == 'darwin' and version < (7, 0, 0):
169 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
Junxiao Shic8a0a252015-10-05 07:25:02 -0700170 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 ]
176 return flags
177
178 def getOptimizedFlags(self, conf):
179 flags = super(ClangFlags, self).getOptimizedFlags(conf)
Vince Lehman277ecf02016-02-10 16:37:48 -0600180 version = tuple(int(i) for i in conf.env['CC_VERSION'])
181 if Utils.unversioned_sys_platform() == 'darwin' and version < (7, 0, 0):
182 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
Junxiao Shic8a0a252015-10-05 07:25:02 -0700183 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
184 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
185 ]
Junxiao Shif7191242015-03-19 05:53:41 -0700186 return flags