blob: 98b3a7f2c89551f299ce053efd18d877e9c4475a [file] [log] [blame]
Davide Pesavento7ae8b082021-10-12 21:45:47 -04001import platform
Alexander Afanasyev67758b12018-03-06 18:36:44 -05002from waflib import Configure, Logs, Utils
Yingdi Yu40cd1c32014-04-17 15:02:17 -07003
Davide Pesavento5110a8a2023-08-20 21:42:11 -04004
Yingdi Yu40cd1c32014-04-17 15:02:17 -07005def options(opt):
Alexander Afanasyev67758b12018-03-06 18:36:44 -05006 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesavento7ae8b082021-10-12 21:45:47 -04007 help='Compile in debugging mode with minimal optimizations (-Og)')
Yingdi Yu40cd1c32014-04-17 15:02:17 -07008
Davide Pesavento5110a8a2023-08-20 21:42:11 -04009
Yingdi Yu40cd1c32014-04-17 15:02:17 -070010def configure(conf):
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -040011 conf.start_msg('Checking C++ compiler version')
12
Alexander Afanasyev67758b12018-03-06 18:36:44 -050013 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 Pesavento6ebfd7c2017-08-26 17:52:54 -040016 errmsg = ''
17 warnmsg = ''
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080018 if cxx == 'gcc':
Davide Pesaventod1f1df82022-03-12 16:40:37 -050019 if ccver < (7, 4, 0):
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -040020 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesavento5110a8a2023-08-20 21:42:11 -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.')
Alexander Afanasyev67758b12018-03-06 18:36:44 -050025 conf.flags = GccFlags()
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080026 elif cxx == 'clang':
Davide Pesaventod1f1df82022-03-12 16:40:37 -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 Pesavento5110a8a2023-08-20 21:42:11 -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 Pesaventod1f1df82022-03-12 16:40:37 -050033 'officially supported and may result in build failures.')
Davide Pesavento5110a8a2023-08-20 21:42:11 -040034 elif ccver < (7, 0, 0):
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -040035 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesavento5110a8a2023-08-20 21:42:11 -040036 'The minimum supported clang version is 7.0.')
Alexander Afanasyev67758b12018-03-06 18:36:44 -050037 conf.flags = ClangFlags()
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080038 else:
Davide Pesaventoede59632022-08-26 20:35:44 -040039 warnmsg = f'{cxx} compiler is unsupported'
Alexander Afanasyev67758b12018-03-06 18:36:44 -050040 conf.flags = CompilerFlags()
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -040041
42 if errmsg:
Alexander Afanasyev67758b12018-03-06 18:36:44 -050043 conf.end_msg(ccverstr, color='RED')
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -040044 conf.fatal(errmsg)
45 elif warnmsg:
Alexander Afanasyev67758b12018-03-06 18:36:44 -050046 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento7ae8b082021-10-12 21:45:47 -040047 Logs.warn('WARNING: ' + warnmsg)
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -040048 else:
Alexander Afanasyev67758b12018-03-06 18:36:44 -050049 conf.end_msg(ccverstr)
Yingdi Yu40cd1c32014-04-17 15:02:17 -070050
Davide Pesavento5110a8a2023-08-20 21:42:11 -040051 conf.areCustomCxxflagsPresent = len(conf.env.CXXFLAGS) > 0
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080052
Davide Pesaventoa08dc3f2018-05-24 00:40:28 -040053 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyev67758b12018-03-06 18:36:44 -050054 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080055 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
56 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
57 conf.env.DEFINES += generalFlags['DEFINES']
58
Davide Pesavento5110a8a2023-08-20 21:42:11 -040059
Alexander Afanasyev67758b12018-03-06 18:36:44 -050060@Configure.conf
61def check_compiler_flags(conf):
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080062 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
63 # corresponding environment variables are not set.
64 # DEFINES are always applied.
Yingdi Yu40cd1c32014-04-17 15:02:17 -070065 if conf.options.debug:
Alexander Afanasyev67758b12018-03-06 18:36:44 -050066 extraFlags = conf.flags.getDebugFlags(conf)
67 if conf.areCustomCxxflagsPresent:
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080068 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev67758b12018-03-06 18:36:44 -050069 if missingFlags:
70 Logs.warn('Selected debug mode, but CXXFLAGS is set to a custom value "%s"'
71 % ' '.join(conf.env.CXXFLAGS))
72 Logs.warn('Default flags "%s" will not be used' % ' '.join(missingFlags))
Yingdi Yu40cd1c32014-04-17 15:02:17 -070073 else:
Alexander Afanasyev67758b12018-03-06 18:36:44 -050074 extraFlags = conf.flags.getOptimizedFlags(conf)
Yingdi Yu40cd1c32014-04-17 15:02:17 -070075
Alexander Afanasyev67758b12018-03-06 18:36:44 -050076 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080077 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
78 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
79
80 conf.env.DEFINES += extraFlags['DEFINES']
Vince Lehman0a7da612014-10-29 14:39:29 -050081
Davide Pesavento5110a8a2023-08-20 21:42:11 -040082
Yingdi Yu40cd1c32014-04-17 15:02:17 -070083@Configure.conf
84def add_supported_cxxflags(self, cxxflags):
85 """
Davide Pesavento5110a8a2023-08-20 21:42:11 -040086 Check which cxxflags are supported by the active compiler and add them to env.CXXFLAGS variable.
Yingdi Yu40cd1c32014-04-17 15:02:17 -070087 """
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080088 if len(cxxflags) == 0:
89 return
90
91 self.start_msg('Checking supported CXXFLAGS')
Yingdi Yu40cd1c32014-04-17 15:02:17 -070092
93 supportedFlags = []
Alexander Afanasyev67758b12018-03-06 18:36:44 -050094 for flags in cxxflags:
95 flags = Utils.to_list(flags)
96 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
97 supportedFlags += flags
Yingdi Yu40cd1c32014-04-17 15:02:17 -070098
99 self.end_msg(' '.join(supportedFlags))
Davide Pesaventoc58cc7f2017-08-08 16:51:28 -0500100 self.env.prepend_value('CXXFLAGS', supportedFlags)
Vince Lehman0a7da612014-10-29 14:39:29 -0500101
Davide Pesavento5110a8a2023-08-20 21:42:11 -0400102
Vince Lehman0a7da612014-10-29 14:39:29 -0500103@Configure.conf
104def add_supported_linkflags(self, linkflags):
105 """
Davide Pesavento5110a8a2023-08-20 21:42:11 -0400106 Check which linkflags are supported by the active compiler and add them to env.LINKFLAGS variable.
Vince Lehman0a7da612014-10-29 14:39:29 -0500107 """
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800108 if len(linkflags) == 0:
109 return
110
111 self.start_msg('Checking supported LINKFLAGS')
Vince Lehman0a7da612014-10-29 14:39:29 -0500112
113 supportedFlags = []
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500114 for flags in linkflags:
115 flags = Utils.to_list(flags)
116 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
117 supportedFlags += flags
Vince Lehman0a7da612014-10-29 14:39:29 -0500118
119 self.end_msg(' '.join(supportedFlags))
Davide Pesaventoc58cc7f2017-08-08 16:51:28 -0500120 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800121
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -0400122
Davide Pesaventoeed32092023-08-20 21:45:54 -0400123class CompilerFlags:
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500124 def getCompilerVersion(self, conf):
125 return tuple(int(i) for i in conf.env.CC_VERSION)
126
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800127 def getGeneralFlags(self, conf):
128 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Davide Pesavento5a1d77a2024-03-13 19:24:11 -0400129 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
130
131 def getDebugFlags(self, conf):
132 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Davide Pesavento5849ee72023-11-12 20:00:21 -0500133 return {
134 'CXXFLAGS': [],
135 'LINKFLAGS': [],
136 'DEFINES': ['BOOST_ASIO_NO_DEPRECATED', 'BOOST_FILESYSTEM_NO_DEPRECATED'],
137 }
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800138
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800139 def getOptimizedFlags(self, conf):
140 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
141 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
142
Davide Pesavento5110a8a2023-08-20 21:42:11 -0400143
Davide Pesaventoeed32092023-08-20 21:45:54 -0400144class GccClangCommonFlags(CompilerFlags):
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800145 """
Davide Pesaventoeed32092023-08-20 21:45:54 -0400146 This class defines common flags that work for both gcc and clang compilers.
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800147 """
Davide Pesaventoeed32092023-08-20 21:45:54 -0400148
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -0400149 def getGeneralFlags(self, conf):
Davide Pesaventoeed32092023-08-20 21:45:54 -0400150 flags = super().getGeneralFlags(conf)
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400151 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesavento65ee9922022-11-16 00:21:43 -0500152 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesaventoe5569912019-01-29 19:39:06 -0500153 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -0400154 return flags
155
Davide Pesaventoeed32092023-08-20 21:45:54 -0400156 __cxxFlags = [
157 '-fdiagnostics-color',
158 '-Wall',
159 '-Wextra',
160 '-Wpedantic',
161 '-Wenum-conversion',
162 '-Wextra-semi',
Davide Pesaventoeed32092023-08-20 21:45:54 -0400163 '-Wno-unused-parameter',
164 ]
165 __linkFlags = ['-Wl,-O1']
166
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800167 def getDebugFlags(self, conf):
Davide Pesaventoeed32092023-08-20 21:45:54 -0400168 flags = super().getDebugFlags(conf)
169 flags['CXXFLAGS'] += ['-Og', '-g'] + self.__cxxFlags + [
170 '-Werror',
171 '-Wno-error=deprecated-declarations', # Bug #3795
172 '-Wno-error=maybe-uninitialized', # Bug #1615
173 ]
174 flags['LINKFLAGS'] += self.__linkFlags
Davide Pesaventoda43c612024-01-19 16:44:25 -0500175 # Enable assertions in libstdc++
176 # https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
177 flags['DEFINES'] += ['_GLIBCXX_ASSERTIONS=1']
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800178 return flags
179
180 def getOptimizedFlags(self, conf):
Davide Pesaventoeed32092023-08-20 21:45:54 -0400181 flags = super().getOptimizedFlags(conf)
182 flags['CXXFLAGS'] += ['-O2', '-g1'] + self.__cxxFlags
183 flags['LINKFLAGS'] += self.__linkFlags
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800184 return flags
185
Davide Pesavento5110a8a2023-08-20 21:42:11 -0400186
Davide Pesaventoeed32092023-08-20 21:45:54 -0400187class 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
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800198 def getDebugFlags(self, conf):
Davide Pesaventoeed32092023-08-20 21:45:54 -0400199 flags = super().getDebugFlags(conf)
200 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500201 if platform.machine() == 'armv7l':
Davide Pesavento7ae8b082021-10-12 21:45:47 -0400202 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800203 return flags
204
205 def getOptimizedFlags(self, conf):
Davide Pesaventoeed32092023-08-20 21:45:54 -0400206 flags = super().getOptimizedFlags(conf)
207 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500208 if platform.machine() == 'armv7l':
Davide Pesavento7ae8b082021-10-12 21:45:47 -0400209 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800210 return flags
211
Davide Pesavento5110a8a2023-08-20 21:42:11 -0400212
Davide Pesaventoeed32092023-08-20 21:45:54 -0400213class ClangFlags(GccClangCommonFlags):
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800214 def getGeneralFlags(self, conf):
Davide Pesaventoeed32092023-08-20 21:45:54 -0400215 flags = super().getGeneralFlags(conf)
Davide Pesavento7ae8b082021-10-12 21:45:47 -0400216 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500217 # Bug #4296
Davide Pesaventoede59632022-08-26 20:35:44 -0400218 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
Davide Pesaventoeed32092023-08-20 21:45:54 -0400219 flags['CXXFLAGS'] += [
220 ['-isystem', f'{brewdir}/include'], # for Homebrew
221 ['-isystem', '/opt/local/include'], # for MacPorts
222 ]
Davide Pesavento7ae8b082021-10-12 21:45:47 -0400223 elif Utils.unversioned_sys_platform() == 'freebsd':
224 # Bug #4790
225 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Davide Pesaventoda43c612024-01-19 16:44:25 -0500226 if self.getCompilerVersion(conf) >= (18, 0, 0):
227 # Bug #5300
228 flags['CXXFLAGS'] += ['-Wno-enum-constexpr-conversion']
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800229 return flags
230
Davide Pesaventoeed32092023-08-20 21:45:54 -0400231 __cxxFlags = [
232 '-Wundefined-func-template',
233 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
234 ]
235
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800236 def getDebugFlags(self, conf):
Davide Pesaventoeed32092023-08-20 21:45:54 -0400237 flags = super().getDebugFlags(conf)
238 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventoda43c612024-01-19 16:44:25 -0500239 # Enable assertions in libc++
240 if self.getCompilerVersion(conf) >= (18, 0, 0):
241 # https://libcxx.llvm.org/Hardening.html
242 flags['DEFINES'] += ['_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE']
243 elif self.getCompilerVersion(conf) >= (15, 0, 0):
244 # https://releases.llvm.org/15.0.0/projects/libcxx/docs/UsingLibcxx.html#enabling-the-safe-libc-mode
245 flags['DEFINES'] += ['_LIBCPP_ENABLE_ASSERTIONS=1']
Davide Pesavento5a1d77a2024-03-13 19:24:11 -0400246 # Tell libc++ to avoid including transitive headers
247 # https://libcxx.llvm.org/DesignDocs/HeaderRemovalPolicy.html
248 flags['DEFINES'] += ['_LIBCPP_REMOVE_TRANSITIVE_INCLUDES=1']
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800249 return flags
250
251 def getOptimizedFlags(self, conf):
Davide Pesaventoeed32092023-08-20 21:45:54 -0400252 flags = super().getOptimizedFlags(conf)
253 flags['CXXFLAGS'] += self.__cxxFlags
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800254 return flags