blob: 4aa9e9b181fea75401168e897389d8df933e050c [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
Davide Pesavento822c5792023-08-16 15:06:23 -04006
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -05007def options(opt):
8 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesavento042dfb32020-07-23 21:07:16 -04009 help='Compile in debugging mode with minimal optimizations (-Og)')
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050010
Davide Pesavento822c5792023-08-16 15:06:23 -040011
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050012def configure(conf):
13 conf.start_msg('Checking C++ compiler version')
14
15 cxx = conf.env.CXX_NAME # generic name of the compiler
16 ccver = tuple(int(i) for i in conf.env.CC_VERSION)
17 ccverstr = '.'.join(conf.env.CC_VERSION)
18 errmsg = ''
19 warnmsg = ''
20 if cxx == 'gcc':
Davide Pesavento85a73d22022-03-06 16:00:01 -050021 if ccver < (7, 4, 0):
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050022 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesavento88b7bbd2023-04-26 15:48:02 -040023 'The minimum supported gcc version is 9.3.')
24 elif ccver < (9, 3, 0):
25 warnmsg = ('Using a version of gcc older than 9.3 is not '
26 'officially supported and may result in build failures.')
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050027 conf.flags = GccFlags()
28 elif cxx == 'clang':
Davide Pesavento85a73d22022-03-06 16:00:01 -050029 if Utils.unversioned_sys_platform() == 'darwin':
30 if ccver < (10, 0, 0):
31 errmsg = ('The version of Xcode you are using is too old.\n'
Davide Pesavento88b7bbd2023-04-26 15:48:02 -040032 'The minimum supported Xcode version is 12.4.')
33 elif ccver < (12, 0, 0):
34 warnmsg = ('Using a version of Xcode older than 12.4 is not '
Davide Pesavento85a73d22022-03-06 16:00:01 -050035 'officially supported and may result in build failures.')
Davide Pesavento88b7bbd2023-04-26 15:48:02 -040036 elif ccver < (7, 0, 0):
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050037 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesavento88b7bbd2023-04-26 15:48:02 -040038 'The minimum supported clang version is 7.0.')
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050039 conf.flags = ClangFlags()
40 else:
Davide Pesaventoda1a4d32022-08-19 19:03:24 -040041 warnmsg = f'{cxx} compiler is unsupported'
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050042 conf.flags = CompilerFlags()
43
44 if errmsg:
45 conf.end_msg(ccverstr, color='RED')
46 conf.fatal(errmsg)
47 elif warnmsg:
48 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento042dfb32020-07-23 21:07:16 -040049 Logs.warn('WARNING: ' + warnmsg)
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050050 else:
51 conf.end_msg(ccverstr)
52
Davide Pesavento822c5792023-08-16 15:06:23 -040053 conf.areCustomCxxflagsPresent = len(conf.env.CXXFLAGS) > 0
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050054
55 # General flags are always applied (e.g., selecting C++ language standard)
56 generalFlags = conf.flags.getGeneralFlags(conf)
57 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
58 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
59 conf.env.DEFINES += generalFlags['DEFINES']
60
Davide Pesavento822c5792023-08-16 15:06:23 -040061
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050062@Configure.conf
63def check_compiler_flags(conf):
64 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
65 # corresponding environment variables are not set.
66 # DEFINES are always applied.
67 if conf.options.debug:
68 extraFlags = conf.flags.getDebugFlags(conf)
69 if conf.areCustomCxxflagsPresent:
70 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
71 if missingFlags:
72 Logs.warn('Selected debug mode, but CXXFLAGS is set to a custom value "%s"'
73 % ' '.join(conf.env.CXXFLAGS))
74 Logs.warn('Default flags "%s" will not be used' % ' '.join(missingFlags))
75 else:
76 extraFlags = conf.flags.getOptimizedFlags(conf)
77
78 if not conf.areCustomCxxflagsPresent:
79 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
80 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
81
82 conf.env.DEFINES += extraFlags['DEFINES']
83
Davide Pesavento822c5792023-08-16 15:06:23 -040084
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050085@Configure.conf
86def add_supported_cxxflags(self, cxxflags):
87 """
Davide Pesavento822c5792023-08-16 15:06:23 -040088 Check which cxxflags are supported by the active compiler and add them to env.CXXFLAGS variable.
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050089 """
90 if len(cxxflags) == 0:
91 return
92
93 self.start_msg('Checking supported CXXFLAGS')
94
95 supportedFlags = []
96 for flags in cxxflags:
97 flags = Utils.to_list(flags)
98 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
99 supportedFlags += flags
100
101 self.end_msg(' '.join(supportedFlags))
102 self.env.prepend_value('CXXFLAGS', supportedFlags)
103
Davide Pesavento822c5792023-08-16 15:06:23 -0400104
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500105@Configure.conf
106def add_supported_linkflags(self, linkflags):
107 """
Davide Pesavento822c5792023-08-16 15:06:23 -0400108 Check which linkflags are supported by the active compiler and add them to env.LINKFLAGS variable.
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500109 """
110 if len(linkflags) == 0:
111 return
112
113 self.start_msg('Checking supported LINKFLAGS')
114
115 supportedFlags = []
116 for flags in linkflags:
117 flags = Utils.to_list(flags)
118 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
119 supportedFlags += flags
120
121 self.end_msg(' '.join(supportedFlags))
122 self.env.prepend_value('LINKFLAGS', supportedFlags)
123
124
Davide Pesavento822c5792023-08-16 15:06:23 -0400125class CompilerFlags:
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500126 def getCompilerVersion(self, conf):
127 return tuple(int(i) for i in conf.env.CC_VERSION)
128
129 def getGeneralFlags(self, conf):
130 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
131 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
132
133 def getDebugFlags(self, conf):
134 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
135 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
136
137 def getOptimizedFlags(self, conf):
138 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
139 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
140
Davide Pesavento822c5792023-08-16 15:06:23 -0400141
142class GccClangCommonFlags(CompilerFlags):
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500143 """
Davide Pesavento822c5792023-08-16 15:06:23 -0400144 This class defines common flags that work for both gcc and clang compilers.
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500145 """
Davide Pesavento822c5792023-08-16 15:06:23 -0400146
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500147 def getGeneralFlags(self, conf):
Davide Pesavento822c5792023-08-16 15:06:23 -0400148 flags = super().getGeneralFlags(conf)
Davide Pesaventoc407dee2022-07-21 23:56:05 -0400149 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesaventob68f2842022-11-17 19:07:04 -0500150 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesaventoda278492019-01-29 15:00:49 -0500151 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500152 return flags
153
Davide Pesavento822c5792023-08-16 15:06:23 -0400154 __cxxFlags = [
155 '-fdiagnostics-color',
156 '-Wall',
157 '-Wextra',
158 '-Wpedantic',
159 '-Wenum-conversion',
160 '-Wextra-semi',
161 '-Wnon-virtual-dtor',
162 '-Wno-unused-parameter',
163 ]
164 __linkFlags = ['-Wl,-O1']
165
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500166 def getDebugFlags(self, conf):
Davide Pesavento822c5792023-08-16 15:06:23 -0400167 flags = super().getDebugFlags(conf)
168 flags['CXXFLAGS'] += ['-Og', '-g'] + self.__cxxFlags + [
169 '-Werror',
170 '-Wno-error=deprecated-declarations', # Bug #3795
171 '-Wno-error=maybe-uninitialized', # Bug #1615
172 ]
173 flags['LINKFLAGS'] += self.__linkFlags
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500174 return flags
175
176 def getOptimizedFlags(self, conf):
Davide Pesavento822c5792023-08-16 15:06:23 -0400177 flags = super().getOptimizedFlags(conf)
178 flags['CXXFLAGS'] += ['-O2', '-g1'] + self.__cxxFlags
179 flags['LINKFLAGS'] += self.__linkFlags
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500180 return flags
181
Davide Pesavento822c5792023-08-16 15:06:23 -0400182
183class GccFlags(GccClangCommonFlags):
184 __cxxFlags = [
185 '-Wcatch-value=2',
186 '-Wcomma-subscript', # enabled by default in C++20
187 '-Wduplicated-branches',
188 '-Wduplicated-cond',
189 '-Wlogical-op',
190 '-Wredundant-tags',
191 '-Wvolatile', # enabled by default in C++20
192 ]
193
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500194 def getDebugFlags(self, conf):
Davide Pesavento822c5792023-08-16 15:06:23 -0400195 flags = super().getDebugFlags(conf)
196 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento85a73d22022-03-06 16:00:01 -0500197 if platform.machine() == 'armv7l':
Davide Pesavento042dfb32020-07-23 21:07:16 -0400198 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500199 return flags
200
201 def getOptimizedFlags(self, conf):
Davide Pesavento822c5792023-08-16 15:06:23 -0400202 flags = super().getOptimizedFlags(conf)
203 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento85a73d22022-03-06 16:00:01 -0500204 if platform.machine() == 'armv7l':
Davide Pesavento042dfb32020-07-23 21:07:16 -0400205 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500206 return flags
207
Davide Pesavento822c5792023-08-16 15:06:23 -0400208
209class ClangFlags(GccClangCommonFlags):
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500210 def getGeneralFlags(self, conf):
Davide Pesavento822c5792023-08-16 15:06:23 -0400211 flags = super().getGeneralFlags(conf)
Davide Pesavento042dfb32020-07-23 21:07:16 -0400212 if Utils.unversioned_sys_platform() == 'darwin':
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500213 # Bug #4296
Davide Pesaventoda1a4d32022-08-19 19:03:24 -0400214 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
Davide Pesavento822c5792023-08-16 15:06:23 -0400215 flags['CXXFLAGS'] += [
216 ['-isystem', f'{brewdir}/include'], # for Homebrew
217 ['-isystem', '/opt/local/include'], # for MacPorts
218 ]
Davide Pesavento042dfb32020-07-23 21:07:16 -0400219 elif Utils.unversioned_sys_platform() == 'freebsd':
220 # Bug #4790
221 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500222 return flags
223
Davide Pesavento822c5792023-08-16 15:06:23 -0400224 __cxxFlags = [
225 '-Wundefined-func-template',
226 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
227 ]
228
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500229 def getDebugFlags(self, conf):
Davide Pesavento822c5792023-08-16 15:06:23 -0400230 flags = super().getDebugFlags(conf)
231 flags['CXXFLAGS'] += self.__cxxFlags
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500232 return flags
233
234 def getOptimizedFlags(self, conf):
Davide Pesavento822c5792023-08-16 15:06:23 -0400235 flags = super().getOptimizedFlags(conf)
236 flags['CXXFLAGS'] += self.__cxxFlags
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500237 return flags