blob: 8bbc29a52358bc171a9031b5cf84f483797fac11 [file] [log] [blame]
Davide Pesavento333e3eb2021-10-12 19:30:51 -04001import platform
Davide Pesavento98ff2bc2018-05-24 00:10:31 -04002from waflib import Configure, Logs, Utils
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -07003
Davide Pesaventofea0f092023-08-09 13:18:02 -04004
Shuo Chen478204c2014-03-18 18:27:04 -07005def options(opt):
Davide Pesavento98ff2bc2018-05-24 00:10:31 -04006 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesavento333e3eb2021-10-12 19:30:51 -04007 help='Compile in debugging mode with minimal optimizations (-Og)')
Shuo Chen478204c2014-03-18 18:27:04 -07008
Davide Pesaventofea0f092023-08-09 13:18:02 -04009
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070010def configure(conf):
Davide Pesaventob545aac2017-09-22 23:54:10 -040011 conf.start_msg('Checking C++ compiler version')
12
Davide Pesavento98ff2bc2018-05-24 00:10:31 -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)
Davide Pesaventob545aac2017-09-22 23:54:10 -040016 errmsg = ''
17 warnmsg = ''
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050018 if cxx == 'gcc':
Davide Pesaventoe4b74bd2022-03-09 18:56:37 -050019 if ccver < (7, 4, 0):
Davide Pesaventob545aac2017-09-22 23:54:10 -040020 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesavento60b52cf2023-04-26 15:27:02 -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 Afanasyevbb058c02018-02-15 22:49:24 +000025 conf.flags = GccFlags()
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050026 elif cxx == 'clang':
Davide Pesaventoe4b74bd2022-03-09 18:56:37 -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 Pesaventof6ea9312024-07-21 13:50:25 -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 Pesaventoe4b74bd2022-03-09 18:56:37 -050033 'officially supported and may result in build failures.')
Davide Pesavento60b52cf2023-04-26 15:27:02 -040034 elif ccver < (7, 0, 0):
Davide Pesaventob545aac2017-09-22 23:54:10 -040035 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesavento60b52cf2023-04-26 15:27:02 -040036 'The minimum supported clang version is 7.0.')
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000037 conf.flags = ClangFlags()
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050038 else:
Davide Pesavento1d484532022-08-19 22:08:31 -040039 warnmsg = f'{cxx} compiler is unsupported'
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000040 conf.flags = CompilerFlags()
Davide Pesaventob545aac2017-09-22 23:54:10 -040041
42 if errmsg:
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040043 conf.end_msg(ccverstr, color='RED')
Davide Pesaventob545aac2017-09-22 23:54:10 -040044 conf.fatal(errmsg)
45 elif warnmsg:
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040046 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento333e3eb2021-10-12 19:30:51 -040047 Logs.warn('WARNING: ' + warnmsg)
Davide Pesaventob545aac2017-09-22 23:54:10 -040048 else:
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040049 conf.end_msg(ccverstr)
Shuo Chen478204c2014-03-18 18:27:04 -070050
Davide Pesaventofea0f092023-08-09 13:18:02 -040051 conf.areCustomCxxflagsPresent = len(conf.env.CXXFLAGS) > 0
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050052
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040053 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000054 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050055 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
56 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
57 conf.env.DEFINES += generalFlags['DEFINES']
58
Davide Pesaventofea0f092023-08-09 13:18:02 -040059
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000060@Configure.conf
61def check_compiler_flags(conf):
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050062 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
63 # corresponding environment variables are not set.
64 # DEFINES are always applied.
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070065 if conf.options.debug:
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000066 extraFlags = conf.flags.getDebugFlags(conf)
67 if conf.areCustomCxxflagsPresent:
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050068 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Davide Pesavento98ff2bc2018-05-24 00:10:31 -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 Afanasyev3fd14f02014-03-26 14:34:39 -070073 else:
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000074 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070075
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000076 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050077 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
78 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
79
80 conf.env.DEFINES += extraFlags['DEFINES']
Wentao Shanga8f3c402014-10-30 14:03:27 -070081
Davide Pesaventofea0f092023-08-09 13:18:02 -040082
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070083@Configure.conf
84def add_supported_cxxflags(self, cxxflags):
85 """
Davide Pesaventofea0f092023-08-09 13:18:02 -040086 Check which cxxflags are supported by the active compiler and add them to env.CXXFLAGS variable.
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070087 """
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050088 if len(cxxflags) == 0:
89 return
90
Wentao Shanga8f3c402014-10-30 14:03:27 -070091 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070092
93 supportedFlags = []
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000094 for flags in cxxflags:
95 flags = Utils.to_list(flags)
96 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
97 supportedFlags += flags
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070098
Shuo Chen478204c2014-03-18 18:27:04 -070099 self.end_msg(' '.join(supportedFlags))
Junxiao Shie1801312017-07-22 19:16:49 +0000100 self.env.prepend_value('CXXFLAGS', supportedFlags)
Wentao Shanga8f3c402014-10-30 14:03:27 -0700101
Davide Pesaventofea0f092023-08-09 13:18:02 -0400102
Wentao Shanga8f3c402014-10-30 14:03:27 -0700103@Configure.conf
104def add_supported_linkflags(self, linkflags):
105 """
Davide Pesaventofea0f092023-08-09 13:18:02 -0400106 Check which linkflags are supported by the active compiler and add them to env.LINKFLAGS variable.
Wentao Shanga8f3c402014-10-30 14:03:27 -0700107 """
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500108 if len(linkflags) == 0:
109 return
110
Wentao Shanga8f3c402014-10-30 14:03:27 -0700111 self.start_msg('Checking supported LINKFLAGS')
112
113 supportedFlags = []
Alexander Afanasyevbb058c02018-02-15 22:49:24 +0000114 for flags in linkflags:
115 flags = Utils.to_list(flags)
116 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
117 supportedFlags += flags
Wentao Shanga8f3c402014-10-30 14:03:27 -0700118
119 self.end_msg(' '.join(supportedFlags))
Junxiao Shie1801312017-07-22 19:16:49 +0000120 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500121
122
Davide Pesaventofea0f092023-08-09 13:18:02 -0400123class CompilerFlags:
Davide Pesavento98ff2bc2018-05-24 00:10:31 -0400124 def getCompilerVersion(self, conf):
125 return tuple(int(i) for i in conf.env.CC_VERSION)
126
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500127 def getGeneralFlags(self, conf):
128 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Davide Pesaventof6ea9312024-07-21 13:50:25 -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 Pesavento164ae3b2023-11-11 22:00:23 -0500133 return {
134 'CXXFLAGS': [],
135 'LINKFLAGS': [],
136 'DEFINES': ['BOOST_ASIO_NO_DEPRECATED', 'BOOST_FILESYSTEM_NO_DEPRECATED'],
137 }
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500138
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500139 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 Pesaventofea0f092023-08-09 13:18:02 -0400143
144class GccClangCommonFlags(CompilerFlags):
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500145 """
Davide Pesaventofea0f092023-08-09 13:18:02 -0400146 This class defines common flags that work for both gcc and clang compilers.
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500147 """
Davide Pesaventofea0f092023-08-09 13:18:02 -0400148
Junxiao Shie1801312017-07-22 19:16:49 +0000149 def getGeneralFlags(self, conf):
Davide Pesaventofea0f092023-08-09 13:18:02 -0400150 flags = super().getGeneralFlags(conf)
Davide Pesavento11904062022-04-14 22:33:28 -0400151 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesaventoc2414752022-11-16 16:56:45 -0500152 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesaventoed2f4762019-01-25 00:40:46 -0500153 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Junxiao Shie1801312017-07-22 19:16:49 +0000154 return flags
155
Davide Pesaventofea0f092023-08-09 13:18:02 -0400156 __cxxFlags = [
157 '-fdiagnostics-color',
158 '-Wall',
159 '-Wextra',
160 '-Wpedantic',
161 '-Wenum-conversion',
162 '-Wextra-semi',
Davide Pesaventofea0f092023-08-09 13:18:02 -0400163 '-Wno-unused-parameter',
164 ]
165 __linkFlags = ['-Wl,-O1']
166
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500167 def getDebugFlags(self, conf):
Davide Pesaventofea0f092023-08-09 13:18: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 Pesavento143f39d2024-01-19 16:00:41 -0500175 # Enable assertions in libstdc++
176 # https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
177 flags['DEFINES'] += ['_GLIBCXX_ASSERTIONS=1']
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500178 return flags
179
180 def getOptimizedFlags(self, conf):
Davide Pesaventofea0f092023-08-09 13:18:02 -0400181 flags = super().getOptimizedFlags(conf)
182 flags['CXXFLAGS'] += ['-O2', '-g1'] + self.__cxxFlags
183 flags['LINKFLAGS'] += self.__linkFlags
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500184 return flags
185
Davide Pesaventofea0f092023-08-09 13:18: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
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500198 def getDebugFlags(self, conf):
Davide Pesaventofea0f092023-08-09 13:18:02 -0400199 flags = super().getDebugFlags(conf)
200 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventoe4b74bd2022-03-09 18:56:37 -0500201 if platform.machine() == 'armv7l':
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400202 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500203 return flags
204
205 def getOptimizedFlags(self, conf):
Davide Pesaventofea0f092023-08-09 13:18:02 -0400206 flags = super().getOptimizedFlags(conf)
207 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventoe4b74bd2022-03-09 18:56:37 -0500208 if platform.machine() == 'armv7l':
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400209 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500210 return flags
211
Davide Pesaventofea0f092023-08-09 13:18:02 -0400212
213class ClangFlags(GccClangCommonFlags):
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500214 def getGeneralFlags(self, conf):
Davide Pesaventofea0f092023-08-09 13:18:02 -0400215 flags = super().getGeneralFlags(conf)
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400216 if Utils.unversioned_sys_platform() == 'darwin':
Davide Pesavento98ff2bc2018-05-24 00:10:31 -0400217 # Bug #4296
Davide Pesavento1d484532022-08-19 22:08:31 -0400218 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
Davide Pesaventofea0f092023-08-09 13:18:02 -0400219 flags['CXXFLAGS'] += [
220 ['-isystem', f'{brewdir}/include'], # for Homebrew
221 ['-isystem', '/opt/local/include'], # for MacPorts
222 ]
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400223 elif Utils.unversioned_sys_platform() == 'freebsd':
224 # Bug #4790
225 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Davide Pesavento143f39d2024-01-19 16:00:41 -0500226 if self.getCompilerVersion(conf) >= (18, 0, 0):
227 # Bug #5300
228 flags['CXXFLAGS'] += ['-Wno-enum-constexpr-conversion']
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500229 return flags
230
Davide Pesaventofea0f092023-08-09 13:18:02 -0400231 __cxxFlags = [
232 '-Wundefined-func-template',
233 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
234 ]
235
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500236 def getDebugFlags(self, conf):
Davide Pesaventofea0f092023-08-09 13:18:02 -0400237 flags = super().getDebugFlags(conf)
238 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento143f39d2024-01-19 16:00:41 -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 Pesaventof6ea9312024-07-21 13:50:25 -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 Afanasyevd352cce2015-11-20 14:15:11 -0500249 return flags
250
251 def getOptimizedFlags(self, conf):
Davide Pesaventofea0f092023-08-09 13:18:02 -0400252 flags = super().getOptimizedFlags(conf)
253 flags['CXXFLAGS'] += self.__cxxFlags
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500254 return flags