blob: 8c718717e3da2f62156c901c59a903bf6b4d7160 [file] [log] [blame]
Alexander Afanasyev7eb59112014-07-02 14:21:11 -07001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
Alexander Afanasyev7eb59112014-07-02 14:21:11 -07002
Davide Pesavento5f408ae2020-07-15 21:17:04 -04003import platform
Alexander Afanasyev40491df2018-03-09 16:29:52 -05004from waflib import Configure, Logs, Utils
Alexander Afanasyev7eb59112014-07-02 14:21:11 -07005
6def options(opt):
Alexander Afanasyev40491df2018-03-09 16:29:52 -05007 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesavento5f408ae2020-07-15 21:17:04 -04008 help='Compile in debugging mode with minimal optimizations (-Og)')
Alexander Afanasyev7eb59112014-07-02 14:21:11 -07009
10def configure(conf):
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040011 conf.start_msg('Checking C++ compiler version')
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070012
Alexander Afanasyev40491df2018-03-09 16:29:52 -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)
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040016 errmsg = ''
17 warnmsg = ''
18 if cxx == 'gcc':
Davide Pesavento26a03262022-03-07 13:37:34 -050019 if ccver < (7, 4, 0):
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040020 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesaventoed778702023-04-26 15:31:31 -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 Afanasyev12d5faa2017-09-21 19:04:22 -040025 conf.flags = GccFlags()
26 elif cxx == 'clang':
Davide Pesavento26a03262022-03-07 13:37:34 -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 Pesaventoed778702023-04-26 15:31:31 -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 Pesavento26a03262022-03-07 13:37:34 -050033 'officially supported and may result in build failures.')
Davide Pesaventoed778702023-04-26 15:31:31 -040034 elif ccver < (7, 0, 0):
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040035 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesaventoed778702023-04-26 15:31:31 -040036 'The minimum supported clang version is 7.0.')
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040037 conf.flags = ClangFlags()
38 else:
Davide Pesavento15a74422022-08-19 15:48:45 -040039 warnmsg = f'{cxx} compiler is unsupported'
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040040 conf.flags = CompilerFlags()
41
42 if errmsg:
Alexander Afanasyev40491df2018-03-09 16:29:52 -050043 conf.end_msg(ccverstr, color='RED')
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040044 conf.fatal(errmsg)
45 elif warnmsg:
Alexander Afanasyev40491df2018-03-09 16:29:52 -050046 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento5f408ae2020-07-15 21:17:04 -040047 Logs.warn('WARNING: ' + warnmsg)
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040048 else:
Alexander Afanasyev40491df2018-03-09 16:29:52 -050049 conf.end_msg(ccverstr)
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040050
51 conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -080052
Davide Pesavento4a9395b2018-05-24 00:23:22 -040053 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040054 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -080055 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
56 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
57 conf.env.DEFINES += generalFlags['DEFINES']
58
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040059@Configure.conf
60def check_compiler_flags(conf):
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -080061 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
62 # corresponding environment variables are not set.
63 # DEFINES are always applied.
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070064 if conf.options.debug:
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040065 extraFlags = conf.flags.getDebugFlags(conf)
66 if conf.areCustomCxxflagsPresent:
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -080067 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev40491df2018-03-09 16:29:52 -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 Afanasyev7eb59112014-07-02 14:21:11 -070072 else:
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040073 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070074
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -040075 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -080076 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
77 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
78
79 conf.env.DEFINES += extraFlags['DEFINES']
Yingdi Yu906c2ea2014-10-31 11:24:50 -070080
Alexander Afanasyev7eb59112014-07-02 14:21: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 Afanasyevf3192eb2016-12-19 17:11:20 -080086 if len(cxxflags) == 0:
87 return
88
Yingdi Yu906c2ea2014-10-31 11:24:50 -070089 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev7eb59112014-07-02 14:21:11 -070090
91 supportedFlags = []
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -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 Afanasyev7eb59112014-07-02 14:21:11 -070096
97 self.end_msg(' '.join(supportedFlags))
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -080098 self.env.prepend_value('CXXFLAGS', supportedFlags)
Yingdi Yu906c2ea2014-10-31 11:24:50 -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 Afanasyevf3192eb2016-12-19 17:11:20 -0800105 if len(linkflags) == 0:
106 return
107
Yingdi Yu906c2ea2014-10-31 11:24:50 -0700108 self.start_msg('Checking supported LINKFLAGS')
109
110 supportedFlags = []
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -0400111 for flags in linkflags:
112 flags = Utils.to_list(flags)
113 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
114 supportedFlags += flags
Yingdi Yu906c2ea2014-10-31 11:24:50 -0700115
116 self.end_msg(' '.join(supportedFlags))
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800117 self.env.prepend_value('LINKFLAGS', supportedFlags)
118
119
120class CompilerFlags(object):
Alexander Afanasyev40491df2018-03-09 16:29:52 -0500121 def getCompilerVersion(self, conf):
122 return tuple(int(i) for i in conf.env.CC_VERSION)
123
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800124 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 """
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -0400140 def getGeneralFlags(self, conf):
141 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
Davide Pesavento8663ed12022-07-23 03:04:27 -0400142 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesaventocda44892022-11-15 13:40:33 -0500143 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesaventofae9def2019-01-29 14:34:33 -0500144 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -0400145 return flags
146
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800147 def getDebugFlags(self, conf):
148 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Davide Pesavento5f408ae2020-07-15 21:17:04 -0400149 flags['CXXFLAGS'] += ['-Og',
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800150 '-g3',
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800151 '-Wall',
152 '-Wextra',
Davide Pesavento3fb27eb2022-12-31 14:00:51 -0500153 '-Wpedantic',
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800154 '-Werror',
Davide Pesavento5f408ae2020-07-15 21:17:04 -0400155 '-Wcatch-value=2',
156 '-Wextra-semi',
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -0400157 '-Wnon-virtual-dtor',
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800158 '-Wno-error=deprecated-declarations', # Bug #3795
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -0400159 '-Wno-error=maybe-uninitialized', # Bug #1615
160 '-Wno-unused-parameter',
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800161 ]
Davide Pesaventofae9def2019-01-29 14:34:33 -0500162 flags['LINKFLAGS'] += ['-Wl,-O1']
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800163 return flags
164
165 def getOptimizedFlags(self, conf):
166 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
167 flags['CXXFLAGS'] += ['-O2',
168 '-g',
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800169 '-Wall',
170 '-Wextra',
Davide Pesavento3fb27eb2022-12-31 14:00:51 -0500171 '-Wpedantic',
Davide Pesavento5f408ae2020-07-15 21:17:04 -0400172 '-Wcatch-value=2',
173 '-Wextra-semi',
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -0400174 '-Wnon-virtual-dtor',
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800175 '-Wno-unused-parameter',
176 ]
Davide Pesaventofae9def2019-01-29 14:34:33 -0500177 flags['LINKFLAGS'] += ['-Wl,-O1']
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800178 return flags
179
180class GccFlags(GccBasicFlags):
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800181 def getDebugFlags(self, conf):
182 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesavento5f408ae2020-07-15 21:17:04 -0400183 flags['CXXFLAGS'] += ['-fdiagnostics-color',
184 '-Wredundant-tags',
185 ]
Davide Pesavento26a03262022-03-07 13:37:34 -0500186 if platform.machine() == 'armv7l':
Davide Pesavento5f408ae2020-07-15 21:17:04 -0400187 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800188 return flags
189
190 def getOptimizedFlags(self, conf):
191 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesavento5f408ae2020-07-15 21:17:04 -0400192 flags['CXXFLAGS'] += ['-fdiagnostics-color',
193 '-Wredundant-tags',
194 ]
Davide Pesavento26a03262022-03-07 13:37:34 -0500195 if platform.machine() == 'armv7l':
Davide Pesavento5f408ae2020-07-15 21:17:04 -0400196 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800197 return flags
198
199class ClangFlags(GccBasicFlags):
200 def getGeneralFlags(self, conf):
201 flags = super(ClangFlags, self).getGeneralFlags(conf)
Davide Pesavento5f408ae2020-07-15 21:17:04 -0400202 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev40491df2018-03-09 16:29:52 -0500203 # Bug #4296
Davide Pesavento15a74422022-08-19 15:48:45 -0400204 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
205 flags['CXXFLAGS'] += [['-isystem', f'{brewdir}/include'], # for Homebrew
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -0400206 ['-isystem', '/opt/local/include']] # for MacPorts
Davide Pesavento5f408ae2020-07-15 21:17:04 -0400207 elif Utils.unversioned_sys_platform() == 'freebsd':
208 # Bug #4790
209 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800210 return flags
211
212 def getDebugFlags(self, conf):
213 flags = super(ClangFlags, self).getDebugFlags(conf)
214 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -0400215 '-Wundefined-func-template',
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -0400216 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800217 ]
218 return flags
219
220 def getOptimizedFlags(self, conf):
221 flags = super(ClangFlags, self).getOptimizedFlags(conf)
222 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Alexander Afanasyev12d5faa2017-09-21 19:04:22 -0400223 '-Wundefined-func-template',
Alexander Afanasyevf3192eb2016-12-19 17:11:20 -0800224 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
225 ]
226 return flags