blob: 54db7ea99aad9af40e521a223fd7443f73072ef9 [file] [log] [blame]
Yingdi Yu40cd1c32014-04-17 15:02:17 -07001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
Yingdi Yu40cd1c32014-04-17 15:02:17 -07002
Alexander Afanasyev67758b12018-03-06 18:36:44 -05003from waflib import Configure, Logs, Utils
Yingdi Yu40cd1c32014-04-17 15:02:17 -07004
5def options(opt):
Alexander Afanasyev67758b12018-03-06 18:36:44 -05006 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
7 help='Compile in debugging mode with minimal optimizations (-O0 or -Og)')
Yingdi Yu40cd1c32014-04-17 15:02:17 -07008
9def configure(conf):
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -040010 conf.start_msg('Checking C++ compiler version')
11
Alexander Afanasyev67758b12018-03-06 18:36:44 -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 Pesavento6ebfd7c2017-08-26 17:52:54 -040015 errmsg = ''
16 warnmsg = ''
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080017 if cxx == 'gcc':
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -040018 if ccver < (4, 8, 2):
19 errmsg = ('The version of gcc you are using is too old.\n'
20 'The minimum supported gcc version is 4.8.2.')
Alexander Afanasyev67758b12018-03-06 18:36:44 -050021 conf.flags = GccFlags()
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080022 elif cxx == 'clang':
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -040023 if ccver < (3, 4, 0):
24 errmsg = ('The version of clang you are using is too old.\n'
25 'The minimum supported clang version is 3.4.0.')
Alexander Afanasyev67758b12018-03-06 18:36:44 -050026 conf.flags = ClangFlags()
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080027 else:
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -040028 warnmsg = 'Note: %s compiler is unsupported' % cxx
Alexander Afanasyev67758b12018-03-06 18:36:44 -050029 conf.flags = CompilerFlags()
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -040030
31 if errmsg:
Alexander Afanasyev67758b12018-03-06 18:36:44 -050032 conf.end_msg(ccverstr, color='RED')
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -040033 conf.fatal(errmsg)
34 elif warnmsg:
Alexander Afanasyev67758b12018-03-06 18:36:44 -050035 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -040036 Logs.warn(warnmsg)
37 else:
Alexander Afanasyev67758b12018-03-06 18:36:44 -050038 conf.end_msg(ccverstr)
Yingdi Yu40cd1c32014-04-17 15:02:17 -070039
Alexander Afanasyev67758b12018-03-06 18:36:44 -050040 conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080041
42 # General flags are always applied (e.g., selecting C++11 mode)
Alexander Afanasyev67758b12018-03-06 18:36:44 -050043 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080044 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
45 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
46 conf.env.DEFINES += generalFlags['DEFINES']
47
Alexander Afanasyev67758b12018-03-06 18:36:44 -050048@Configure.conf
49def check_compiler_flags(conf):
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080050 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
51 # corresponding environment variables are not set.
52 # DEFINES are always applied.
Yingdi Yu40cd1c32014-04-17 15:02:17 -070053 if conf.options.debug:
Alexander Afanasyev67758b12018-03-06 18:36:44 -050054 extraFlags = conf.flags.getDebugFlags(conf)
55 if conf.areCustomCxxflagsPresent:
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080056 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev67758b12018-03-06 18:36:44 -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))
Yingdi Yu40cd1c32014-04-17 15:02:17 -070061 else:
Alexander Afanasyev67758b12018-03-06 18:36:44 -050062 extraFlags = conf.flags.getOptimizedFlags(conf)
Yingdi Yu40cd1c32014-04-17 15:02:17 -070063
Alexander Afanasyev67758b12018-03-06 18:36:44 -050064 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080065 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
66 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
67
68 conf.env.DEFINES += extraFlags['DEFINES']
Vince Lehman0a7da612014-10-29 14:39:29 -050069
Yingdi Yu40cd1c32014-04-17 15:02:17 -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 Afanasyevf9f39102015-12-01 17:43:40 -080075 if len(cxxflags) == 0:
76 return
77
78 self.start_msg('Checking supported CXXFLAGS')
Yingdi Yu40cd1c32014-04-17 15:02:17 -070079
80 supportedFlags = []
Alexander Afanasyev67758b12018-03-06 18:36:44 -050081 for flags in cxxflags:
82 flags = Utils.to_list(flags)
83 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
84 supportedFlags += flags
Yingdi Yu40cd1c32014-04-17 15:02:17 -070085
86 self.end_msg(' '.join(supportedFlags))
Davide Pesaventoc58cc7f2017-08-08 16:51:28 -050087 self.env.prepend_value('CXXFLAGS', supportedFlags)
Vince Lehman0a7da612014-10-29 14:39:29 -050088
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 Afanasyevf9f39102015-12-01 17:43:40 -080094 if len(linkflags) == 0:
95 return
96
97 self.start_msg('Checking supported LINKFLAGS')
Vince Lehman0a7da612014-10-29 14:39:29 -050098
99 supportedFlags = []
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500100 for flags in linkflags:
101 flags = Utils.to_list(flags)
102 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
103 supportedFlags += flags
Vince Lehman0a7da612014-10-29 14:39:29 -0500104
105 self.end_msg(' '.join(supportedFlags))
Davide Pesaventoc58cc7f2017-08-08 16:51:28 -0500106 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800107
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -0400108
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800109class CompilerFlags(object):
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500110 def getCompilerVersion(self, conf):
111 return tuple(int(i) for i in conf.env.CC_VERSION)
112
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800113 def getGeneralFlags(self, conf):
114 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
115 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
116
117 def getDebugFlags(self, conf):
118 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
119 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
120
121 def getOptimizedFlags(self, conf):
122 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
123 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
124
125class GccBasicFlags(CompilerFlags):
126 """
127 This class defines basic flags that work for both gcc and clang compilers
128 """
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -0400129 def getGeneralFlags(self, conf):
130 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
131 flags['CXXFLAGS'] += ['-std=c++11']
132 return flags
133
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800134 def getDebugFlags(self, conf):
135 flags = super(GccBasicFlags, self).getDebugFlags(conf)
136 flags['CXXFLAGS'] += ['-O0',
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -0400137 '-Og', # gcc >= 4.8, clang >= 4.0
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800138 '-g3',
139 '-pedantic',
140 '-Wall',
141 '-Wextra',
142 '-Werror',
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500143 '-Wnon-virtual-dtor',
Alexander Afanasyev96bf2f02016-10-11 16:37:45 -0700144 '-Wno-error=deprecated-declarations', # Bug #3795
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -0400145 '-Wno-error=maybe-uninitialized', # Bug #1615
146 '-Wno-unused-parameter',
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800147 ]
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -0400148 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800149 return flags
150
151 def getOptimizedFlags(self, conf):
152 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
153 flags['CXXFLAGS'] += ['-O2',
154 '-g',
155 '-pedantic',
156 '-Wall',
157 '-Wextra',
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500158 '-Wnon-virtual-dtor',
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800159 '-Wno-unused-parameter',
160 ]
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -0400161 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800162 return flags
163
164class GccFlags(GccBasicFlags):
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800165 def getDebugFlags(self, conf):
166 flags = super(GccFlags, self).getDebugFlags(conf)
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500167 if self.getCompilerVersion(conf) < (5, 1, 0):
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800168 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -0400169 flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800170 return flags
171
172 def getOptimizedFlags(self, conf):
173 flags = super(GccFlags, self).getOptimizedFlags(conf)
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500174 if self.getCompilerVersion(conf) < (5, 1, 0):
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800175 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
176 flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
177 return flags
178
179class ClangFlags(GccBasicFlags):
180 def getGeneralFlags(self, conf):
181 flags = super(ClangFlags, self).getGeneralFlags(conf)
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500182 if Utils.unversioned_sys_platform() == 'darwin' and self.getCompilerVersion(conf) >= (9, 0, 0):
183 # Bug #4296
184 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include'], # for Homebrew
185 ['-isystem', '/opt/local/include']] # for MacPorts
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800186 return flags
187
188 def getDebugFlags(self, conf):
189 flags = super(ClangFlags, self).getDebugFlags(conf)
190 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500191 '-Wextra-semi',
192 '-Wundefined-func-template',
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800193 '-Wno-error=deprecated-register',
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -0400194 '-Wno-error=infinite-recursion', # Bug #3358
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800195 '-Wno-error=keyword-macro', # Bug #3235
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -0400196 '-Wno-error=unneeded-internal-declaration', # Bug #1588
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -0400197 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800198 ]
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500199 version = self.getCompilerVersion(conf)
200 if version < (3, 9, 0) or (Utils.unversioned_sys_platform() == 'darwin' and version < (8, 1, 0)):
201 flags['CXXFLAGS'] += ['-Wno-unknown-pragmas']
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800202 return flags
203
204 def getOptimizedFlags(self, conf):
205 flags = super(ClangFlags, self).getOptimizedFlags(conf)
206 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500207 '-Wextra-semi',
208 '-Wundefined-func-template',
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -0400209 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800210 ]
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500211 version = self.getCompilerVersion(conf)
212 if version < (3, 9, 0) or (Utils.unversioned_sys_platform() == 'darwin' and version < (8, 1, 0)):
213 flags['CXXFLAGS'] += ['-Wno-unknown-pragmas']
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800214 return flags