blob: 7c6d282d735e22d722fbb7fc7ac37184677e0d80 [file] [log] [blame]
Zhiyi Zhang8617a792017-01-17 16:45:56 -08001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
2
Davide Pesavento725d3fc2021-11-19 23:37:03 -05003import platform
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -07004from waflib import Configure, Logs, Utils
Zhiyi Zhang8617a792017-01-17 16:45:56 -08005
6def options(opt):
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -07007 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesavento725d3fc2021-11-19 23:37:03 -05008 help='Compile in debugging mode with minimal optimizations (-Og)')
Zhiyi Zhang8617a792017-01-17 16:45:56 -08009
10def configure(conf):
Davide Pesavento08994782018-01-22 12:13:41 -050011 conf.start_msg('Checking C++ compiler version')
Zhiyi Zhang8617a792017-01-17 16:45:56 -080012
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -070013 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 Pesavento08994782018-01-22 12:13:41 -050016 errmsg = ''
17 warnmsg = ''
18 if cxx == 'gcc':
Davide Pesaventoeec07f52022-03-09 14:20:08 -050019 if ccver < (7, 4, 0):
Davide Pesavento08994782018-01-22 12:13:41 -050020 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesaventoeec07f52022-03-09 14:20:08 -050021 'The minimum supported gcc version is 7.4.')
Davide Pesavento08994782018-01-22 12:13:41 -050022 conf.flags = GccFlags()
23 elif cxx == 'clang':
Davide Pesaventoeec07f52022-03-09 14:20:08 -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):
Davide Pesavento08994782018-01-22 12:13:41 -050032 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesaventoeec07f52022-03-09 14:20:08 -050033 'The minimum supported clang version is 6.0.')
Davide Pesavento08994782018-01-22 12:13:41 -050034 conf.flags = ClangFlags()
35 else:
Davide Pesavento725d3fc2021-11-19 23:37:03 -050036 warnmsg = '%s compiler is unsupported' % cxx
Davide Pesavento08994782018-01-22 12:13:41 -050037 conf.flags = CompilerFlags()
38
39 if errmsg:
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -070040 conf.end_msg(ccverstr, color='RED')
Davide Pesavento08994782018-01-22 12:13:41 -050041 conf.fatal(errmsg)
42 elif warnmsg:
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -070043 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento725d3fc2021-11-19 23:37:03 -050044 Logs.warn('WARNING: ' + warnmsg)
Davide Pesavento08994782018-01-22 12:13:41 -050045 else:
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -070046 conf.end_msg(ccverstr)
Davide Pesavento08994782018-01-22 12:13:41 -050047
48 conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
Zhiyi Zhang8617a792017-01-17 16:45:56 -080049
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -070050 # General flags are always applied (e.g., selecting C++ language standard)
Davide Pesavento08994782018-01-22 12:13:41 -050051 generalFlags = conf.flags.getGeneralFlags(conf)
Zhiyi Zhang8617a792017-01-17 16:45:56 -080052 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
53 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
54 conf.env.DEFINES += generalFlags['DEFINES']
55
Davide Pesavento08994782018-01-22 12:13:41 -050056@Configure.conf
57def check_compiler_flags(conf):
Zhiyi Zhang8617a792017-01-17 16:45:56 -080058 # 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:
Davide Pesavento08994782018-01-22 12:13:41 -050062 extraFlags = conf.flags.getDebugFlags(conf)
63 if conf.areCustomCxxflagsPresent:
Zhiyi Zhang8617a792017-01-17 16:45:56 -080064 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -070065 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))
Zhiyi Zhang8617a792017-01-17 16:45:56 -080069 else:
Davide Pesavento08994782018-01-22 12:13:41 -050070 extraFlags = conf.flags.getOptimizedFlags(conf)
Zhiyi Zhang8617a792017-01-17 16:45:56 -080071
Davide Pesavento08994782018-01-22 12:13:41 -050072 if not conf.areCustomCxxflagsPresent:
Zhiyi Zhang8617a792017-01-17 16:45:56 -080073 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 = []
Davide Pesavento08994782018-01-22 12:13:41 -050089 for flags in cxxflags:
90 flags = Utils.to_list(flags)
91 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
92 supportedFlags += flags
Zhiyi Zhang8617a792017-01-17 16:45:56 -080093
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 = []
Davide Pesavento08994782018-01-22 12:13:41 -0500108 for flags in linkflags:
109 flags = Utils.to_list(flags)
110 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
111 supportedFlags += flags
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800112
113 self.end_msg(' '.join(supportedFlags))
114 self.env.prepend_value('LINKFLAGS', supportedFlags)
115
116
117class CompilerFlags(object):
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -0700118 def getCompilerVersion(self, conf):
119 return tuple(int(i) for i in conf.env.CC_VERSION)
120
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800121 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 """
Davide Pesavento08994782018-01-22 12:13:41 -0500137 def getGeneralFlags(self, conf):
138 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
Davide Pesavento0d1d11c2022-04-11 22:11:34 -0400139 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesavento55d98602019-10-15 22:40:05 -0400140 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 Pesavento08994782018-01-22 12:13:41 -0500144 return flags
145
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800146 def getDebugFlags(self, conf):
147 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Davide Pesavento725d3fc2021-11-19 23:37:03 -0500148 flags['CXXFLAGS'] += ['-Og',
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800149 '-g3',
150 '-pedantic',
151 '-Wall',
152 '-Wextra',
153 '-Werror',
Davide Pesavento725d3fc2021-11-19 23:37:03 -0500154 '-Wcatch-value=2',
155 '-Wextra-semi',
Davide Pesavento08994782018-01-22 12:13:41 -0500156 '-Wnon-virtual-dtor',
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800157 '-Wno-error=deprecated-declarations', # Bug #3795
Davide Pesavento08994782018-01-22 12:13:41 -0500158 '-Wno-error=maybe-uninitialized', # Bug #1615
159 '-Wno-unused-parameter',
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800160 ]
Davide Pesavento55d98602019-10-15 22:40:05 -0400161 flags['LINKFLAGS'] += ['-Wl,-O1']
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800162 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 Pesavento725d3fc2021-11-19 23:37:03 -0500171 '-Wcatch-value=2',
172 '-Wextra-semi',
Davide Pesavento08994782018-01-22 12:13:41 -0500173 '-Wnon-virtual-dtor',
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800174 '-Wno-unused-parameter',
175 ]
Davide Pesavento55d98602019-10-15 22:40:05 -0400176 flags['LINKFLAGS'] += ['-Wl,-O1']
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800177 return flags
178
179class GccFlags(GccBasicFlags):
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800180 def getDebugFlags(self, conf):
181 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesavento725d3fc2021-11-19 23:37:03 -0500182 flags['CXXFLAGS'] += ['-fdiagnostics-color',
183 '-Wredundant-tags',
184 ]
Davide Pesaventoeec07f52022-03-09 14:20:08 -0500185 if platform.machine() == 'armv7l':
Davide Pesavento725d3fc2021-11-19 23:37:03 -0500186 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800187 return flags
188
189 def getOptimizedFlags(self, conf):
190 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesavento725d3fc2021-11-19 23:37:03 -0500191 flags['CXXFLAGS'] += ['-fdiagnostics-color',
192 '-Wredundant-tags',
193 ]
Davide Pesaventoeec07f52022-03-09 14:20:08 -0500194 if platform.machine() == 'armv7l':
Davide Pesavento725d3fc2021-11-19 23:37:03 -0500195 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800196 return flags
197
198class ClangFlags(GccBasicFlags):
199 def getGeneralFlags(self, conf):
200 flags = super(ClangFlags, self).getGeneralFlags(conf)
Davide Pesavento725d3fc2021-11-19 23:37:03 -0500201 if Utils.unversioned_sys_platform() == 'darwin':
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -0700202 # Bug #4296
Davide Pesavento08994782018-01-22 12:13:41 -0500203 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include'], # for Homebrew
204 ['-isystem', '/opt/local/include']] # for MacPorts
Davide Pesavento725d3fc2021-11-19 23:37:03 -0500205 elif Utils.unversioned_sys_platform() == 'freebsd':
206 # Bug #4790
207 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800208 return flags
209
210 def getDebugFlags(self, conf):
211 flags = super(ClangFlags, self).getDebugFlags(conf)
212 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesavento08994782018-01-22 12:13:41 -0500213 '-Wundefined-func-template',
Davide Pesavento08994782018-01-22 12:13:41 -0500214 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800215 ]
216 return flags
217
218 def getOptimizedFlags(self, conf):
219 flags = super(ClangFlags, self).getOptimizedFlags(conf)
220 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesavento08994782018-01-22 12:13:41 -0500221 '-Wundefined-func-template',
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800222 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
223 ]
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800224 return flags