blob: 4e09a828441970b708e8273269e3d242fb5bec8c [file] [log] [blame]
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -07001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
2
Davide Pesavento333e3eb2021-10-12 19:30:51 -04003import platform
Davide Pesavento98ff2bc2018-05-24 00:10:31 -04004from waflib import Configure, Logs, Utils
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -07005
Davide Pesaventofea0f092023-08-09 13:18:02 -04006
Shuo Chen478204c2014-03-18 18:27:04 -07007def options(opt):
Davide Pesavento98ff2bc2018-05-24 00:10:31 -04008 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesavento333e3eb2021-10-12 19:30:51 -04009 help='Compile in debugging mode with minimal optimizations (-Og)')
Shuo Chen478204c2014-03-18 18:27:04 -070010
Davide Pesaventofea0f092023-08-09 13:18:02 -040011
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070012def configure(conf):
Davide Pesaventob545aac2017-09-22 23:54:10 -040013 conf.start_msg('Checking C++ compiler version')
14
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040015 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 Pesaventob545aac2017-09-22 23:54:10 -040018 errmsg = ''
19 warnmsg = ''
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050020 if cxx == 'gcc':
Davide Pesaventoe4b74bd2022-03-09 18:56:37 -050021 if ccver < (7, 4, 0):
Davide Pesaventob545aac2017-09-22 23:54:10 -040022 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesavento60b52cf2023-04-26 15:27: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.')
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000027 conf.flags = GccFlags()
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050028 elif cxx == 'clang':
Davide Pesaventoe4b74bd2022-03-09 18:56:37 -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 Pesavento60b52cf2023-04-26 15:27: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 Pesaventoe4b74bd2022-03-09 18:56:37 -050035 'officially supported and may result in build failures.')
Davide Pesavento60b52cf2023-04-26 15:27:02 -040036 elif ccver < (7, 0, 0):
Davide Pesaventob545aac2017-09-22 23:54:10 -040037 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesavento60b52cf2023-04-26 15:27:02 -040038 'The minimum supported clang version is 7.0.')
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000039 conf.flags = ClangFlags()
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050040 else:
Davide Pesavento1d484532022-08-19 22:08:31 -040041 warnmsg = f'{cxx} compiler is unsupported'
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000042 conf.flags = CompilerFlags()
Davide Pesaventob545aac2017-09-22 23:54:10 -040043
44 if errmsg:
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040045 conf.end_msg(ccverstr, color='RED')
Davide Pesaventob545aac2017-09-22 23:54:10 -040046 conf.fatal(errmsg)
47 elif warnmsg:
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040048 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento333e3eb2021-10-12 19:30:51 -040049 Logs.warn('WARNING: ' + warnmsg)
Davide Pesaventob545aac2017-09-22 23:54:10 -040050 else:
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040051 conf.end_msg(ccverstr)
Shuo Chen478204c2014-03-18 18:27:04 -070052
Davide Pesaventofea0f092023-08-09 13:18:02 -040053 conf.areCustomCxxflagsPresent = len(conf.env.CXXFLAGS) > 0
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050054
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040055 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000056 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050057 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
58 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
59 conf.env.DEFINES += generalFlags['DEFINES']
60
Davide Pesaventofea0f092023-08-09 13:18:02 -040061
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000062@Configure.conf
63def check_compiler_flags(conf):
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050064 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
65 # corresponding environment variables are not set.
66 # DEFINES are always applied.
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070067 if conf.options.debug:
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000068 extraFlags = conf.flags.getDebugFlags(conf)
69 if conf.areCustomCxxflagsPresent:
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050070 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040071 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))
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070075 else:
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000076 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070077
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000078 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050079 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
80 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
81
82 conf.env.DEFINES += extraFlags['DEFINES']
Wentao Shanga8f3c402014-10-30 14:03:27 -070083
Davide Pesaventofea0f092023-08-09 13:18:02 -040084
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070085@Configure.conf
86def add_supported_cxxflags(self, cxxflags):
87 """
Davide Pesaventofea0f092023-08-09 13:18:02 -040088 Check which cxxflags are supported by the active compiler and add them to env.CXXFLAGS variable.
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070089 """
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050090 if len(cxxflags) == 0:
91 return
92
Wentao Shanga8f3c402014-10-30 14:03:27 -070093 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070094
95 supportedFlags = []
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000096 for flags in cxxflags:
97 flags = Utils.to_list(flags)
98 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
99 supportedFlags += flags
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -0700100
Shuo Chen478204c2014-03-18 18:27:04 -0700101 self.end_msg(' '.join(supportedFlags))
Junxiao Shie1801312017-07-22 19:16:49 +0000102 self.env.prepend_value('CXXFLAGS', supportedFlags)
Wentao Shanga8f3c402014-10-30 14:03:27 -0700103
Davide Pesaventofea0f092023-08-09 13:18:02 -0400104
Wentao Shanga8f3c402014-10-30 14:03:27 -0700105@Configure.conf
106def add_supported_linkflags(self, linkflags):
107 """
Davide Pesaventofea0f092023-08-09 13:18:02 -0400108 Check which linkflags are supported by the active compiler and add them to env.LINKFLAGS variable.
Wentao Shanga8f3c402014-10-30 14:03:27 -0700109 """
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500110 if len(linkflags) == 0:
111 return
112
Wentao Shanga8f3c402014-10-30 14:03:27 -0700113 self.start_msg('Checking supported LINKFLAGS')
114
115 supportedFlags = []
Alexander Afanasyevbb058c02018-02-15 22:49:24 +0000116 for flags in linkflags:
117 flags = Utils.to_list(flags)
118 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
119 supportedFlags += flags
Wentao Shanga8f3c402014-10-30 14:03:27 -0700120
121 self.end_msg(' '.join(supportedFlags))
Junxiao Shie1801312017-07-22 19:16:49 +0000122 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500123
124
Davide Pesaventofea0f092023-08-09 13:18:02 -0400125class CompilerFlags:
Davide Pesavento98ff2bc2018-05-24 00:10:31 -0400126 def getCompilerVersion(self, conf):
127 return tuple(int(i) for i in conf.env.CC_VERSION)
128
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500129 def getGeneralFlags(self, conf):
130 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Davide Pesavento164ae3b2023-11-11 22:00:23 -0500131 return {
132 'CXXFLAGS': [],
133 'LINKFLAGS': [],
134 'DEFINES': ['BOOST_ASIO_NO_DEPRECATED', 'BOOST_FILESYSTEM_NO_DEPRECATED'],
135 }
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500136
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 Pesaventofea0f092023-08-09 13:18:02 -0400145
146class GccClangCommonFlags(CompilerFlags):
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500147 """
Davide Pesaventofea0f092023-08-09 13:18:02 -0400148 This class defines common flags that work for both gcc and clang compilers.
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500149 """
Davide Pesaventofea0f092023-08-09 13:18:02 -0400150
Junxiao Shie1801312017-07-22 19:16:49 +0000151 def getGeneralFlags(self, conf):
Davide Pesaventofea0f092023-08-09 13:18:02 -0400152 flags = super().getGeneralFlags(conf)
Davide Pesavento11904062022-04-14 22:33:28 -0400153 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesaventoc2414752022-11-16 16:56:45 -0500154 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesaventoed2f4762019-01-25 00:40:46 -0500155 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Junxiao Shie1801312017-07-22 19:16:49 +0000156 return flags
157
Davide Pesaventofea0f092023-08-09 13:18:02 -0400158 __cxxFlags = [
159 '-fdiagnostics-color',
160 '-Wall',
161 '-Wextra',
162 '-Wpedantic',
163 '-Wenum-conversion',
164 '-Wextra-semi',
Davide Pesaventofea0f092023-08-09 13:18:02 -0400165 '-Wno-unused-parameter',
166 ]
167 __linkFlags = ['-Wl,-O1']
168
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500169 def getDebugFlags(self, conf):
Davide Pesaventofea0f092023-08-09 13:18:02 -0400170 flags = super().getDebugFlags(conf)
171 flags['CXXFLAGS'] += ['-Og', '-g'] + self.__cxxFlags + [
172 '-Werror',
173 '-Wno-error=deprecated-declarations', # Bug #3795
174 '-Wno-error=maybe-uninitialized', # Bug #1615
175 ]
176 flags['LINKFLAGS'] += self.__linkFlags
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500177 return flags
178
179 def getOptimizedFlags(self, conf):
Davide Pesaventofea0f092023-08-09 13:18:02 -0400180 flags = super().getOptimizedFlags(conf)
181 flags['CXXFLAGS'] += ['-O2', '-g1'] + self.__cxxFlags
182 flags['LINKFLAGS'] += self.__linkFlags
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500183 return flags
184
Davide Pesaventofea0f092023-08-09 13:18:02 -0400185
186class GccFlags(GccClangCommonFlags):
187 __cxxFlags = [
188 '-Wcatch-value=2',
189 '-Wcomma-subscript', # enabled by default in C++20
190 '-Wduplicated-branches',
191 '-Wduplicated-cond',
192 '-Wlogical-op',
193 '-Wredundant-tags',
194 '-Wvolatile', # enabled by default in C++20
195 ]
196
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500197 def getDebugFlags(self, conf):
Davide Pesaventofea0f092023-08-09 13:18:02 -0400198 flags = super().getDebugFlags(conf)
199 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventoe4b74bd2022-03-09 18:56:37 -0500200 if platform.machine() == 'armv7l':
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400201 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500202 return flags
203
204 def getOptimizedFlags(self, conf):
Davide Pesaventofea0f092023-08-09 13:18:02 -0400205 flags = super().getOptimizedFlags(conf)
206 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventoe4b74bd2022-03-09 18:56:37 -0500207 if platform.machine() == 'armv7l':
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400208 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500209 return flags
210
Davide Pesaventofea0f092023-08-09 13:18:02 -0400211
212class ClangFlags(GccClangCommonFlags):
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500213 def getGeneralFlags(self, conf):
Davide Pesaventofea0f092023-08-09 13:18:02 -0400214 flags = super().getGeneralFlags(conf)
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400215 if Utils.unversioned_sys_platform() == 'darwin':
Davide Pesavento98ff2bc2018-05-24 00:10:31 -0400216 # Bug #4296
Davide Pesavento1d484532022-08-19 22:08:31 -0400217 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
Davide Pesaventofea0f092023-08-09 13:18:02 -0400218 flags['CXXFLAGS'] += [
219 ['-isystem', f'{brewdir}/include'], # for Homebrew
220 ['-isystem', '/opt/local/include'], # for MacPorts
221 ]
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400222 elif Utils.unversioned_sys_platform() == 'freebsd':
223 # Bug #4790
224 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500225 return flags
226
Davide Pesaventofea0f092023-08-09 13:18:02 -0400227 __cxxFlags = [
228 '-Wundefined-func-template',
229 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
230 ]
231
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500232 def getDebugFlags(self, conf):
Davide Pesaventofea0f092023-08-09 13:18:02 -0400233 flags = super().getDebugFlags(conf)
234 flags['CXXFLAGS'] += self.__cxxFlags
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500235 return flags
236
237 def getOptimizedFlags(self, conf):
Davide Pesaventofea0f092023-08-09 13:18:02 -0400238 flags = super().getOptimizedFlags(conf)
239 flags['CXXFLAGS'] += self.__cxxFlags
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500240 return flags