blob: ac5cdfdcd9f1a9d0068c13b0537022c7c0770a6e [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
Davide Pesaventoafe42f12025-03-25 12:46:22 -040014 ccver = get_compiler_ver(conf)
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040015 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
Davide Pesaventoafe42f12025-03-25 12:46:22 -040063def get_compiler_ver(conf):
64 return tuple(int(i) for i in conf.env.CC_VERSION)
65
66
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000067@Configure.conf
68def check_compiler_flags(conf):
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050069 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
70 # corresponding environment variables are not set.
71 # DEFINES are always applied.
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070072 if conf.options.debug:
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000073 extraFlags = conf.flags.getDebugFlags(conf)
74 if conf.areCustomCxxflagsPresent:
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050075 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040076 if missingFlags:
77 Logs.warn('Selected debug mode, but CXXFLAGS is set to a custom value "%s"'
78 % ' '.join(conf.env.CXXFLAGS))
79 Logs.warn('Default flags "%s" will not be used' % ' '.join(missingFlags))
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070080 else:
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000081 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070082
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000083 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050084 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
85 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
86
87 conf.env.DEFINES += extraFlags['DEFINES']
Wentao Shanga8f3c402014-10-30 14:03:27 -070088
Davide Pesaventofea0f092023-08-09 13:18:02 -040089
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070090@Configure.conf
91def add_supported_cxxflags(self, cxxflags):
92 """
Davide Pesaventofea0f092023-08-09 13:18:02 -040093 Check which cxxflags are supported by the active compiler and add them to env.CXXFLAGS variable.
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070094 """
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050095 if len(cxxflags) == 0:
96 return
97
Wentao Shanga8f3c402014-10-30 14:03:27 -070098 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070099
100 supportedFlags = []
Alexander Afanasyevbb058c02018-02-15 22:49:24 +0000101 for flags in cxxflags:
102 flags = Utils.to_list(flags)
103 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
104 supportedFlags += flags
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -0700105
Shuo Chen478204c2014-03-18 18:27:04 -0700106 self.end_msg(' '.join(supportedFlags))
Junxiao Shie1801312017-07-22 19:16:49 +0000107 self.env.prepend_value('CXXFLAGS', supportedFlags)
Wentao Shanga8f3c402014-10-30 14:03:27 -0700108
Davide Pesaventofea0f092023-08-09 13:18:02 -0400109
Wentao Shanga8f3c402014-10-30 14:03:27 -0700110@Configure.conf
111def add_supported_linkflags(self, linkflags):
112 """
Davide Pesaventofea0f092023-08-09 13:18:02 -0400113 Check which linkflags are supported by the active compiler and add them to env.LINKFLAGS variable.
Wentao Shanga8f3c402014-10-30 14:03:27 -0700114 """
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500115 if len(linkflags) == 0:
116 return
117
Wentao Shanga8f3c402014-10-30 14:03:27 -0700118 self.start_msg('Checking supported LINKFLAGS')
119
120 supportedFlags = []
Alexander Afanasyevbb058c02018-02-15 22:49:24 +0000121 for flags in linkflags:
122 flags = Utils.to_list(flags)
123 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
124 supportedFlags += flags
Wentao Shanga8f3c402014-10-30 14:03:27 -0700125
126 self.end_msg(' '.join(supportedFlags))
Junxiao Shie1801312017-07-22 19:16:49 +0000127 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500128
129
Davide Pesaventofea0f092023-08-09 13:18:02 -0400130class CompilerFlags:
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500131 def getGeneralFlags(self, conf):
132 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Davide Pesaventof6ea9312024-07-21 13:50:25 -0400133 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
134
135 def getDebugFlags(self, conf):
136 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Davide Pesavento164ae3b2023-11-11 22:00:23 -0500137 return {
138 'CXXFLAGS': [],
139 'LINKFLAGS': [],
Davide Pesavento01a2a8c2024-12-13 14:25:50 -0500140 'DEFINES': ['BOOST_ASIO_NO_DEPRECATED'],
Davide Pesavento164ae3b2023-11-11 22:00:23 -0500141 }
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500142
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500143 def getOptimizedFlags(self, conf):
144 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
145 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
146
Davide Pesaventofea0f092023-08-09 13:18:02 -0400147
148class GccClangCommonFlags(CompilerFlags):
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500149 """
Davide Pesaventofea0f092023-08-09 13:18:02 -0400150 This class defines common flags that work for both gcc and clang compilers.
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500151 """
Davide Pesaventofea0f092023-08-09 13:18:02 -0400152
Junxiao Shie1801312017-07-22 19:16:49 +0000153 def getGeneralFlags(self, conf):
Davide Pesaventofea0f092023-08-09 13:18:02 -0400154 flags = super().getGeneralFlags(conf)
Davide Pesavento11904062022-04-14 22:33:28 -0400155 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesaventoc2414752022-11-16 16:56:45 -0500156 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesaventoed2f4762019-01-25 00:40:46 -0500157 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Junxiao Shie1801312017-07-22 19:16:49 +0000158 return flags
159
Davide Pesaventofea0f092023-08-09 13:18:02 -0400160 __cxxFlags = [
161 '-fdiagnostics-color',
162 '-Wall',
163 '-Wextra',
164 '-Wpedantic',
165 '-Wenum-conversion',
166 '-Wextra-semi',
Davide Pesaventofea0f092023-08-09 13:18:02 -0400167 '-Wno-unused-parameter',
168 ]
169 __linkFlags = ['-Wl,-O1']
170
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500171 def getDebugFlags(self, conf):
Davide Pesaventofea0f092023-08-09 13:18:02 -0400172 flags = super().getDebugFlags(conf)
173 flags['CXXFLAGS'] += ['-Og', '-g'] + self.__cxxFlags + [
174 '-Werror',
175 '-Wno-error=deprecated-declarations', # Bug #3795
176 '-Wno-error=maybe-uninitialized', # Bug #1615
177 ]
178 flags['LINKFLAGS'] += self.__linkFlags
Davide Pesavento143f39d2024-01-19 16:00:41 -0500179 # Enable assertions in libstdc++
180 # https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
181 flags['DEFINES'] += ['_GLIBCXX_ASSERTIONS=1']
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500182 return flags
183
184 def getOptimizedFlags(self, conf):
Davide Pesaventofea0f092023-08-09 13:18:02 -0400185 flags = super().getOptimizedFlags(conf)
186 flags['CXXFLAGS'] += ['-O2', '-g1'] + self.__cxxFlags
187 flags['LINKFLAGS'] += self.__linkFlags
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500188 return flags
189
Davide Pesaventofea0f092023-08-09 13:18:02 -0400190
191class GccFlags(GccClangCommonFlags):
192 __cxxFlags = [
193 '-Wcatch-value=2',
194 '-Wcomma-subscript', # enabled by default in C++20
195 '-Wduplicated-branches',
196 '-Wduplicated-cond',
197 '-Wlogical-op',
198 '-Wredundant-tags',
199 '-Wvolatile', # enabled by default in C++20
200 ]
201
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500202 def getDebugFlags(self, conf):
Davide Pesaventofea0f092023-08-09 13:18:02 -0400203 flags = super().getDebugFlags(conf)
204 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventoe4b74bd2022-03-09 18:56:37 -0500205 if platform.machine() == 'armv7l':
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400206 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500207 return flags
208
209 def getOptimizedFlags(self, conf):
Davide Pesaventofea0f092023-08-09 13:18:02 -0400210 flags = super().getOptimizedFlags(conf)
211 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventoe4b74bd2022-03-09 18:56:37 -0500212 if platform.machine() == 'armv7l':
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400213 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500214 return flags
215
Davide Pesaventofea0f092023-08-09 13:18:02 -0400216
217class ClangFlags(GccClangCommonFlags):
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500218 def getGeneralFlags(self, conf):
Davide Pesaventofea0f092023-08-09 13:18:02 -0400219 flags = super().getGeneralFlags(conf)
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400220 if Utils.unversioned_sys_platform() == 'darwin':
Davide Pesavento98ff2bc2018-05-24 00:10:31 -0400221 # Bug #4296
Davide Pesavento1d484532022-08-19 22:08:31 -0400222 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
Davide Pesaventofea0f092023-08-09 13:18:02 -0400223 flags['CXXFLAGS'] += [
224 ['-isystem', f'{brewdir}/include'], # for Homebrew
225 ['-isystem', '/opt/local/include'], # for MacPorts
226 ]
Davide Pesaventoe51144e2025-04-04 02:23:11 -0400227 else:
228 if Utils.unversioned_sys_platform() == 'freebsd':
229 # Bug #4790
230 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
231 if get_compiler_ver(conf) >= (18, 0, 0) and get_compiler_ver(conf) < (20, 1, 0):
232 # Bug #5300
233 flags['CXXFLAGS'] += ['-Wno-enum-constexpr-conversion']
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500234 return flags
235
Davide Pesaventofea0f092023-08-09 13:18:02 -0400236 __cxxFlags = [
237 '-Wundefined-func-template',
238 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
239 ]
240
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500241 def getDebugFlags(self, conf):
Davide Pesaventofea0f092023-08-09 13:18:02 -0400242 flags = super().getDebugFlags(conf)
243 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesaventoe51144e2025-04-04 02:23:11 -0400244 ccver = get_compiler_ver(conf)
245 darwin = Utils.unversioned_sys_platform() == 'darwin'
Davide Pesavento143f39d2024-01-19 16:00:41 -0500246 # Enable assertions in libc++
Davide Pesaventoe51144e2025-04-04 02:23:11 -0400247 if (darwin and ccver >= (17, 0, 0)) or (not darwin and ccver >= (18, 0, 0)):
Davide Pesavento143f39d2024-01-19 16:00:41 -0500248 # https://libcxx.llvm.org/Hardening.html
249 flags['DEFINES'] += ['_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE']
Davide Pesaventoe51144e2025-04-04 02:23:11 -0400250 elif ccver >= (15, 0, 0):
Davide Pesavento143f39d2024-01-19 16:00:41 -0500251 # https://releases.llvm.org/15.0.0/projects/libcxx/docs/UsingLibcxx.html#enabling-the-safe-libc-mode
252 flags['DEFINES'] += ['_LIBCPP_ENABLE_ASSERTIONS=1']
Davide Pesaventof6ea9312024-07-21 13:50:25 -0400253 # Tell libc++ to avoid including transitive headers
254 # https://libcxx.llvm.org/DesignDocs/HeaderRemovalPolicy.html
255 flags['DEFINES'] += ['_LIBCPP_REMOVE_TRANSITIVE_INCLUDES=1']
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500256 return flags
257
258 def getOptimizedFlags(self, conf):
Davide Pesaventofea0f092023-08-09 13:18:02 -0400259 flags = super().getOptimizedFlags(conf)
260 flags['CXXFLAGS'] += self.__cxxFlags
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500261 return flags