blob: 8c718717e3da2f62156c901c59a903bf6b4d7160 [file] [log] [blame]
Alexander Afanasyev740812e2014-10-30 15:37:45 -07001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
2
Davide Pesaventoab6ac5d2021-10-05 23:23:53 -04003import platform
Davide Pesavento64cafaa2018-05-24 00:50:41 -04004from waflib import Configure, Logs, Utils
Alexander Afanasyev740812e2014-10-30 15:37:45 -07005
6def options(opt):
Davide Pesavento64cafaa2018-05-24 00:50:41 -04007 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
Davide Pesaventoab6ac5d2021-10-05 23:23:53 -04008 help='Compile in debugging mode with minimal optimizations (-Og)')
Alexander Afanasyev740812e2014-10-30 15:37:45 -07009
10def configure(conf):
Davide Pesavento29db0fd2017-08-29 13:32:00 -040011 conf.start_msg('Checking C++ compiler version')
Alexander Afanasyev740812e2014-10-30 15:37:45 -070012
Davide Pesavento64cafaa2018-05-24 00:50:41 -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 Pesavento29db0fd2017-08-29 13:32:00 -040016 errmsg = ''
17 warnmsg = ''
18 if cxx == 'gcc':
Davide Pesavento92669062022-03-10 19:09:44 -050019 if ccver < (7, 4, 0):
Davide Pesavento29db0fd2017-08-29 13:32:00 -040020 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesavento08208492023-04-17 01:25:01 -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.')
Davide Pesavento64cafaa2018-05-24 00:50:41 -040025 conf.flags = GccFlags()
Davide Pesavento29db0fd2017-08-29 13:32:00 -040026 elif cxx == 'clang':
Davide Pesavento92669062022-03-10 19:09:44 -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 Pesavento08208492023-04-17 01:25:01 -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 Pesavento92669062022-03-10 19:09:44 -050033 'officially supported and may result in build failures.')
Davide Pesavento08208492023-04-17 01:25:01 -040034 elif ccver < (7, 0, 0):
Davide Pesavento29db0fd2017-08-29 13:32:00 -040035 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesavento08208492023-04-17 01:25:01 -040036 'The minimum supported clang version is 7.0.')
Davide Pesavento64cafaa2018-05-24 00:50:41 -040037 conf.flags = ClangFlags()
Davide Pesavento29db0fd2017-08-29 13:32:00 -040038 else:
Davide Pesaventof5006be2022-08-19 17:36:36 -040039 warnmsg = f'{cxx} compiler is unsupported'
Davide Pesavento64cafaa2018-05-24 00:50:41 -040040 conf.flags = CompilerFlags()
Davide Pesavento29db0fd2017-08-29 13:32:00 -040041
42 if errmsg:
Davide Pesavento64cafaa2018-05-24 00:50:41 -040043 conf.end_msg(ccverstr, color='RED')
Davide Pesavento29db0fd2017-08-29 13:32:00 -040044 conf.fatal(errmsg)
45 elif warnmsg:
Davide Pesavento64cafaa2018-05-24 00:50:41 -040046 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesaventoab6ac5d2021-10-05 23:23:53 -040047 Logs.warn('WARNING: ' + warnmsg)
Davide Pesavento29db0fd2017-08-29 13:32:00 -040048 else:
Davide Pesavento64cafaa2018-05-24 00:50:41 -040049 conf.end_msg(ccverstr)
Davide Pesavento29db0fd2017-08-29 13:32:00 -040050
Davide Pesavento64cafaa2018-05-24 00:50:41 -040051 conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
Davide Pesavento29db0fd2017-08-29 13:32:00 -040052
Davide Pesavento64cafaa2018-05-24 00:50:41 -040053 # General flags are always applied (e.g., selecting C++ language standard)
54 generalFlags = conf.flags.getGeneralFlags(conf)
Davide Pesavento29db0fd2017-08-29 13:32:00 -040055 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
56 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
57 conf.env.DEFINES += generalFlags['DEFINES']
58
Davide Pesavento64cafaa2018-05-24 00:50:41 -040059@Configure.conf
60def check_compiler_flags(conf):
Davide Pesavento29db0fd2017-08-29 13:32:00 -040061 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
62 # corresponding environment variables are not set.
63 # DEFINES are always applied.
Alexander Afanasyev740812e2014-10-30 15:37:45 -070064 if conf.options.debug:
Davide Pesavento64cafaa2018-05-24 00:50:41 -040065 extraFlags = conf.flags.getDebugFlags(conf)
66 if conf.areCustomCxxflagsPresent:
Davide Pesavento29db0fd2017-08-29 13:32:00 -040067 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Davide Pesavento64cafaa2018-05-24 00:50:41 -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 Afanasyev740812e2014-10-30 15:37:45 -070072 else:
Davide Pesavento64cafaa2018-05-24 00:50:41 -040073 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev740812e2014-10-30 15:37:45 -070074
Davide Pesavento64cafaa2018-05-24 00:50:41 -040075 if not conf.areCustomCxxflagsPresent:
Davide Pesavento29db0fd2017-08-29 13:32:00 -040076 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
77 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
78
79 conf.env.DEFINES += extraFlags['DEFINES']
Alexander Afanasyev740812e2014-10-30 15:37:45 -070080
81@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 """
Davide Pesavento29db0fd2017-08-29 13:32:00 -040086 if len(cxxflags) == 0:
87 return
88
Alexander Afanasyev740812e2014-10-30 15:37:45 -070089 self.start_msg('Checking supported CXXFLAGS')
90
91 supportedFlags = []
Davide Pesavento64cafaa2018-05-24 00:50:41 -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 Afanasyev740812e2014-10-30 15:37:45 -070096
97 self.end_msg(' '.join(supportedFlags))
Davide Pesavento29db0fd2017-08-29 13:32:00 -040098 self.env.prepend_value('CXXFLAGS', supportedFlags)
Alexander Afanasyev740812e2014-10-30 15:37:45 -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 """
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400105 if len(linkflags) == 0:
106 return
107
Alexander Afanasyev740812e2014-10-30 15:37:45 -0700108 self.start_msg('Checking supported LINKFLAGS')
109
110 supportedFlags = []
Davide Pesavento64cafaa2018-05-24 00:50:41 -0400111 for flags in linkflags:
112 flags = Utils.to_list(flags)
113 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
114 supportedFlags += flags
Alexander Afanasyev740812e2014-10-30 15:37:45 -0700115
116 self.end_msg(' '.join(supportedFlags))
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400117 self.env.prepend_value('LINKFLAGS', supportedFlags)
118
119
120class CompilerFlags(object):
Davide Pesavento64cafaa2018-05-24 00:50:41 -0400121 def getCompilerVersion(self, conf):
122 return tuple(int(i) for i in conf.env.CC_VERSION)
123
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400124 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 """
140 def getGeneralFlags(self, conf):
141 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
Davide Pesaventoef064892022-04-05 02:26:03 -0400142 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesaventof855ec12022-11-16 16:08:35 -0500143 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesavento7eaed5c2019-01-09 20:03:43 -0500144 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400145 return flags
146
147 def getDebugFlags(self, conf):
148 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Davide Pesaventoab6ac5d2021-10-05 23:23:53 -0400149 flags['CXXFLAGS'] += ['-Og',
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400150 '-g3',
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400151 '-Wall',
152 '-Wextra',
Davide Pesavento326ec852023-02-14 16:42:33 -0500153 '-Wpedantic',
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400154 '-Werror',
Davide Pesaventoab6ac5d2021-10-05 23:23:53 -0400155 '-Wcatch-value=2',
156 '-Wextra-semi',
Davide Pesavento64cafaa2018-05-24 00:50:41 -0400157 '-Wnon-virtual-dtor',
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400158 '-Wno-error=deprecated-declarations', # Bug #3795
159 '-Wno-error=maybe-uninitialized', # Bug #1615
160 '-Wno-unused-parameter',
161 ]
Davide Pesavento7eaed5c2019-01-09 20:03:43 -0500162 flags['LINKFLAGS'] += ['-Wl,-O1']
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400163 return flags
164
165 def getOptimizedFlags(self, conf):
166 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
167 flags['CXXFLAGS'] += ['-O2',
168 '-g',
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400169 '-Wall',
170 '-Wextra',
Davide Pesavento326ec852023-02-14 16:42:33 -0500171 '-Wpedantic',
Davide Pesaventoab6ac5d2021-10-05 23:23:53 -0400172 '-Wcatch-value=2',
173 '-Wextra-semi',
Davide Pesavento64cafaa2018-05-24 00:50:41 -0400174 '-Wnon-virtual-dtor',
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400175 '-Wno-unused-parameter',
176 ]
Davide Pesavento7eaed5c2019-01-09 20:03:43 -0500177 flags['LINKFLAGS'] += ['-Wl,-O1']
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400178 return flags
179
180class GccFlags(GccBasicFlags):
181 def getDebugFlags(self, conf):
182 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesaventoab6ac5d2021-10-05 23:23:53 -0400183 flags['CXXFLAGS'] += ['-fdiagnostics-color',
184 '-Wredundant-tags',
185 ]
Davide Pesavento92669062022-03-10 19:09:44 -0500186 if platform.machine() == 'armv7l':
Davide Pesaventoab6ac5d2021-10-05 23:23:53 -0400187 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400188 return flags
189
190 def getOptimizedFlags(self, conf):
191 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesaventoab6ac5d2021-10-05 23:23:53 -0400192 flags['CXXFLAGS'] += ['-fdiagnostics-color',
193 '-Wredundant-tags',
194 ]
Davide Pesavento92669062022-03-10 19:09:44 -0500195 if platform.machine() == 'armv7l':
Davide Pesaventoab6ac5d2021-10-05 23:23:53 -0400196 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400197 return flags
198
199class ClangFlags(GccBasicFlags):
Davide Pesavento64cafaa2018-05-24 00:50:41 -0400200 def getGeneralFlags(self, conf):
201 flags = super(ClangFlags, self).getGeneralFlags(conf)
Davide Pesaventoab6ac5d2021-10-05 23:23:53 -0400202 if Utils.unversioned_sys_platform() == 'darwin':
Davide Pesavento64cafaa2018-05-24 00:50:41 -0400203 # Bug #4296
Davide Pesaventof5006be2022-08-19 17:36:36 -0400204 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
205 flags['CXXFLAGS'] += [['-isystem', f'{brewdir}/include'], # for Homebrew
Davide Pesavento64cafaa2018-05-24 00:50:41 -0400206 ['-isystem', '/opt/local/include']] # for MacPorts
Davide Pesaventoab6ac5d2021-10-05 23:23:53 -0400207 elif Utils.unversioned_sys_platform() == 'freebsd':
208 # Bug #4790
209 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Davide Pesavento64cafaa2018-05-24 00:50:41 -0400210 return flags
211
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400212 def getDebugFlags(self, conf):
213 flags = super(ClangFlags, self).getDebugFlags(conf)
Davide Pesavento7eaed5c2019-01-09 20:03:43 -0500214 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesavento7eaed5c2019-01-09 20:03:43 -0500215 '-Wundefined-func-template',
216 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
217 ]
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400218 return flags
219
220 def getOptimizedFlags(self, conf):
221 flags = super(ClangFlags, self).getOptimizedFlags(conf)
Davide Pesavento7eaed5c2019-01-09 20:03:43 -0500222 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesavento7eaed5c2019-01-09 20:03:43 -0500223 '-Wundefined-func-template',
224 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
225 ]
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400226 return flags