blob: 9e045c3fb384505e04c63b74e8ddbda3a0f75ff2 [file] [log] [blame]
Zhiyi Zhang8617a792017-01-17 16:45:56 -08001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
2
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -07003from waflib import Configure, Logs, Utils
Zhiyi Zhang8617a792017-01-17 16:45:56 -08004
5def options(opt):
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -07006 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
7 help='Compile in debugging mode with minimal optimizations (-O0 or -Og)')
Zhiyi Zhang8617a792017-01-17 16:45:56 -08008
9def configure(conf):
Davide Pesavento08994782018-01-22 12:13:41 -050010 conf.start_msg('Checking C++ compiler version')
Zhiyi Zhang8617a792017-01-17 16:45:56 -080011
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -070012 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 Pesavento08994782018-01-22 12:13:41 -050015 errmsg = ''
16 warnmsg = ''
17 if cxx == 'gcc':
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -070018 if ccver < (5, 3, 0):
Davide Pesavento08994782018-01-22 12:13:41 -050019 errmsg = ('The version of gcc you are using is too old.\n'
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -070020 'The minimum supported gcc version is 5.3.0.')
Davide Pesavento08994782018-01-22 12:13:41 -050021 conf.flags = GccFlags()
22 elif cxx == 'clang':
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -070023 if ccver < (3, 6, 0):
Davide Pesavento08994782018-01-22 12:13:41 -050024 errmsg = ('The version of clang you are using is too old.\n'
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -070025 'The minimum supported clang version is 3.6.0.')
Davide Pesavento08994782018-01-22 12:13:41 -050026 conf.flags = ClangFlags()
27 else:
28 warnmsg = 'Note: %s compiler is unsupported' % cxx
29 conf.flags = CompilerFlags()
30
31 if errmsg:
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -070032 conf.end_msg(ccverstr, color='RED')
Davide Pesavento08994782018-01-22 12:13:41 -050033 conf.fatal(errmsg)
34 elif warnmsg:
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -070035 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento08994782018-01-22 12:13:41 -050036 Logs.warn(warnmsg)
37 else:
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -070038 conf.end_msg(ccverstr)
Davide Pesavento08994782018-01-22 12:13:41 -050039
40 conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
Zhiyi Zhang8617a792017-01-17 16:45:56 -080041
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -070042 # General flags are always applied (e.g., selecting C++ language standard)
Davide Pesavento08994782018-01-22 12:13:41 -050043 generalFlags = conf.flags.getGeneralFlags(conf)
Zhiyi Zhang8617a792017-01-17 16:45:56 -080044 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
45 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
46 conf.env.DEFINES += generalFlags['DEFINES']
47
Davide Pesavento08994782018-01-22 12:13:41 -050048@Configure.conf
49def check_compiler_flags(conf):
Zhiyi Zhang8617a792017-01-17 16:45:56 -080050 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
51 # corresponding environment variables are not set.
52 # DEFINES are always applied.
53 if conf.options.debug:
Davide Pesavento08994782018-01-22 12:13:41 -050054 extraFlags = conf.flags.getDebugFlags(conf)
55 if conf.areCustomCxxflagsPresent:
Zhiyi Zhang8617a792017-01-17 16:45:56 -080056 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -070057 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))
Zhiyi Zhang8617a792017-01-17 16:45:56 -080061 else:
Davide Pesavento08994782018-01-22 12:13:41 -050062 extraFlags = conf.flags.getOptimizedFlags(conf)
Zhiyi Zhang8617a792017-01-17 16:45:56 -080063
Davide Pesavento08994782018-01-22 12:13:41 -050064 if not conf.areCustomCxxflagsPresent:
Zhiyi Zhang8617a792017-01-17 16:45:56 -080065 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
66 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
67
68 conf.env.DEFINES += extraFlags['DEFINES']
69
70@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 """
75 if len(cxxflags) == 0:
76 return
77
78 self.start_msg('Checking supported CXXFLAGS')
79
80 supportedFlags = []
Davide Pesavento08994782018-01-22 12:13:41 -050081 for flags in cxxflags:
82 flags = Utils.to_list(flags)
83 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
84 supportedFlags += flags
Zhiyi Zhang8617a792017-01-17 16:45:56 -080085
86 self.end_msg(' '.join(supportedFlags))
87 self.env.prepend_value('CXXFLAGS', supportedFlags)
88
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 """
94 if len(linkflags) == 0:
95 return
96
97 self.start_msg('Checking supported LINKFLAGS')
98
99 supportedFlags = []
Davide Pesavento08994782018-01-22 12:13:41 -0500100 for flags in linkflags:
101 flags = Utils.to_list(flags)
102 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
103 supportedFlags += flags
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800104
105 self.end_msg(' '.join(supportedFlags))
106 self.env.prepend_value('LINKFLAGS', supportedFlags)
107
108
109class CompilerFlags(object):
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -0700110 def getCompilerVersion(self, conf):
111 return tuple(int(i) for i in conf.env.CC_VERSION)
112
Zhiyi Zhang8617a792017-01-17 16:45:56 -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 Pesavento08994782018-01-22 12:13:41 -0500129 def getGeneralFlags(self, conf):
130 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -0700131 flags['CXXFLAGS'] += ['-std=c++14']
Davide Pesavento55d98602019-10-15 22:40:05 -0400132 if Utils.unversioned_sys_platform() == 'linux':
133 flags['LINKFLAGS'] += ['-fuse-ld=gold']
134 elif Utils.unversioned_sys_platform() == 'freebsd':
135 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Davide Pesavento08994782018-01-22 12:13:41 -0500136 return flags
137
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800138 def getDebugFlags(self, conf):
139 flags = super(GccBasicFlags, self).getDebugFlags(conf)
140 flags['CXXFLAGS'] += ['-O0',
Davide Pesavento08994782018-01-22 12:13:41 -0500141 '-Og', # gcc >= 4.8, clang >= 4.0
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800142 '-g3',
143 '-pedantic',
144 '-Wall',
145 '-Wextra',
146 '-Werror',
Davide Pesavento08994782018-01-22 12:13:41 -0500147 '-Wnon-virtual-dtor',
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800148 '-Wno-error=deprecated-declarations', # Bug #3795
Davide Pesavento08994782018-01-22 12:13:41 -0500149 '-Wno-error=maybe-uninitialized', # Bug #1615
150 '-Wno-unused-parameter',
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800151 ]
Davide Pesavento55d98602019-10-15 22:40:05 -0400152 flags['LINKFLAGS'] += ['-Wl,-O1']
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800153 return flags
154
155 def getOptimizedFlags(self, conf):
156 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
157 flags['CXXFLAGS'] += ['-O2',
158 '-g',
159 '-pedantic',
160 '-Wall',
161 '-Wextra',
Davide Pesavento08994782018-01-22 12:13:41 -0500162 '-Wnon-virtual-dtor',
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800163 '-Wno-unused-parameter',
164 ]
Davide Pesavento55d98602019-10-15 22:40:05 -0400165 flags['LINKFLAGS'] += ['-Wl,-O1']
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800166 return flags
167
168class GccFlags(GccBasicFlags):
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800169 def getDebugFlags(self, conf):
170 flags = super(GccFlags, self).getDebugFlags(conf)
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -0700171 flags['CXXFLAGS'] += ['-fdiagnostics-color']
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800172 return flags
173
174 def getOptimizedFlags(self, conf):
175 flags = super(GccFlags, self).getOptimizedFlags(conf)
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -0700176 flags['CXXFLAGS'] += ['-fdiagnostics-color']
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800177 return flags
178
179class ClangFlags(GccBasicFlags):
180 def getGeneralFlags(self, conf):
181 flags = super(ClangFlags, self).getGeneralFlags(conf)
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -0700182 if Utils.unversioned_sys_platform() == 'darwin' and self.getCompilerVersion(conf) >= (9, 0, 0):
183 # Bug #4296
Davide Pesavento08994782018-01-22 12:13:41 -0500184 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include'], # for Homebrew
185 ['-isystem', '/opt/local/include']] # for MacPorts
Davide Pesavento55d98602019-10-15 22:40:05 -0400186 if Utils.unversioned_sys_platform() == 'freebsd':
187 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']] # Bug #4790
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800188 return flags
189
190 def getDebugFlags(self, conf):
191 flags = super(ClangFlags, self).getDebugFlags(conf)
192 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesavento08994782018-01-22 12:13:41 -0500193 '-Wextra-semi',
194 '-Wundefined-func-template',
Davide Pesavento08994782018-01-22 12:13:41 -0500195 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800196 ]
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -0700197 version = self.getCompilerVersion(conf)
Davide Pesavento08994782018-01-22 12:13:41 -0500198 if version < (3, 9, 0) or (Utils.unversioned_sys_platform() == 'darwin' and version < (8, 1, 0)):
199 flags['CXXFLAGS'] += ['-Wno-unknown-pragmas']
Davide Pesaventoaae119a2019-11-09 18:30:03 -0500200 if version < (6, 0, 0):
201 flags['CXXFLAGS'] += ['-Wno-missing-braces'] # Bug #4721
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800202 return flags
203
204 def getOptimizedFlags(self, conf):
205 flags = super(ClangFlags, self).getOptimizedFlags(conf)
206 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesavento08994782018-01-22 12:13:41 -0500207 '-Wextra-semi',
208 '-Wundefined-func-template',
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800209 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
210 ]
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -0700211 version = self.getCompilerVersion(conf)
Davide Pesavento08994782018-01-22 12:13:41 -0500212 if version < (3, 9, 0) or (Utils.unversioned_sys_platform() == 'darwin' and version < (8, 1, 0)):
213 flags['CXXFLAGS'] += ['-Wno-unknown-pragmas']
Davide Pesaventoaae119a2019-11-09 18:30:03 -0500214 if version < (6, 0, 0):
215 flags['CXXFLAGS'] += ['-Wno-missing-braces'] # Bug #4721
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800216 return flags