blob: 850b09fff12372690bcffb3553a82e8cf39acf56 [file] [log] [blame]
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -07001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -07002
Alexander Afanasyev01515792014-12-16 14:20:01 -08003from waflib import Logs, Configure, Utils
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -07004
5def options(opt):
6 opt.add_option('--debug', '--with-debug', action='store_true', default=False, dest='debug',
Davide Pesavento37271c52017-06-23 00:58:50 -04007 help='''Compile in debugging mode with minimal optimizations (-O0 or -Og)''')
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -07008
9def configure(conf):
Alexander Afanasyev01515792014-12-16 14:20:01 -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()
Davide Pesaventod06ea052015-10-02 01:48:24 +020017 Logs.warn('The code has not yet been tested with %s compiler' % cxx)
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -070018
Alexander Afanasyev01515792014-12-16 14:20:01 -080019 areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
20
Davide Pesaventod06ea052015-10-02 01:48:24 +020021 # General flags are always applied (e.g., selecting C++11 mode)
Alexander Afanasyev01515792014-12-16 14:20:01 -080022 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
Davide Pesaventod06ea052015-10-02 01:48:24 +020027 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Alexander Afanasyev01515792014-12-16 14:20:01 -080028 # corresponding environment variables are not set.
Davide Pesaventod06ea052015-10-02 01:48:24 +020029 # DEFINES are always applied.
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070030 if conf.options.debug:
Alexander Afanasyev01515792014-12-16 14:20:01 -080031 extraFlags = flags.getDebugFlags(conf)
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070032 if areCustomCxxflagsPresent:
Alexander Afanasyev01515792014-12-16 14:20:01 -080033 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070034 if len(missingFlags) > 0:
35 Logs.warn("Selected debug mode, but CXXFLAGS is set to a custom value '%s'"
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -070036 % " ".join(conf.env.CXXFLAGS))
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070037 Logs.warn("Default flags '%s' are not activated" % " ".join(missingFlags))
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070038 else:
Alexander Afanasyev01515792014-12-16 14:20:01 -080039 extraFlags = flags.getOptimizedFlags(conf)
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070040
Alexander Afanasyev01515792014-12-16 14:20:01 -080041 if not areCustomCxxflagsPresent:
42 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Davide Pesavento7c02b472015-10-28 20:50:17 +010043 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Alexander Afanasyev01515792014-12-16 14:20:01 -080044
45 conf.env.DEFINES += extraFlags['DEFINES']
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020046
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -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 Afanasyev01515792014-12-16 14:20:01 -080052 if len(cxxflags) == 0:
53 return
54
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020055 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070056
57 supportedFlags = []
58 for flag in cxxflags:
Alexander Afanasyev3e6a98e2014-04-25 12:02:13 -070059 if self.check_cxx(cxxflags=['-Werror', flag], mandatory=False):
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070060 supportedFlags += [flag]
61
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -070062 self.end_msg(' '.join(supportedFlags))
Davide Pesavento1349d2d2016-08-09 06:43:57 +020063 self.env.prepend_value('CXXFLAGS', supportedFlags)
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020064
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 Afanasyev01515792014-12-16 14:20:01 -080070 if len(linkflags) == 0:
71 return
72
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020073 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 Pesavento1349d2d2016-08-09 06:43:57 +020081 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyev01515792014-12-16 14:20:01 -080082
83
84class CompilerFlags(object):
85 def getGeneralFlags(self, conf):
Davide Pesavento7c02b472015-10-28 20:50:17 +010086 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Alexander Afanasyev01515792014-12-16 14:20:01 -080087 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
88
89 def getDebugFlags(self, conf):
Davide Pesavento7c02b472015-10-28 20:50:17 +010090 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Alexander Afanasyev01515792014-12-16 14:20:01 -080091 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
92
93 def getOptimizedFlags(self, conf):
Davide Pesavento7c02b472015-10-28 20:50:17 +010094 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Alexander Afanasyev01515792014-12-16 14:20:01 -080095 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
96
97class GccBasicFlags(CompilerFlags):
Davide Pesaventod06ea052015-10-02 01:48:24 +020098 """
99 This class defines basic flags that work for both gcc and clang compilers
100 """
Alexander Afanasyev01515792014-12-16 14:20:01 -0800101 def getDebugFlags(self, conf):
102 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Davide Pesavento7c02b472015-10-28 20:50:17 +0100103 flags['CXXFLAGS'] += ['-O0',
Davide Pesaventof714c1f2017-04-12 23:53:40 -0400104 '-Og', # gcc >= 4.8, clang >= 4.0
Alexander Afanasyev01515792014-12-16 14:20:01 -0800105 '-g3',
Davide Pesavento7c02b472015-10-28 20:50:17 +0100106 '-pedantic',
107 '-Wall',
108 '-Wextra',
Alexander Afanasyev01515792014-12-16 14:20:01 -0800109 '-Werror',
Davide Pesavento7f20d6e2017-01-16 14:43:58 -0500110 '-Wnon-virtual-dtor',
Davide Pesavento7c02b472015-10-28 20:50:17 +0100111 '-Wno-unused-parameter',
Alexander Afanasyev01515792014-12-16 14:20:01 -0800112 '-Wno-error=maybe-uninitialized', # Bug #1615
Alexander Afanasyev465c9732016-10-11 16:35:32 -0700113 '-Wno-error=deprecated-declarations', # Bug #3795
Davide Pesavento1aafba42015-09-17 17:22:31 -0700114 ]
Davide Pesavento37271c52017-06-23 00:58:50 -0400115 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Alexander Afanasyev01515792014-12-16 14:20:01 -0800116 return flags
117
118 def getOptimizedFlags(self, conf):
119 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
Davide Pesavento7c02b472015-10-28 20:50:17 +0100120 flags['CXXFLAGS'] += ['-O2',
Davide Pesavento1aafba42015-09-17 17:22:31 -0700121 '-g',
Davide Pesavento7c02b472015-10-28 20:50:17 +0100122 '-pedantic',
123 '-Wall',
124 '-Wextra',
Davide Pesavento7f20d6e2017-01-16 14:43:58 -0500125 '-Wnon-virtual-dtor',
Davide Pesavento7c02b472015-10-28 20:50:17 +0100126 '-Wno-unused-parameter',
Davide Pesavento1aafba42015-09-17 17:22:31 -0700127 ]
Davide Pesavento37271c52017-06-23 00:58:50 -0400128 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Alexander Afanasyev01515792014-12-16 14:20:01 -0800129 return flags
130
131class GccFlags(GccBasicFlags):
132 def getGeneralFlags(self, conf):
133 flags = super(GccFlags, self).getGeneralFlags(conf)
134 version = tuple(int(i) for i in conf.env['CC_VERSION'])
Davide Pesaventoe6e6fde2016-04-16 14:44:45 +0200135 if version < (4, 8, 2):
Alexander Afanasyev01515792014-12-16 14:20:01 -0800136 conf.fatal('The version of gcc you are using (%s) is too old.\n' %
Stephen McQuistine3d62662015-01-15 15:08:34 +0000137 '.'.join(conf.env['CC_VERSION']) +
Davide Pesaventoe6e6fde2016-04-16 14:44:45 +0200138 'The minimum supported gcc version is 4.8.2.')
Alexander Afanasyev01515792014-12-16 14:20:01 -0800139 else:
140 flags['CXXFLAGS'] += ['-std=c++11']
141 return flags
142
143 def getDebugFlags(self, conf):
144 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesavento7c02b472015-10-28 20:50:17 +0100145 version = tuple(int(i) for i in conf.env['CC_VERSION'])
146 if version < (5, 1, 0):
147 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
Davide Pesaventof714c1f2017-04-12 23:53:40 -0400148 flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
Alexander Afanasyev01515792014-12-16 14:20:01 -0800149 return flags
150
Davide Pesaventod06ea052015-10-02 01:48:24 +0200151 def getOptimizedFlags(self, conf):
152 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesavento7c02b472015-10-28 20:50:17 +0100153 version = tuple(int(i) for i in conf.env['CC_VERSION'])
154 if version < (5, 1, 0):
155 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
Davide Pesaventod06ea052015-10-02 01:48:24 +0200156 flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
157 return flags
158
Alexander Afanasyev01515792014-12-16 14:20:01 -0800159class ClangFlags(GccBasicFlags):
160 def getGeneralFlags(self, conf):
161 flags = super(ClangFlags, self).getGeneralFlags(conf)
Davide Pesavento1aafba42015-09-17 17:22:31 -0700162 flags['CXXFLAGS'] += ['-std=c++11']
Davide Pesaventod06ea052015-10-02 01:48:24 +0200163 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev01515792014-12-16 14:20:01 -0800164 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)
Davide Pesavento1aafba42015-09-17 17:22:31 -0700170 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesaventod06ea052015-10-02 01:48:24 +0200171 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Davide Pesavento1aafba42015-09-17 17:22:31 -0700172 '-Wno-error=unneeded-internal-declaration', # Bug #1588
173 '-Wno-error=deprecated-register',
Junxiao Shib1ba3e02015-10-04 16:56:37 -0700174 '-Wno-error=keyword-macro', # Bug #3235
Alexander Afanasyevefa22d62015-12-04 13:39:17 -0800175 '-Wno-error=infinite-recursion', # Bug #3358
Davide Pesaventod06ea052015-10-02 01:48:24 +0200176 ]
177 return flags
178
179 def getOptimizedFlags(self, conf):
180 flags = super(ClangFlags, self).getOptimizedFlags(conf)
181 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
182 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Davide Pesavento1aafba42015-09-17 17:22:31 -0700183 ]
Alexander Afanasyev01515792014-12-16 14:20:01 -0800184 return flags