blob: 3e001a90b21e190521c72759a651c43a4161ec91 [file] [log] [blame]
Davide Pesavento5f408ae2020-07-15 21:17:04 -04001import platform
Alexander Afanasyev40491df2018-03-09 16:29:52 -05002from waflib import Configure, Logs, Utils
Alexander Afanasyev7eb59112014-07-02 14:21:11 -07003
Davide Pesavento97741e72023-08-12 16:36:07 -04004
Alexander Afanasyev7eb59112014-07-02 14:21:11 -07005def options(opt):
Alexander Afanasyev40491df2018-03-09 16:29:52 -05006 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesavento5f408ae2020-07-15 21:17:04 -04007 help='Compile in debugging mode with minimal optimizations (-Og)')
Alexander Afanasyev7eb59112014-07-02 14:21:11 -07008
Davide Pesavento97741e72023-08-12 16:36:07 -04009
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070010def configure(conf):
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040011 conf.start_msg('Checking C++ compiler version')
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070012
Alexander Afanasyev40491df2018-03-09 16:29:52 -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)
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040016 errmsg = ''
17 warnmsg = ''
18 if cxx == 'gcc':
Davide Pesavento6c4bb212024-12-14 15:30:21 -050019 if ccver < (9, 1, 0):
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040020 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesavento6c4bb212024-12-14 15:30:21 -050021 'The minimum supported gcc version is 10.2.')
22 elif ccver < (10, 2, 0):
23 warnmsg = ('Using a version of gcc older than 10.2 is not '
Davide Pesaventoed778702023-04-26 15:31:31 -040024 'officially supported and may result in build failures.')
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040025 conf.flags = GccFlags()
26 elif cxx == 'clang':
Davide Pesavento26a03262022-03-07 13:37:34 -050027 if Utils.unversioned_sys_platform() == 'darwin':
Davide Pesavento6c4bb212024-12-14 15:30:21 -050028 if ccver < (11, 0, 0):
Davide Pesavento26a03262022-03-07 13:37:34 -050029 errmsg = ('The version of Xcode you are using is too old.\n'
Davide Pesavento27b70922024-07-13 17:20:01 -040030 'The minimum supported Xcode version is 13.0.')
31 elif ccver < (13, 0, 0):
32 warnmsg = ('Using a version of Xcode older than 13.0 is not '
Davide Pesavento26a03262022-03-07 13:37:34 -050033 'officially supported and may result in build failures.')
Davide Pesaventoed778702023-04-26 15:31:31 -040034 elif ccver < (7, 0, 0):
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040035 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesavento6c4bb212024-12-14 15:30:21 -050036 'The minimum supported clang version is 10.0.')
37 elif ccver < (10, 0, 0):
38 warnmsg = ('Using a version of clang older than 10.0 is not '
39 'officially supported and may result in build failures.')
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040040 conf.flags = ClangFlags()
41 else:
Davide Pesavento15a74422022-08-19 15:48:45 -040042 warnmsg = f'{cxx} compiler is unsupported'
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040043 conf.flags = CompilerFlags()
44
45 if errmsg:
Alexander Afanasyev40491df2018-03-09 16:29:52 -050046 conf.end_msg(ccverstr, color='RED')
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040047 conf.fatal(errmsg)
48 elif warnmsg:
Alexander Afanasyev40491df2018-03-09 16:29:52 -050049 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento5f408ae2020-07-15 21:17:04 -040050 Logs.warn('WARNING: ' + warnmsg)
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040051 else:
Alexander Afanasyev40491df2018-03-09 16:29:52 -050052 conf.end_msg(ccverstr)
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040053
Davide Pesavento97741e72023-08-12 16:36:07 -040054 conf.areCustomCxxflagsPresent = len(conf.env.CXXFLAGS) > 0
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -080055
Davide Pesavento4a9395b2018-05-24 00:23:22 -040056 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040057 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -080058 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
59 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
60 conf.env.DEFINES += generalFlags['DEFINES']
61
Davide Pesavento97741e72023-08-12 16:36:07 -040062
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040063@Configure.conf
64def check_compiler_flags(conf):
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -080065 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
66 # corresponding environment variables are not set.
67 # DEFINES are always applied.
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070068 if conf.options.debug:
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040069 extraFlags = conf.flags.getDebugFlags(conf)
70 if conf.areCustomCxxflagsPresent:
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -080071 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev40491df2018-03-09 16:29:52 -050072 if missingFlags:
73 Logs.warn('Selected debug mode, but CXXFLAGS is set to a custom value "%s"'
74 % ' '.join(conf.env.CXXFLAGS))
75 Logs.warn('Default flags "%s" will not be used' % ' '.join(missingFlags))
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070076 else:
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040077 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070078
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040079 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -080080 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
81 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
82
83 conf.env.DEFINES += extraFlags['DEFINES']
Yingdi Yu906c2ea2014-10-31 11:24:50 -070084
Davide Pesavento97741e72023-08-12 16:36:07 -040085
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070086@Configure.conf
87def add_supported_cxxflags(self, cxxflags):
88 """
Davide Pesavento97741e72023-08-12 16:36:07 -040089 Check which cxxflags are supported by the active compiler and add them to env.CXXFLAGS variable.
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070090 """
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -080091 if len(cxxflags) == 0:
92 return
93
Yingdi Yu906c2ea2014-10-31 11:24:50 -070094 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070095
96 supportedFlags = []
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040097 for flags in cxxflags:
98 flags = Utils.to_list(flags)
99 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
100 supportedFlags += flags
Alexander Afanasyev7eb59112014-07-02 14:21:11 -0700101
102 self.end_msg(' '.join(supportedFlags))
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800103 self.env.prepend_value('CXXFLAGS', supportedFlags)
Yingdi Yu906c2ea2014-10-31 11:24:50 -0700104
Davide Pesavento97741e72023-08-12 16:36:07 -0400105
Yingdi Yu906c2ea2014-10-31 11:24:50 -0700106@Configure.conf
107def add_supported_linkflags(self, linkflags):
108 """
Davide Pesavento97741e72023-08-12 16:36:07 -0400109 Check which linkflags are supported by the active compiler and add them to env.LINKFLAGS variable.
Yingdi Yu906c2ea2014-10-31 11:24:50 -0700110 """
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800111 if len(linkflags) == 0:
112 return
113
Yingdi Yu906c2ea2014-10-31 11:24:50 -0700114 self.start_msg('Checking supported LINKFLAGS')
115
116 supportedFlags = []
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -0400117 for flags in linkflags:
118 flags = Utils.to_list(flags)
119 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
120 supportedFlags += flags
Yingdi Yu906c2ea2014-10-31 11:24:50 -0700121
122 self.end_msg(' '.join(supportedFlags))
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800123 self.env.prepend_value('LINKFLAGS', supportedFlags)
124
125
Davide Pesavento97741e72023-08-12 16:36:07 -0400126class CompilerFlags:
Alexander Afanasyev40491df2018-03-09 16:29:52 -0500127 def getCompilerVersion(self, conf):
128 return tuple(int(i) for i in conf.env.CC_VERSION)
129
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800130 def getGeneralFlags(self, conf):
131 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Davide Pesavento7558ed22024-03-13 18:28:07 -0400132 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
133
134 def getDebugFlags(self, conf):
135 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Davide Pesaventod45f8882023-11-11 17:32:23 -0500136 return {
137 'CXXFLAGS': [],
138 'LINKFLAGS': [],
139 'DEFINES': ['BOOST_ASIO_NO_DEPRECATED', 'BOOST_FILESYSTEM_NO_DEPRECATED'],
140 }
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800141
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800142 def getOptimizedFlags(self, conf):
143 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
144 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
145
Davide Pesavento97741e72023-08-12 16:36:07 -0400146
147class GccClangCommonFlags(CompilerFlags):
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800148 """
Davide Pesavento97741e72023-08-12 16:36:07 -0400149 This class defines common flags that work for both gcc and clang compilers.
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800150 """
Davide Pesavento97741e72023-08-12 16:36:07 -0400151
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -0400152 def getGeneralFlags(self, conf):
Davide Pesavento97741e72023-08-12 16:36:07 -0400153 flags = super().getGeneralFlags(conf)
Davide Pesavento8663ed12022-07-23 03:04:27 -0400154 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesaventocda44892022-11-15 13:40:33 -0500155 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesaventofae9def2019-01-29 14:34:33 -0500156 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -0400157 return flags
158
Davide Pesavento97741e72023-08-12 16:36:07 -0400159 __cxxFlags = [
160 '-fdiagnostics-color',
161 '-Wall',
162 '-Wextra',
163 '-Wpedantic',
164 '-Wenum-conversion',
165 '-Wextra-semi',
166 '-Wnon-virtual-dtor',
167 '-Wno-unused-parameter',
168 ]
169 __linkFlags = ['-Wl,-O1']
170
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800171 def getDebugFlags(self, conf):
Davide Pesavento97741e72023-08-12 16:36:07 -0400172 flags = super().getDebugFlags(conf)
173 flags['CXXFLAGS'] += ['-Og', '-g'] + self.__cxxFlags + [
174 '-Werror',
175 '-Wno-error=deprecated-declarations', # Bug #3795
176 '-Wno-error=maybe-uninitialized', # Bug #1615
177 ]
178 flags['LINKFLAGS'] += self.__linkFlags
Davide Pesavento11c36382024-01-19 00:23:02 -0500179 # Enable assertions in libstdc++
180 # https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
181 flags['DEFINES'] += ['_GLIBCXX_ASSERTIONS=1']
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800182 return flags
183
184 def getOptimizedFlags(self, conf):
Davide Pesavento97741e72023-08-12 16:36:07 -0400185 flags = super().getOptimizedFlags(conf)
186 flags['CXXFLAGS'] += ['-O2', '-g1'] + self.__cxxFlags
187 flags['LINKFLAGS'] += self.__linkFlags
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800188 return flags
189
Davide Pesavento97741e72023-08-12 16:36:07 -0400190
191class GccFlags(GccClangCommonFlags):
192 __cxxFlags = [
193 '-Wcatch-value=2',
194 '-Wcomma-subscript', # enabled by default in C++20
195 '-Wduplicated-branches',
196 '-Wduplicated-cond',
197 '-Wlogical-op',
198 '-Wredundant-tags',
199 '-Wvolatile', # enabled by default in C++20
200 ]
201
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800202 def getDebugFlags(self, conf):
Davide Pesavento97741e72023-08-12 16:36:07 -0400203 flags = super().getDebugFlags(conf)
204 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento26a03262022-03-07 13:37:34 -0500205 if platform.machine() == 'armv7l':
Davide Pesavento5f408ae2020-07-15 21:17:04 -0400206 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800207 return flags
208
209 def getOptimizedFlags(self, conf):
Davide Pesavento97741e72023-08-12 16:36:07 -0400210 flags = super().getOptimizedFlags(conf)
211 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento26a03262022-03-07 13:37:34 -0500212 if platform.machine() == 'armv7l':
Davide Pesavento5f408ae2020-07-15 21:17:04 -0400213 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800214 return flags
215
Davide Pesavento97741e72023-08-12 16:36:07 -0400216
217class ClangFlags(GccClangCommonFlags):
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800218 def getGeneralFlags(self, conf):
Davide Pesavento97741e72023-08-12 16:36:07 -0400219 flags = super().getGeneralFlags(conf)
Davide Pesavento5f408ae2020-07-15 21:17:04 -0400220 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev40491df2018-03-09 16:29:52 -0500221 # Bug #4296
Davide Pesavento15a74422022-08-19 15:48:45 -0400222 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
Davide Pesavento97741e72023-08-12 16:36:07 -0400223 flags['CXXFLAGS'] += [
224 ['-isystem', f'{brewdir}/include'], # for Homebrew
225 ['-isystem', '/opt/local/include'], # for MacPorts
226 ]
Davide Pesavento5f408ae2020-07-15 21:17:04 -0400227 elif Utils.unversioned_sys_platform() == 'freebsd':
228 # Bug #4790
229 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Davide Pesavento11c36382024-01-19 00:23:02 -0500230 if self.getCompilerVersion(conf) >= (18, 0, 0):
231 # Bug #5300
232 flags['CXXFLAGS'] += ['-Wno-enum-constexpr-conversion']
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800233 return flags
234
Davide Pesavento97741e72023-08-12 16:36:07 -0400235 __cxxFlags = [
236 '-Wundefined-func-template',
237 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
238 ]
239
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800240 def getDebugFlags(self, conf):
Davide Pesavento97741e72023-08-12 16:36:07 -0400241 flags = super().getDebugFlags(conf)
242 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento11c36382024-01-19 00:23:02 -0500243 # Enable assertions in libc++
244 if self.getCompilerVersion(conf) >= (18, 0, 0):
245 # https://libcxx.llvm.org/Hardening.html
246 flags['DEFINES'] += ['_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE']
247 elif self.getCompilerVersion(conf) >= (15, 0, 0):
248 # https://releases.llvm.org/15.0.0/projects/libcxx/docs/UsingLibcxx.html#enabling-the-safe-libc-mode
249 flags['DEFINES'] += ['_LIBCPP_ENABLE_ASSERTIONS=1']
Davide Pesavento7558ed22024-03-13 18:28:07 -0400250 # Tell libc++ to avoid including transitive headers
251 # https://libcxx.llvm.org/DesignDocs/HeaderRemovalPolicy.html
252 flags['DEFINES'] += ['_LIBCPP_REMOVE_TRANSITIVE_INCLUDES=1']
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800253 return flags
254
255 def getOptimizedFlags(self, conf):
Davide Pesavento97741e72023-08-12 16:36:07 -0400256 flags = super().getOptimizedFlags(conf)
257 flags['CXXFLAGS'] += self.__cxxFlags
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800258 return flags