blob: 98b3a7f2c89551f299ce053efd18d877e9c4475a [file] [log] [blame]
Davide Pesaventob07d7a92020-05-13 23:30:07 -04001import platform
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -05002from waflib import Configure, Logs, Utils
Junxiao Shif7191242015-03-19 05:53:41 -07003
Davide Pesaventodd808b02023-08-06 16:00:02 -04004
Junxiao Shif7191242015-03-19 05:53:41 -07005def options(opt):
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -05006 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesaventob07d7a92020-05-13 23:30:07 -04007 help='Compile in debugging mode with minimal optimizations (-Og)')
Junxiao Shif7191242015-03-19 05:53:41 -07008
Davide Pesaventodd808b02023-08-06 16:00:02 -04009
Junxiao Shif7191242015-03-19 05:53:41 -070010def configure(conf):
Davide Pesaventoc0702702017-08-24 22:04:00 -040011 conf.start_msg('Checking C++ compiler version')
12
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -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 Pesaventoc0702702017-08-24 22:04:00 -040016 errmsg = ''
17 warnmsg = ''
Junxiao Shif7191242015-03-19 05:53:41 -070018 if cxx == 'gcc':
Davide Pesaventob04c52b2022-02-20 15:20:02 -050019 if ccver < (7, 4, 0):
Davide Pesaventoc0702702017-08-24 22:04:00 -040020 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesaventoad266072023-03-07 21:10:55 -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 Afanasyev11e74eb2017-09-21 19:01:54 -040025 conf.flags = GccFlags()
Junxiao Shif7191242015-03-19 05:53:41 -070026 elif cxx == 'clang':
Davide Pesaventob04c52b2022-02-20 15:20:02 -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 Pesaventoad266072023-03-07 21:10:55 -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 Pesaventob04c52b2022-02-20 15:20:02 -050033 'officially supported and may result in build failures.')
Davide Pesaventoad266072023-03-07 21:10:55 -050034 elif ccver < (7, 0, 0):
Davide Pesaventoc0702702017-08-24 22:04:00 -040035 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesaventoad266072023-03-07 21:10:55 -050036 'The minimum supported clang version is 7.0.')
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040037 conf.flags = ClangFlags()
Junxiao Shif7191242015-03-19 05:53:41 -070038 else:
Davide Pesavento423e58a2022-08-12 15:51:42 -040039 warnmsg = f'{cxx} compiler is unsupported'
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040040 conf.flags = CompilerFlags()
Davide Pesaventoc0702702017-08-24 22:04:00 -040041
42 if errmsg:
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -050043 conf.end_msg(ccverstr, color='RED')
Davide Pesaventoc0702702017-08-24 22:04:00 -040044 conf.fatal(errmsg)
45 elif warnmsg:
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -050046 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesaventob07d7a92020-05-13 23:30:07 -040047 Logs.warn('WARNING: ' + warnmsg)
Davide Pesaventoc0702702017-08-24 22:04:00 -040048 else:
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -050049 conf.end_msg(ccverstr)
Junxiao Shif7191242015-03-19 05:53:41 -070050
Davide Pesaventodd808b02023-08-06 16:00:02 -040051 conf.areCustomCxxflagsPresent = len(conf.env.CXXFLAGS) > 0
Junxiao Shif7191242015-03-19 05:53:41 -070052
Davide Pesavento60f8cc12018-05-10 22:05:21 -040053 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040054 generalFlags = conf.flags.getGeneralFlags(conf)
Junxiao Shif7191242015-03-19 05:53:41 -070055 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
56 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
57 conf.env.DEFINES += generalFlags['DEFINES']
58
Davide Pesaventodd808b02023-08-06 16:00:02 -040059
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040060@Configure.conf
61def check_compiler_flags(conf):
Junxiao Shic8a0a252015-10-05 07:25:02 -070062 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Junxiao Shif7191242015-03-19 05:53:41 -070063 # corresponding environment variables are not set.
Junxiao Shic8a0a252015-10-05 07:25:02 -070064 # DEFINES are always applied.
Junxiao Shif7191242015-03-19 05:53:41 -070065 if conf.options.debug:
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040066 extraFlags = conf.flags.getDebugFlags(conf)
67 if conf.areCustomCxxflagsPresent:
Junxiao Shif7191242015-03-19 05:53:41 -070068 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -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))
Junxiao Shif7191242015-03-19 05:53:41 -070073 else:
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040074 extraFlags = conf.flags.getOptimizedFlags(conf)
Junxiao Shif7191242015-03-19 05:53:41 -070075
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040076 if not conf.areCustomCxxflagsPresent:
Junxiao Shif7191242015-03-19 05:53:41 -070077 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Davide Pesavento3e79c9c2015-11-01 20:38:28 +010078 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Junxiao Shif7191242015-03-19 05:53:41 -070079
80 conf.env.DEFINES += extraFlags['DEFINES']
81
Davide Pesaventodd808b02023-08-06 16:00:02 -040082
Junxiao Shif7191242015-03-19 05:53:41 -070083@Configure.conf
84def add_supported_cxxflags(self, cxxflags):
85 """
Davide Pesavento8148cd42023-08-06 14:01:32 -040086 Check which cxxflags are supported by the active compiler and add them to env.CXXFLAGS variable.
Junxiao Shif7191242015-03-19 05:53:41 -070087 """
88 if len(cxxflags) == 0:
89 return
90
91 self.start_msg('Checking supported CXXFLAGS')
92
93 supportedFlags = []
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040094 for flags in cxxflags:
95 flags = Utils.to_list(flags)
96 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
97 supportedFlags += flags
Junxiao Shif7191242015-03-19 05:53:41 -070098
99 self.end_msg(' '.join(supportedFlags))
Davide Pesavento89d91752016-08-14 11:34:09 +0200100 self.env.prepend_value('CXXFLAGS', supportedFlags)
Junxiao Shif7191242015-03-19 05:53:41 -0700101
Davide Pesaventodd808b02023-08-06 16:00:02 -0400102
Junxiao Shif7191242015-03-19 05:53:41 -0700103@Configure.conf
104def add_supported_linkflags(self, linkflags):
105 """
Davide Pesavento8148cd42023-08-06 14:01:32 -0400106 Check which linkflags are supported by the active compiler and add them to env.LINKFLAGS variable.
Junxiao Shif7191242015-03-19 05:53:41 -0700107 """
108 if len(linkflags) == 0:
109 return
110
111 self.start_msg('Checking supported LINKFLAGS')
112
113 supportedFlags = []
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -0400114 for flags in linkflags:
115 flags = Utils.to_list(flags)
116 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
117 supportedFlags += flags
Junxiao Shif7191242015-03-19 05:53:41 -0700118
119 self.end_msg(' '.join(supportedFlags))
Davide Pesavento89d91752016-08-14 11:34:09 +0200120 self.env.prepend_value('LINKFLAGS', supportedFlags)
Junxiao Shif7191242015-03-19 05:53:41 -0700121
122
Davide Pesaventodd808b02023-08-06 16:00:02 -0400123class CompilerFlags:
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -0500124 def getCompilerVersion(self, conf):
125 return tuple(int(i) for i in conf.env.CC_VERSION)
126
Junxiao Shif7191242015-03-19 05:53:41 -0700127 def getGeneralFlags(self, conf):
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100128 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Davide Pesaventof6d75992024-02-27 19:56:06 -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 Pesavento7e9d7e42023-11-11 15:00:03 -0500133 return {
134 'CXXFLAGS': [],
135 'LINKFLAGS': [],
136 'DEFINES': ['BOOST_ASIO_NO_DEPRECATED', 'BOOST_FILESYSTEM_NO_DEPRECATED'],
137 }
Junxiao Shif7191242015-03-19 05:53:41 -0700138
Junxiao Shif7191242015-03-19 05:53:41 -0700139 def getOptimizedFlags(self, conf):
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100140 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Junxiao Shif7191242015-03-19 05:53:41 -0700141 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
142
Davide Pesaventodd808b02023-08-06 16:00:02 -0400143
144class GccClangCommonFlags(CompilerFlags):
Junxiao Shic8a0a252015-10-05 07:25:02 -0700145 """
Davide Pesavento8148cd42023-08-06 14:01:32 -0400146 This class defines common flags that work for both gcc and clang compilers.
Junxiao Shic8a0a252015-10-05 07:25:02 -0700147 """
Davide Pesaventodd808b02023-08-06 16:00:02 -0400148
Davide Pesaventodd0e1952017-06-24 13:37:13 -0400149 def getGeneralFlags(self, conf):
Davide Pesaventodd808b02023-08-06 16:00:02 -0400150 flags = super().getGeneralFlags(conf)
Davide Pesaventob3570c62022-02-19 19:19:00 -0500151 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesavento3d7b0332022-10-05 15:30:02 -0400152 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesaventoe1b18a62018-12-19 01:17:42 -0500153 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Davide Pesaventodd0e1952017-06-24 13:37:13 -0400154 return flags
155
Davide Pesaventodd808b02023-08-06 16:00:02 -0400156 __cxxFlags = [
157 '-fdiagnostics-color',
158 '-Wall',
159 '-Wextra',
160 '-Wpedantic',
161 '-Wenum-conversion',
162 '-Wextra-semi',
Davide Pesaventodd808b02023-08-06 16:00:02 -0400163 '-Wno-unused-parameter',
164 ]
165 __linkFlags = ['-Wl,-O1']
166
Junxiao Shif7191242015-03-19 05:53:41 -0700167 def getDebugFlags(self, conf):
Davide Pesaventodd808b02023-08-06 16:00:02 -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 Pesaventoa9d7db12024-01-15 00:30:05 -0500175 # Enable assertions in libstdc++
176 # https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
177 flags['DEFINES'] += ['_GLIBCXX_ASSERTIONS=1']
Junxiao Shif7191242015-03-19 05:53:41 -0700178 return flags
179
180 def getOptimizedFlags(self, conf):
Davide Pesaventodd808b02023-08-06 16:00:02 -0400181 flags = super().getOptimizedFlags(conf)
182 flags['CXXFLAGS'] += ['-O2', '-g1'] + self.__cxxFlags
183 flags['LINKFLAGS'] += self.__linkFlags
Junxiao Shif7191242015-03-19 05:53:41 -0700184 return flags
185
Davide Pesaventodd808b02023-08-06 16:00:02 -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
Junxiao Shif7191242015-03-19 05:53:41 -0700198 def getDebugFlags(self, conf):
Davide Pesaventodd808b02023-08-06 16:00:02 -0400199 flags = super().getDebugFlags(conf)
200 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventob04c52b2022-02-20 15:20:02 -0500201 if platform.machine() == 'armv7l':
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400202 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Junxiao Shic8a0a252015-10-05 07:25:02 -0700203 return flags
204
205 def getOptimizedFlags(self, conf):
Davide Pesaventodd808b02023-08-06 16:00:02 -0400206 flags = super().getOptimizedFlags(conf)
207 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventob04c52b2022-02-20 15:20:02 -0500208 if platform.machine() == 'armv7l':
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400209 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Junxiao Shif7191242015-03-19 05:53:41 -0700210 return flags
211
Davide Pesaventodd808b02023-08-06 16:00:02 -0400212
213class ClangFlags(GccClangCommonFlags):
Junxiao Shif7191242015-03-19 05:53:41 -0700214 def getGeneralFlags(self, conf):
Davide Pesaventodd808b02023-08-06 16:00:02 -0400215 flags = super().getGeneralFlags(conf)
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400216 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -0500217 # Bug #4296
Davide Pesavento423e58a2022-08-12 15:51:42 -0400218 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
Davide Pesaventodd808b02023-08-06 16:00:02 -0400219 flags['CXXFLAGS'] += [
220 ['-isystem', f'{brewdir}/include'], # for Homebrew
221 ['-isystem', '/opt/local/include'], # for MacPorts
222 ]
Davide Pesaventob07d7a92020-05-13 23:30:07 -0400223 elif Utils.unversioned_sys_platform() == 'freebsd':
224 # Bug #4790
225 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Davide Pesavento0a675562024-01-13 21:54:52 -0500226 if self.getCompilerVersion(conf) >= (18, 0, 0):
227 # Bug #5300
228 flags['CXXFLAGS'] += ['-Wno-enum-constexpr-conversion']
Junxiao Shif7191242015-03-19 05:53:41 -0700229 return flags
230
Davide Pesaventodd808b02023-08-06 16:00:02 -0400231 __cxxFlags = [
232 '-Wundefined-func-template',
233 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
234 ]
235
Junxiao Shif7191242015-03-19 05:53:41 -0700236 def getDebugFlags(self, conf):
Davide Pesaventodd808b02023-08-06 16:00:02 -0400237 flags = super().getDebugFlags(conf)
238 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventoa9d7db12024-01-15 00:30:05 -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 Pesaventof6d75992024-02-27 19:56:06 -0500246 # Tell libc++ to avoid including transitive headers
247 # https://libcxx.llvm.org/DesignDocs/HeaderRemovalPolicy.html
248 flags['DEFINES'] += ['_LIBCPP_REMOVE_TRANSITIVE_INCLUDES=1']
Junxiao Shic8a0a252015-10-05 07:25:02 -0700249 return flags
250
251 def getOptimizedFlags(self, conf):
Davide Pesaventodd808b02023-08-06 16:00:02 -0400252 flags = super().getOptimizedFlags(conf)
253 flags['CXXFLAGS'] += self.__cxxFlags
Junxiao Shif7191242015-03-19 05:53:41 -0700254 return flags