blob: 39b3025d6e0655bf51c74fead9cdd1d3022b3eff [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 Pesavento762548b2023-03-05 11:02:02 -050030 '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 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': [],
136 'DEFINES': ['BOOST_ASIO_NO_DEPRECATED', 'BOOST_FILESYSTEM_NO_DEPRECATED'],
137 }
Alexander Afanasyev01515792014-12-16 14:20:01 -0800138
Alexander Afanasyev01515792014-12-16 14:20:01 -0800139 def getOptimizedFlags(self, conf):
Davide Pesavento7c02b472015-10-28 20:50:17 +0100140 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Alexander Afanasyev01515792014-12-16 14:20:01 -0800141 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
142
Davide Pesavento12b8aaa2023-08-06 19:18:51 -0400143
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400144class GccClangCommonFlags(CompilerFlags):
Davide Pesaventod06ea052015-10-02 01:48:24 +0200145 """
Davide Pesavento12b8aaa2023-08-06 19:18:51 -0400146 This class defines common flags that work for both gcc and clang compilers.
Davide Pesaventod06ea052015-10-02 01:48:24 +0200147 """
Davide Pesavento12b8aaa2023-08-06 19:18:51 -0400148
Davide Pesavento724d65a2017-06-23 17:14:08 -0400149 def getGeneralFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400150 flags = super().getGeneralFlags(conf)
Davide Pesaventofcd3e442023-03-10 18:44:11 -0500151 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesaventoea5ce492022-09-30 20:25:25 -0400152 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesavento64ae55d2018-12-19 00:25:47 -0500153 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Davide Pesavento724d65a2017-06-23 17:14:08 -0400154 return flags
155
Davide Pesavento03fdfb12023-08-06 19:35:10 -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 Afanasyev01515792014-12-16 14:20:01 -0800168 def getDebugFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -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 Pesavento5fdab132024-01-14 16:30:06 -0500176 # Enable assertions in libstdc++
177 # https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
178 flags['DEFINES'] += ['_GLIBCXX_ASSERTIONS=1']
Alexander Afanasyev01515792014-12-16 14:20:01 -0800179 return flags
180
181 def getOptimizedFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400182 flags = super().getOptimizedFlags(conf)
183 flags['CXXFLAGS'] += ['-O2', '-g1'] + self.__cxxFlags
184 flags['LINKFLAGS'] += self.__linkFlags
Alexander Afanasyev01515792014-12-16 14:20:01 -0800185 return flags
186
Davide Pesavento12b8aaa2023-08-06 19:18:51 -0400187
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400188class 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 Afanasyev01515792014-12-16 14:20:01 -0800199 def getDebugFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400200 flags = super().getDebugFlags(conf)
201 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento541a8222022-03-01 15:08:42 -0500202 if platform.machine() == 'armv7l':
Davide Pesavento71c622b2020-04-24 01:39:01 -0400203 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyev01515792014-12-16 14:20:01 -0800204 return flags
205
Davide Pesaventod06ea052015-10-02 01:48:24 +0200206 def getOptimizedFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400207 flags = super().getOptimizedFlags(conf)
208 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento541a8222022-03-01 15:08:42 -0500209 if platform.machine() == 'armv7l':
Davide Pesavento71c622b2020-04-24 01:39:01 -0400210 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Davide Pesaventod06ea052015-10-02 01:48:24 +0200211 return flags
212
Davide Pesavento12b8aaa2023-08-06 19:18:51 -0400213
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400214class ClangFlags(GccClangCommonFlags):
Alexander Afanasyev01515792014-12-16 14:20:01 -0800215 def getGeneralFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400216 flags = super().getGeneralFlags(conf)
Davide Pesavento71c622b2020-04-24 01:39:01 -0400217 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev5560fd42018-03-07 17:03:03 -0500218 # Bug #4296
Davide Pesaventof6625002022-07-31 17:15:02 -0400219 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400220 flags['CXXFLAGS'] += [
221 ['-isystem', f'{brewdir}/include'], # for Homebrew
222 ['-isystem', '/opt/local/include'], # for MacPorts
223 ]
Davide Pesavento71c622b2020-04-24 01:39:01 -0400224 elif Utils.unversioned_sys_platform() == 'freebsd':
225 # Bug #4790
226 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Davide Pesaventoecfb2172024-01-13 18:26:18 -0500227 if self.getCompilerVersion(conf) >= (18, 0, 0):
228 # Bug #5300
229 flags['CXXFLAGS'] += ['-Wno-enum-constexpr-conversion']
Alexander Afanasyev01515792014-12-16 14:20:01 -0800230 return flags
231
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400232 __cxxFlags = [
233 '-Wundefined-func-template',
234 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
235 ]
236
Alexander Afanasyev01515792014-12-16 14:20:01 -0800237 def getDebugFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400238 flags = super().getDebugFlags(conf)
239 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento5fdab132024-01-14 16:30:06 -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 Pesavento682b2af2024-02-23 21:25:41 -0500247 # Tell libc++ to avoid including transitive headers
248 # https://libcxx.llvm.org/DesignDocs/HeaderRemovalPolicy.html
249 flags['DEFINES'] += ['_LIBCPP_REMOVE_TRANSITIVE_INCLUDES=1']
Davide Pesaventod06ea052015-10-02 01:48:24 +0200250 return flags
251
252 def getOptimizedFlags(self, conf):
Davide Pesavento03fdfb12023-08-06 19:35:10 -0400253 flags = super().getOptimizedFlags(conf)
254 flags['CXXFLAGS'] += self.__cxxFlags
Alexander Afanasyev01515792014-12-16 14:20:01 -0800255 return flags