blob: 8c718717e3da2f62156c901c59a903bf6b4d7160 [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 Pesavento88b7bbd2023-04-26 15:48:02 -040021 'The minimum supported gcc version is 9.3.')
22 elif ccver < (9, 3, 0):
23 warnmsg = ('Using a version of gcc older than 9.3 is not '
24 'officially supported and may result in build failures.')
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050025 conf.flags = GccFlags()
26 elif cxx == 'clang':
Davide Pesavento85a73d22022-03-06 16:00:01 -050027 if Utils.unversioned_sys_platform() == 'darwin':
28 if ccver < (10, 0, 0):
29 errmsg = ('The version of Xcode you are using is too old.\n'
Davide Pesavento88b7bbd2023-04-26 15:48:02 -040030 'The minimum supported Xcode version is 12.4.')
31 elif ccver < (12, 0, 0):
32 warnmsg = ('Using a version of Xcode older than 12.4 is not '
Davide Pesavento85a73d22022-03-06 16:00:01 -050033 'officially supported and may result in build failures.')
Davide Pesavento88b7bbd2023-04-26 15:48:02 -040034 elif ccver < (7, 0, 0):
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050035 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesavento88b7bbd2023-04-26 15:48:02 -040036 'The minimum supported clang version is 7.0.')
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050037 conf.flags = ClangFlags()
38 else:
Davide Pesaventoda1a4d32022-08-19 19:03:24 -040039 warnmsg = f'{cxx} compiler is unsupported'
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050040 conf.flags = CompilerFlags()
41
42 if errmsg:
43 conf.end_msg(ccverstr, color='RED')
44 conf.fatal(errmsg)
45 elif warnmsg:
46 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento042dfb32020-07-23 21:07:16 -040047 Logs.warn('WARNING: ' + warnmsg)
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050048 else:
49 conf.end_msg(ccverstr)
50
51 conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
52
53 # General flags are always applied (e.g., selecting C++ language standard)
54 generalFlags = conf.flags.getGeneralFlags(conf)
55 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
56 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
57 conf.env.DEFINES += generalFlags['DEFINES']
58
59@Configure.conf
60def check_compiler_flags(conf):
61 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
62 # corresponding environment variables are not set.
63 # DEFINES are always applied.
64 if conf.options.debug:
65 extraFlags = conf.flags.getDebugFlags(conf)
66 if conf.areCustomCxxflagsPresent:
67 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
68 if missingFlags:
69 Logs.warn('Selected debug mode, but CXXFLAGS is set to a custom value "%s"'
70 % ' '.join(conf.env.CXXFLAGS))
71 Logs.warn('Default flags "%s" will not be used' % ' '.join(missingFlags))
72 else:
73 extraFlags = conf.flags.getOptimizedFlags(conf)
74
75 if not conf.areCustomCxxflagsPresent:
76 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
77 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
78
79 conf.env.DEFINES += extraFlags['DEFINES']
80
81@Configure.conf
82def add_supported_cxxflags(self, cxxflags):
83 """
84 Check which cxxflags are supported by compiler and add them to env.CXXFLAGS variable
85 """
86 if len(cxxflags) == 0:
87 return
88
89 self.start_msg('Checking supported CXXFLAGS')
90
91 supportedFlags = []
92 for flags in cxxflags:
93 flags = Utils.to_list(flags)
94 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
95 supportedFlags += flags
96
97 self.end_msg(' '.join(supportedFlags))
98 self.env.prepend_value('CXXFLAGS', supportedFlags)
99
100@Configure.conf
101def add_supported_linkflags(self, linkflags):
102 """
103 Check which linkflags are supported by compiler and add them to env.LINKFLAGS variable
104 """
105 if len(linkflags) == 0:
106 return
107
108 self.start_msg('Checking supported LINKFLAGS')
109
110 supportedFlags = []
111 for flags in linkflags:
112 flags = Utils.to_list(flags)
113 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
114 supportedFlags += flags
115
116 self.end_msg(' '.join(supportedFlags))
117 self.env.prepend_value('LINKFLAGS', supportedFlags)
118
119
120class CompilerFlags(object):
121 def getCompilerVersion(self, conf):
122 return tuple(int(i) for i in conf.env.CC_VERSION)
123
124 def getGeneralFlags(self, conf):
125 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
126 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
127
128 def getDebugFlags(self, conf):
129 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
130 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
131
132 def getOptimizedFlags(self, conf):
133 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
134 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
135
136class GccBasicFlags(CompilerFlags):
137 """
138 This class defines basic flags that work for both gcc and clang compilers
139 """
140 def getGeneralFlags(self, conf):
141 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
Davide Pesaventoc407dee2022-07-21 23:56:05 -0400142 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesaventob68f2842022-11-17 19:07:04 -0500143 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesaventoda278492019-01-29 15:00:49 -0500144 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500145 return flags
146
147 def getDebugFlags(self, conf):
148 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Davide Pesavento042dfb32020-07-23 21:07:16 -0400149 flags['CXXFLAGS'] += ['-Og',
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500150 '-g3',
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500151 '-Wall',
152 '-Wextra',
Davide Pesaventoec8f8542023-01-18 23:10:24 -0500153 '-Wpedantic',
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500154 '-Werror',
Davide Pesavento042dfb32020-07-23 21:07:16 -0400155 '-Wcatch-value=2',
156 '-Wextra-semi',
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500157 '-Wnon-virtual-dtor',
158 '-Wno-error=deprecated-declarations', # Bug #3795
159 '-Wno-error=maybe-uninitialized', # Bug #1615
160 '-Wno-unused-parameter',
161 ]
Davide Pesaventoda278492019-01-29 15:00:49 -0500162 flags['LINKFLAGS'] += ['-Wl,-O1']
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500163 return flags
164
165 def getOptimizedFlags(self, conf):
166 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
167 flags['CXXFLAGS'] += ['-O2',
168 '-g',
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500169 '-Wall',
170 '-Wextra',
Davide Pesaventoec8f8542023-01-18 23:10:24 -0500171 '-Wpedantic',
Davide Pesavento042dfb32020-07-23 21:07:16 -0400172 '-Wcatch-value=2',
173 '-Wextra-semi',
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500174 '-Wnon-virtual-dtor',
175 '-Wno-unused-parameter',
176 ]
Davide Pesaventoda278492019-01-29 15:00:49 -0500177 flags['LINKFLAGS'] += ['-Wl,-O1']
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500178 return flags
179
180class GccFlags(GccBasicFlags):
181 def getDebugFlags(self, conf):
182 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesavento042dfb32020-07-23 21:07:16 -0400183 flags['CXXFLAGS'] += ['-fdiagnostics-color',
184 '-Wredundant-tags',
185 ]
Davide Pesavento85a73d22022-03-06 16:00:01 -0500186 if platform.machine() == 'armv7l':
Davide Pesavento042dfb32020-07-23 21:07:16 -0400187 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500188 return flags
189
190 def getOptimizedFlags(self, conf):
191 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesavento042dfb32020-07-23 21:07:16 -0400192 flags['CXXFLAGS'] += ['-fdiagnostics-color',
193 '-Wredundant-tags',
194 ]
Davide Pesavento85a73d22022-03-06 16:00:01 -0500195 if platform.machine() == 'armv7l':
Davide Pesavento042dfb32020-07-23 21:07:16 -0400196 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500197 return flags
198
199class ClangFlags(GccBasicFlags):
200 def getGeneralFlags(self, conf):
201 flags = super(ClangFlags, self).getGeneralFlags(conf)
Davide Pesavento042dfb32020-07-23 21:07:16 -0400202 if Utils.unversioned_sys_platform() == 'darwin':
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500203 # Bug #4296
Davide Pesaventoda1a4d32022-08-19 19:03:24 -0400204 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
205 flags['CXXFLAGS'] += [['-isystem', f'{brewdir}/include'], # for Homebrew
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500206 ['-isystem', '/opt/local/include']] # for MacPorts
Davide Pesavento042dfb32020-07-23 21:07:16 -0400207 elif Utils.unversioned_sys_platform() == 'freebsd':
208 # Bug #4790
209 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500210 return flags
211
212 def getDebugFlags(self, conf):
213 flags = super(ClangFlags, self).getDebugFlags(conf)
214 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500215 '-Wundefined-func-template',
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500216 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
217 ]
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500218 return flags
219
220 def getOptimizedFlags(self, conf):
221 flags = super(ClangFlags, self).getOptimizedFlags(conf)
222 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500223 '-Wundefined-func-template',
224 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
225 ]
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500226 return flags