blob: 54db7ea99aad9af40e521a223fd7443f73072ef9 [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
Davide Pesavento0064c1d2018-03-03 18:43:53 -05003from waflib import Configure, Logs, Utils
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -07004
5def options(opt):
Davide Pesavento0064c1d2018-03-03 18:43:53 -05006 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
7 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
Davide Pesavento0064c1d2018-03-03 18:43:53 -050012 cxx = conf.env.CXX_NAME # generic name of the compiler
13 ccver = tuple(int(i) for i in conf.env.CC_VERSION)
14 ccverstr = '.'.join(conf.env.CC_VERSION)
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040015 errmsg = ''
16 warnmsg = ''
Alexander Afanasyev76818632015-01-26 21:30:45 -080017 if cxx == 'gcc':
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -040018 if ccver < (4, 8, 2):
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040019 errmsg = ('The version of gcc you are using is too old.\n'
20 'The minimum supported gcc version is 4.8.2.')
Alexander Afanasyevb5220702017-09-21 18:57:00 -040021 conf.flags = GccFlags()
Alexander Afanasyev76818632015-01-26 21:30:45 -080022 elif cxx == 'clang':
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -040023 if ccver < (3, 4, 0):
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040024 errmsg = ('The version of clang you are using is too old.\n'
25 'The minimum supported clang version is 3.4.0.')
Alexander Afanasyevb5220702017-09-21 18:57:00 -040026 conf.flags = ClangFlags()
Alexander Afanasyev76818632015-01-26 21:30:45 -080027 else:
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040028 warnmsg = 'Note: %s compiler is unsupported' % cxx
Alexander Afanasyevb5220702017-09-21 18:57:00 -040029 conf.flags = CompilerFlags()
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040030
31 if errmsg:
Davide Pesavento0064c1d2018-03-03 18:43:53 -050032 conf.end_msg(ccverstr, color='RED')
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040033 conf.fatal(errmsg)
34 elif warnmsg:
Davide Pesavento0064c1d2018-03-03 18:43:53 -050035 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040036 Logs.warn(warnmsg)
37 else:
Davide Pesavento0064c1d2018-03-03 18:43:53 -050038 conf.end_msg(ccverstr)
Davide Pesavento1bdef282014-04-08 20:59:50 +020039
Alexander Afanasyevb5220702017-09-21 18:57:00 -040040 conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
Alexander Afanasyev76818632015-01-26 21:30:45 -080041
Davide Pesaventof485d892015-10-02 02:00:54 +020042 # General flags are always applied (e.g., selecting C++11 mode)
Alexander Afanasyevb5220702017-09-21 18:57:00 -040043 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyev76818632015-01-26 21:30:45 -080044 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
45 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
46 conf.env.DEFINES += generalFlags['DEFINES']
47
Alexander Afanasyevb5220702017-09-21 18:57:00 -040048@Configure.conf
49def check_compiler_flags(conf):
Davide Pesaventof485d892015-10-02 02:00:54 +020050 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Alexander Afanasyev76818632015-01-26 21:30:45 -080051 # corresponding environment variables are not set.
Davide Pesaventof485d892015-10-02 02:00:54 +020052 # DEFINES are always applied.
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070053 if conf.options.debug:
Alexander Afanasyevb5220702017-09-21 18:57:00 -040054 extraFlags = conf.flags.getDebugFlags(conf)
55 if conf.areCustomCxxflagsPresent:
Alexander Afanasyev76818632015-01-26 21:30:45 -080056 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Davide Pesavento0064c1d2018-03-03 18:43:53 -050057 if missingFlags:
58 Logs.warn('Selected debug mode, but CXXFLAGS is set to a custom value "%s"'
59 % ' '.join(conf.env.CXXFLAGS))
60 Logs.warn('Default flags "%s" will not be used' % ' '.join(missingFlags))
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070061 else:
Alexander Afanasyevb5220702017-09-21 18:57:00 -040062 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070063
Alexander Afanasyevb5220702017-09-21 18:57:00 -040064 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyev76818632015-01-26 21:30:45 -080065 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Davide Pesaventoed89ccc2015-11-01 20:35:04 +010066 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Alexander Afanasyev76818632015-01-26 21:30:45 -080067
68 conf.env.DEFINES += extraFlags['DEFINES']
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020069
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070070@Configure.conf
71def add_supported_cxxflags(self, cxxflags):
72 """
73 Check which cxxflags are supported by compiler and add them to env.CXXFLAGS variable
74 """
Alexander Afanasyev76818632015-01-26 21:30:45 -080075 if len(cxxflags) == 0:
76 return
77
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020078 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070079
80 supportedFlags = []
Alexander Afanasyevb5220702017-09-21 18:57:00 -040081 for flags in cxxflags:
82 flags = Utils.to_list(flags)
83 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
84 supportedFlags += flags
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070085
Davide Pesavento1bdef282014-04-08 20:59:50 +020086 self.end_msg(' '.join(supportedFlags))
Davide Pesavento67f30272016-08-10 01:55:16 +000087 self.env.prepend_value('CXXFLAGS', supportedFlags)
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020088
89@Configure.conf
90def add_supported_linkflags(self, linkflags):
91 """
92 Check which linkflags are supported by compiler and add them to env.LINKFLAGS variable
93 """
Alexander Afanasyev76818632015-01-26 21:30:45 -080094 if len(linkflags) == 0:
95 return
96
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020097 self.start_msg('Checking supported LINKFLAGS')
98
99 supportedFlags = []
Alexander Afanasyevb5220702017-09-21 18:57:00 -0400100 for flags in linkflags:
101 flags = Utils.to_list(flags)
102 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
103 supportedFlags += flags
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200104
105 self.end_msg(' '.join(supportedFlags))
Davide Pesavento67f30272016-08-10 01:55:16 +0000106 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyev76818632015-01-26 21:30:45 -0800107
108
109class CompilerFlags(object):
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500110 def getCompilerVersion(self, conf):
111 return tuple(int(i) for i in conf.env.CC_VERSION)
112
Alexander Afanasyev76818632015-01-26 21:30:45 -0800113 def getGeneralFlags(self, conf):
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100114 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Alexander Afanasyev76818632015-01-26 21:30:45 -0800115 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
116
117 def getDebugFlags(self, conf):
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100118 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Alexander Afanasyev76818632015-01-26 21:30:45 -0800119 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
120
121 def getOptimizedFlags(self, conf):
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100122 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Alexander Afanasyev76818632015-01-26 21:30:45 -0800123 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
124
125class GccBasicFlags(CompilerFlags):
Davide Pesaventof485d892015-10-02 02:00:54 +0200126 """
127 This class defines basic flags that work for both gcc and clang compilers
128 """
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -0400129 def getGeneralFlags(self, conf):
130 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
131 flags['CXXFLAGS'] += ['-std=c++11']
132 return flags
133
Alexander Afanasyev76818632015-01-26 21:30:45 -0800134 def getDebugFlags(self, conf):
135 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100136 flags['CXXFLAGS'] += ['-O0',
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -0400137 '-Og', # gcc >= 4.8, clang >= 4.0
Alexander Afanasyev76818632015-01-26 21:30:45 -0800138 '-g3',
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100139 '-pedantic',
140 '-Wall',
141 '-Wextra',
Alexander Afanasyev76818632015-01-26 21:30:45 -0800142 '-Werror',
Alexander Afanasyev847de402017-09-21 18:57:30 -0400143 '-Wnon-virtual-dtor',
Alexander Afanasyev436f3bd2016-10-11 16:37:45 -0700144 '-Wno-error=deprecated-declarations', # Bug #3795
Davide Pesaventoc0a5a392017-08-19 16:49:30 -0400145 '-Wno-error=maybe-uninitialized', # Bug #1615
146 '-Wno-unused-parameter',
Davide Pesaventof485d892015-10-02 02:00:54 +0200147 ]
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -0400148 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Alexander Afanasyev76818632015-01-26 21:30:45 -0800149 return flags
150
151 def getOptimizedFlags(self, conf):
152 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100153 flags['CXXFLAGS'] += ['-O2',
Davide Pesaventof485d892015-10-02 02:00:54 +0200154 '-g',
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100155 '-pedantic',
156 '-Wall',
157 '-Wextra',
Alexander Afanasyev847de402017-09-21 18:57:30 -0400158 '-Wnon-virtual-dtor',
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100159 '-Wno-unused-parameter',
Davide Pesaventof485d892015-10-02 02:00:54 +0200160 ]
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -0400161 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Alexander Afanasyev76818632015-01-26 21:30:45 -0800162 return flags
163
164class GccFlags(GccBasicFlags):
Alexander Afanasyev76818632015-01-26 21:30:45 -0800165 def getDebugFlags(self, conf):
166 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500167 if self.getCompilerVersion(conf) < (5, 1, 0):
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100168 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -0400169 flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
Davide Pesaventof485d892015-10-02 02:00:54 +0200170 return flags
171
172 def getOptimizedFlags(self, conf):
173 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500174 if self.getCompilerVersion(conf) < (5, 1, 0):
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100175 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
Davide Pesaventof485d892015-10-02 02:00:54 +0200176 flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
Alexander Afanasyev76818632015-01-26 21:30:45 -0800177 return flags
178
179class ClangFlags(GccBasicFlags):
180 def getGeneralFlags(self, conf):
181 flags = super(ClangFlags, self).getGeneralFlags(conf)
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500182 if Utils.unversioned_sys_platform() == 'darwin' and self.getCompilerVersion(conf) >= (9, 0, 0):
183 # Bug #4296
Alexander Afanasyevb5220702017-09-21 18:57:00 -0400184 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include'], # for Homebrew
185 ['-isystem', '/opt/local/include']] # for MacPorts
Alexander Afanasyev76818632015-01-26 21:30:45 -0800186 return flags
187
188 def getDebugFlags(self, conf):
189 flags = super(ClangFlags, self).getDebugFlags(conf)
Davide Pesaventof485d892015-10-02 02:00:54 +0200190 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesaventoc0a5a392017-08-19 16:49:30 -0400191 '-Wextra-semi',
192 '-Wundefined-func-template',
Davide Pesaventof485d892015-10-02 02:00:54 +0200193 '-Wno-error=deprecated-register',
Alexander Afanasyev0fc97482016-09-13 18:33:15 +0000194 '-Wno-error=infinite-recursion', # Bug #3358
Davide Pesaventoc0a5a392017-08-19 16:49:30 -0400195 '-Wno-error=keyword-macro', # Bug #3235
196 '-Wno-error=unneeded-internal-declaration', # Bug #1588
197 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Davide Pesaventof485d892015-10-02 02:00:54 +0200198 ]
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500199 version = self.getCompilerVersion(conf)
Davide Pesaventoa997d292017-08-24 20:16:59 -0400200 if version < (3, 9, 0) or (Utils.unversioned_sys_platform() == 'darwin' and version < (8, 1, 0)):
Davide Pesaventoc0a5a392017-08-19 16:49:30 -0400201 flags['CXXFLAGS'] += ['-Wno-unknown-pragmas']
Davide Pesaventof485d892015-10-02 02:00:54 +0200202 return flags
203
204 def getOptimizedFlags(self, conf):
205 flags = super(ClangFlags, self).getOptimizedFlags(conf)
206 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesaventoc0a5a392017-08-19 16:49:30 -0400207 '-Wextra-semi',
208 '-Wundefined-func-template',
Davide Pesaventof485d892015-10-02 02:00:54 +0200209 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
210 ]
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500211 version = self.getCompilerVersion(conf)
Davide Pesaventoa997d292017-08-24 20:16:59 -0400212 if version < (3, 9, 0) or (Utils.unversioned_sys_platform() == 'darwin' and version < (8, 1, 0)):
Davide Pesaventoc0a5a392017-08-19 16:49:30 -0400213 flags['CXXFLAGS'] += ['-Wno-unknown-pragmas']
Alexander Afanasyev76818632015-01-26 21:30:45 -0800214 return flags