blob: 48a88747654f6a853e2a4e65b91cda8d4e439cc7 [file] [log] [blame]
Davide Pesaventoab6ac5d2021-10-05 23:23:53 -04001import platform
Davide Pesavento64cafaa2018-05-24 00:50:41 -04002from waflib import Configure, Logs, Utils
Alexander Afanasyev740812e2014-10-30 15:37:45 -07003
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -04004
Alexander Afanasyev740812e2014-10-30 15:37:45 -07005def options(opt):
Davide Pesavento64cafaa2018-05-24 00:50:41 -04006 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesaventoab6ac5d2021-10-05 23:23:53 -04007 help='Compile in debugging mode with minimal optimizations (-Og)')
Alexander Afanasyev740812e2014-10-30 15:37:45 -07008
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -04009
Alexander Afanasyev740812e2014-10-30 15:37:45 -070010def configure(conf):
Davide Pesavento29db0fd2017-08-29 13:32:00 -040011 conf.start_msg('Checking C++ compiler version')
Alexander Afanasyev740812e2014-10-30 15:37:45 -070012
Davide Pesavento64cafaa2018-05-24 00:50:41 -040013 cxx = conf.env.CXX_NAME # generic name of the compiler
Davide Pesavento53156c32025-03-19 19:04:28 -040014 ccver = get_compiler_ver(conf)
Davide Pesavento64cafaa2018-05-24 00:50:41 -040015 ccverstr = '.'.join(conf.env.CC_VERSION)
Davide Pesavento29db0fd2017-08-29 13:32:00 -040016 errmsg = ''
17 warnmsg = ''
18 if cxx == 'gcc':
Davide Pesaventoc12f9152024-12-11 18:42:03 -050019 if ccver < (9, 1, 0):
Davide Pesavento29db0fd2017-08-29 13:32:00 -040020 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesaventoc12f9152024-12-11 18:42:03 -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 Pesavento08208492023-04-17 01:25:01 -040024 'officially supported and may result in build failures.')
Davide Pesavento64cafaa2018-05-24 00:50:41 -040025 conf.flags = GccFlags()
Davide Pesavento29db0fd2017-08-29 13:32:00 -040026 elif cxx == 'clang':
Davide Pesavento92669062022-03-10 19:09:44 -050027 if Utils.unversioned_sys_platform() == 'darwin':
Davide Pesaventoc12f9152024-12-11 18:42:03 -050028 if ccver < (11, 0, 0):
Davide Pesavento92669062022-03-10 19:09:44 -050029 errmsg = ('The version of Xcode you are using is too old.\n'
Davide Pesaventoc12f9152024-12-11 18:42:03 -050030 '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 Pesavento92669062022-03-10 19:09:44 -050033 'officially supported and may result in build failures.')
Davide Pesavento08208492023-04-17 01:25:01 -040034 elif ccver < (7, 0, 0):
Davide Pesavento29db0fd2017-08-29 13:32:00 -040035 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesaventoc12f9152024-12-11 18:42:03 -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.')
Davide Pesavento64cafaa2018-05-24 00:50:41 -040040 conf.flags = ClangFlags()
Davide Pesavento29db0fd2017-08-29 13:32:00 -040041 else:
Davide Pesaventof5006be2022-08-19 17:36:36 -040042 warnmsg = f'{cxx} compiler is unsupported'
Davide Pesavento64cafaa2018-05-24 00:50:41 -040043 conf.flags = CompilerFlags()
Davide Pesavento29db0fd2017-08-29 13:32:00 -040044
45 if errmsg:
Davide Pesavento64cafaa2018-05-24 00:50:41 -040046 conf.end_msg(ccverstr, color='RED')
Davide Pesavento29db0fd2017-08-29 13:32:00 -040047 conf.fatal(errmsg)
48 elif warnmsg:
Davide Pesavento64cafaa2018-05-24 00:50:41 -040049 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesaventoab6ac5d2021-10-05 23:23:53 -040050 Logs.warn('WARNING: ' + warnmsg)
Davide Pesavento29db0fd2017-08-29 13:32:00 -040051 else:
Davide Pesavento64cafaa2018-05-24 00:50:41 -040052 conf.end_msg(ccverstr)
Davide Pesavento29db0fd2017-08-29 13:32:00 -040053
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -040054 conf.areCustomCxxflagsPresent = len(conf.env.CXXFLAGS) > 0
Davide Pesavento29db0fd2017-08-29 13:32:00 -040055
Davide Pesavento64cafaa2018-05-24 00:50:41 -040056 # General flags are always applied (e.g., selecting C++ language standard)
57 generalFlags = conf.flags.getGeneralFlags(conf)
Davide Pesavento29db0fd2017-08-29 13:32:00 -040058 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
59 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
60 conf.env.DEFINES += generalFlags['DEFINES']
61
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -040062
Davide Pesavento53156c32025-03-19 19:04:28 -040063def get_compiler_ver(conf):
64 return tuple(int(i) for i in conf.env.CC_VERSION)
65
66
Davide Pesavento64cafaa2018-05-24 00:50:41 -040067@Configure.conf
68def check_compiler_flags(conf):
Davide Pesavento29db0fd2017-08-29 13:32:00 -040069 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
70 # corresponding environment variables are not set.
71 # DEFINES are always applied.
Alexander Afanasyev740812e2014-10-30 15:37:45 -070072 if conf.options.debug:
Davide Pesavento64cafaa2018-05-24 00:50:41 -040073 extraFlags = conf.flags.getDebugFlags(conf)
74 if conf.areCustomCxxflagsPresent:
Davide Pesavento29db0fd2017-08-29 13:32:00 -040075 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Davide Pesavento64cafaa2018-05-24 00:50:41 -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 Afanasyev740812e2014-10-30 15:37:45 -070080 else:
Davide Pesavento64cafaa2018-05-24 00:50:41 -040081 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev740812e2014-10-30 15:37:45 -070082
Davide Pesavento64cafaa2018-05-24 00:50:41 -040083 if not conf.areCustomCxxflagsPresent:
Davide Pesavento29db0fd2017-08-29 13:32:00 -040084 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
85 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
86
87 conf.env.DEFINES += extraFlags['DEFINES']
Alexander Afanasyev740812e2014-10-30 15:37:45 -070088
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -040089
Alexander Afanasyev740812e2014-10-30 15:37:45 -070090@Configure.conf
91def add_supported_cxxflags(self, cxxflags):
92 """
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -040093 Check which cxxflags are supported by the active compiler and add them to env.CXXFLAGS variable.
Alexander Afanasyev740812e2014-10-30 15:37:45 -070094 """
Davide Pesavento29db0fd2017-08-29 13:32:00 -040095 if len(cxxflags) == 0:
96 return
97
Alexander Afanasyev740812e2014-10-30 15:37:45 -070098 self.start_msg('Checking supported CXXFLAGS')
99
100 supportedFlags = []
Davide Pesavento64cafaa2018-05-24 00:50:41 -0400101 for flags in cxxflags:
102 flags = Utils.to_list(flags)
103 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
104 supportedFlags += flags
Alexander Afanasyev740812e2014-10-30 15:37:45 -0700105
106 self.end_msg(' '.join(supportedFlags))
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400107 self.env.prepend_value('CXXFLAGS', supportedFlags)
Alexander Afanasyev740812e2014-10-30 15:37:45 -0700108
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -0400109
Alexander Afanasyev740812e2014-10-30 15:37:45 -0700110@Configure.conf
111def add_supported_linkflags(self, linkflags):
112 """
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -0400113 Check which linkflags are supported by the active compiler and add them to env.LINKFLAGS variable.
Alexander Afanasyev740812e2014-10-30 15:37:45 -0700114 """
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400115 if len(linkflags) == 0:
116 return
117
Alexander Afanasyev740812e2014-10-30 15:37:45 -0700118 self.start_msg('Checking supported LINKFLAGS')
119
120 supportedFlags = []
Davide Pesavento64cafaa2018-05-24 00:50:41 -0400121 for flags in linkflags:
122 flags = Utils.to_list(flags)
123 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
124 supportedFlags += flags
Alexander Afanasyev740812e2014-10-30 15:37:45 -0700125
126 self.end_msg(' '.join(supportedFlags))
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400127 self.env.prepend_value('LINKFLAGS', supportedFlags)
128
129
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -0400130class CompilerFlags:
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400131 def getGeneralFlags(self, conf):
132 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Davide Pesavento8cb3cb72024-03-12 20:21:48 -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 Pesavento06784b42023-11-11 18:10:43 -0500137 return {
138 'CXXFLAGS': [],
139 'LINKFLAGS': [],
Davide Pesaventoc2f25b72024-12-13 19:23:50 -0500140 'DEFINES': ['BOOST_ASIO_NO_DEPRECATED'],
Davide Pesavento06784b42023-11-11 18:10:43 -0500141 }
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400142
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400143 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 Pesaventoc1a7fbc2023-08-09 13:22:02 -0400147
148class GccClangCommonFlags(CompilerFlags):
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400149 """
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -0400150 This class defines common flags that work for both gcc and clang compilers.
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400151 """
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -0400152
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400153 def getGeneralFlags(self, conf):
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -0400154 flags = super().getGeneralFlags(conf)
Davide Pesaventoef064892022-04-05 02:26:03 -0400155 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesaventof855ec12022-11-16 16:08:35 -0500156 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesavento7eaed5c2019-01-09 20:03:43 -0500157 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400158 return flags
159
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -0400160 __cxxFlags = [
161 '-fdiagnostics-color',
162 '-Wall',
163 '-Wextra',
164 '-Wpedantic',
165 '-Wenum-conversion',
166 '-Wextra-semi',
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -0400167 '-Wno-unused-parameter',
168 ]
169 __linkFlags = ['-Wl,-O1']
170
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400171 def getDebugFlags(self, conf):
Davide Pesaventoc1a7fbc2023-08-09 13:22: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 Pesavento4c24cf72024-01-19 15:53:53 -0500179 # Enable assertions in libstdc++
180 # https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
181 flags['DEFINES'] += ['_GLIBCXX_ASSERTIONS=1']
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400182 return flags
183
184 def getOptimizedFlags(self, conf):
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -0400185 flags = super().getOptimizedFlags(conf)
186 flags['CXXFLAGS'] += ['-O2', '-g1'] + self.__cxxFlags
187 flags['LINKFLAGS'] += self.__linkFlags
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400188 return flags
189
Davide Pesaventoc1a7fbc2023-08-09 13:22: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
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400202 def getDebugFlags(self, conf):
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -0400203 flags = super().getDebugFlags(conf)
204 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento92669062022-03-10 19:09:44 -0500205 if platform.machine() == 'armv7l':
Davide Pesaventoab6ac5d2021-10-05 23:23:53 -0400206 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400207 return flags
208
209 def getOptimizedFlags(self, conf):
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -0400210 flags = super().getOptimizedFlags(conf)
211 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento92669062022-03-10 19:09:44 -0500212 if platform.machine() == 'armv7l':
Davide Pesaventoab6ac5d2021-10-05 23:23:53 -0400213 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400214 return flags
215
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -0400216
217class ClangFlags(GccClangCommonFlags):
Davide Pesavento64cafaa2018-05-24 00:50:41 -0400218 def getGeneralFlags(self, conf):
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -0400219 flags = super().getGeneralFlags(conf)
Davide Pesaventoab6ac5d2021-10-05 23:23:53 -0400220 if Utils.unversioned_sys_platform() == 'darwin':
Davide Pesavento64cafaa2018-05-24 00:50:41 -0400221 # Bug #4296
Davide Pesaventof5006be2022-08-19 17:36:36 -0400222 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -0400223 flags['CXXFLAGS'] += [
224 ['-isystem', f'{brewdir}/include'], # for Homebrew
225 ['-isystem', '/opt/local/include'], # for MacPorts
226 ]
Davide Pesaventoab6ac5d2021-10-05 23:23:53 -0400227 elif Utils.unversioned_sys_platform() == 'freebsd':
228 # Bug #4790
229 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Davide Pesavento53156c32025-03-19 19:04:28 -0400230 if get_compiler_ver(conf) >= (18, 0, 0) and get_compiler_ver(conf) < (20, 1, 0):
Davide Pesavento4c24cf72024-01-19 15:53:53 -0500231 # Bug #5300
232 flags['CXXFLAGS'] += ['-Wno-enum-constexpr-conversion']
Davide Pesavento64cafaa2018-05-24 00:50:41 -0400233 return flags
234
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -0400235 __cxxFlags = [
236 '-Wundefined-func-template',
237 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
238 ]
239
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400240 def getDebugFlags(self, conf):
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -0400241 flags = super().getDebugFlags(conf)
242 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento4c24cf72024-01-19 15:53:53 -0500243 # Enable assertions in libc++
Davide Pesavento53156c32025-03-19 19:04:28 -0400244 if get_compiler_ver(conf) >= (18, 0, 0):
Davide Pesavento4c24cf72024-01-19 15:53:53 -0500245 # https://libcxx.llvm.org/Hardening.html
246 flags['DEFINES'] += ['_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE']
Davide Pesavento53156c32025-03-19 19:04:28 -0400247 elif get_compiler_ver(conf) >= (15, 0, 0):
Davide Pesavento4c24cf72024-01-19 15:53:53 -0500248 # https://releases.llvm.org/15.0.0/projects/libcxx/docs/UsingLibcxx.html#enabling-the-safe-libc-mode
249 flags['DEFINES'] += ['_LIBCPP_ENABLE_ASSERTIONS=1']
Davide Pesavento8cb3cb72024-03-12 20:21:48 -0400250 # Tell libc++ to avoid including transitive headers
251 # https://libcxx.llvm.org/DesignDocs/HeaderRemovalPolicy.html
252 flags['DEFINES'] += ['_LIBCPP_REMOVE_TRANSITIVE_INCLUDES=1']
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400253 return flags
254
255 def getOptimizedFlags(self, conf):
Davide Pesaventoc1a7fbc2023-08-09 13:22:02 -0400256 flags = super().getOptimizedFlags(conf)
257 flags['CXXFLAGS'] += self.__cxxFlags
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400258 return flags