blob: 39b3025d6e0655bf51c74fead9cdd1d3022b3eff [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 Pesavento26a03262022-03-07 13:37:34 -050019 if ccver < (7, 4, 0):
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040020 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesaventoed778702023-04-26 15:31:31 -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 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':
28 if ccver < (10, 0, 0):
29 errmsg = ('The version of Xcode you are using is too old.\n'
Davide Pesaventoed778702023-04-26 15:31:31 -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 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 Pesaventoed778702023-04-26 15:31:31 -040036 'The minimum supported clang version is 7.0.')
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040037 conf.flags = ClangFlags()
38 else:
Davide Pesavento15a74422022-08-19 15:48:45 -040039 warnmsg = f'{cxx} compiler is unsupported'
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040040 conf.flags = CompilerFlags()
41
42 if errmsg:
Alexander Afanasyev40491df2018-03-09 16:29:52 -050043 conf.end_msg(ccverstr, color='RED')
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040044 conf.fatal(errmsg)
45 elif warnmsg:
Alexander Afanasyev40491df2018-03-09 16:29:52 -050046 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento5f408ae2020-07-15 21:17:04 -040047 Logs.warn('WARNING: ' + warnmsg)
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040048 else:
Alexander Afanasyev40491df2018-03-09 16:29:52 -050049 conf.end_msg(ccverstr)
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040050
Davide Pesavento97741e72023-08-12 16:36:07 -040051 conf.areCustomCxxflagsPresent = len(conf.env.CXXFLAGS) > 0
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -080052
Davide Pesavento4a9395b2018-05-24 00:23:22 -040053 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040054 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -080055 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
56 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
57 conf.env.DEFINES += generalFlags['DEFINES']
58
Davide Pesavento97741e72023-08-12 16:36:07 -040059
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040060@Configure.conf
61def check_compiler_flags(conf):
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -080062 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
63 # corresponding environment variables are not set.
64 # DEFINES are always applied.
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070065 if conf.options.debug:
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040066 extraFlags = conf.flags.getDebugFlags(conf)
67 if conf.areCustomCxxflagsPresent:
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -080068 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev40491df2018-03-09 16:29:52 -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))
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070073 else:
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040074 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070075
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040076 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -080077 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
78 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
79
80 conf.env.DEFINES += extraFlags['DEFINES']
Yingdi Yu906c2ea2014-10-31 11:24:50 -070081
Davide Pesavento97741e72023-08-12 16:36:07 -040082
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070083@Configure.conf
84def add_supported_cxxflags(self, cxxflags):
85 """
Davide Pesavento97741e72023-08-12 16:36:07 -040086 Check which cxxflags are supported by the active compiler and add them to env.CXXFLAGS variable.
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070087 """
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -080088 if len(cxxflags) == 0:
89 return
90
Yingdi Yu906c2ea2014-10-31 11:24:50 -070091 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070092
93 supportedFlags = []
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040094 for flags in cxxflags:
95 flags = Utils.to_list(flags)
96 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
97 supportedFlags += flags
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070098
99 self.end_msg(' '.join(supportedFlags))
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800100 self.env.prepend_value('CXXFLAGS', supportedFlags)
Yingdi Yu906c2ea2014-10-31 11:24:50 -0700101
Davide Pesavento97741e72023-08-12 16:36:07 -0400102
Yingdi Yu906c2ea2014-10-31 11:24:50 -0700103@Configure.conf
104def add_supported_linkflags(self, linkflags):
105 """
Davide Pesavento97741e72023-08-12 16:36:07 -0400106 Check which linkflags are supported by the active compiler and add them to env.LINKFLAGS variable.
Yingdi Yu906c2ea2014-10-31 11:24:50 -0700107 """
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800108 if len(linkflags) == 0:
109 return
110
Yingdi Yu906c2ea2014-10-31 11:24:50 -0700111 self.start_msg('Checking supported LINKFLAGS')
112
113 supportedFlags = []
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -0400114 for flags in linkflags:
115 flags = Utils.to_list(flags)
116 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
117 supportedFlags += flags
Yingdi Yu906c2ea2014-10-31 11:24:50 -0700118
119 self.end_msg(' '.join(supportedFlags))
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800120 self.env.prepend_value('LINKFLAGS', supportedFlags)
121
122
Davide Pesavento97741e72023-08-12 16:36:07 -0400123class CompilerFlags:
Alexander Afanasyev40491df2018-03-09 16:29:52 -0500124 def getCompilerVersion(self, conf):
125 return tuple(int(i) for i in conf.env.CC_VERSION)
126
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800127 def getGeneralFlags(self, conf):
128 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Davide Pesavento7558ed22024-03-13 18:28:07 -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 Pesaventod45f8882023-11-11 17:32:23 -0500133 return {
134 'CXXFLAGS': [],
135 'LINKFLAGS': [],
136 'DEFINES': ['BOOST_ASIO_NO_DEPRECATED', 'BOOST_FILESYSTEM_NO_DEPRECATED'],
137 }
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800138
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -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 Pesavento97741e72023-08-12 16:36:07 -0400143
144class GccClangCommonFlags(CompilerFlags):
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800145 """
Davide Pesavento97741e72023-08-12 16:36:07 -0400146 This class defines common flags that work for both gcc and clang compilers.
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800147 """
Davide Pesavento97741e72023-08-12 16:36:07 -0400148
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -0400149 def getGeneralFlags(self, conf):
Davide Pesavento97741e72023-08-12 16:36:07 -0400150 flags = super().getGeneralFlags(conf)
Davide Pesavento8663ed12022-07-23 03:04:27 -0400151 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesaventocda44892022-11-15 13:40:33 -0500152 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesaventofae9def2019-01-29 14:34:33 -0500153 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -0400154 return flags
155
Davide Pesavento97741e72023-08-12 16:36:07 -0400156 __cxxFlags = [
157 '-fdiagnostics-color',
158 '-Wall',
159 '-Wextra',
160 '-Wpedantic',
161 '-Wenum-conversion',
162 '-Wextra-semi',
163 '-Wnon-virtual-dtor',
164 '-Wno-unused-parameter',
165 ]
166 __linkFlags = ['-Wl,-O1']
167
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800168 def getDebugFlags(self, conf):
Davide Pesavento97741e72023-08-12 16:36:07 -0400169 flags = super().getDebugFlags(conf)
170 flags['CXXFLAGS'] += ['-Og', '-g'] + self.__cxxFlags + [
171 '-Werror',
172 '-Wno-error=deprecated-declarations', # Bug #3795
173 '-Wno-error=maybe-uninitialized', # Bug #1615
174 ]
175 flags['LINKFLAGS'] += self.__linkFlags
Davide Pesavento11c36382024-01-19 00:23:02 -0500176 # Enable assertions in libstdc++
177 # https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
178 flags['DEFINES'] += ['_GLIBCXX_ASSERTIONS=1']
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800179 return flags
180
181 def getOptimizedFlags(self, conf):
Davide Pesavento97741e72023-08-12 16:36:07 -0400182 flags = super().getOptimizedFlags(conf)
183 flags['CXXFLAGS'] += ['-O2', '-g1'] + self.__cxxFlags
184 flags['LINKFLAGS'] += self.__linkFlags
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800185 return flags
186
Davide Pesavento97741e72023-08-12 16:36:07 -0400187
188class GccFlags(GccClangCommonFlags):
189 __cxxFlags = [
190 '-Wcatch-value=2',
191 '-Wcomma-subscript', # enabled by default in C++20
192 '-Wduplicated-branches',
193 '-Wduplicated-cond',
194 '-Wlogical-op',
195 '-Wredundant-tags',
196 '-Wvolatile', # enabled by default in C++20
197 ]
198
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800199 def getDebugFlags(self, conf):
Davide Pesavento97741e72023-08-12 16:36:07 -0400200 flags = super().getDebugFlags(conf)
201 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento26a03262022-03-07 13:37:34 -0500202 if platform.machine() == 'armv7l':
Davide Pesavento5f408ae2020-07-15 21:17:04 -0400203 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800204 return flags
205
206 def getOptimizedFlags(self, conf):
Davide Pesavento97741e72023-08-12 16:36:07 -0400207 flags = super().getOptimizedFlags(conf)
208 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento26a03262022-03-07 13:37:34 -0500209 if platform.machine() == 'armv7l':
Davide Pesavento5f408ae2020-07-15 21:17:04 -0400210 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800211 return flags
212
Davide Pesavento97741e72023-08-12 16:36:07 -0400213
214class ClangFlags(GccClangCommonFlags):
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800215 def getGeneralFlags(self, conf):
Davide Pesavento97741e72023-08-12 16:36:07 -0400216 flags = super().getGeneralFlags(conf)
Davide Pesavento5f408ae2020-07-15 21:17:04 -0400217 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev40491df2018-03-09 16:29:52 -0500218 # Bug #4296
Davide Pesavento15a74422022-08-19 15:48:45 -0400219 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
Davide Pesavento97741e72023-08-12 16:36:07 -0400220 flags['CXXFLAGS'] += [
221 ['-isystem', f'{brewdir}/include'], # for Homebrew
222 ['-isystem', '/opt/local/include'], # for MacPorts
223 ]
Davide Pesavento5f408ae2020-07-15 21:17:04 -0400224 elif Utils.unversioned_sys_platform() == 'freebsd':
225 # Bug #4790
226 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Davide Pesavento11c36382024-01-19 00:23:02 -0500227 if self.getCompilerVersion(conf) >= (18, 0, 0):
228 # Bug #5300
229 flags['CXXFLAGS'] += ['-Wno-enum-constexpr-conversion']
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800230 return flags
231
Davide Pesavento97741e72023-08-12 16:36:07 -0400232 __cxxFlags = [
233 '-Wundefined-func-template',
234 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
235 ]
236
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800237 def getDebugFlags(self, conf):
Davide Pesavento97741e72023-08-12 16:36:07 -0400238 flags = super().getDebugFlags(conf)
239 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento11c36382024-01-19 00:23:02 -0500240 # Enable assertions in libc++
241 if self.getCompilerVersion(conf) >= (18, 0, 0):
242 # https://libcxx.llvm.org/Hardening.html
243 flags['DEFINES'] += ['_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE']
244 elif self.getCompilerVersion(conf) >= (15, 0, 0):
245 # https://releases.llvm.org/15.0.0/projects/libcxx/docs/UsingLibcxx.html#enabling-the-safe-libc-mode
246 flags['DEFINES'] += ['_LIBCPP_ENABLE_ASSERTIONS=1']
Davide Pesavento7558ed22024-03-13 18:28:07 -0400247 # Tell libc++ to avoid including transitive headers
248 # https://libcxx.llvm.org/DesignDocs/HeaderRemovalPolicy.html
249 flags['DEFINES'] += ['_LIBCPP_REMOVE_TRANSITIVE_INCLUDES=1']
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800250 return flags
251
252 def getOptimizedFlags(self, conf):
Davide Pesavento97741e72023-08-12 16:36:07 -0400253 flags = super().getOptimizedFlags(conf)
254 flags['CXXFLAGS'] += self.__cxxFlags
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800255 return flags