blob: 3a7bf660c3489257f96e45df7491fb481e9156de [file] [log] [blame]
Yingdi Yu40cd1c32014-04-17 15:02:17 -07001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
Yingdi Yu40cd1c32014-04-17 15:02:17 -07002
Davide Pesavento7ae8b082021-10-12 21:45:47 -04003import platform
Alexander Afanasyev67758b12018-03-06 18:36:44 -05004from waflib import Configure, Logs, Utils
Yingdi Yu40cd1c32014-04-17 15:02:17 -07005
6def options(opt):
Alexander Afanasyev67758b12018-03-06 18:36:44 -05007 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesavento7ae8b082021-10-12 21:45:47 -04008 help='Compile in debugging mode with minimal optimizations (-Og)')
Yingdi Yu40cd1c32014-04-17 15:02:17 -07009
10def configure(conf):
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -040011 conf.start_msg('Checking C++ compiler version')
12
Alexander Afanasyev67758b12018-03-06 18:36:44 -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 Pesavento6ebfd7c2017-08-26 17:52:54 -040016 errmsg = ''
17 warnmsg = ''
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080018 if cxx == 'gcc':
Davide Pesaventod1f1df82022-03-12 16:40:37 -050019 if ccver < (7, 4, 0):
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -040020 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesaventod1f1df82022-03-12 16:40:37 -050021 'The minimum supported gcc version is 7.4.')
Alexander Afanasyev67758b12018-03-06 18:36:44 -050022 conf.flags = GccFlags()
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080023 elif cxx == 'clang':
Davide Pesaventod1f1df82022-03-12 16:40:37 -050024 if Utils.unversioned_sys_platform() == 'darwin':
25 if ccver < (10, 0, 0):
26 errmsg = ('The version of Xcode you are using is too old.\n'
27 'The minimum supported Xcode version is 11.3.')
28 elif ccver < (11, 0, 0):
29 warnmsg = ('Using a version of Xcode older than 11.3 is not '
30 'officially supported and may result in build failures.')
31 elif ccver < (6, 0, 0):
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -040032 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesaventod1f1df82022-03-12 16:40:37 -050033 'The minimum supported clang version is 6.0.')
Alexander Afanasyev67758b12018-03-06 18:36:44 -050034 conf.flags = ClangFlags()
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080035 else:
Davide Pesaventoede59632022-08-26 20:35:44 -040036 warnmsg = f'{cxx} compiler is unsupported'
Alexander Afanasyev67758b12018-03-06 18:36:44 -050037 conf.flags = CompilerFlags()
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -040038
39 if errmsg:
Alexander Afanasyev67758b12018-03-06 18:36:44 -050040 conf.end_msg(ccverstr, color='RED')
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -040041 conf.fatal(errmsg)
42 elif warnmsg:
Alexander Afanasyev67758b12018-03-06 18:36:44 -050043 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento7ae8b082021-10-12 21:45:47 -040044 Logs.warn('WARNING: ' + warnmsg)
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -040045 else:
Alexander Afanasyev67758b12018-03-06 18:36:44 -050046 conf.end_msg(ccverstr)
Yingdi Yu40cd1c32014-04-17 15:02:17 -070047
Alexander Afanasyev67758b12018-03-06 18:36:44 -050048 conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080049
Davide Pesaventoa08dc3f2018-05-24 00:40:28 -040050 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyev67758b12018-03-06 18:36:44 -050051 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080052 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
53 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
54 conf.env.DEFINES += generalFlags['DEFINES']
55
Alexander Afanasyev67758b12018-03-06 18:36:44 -050056@Configure.conf
57def check_compiler_flags(conf):
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080058 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
59 # corresponding environment variables are not set.
60 # DEFINES are always applied.
Yingdi Yu40cd1c32014-04-17 15:02:17 -070061 if conf.options.debug:
Alexander Afanasyev67758b12018-03-06 18:36:44 -050062 extraFlags = conf.flags.getDebugFlags(conf)
63 if conf.areCustomCxxflagsPresent:
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080064 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev67758b12018-03-06 18:36:44 -050065 if missingFlags:
66 Logs.warn('Selected debug mode, but CXXFLAGS is set to a custom value "%s"'
67 % ' '.join(conf.env.CXXFLAGS))
68 Logs.warn('Default flags "%s" will not be used' % ' '.join(missingFlags))
Yingdi Yu40cd1c32014-04-17 15:02:17 -070069 else:
Alexander Afanasyev67758b12018-03-06 18:36:44 -050070 extraFlags = conf.flags.getOptimizedFlags(conf)
Yingdi Yu40cd1c32014-04-17 15:02:17 -070071
Alexander Afanasyev67758b12018-03-06 18:36:44 -050072 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080073 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
74 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
75
76 conf.env.DEFINES += extraFlags['DEFINES']
Vince Lehman0a7da612014-10-29 14:39:29 -050077
Yingdi Yu40cd1c32014-04-17 15:02:17 -070078@Configure.conf
79def add_supported_cxxflags(self, cxxflags):
80 """
81 Check which cxxflags are supported by compiler and add them to env.CXXFLAGS variable
82 """
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080083 if len(cxxflags) == 0:
84 return
85
86 self.start_msg('Checking supported CXXFLAGS')
Yingdi Yu40cd1c32014-04-17 15:02:17 -070087
88 supportedFlags = []
Alexander Afanasyev67758b12018-03-06 18:36:44 -050089 for flags in cxxflags:
90 flags = Utils.to_list(flags)
91 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
92 supportedFlags += flags
Yingdi Yu40cd1c32014-04-17 15:02:17 -070093
94 self.end_msg(' '.join(supportedFlags))
Davide Pesaventoc58cc7f2017-08-08 16:51:28 -050095 self.env.prepend_value('CXXFLAGS', supportedFlags)
Vince Lehman0a7da612014-10-29 14:39:29 -050096
97@Configure.conf
98def add_supported_linkflags(self, linkflags):
99 """
100 Check which linkflags are supported by compiler and add them to env.LINKFLAGS variable
101 """
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800102 if len(linkflags) == 0:
103 return
104
105 self.start_msg('Checking supported LINKFLAGS')
Vince Lehman0a7da612014-10-29 14:39:29 -0500106
107 supportedFlags = []
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500108 for flags in linkflags:
109 flags = Utils.to_list(flags)
110 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
111 supportedFlags += flags
Vince Lehman0a7da612014-10-29 14:39:29 -0500112
113 self.end_msg(' '.join(supportedFlags))
Davide Pesaventoc58cc7f2017-08-08 16:51:28 -0500114 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800115
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -0400116
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800117class CompilerFlags(object):
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500118 def getCompilerVersion(self, conf):
119 return tuple(int(i) for i in conf.env.CC_VERSION)
120
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800121 def getGeneralFlags(self, conf):
122 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
123 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
124
125 def getDebugFlags(self, conf):
126 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
127 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
128
129 def getOptimizedFlags(self, conf):
130 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
131 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
132
133class GccBasicFlags(CompilerFlags):
134 """
135 This class defines basic flags that work for both gcc and clang compilers
136 """
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -0400137 def getGeneralFlags(self, conf):
138 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400139 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesavento65ee9922022-11-16 00:21:43 -0500140 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesaventoe5569912019-01-29 19:39:06 -0500141 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -0400142 return flags
143
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800144 def getDebugFlags(self, conf):
145 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Davide Pesavento7ae8b082021-10-12 21:45:47 -0400146 flags['CXXFLAGS'] += ['-Og',
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800147 '-g3',
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800148 '-Wall',
149 '-Wextra',
Davide Pesavento1b640fd2023-01-04 21:00:05 -0500150 '-Wpedantic',
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800151 '-Werror',
Davide Pesavento7ae8b082021-10-12 21:45:47 -0400152 '-Wcatch-value=2',
153 '-Wextra-semi',
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500154 '-Wnon-virtual-dtor',
Alexander Afanasyev96bf2f02016-10-11 16:37:45 -0700155 '-Wno-error=deprecated-declarations', # Bug #3795
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -0400156 '-Wno-error=maybe-uninitialized', # Bug #1615
157 '-Wno-unused-parameter',
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800158 ]
Davide Pesaventoe5569912019-01-29 19:39:06 -0500159 flags['LINKFLAGS'] += ['-Wl,-O1']
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800160 return flags
161
162 def getOptimizedFlags(self, conf):
163 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
164 flags['CXXFLAGS'] += ['-O2',
165 '-g',
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800166 '-Wall',
167 '-Wextra',
Davide Pesavento1b640fd2023-01-04 21:00:05 -0500168 '-Wpedantic',
Davide Pesavento7ae8b082021-10-12 21:45:47 -0400169 '-Wcatch-value=2',
170 '-Wextra-semi',
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500171 '-Wnon-virtual-dtor',
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800172 '-Wno-unused-parameter',
173 ]
Davide Pesaventoe5569912019-01-29 19:39:06 -0500174 flags['LINKFLAGS'] += ['-Wl,-O1']
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800175 return flags
176
177class GccFlags(GccBasicFlags):
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800178 def getDebugFlags(self, conf):
179 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesavento7ae8b082021-10-12 21:45:47 -0400180 flags['CXXFLAGS'] += ['-fdiagnostics-color',
181 '-Wredundant-tags',
182 ]
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500183 if platform.machine() == 'armv7l':
Davide Pesavento7ae8b082021-10-12 21:45:47 -0400184 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800185 return flags
186
187 def getOptimizedFlags(self, conf):
188 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesavento7ae8b082021-10-12 21:45:47 -0400189 flags['CXXFLAGS'] += ['-fdiagnostics-color',
190 '-Wredundant-tags',
191 ]
Davide Pesaventod1f1df82022-03-12 16:40:37 -0500192 if platform.machine() == 'armv7l':
Davide Pesavento7ae8b082021-10-12 21:45:47 -0400193 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800194 return flags
195
196class ClangFlags(GccBasicFlags):
197 def getGeneralFlags(self, conf):
198 flags = super(ClangFlags, self).getGeneralFlags(conf)
Davide Pesavento7ae8b082021-10-12 21:45:47 -0400199 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500200 # Bug #4296
Davide Pesaventoede59632022-08-26 20:35:44 -0400201 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
202 flags['CXXFLAGS'] += [['-isystem', f'{brewdir}/include'], # for Homebrew
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500203 ['-isystem', '/opt/local/include']] # for MacPorts
Davide Pesavento7ae8b082021-10-12 21:45:47 -0400204 elif Utils.unversioned_sys_platform() == 'freebsd':
205 # Bug #4790
206 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800207 return flags
208
209 def getDebugFlags(self, conf):
210 flags = super(ClangFlags, self).getDebugFlags(conf)
211 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500212 '-Wundefined-func-template',
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -0400213 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800214 ]
215 return flags
216
217 def getOptimizedFlags(self, conf):
218 flags = super(ClangFlags, self).getOptimizedFlags(conf)
219 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500220 '-Wundefined-func-template',
Davide Pesavento6ebfd7c2017-08-26 17:52:54 -0400221 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800222 ]
223 return flags