blob: ae2058836d86c0769a479b6e23089db03f658f7e [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
Davide Pesavento724d65a2017-06-23 17:14:08 -040011 ccver = tuple(int(i) for i in conf.env['CC_VERSION'])
Alexander Afanasyev01515792014-12-16 14:20:01 -080012 if cxx == 'gcc':
Davide Pesavento724d65a2017-06-23 17:14:08 -040013 if ccver < (4, 8, 2):
14 conf.fatal('The version of gcc you are using (%s) is too old.\n'
15 'The minimum supported gcc version is 4.8.2.' %
16 '.'.join(conf.env['CC_VERSION']))
Alexander Afanasyev01515792014-12-16 14:20:01 -080017 flags = GccFlags()
18 elif cxx == 'clang':
Davide Pesavento724d65a2017-06-23 17:14:08 -040019 if ccver < (3, 4, 0):
20 conf.fatal('The version of clang you are using (%s) is too old.\n'
21 'The minimum supported clang version is 3.4.0.' %
22 '.'.join(conf.env['CC_VERSION']))
Alexander Afanasyev01515792014-12-16 14:20:01 -080023 flags = ClangFlags()
24 else:
25 flags = CompilerFlags()
Davide Pesaventod06ea052015-10-02 01:48:24 +020026 Logs.warn('The code has not yet been tested with %s compiler' % cxx)
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -070027
Alexander Afanasyev01515792014-12-16 14:20:01 -080028 areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
29
Davide Pesaventod06ea052015-10-02 01:48:24 +020030 # General flags are always applied (e.g., selecting C++11 mode)
Alexander Afanasyev01515792014-12-16 14:20:01 -080031 generalFlags = flags.getGeneralFlags(conf)
32 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
33 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
34 conf.env.DEFINES += generalFlags['DEFINES']
35
Davide Pesaventod06ea052015-10-02 01:48:24 +020036 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Alexander Afanasyev01515792014-12-16 14:20:01 -080037 # corresponding environment variables are not set.
Davide Pesaventod06ea052015-10-02 01:48:24 +020038 # DEFINES are always applied.
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070039 if conf.options.debug:
Alexander Afanasyev01515792014-12-16 14:20:01 -080040 extraFlags = flags.getDebugFlags(conf)
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070041 if areCustomCxxflagsPresent:
Alexander Afanasyev01515792014-12-16 14:20:01 -080042 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070043 if len(missingFlags) > 0:
44 Logs.warn("Selected debug mode, but CXXFLAGS is set to a custom value '%s'"
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -070045 % " ".join(conf.env.CXXFLAGS))
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070046 Logs.warn("Default flags '%s' are not activated" % " ".join(missingFlags))
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070047 else:
Alexander Afanasyev01515792014-12-16 14:20:01 -080048 extraFlags = flags.getOptimizedFlags(conf)
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070049
Alexander Afanasyev01515792014-12-16 14:20:01 -080050 if not areCustomCxxflagsPresent:
51 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Davide Pesavento7c02b472015-10-28 20:50:17 +010052 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Alexander Afanasyev01515792014-12-16 14:20:01 -080053
54 conf.env.DEFINES += extraFlags['DEFINES']
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020055
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070056@Configure.conf
57def add_supported_cxxflags(self, cxxflags):
58 """
59 Check which cxxflags are supported by compiler and add them to env.CXXFLAGS variable
60 """
Alexander Afanasyev01515792014-12-16 14:20:01 -080061 if len(cxxflags) == 0:
62 return
63
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020064 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070065
66 supportedFlags = []
67 for flag in cxxflags:
Alexander Afanasyev3e6a98e2014-04-25 12:02:13 -070068 if self.check_cxx(cxxflags=['-Werror', flag], mandatory=False):
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070069 supportedFlags += [flag]
70
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -070071 self.end_msg(' '.join(supportedFlags))
Davide Pesavento1349d2d2016-08-09 06:43:57 +020072 self.env.prepend_value('CXXFLAGS', supportedFlags)
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020073
74@Configure.conf
75def add_supported_linkflags(self, linkflags):
76 """
77 Check which linkflags are supported by compiler and add them to env.LINKFLAGS variable
78 """
Alexander Afanasyev01515792014-12-16 14:20:01 -080079 if len(linkflags) == 0:
80 return
81
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020082 self.start_msg('Checking supported LINKFLAGS')
83
84 supportedFlags = []
85 for flag in linkflags:
86 if self.check_cxx(linkflags=['-Werror', flag], mandatory=False):
87 supportedFlags += [flag]
88
89 self.end_msg(' '.join(supportedFlags))
Davide Pesavento1349d2d2016-08-09 06:43:57 +020090 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyev01515792014-12-16 14:20:01 -080091
92
93class CompilerFlags(object):
94 def getGeneralFlags(self, conf):
Davide Pesavento7c02b472015-10-28 20:50:17 +010095 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Alexander Afanasyev01515792014-12-16 14:20:01 -080096 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
97
98 def getDebugFlags(self, conf):
Davide Pesavento7c02b472015-10-28 20:50:17 +010099 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Alexander Afanasyev01515792014-12-16 14:20:01 -0800100 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
101
102 def getOptimizedFlags(self, conf):
Davide Pesavento7c02b472015-10-28 20:50:17 +0100103 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Alexander Afanasyev01515792014-12-16 14:20:01 -0800104 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
105
106class GccBasicFlags(CompilerFlags):
Davide Pesaventod06ea052015-10-02 01:48:24 +0200107 """
108 This class defines basic flags that work for both gcc and clang compilers
109 """
Davide Pesavento724d65a2017-06-23 17:14:08 -0400110 def getGeneralFlags(self, conf):
111 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
112 flags['CXXFLAGS'] += ['-std=c++11']
113 return flags
114
Alexander Afanasyev01515792014-12-16 14:20:01 -0800115 def getDebugFlags(self, conf):
116 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Davide Pesavento7c02b472015-10-28 20:50:17 +0100117 flags['CXXFLAGS'] += ['-O0',
Davide Pesaventof714c1f2017-04-12 23:53:40 -0400118 '-Og', # gcc >= 4.8, clang >= 4.0
Alexander Afanasyev01515792014-12-16 14:20:01 -0800119 '-g3',
Davide Pesavento7c02b472015-10-28 20:50:17 +0100120 '-pedantic',
121 '-Wall',
122 '-Wextra',
Alexander Afanasyev01515792014-12-16 14:20:01 -0800123 '-Werror',
Davide Pesavento7f20d6e2017-01-16 14:43:58 -0500124 '-Wnon-virtual-dtor',
Davide Pesavento7c02b472015-10-28 20:50:17 +0100125 '-Wno-unused-parameter',
Alexander Afanasyev01515792014-12-16 14:20:01 -0800126 '-Wno-error=maybe-uninitialized', # Bug #1615
Alexander Afanasyev465c9732016-10-11 16:35:32 -0700127 '-Wno-error=deprecated-declarations', # Bug #3795
Davide Pesavento1aafba42015-09-17 17:22:31 -0700128 ]
Davide Pesavento37271c52017-06-23 00:58:50 -0400129 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Alexander Afanasyev01515792014-12-16 14:20:01 -0800130 return flags
131
132 def getOptimizedFlags(self, conf):
133 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
Davide Pesavento7c02b472015-10-28 20:50:17 +0100134 flags['CXXFLAGS'] += ['-O2',
Davide Pesavento1aafba42015-09-17 17:22:31 -0700135 '-g',
Davide Pesavento7c02b472015-10-28 20:50:17 +0100136 '-pedantic',
137 '-Wall',
138 '-Wextra',
Davide Pesavento7f20d6e2017-01-16 14:43:58 -0500139 '-Wnon-virtual-dtor',
Davide Pesavento7c02b472015-10-28 20:50:17 +0100140 '-Wno-unused-parameter',
Davide Pesavento1aafba42015-09-17 17:22:31 -0700141 ]
Davide Pesavento37271c52017-06-23 00:58:50 -0400142 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Alexander Afanasyev01515792014-12-16 14:20:01 -0800143 return flags
144
145class GccFlags(GccBasicFlags):
Alexander Afanasyev01515792014-12-16 14:20:01 -0800146 def getDebugFlags(self, conf):
147 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesavento7c02b472015-10-28 20:50:17 +0100148 version = tuple(int(i) for i in conf.env['CC_VERSION'])
149 if version < (5, 1, 0):
150 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
Davide Pesaventof714c1f2017-04-12 23:53:40 -0400151 flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
Alexander Afanasyev01515792014-12-16 14:20:01 -0800152 return flags
153
Davide Pesaventod06ea052015-10-02 01:48:24 +0200154 def getOptimizedFlags(self, conf):
155 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesavento7c02b472015-10-28 20:50:17 +0100156 version = tuple(int(i) for i in conf.env['CC_VERSION'])
157 if version < (5, 1, 0):
158 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
Davide Pesaventod06ea052015-10-02 01:48:24 +0200159 flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
160 return flags
161
Alexander Afanasyev01515792014-12-16 14:20:01 -0800162class ClangFlags(GccBasicFlags):
163 def getGeneralFlags(self, conf):
164 flags = super(ClangFlags, self).getGeneralFlags(conf)
Davide Pesaventod06ea052015-10-02 01:48:24 +0200165 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev01515792014-12-16 14:20:01 -0800166 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)
Davide Pesavento1aafba42015-09-17 17:22:31 -0700172 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesaventod06ea052015-10-02 01:48:24 +0200173 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Davide Pesavento1aafba42015-09-17 17:22:31 -0700174 '-Wno-error=unneeded-internal-declaration', # Bug #1588
175 '-Wno-error=deprecated-register',
Junxiao Shib1ba3e02015-10-04 16:56:37 -0700176 '-Wno-error=keyword-macro', # Bug #3235
Alexander Afanasyevefa22d62015-12-04 13:39:17 -0800177 '-Wno-error=infinite-recursion', # Bug #3358
Davide Pesaventod06ea052015-10-02 01:48:24 +0200178 ]
179 return flags
180
181 def getOptimizedFlags(self, conf):
182 flags = super(ClangFlags, self).getOptimizedFlags(conf)
183 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
184 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Davide Pesavento1aafba42015-09-17 17:22:31 -0700185 ]
Alexander Afanasyev01515792014-12-16 14:20:01 -0800186 return flags