blob: 3ba2dc3ecdc8944ad8195492a9a886768b755fab [file] [log] [blame]
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -05001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
2
Davide Pesavento042dfb32020-07-23 21:07:16 -04003import platform
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -05004from waflib import Configure, Logs, Utils
5
6def options(opt):
7 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesavento042dfb32020-07-23 21:07:16 -04008 help='Compile in debugging mode with minimal optimizations (-Og)')
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -05009
10def configure(conf):
11 conf.start_msg('Checking C++ compiler version')
12
13 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 Pesavento85a73d22022-03-06 16:00:01 -050019 if ccver < (7, 4, 0):
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050020 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesavento85a73d22022-03-06 16:00:01 -050021 'The minimum supported gcc version is 7.4.')
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050022 conf.flags = GccFlags()
23 elif cxx == 'clang':
Davide Pesavento85a73d22022-03-06 16:00:01 -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):
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050032 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesavento85a73d22022-03-06 16:00:01 -050033 'The minimum supported clang version is 6.0.')
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050034 conf.flags = ClangFlags()
35 else:
Davide Pesaventoda1a4d32022-08-19 19:03:24 -040036 warnmsg = f'{cxx} compiler is unsupported'
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050037 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 Pesavento042dfb32020-07-23 21:07:16 -040044 Logs.warn('WARNING: ' + warnmsg)
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050045 else:
46 conf.end_msg(ccverstr)
47
48 conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
49
50 # General flags are always applied (e.g., selecting C++ language standard)
51 generalFlags = conf.flags.getGeneralFlags(conf)
52 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
53 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
54 conf.env.DEFINES += generalFlags['DEFINES']
55
56@Configure.conf
57def check_compiler_flags(conf):
58 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
59 # corresponding environment variables are not set.
60 # DEFINES are always applied.
61 if conf.options.debug:
62 extraFlags = conf.flags.getDebugFlags(conf)
63 if conf.areCustomCxxflagsPresent:
64 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
65 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))
69 else:
70 extraFlags = conf.flags.getOptimizedFlags(conf)
71
72 if not conf.areCustomCxxflagsPresent:
73 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
74 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
75
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 = []
89 for flags in cxxflags:
90 flags = Utils.to_list(flags)
91 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
92 supportedFlags += flags
93
94 self.end_msg(' '.join(supportedFlags))
95 self.env.prepend_value('CXXFLAGS', supportedFlags)
96
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 = []
108 for flags in linkflags:
109 flags = Utils.to_list(flags)
110 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
111 supportedFlags += flags
112
113 self.end_msg(' '.join(supportedFlags))
114 self.env.prepend_value('LINKFLAGS', supportedFlags)
115
116
117class CompilerFlags(object):
118 def getCompilerVersion(self, conf):
119 return tuple(int(i) for i in conf.env.CC_VERSION)
120
121 def getGeneralFlags(self, conf):
122 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
123 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
124
125 def getDebugFlags(self, conf):
126 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
127 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
128
129 def getOptimizedFlags(self, conf):
130 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
131 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
132
133class GccBasicFlags(CompilerFlags):
134 """
135 This class defines basic flags that work for both gcc and clang compilers
136 """
137 def getGeneralFlags(self, conf):
138 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
Davide Pesaventoc407dee2022-07-21 23:56:05 -0400139 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesaventoda278492019-01-29 15:00:49 -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']
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500144 return flags
145
146 def getDebugFlags(self, conf):
147 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Davide Pesavento042dfb32020-07-23 21:07:16 -0400148 flags['CXXFLAGS'] += ['-Og',
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500149 '-g3',
150 '-pedantic',
151 '-Wall',
152 '-Wextra',
153 '-Werror',
Davide Pesavento042dfb32020-07-23 21:07:16 -0400154 '-Wcatch-value=2',
155 '-Wextra-semi',
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500156 '-Wnon-virtual-dtor',
157 '-Wno-error=deprecated-declarations', # Bug #3795
158 '-Wno-error=maybe-uninitialized', # Bug #1615
159 '-Wno-unused-parameter',
160 ]
Davide Pesaventoda278492019-01-29 15:00:49 -0500161 flags['LINKFLAGS'] += ['-Wl,-O1']
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500162 return flags
163
164 def getOptimizedFlags(self, conf):
165 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
166 flags['CXXFLAGS'] += ['-O2',
167 '-g',
168 '-pedantic',
169 '-Wall',
170 '-Wextra',
Davide Pesavento042dfb32020-07-23 21:07:16 -0400171 '-Wcatch-value=2',
172 '-Wextra-semi',
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500173 '-Wnon-virtual-dtor',
174 '-Wno-unused-parameter',
175 ]
Davide Pesaventoda278492019-01-29 15:00:49 -0500176 flags['LINKFLAGS'] += ['-Wl,-O1']
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500177 return flags
178
179class GccFlags(GccBasicFlags):
180 def getDebugFlags(self, conf):
181 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesavento042dfb32020-07-23 21:07:16 -0400182 flags['CXXFLAGS'] += ['-fdiagnostics-color',
183 '-Wredundant-tags',
184 ]
Davide Pesavento85a73d22022-03-06 16:00:01 -0500185 if platform.machine() == 'armv7l':
Davide Pesavento042dfb32020-07-23 21:07:16 -0400186 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500187 return flags
188
189 def getOptimizedFlags(self, conf):
190 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesavento042dfb32020-07-23 21:07:16 -0400191 flags['CXXFLAGS'] += ['-fdiagnostics-color',
192 '-Wredundant-tags',
193 ]
Davide Pesavento85a73d22022-03-06 16:00:01 -0500194 if platform.machine() == 'armv7l':
Davide Pesavento042dfb32020-07-23 21:07:16 -0400195 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500196 return flags
197
198class ClangFlags(GccBasicFlags):
199 def getGeneralFlags(self, conf):
200 flags = super(ClangFlags, self).getGeneralFlags(conf)
Davide Pesavento042dfb32020-07-23 21:07:16 -0400201 if Utils.unversioned_sys_platform() == 'darwin':
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500202 # Bug #4296
Davide Pesaventoda1a4d32022-08-19 19:03:24 -0400203 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
204 flags['CXXFLAGS'] += [['-isystem', f'{brewdir}/include'], # for Homebrew
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500205 ['-isystem', '/opt/local/include']] # for MacPorts
Davide Pesavento042dfb32020-07-23 21:07:16 -0400206 elif Utils.unversioned_sys_platform() == 'freebsd':
207 # Bug #4790
208 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500209 return flags
210
211 def getDebugFlags(self, conf):
212 flags = super(ClangFlags, self).getDebugFlags(conf)
213 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500214 '-Wundefined-func-template',
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500215 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
216 ]
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500217 return flags
218
219 def getOptimizedFlags(self, conf):
220 flags = super(ClangFlags, self).getOptimizedFlags(conf)
221 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500222 '-Wundefined-func-template',
223 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
224 ]
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500225 return flags