blob: 9899d05b0b2c38eca520c67024e9816b5509a51f [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 Pesaventoc1f13622024-12-13 13:35:01 -050019 if ccver < (9, 1, 0):
Davide Pesaventob545aac2017-09-22 23:54:10 -040020 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesaventoc1f13622024-12-13 13:35:01 -050021 'The minimum supported gcc version is 10.2.')
22 elif ccver < (10, 2, 0):
23 warnmsg = ('Using a version of gcc older than 10.2 is not '
Davide Pesavento60b52cf2023-04-26 15:27:02 -040024 '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':
Davide Pesaventoc1f13622024-12-13 13:35:01 -050028 if ccver < (11, 0, 0):
Davide Pesaventoe4b74bd2022-03-09 18:56:37 -050029 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 Pesaventoc1f13622024-12-13 13:35:01 -050036 'The minimum supported clang version is 10.0.')
37 elif ccver < (10, 0, 0):
38 warnmsg = ('Using a version of clang older than 10.0 is not '
39 'officially supported and may result in build failures.')
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000040 conf.flags = ClangFlags()
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050041 else:
Davide Pesavento1d484532022-08-19 22:08:31 -040042 warnmsg = f'{cxx} compiler is unsupported'
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000043 conf.flags = CompilerFlags()
Davide Pesaventob545aac2017-09-22 23:54:10 -040044
45 if errmsg:
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040046 conf.end_msg(ccverstr, color='RED')
Davide Pesaventob545aac2017-09-22 23:54:10 -040047 conf.fatal(errmsg)
48 elif warnmsg:
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040049 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento333e3eb2021-10-12 19:30:51 -040050 Logs.warn('WARNING: ' + warnmsg)
Davide Pesaventob545aac2017-09-22 23:54:10 -040051 else:
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040052 conf.end_msg(ccverstr)
Shuo Chen478204c2014-03-18 18:27:04 -070053
Davide Pesaventofea0f092023-08-09 13:18:02 -040054 conf.areCustomCxxflagsPresent = len(conf.env.CXXFLAGS) > 0
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050055
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040056 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000057 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050058 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
59 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
60 conf.env.DEFINES += generalFlags['DEFINES']
61
Davide Pesaventofea0f092023-08-09 13:18:02 -040062
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000063@Configure.conf
64def check_compiler_flags(conf):
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050065 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
66 # corresponding environment variables are not set.
67 # DEFINES are always applied.
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070068 if conf.options.debug:
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000069 extraFlags = conf.flags.getDebugFlags(conf)
70 if conf.areCustomCxxflagsPresent:
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050071 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040072 if missingFlags:
73 Logs.warn('Selected debug mode, but CXXFLAGS is set to a custom value "%s"'
74 % ' '.join(conf.env.CXXFLAGS))
75 Logs.warn('Default flags "%s" will not be used' % ' '.join(missingFlags))
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070076 else:
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000077 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070078
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000079 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050080 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
81 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
82
83 conf.env.DEFINES += extraFlags['DEFINES']
Wentao Shanga8f3c402014-10-30 14:03:27 -070084
Davide Pesaventofea0f092023-08-09 13:18:02 -040085
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070086@Configure.conf
87def add_supported_cxxflags(self, cxxflags):
88 """
Davide Pesaventofea0f092023-08-09 13:18:02 -040089 Check which cxxflags are supported by the active compiler and add them to env.CXXFLAGS variable.
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070090 """
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050091 if len(cxxflags) == 0:
92 return
93
Wentao Shanga8f3c402014-10-30 14:03:27 -070094 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070095
96 supportedFlags = []
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000097 for flags in cxxflags:
98 flags = Utils.to_list(flags)
99 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
100 supportedFlags += flags
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -0700101
Shuo Chen478204c2014-03-18 18:27:04 -0700102 self.end_msg(' '.join(supportedFlags))
Junxiao Shie1801312017-07-22 19:16:49 +0000103 self.env.prepend_value('CXXFLAGS', supportedFlags)
Wentao Shanga8f3c402014-10-30 14:03:27 -0700104
Davide Pesaventofea0f092023-08-09 13:18:02 -0400105
Wentao Shanga8f3c402014-10-30 14:03:27 -0700106@Configure.conf
107def add_supported_linkflags(self, linkflags):
108 """
Davide Pesaventofea0f092023-08-09 13:18:02 -0400109 Check which linkflags are supported by the active compiler and add them to env.LINKFLAGS variable.
Wentao Shanga8f3c402014-10-30 14:03:27 -0700110 """
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500111 if len(linkflags) == 0:
112 return
113
Wentao Shanga8f3c402014-10-30 14:03:27 -0700114 self.start_msg('Checking supported LINKFLAGS')
115
116 supportedFlags = []
Alexander Afanasyevbb058c02018-02-15 22:49:24 +0000117 for flags in linkflags:
118 flags = Utils.to_list(flags)
119 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
120 supportedFlags += flags
Wentao Shanga8f3c402014-10-30 14:03:27 -0700121
122 self.end_msg(' '.join(supportedFlags))
Junxiao Shie1801312017-07-22 19:16:49 +0000123 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500124
125
Davide Pesaventofea0f092023-08-09 13:18:02 -0400126class CompilerFlags:
Davide Pesavento98ff2bc2018-05-24 00:10:31 -0400127 def getCompilerVersion(self, conf):
128 return tuple(int(i) for i in conf.env.CC_VERSION)
129
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500130 def getGeneralFlags(self, conf):
131 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Davide Pesaventof6ea9312024-07-21 13:50:25 -0400132 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
133
134 def getDebugFlags(self, conf):
135 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Davide Pesavento164ae3b2023-11-11 22:00:23 -0500136 return {
137 'CXXFLAGS': [],
138 'LINKFLAGS': [],
139 'DEFINES': ['BOOST_ASIO_NO_DEPRECATED', 'BOOST_FILESYSTEM_NO_DEPRECATED'],
140 }
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500141
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500142 def getOptimizedFlags(self, conf):
143 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
144 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
145
Davide Pesaventofea0f092023-08-09 13:18:02 -0400146
147class GccClangCommonFlags(CompilerFlags):
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500148 """
Davide Pesaventofea0f092023-08-09 13:18:02 -0400149 This class defines common flags that work for both gcc and clang compilers.
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500150 """
Davide Pesaventofea0f092023-08-09 13:18:02 -0400151
Junxiao Shie1801312017-07-22 19:16:49 +0000152 def getGeneralFlags(self, conf):
Davide Pesaventofea0f092023-08-09 13:18:02 -0400153 flags = super().getGeneralFlags(conf)
Davide Pesavento11904062022-04-14 22:33:28 -0400154 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesaventoc2414752022-11-16 16:56:45 -0500155 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesaventoed2f4762019-01-25 00:40:46 -0500156 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Junxiao Shie1801312017-07-22 19:16:49 +0000157 return flags
158
Davide Pesaventofea0f092023-08-09 13:18:02 -0400159 __cxxFlags = [
160 '-fdiagnostics-color',
161 '-Wall',
162 '-Wextra',
163 '-Wpedantic',
164 '-Wenum-conversion',
165 '-Wextra-semi',
Davide Pesaventofea0f092023-08-09 13:18:02 -0400166 '-Wno-unused-parameter',
167 ]
168 __linkFlags = ['-Wl,-O1']
169
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500170 def getDebugFlags(self, conf):
Davide Pesaventofea0f092023-08-09 13:18:02 -0400171 flags = super().getDebugFlags(conf)
172 flags['CXXFLAGS'] += ['-Og', '-g'] + self.__cxxFlags + [
173 '-Werror',
174 '-Wno-error=deprecated-declarations', # Bug #3795
175 '-Wno-error=maybe-uninitialized', # Bug #1615
176 ]
177 flags['LINKFLAGS'] += self.__linkFlags
Davide Pesavento143f39d2024-01-19 16:00:41 -0500178 # Enable assertions in libstdc++
179 # https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
180 flags['DEFINES'] += ['_GLIBCXX_ASSERTIONS=1']
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500181 return flags
182
183 def getOptimizedFlags(self, conf):
Davide Pesaventofea0f092023-08-09 13:18:02 -0400184 flags = super().getOptimizedFlags(conf)
185 flags['CXXFLAGS'] += ['-O2', '-g1'] + self.__cxxFlags
186 flags['LINKFLAGS'] += self.__linkFlags
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500187 return flags
188
Davide Pesaventofea0f092023-08-09 13:18:02 -0400189
190class GccFlags(GccClangCommonFlags):
191 __cxxFlags = [
192 '-Wcatch-value=2',
193 '-Wcomma-subscript', # enabled by default in C++20
194 '-Wduplicated-branches',
195 '-Wduplicated-cond',
196 '-Wlogical-op',
197 '-Wredundant-tags',
198 '-Wvolatile', # enabled by default in C++20
199 ]
200
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500201 def getDebugFlags(self, conf):
Davide Pesaventofea0f092023-08-09 13:18:02 -0400202 flags = super().getDebugFlags(conf)
203 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventoe4b74bd2022-03-09 18:56:37 -0500204 if platform.machine() == 'armv7l':
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400205 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500206 return flags
207
208 def getOptimizedFlags(self, conf):
Davide Pesaventofea0f092023-08-09 13:18:02 -0400209 flags = super().getOptimizedFlags(conf)
210 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventoe4b74bd2022-03-09 18:56:37 -0500211 if platform.machine() == 'armv7l':
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400212 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500213 return flags
214
Davide Pesaventofea0f092023-08-09 13:18:02 -0400215
216class ClangFlags(GccClangCommonFlags):
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500217 def getGeneralFlags(self, conf):
Davide Pesaventofea0f092023-08-09 13:18:02 -0400218 flags = super().getGeneralFlags(conf)
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400219 if Utils.unversioned_sys_platform() == 'darwin':
Davide Pesavento98ff2bc2018-05-24 00:10:31 -0400220 # Bug #4296
Davide Pesavento1d484532022-08-19 22:08:31 -0400221 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
Davide Pesaventofea0f092023-08-09 13:18:02 -0400222 flags['CXXFLAGS'] += [
223 ['-isystem', f'{brewdir}/include'], # for Homebrew
224 ['-isystem', '/opt/local/include'], # for MacPorts
225 ]
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400226 elif Utils.unversioned_sys_platform() == 'freebsd':
227 # Bug #4790
228 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Davide Pesavento143f39d2024-01-19 16:00:41 -0500229 if self.getCompilerVersion(conf) >= (18, 0, 0):
230 # Bug #5300
231 flags['CXXFLAGS'] += ['-Wno-enum-constexpr-conversion']
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500232 return flags
233
Davide Pesaventofea0f092023-08-09 13:18:02 -0400234 __cxxFlags = [
235 '-Wundefined-func-template',
236 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
237 ]
238
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500239 def getDebugFlags(self, conf):
Davide Pesaventofea0f092023-08-09 13:18:02 -0400240 flags = super().getDebugFlags(conf)
241 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento143f39d2024-01-19 16:00:41 -0500242 # Enable assertions in libc++
243 if self.getCompilerVersion(conf) >= (18, 0, 0):
244 # https://libcxx.llvm.org/Hardening.html
245 flags['DEFINES'] += ['_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE']
246 elif self.getCompilerVersion(conf) >= (15, 0, 0):
247 # https://releases.llvm.org/15.0.0/projects/libcxx/docs/UsingLibcxx.html#enabling-the-safe-libc-mode
248 flags['DEFINES'] += ['_LIBCPP_ENABLE_ASSERTIONS=1']
Davide Pesaventof6ea9312024-07-21 13:50:25 -0400249 # Tell libc++ to avoid including transitive headers
250 # https://libcxx.llvm.org/DesignDocs/HeaderRemovalPolicy.html
251 flags['DEFINES'] += ['_LIBCPP_REMOVE_TRANSITIVE_INCLUDES=1']
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500252 return flags
253
254 def getOptimizedFlags(self, conf):
Davide Pesaventofea0f092023-08-09 13:18:02 -0400255 flags = super().getOptimizedFlags(conf)
256 flags['CXXFLAGS'] += self.__cxxFlags
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500257 return flags