blob: 8c718717e3da2f62156c901c59a903bf6b4d7160 [file] [log] [blame]
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -07001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
2
Davide Pesavento333e3eb2021-10-12 19:30:51 -04003import platform
Davide Pesavento98ff2bc2018-05-24 00:10:31 -04004from waflib import Configure, Logs, Utils
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -07005
Shuo Chen478204c2014-03-18 18:27:04 -07006def options(opt):
Davide Pesavento98ff2bc2018-05-24 00:10:31 -04007 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesavento333e3eb2021-10-12 19:30:51 -04008 help='Compile in debugging mode with minimal optimizations (-Og)')
Shuo Chen478204c2014-03-18 18:27:04 -07009
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
14 ccver = tuple(int(i) for i in conf.env.CC_VERSION)
15 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 Pesaventoe4b74bd2022-03-09 18:56:37 -050019 if ccver < (7, 4, 0):
Davide Pesaventob545aac2017-09-22 23:54:10 -040020 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesavento60b52cf2023-04-26 15:27:02 -040021 '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 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':
28 if ccver < (10, 0, 0):
29 errmsg = ('The version of Xcode you are using is too old.\n'
Davide Pesavento60b52cf2023-04-26 15:27:02 -040030 '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 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 Pesavento60b52cf2023-04-26 15:27:02 -040036 'The minimum supported clang version is 7.0.')
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000037 conf.flags = ClangFlags()
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050038 else:
Davide Pesavento1d484532022-08-19 22:08:31 -040039 warnmsg = f'{cxx} compiler is unsupported'
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000040 conf.flags = CompilerFlags()
Davide Pesaventob545aac2017-09-22 23:54:10 -040041
42 if errmsg:
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040043 conf.end_msg(ccverstr, color='RED')
Davide Pesaventob545aac2017-09-22 23:54:10 -040044 conf.fatal(errmsg)
45 elif warnmsg:
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040046 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento333e3eb2021-10-12 19:30:51 -040047 Logs.warn('WARNING: ' + warnmsg)
Davide Pesaventob545aac2017-09-22 23:54:10 -040048 else:
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040049 conf.end_msg(ccverstr)
Shuo Chen478204c2014-03-18 18:27:04 -070050
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000051 conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050052
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040053 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000054 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050055 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
56 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
57 conf.env.DEFINES += generalFlags['DEFINES']
58
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000059@Configure.conf
60def check_compiler_flags(conf):
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050061 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
62 # corresponding environment variables are not set.
63 # DEFINES are always applied.
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070064 if conf.options.debug:
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000065 extraFlags = conf.flags.getDebugFlags(conf)
66 if conf.areCustomCxxflagsPresent:
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050067 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040068 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 Afanasyev3fd14f02014-03-26 14:34:39 -070072 else:
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000073 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070074
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000075 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050076 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
77 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
78
79 conf.env.DEFINES += extraFlags['DEFINES']
Wentao Shanga8f3c402014-10-30 14:03:27 -070080
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -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 Afanasyevd352cce2015-11-20 14:15:11 -050086 if len(cxxflags) == 0:
87 return
88
Wentao Shanga8f3c402014-10-30 14:03:27 -070089 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070090
91 supportedFlags = []
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000092 for flags in cxxflags:
93 flags = Utils.to_list(flags)
94 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
95 supportedFlags += flags
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070096
Shuo Chen478204c2014-03-18 18:27:04 -070097 self.end_msg(' '.join(supportedFlags))
Junxiao Shie1801312017-07-22 19:16:49 +000098 self.env.prepend_value('CXXFLAGS', supportedFlags)
Wentao Shanga8f3c402014-10-30 14:03:27 -070099
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 Afanasyevd352cce2015-11-20 14:15:11 -0500105 if len(linkflags) == 0:
106 return
107
Wentao Shanga8f3c402014-10-30 14:03:27 -0700108 self.start_msg('Checking supported LINKFLAGS')
109
110 supportedFlags = []
Alexander Afanasyevbb058c02018-02-15 22:49:24 +0000111 for flags in linkflags:
112 flags = Utils.to_list(flags)
113 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
114 supportedFlags += flags
Wentao Shanga8f3c402014-10-30 14:03:27 -0700115
116 self.end_msg(' '.join(supportedFlags))
Junxiao Shie1801312017-07-22 19:16:49 +0000117 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500118
119
120class CompilerFlags(object):
Davide Pesavento98ff2bc2018-05-24 00:10:31 -0400121 def getCompilerVersion(self, conf):
122 return tuple(int(i) for i in conf.env.CC_VERSION)
123
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500124 def getGeneralFlags(self, conf):
125 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
126 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
127
128 def getDebugFlags(self, conf):
129 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
130 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
131
132 def getOptimizedFlags(self, conf):
133 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
134 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
135
136class GccBasicFlags(CompilerFlags):
137 """
138 This class defines basic flags that work for both gcc and clang compilers
139 """
Junxiao Shie1801312017-07-22 19:16:49 +0000140 def getGeneralFlags(self, conf):
141 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
Davide Pesavento11904062022-04-14 22:33:28 -0400142 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesaventoc2414752022-11-16 16:56:45 -0500143 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesaventoed2f4762019-01-25 00:40:46 -0500144 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Junxiao Shie1801312017-07-22 19:16:49 +0000145 return flags
146
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500147 def getDebugFlags(self, conf):
148 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400149 flags['CXXFLAGS'] += ['-Og',
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500150 '-g3',
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500151 '-Wall',
152 '-Wextra',
Davide Pesaventoe9b09b82023-01-19 13:30:07 -0500153 '-Wpedantic',
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500154 '-Werror',
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400155 '-Wcatch-value=2',
156 '-Wextra-semi',
Junxiao Shie1801312017-07-22 19:16:49 +0000157 '-Wnon-virtual-dtor',
Junxiao Shie1801312017-07-22 19:16:49 +0000158 '-Wno-error=deprecated-declarations', # Bug #3795
Davide Pesaventob545aac2017-09-22 23:54:10 -0400159 '-Wno-error=maybe-uninitialized', # Bug #1615
160 '-Wno-unused-parameter',
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500161 ]
Davide Pesaventoed2f4762019-01-25 00:40:46 -0500162 flags['LINKFLAGS'] += ['-Wl,-O1']
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500163 return flags
164
165 def getOptimizedFlags(self, conf):
166 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
167 flags['CXXFLAGS'] += ['-O2',
168 '-g',
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500169 '-Wall',
170 '-Wextra',
Davide Pesaventoe9b09b82023-01-19 13:30:07 -0500171 '-Wpedantic',
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400172 '-Wcatch-value=2',
173 '-Wextra-semi',
Junxiao Shie1801312017-07-22 19:16:49 +0000174 '-Wnon-virtual-dtor',
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500175 '-Wno-unused-parameter',
176 ]
Davide Pesaventoed2f4762019-01-25 00:40:46 -0500177 flags['LINKFLAGS'] += ['-Wl,-O1']
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500178 return flags
179
180class GccFlags(GccBasicFlags):
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500181 def getDebugFlags(self, conf):
182 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400183 flags['CXXFLAGS'] += ['-fdiagnostics-color',
184 '-Wredundant-tags',
185 ]
Davide Pesaventoe4b74bd2022-03-09 18:56:37 -0500186 if platform.machine() == 'armv7l':
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400187 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500188 return flags
189
190 def getOptimizedFlags(self, conf):
191 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400192 flags['CXXFLAGS'] += ['-fdiagnostics-color',
193 '-Wredundant-tags',
194 ]
Davide Pesaventoe4b74bd2022-03-09 18:56:37 -0500195 if platform.machine() == 'armv7l':
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400196 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500197 return flags
198
199class ClangFlags(GccBasicFlags):
200 def getGeneralFlags(self, conf):
201 flags = super(ClangFlags, self).getGeneralFlags(conf)
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400202 if Utils.unversioned_sys_platform() == 'darwin':
Davide Pesavento98ff2bc2018-05-24 00:10:31 -0400203 # Bug #4296
Davide Pesavento1d484532022-08-19 22:08:31 -0400204 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
205 flags['CXXFLAGS'] += [['-isystem', f'{brewdir}/include'], # for Homebrew
Alexander Afanasyevbb058c02018-02-15 22:49:24 +0000206 ['-isystem', '/opt/local/include']] # for MacPorts
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400207 elif Utils.unversioned_sys_platform() == 'freebsd':
208 # Bug #4790
209 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500210 return flags
211
212 def getDebugFlags(self, conf):
213 flags = super(ClangFlags, self).getDebugFlags(conf)
214 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Alexander Afanasyevbb058c02018-02-15 22:49:24 +0000215 '-Wundefined-func-template',
Davide Pesaventob545aac2017-09-22 23:54:10 -0400216 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500217 ]
218 return flags
219
220 def getOptimizedFlags(self, conf):
221 flags = super(ClangFlags, self).getOptimizedFlags(conf)
222 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Alexander Afanasyevbb058c02018-02-15 22:49:24 +0000223 '-Wundefined-func-template',
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500224 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
225 ]
226 return flags