blob: 53f0f8c51b4a228f6232cb800e43e28f3804480a [file] [log] [blame]
Junxiao Shif7191242015-03-19 05:53:41 -07001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
2
Davide Pesaventob07d7a92020-05-13 23:30:07 -04003import platform
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -05004from waflib import Configure, Logs, Utils
Junxiao Shif7191242015-03-19 05:53:41 -07005
6def options(opt):
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -05007 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesaventob07d7a92020-05-13 23:30:07 -04008 help='Compile in debugging mode with minimal optimizations (-Og)')
Junxiao Shif7191242015-03-19 05:53:41 -07009
10def configure(conf):
Davide Pesaventoc0702702017-08-24 22:04:00 -040011 conf.start_msg('Checking C++ compiler version')
12
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -050013 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 Pesaventoc0702702017-08-24 22:04:00 -040016 errmsg = ''
17 warnmsg = ''
Junxiao Shif7191242015-03-19 05:53:41 -070018 if cxx == 'gcc':
Davide Pesaventob04c52b2022-02-20 15:20:02 -050019 if ccver < (7, 4, 0):
Davide Pesaventoc0702702017-08-24 22:04:00 -040020 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesaventob04c52b2022-02-20 15:20:02 -050021 'The minimum supported gcc version is 7.4.')
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040022 conf.flags = GccFlags()
Junxiao Shif7191242015-03-19 05:53:41 -070023 elif cxx == 'clang':
Davide Pesaventob04c52b2022-02-20 15:20:02 -050024 if Utils.unversioned_sys_platform() == 'darwin':
25 if ccver < (10, 0, 0):
26 errmsg = ('The version of Xcode you are using is too old.\n'
27 'The minimum supported Xcode version is 11.3.')
28 elif ccver < (11, 0, 0):
29 warnmsg = ('Using a version of Xcode older than 11.3 is not '
30 'officially supported and may result in build failures.')
31 elif ccver < (5, 0, 0):
Davide Pesaventoc0702702017-08-24 22:04:00 -040032 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesaventob04c52b2022-02-20 15:20:02 -050033 'The minimum supported clang version is 5.0.')
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040034 conf.flags = ClangFlags()
Junxiao Shif7191242015-03-19 05:53:41 -070035 else:
Davide Pesaventob07d7a92020-05-13 23:30:07 -040036 warnmsg = '%s compiler is unsupported' % cxx
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040037 conf.flags = CompilerFlags()
Davide Pesaventoc0702702017-08-24 22:04:00 -040038
39 if errmsg:
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -050040 conf.end_msg(ccverstr, color='RED')
Davide Pesaventoc0702702017-08-24 22:04:00 -040041 conf.fatal(errmsg)
42 elif warnmsg:
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -050043 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesaventob07d7a92020-05-13 23:30:07 -040044 Logs.warn('WARNING: ' + warnmsg)
Davide Pesaventoc0702702017-08-24 22:04:00 -040045 else:
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -050046 conf.end_msg(ccverstr)
Junxiao Shif7191242015-03-19 05:53:41 -070047
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040048 conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
Junxiao Shif7191242015-03-19 05:53:41 -070049
Davide Pesavento60f8cc12018-05-10 22:05:21 -040050 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040051 generalFlags = conf.flags.getGeneralFlags(conf)
Junxiao Shif7191242015-03-19 05:53:41 -070052 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
53 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
54 conf.env.DEFINES += generalFlags['DEFINES']
55
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040056@Configure.conf
57def check_compiler_flags(conf):
Junxiao Shic8a0a252015-10-05 07:25:02 -070058 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Junxiao Shif7191242015-03-19 05:53:41 -070059 # corresponding environment variables are not set.
Junxiao Shic8a0a252015-10-05 07:25:02 -070060 # DEFINES are always applied.
Junxiao Shif7191242015-03-19 05:53:41 -070061 if conf.options.debug:
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040062 extraFlags = conf.flags.getDebugFlags(conf)
63 if conf.areCustomCxxflagsPresent:
Junxiao Shif7191242015-03-19 05:53:41 -070064 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -050065 if missingFlags:
66 Logs.warn('Selected debug mode, but CXXFLAGS is set to a custom value "%s"'
67 % ' '.join(conf.env.CXXFLAGS))
68 Logs.warn('Default flags "%s" will not be used' % ' '.join(missingFlags))
Junxiao Shif7191242015-03-19 05:53:41 -070069 else:
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040070 extraFlags = conf.flags.getOptimizedFlags(conf)
Junxiao Shif7191242015-03-19 05:53:41 -070071
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040072 if not conf.areCustomCxxflagsPresent:
Junxiao Shif7191242015-03-19 05:53:41 -070073 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Davide Pesavento3e79c9c2015-11-01 20:38:28 +010074 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Junxiao Shif7191242015-03-19 05:53:41 -070075
76 conf.env.DEFINES += extraFlags['DEFINES']
77
78@Configure.conf
79def add_supported_cxxflags(self, cxxflags):
80 """
81 Check which cxxflags are supported by compiler and add them to env.CXXFLAGS variable
82 """
83 if len(cxxflags) == 0:
84 return
85
86 self.start_msg('Checking supported CXXFLAGS')
87
88 supportedFlags = []
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040089 for flags in cxxflags:
90 flags = Utils.to_list(flags)
91 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
92 supportedFlags += flags
Junxiao Shif7191242015-03-19 05:53:41 -070093
94 self.end_msg(' '.join(supportedFlags))
Davide Pesavento89d91752016-08-14 11:34:09 +020095 self.env.prepend_value('CXXFLAGS', supportedFlags)
Junxiao Shif7191242015-03-19 05:53:41 -070096
97@Configure.conf
98def add_supported_linkflags(self, linkflags):
99 """
100 Check which linkflags are supported by compiler and add them to env.LINKFLAGS variable
101 """
102 if len(linkflags) == 0:
103 return
104
105 self.start_msg('Checking supported LINKFLAGS')
106
107 supportedFlags = []
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -0400108 for flags in linkflags:
109 flags = Utils.to_list(flags)
110 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
111 supportedFlags += flags
Junxiao Shif7191242015-03-19 05:53:41 -0700112
113 self.end_msg(' '.join(supportedFlags))
Davide Pesavento89d91752016-08-14 11:34:09 +0200114 self.env.prepend_value('LINKFLAGS', supportedFlags)
Junxiao Shif7191242015-03-19 05:53:41 -0700115
116
117class CompilerFlags(object):
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -0500118 def getCompilerVersion(self, conf):
119 return tuple(int(i) for i in conf.env.CC_VERSION)
120
Junxiao Shif7191242015-03-19 05:53:41 -0700121 def getGeneralFlags(self, conf):
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100122 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Junxiao Shif7191242015-03-19 05:53:41 -0700123 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
124
125 def getDebugFlags(self, conf):
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100126 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Junxiao Shif7191242015-03-19 05:53:41 -0700127 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
128
129 def getOptimizedFlags(self, conf):
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100130 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Junxiao Shif7191242015-03-19 05:53:41 -0700131 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
132
133class GccBasicFlags(CompilerFlags):
Junxiao Shic8a0a252015-10-05 07:25:02 -0700134 """
135 This class defines basic flags that work for both gcc and clang compilers
136 """
Davide Pesaventodd0e1952017-06-24 13:37:13 -0400137 def getGeneralFlags(self, conf):
138 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
Davide Pesaventob3570c62022-02-19 19:19:00 -0500139 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesaventoe1b18a62018-12-19 01:17:42 -0500140 if Utils.unversioned_sys_platform() == 'linux':
141 flags['LINKFLAGS'] += ['-fuse-ld=gold']
142 elif Utils.unversioned_sys_platform() == 'freebsd':
143 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Davide Pesaventodd0e1952017-06-24 13:37:13 -0400144 return flags
145
Junxiao Shif7191242015-03-19 05:53:41 -0700146 def getDebugFlags(self, conf):
147 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400148 flags['CXXFLAGS'] += ['-Og',
Junxiao Shif7191242015-03-19 05:53:41 -0700149 '-g3',
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100150 '-pedantic',
151 '-Wall',
152 '-Wextra',
Junxiao Shif7191242015-03-19 05:53:41 -0700153 '-Werror',
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400154 '-Wcatch-value=2',
155 '-Wextra-semi',
Davide Pesavento887336c2017-09-19 15:25:23 -0400156 '-Wnon-virtual-dtor',
Alexander Afanasyevfc4aa0d2016-10-15 00:57:33 +0000157 '-Wno-error=deprecated-declarations', # Bug #3795
Davide Pesaventoc0702702017-08-24 22:04:00 -0400158 '-Wno-error=maybe-uninitialized', # Bug #1615
159 '-Wno-unused-parameter',
Junxiao Shic8a0a252015-10-05 07:25:02 -0700160 ]
Davide Pesaventoe1b18a62018-12-19 01:17:42 -0500161 flags['LINKFLAGS'] += ['-Wl,-O1']
Junxiao Shif7191242015-03-19 05:53:41 -0700162 return flags
163
164 def getOptimizedFlags(self, conf):
165 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100166 flags['CXXFLAGS'] += ['-O2',
Junxiao Shic8a0a252015-10-05 07:25:02 -0700167 '-g',
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100168 '-pedantic',
169 '-Wall',
170 '-Wextra',
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400171 '-Wcatch-value=2',
172 '-Wextra-semi',
Davide Pesavento887336c2017-09-19 15:25:23 -0400173 '-Wnon-virtual-dtor',
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100174 '-Wno-unused-parameter',
Junxiao Shic8a0a252015-10-05 07:25:02 -0700175 ]
Davide Pesaventoe1b18a62018-12-19 01:17:42 -0500176 flags['LINKFLAGS'] += ['-Wl,-O1']
Junxiao Shif7191242015-03-19 05:53:41 -0700177 return flags
178
179class GccFlags(GccBasicFlags):
Junxiao Shif7191242015-03-19 05:53:41 -0700180 def getDebugFlags(self, conf):
181 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400182 flags['CXXFLAGS'] += ['-fdiagnostics-color',
183 '-Wredundant-tags',
184 ]
Davide Pesaventob04c52b2022-02-20 15:20:02 -0500185 if platform.machine() == 'armv7l':
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400186 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Junxiao Shic8a0a252015-10-05 07:25:02 -0700187 return flags
188
189 def getOptimizedFlags(self, conf):
190 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400191 flags['CXXFLAGS'] += ['-fdiagnostics-color',
192 '-Wredundant-tags',
193 ]
Davide Pesaventob04c52b2022-02-20 15:20:02 -0500194 if platform.machine() == 'armv7l':
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400195 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Junxiao Shif7191242015-03-19 05:53:41 -0700196 return flags
197
198class ClangFlags(GccBasicFlags):
199 def getGeneralFlags(self, conf):
200 flags = super(ClangFlags, self).getGeneralFlags(conf)
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400201 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -0500202 # Bug #4296
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -0400203 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include'], # for Homebrew
204 ['-isystem', '/opt/local/include']] # for MacPorts
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400205 elif Utils.unversioned_sys_platform() == 'freebsd':
206 # Bug #4790
207 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Junxiao Shif7191242015-03-19 05:53:41 -0700208 return flags
209
210 def getDebugFlags(self, conf):
211 flags = super(ClangFlags, self).getDebugFlags(conf)
Davide Pesaventoc0702702017-08-24 22:04:00 -0400212 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesaventoc0702702017-08-24 22:04:00 -0400213 '-Wundefined-func-template',
Davide Pesaventoc0702702017-08-24 22:04:00 -0400214 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
215 ]
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400216 if self.getCompilerVersion(conf) < (6, 0, 0):
Davide Pesavento88beb4d2018-09-11 18:14:18 -0400217 flags['CXXFLAGS'] += ['-Wno-missing-braces'] # Bug #4721
Junxiao Shic8a0a252015-10-05 07:25:02 -0700218 return flags
219
220 def getOptimizedFlags(self, conf):
221 flags = super(ClangFlags, self).getOptimizedFlags(conf)
222 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesaventoc0702702017-08-24 22:04:00 -0400223 '-Wundefined-func-template',
Junxiao Shic8a0a252015-10-05 07:25:02 -0700224 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
225 ]
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400226 if self.getCompilerVersion(conf) < (6, 0, 0):
Davide Pesavento88beb4d2018-09-11 18:14:18 -0400227 flags['CXXFLAGS'] += ['-Wno-missing-braces'] # Bug #4721
Junxiao Shif7191242015-03-19 05:53:41 -0700228 return flags