blob: f3be6e7bf841b3eb83173308ea7b1f877c0cfac1 [file] [log] [blame]
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -07001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
2
Davide Pesavento333e3eb2021-10-12 19:30:51 -04003import platform
Davide Pesavento98ff2bc2018-05-24 00:10:31 -04004from waflib import Configure, Logs, Utils
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -07005
Shuo Chen478204c2014-03-18 18:27:04 -07006def options(opt):
Davide Pesavento98ff2bc2018-05-24 00:10:31 -04007 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesavento333e3eb2021-10-12 19:30:51 -04008 help='Compile in debugging mode with minimal optimizations (-Og)')
Shuo Chen478204c2014-03-18 18:27:04 -07009
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070010def configure(conf):
Davide Pesaventob545aac2017-09-22 23:54:10 -040011 conf.start_msg('Checking C++ compiler version')
12
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040013 cxx = conf.env.CXX_NAME # generic name of the compiler
14 ccver = tuple(int(i) for i in conf.env.CC_VERSION)
15 ccverstr = '.'.join(conf.env.CC_VERSION)
Davide Pesaventob545aac2017-09-22 23:54:10 -040016 errmsg = ''
17 warnmsg = ''
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050018 if cxx == 'gcc':
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040019 if ccver < (5, 3, 0):
Davide Pesaventob545aac2017-09-22 23:54:10 -040020 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesavento333e3eb2021-10-12 19:30:51 -040021 'The minimum supported gcc version is 7.4.0.')
22 elif ccver < (7, 4, 0):
23 warnmsg = ('Using a version of gcc older than 7.4.0 is not '
24 'officially supported and may result in build failures.')
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000025 conf.flags = GccFlags()
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050026 elif cxx == 'clang':
Davide Pesavento333e3eb2021-10-12 19:30:51 -040027 if Utils.unversioned_sys_platform() == 'darwin' and ccver < (9, 0, 0):
28 errmsg = ('The version of Xcode you are using is too old.\n'
29 'The minimum supported Xcode version is 9.0.')
30 elif ccver < (4, 0, 0):
Davide Pesaventob545aac2017-09-22 23:54:10 -040031 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesavento333e3eb2021-10-12 19:30:51 -040032 'The minimum supported clang version is 4.0.')
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000033 conf.flags = ClangFlags()
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050034 else:
Davide Pesavento333e3eb2021-10-12 19:30:51 -040035 warnmsg = '%s compiler is unsupported' % cxx
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000036 conf.flags = CompilerFlags()
Davide Pesaventob545aac2017-09-22 23:54:10 -040037
38 if errmsg:
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040039 conf.end_msg(ccverstr, color='RED')
Davide Pesaventob545aac2017-09-22 23:54:10 -040040 conf.fatal(errmsg)
41 elif warnmsg:
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040042 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento333e3eb2021-10-12 19:30:51 -040043 Logs.warn('WARNING: ' + warnmsg)
Davide Pesaventob545aac2017-09-22 23:54:10 -040044 else:
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040045 conf.end_msg(ccverstr)
Shuo Chen478204c2014-03-18 18:27:04 -070046
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000047 conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050048
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040049 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000050 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050051 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
52 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
53 conf.env.DEFINES += generalFlags['DEFINES']
54
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000055@Configure.conf
56def check_compiler_flags(conf):
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050057 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
58 # corresponding environment variables are not set.
59 # DEFINES are always applied.
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070060 if conf.options.debug:
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000061 extraFlags = conf.flags.getDebugFlags(conf)
62 if conf.areCustomCxxflagsPresent:
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050063 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040064 if missingFlags:
65 Logs.warn('Selected debug mode, but CXXFLAGS is set to a custom value "%s"'
66 % ' '.join(conf.env.CXXFLAGS))
67 Logs.warn('Default flags "%s" will not be used' % ' '.join(missingFlags))
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070068 else:
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000069 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070070
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000071 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050072 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
73 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
74
75 conf.env.DEFINES += extraFlags['DEFINES']
Wentao Shanga8f3c402014-10-30 14:03:27 -070076
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070077@Configure.conf
78def add_supported_cxxflags(self, cxxflags):
79 """
80 Check which cxxflags are supported by compiler and add them to env.CXXFLAGS variable
81 """
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050082 if len(cxxflags) == 0:
83 return
84
Wentao Shanga8f3c402014-10-30 14:03:27 -070085 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070086
87 supportedFlags = []
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000088 for flags in cxxflags:
89 flags = Utils.to_list(flags)
90 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
91 supportedFlags += flags
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070092
Shuo Chen478204c2014-03-18 18:27:04 -070093 self.end_msg(' '.join(supportedFlags))
Junxiao Shie1801312017-07-22 19:16:49 +000094 self.env.prepend_value('CXXFLAGS', supportedFlags)
Wentao Shanga8f3c402014-10-30 14:03:27 -070095
96@Configure.conf
97def add_supported_linkflags(self, linkflags):
98 """
99 Check which linkflags are supported by compiler and add them to env.LINKFLAGS variable
100 """
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500101 if len(linkflags) == 0:
102 return
103
Wentao Shanga8f3c402014-10-30 14:03:27 -0700104 self.start_msg('Checking supported LINKFLAGS')
105
106 supportedFlags = []
Alexander Afanasyevbb058c02018-02-15 22:49:24 +0000107 for flags in linkflags:
108 flags = Utils.to_list(flags)
109 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
110 supportedFlags += flags
Wentao Shanga8f3c402014-10-30 14:03:27 -0700111
112 self.end_msg(' '.join(supportedFlags))
Junxiao Shie1801312017-07-22 19:16:49 +0000113 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500114
115
116class CompilerFlags(object):
Davide Pesavento98ff2bc2018-05-24 00:10:31 -0400117 def getCompilerVersion(self, conf):
118 return tuple(int(i) for i in conf.env.CC_VERSION)
119
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500120 def getGeneralFlags(self, conf):
121 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
122 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
123
124 def getDebugFlags(self, conf):
125 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
126 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
127
128 def getOptimizedFlags(self, conf):
129 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
130 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
131
132class GccBasicFlags(CompilerFlags):
133 """
134 This class defines basic flags that work for both gcc and clang compilers
135 """
Junxiao Shie1801312017-07-22 19:16:49 +0000136 def getGeneralFlags(self, conf):
137 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
Davide Pesavento98ff2bc2018-05-24 00:10:31 -0400138 flags['CXXFLAGS'] += ['-std=c++14']
Davide Pesaventoed2f4762019-01-25 00:40:46 -0500139 if Utils.unversioned_sys_platform() == 'linux':
140 flags['LINKFLAGS'] += ['-fuse-ld=gold']
141 elif Utils.unversioned_sys_platform() == 'freebsd':
142 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Junxiao Shie1801312017-07-22 19:16:49 +0000143 return flags
144
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500145 def getDebugFlags(self, conf):
146 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400147 flags['CXXFLAGS'] += ['-Og',
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500148 '-g3',
149 '-pedantic',
150 '-Wall',
151 '-Wextra',
152 '-Werror',
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400153 '-Wcatch-value=2',
154 '-Wextra-semi',
Junxiao Shie1801312017-07-22 19:16:49 +0000155 '-Wnon-virtual-dtor',
Junxiao Shie1801312017-07-22 19:16:49 +0000156 '-Wno-error=deprecated-declarations', # Bug #3795
Davide Pesaventob545aac2017-09-22 23:54:10 -0400157 '-Wno-error=maybe-uninitialized', # Bug #1615
158 '-Wno-unused-parameter',
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500159 ]
Davide Pesaventoed2f4762019-01-25 00:40:46 -0500160 flags['LINKFLAGS'] += ['-Wl,-O1']
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500161 return flags
162
163 def getOptimizedFlags(self, conf):
164 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
165 flags['CXXFLAGS'] += ['-O2',
166 '-g',
167 '-pedantic',
168 '-Wall',
169 '-Wextra',
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400170 '-Wcatch-value=2',
171 '-Wextra-semi',
Junxiao Shie1801312017-07-22 19:16:49 +0000172 '-Wnon-virtual-dtor',
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500173 '-Wno-unused-parameter',
174 ]
Davide Pesaventoed2f4762019-01-25 00:40:46 -0500175 flags['LINKFLAGS'] += ['-Wl,-O1']
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500176 return flags
177
178class GccFlags(GccBasicFlags):
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500179 def getDebugFlags(self, conf):
180 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400181 flags['CXXFLAGS'] += ['-fdiagnostics-color',
182 '-Wredundant-tags',
183 ]
184 if platform.machine() == 'armv7l' and self.getCompilerVersion(conf) >= (7, 1, 0):
185 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500186 return flags
187
188 def getOptimizedFlags(self, conf):
189 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400190 flags['CXXFLAGS'] += ['-fdiagnostics-color',
191 '-Wredundant-tags',
192 ]
193 if platform.machine() == 'armv7l' and self.getCompilerVersion(conf) >= (7, 1, 0):
194 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500195 return flags
196
197class ClangFlags(GccBasicFlags):
198 def getGeneralFlags(self, conf):
199 flags = super(ClangFlags, self).getGeneralFlags(conf)
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400200 if Utils.unversioned_sys_platform() == 'darwin':
Davide Pesavento98ff2bc2018-05-24 00:10:31 -0400201 # Bug #4296
Alexander Afanasyevbb058c02018-02-15 22:49:24 +0000202 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include'], # for Homebrew
203 ['-isystem', '/opt/local/include']] # for MacPorts
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400204 elif Utils.unversioned_sys_platform() == 'freebsd':
205 # Bug #4790
206 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500207 return flags
208
209 def getDebugFlags(self, conf):
210 flags = super(ClangFlags, self).getDebugFlags(conf)
211 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Alexander Afanasyevbb058c02018-02-15 22:49:24 +0000212 '-Wundefined-func-template',
Davide Pesaventob545aac2017-09-22 23:54:10 -0400213 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500214 ]
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400215 if self.getCompilerVersion(conf) < (6, 0, 0):
Davide Pesaventoed2f4762019-01-25 00:40:46 -0500216 flags['CXXFLAGS'] += ['-Wno-missing-braces'] # Bug #4721
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500217 return flags
218
219 def getOptimizedFlags(self, conf):
220 flags = super(ClangFlags, self).getOptimizedFlags(conf)
221 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Alexander Afanasyevbb058c02018-02-15 22:49:24 +0000222 '-Wundefined-func-template',
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500223 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
224 ]
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400225 if self.getCompilerVersion(conf) < (6, 0, 0):
Davide Pesaventoed2f4762019-01-25 00:40:46 -0500226 flags['CXXFLAGS'] += ['-Wno-missing-braces'] # Bug #4721
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500227 return flags