blob: 1521d2a48f1c22ce6ed7dcf2db5c33b1b7bcccc2 [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 Afanasyevb5220702017-09-21 18:57:00 -040020 conf.flags = GccFlags()
Alexander Afanasyev76818632015-01-26 21:30:45 -080021 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 Afanasyevb5220702017-09-21 18:57:00 -040025 conf.flags = ClangFlags()
Alexander Afanasyev76818632015-01-26 21:30:45 -080026 else:
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040027 warnmsg = 'Note: %s compiler is unsupported' % cxx
Alexander Afanasyevb5220702017-09-21 18:57:00 -040028 conf.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 Afanasyevb5220702017-09-21 18:57:00 -040039 conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
Alexander Afanasyev76818632015-01-26 21:30:45 -080040
Davide Pesaventof485d892015-10-02 02:00:54 +020041 # General flags are always applied (e.g., selecting C++11 mode)
Alexander Afanasyevb5220702017-09-21 18:57:00 -040042 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyev76818632015-01-26 21:30:45 -080043 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
44 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
45 conf.env.DEFINES += generalFlags['DEFINES']
46
Alexander Afanasyevb5220702017-09-21 18:57:00 -040047@Configure.conf
48def check_compiler_flags(conf):
Davide Pesaventof485d892015-10-02 02:00:54 +020049 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Alexander Afanasyev76818632015-01-26 21:30:45 -080050 # corresponding environment variables are not set.
Davide Pesaventof485d892015-10-02 02:00:54 +020051 # DEFINES are always applied.
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070052 if conf.options.debug:
Alexander Afanasyevb5220702017-09-21 18:57:00 -040053 extraFlags = conf.flags.getDebugFlags(conf)
54 if conf.areCustomCxxflagsPresent:
Alexander Afanasyev76818632015-01-26 21:30:45 -080055 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070056 if len(missingFlags) > 0:
57 Logs.warn("Selected debug mode, but CXXFLAGS is set to a custom value '%s'"
58 % " ".join(conf.env.CXXFLAGS))
59 Logs.warn("Default flags '%s' are not activated" % " ".join(missingFlags))
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070060 else:
Alexander Afanasyevb5220702017-09-21 18:57:00 -040061 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070062
Alexander Afanasyevb5220702017-09-21 18:57:00 -040063 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyev76818632015-01-26 21:30:45 -080064 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Davide Pesaventoed89ccc2015-11-01 20:35:04 +010065 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Alexander Afanasyev76818632015-01-26 21:30:45 -080066
67 conf.env.DEFINES += extraFlags['DEFINES']
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020068
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070069@Configure.conf
70def add_supported_cxxflags(self, cxxflags):
71 """
72 Check which cxxflags are supported by compiler and add them to env.CXXFLAGS variable
73 """
Alexander Afanasyev76818632015-01-26 21:30:45 -080074 if len(cxxflags) == 0:
75 return
76
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020077 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070078
79 supportedFlags = []
Alexander Afanasyevb5220702017-09-21 18:57:00 -040080 for flags in cxxflags:
81 flags = Utils.to_list(flags)
82 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
83 supportedFlags += flags
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070084
Davide Pesavento1bdef282014-04-08 20:59:50 +020085 self.end_msg(' '.join(supportedFlags))
Davide Pesavento67f30272016-08-10 01:55:16 +000086 self.env.prepend_value('CXXFLAGS', supportedFlags)
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020087
88@Configure.conf
89def add_supported_linkflags(self, linkflags):
90 """
91 Check which linkflags are supported by compiler and add them to env.LINKFLAGS variable
92 """
Alexander Afanasyev76818632015-01-26 21:30:45 -080093 if len(linkflags) == 0:
94 return
95
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020096 self.start_msg('Checking supported LINKFLAGS')
97
98 supportedFlags = []
Alexander Afanasyevb5220702017-09-21 18:57:00 -040099 for flags in linkflags:
100 flags = Utils.to_list(flags)
101 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
102 supportedFlags += flags
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200103
104 self.end_msg(' '.join(supportedFlags))
Davide Pesavento67f30272016-08-10 01:55:16 +0000105 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyev76818632015-01-26 21:30:45 -0800106
107
108class CompilerFlags(object):
109 def getGeneralFlags(self, conf):
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100110 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Alexander Afanasyev76818632015-01-26 21:30:45 -0800111 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
112
113 def getDebugFlags(self, conf):
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100114 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Alexander Afanasyev76818632015-01-26 21:30:45 -0800115 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
116
117 def getOptimizedFlags(self, conf):
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100118 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Alexander Afanasyev76818632015-01-26 21:30:45 -0800119 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
120
121class GccBasicFlags(CompilerFlags):
Davide Pesaventof485d892015-10-02 02:00:54 +0200122 """
123 This class defines basic flags that work for both gcc and clang compilers
124 """
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -0400125 def getGeneralFlags(self, conf):
126 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
127 flags['CXXFLAGS'] += ['-std=c++11']
128 return flags
129
Alexander Afanasyev76818632015-01-26 21:30:45 -0800130 def getDebugFlags(self, conf):
131 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100132 flags['CXXFLAGS'] += ['-O0',
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -0400133 '-Og', # gcc >= 4.8, clang >= 4.0
Alexander Afanasyev76818632015-01-26 21:30:45 -0800134 '-g3',
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100135 '-pedantic',
136 '-Wall',
137 '-Wextra',
Alexander Afanasyev76818632015-01-26 21:30:45 -0800138 '-Werror',
Alexander Afanasyev436f3bd2016-10-11 16:37:45 -0700139 '-Wno-error=deprecated-declarations', # Bug #3795
Davide Pesaventoc0a5a392017-08-19 16:49:30 -0400140 '-Wno-error=maybe-uninitialized', # Bug #1615
141 '-Wno-unused-parameter',
Davide Pesaventof485d892015-10-02 02:00:54 +0200142 ]
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -0400143 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Alexander Afanasyev76818632015-01-26 21:30:45 -0800144 return flags
145
146 def getOptimizedFlags(self, conf):
147 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100148 flags['CXXFLAGS'] += ['-O2',
Davide Pesaventof485d892015-10-02 02:00:54 +0200149 '-g',
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100150 '-pedantic',
151 '-Wall',
152 '-Wextra',
153 '-Wno-unused-parameter',
Davide Pesaventof485d892015-10-02 02:00:54 +0200154 ]
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -0400155 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Alexander Afanasyev76818632015-01-26 21:30:45 -0800156 return flags
157
158class GccFlags(GccBasicFlags):
Alexander Afanasyev76818632015-01-26 21:30:45 -0800159 def getDebugFlags(self, conf):
160 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100161 version = tuple(int(i) for i in conf.env['CC_VERSION'])
162 if version < (5, 1, 0):
163 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -0400164 flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
Davide Pesaventof485d892015-10-02 02:00:54 +0200165 return flags
166
167 def getOptimizedFlags(self, conf):
168 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100169 version = tuple(int(i) for i in conf.env['CC_VERSION'])
170 if version < (5, 1, 0):
171 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
Davide Pesaventof485d892015-10-02 02:00:54 +0200172 flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
Alexander Afanasyev76818632015-01-26 21:30:45 -0800173 return flags
174
175class ClangFlags(GccBasicFlags):
176 def getGeneralFlags(self, conf):
177 flags = super(ClangFlags, self).getGeneralFlags(conf)
Alexander Afanasyevb5220702017-09-21 18:57:00 -0400178 version = tuple(int(i) for i in conf.env['CC_VERSION'])
179 if Utils.unversioned_sys_platform() == 'darwin' and version >= (9, 0, 0): # Bug #4296
180 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include'], # for Homebrew
181 ['-isystem', '/opt/local/include']] # for MacPorts
Alexander Afanasyev76818632015-01-26 21:30:45 -0800182 return flags
183
184 def getDebugFlags(self, conf):
185 flags = super(ClangFlags, self).getDebugFlags(conf)
Davide Pesaventof485d892015-10-02 02:00:54 +0200186 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesaventoc0a5a392017-08-19 16:49:30 -0400187 '-Wextra-semi',
188 '-Wundefined-func-template',
Davide Pesaventof485d892015-10-02 02:00:54 +0200189 '-Wno-error=deprecated-register',
Alexander Afanasyev0fc97482016-09-13 18:33:15 +0000190 '-Wno-error=infinite-recursion', # Bug #3358
Davide Pesaventoc0a5a392017-08-19 16:49:30 -0400191 '-Wno-error=keyword-macro', # Bug #3235
192 '-Wno-error=unneeded-internal-declaration', # Bug #1588
193 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Davide Pesaventof485d892015-10-02 02:00:54 +0200194 ]
Davide Pesaventoc0a5a392017-08-19 16:49:30 -0400195 version = tuple(int(i) for i in conf.env['CC_VERSION'])
Davide Pesaventoa997d292017-08-24 20:16:59 -0400196 if version < (3, 9, 0) or (Utils.unversioned_sys_platform() == 'darwin' and version < (8, 1, 0)):
Davide Pesaventoc0a5a392017-08-19 16:49:30 -0400197 flags['CXXFLAGS'] += ['-Wno-unknown-pragmas']
Davide Pesaventof485d892015-10-02 02:00:54 +0200198 return flags
199
200 def getOptimizedFlags(self, conf):
201 flags = super(ClangFlags, self).getOptimizedFlags(conf)
202 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesaventoc0a5a392017-08-19 16:49:30 -0400203 '-Wextra-semi',
204 '-Wundefined-func-template',
Davide Pesaventof485d892015-10-02 02:00:54 +0200205 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
206 ]
Davide Pesaventoc0a5a392017-08-19 16:49:30 -0400207 version = tuple(int(i) for i in conf.env['CC_VERSION'])
Davide Pesaventoa997d292017-08-24 20:16:59 -0400208 if version < (3, 9, 0) or (Utils.unversioned_sys_platform() == 'darwin' and version < (8, 1, 0)):
Davide Pesaventoc0a5a392017-08-19 16:49:30 -0400209 flags['CXXFLAGS'] += ['-Wno-unknown-pragmas']
Alexander Afanasyev76818632015-01-26 21:30:45 -0800210 return flags