blob: 681913f9c52eaeacf415bf0c734bb9f859c6f7a2 [file] [log] [blame]
Davide Pesavento3d01fa32021-10-03 17:13:38 -04001import platform
Alexander Afanasyev08d18742018-03-15 16:31:28 -04002from waflib import Configure, Logs, Utils
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -07003
Davide Pesaventoaf340ea2023-08-10 20:46:06 -04004
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -07005def options(opt):
Alexander Afanasyev08d18742018-03-15 16:31:28 -04006 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesavento3d01fa32021-10-03 17:13:38 -04007 help='Compile in debugging mode with minimal optimizations (-Og)')
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -07008
Davide Pesaventoaf340ea2023-08-10 20:46:06 -04009
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070010def configure(conf):
Alexander Afanasyev08d18742018-03-15 16:31:28 -040011 conf.start_msg('Checking C++ compiler version')
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070012
Alexander Afanasyev08d18742018-03-15 16:31:28 -040013 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)
16 errmsg = ''
17 warnmsg = ''
18 if cxx == 'gcc':
Davide Pesavento7f27ec12022-03-10 20:10:54 -050019 if ccver < (7, 4, 0):
Alexander Afanasyev08d18742018-03-15 16:31:28 -040020 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesaventoaf340ea2023-08-10 20:46:06 -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 Afanasyev08d18742018-03-15 16:31:28 -040025 conf.flags = GccFlags()
26 elif cxx == 'clang':
Davide Pesavento7f27ec12022-03-10 20:10:54 -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 Pesaventoaf340ea2023-08-10 20:46:06 -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 Pesavento7f27ec12022-03-10 20:10:54 -050033 'officially supported and may result in build failures.')
Davide Pesaventoaf340ea2023-08-10 20:46:06 -040034 elif ccver < (7, 0, 0):
Alexander Afanasyev08d18742018-03-15 16:31:28 -040035 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesaventoaf340ea2023-08-10 20:46:06 -040036 'The minimum supported clang version is 7.0.')
Alexander Afanasyev08d18742018-03-15 16:31:28 -040037 conf.flags = ClangFlags()
38 else:
Davide Pesavento423553e2022-08-19 20:46:06 -040039 warnmsg = f'{cxx} compiler is unsupported'
Alexander Afanasyev08d18742018-03-15 16:31:28 -040040 conf.flags = CompilerFlags()
41
42 if errmsg:
43 conf.end_msg(ccverstr, color='RED')
44 conf.fatal(errmsg)
45 elif warnmsg:
46 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento3d01fa32021-10-03 17:13:38 -040047 Logs.warn('WARNING: ' + warnmsg)
Alexander Afanasyev08d18742018-03-15 16:31:28 -040048 else:
49 conf.end_msg(ccverstr)
50
Davide Pesaventoaf340ea2023-08-10 20:46:06 -040051 conf.areCustomCxxflagsPresent = len(conf.env.CXXFLAGS) > 0
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080052
Davide Pesavento90034832018-05-30 10:10:31 -040053 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyev08d18742018-03-15 16:31:28 -040054 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080055 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
56 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
57 conf.env.DEFINES += generalFlags['DEFINES']
58
Davide Pesaventoaf340ea2023-08-10 20:46:06 -040059
Alexander Afanasyev08d18742018-03-15 16:31:28 -040060@Configure.conf
61def check_compiler_flags(conf):
Alexander Afanasyevc7c99002015-10-09 17:27:30 -070062 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080063 # corresponding environment variables are not set.
Alexander Afanasyevc7c99002015-10-09 17:27:30 -070064 # DEFINES are always applied.
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070065 if conf.options.debug:
Alexander Afanasyev08d18742018-03-15 16:31:28 -040066 extraFlags = conf.flags.getDebugFlags(conf)
67 if conf.areCustomCxxflagsPresent:
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080068 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev08d18742018-03-15 16:31:28 -040069 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 Afanasyev4ffcff22014-09-02 15:39:20 -070073 else:
Alexander Afanasyev08d18742018-03-15 16:31:28 -040074 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070075
Alexander Afanasyev08d18742018-03-15 16:31:28 -040076 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080077 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -080078 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080079
80 conf.env.DEFINES += extraFlags['DEFINES']
Shock Jiang2d8483c2014-10-30 10:15:37 -070081
Davide Pesaventoaf340ea2023-08-10 20:46:06 -040082
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070083@Configure.conf
84def add_supported_cxxflags(self, cxxflags):
85 """
Davide Pesaventoaf340ea2023-08-10 20:46:06 -040086 Check which cxxflags are supported by the active compiler and add them to env.CXXFLAGS variable.
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070087 """
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080088 if len(cxxflags) == 0:
89 return
90
Shock Jiang2d8483c2014-10-30 10:15:37 -070091 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070092
93 supportedFlags = []
Alexander Afanasyev08d18742018-03-15 16:31:28 -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 Afanasyev4ffcff22014-09-02 15:39:20 -070098
99 self.end_msg(' '.join(supportedFlags))
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -0800100 self.env.prepend_value('CXXFLAGS', supportedFlags)
Shock Jiang2d8483c2014-10-30 10:15:37 -0700101
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400102
Shock Jiang2d8483c2014-10-30 10:15:37 -0700103@Configure.conf
104def add_supported_linkflags(self, linkflags):
105 """
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400106 Check which linkflags are supported by the active compiler and add them to env.LINKFLAGS variable.
Shock Jiang2d8483c2014-10-30 10:15:37 -0700107 """
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800108 if len(linkflags) == 0:
109 return
110
Shock Jiang2d8483c2014-10-30 10:15:37 -0700111 self.start_msg('Checking supported LINKFLAGS')
112
113 supportedFlags = []
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400114 for flags in linkflags:
115 flags = Utils.to_list(flags)
116 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
117 supportedFlags += flags
Shock Jiang2d8483c2014-10-30 10:15:37 -0700118
119 self.end_msg(' '.join(supportedFlags))
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -0800120 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800121
122
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400123class CompilerFlags:
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400124 def getCompilerVersion(self, conf):
125 return tuple(int(i) for i in conf.env.CC_VERSION)
126
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800127 def getGeneralFlags(self, conf):
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -0800128 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Davide Pesavento5ca40bb2024-03-12 20:50:42 -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 Pesaventoce6f03b2024-02-10 17:04:11 -0500133 return {
134 'CXXFLAGS': [],
135 'LINKFLAGS': [],
136 'DEFINES': ['BOOST_FILESYSTEM_NO_DEPRECATED'],
137 }
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800138
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800139 def getOptimizedFlags(self, conf):
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -0800140 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800141 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
142
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400143
144class GccClangCommonFlags(CompilerFlags):
Alexander Afanasyevc7c99002015-10-09 17:27:30 -0700145 """
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400146 This class defines common flags that work for both gcc and clang compilers.
Alexander Afanasyevc7c99002015-10-09 17:27:30 -0700147 """
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400148
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400149 def getGeneralFlags(self, conf):
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400150 flags = super().getGeneralFlags(conf)
Davide Pesavento38fd3982022-04-18 22:22:02 -0400151 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesaventob2034db2022-11-16 17:21:22 -0500152 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesaventoc7888e62019-01-29 15:20:32 -0500153 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400154 return flags
155
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400156 __cxxFlags = [
157 '-fdiagnostics-color',
158 '-Wall',
159 '-Wextra',
160 '-Wpedantic',
161 '-Wenum-conversion',
162 '-Wextra-semi',
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400163 '-Wno-unused-parameter',
164 ]
165 __linkFlags = ['-Wl,-O1']
166
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800167 def getDebugFlags(self, conf):
Davide Pesaventoaf340ea2023-08-10 20:46:06 -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 Pesaventoce6f03b2024-02-10 17:04:11 -0500175 # Enable assertions in libstdc++
176 # https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
177 flags['DEFINES'] += ['_GLIBCXX_ASSERTIONS=1']
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800178 return flags
179
180 def getOptimizedFlags(self, conf):
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400181 flags = super().getOptimizedFlags(conf)
182 flags['CXXFLAGS'] += ['-O2', '-g1'] + self.__cxxFlags
183 flags['LINKFLAGS'] += self.__linkFlags
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800184 return flags
185
Davide Pesaventoaf340ea2023-08-10 20:46:06 -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
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800198 def getDebugFlags(self, conf):
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400199 flags = super().getDebugFlags(conf)
200 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento7f27ec12022-03-10 20:10:54 -0500201 if platform.machine() == 'armv7l':
Davide Pesavento3d01fa32021-10-03 17:13:38 -0400202 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevc7c99002015-10-09 17:27:30 -0700203 return flags
204
205 def getOptimizedFlags(self, conf):
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400206 flags = super().getOptimizedFlags(conf)
207 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento7f27ec12022-03-10 20:10:54 -0500208 if platform.machine() == 'armv7l':
Davide Pesavento3d01fa32021-10-03 17:13:38 -0400209 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800210 return flags
211
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400212
213class ClangFlags(GccClangCommonFlags):
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800214 def getGeneralFlags(self, conf):
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400215 flags = super().getGeneralFlags(conf)
Davide Pesavento3d01fa32021-10-03 17:13:38 -0400216 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400217 # Bug #4296
Davide Pesavento423553e2022-08-19 20:46:06 -0400218 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400219 flags['CXXFLAGS'] += [
220 ['-isystem', f'{brewdir}/include'], # for Homebrew
221 ['-isystem', '/opt/local/include'], # for MacPorts
222 ]
Davide Pesavento3d01fa32021-10-03 17:13:38 -0400223 elif Utils.unversioned_sys_platform() == 'freebsd':
224 # Bug #4790
225 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Davide Pesaventoce6f03b2024-02-10 17:04:11 -0500226 if self.getCompilerVersion(conf) >= (18, 0, 0):
227 # Bug #5300
228 flags['CXXFLAGS'] += ['-Wno-enum-constexpr-conversion']
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800229 return flags
230
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400231 __cxxFlags = [
232 '-Wundefined-func-template',
233 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
234 ]
235
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800236 def getDebugFlags(self, conf):
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400237 flags = super().getDebugFlags(conf)
238 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventoce6f03b2024-02-10 17:04:11 -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 Pesavento5ca40bb2024-03-12 20:50:42 -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 Afanasyevc7c99002015-10-09 17:27:30 -0700249 return flags
250
251 def getOptimizedFlags(self, conf):
Davide Pesaventoaf340ea2023-08-10 20:46:06 -0400252 flags = super().getOptimizedFlags(conf)
253 flags['CXXFLAGS'] += self.__cxxFlags
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800254 return flags