blob: 4debb4e2d9ade728bdea63f82da95cab56a702bd [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 Pesaventoc52cd5e2022-03-05 20:40:54 -050021 'The minimum supported gcc version is 7.4.')
Alexander Afanasyevb5220702017-09-21 18:57:00 -040022 conf.flags = GccFlags()
Alexander Afanasyev76818632015-01-26 21:30:45 -080023 elif cxx == 'clang':
Davide Pesaventoc52cd5e2022-03-05 20:40:54 -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 Pesaventoc0a5a392017-08-19 16:49:30 -040032 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesaventoc52cd5e2022-03-05 20:40:54 -050033 'The minimum supported clang version is 6.0.')
Alexander Afanasyevb5220702017-09-21 18:57:00 -040034 conf.flags = ClangFlags()
Alexander Afanasyev76818632015-01-26 21:30:45 -080035 else:
Davide Pesaventoe541d1b2022-08-17 15:10:32 -040036 warnmsg = f'{cxx} compiler is unsupported'
Alexander Afanasyevb5220702017-09-21 18:57:00 -040037 conf.flags = CompilerFlags()
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040038
39 if errmsg:
Davide Pesavento0064c1d2018-03-03 18:43:53 -050040 conf.end_msg(ccverstr, color='RED')
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040041 conf.fatal(errmsg)
42 elif warnmsg:
Davide Pesavento0064c1d2018-03-03 18:43:53 -050043 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento17521592020-05-14 19:01:32 -040044 Logs.warn('WARNING: ' + warnmsg)
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040045 else:
Davide Pesavento0064c1d2018-03-03 18:43:53 -050046 conf.end_msg(ccverstr)
Davide Pesavento1bdef282014-04-08 20:59:50 +020047
Alexander Afanasyevb5220702017-09-21 18:57:00 -040048 conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
Alexander Afanasyev76818632015-01-26 21:30:45 -080049
Davide Pesaventoc0822fa2018-05-10 21:54:10 -040050 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyevb5220702017-09-21 18:57:00 -040051 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyev76818632015-01-26 21:30:45 -080052 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
53 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
54 conf.env.DEFINES += generalFlags['DEFINES']
55
Alexander Afanasyevb5220702017-09-21 18:57:00 -040056@Configure.conf
57def check_compiler_flags(conf):
Davide Pesaventof485d892015-10-02 02:00:54 +020058 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Alexander Afanasyev76818632015-01-26 21:30:45 -080059 # corresponding environment variables are not set.
Davide Pesaventof485d892015-10-02 02:00:54 +020060 # DEFINES are always applied.
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070061 if conf.options.debug:
Alexander Afanasyevb5220702017-09-21 18:57:00 -040062 extraFlags = conf.flags.getDebugFlags(conf)
63 if conf.areCustomCxxflagsPresent:
Alexander Afanasyev76818632015-01-26 21:30:45 -080064 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Davide Pesavento0064c1d2018-03-03 18:43:53 -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))
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070069 else:
Alexander Afanasyevb5220702017-09-21 18:57:00 -040070 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070071
Alexander Afanasyevb5220702017-09-21 18:57:00 -040072 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyev76818632015-01-26 21:30:45 -080073 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Davide Pesaventoed89ccc2015-11-01 20:35:04 +010074 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Alexander Afanasyev76818632015-01-26 21:30:45 -080075
76 conf.env.DEFINES += extraFlags['DEFINES']
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020077
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -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 Afanasyev76818632015-01-26 21:30:45 -080083 if len(cxxflags) == 0:
84 return
85
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020086 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070087
88 supportedFlags = []
Alexander Afanasyevb5220702017-09-21 18:57:00 -040089 for flags in cxxflags:
90 flags = Utils.to_list(flags)
91 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
92 supportedFlags += flags
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070093
Davide Pesavento1bdef282014-04-08 20:59:50 +020094 self.end_msg(' '.join(supportedFlags))
Davide Pesavento67f30272016-08-10 01:55:16 +000095 self.env.prepend_value('CXXFLAGS', supportedFlags)
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020096
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 Afanasyev76818632015-01-26 21:30:45 -0800102 if len(linkflags) == 0:
103 return
104
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200105 self.start_msg('Checking supported LINKFLAGS')
106
107 supportedFlags = []
Alexander Afanasyevb5220702017-09-21 18:57:00 -0400108 for flags in linkflags:
109 flags = Utils.to_list(flags)
110 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
111 supportedFlags += flags
Davide Pesaventoab1e8f22014-10-21 22:45:33 +0200112
113 self.end_msg(' '.join(supportedFlags))
Davide Pesavento67f30272016-08-10 01:55:16 +0000114 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyev76818632015-01-26 21:30:45 -0800115
116
117class CompilerFlags(object):
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500118 def getCompilerVersion(self, conf):
119 return tuple(int(i) for i in conf.env.CC_VERSION)
120
Alexander Afanasyev76818632015-01-26 21:30:45 -0800121 def getGeneralFlags(self, conf):
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100122 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Alexander Afanasyev76818632015-01-26 21:30:45 -0800123 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
124
125 def getDebugFlags(self, conf):
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100126 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Alexander Afanasyev76818632015-01-26 21:30:45 -0800127 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
128
129 def getOptimizedFlags(self, conf):
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100130 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Alexander Afanasyev76818632015-01-26 21:30:45 -0800131 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
132
133class GccBasicFlags(CompilerFlags):
Davide Pesaventof485d892015-10-02 02:00:54 +0200134 """
135 This class defines basic flags that work for both gcc and clang compilers
136 """
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -0400137 def getGeneralFlags(self, conf):
138 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400139 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesavento9bfdaef2022-10-05 17:38:44 -0400140 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesaventodfbeda22018-12-19 01:15:48 -0500141 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -0400142 return flags
143
Alexander Afanasyev76818632015-01-26 21:30:45 -0800144 def getDebugFlags(self, conf):
145 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Davide Pesavento17521592020-05-14 19:01:32 -0400146 flags['CXXFLAGS'] += ['-Og',
Alexander Afanasyev76818632015-01-26 21:30:45 -0800147 '-g3',
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100148 '-pedantic',
149 '-Wall',
150 '-Wextra',
Alexander Afanasyev76818632015-01-26 21:30:45 -0800151 '-Werror',
Davide Pesavento17521592020-05-14 19:01:32 -0400152 '-Wcatch-value=2',
153 '-Wextra-semi',
Alexander Afanasyev847de402017-09-21 18:57:30 -0400154 '-Wnon-virtual-dtor',
Alexander Afanasyev436f3bd2016-10-11 16:37:45 -0700155 '-Wno-error=deprecated-declarations', # Bug #3795
Davide Pesaventoc0a5a392017-08-19 16:49:30 -0400156 '-Wno-error=maybe-uninitialized', # Bug #1615
157 '-Wno-unused-parameter',
Davide Pesaventof485d892015-10-02 02:00:54 +0200158 ]
Davide Pesaventodfbeda22018-12-19 01:15:48 -0500159 flags['LINKFLAGS'] += ['-Wl,-O1']
Alexander Afanasyev76818632015-01-26 21:30:45 -0800160 return flags
161
162 def getOptimizedFlags(self, conf):
163 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100164 flags['CXXFLAGS'] += ['-O2',
Davide Pesaventof485d892015-10-02 02:00:54 +0200165 '-g',
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100166 '-pedantic',
167 '-Wall',
168 '-Wextra',
Davide Pesavento17521592020-05-14 19:01:32 -0400169 '-Wcatch-value=2',
170 '-Wextra-semi',
Alexander Afanasyev847de402017-09-21 18:57:30 -0400171 '-Wnon-virtual-dtor',
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100172 '-Wno-unused-parameter',
Davide Pesaventof485d892015-10-02 02:00:54 +0200173 ]
Davide Pesaventodfbeda22018-12-19 01:15:48 -0500174 flags['LINKFLAGS'] += ['-Wl,-O1']
Alexander Afanasyev76818632015-01-26 21:30:45 -0800175 return flags
176
177class GccFlags(GccBasicFlags):
Alexander Afanasyev76818632015-01-26 21:30:45 -0800178 def getDebugFlags(self, conf):
179 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesavento17521592020-05-14 19:01:32 -0400180 flags['CXXFLAGS'] += ['-fdiagnostics-color',
181 '-Wredundant-tags',
182 ]
Davide Pesaventoc52cd5e2022-03-05 20:40:54 -0500183 if platform.machine() == 'armv7l':
Davide Pesavento17521592020-05-14 19:01:32 -0400184 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Davide Pesaventof485d892015-10-02 02:00:54 +0200185 return flags
186
187 def getOptimizedFlags(self, conf):
188 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesavento17521592020-05-14 19:01:32 -0400189 flags['CXXFLAGS'] += ['-fdiagnostics-color',
190 '-Wredundant-tags',
191 ]
Davide Pesaventoc52cd5e2022-03-05 20:40:54 -0500192 if platform.machine() == 'armv7l':
Davide Pesavento17521592020-05-14 19:01:32 -0400193 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyev76818632015-01-26 21:30:45 -0800194 return flags
195
196class ClangFlags(GccBasicFlags):
197 def getGeneralFlags(self, conf):
198 flags = super(ClangFlags, self).getGeneralFlags(conf)
Davide Pesavento17521592020-05-14 19:01:32 -0400199 if Utils.unversioned_sys_platform() == 'darwin':
Davide Pesavento0064c1d2018-03-03 18:43:53 -0500200 # Bug #4296
Davide Pesaventoe541d1b2022-08-17 15:10:32 -0400201 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
202 flags['CXXFLAGS'] += [['-isystem', f'{brewdir}/include'], # for Homebrew
Alexander Afanasyevb5220702017-09-21 18:57:00 -0400203 ['-isystem', '/opt/local/include']] # for MacPorts
Davide Pesavento17521592020-05-14 19:01:32 -0400204 elif Utils.unversioned_sys_platform() == 'freebsd':
205 # Bug #4790
206 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Alexander Afanasyev76818632015-01-26 21:30:45 -0800207 return flags
208
209 def getDebugFlags(self, conf):
210 flags = super(ClangFlags, self).getDebugFlags(conf)
Davide Pesaventof485d892015-10-02 02:00:54 +0200211 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesaventoc0a5a392017-08-19 16:49:30 -0400212 '-Wundefined-func-template',
Davide Pesaventoc0a5a392017-08-19 16:49:30 -0400213 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Davide Pesaventof485d892015-10-02 02:00:54 +0200214 ]
215 return flags
216
217 def getOptimizedFlags(self, conf):
218 flags = super(ClangFlags, self).getOptimizedFlags(conf)
219 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesaventoc0a5a392017-08-19 16:49:30 -0400220 '-Wundefined-func-template',
Davide Pesaventof485d892015-10-02 02:00:54 +0200221 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
222 ]
Alexander Afanasyev76818632015-01-26 21:30:45 -0800223 return flags