blob: a765b4d64c53bc75dd3215dd7657bd15d484e0c1 [file] [log] [blame]
Davide Pesavento71c622b2020-04-24 01:39:01 -04001import platform
Alexander Afanasyev5560fd42018-03-07 17:03:03 -05002from waflib import Configure, Logs, Utils
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -07003
Davide Pesavento12b8aaa2023-08-06 19:18:51 -04004
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -07005def options(opt):
Alexander Afanasyev5560fd42018-03-07 17:03:03 -05006 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesavento71c622b2020-04-24 01:39:01 -04007 help='Compile in debugging mode with minimal optimizations (-Og)')
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -07008
Davide Pesavento12b8aaa2023-08-06 19:18:51 -04009
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070010def configure(conf):
Davide Pesavento88a0d812017-08-19 21:31:42 -040011 conf.start_msg('Checking C++ compiler version')
12
Alexander Afanasyev5560fd42018-03-07 17:03:03 -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 Pesavento88a0d812017-08-19 21:31:42 -040016 errmsg = ''
17 warnmsg = ''
Alexander Afanasyev01515792014-12-16 14:20:01 -080018 if cxx == 'gcc':
Davide Pesavento541a8222022-03-01 15:08:42 -050019 if ccver < (7, 4, 0):
Davide Pesavento88a0d812017-08-19 21:31:42 -040020 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesavento762548b2023-03-05 11:02:02 -050021 '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 Afanasyevb82d8c32017-09-21 11:35:07 -040025 conf.flags = GccFlags()
Alexander Afanasyev01515792014-12-16 14:20:01 -080026 elif cxx == 'clang':
Davide Pesavento541a8222022-03-01 15:08:42 -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 Pesavento2be774b2024-06-07 17:25:31 -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 Pesavento541a8222022-03-01 15:08:42 -050033 'officially supported and may result in build failures.')
Davide Pesavento762548b2023-03-05 11:02:02 -050034 elif ccver < (7, 0, 0):
Davide Pesavento88a0d812017-08-19 21:31:42 -040035 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesavento762548b2023-03-05 11:02:02 -050036 'The minimum supported clang version is 7.0.')
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040037 conf.flags = ClangFlags()
Alexander Afanasyev01515792014-12-16 14:20:01 -080038 else:
Davide Pesaventof6625002022-07-31 17:15:02 -040039 warnmsg = f'{cxx} compiler is unsupported'
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040040 conf.flags = CompilerFlags()
Davide Pesavento88a0d812017-08-19 21:31:42 -040041
42 if errmsg:
Alexander Afanasyev5560fd42018-03-07 17:03:03 -050043 conf.end_msg(ccverstr, color='RED')
Davide Pesavento88a0d812017-08-19 21:31:42 -040044 conf.fatal(errmsg)
45 elif warnmsg:
Alexander Afanasyev5560fd42018-03-07 17:03:03 -050046 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento71c622b2020-04-24 01:39:01 -040047 Logs.warn('WARNING: ' + warnmsg)
Davide Pesavento88a0d812017-08-19 21:31:42 -040048 else:
Alexander Afanasyev5560fd42018-03-07 17:03:03 -050049 conf.end_msg(ccverstr)
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -070050
Davide Pesavento12b8aaa2023-08-06 19:18:51 -040051 conf.areCustomCxxflagsPresent = len(conf.env.CXXFLAGS) > 0
Alexander Afanasyev01515792014-12-16 14:20:01 -080052
Davide Pesavento1fd00242018-05-20 00:11:01 -040053 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040054 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyev01515792014-12-16 14:20:01 -080055 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
56 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
57 conf.env.DEFINES += generalFlags['DEFINES']
58
Davide Pesavento12b8aaa2023-08-06 19:18:51 -040059
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040060@Configure.conf
61def check_compiler_flags(conf):
Davide Pesaventod06ea052015-10-02 01:48:24 +020062 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Alexander Afanasyev01515792014-12-16 14:20:01 -080063 # corresponding environment variables are not set.
Davide Pesaventod06ea052015-10-02 01:48:24 +020064 # DEFINES are always applied.
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070065 if conf.options.debug:
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040066 extraFlags = conf.flags.getDebugFlags(conf)
67 if conf.areCustomCxxflagsPresent:
Alexander Afanasyev01515792014-12-16 14:20:01 -080068 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev5560fd42018-03-07 17:03:03 -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 Afanasyev5e1288e2014-03-28 11:11:48 -070073 else:
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040074 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070075
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040076 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyev01515792014-12-16 14:20:01 -080077 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Davide Pesavento7c02b472015-10-28 20:50:17 +010078 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Alexander Afanasyev01515792014-12-16 14:20:01 -080079
80 conf.env.DEFINES += extraFlags['DEFINES']
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020081
Davide Pesavento12b8aaa2023-08-06 19:18:51 -040082
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070083@Configure.conf
84def add_supported_cxxflags(self, cxxflags):
85 """
Davide Pesavento12b8aaa2023-08-06 19:18:51 -040086 Check which cxxflags are supported by the active compiler and add them to env.CXXFLAGS variable.
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070087 """
Alexander Afanasyev01515792014-12-16 14:20:01 -080088 if len(cxxflags) == 0:
89 return
90
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020091 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070092
93 supportedFlags = []
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -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 Afanasyev5e1288e2014-03-28 11:11:48 -070098
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -070099 self.end_msg(' '.join(supportedFlags))
Davide Pesavento1349d2d2016-08-09 06:43:57 +0200100 self.env.prepend_value('CXXFLAGS', supportedFlags)
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +0200101
Davide Pesavento12b8aaa2023-08-06 19:18:51 -0400102
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +0200103@Configure.conf
104def add_supported_linkflags(self, linkflags):
105 """
Davide Pesavento12b8aaa2023-08-06 19:18:51 -0400106 Check which linkflags are supported by the active compiler and add them to env.LINKFLAGS variable.
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +0200107 """
Alexander Afanasyev01515792014-12-16 14:20:01 -0800108 if len(linkflags) == 0:
109 return
110
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +0200111 self.start_msg('Checking supported LINKFLAGS')
112
113 supportedFlags = []
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -0400114 for flags in linkflags:
115 flags = Utils.to_list(flags)
116 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
117 supportedFlags += flags
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +0200118
119 self.end_msg(' '.join(supportedFlags))
Davide Pesavento1349d2d2016-08-09 06:43:57 +0200120 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyev01515792014-12-16 14:20:01 -0800121
122
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400123class CompilerFlags:
Alexander Afanasyev5560fd42018-03-07 17:03:03 -0500124 def getCompilerVersion(self, conf):
125 return tuple(int(i) for i in conf.env.CC_VERSION)
126
Alexander Afanasyev01515792014-12-16 14:20:01 -0800127 def getGeneralFlags(self, conf):
Davide Pesavento7c02b472015-10-28 20:50:17 +0100128 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Davide Pesavento682b2af2024-02-23 21:25:41 -0500129 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 Pesavento2f46d652023-11-09 23:40:01 -0500133 return {
134 'CXXFLAGS': [],
135 'LINKFLAGS': [],
Davide Pesavento80f67552024-06-08 18:50:15 -0400136 'DEFINES': [
137 'BOOST_ASIO_NO_DEPRECATED',
138 'BOOST_FILESYSTEM_NO_DEPRECATED',
139 'BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING',
140 'BOOST_MULTI_INDEX_ENABLE_SAFE_MODE',
141 ],
Davide Pesavento2f46d652023-11-09 23:40:01 -0500142 }
Alexander Afanasyev01515792014-12-16 14:20:01 -0800143
Alexander Afanasyev01515792014-12-16 14:20:01 -0800144 def getOptimizedFlags(self, conf):
Davide Pesavento7c02b472015-10-28 20:50:17 +0100145 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Alexander Afanasyev01515792014-12-16 14:20:01 -0800146 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
147
Davide Pesavento12b8aaa2023-08-06 19:18:51 -0400148
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400149class GccClangCommonFlags(CompilerFlags):
Davide Pesaventod06ea052015-10-02 01:48:24 +0200150 """
Davide Pesavento12b8aaa2023-08-06 19:18:51 -0400151 This class defines common flags that work for both gcc and clang compilers.
Davide Pesaventod06ea052015-10-02 01:48:24 +0200152 """
Davide Pesavento12b8aaa2023-08-06 19:18:51 -0400153
Davide Pesavento724d65a2017-06-23 17:14:08 -0400154 def getGeneralFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400155 flags = super().getGeneralFlags(conf)
Davide Pesaventofcd3e442023-03-10 18:44:11 -0500156 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesaventoea5ce492022-09-30 20:25:25 -0400157 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesavento64ae55d2018-12-19 00:25:47 -0500158 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Davide Pesavento724d65a2017-06-23 17:14:08 -0400159 return flags
160
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400161 __cxxFlags = [
162 '-fdiagnostics-color',
163 '-Wall',
164 '-Wextra',
165 '-Wpedantic',
166 '-Wenum-conversion',
167 '-Wextra-semi',
168 '-Wnon-virtual-dtor',
169 '-Wno-unused-parameter',
170 ]
171 __linkFlags = ['-Wl,-O1']
172
Alexander Afanasyev01515792014-12-16 14:20:01 -0800173 def getDebugFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400174 flags = super().getDebugFlags(conf)
175 flags['CXXFLAGS'] += ['-Og', '-g'] + self.__cxxFlags + [
176 '-Werror',
177 '-Wno-error=deprecated-declarations', # Bug #3795
178 '-Wno-error=maybe-uninitialized', # Bug #1615
179 ]
180 flags['LINKFLAGS'] += self.__linkFlags
Davide Pesavento5fdab132024-01-14 16:30:06 -0500181 # Enable assertions in libstdc++
182 # https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
183 flags['DEFINES'] += ['_GLIBCXX_ASSERTIONS=1']
Alexander Afanasyev01515792014-12-16 14:20:01 -0800184 return flags
185
186 def getOptimizedFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400187 flags = super().getOptimizedFlags(conf)
188 flags['CXXFLAGS'] += ['-O2', '-g1'] + self.__cxxFlags
189 flags['LINKFLAGS'] += self.__linkFlags
Alexander Afanasyev01515792014-12-16 14:20:01 -0800190 return flags
191
Davide Pesavento12b8aaa2023-08-06 19:18:51 -0400192
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400193class GccFlags(GccClangCommonFlags):
194 __cxxFlags = [
195 '-Wcatch-value=2',
196 '-Wcomma-subscript', # enabled by default in C++20
197 '-Wduplicated-branches',
198 '-Wduplicated-cond',
199 '-Wlogical-op',
200 '-Wredundant-tags',
201 '-Wvolatile', # enabled by default in C++20
202 ]
203
Alexander Afanasyev01515792014-12-16 14:20:01 -0800204 def getDebugFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400205 flags = super().getDebugFlags(conf)
206 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento541a8222022-03-01 15:08:42 -0500207 if platform.machine() == 'armv7l':
Davide Pesavento71c622b2020-04-24 01:39:01 -0400208 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyev01515792014-12-16 14:20:01 -0800209 return flags
210
Davide Pesaventod06ea052015-10-02 01:48:24 +0200211 def getOptimizedFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400212 flags = super().getOptimizedFlags(conf)
213 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento541a8222022-03-01 15:08:42 -0500214 if platform.machine() == 'armv7l':
Davide Pesavento71c622b2020-04-24 01:39:01 -0400215 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Davide Pesaventod06ea052015-10-02 01:48:24 +0200216 return flags
217
Davide Pesavento12b8aaa2023-08-06 19:18:51 -0400218
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400219class ClangFlags(GccClangCommonFlags):
Alexander Afanasyev01515792014-12-16 14:20:01 -0800220 def getGeneralFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400221 flags = super().getGeneralFlags(conf)
Davide Pesavento71c622b2020-04-24 01:39:01 -0400222 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev5560fd42018-03-07 17:03:03 -0500223 # Bug #4296
Davide Pesaventof6625002022-07-31 17:15:02 -0400224 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400225 flags['CXXFLAGS'] += [
226 ['-isystem', f'{brewdir}/include'], # for Homebrew
227 ['-isystem', '/opt/local/include'], # for MacPorts
228 ]
Davide Pesavento71c622b2020-04-24 01:39:01 -0400229 elif Utils.unversioned_sys_platform() == 'freebsd':
230 # Bug #4790
231 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Davide Pesaventoecfb2172024-01-13 18:26:18 -0500232 if self.getCompilerVersion(conf) >= (18, 0, 0):
233 # Bug #5300
234 flags['CXXFLAGS'] += ['-Wno-enum-constexpr-conversion']
Alexander Afanasyev01515792014-12-16 14:20:01 -0800235 return flags
236
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400237 __cxxFlags = [
238 '-Wundefined-func-template',
239 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
240 ]
241
Alexander Afanasyev01515792014-12-16 14:20:01 -0800242 def getDebugFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400243 flags = super().getDebugFlags(conf)
244 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento5fdab132024-01-14 16:30:06 -0500245 # Enable assertions in libc++
246 if self.getCompilerVersion(conf) >= (18, 0, 0):
247 # https://libcxx.llvm.org/Hardening.html
248 flags['DEFINES'] += ['_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE']
249 elif self.getCompilerVersion(conf) >= (15, 0, 0):
250 # https://releases.llvm.org/15.0.0/projects/libcxx/docs/UsingLibcxx.html#enabling-the-safe-libc-mode
251 flags['DEFINES'] += ['_LIBCPP_ENABLE_ASSERTIONS=1']
Davide Pesavento682b2af2024-02-23 21:25:41 -0500252 # Tell libc++ to avoid including transitive headers
253 # https://libcxx.llvm.org/DesignDocs/HeaderRemovalPolicy.html
254 flags['DEFINES'] += ['_LIBCPP_REMOVE_TRANSITIVE_INCLUDES=1']
Davide Pesaventod06ea052015-10-02 01:48:24 +0200255 return flags
256
257 def getOptimizedFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400258 flags = super().getOptimizedFlags(conf)
259 flags['CXXFLAGS'] += self.__cxxFlags
Alexander Afanasyev01515792014-12-16 14:20:01 -0800260 return flags