blob: 3a7bf660c3489257f96e45df7491fb481e9156de [file] [log] [blame]
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -07001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -07002
Davide Pesavento3d01fa32021-10-03 17:13:38 -04003import platform
Alexander Afanasyev08d18742018-03-15 16:31:28 -04004from waflib import Configure, Logs, Utils
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -07005
6def options(opt):
Alexander Afanasyev08d18742018-03-15 16:31:28 -04007 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesavento3d01fa32021-10-03 17:13:38 -04008 help='Compile in debugging mode with minimal optimizations (-Og)')
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -07009
10def configure(conf):
Alexander Afanasyev08d18742018-03-15 16:31:28 -040011 conf.start_msg('Checking C++ compiler version')
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070012
Alexander Afanasyev08d18742018-03-15 16:31:28 -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)
16 errmsg = ''
17 warnmsg = ''
18 if cxx == 'gcc':
Davide Pesavento7f27ec12022-03-10 20:10:54 -050019 if ccver < (7, 4, 0):
Alexander Afanasyev08d18742018-03-15 16:31:28 -040020 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesavento7f27ec12022-03-10 20:10:54 -050021 'The minimum supported gcc version is 7.4.')
Alexander Afanasyev08d18742018-03-15 16:31:28 -040022 conf.flags = GccFlags()
23 elif cxx == 'clang':
Davide Pesavento7f27ec12022-03-10 20:10:54 -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 < (6, 0, 0):
Alexander Afanasyev08d18742018-03-15 16:31:28 -040032 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesavento7f27ec12022-03-10 20:10:54 -050033 'The minimum supported clang version is 6.0.')
Alexander Afanasyev08d18742018-03-15 16:31:28 -040034 conf.flags = ClangFlags()
35 else:
Davide Pesavento423553e2022-08-19 20:46:06 -040036 warnmsg = f'{cxx} compiler is unsupported'
Alexander Afanasyev08d18742018-03-15 16:31:28 -040037 conf.flags = CompilerFlags()
38
39 if errmsg:
40 conf.end_msg(ccverstr, color='RED')
41 conf.fatal(errmsg)
42 elif warnmsg:
43 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento3d01fa32021-10-03 17:13:38 -040044 Logs.warn('WARNING: ' + warnmsg)
Alexander Afanasyev08d18742018-03-15 16:31:28 -040045 else:
46 conf.end_msg(ccverstr)
47
48 conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080049
Davide Pesavento90034832018-05-30 10:10:31 -040050 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyev08d18742018-03-15 16:31:28 -040051 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080052 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
53 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
54 conf.env.DEFINES += generalFlags['DEFINES']
55
Alexander Afanasyev08d18742018-03-15 16:31:28 -040056@Configure.conf
57def check_compiler_flags(conf):
Alexander Afanasyevc7c99002015-10-09 17:27:30 -070058 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080059 # corresponding environment variables are not set.
Alexander Afanasyevc7c99002015-10-09 17:27:30 -070060 # DEFINES are always applied.
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070061 if conf.options.debug:
Alexander Afanasyev08d18742018-03-15 16:31:28 -040062 extraFlags = conf.flags.getDebugFlags(conf)
63 if conf.areCustomCxxflagsPresent:
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080064 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev08d18742018-03-15 16:31:28 -040065 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))
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070069 else:
Alexander Afanasyev08d18742018-03-15 16:31:28 -040070 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070071
Alexander Afanasyev08d18742018-03-15 16:31:28 -040072 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080073 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -080074 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080075
76 conf.env.DEFINES += extraFlags['DEFINES']
Shock Jiang2d8483c2014-10-30 10:15:37 -070077
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070078@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 """
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080083 if len(cxxflags) == 0:
84 return
85
Shock Jiang2d8483c2014-10-30 10:15:37 -070086 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070087
88 supportedFlags = []
Alexander Afanasyev08d18742018-03-15 16:31:28 -040089 for flags in cxxflags:
90 flags = Utils.to_list(flags)
91 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
92 supportedFlags += flags
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070093
94 self.end_msg(' '.join(supportedFlags))
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -080095 self.env.prepend_value('CXXFLAGS', supportedFlags)
Shock Jiang2d8483c2014-10-30 10:15:37 -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 """
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800102 if len(linkflags) == 0:
103 return
104
Shock Jiang2d8483c2014-10-30 10:15:37 -0700105 self.start_msg('Checking supported LINKFLAGS')
106
107 supportedFlags = []
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400108 for flags in linkflags:
109 flags = Utils.to_list(flags)
110 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
111 supportedFlags += flags
Shock Jiang2d8483c2014-10-30 10:15:37 -0700112
113 self.end_msg(' '.join(supportedFlags))
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -0800114 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800115
116
117class CompilerFlags(object):
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400118 def getCompilerVersion(self, conf):
119 return tuple(int(i) for i in conf.env.CC_VERSION)
120
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800121 def getGeneralFlags(self, conf):
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -0800122 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800123 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
124
125 def getDebugFlags(self, conf):
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -0800126 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800127 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
128
129 def getOptimizedFlags(self, conf):
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -0800130 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800131 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
132
133class GccBasicFlags(CompilerFlags):
Alexander Afanasyevc7c99002015-10-09 17:27:30 -0700134 """
135 This class defines basic flags that work for both gcc and clang compilers
136 """
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400137 def getGeneralFlags(self, conf):
138 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
Davide Pesavento38fd3982022-04-18 22:22:02 -0400139 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesaventob2034db2022-11-16 17:21:22 -0500140 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesaventoc7888e62019-01-29 15:20:32 -0500141 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400142 return flags
143
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800144 def getDebugFlags(self, conf):
145 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Davide Pesavento3d01fa32021-10-03 17:13:38 -0400146 flags['CXXFLAGS'] += ['-Og',
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800147 '-g3',
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -0800148 '-Wall',
149 '-Wextra',
Davide Pesaventodadbc3e2023-02-14 02:32:36 -0500150 '-Wpedantic',
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800151 '-Werror',
Davide Pesavento3d01fa32021-10-03 17:13:38 -0400152 '-Wcatch-value=2',
153 '-Wextra-semi',
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400154 '-Wnon-virtual-dtor',
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -0800155 '-Wno-error=deprecated-declarations', # Bug #3795
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400156 '-Wno-error=maybe-uninitialized', # Bug #1615
157 '-Wno-unused-parameter',
Alexander Afanasyevc7c99002015-10-09 17:27:30 -0700158 ]
Davide Pesaventoc7888e62019-01-29 15:20:32 -0500159 flags['LINKFLAGS'] += ['-Wl,-O1']
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800160 return flags
161
162 def getOptimizedFlags(self, conf):
163 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -0800164 flags['CXXFLAGS'] += ['-O2',
Alexander Afanasyevc7c99002015-10-09 17:27:30 -0700165 '-g',
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -0800166 '-Wall',
167 '-Wextra',
Davide Pesaventodadbc3e2023-02-14 02:32:36 -0500168 '-Wpedantic',
Davide Pesavento3d01fa32021-10-03 17:13:38 -0400169 '-Wcatch-value=2',
170 '-Wextra-semi',
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400171 '-Wnon-virtual-dtor',
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -0800172 '-Wno-unused-parameter',
Alexander Afanasyevc7c99002015-10-09 17:27:30 -0700173 ]
Davide Pesaventoc7888e62019-01-29 15:20:32 -0500174 flags['LINKFLAGS'] += ['-Wl,-O1']
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800175 return flags
176
177class GccFlags(GccBasicFlags):
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800178 def getDebugFlags(self, conf):
179 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesavento3d01fa32021-10-03 17:13:38 -0400180 flags['CXXFLAGS'] += ['-fdiagnostics-color',
181 '-Wredundant-tags',
182 ]
Davide Pesavento7f27ec12022-03-10 20:10:54 -0500183 if platform.machine() == 'armv7l':
Davide Pesavento3d01fa32021-10-03 17:13:38 -0400184 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevc7c99002015-10-09 17:27:30 -0700185 return flags
186
187 def getOptimizedFlags(self, conf):
188 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesavento3d01fa32021-10-03 17:13:38 -0400189 flags['CXXFLAGS'] += ['-fdiagnostics-color',
190 '-Wredundant-tags',
191 ]
Davide Pesavento7f27ec12022-03-10 20:10:54 -0500192 if platform.machine() == 'armv7l':
Davide Pesavento3d01fa32021-10-03 17:13:38 -0400193 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800194 return flags
195
196class ClangFlags(GccBasicFlags):
197 def getGeneralFlags(self, conf):
198 flags = super(ClangFlags, self).getGeneralFlags(conf)
Davide Pesavento3d01fa32021-10-03 17:13:38 -0400199 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400200 # Bug #4296
Davide Pesavento423553e2022-08-19 20:46:06 -0400201 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
202 flags['CXXFLAGS'] += [['-isystem', f'{brewdir}/include'], # for Homebrew
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400203 ['-isystem', '/opt/local/include']] # for MacPorts
Davide Pesavento3d01fa32021-10-03 17:13:38 -0400204 elif Utils.unversioned_sys_platform() == 'freebsd':
205 # Bug #4790
206 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800207 return flags
208
209 def getDebugFlags(self, conf):
210 flags = super(ClangFlags, self).getDebugFlags(conf)
Alexander Afanasyevc7c99002015-10-09 17:27:30 -0700211 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400212 '-Wundefined-func-template',
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400213 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Alexander Afanasyevc7c99002015-10-09 17:27:30 -0700214 ]
215 return flags
216
217 def getOptimizedFlags(self, conf):
218 flags = super(ClangFlags, self).getOptimizedFlags(conf)
219 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400220 '-Wundefined-func-template',
Alexander Afanasyevc7c99002015-10-09 17:27:30 -0700221 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
222 ]
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800223 return flags