blob: c946d1f056ed68b76b1817377f40d38fe7c295ae [file] [log] [blame]
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -07001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -07002
Alexander Afanasyev76818632015-01-26 21:30:45 -08003from waflib import Logs, Configure, Utils
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -07004
5def options(opt):
6 opt.add_option('--debug', '--with-debug', action='store_true', default=False, dest='debug',
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -04007 help='''Compile in debugging mode with minimal optimizations (-O0 or -Og)''')
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -07008
9def configure(conf):
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040010 conf.start_msg('Checking C++ compiler version')
11
12 cxx = conf.env['CXX_NAME'] # CXX_NAME is the generic name of the compiler
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -040013 ccver = tuple(int(i) for i in conf.env['CC_VERSION'])
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040014 errmsg = ''
15 warnmsg = ''
Alexander Afanasyev76818632015-01-26 21:30:45 -080016 if cxx == 'gcc':
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -040017 if ccver < (4, 8, 2):
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040018 errmsg = ('The version of gcc you are using is too old.\n'
19 'The minimum supported gcc version is 4.8.2.')
Alexander Afanasyev76818632015-01-26 21:30:45 -080020 flags = GccFlags()
21 elif cxx == 'clang':
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -040022 if ccver < (3, 4, 0):
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040023 errmsg = ('The version of clang you are using is too old.\n'
24 'The minimum supported clang version is 3.4.0.')
Alexander Afanasyev76818632015-01-26 21:30:45 -080025 flags = ClangFlags()
26 else:
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040027 warnmsg = 'Note: %s compiler is unsupported' % cxx
Alexander Afanasyev76818632015-01-26 21:30:45 -080028 flags = CompilerFlags()
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040029
30 if errmsg:
31 conf.end_msg('.'.join(conf.env['CC_VERSION']), color='RED')
32 conf.fatal(errmsg)
33 elif warnmsg:
34 conf.end_msg('.'.join(conf.env['CC_VERSION']), color='YELLOW')
35 Logs.warn(warnmsg)
36 else:
37 conf.end_msg('.'.join(conf.env['CC_VERSION']))
Davide Pesavento1bdef282014-04-08 20:59:50 +020038
Alexander Afanasyev76818632015-01-26 21:30:45 -080039 areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
40
Davide Pesaventof485d892015-10-02 02:00:54 +020041 # General flags are always applied (e.g., selecting C++11 mode)
Alexander Afanasyev76818632015-01-26 21:30:45 -080042 generalFlags = flags.getGeneralFlags(conf)
43 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
44 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
45 conf.env.DEFINES += generalFlags['DEFINES']
46
Davide Pesaventof485d892015-10-02 02:00:54 +020047 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Alexander Afanasyev76818632015-01-26 21:30:45 -080048 # corresponding environment variables are not set.
Davide Pesaventof485d892015-10-02 02:00:54 +020049 # DEFINES are always applied.
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070050 if conf.options.debug:
Alexander Afanasyev76818632015-01-26 21:30:45 -080051 extraFlags = flags.getDebugFlags(conf)
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070052 if areCustomCxxflagsPresent:
Alexander Afanasyev76818632015-01-26 21:30:45 -080053 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070054 if len(missingFlags) > 0:
55 Logs.warn("Selected debug mode, but CXXFLAGS is set to a custom value '%s'"
56 % " ".join(conf.env.CXXFLAGS))
57 Logs.warn("Default flags '%s' are not activated" % " ".join(missingFlags))
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070058 else:
Alexander Afanasyev76818632015-01-26 21:30:45 -080059 extraFlags = flags.getOptimizedFlags(conf)
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070060
Alexander Afanasyev76818632015-01-26 21:30:45 -080061 if not areCustomCxxflagsPresent:
62 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Davide Pesaventoed89ccc2015-11-01 20:35:04 +010063 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Alexander Afanasyev76818632015-01-26 21:30:45 -080064
65 conf.env.DEFINES += extraFlags['DEFINES']
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020066
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070067@Configure.conf
68def add_supported_cxxflags(self, cxxflags):
69 """
70 Check which cxxflags are supported by compiler and add them to env.CXXFLAGS variable
71 """
Alexander Afanasyev76818632015-01-26 21:30:45 -080072 if len(cxxflags) == 0:
73 return
74
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020075 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070076
77 supportedFlags = []
78 for flag in cxxflags:
Alexander Afanasyevd35176c2014-04-25 19:09:41 -070079 if self.check_cxx(cxxflags=['-Werror', flag], mandatory=False):
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070080 supportedFlags += [flag]
81
Davide Pesavento1bdef282014-04-08 20:59:50 +020082 self.end_msg(' '.join(supportedFlags))
Davide Pesavento67f30272016-08-10 01:55:16 +000083 self.env.prepend_value('CXXFLAGS', supportedFlags)
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020084
85@Configure.conf
86def add_supported_linkflags(self, linkflags):
87 """
88 Check which linkflags are supported by compiler and add them to env.LINKFLAGS variable
89 """
Alexander Afanasyev76818632015-01-26 21:30:45 -080090 if len(linkflags) == 0:
91 return
92
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020093 self.start_msg('Checking supported LINKFLAGS')
94
95 supportedFlags = []
96 for flag in linkflags:
97 if self.check_cxx(linkflags=['-Werror', flag], mandatory=False):
98 supportedFlags += [flag]
99
100 self.end_msg(' '.join(supportedFlags))
Davide Pesavento67f30272016-08-10 01:55:16 +0000101 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyev76818632015-01-26 21:30:45 -0800102
103
104class CompilerFlags(object):
105 def getGeneralFlags(self, conf):
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100106 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Alexander Afanasyev76818632015-01-26 21:30:45 -0800107 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
108
109 def getDebugFlags(self, conf):
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100110 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Alexander Afanasyev76818632015-01-26 21:30:45 -0800111 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
112
113 def getOptimizedFlags(self, conf):
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100114 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Alexander Afanasyev76818632015-01-26 21:30:45 -0800115 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
116
117class GccBasicFlags(CompilerFlags):
Davide Pesaventof485d892015-10-02 02:00:54 +0200118 """
119 This class defines basic flags that work for both gcc and clang compilers
120 """
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -0400121 def getGeneralFlags(self, conf):
122 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
123 flags['CXXFLAGS'] += ['-std=c++11']
124 return flags
125
Alexander Afanasyev76818632015-01-26 21:30:45 -0800126 def getDebugFlags(self, conf):
127 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100128 flags['CXXFLAGS'] += ['-O0',
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -0400129 '-Og', # gcc >= 4.8, clang >= 4.0
Alexander Afanasyev76818632015-01-26 21:30:45 -0800130 '-g3',
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100131 '-pedantic',
132 '-Wall',
133 '-Wextra',
Alexander Afanasyev76818632015-01-26 21:30:45 -0800134 '-Werror',
Alexander Afanasyev436f3bd2016-10-11 16:37:45 -0700135 '-Wno-error=deprecated-declarations', # Bug #3795
Davide Pesaventoc0a5a392017-08-19 16:49:30 -0400136 '-Wno-error=maybe-uninitialized', # Bug #1615
137 '-Wno-unused-parameter',
Davide Pesaventof485d892015-10-02 02:00:54 +0200138 ]
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -0400139 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Alexander Afanasyev76818632015-01-26 21:30:45 -0800140 return flags
141
142 def getOptimizedFlags(self, conf):
143 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100144 flags['CXXFLAGS'] += ['-O2',
Davide Pesaventof485d892015-10-02 02:00:54 +0200145 '-g',
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100146 '-pedantic',
147 '-Wall',
148 '-Wextra',
149 '-Wno-unused-parameter',
Davide Pesaventof485d892015-10-02 02:00:54 +0200150 ]
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -0400151 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Alexander Afanasyev76818632015-01-26 21:30:45 -0800152 return flags
153
154class GccFlags(GccBasicFlags):
Alexander Afanasyev76818632015-01-26 21:30:45 -0800155 def getDebugFlags(self, conf):
156 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100157 version = tuple(int(i) for i in conf.env['CC_VERSION'])
158 if version < (5, 1, 0):
159 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -0400160 flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
Davide Pesaventof485d892015-10-02 02:00:54 +0200161 return flags
162
163 def getOptimizedFlags(self, conf):
164 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100165 version = tuple(int(i) for i in conf.env['CC_VERSION'])
166 if version < (5, 1, 0):
167 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
Davide Pesaventof485d892015-10-02 02:00:54 +0200168 flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
Alexander Afanasyev76818632015-01-26 21:30:45 -0800169 return flags
170
171class ClangFlags(GccBasicFlags):
172 def getGeneralFlags(self, conf):
173 flags = super(ClangFlags, self).getGeneralFlags(conf)
Davide Pesaventof485d892015-10-02 02:00:54 +0200174 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev76818632015-01-26 21:30:45 -0800175 flags['CXXFLAGS'] += ['-stdlib=libc++']
176 flags['LINKFLAGS'] += ['-stdlib=libc++']
177 return flags
178
179 def getDebugFlags(self, conf):
180 flags = super(ClangFlags, self).getDebugFlags(conf)
Davide Pesaventof485d892015-10-02 02:00:54 +0200181 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesaventoc0a5a392017-08-19 16:49:30 -0400182 '-Wextra-semi',
183 '-Wundefined-func-template',
Davide Pesaventof485d892015-10-02 02:00:54 +0200184 '-Wno-error=deprecated-register',
Alexander Afanasyev0fc97482016-09-13 18:33:15 +0000185 '-Wno-error=infinite-recursion', # Bug #3358
Davide Pesaventoc0a5a392017-08-19 16:49:30 -0400186 '-Wno-error=keyword-macro', # Bug #3235
187 '-Wno-error=unneeded-internal-declaration', # Bug #1588
188 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Davide Pesaventof485d892015-10-02 02:00:54 +0200189 ]
Davide Pesaventoc0a5a392017-08-19 16:49:30 -0400190 version = tuple(int(i) for i in conf.env['CC_VERSION'])
Davide Pesaventoa997d292017-08-24 20:16:59 -0400191 if version < (3, 9, 0) or (Utils.unversioned_sys_platform() == 'darwin' and version < (8, 1, 0)):
Davide Pesaventoc0a5a392017-08-19 16:49:30 -0400192 flags['CXXFLAGS'] += ['-Wno-unknown-pragmas']
Davide Pesaventof485d892015-10-02 02:00:54 +0200193 return flags
194
195 def getOptimizedFlags(self, conf):
196 flags = super(ClangFlags, self).getOptimizedFlags(conf)
197 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesaventoc0a5a392017-08-19 16:49:30 -0400198 '-Wextra-semi',
199 '-Wundefined-func-template',
Davide Pesaventof485d892015-10-02 02:00:54 +0200200 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
201 ]
Davide Pesaventoc0a5a392017-08-19 16:49:30 -0400202 version = tuple(int(i) for i in conf.env['CC_VERSION'])
Davide Pesaventoa997d292017-08-24 20:16:59 -0400203 if version < (3, 9, 0) or (Utils.unversioned_sys_platform() == 'darwin' and version < (8, 1, 0)):
Davide Pesaventoc0a5a392017-08-19 16:49:30 -0400204 flags['CXXFLAGS'] += ['-Wno-unknown-pragmas']
Alexander Afanasyev76818632015-01-26 21:30:45 -0800205 return flags