blob: bee50724ee61b81ffcbe97edd8360042858140a8 [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
Davide Pesavento22835d72023-08-11 00:50:21 -04006
Zhiyi Zhang8617a792017-01-17 16:45:56 -08007def options(opt):
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -07008 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesavento725d3fc2021-11-19 23:37:03 -05009 help='Compile in debugging mode with minimal optimizations (-Og)')
Zhiyi Zhang8617a792017-01-17 16:45:56 -080010
Davide Pesavento22835d72023-08-11 00:50:21 -040011
Zhiyi Zhang8617a792017-01-17 16:45:56 -080012def configure(conf):
Davide Pesavento08994782018-01-22 12:13:41 -050013 conf.start_msg('Checking C++ compiler version')
Zhiyi Zhang8617a792017-01-17 16:45:56 -080014
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -070015 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)
Davide Pesavento08994782018-01-22 12:13:41 -050018 errmsg = ''
19 warnmsg = ''
20 if cxx == 'gcc':
Davide Pesaventoeec07f52022-03-09 14:20:08 -050021 if ccver < (7, 4, 0):
Davide Pesavento08994782018-01-22 12:13:41 -050022 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesaventof1c32c32023-04-26 15:34:51 -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.')
Davide Pesavento08994782018-01-22 12:13:41 -050027 conf.flags = GccFlags()
28 elif cxx == 'clang':
Davide Pesaventoeec07f52022-03-09 14:20:08 -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 Pesaventof1c32c32023-04-26 15:34:51 -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 Pesaventoeec07f52022-03-09 14:20:08 -050035 'officially supported and may result in build failures.')
Davide Pesaventof1c32c32023-04-26 15:34:51 -040036 elif ccver < (7, 0, 0):
Davide Pesavento08994782018-01-22 12:13:41 -050037 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesaventof1c32c32023-04-26 15:34:51 -040038 'The minimum supported clang version is 7.0.')
Davide Pesavento08994782018-01-22 12:13:41 -050039 conf.flags = ClangFlags()
40 else:
Davide Pesavento6d1b6722022-08-20 17:54:05 -040041 warnmsg = f'{cxx} compiler is unsupported'
Davide Pesavento08994782018-01-22 12:13:41 -050042 conf.flags = CompilerFlags()
43
44 if errmsg:
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -070045 conf.end_msg(ccverstr, color='RED')
Davide Pesavento08994782018-01-22 12:13:41 -050046 conf.fatal(errmsg)
47 elif warnmsg:
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -070048 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento725d3fc2021-11-19 23:37:03 -050049 Logs.warn('WARNING: ' + warnmsg)
Davide Pesavento08994782018-01-22 12:13:41 -050050 else:
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -070051 conf.end_msg(ccverstr)
Davide Pesavento08994782018-01-22 12:13:41 -050052
Davide Pesavento22835d72023-08-11 00:50:21 -040053 conf.areCustomCxxflagsPresent = len(conf.env.CXXFLAGS) > 0
Zhiyi Zhang8617a792017-01-17 16:45:56 -080054
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -070055 # General flags are always applied (e.g., selecting C++ language standard)
Davide Pesavento08994782018-01-22 12:13:41 -050056 generalFlags = conf.flags.getGeneralFlags(conf)
Zhiyi Zhang8617a792017-01-17 16:45:56 -080057 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
58 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
59 conf.env.DEFINES += generalFlags['DEFINES']
60
Davide Pesavento22835d72023-08-11 00:50:21 -040061
Davide Pesavento08994782018-01-22 12:13:41 -050062@Configure.conf
63def check_compiler_flags(conf):
Zhiyi Zhang8617a792017-01-17 16:45:56 -080064 # 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:
Davide Pesavento08994782018-01-22 12:13:41 -050068 extraFlags = conf.flags.getDebugFlags(conf)
69 if conf.areCustomCxxflagsPresent:
Zhiyi Zhang8617a792017-01-17 16:45:56 -080070 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -070071 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))
Zhiyi Zhang8617a792017-01-17 16:45:56 -080075 else:
Davide Pesavento08994782018-01-22 12:13:41 -050076 extraFlags = conf.flags.getOptimizedFlags(conf)
Zhiyi Zhang8617a792017-01-17 16:45:56 -080077
Davide Pesavento08994782018-01-22 12:13:41 -050078 if not conf.areCustomCxxflagsPresent:
Zhiyi Zhang8617a792017-01-17 16:45:56 -080079 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
80 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
81
82 conf.env.DEFINES += extraFlags['DEFINES']
83
Davide Pesavento22835d72023-08-11 00:50:21 -040084
Zhiyi Zhang8617a792017-01-17 16:45:56 -080085@Configure.conf
86def add_supported_cxxflags(self, cxxflags):
87 """
Davide Pesavento22835d72023-08-11 00:50:21 -040088 Check which cxxflags are supported by the active compiler and add them to env.CXXFLAGS variable.
Zhiyi Zhang8617a792017-01-17 16:45:56 -080089 """
90 if len(cxxflags) == 0:
91 return
92
93 self.start_msg('Checking supported CXXFLAGS')
94
95 supportedFlags = []
Davide Pesavento08994782018-01-22 12:13:41 -050096 for flags in cxxflags:
97 flags = Utils.to_list(flags)
98 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
99 supportedFlags += flags
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800100
101 self.end_msg(' '.join(supportedFlags))
102 self.env.prepend_value('CXXFLAGS', supportedFlags)
103
Davide Pesavento22835d72023-08-11 00:50:21 -0400104
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800105@Configure.conf
106def add_supported_linkflags(self, linkflags):
107 """
Davide Pesavento22835d72023-08-11 00:50:21 -0400108 Check which linkflags are supported by the active compiler and add them to env.LINKFLAGS variable.
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800109 """
110 if len(linkflags) == 0:
111 return
112
113 self.start_msg('Checking supported LINKFLAGS')
114
115 supportedFlags = []
Davide Pesavento08994782018-01-22 12:13:41 -0500116 for flags in linkflags:
117 flags = Utils.to_list(flags)
118 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
119 supportedFlags += flags
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800120
121 self.end_msg(' '.join(supportedFlags))
122 self.env.prepend_value('LINKFLAGS', supportedFlags)
123
124
Davide Pesavento22835d72023-08-11 00:50:21 -0400125class CompilerFlags:
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -0700126 def getCompilerVersion(self, conf):
127 return tuple(int(i) for i in conf.env.CC_VERSION)
128
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800129 def getGeneralFlags(self, conf):
130 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Davide Pesavento3e481842023-11-11 18:01:32 -0500131 return {
132 'CXXFLAGS': [],
133 'LINKFLAGS': [],
134 'DEFINES': ['BOOST_ASIO_NO_DEPRECATED', 'BOOST_FILESYSTEM_NO_DEPRECATED'],
135 }
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800136
137 def getDebugFlags(self, conf):
138 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
139 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
140
141 def getOptimizedFlags(self, conf):
142 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
143 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
144
Davide Pesavento22835d72023-08-11 00:50:21 -0400145
146class GccClangCommonFlags(CompilerFlags):
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800147 """
Davide Pesavento22835d72023-08-11 00:50:21 -0400148 This class defines common flags that work for both gcc and clang compilers.
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800149 """
Davide Pesavento22835d72023-08-11 00:50:21 -0400150
Davide Pesavento08994782018-01-22 12:13:41 -0500151 def getGeneralFlags(self, conf):
Davide Pesavento22835d72023-08-11 00:50:21 -0400152 flags = super().getGeneralFlags(conf)
Davide Pesavento0d1d11c2022-04-11 22:11:34 -0400153 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesaventof8fe6852022-11-15 13:55:07 -0500154 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesavento55d98602019-10-15 22:40:05 -0400155 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Davide Pesavento08994782018-01-22 12:13:41 -0500156 return flags
157
Davide Pesavento22835d72023-08-11 00:50:21 -0400158 __cxxFlags = [
159 '-fdiagnostics-color',
160 '-Wall',
161 '-Wextra',
162 '-Wpedantic',
163 '-Wenum-conversion',
164 '-Wextra-semi',
165 '-Wnon-virtual-dtor',
166 '-Wno-unused-parameter',
167 ]
168 __linkFlags = ['-Wl,-O1']
169
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800170 def getDebugFlags(self, conf):
Davide Pesavento22835d72023-08-11 00:50:21 -0400171 flags = super().getDebugFlags(conf)
172 flags['CXXFLAGS'] += ['-Og', '-g'] + self.__cxxFlags + [
173 '-Werror',
174 '-Wno-error=deprecated-declarations', # Bug #3795
175 '-Wno-error=maybe-uninitialized', # Bug #1615
176 ]
177 flags['LINKFLAGS'] += self.__linkFlags
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800178 return flags
179
180 def getOptimizedFlags(self, conf):
Davide Pesavento22835d72023-08-11 00:50:21 -0400181 flags = super().getOptimizedFlags(conf)
182 flags['CXXFLAGS'] += ['-O2', '-g1'] + self.__cxxFlags
183 flags['LINKFLAGS'] += self.__linkFlags
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800184 return flags
185
Davide Pesavento22835d72023-08-11 00:50:21 -0400186
187class GccFlags(GccClangCommonFlags):
188 __cxxFlags = [
189 '-Wcatch-value=2',
190 '-Wcomma-subscript', # enabled by default in C++20
191 '-Wduplicated-branches',
192 '-Wduplicated-cond',
193 '-Wlogical-op',
194 '-Wredundant-tags',
195 '-Wvolatile', # enabled by default in C++20
196 ]
197
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800198 def getDebugFlags(self, conf):
Davide Pesavento22835d72023-08-11 00:50:21 -0400199 flags = super().getDebugFlags(conf)
200 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventoeec07f52022-03-09 14:20:08 -0500201 if platform.machine() == 'armv7l':
Davide Pesavento725d3fc2021-11-19 23:37:03 -0500202 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800203 return flags
204
205 def getOptimizedFlags(self, conf):
Davide Pesavento22835d72023-08-11 00:50:21 -0400206 flags = super().getOptimizedFlags(conf)
207 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventoeec07f52022-03-09 14:20:08 -0500208 if platform.machine() == 'armv7l':
Davide Pesavento725d3fc2021-11-19 23:37:03 -0500209 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800210 return flags
211
Davide Pesavento22835d72023-08-11 00:50:21 -0400212
213class ClangFlags(GccClangCommonFlags):
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800214 def getGeneralFlags(self, conf):
Davide Pesavento22835d72023-08-11 00:50:21 -0400215 flags = super().getGeneralFlags(conf)
Davide Pesavento725d3fc2021-11-19 23:37:03 -0500216 if Utils.unversioned_sys_platform() == 'darwin':
Zhiyi Zhang8ce677b2018-07-13 14:44:06 -0700217 # Bug #4296
Davide Pesavento6d1b6722022-08-20 17:54:05 -0400218 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
Davide Pesavento22835d72023-08-11 00:50:21 -0400219 flags['CXXFLAGS'] += [
220 ['-isystem', f'{brewdir}/include'], # for Homebrew
221 ['-isystem', '/opt/local/include'], # for MacPorts
222 ]
Davide Pesavento725d3fc2021-11-19 23:37:03 -0500223 elif Utils.unversioned_sys_platform() == 'freebsd':
224 # Bug #4790
225 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800226 return flags
227
Davide Pesavento22835d72023-08-11 00:50:21 -0400228 __cxxFlags = [
229 '-Wundefined-func-template',
230 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
231 ]
232
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800233 def getDebugFlags(self, conf):
Davide Pesavento22835d72023-08-11 00:50:21 -0400234 flags = super().getDebugFlags(conf)
235 flags['CXXFLAGS'] += self.__cxxFlags
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800236 return flags
237
238 def getOptimizedFlags(self, conf):
Davide Pesavento22835d72023-08-11 00:50:21 -0400239 flags = super().getOptimizedFlags(conf)
240 flags['CXXFLAGS'] += self.__cxxFlags
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800241 return flags