blob: 8c718717e3da2f62156c901c59a903bf6b4d7160 [file] [log] [blame]
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -07001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -07002
Davide Pesavento17521592020-05-14 19:01:32 -04003import platform
Davide Pesavento0064c1d2018-03-03 18:43:53 -05004from waflib import Configure, Logs, Utils
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -07005
6def options(opt):
Davide Pesavento0064c1d2018-03-03 18:43:53 -05007 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesavento17521592020-05-14 19:01:32 -04008 help='Compile in debugging mode with minimal optimizations (-Og)')
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -07009
10def configure(conf):
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040011 conf.start_msg('Checking C++ compiler version')
12
Davide Pesavento0064c1d2018-03-03 18:43:53 -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 Pesaventoc0a5a392017-08-19 16:49:30 -040016 errmsg = ''
17 warnmsg = ''
Alexander Afanasyev76818632015-01-26 21:30:45 -080018 if cxx == 'gcc':
Davide Pesaventoc52cd5e2022-03-05 20:40:54 -050019 if ccver < (7, 4, 0):
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040020 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesaventoc5e3e7a2023-03-07 21:25:27 -050021 '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 Afanasyevb5220702017-09-21 18:57:00 -040025 conf.flags = GccFlags()
Alexander Afanasyev76818632015-01-26 21:30:45 -080026 elif cxx == 'clang':
Davide Pesaventoc52cd5e2022-03-05 20:40:54 -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 Pesaventoc5e3e7a2023-03-07 21:25:27 -050030 'The minimum supported Xcode version is 12.4.')
31 elif ccver < (12, 0, 0):
32 warnmsg = ('Using a version of Xcode older than 12.4 is not '
Davide Pesaventoc52cd5e2022-03-05 20:40:54 -050033 'officially supported and may result in build failures.')
Davide Pesaventoc5e3e7a2023-03-07 21:25:27 -050034 elif ccver < (7, 0, 0):
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040035 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesaventoc5e3e7a2023-03-07 21:25:27 -050036 'The minimum supported clang version is 7.0.')
Alexander Afanasyevb5220702017-09-21 18:57:00 -040037 conf.flags = ClangFlags()
Alexander Afanasyev76818632015-01-26 21:30:45 -080038 else:
Davide Pesaventoe541d1b2022-08-17 15:10:32 -040039 warnmsg = f'{cxx} compiler is unsupported'
Alexander Afanasyevb5220702017-09-21 18:57:00 -040040 conf.flags = CompilerFlags()
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040041
42 if errmsg:
Davide Pesavento0064c1d2018-03-03 18:43:53 -050043 conf.end_msg(ccverstr, color='RED')
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040044 conf.fatal(errmsg)
45 elif warnmsg:
Davide Pesavento0064c1d2018-03-03 18:43:53 -050046 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento17521592020-05-14 19:01:32 -040047 Logs.warn('WARNING: ' + warnmsg)
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040048 else:
Davide Pesavento0064c1d2018-03-03 18:43:53 -050049 conf.end_msg(ccverstr)
Davide Pesavento1bdef282014-04-08 20:59:50 +020050
Alexander Afanasyevb5220702017-09-21 18:57:00 -040051 conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
Alexander Afanasyev76818632015-01-26 21:30:45 -080052
Davide Pesaventoc0822fa2018-05-10 21:54:10 -040053 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyevb5220702017-09-21 18:57:00 -040054 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyev76818632015-01-26 21:30:45 -080055 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
56 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
57 conf.env.DEFINES += generalFlags['DEFINES']
58
Alexander Afanasyevb5220702017-09-21 18:57:00 -040059@Configure.conf
60def check_compiler_flags(conf):
Davide Pesaventof485d892015-10-02 02:00:54 +020061 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Alexander Afanasyev76818632015-01-26 21:30:45 -080062 # corresponding environment variables are not set.
Davide Pesaventof485d892015-10-02 02:00:54 +020063 # DEFINES are always applied.
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070064 if conf.options.debug:
Alexander Afanasyevb5220702017-09-21 18:57:00 -040065 extraFlags = conf.flags.getDebugFlags(conf)
66 if conf.areCustomCxxflagsPresent:
Alexander Afanasyev76818632015-01-26 21:30:45 -080067 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Davide Pesavento0064c1d2018-03-03 18:43:53 -050068 if missingFlags:
69 Logs.warn('Selected debug mode, but CXXFLAGS is set to a custom value "%s"'
70 % ' '.join(conf.env.CXXFLAGS))
71 Logs.warn('Default flags "%s" will not be used' % ' '.join(missingFlags))
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070072 else:
Alexander Afanasyevb5220702017-09-21 18:57:00 -040073 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070074
Alexander Afanasyevb5220702017-09-21 18:57:00 -040075 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyev76818632015-01-26 21:30:45 -080076 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Davide Pesaventoed89ccc2015-11-01 20:35:04 +010077 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Alexander Afanasyev76818632015-01-26 21:30:45 -080078
79 conf.env.DEFINES += extraFlags['DEFINES']
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020080
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070081@Configure.conf
82def add_supported_cxxflags(self, cxxflags):
83 """
84 Check which cxxflags are supported by compiler and add them to env.CXXFLAGS variable
85 """
Alexander Afanasyev76818632015-01-26 21:30:45 -080086 if len(cxxflags) == 0:
87 return
88
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020089 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070090
91 supportedFlags = []
Alexander Afanasyevb5220702017-09-21 18:57:00 -040092 for flags in cxxflags:
93 flags = Utils.to_list(flags)
94 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
95 supportedFlags += flags
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070096
Davide Pesavento1bdef282014-04-08 20:59:50 +020097 self.end_msg(' '.join(supportedFlags))
Davide Pesavento67f30272016-08-10 01:55:16 +000098 self.env.prepend_value('CXXFLAGS', supportedFlags)
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020099
100@Configure.conf
101def add_supported_linkflags(self, linkflags):
102 """
103 Check which linkflags are supported by compiler and add them to env.LINKFLAGS variable
104 """
Alexander Afanasyev76818632015-01-26 21:30:45 -0800105 if len(linkflags) == 0:
106 return
107
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200108 self.start_msg('Checking supported LINKFLAGS')
109
110 supportedFlags = []
Alexander Afanasyevb5220702017-09-21 18:57:00 -0400111 for flags in linkflags:
112 flags = Utils.to_list(flags)
113 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
114 supportedFlags += flags
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200115
116 self.end_msg(' '.join(supportedFlags))
Davide Pesavento67f30272016-08-10 01:55:16 +0000117 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyev76818632015-01-26 21:30:45 -0800118
119
120class CompilerFlags(object):
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500121 def getCompilerVersion(self, conf):
122 return tuple(int(i) for i in conf.env.CC_VERSION)
123
Alexander Afanasyev76818632015-01-26 21:30:45 -0800124 def getGeneralFlags(self, conf):
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100125 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Alexander Afanasyev76818632015-01-26 21:30:45 -0800126 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
127
128 def getDebugFlags(self, conf):
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100129 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Alexander Afanasyev76818632015-01-26 21:30:45 -0800130 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
131
132 def getOptimizedFlags(self, conf):
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100133 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Alexander Afanasyev76818632015-01-26 21:30:45 -0800134 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
135
136class GccBasicFlags(CompilerFlags):
Davide Pesaventof485d892015-10-02 02:00:54 +0200137 """
138 This class defines basic flags that work for both gcc and clang compilers
139 """
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -0400140 def getGeneralFlags(self, conf):
141 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400142 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesavento9bfdaef2022-10-05 17:38:44 -0400143 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesaventodfbeda22018-12-19 01:15:48 -0500144 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -0400145 return flags
146
Alexander Afanasyev76818632015-01-26 21:30:45 -0800147 def getDebugFlags(self, conf):
148 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Davide Pesavento17521592020-05-14 19:01:32 -0400149 flags['CXXFLAGS'] += ['-Og',
Alexander Afanasyev76818632015-01-26 21:30:45 -0800150 '-g3',
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100151 '-Wall',
152 '-Wextra',
Davide Pesavento3bc8e192022-12-31 01:40:11 -0500153 '-Wpedantic',
Alexander Afanasyev76818632015-01-26 21:30:45 -0800154 '-Werror',
Davide Pesavento17521592020-05-14 19:01:32 -0400155 '-Wcatch-value=2',
156 '-Wextra-semi',
Alexander Afanasyev847de402017-09-21 18:57:30 -0400157 '-Wnon-virtual-dtor',
Alexander Afanasyev436f3bd2016-10-11 16:37:45 -0700158 '-Wno-error=deprecated-declarations', # Bug #3795
Davide Pesaventoc0a5a392017-08-19 16:49:30 -0400159 '-Wno-error=maybe-uninitialized', # Bug #1615
160 '-Wno-unused-parameter',
Davide Pesaventof485d892015-10-02 02:00:54 +0200161 ]
Davide Pesaventodfbeda22018-12-19 01:15:48 -0500162 flags['LINKFLAGS'] += ['-Wl,-O1']
Alexander Afanasyev76818632015-01-26 21:30:45 -0800163 return flags
164
165 def getOptimizedFlags(self, conf):
166 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100167 flags['CXXFLAGS'] += ['-O2',
Davide Pesaventof485d892015-10-02 02:00:54 +0200168 '-g',
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100169 '-Wall',
170 '-Wextra',
Davide Pesavento3bc8e192022-12-31 01:40:11 -0500171 '-Wpedantic',
Davide Pesavento17521592020-05-14 19:01:32 -0400172 '-Wcatch-value=2',
173 '-Wextra-semi',
Alexander Afanasyev847de402017-09-21 18:57:30 -0400174 '-Wnon-virtual-dtor',
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100175 '-Wno-unused-parameter',
Davide Pesaventof485d892015-10-02 02:00:54 +0200176 ]
Davide Pesaventodfbeda22018-12-19 01:15:48 -0500177 flags['LINKFLAGS'] += ['-Wl,-O1']
Alexander Afanasyev76818632015-01-26 21:30:45 -0800178 return flags
179
180class GccFlags(GccBasicFlags):
Alexander Afanasyev76818632015-01-26 21:30:45 -0800181 def getDebugFlags(self, conf):
182 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesavento17521592020-05-14 19:01:32 -0400183 flags['CXXFLAGS'] += ['-fdiagnostics-color',
184 '-Wredundant-tags',
185 ]
Davide Pesaventoc52cd5e2022-03-05 20:40:54 -0500186 if platform.machine() == 'armv7l':
Davide Pesavento17521592020-05-14 19:01:32 -0400187 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Davide Pesaventof485d892015-10-02 02:00:54 +0200188 return flags
189
190 def getOptimizedFlags(self, conf):
191 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesavento17521592020-05-14 19:01:32 -0400192 flags['CXXFLAGS'] += ['-fdiagnostics-color',
193 '-Wredundant-tags',
194 ]
Davide Pesaventoc52cd5e2022-03-05 20:40:54 -0500195 if platform.machine() == 'armv7l':
Davide Pesavento17521592020-05-14 19:01:32 -0400196 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyev76818632015-01-26 21:30:45 -0800197 return flags
198
199class ClangFlags(GccBasicFlags):
200 def getGeneralFlags(self, conf):
201 flags = super(ClangFlags, self).getGeneralFlags(conf)
Davide Pesavento17521592020-05-14 19:01:32 -0400202 if Utils.unversioned_sys_platform() == 'darwin':
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500203 # Bug #4296
Davide Pesaventoe541d1b2022-08-17 15:10:32 -0400204 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
205 flags['CXXFLAGS'] += [['-isystem', f'{brewdir}/include'], # for Homebrew
Alexander Afanasyevb5220702017-09-21 18:57:00 -0400206 ['-isystem', '/opt/local/include']] # for MacPorts
Davide Pesavento17521592020-05-14 19:01:32 -0400207 elif Utils.unversioned_sys_platform() == 'freebsd':
208 # Bug #4790
209 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Alexander Afanasyev76818632015-01-26 21:30:45 -0800210 return flags
211
212 def getDebugFlags(self, conf):
213 flags = super(ClangFlags, self).getDebugFlags(conf)
Davide Pesaventof485d892015-10-02 02:00:54 +0200214 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesaventoc0a5a392017-08-19 16:49:30 -0400215 '-Wundefined-func-template',
Davide Pesaventoc0a5a392017-08-19 16:49:30 -0400216 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Davide Pesaventof485d892015-10-02 02:00:54 +0200217 ]
218 return flags
219
220 def getOptimizedFlags(self, conf):
221 flags = super(ClangFlags, self).getOptimizedFlags(conf)
222 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesaventoc0a5a392017-08-19 16:49:30 -0400223 '-Wundefined-func-template',
Davide Pesaventof485d892015-10-02 02:00:54 +0200224 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
225 ]
Alexander Afanasyev76818632015-01-26 21:30:45 -0800226 return flags