blob: 4debb4e2d9ade728bdea63f82da95cab56a702bd [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 Pesavento92669062022-03-10 19:09:44 -050021 'The minimum supported gcc version is 7.4.')
Davide Pesavento64cafaa2018-05-24 00:50:41 -040022 conf.flags = GccFlags()
Davide Pesavento29db0fd2017-08-29 13:32:00 -040023 elif cxx == 'clang':
Davide Pesavento92669062022-03-10 19:09:44 -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 Pesavento29db0fd2017-08-29 13:32:00 -040032 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesavento92669062022-03-10 19:09:44 -050033 'The minimum supported clang version is 6.0.')
Davide Pesavento64cafaa2018-05-24 00:50:41 -040034 conf.flags = ClangFlags()
Davide Pesavento29db0fd2017-08-29 13:32:00 -040035 else:
Davide Pesaventof5006be2022-08-19 17:36:36 -040036 warnmsg = f'{cxx} compiler is unsupported'
Davide Pesavento64cafaa2018-05-24 00:50:41 -040037 conf.flags = CompilerFlags()
Davide Pesavento29db0fd2017-08-29 13:32:00 -040038
39 if errmsg:
Davide Pesavento64cafaa2018-05-24 00:50:41 -040040 conf.end_msg(ccverstr, color='RED')
Davide Pesavento29db0fd2017-08-29 13:32:00 -040041 conf.fatal(errmsg)
42 elif warnmsg:
Davide Pesavento64cafaa2018-05-24 00:50:41 -040043 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesaventoab6ac5d2021-10-05 23:23:53 -040044 Logs.warn('WARNING: ' + warnmsg)
Davide Pesavento29db0fd2017-08-29 13:32:00 -040045 else:
Davide Pesavento64cafaa2018-05-24 00:50:41 -040046 conf.end_msg(ccverstr)
Davide Pesavento29db0fd2017-08-29 13:32:00 -040047
Davide Pesavento64cafaa2018-05-24 00:50:41 -040048 conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
Davide Pesavento29db0fd2017-08-29 13:32:00 -040049
Davide Pesavento64cafaa2018-05-24 00:50:41 -040050 # General flags are always applied (e.g., selecting C++ language standard)
51 generalFlags = conf.flags.getGeneralFlags(conf)
Davide Pesavento29db0fd2017-08-29 13:32:00 -040052 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
53 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
54 conf.env.DEFINES += generalFlags['DEFINES']
55
Davide Pesavento64cafaa2018-05-24 00:50:41 -040056@Configure.conf
57def check_compiler_flags(conf):
Davide Pesavento29db0fd2017-08-29 13:32:00 -040058 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
59 # corresponding environment variables are not set.
60 # DEFINES are always applied.
Alexander Afanasyev740812e2014-10-30 15:37:45 -070061 if conf.options.debug:
Davide Pesavento64cafaa2018-05-24 00:50:41 -040062 extraFlags = conf.flags.getDebugFlags(conf)
63 if conf.areCustomCxxflagsPresent:
Davide Pesavento29db0fd2017-08-29 13:32:00 -040064 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Davide Pesavento64cafaa2018-05-24 00:50:41 -040065 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 Afanasyev740812e2014-10-30 15:37:45 -070069 else:
Davide Pesavento64cafaa2018-05-24 00:50:41 -040070 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev740812e2014-10-30 15:37:45 -070071
Davide Pesavento64cafaa2018-05-24 00:50:41 -040072 if not conf.areCustomCxxflagsPresent:
Davide Pesavento29db0fd2017-08-29 13:32:00 -040073 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
74 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
75
76 conf.env.DEFINES += extraFlags['DEFINES']
Alexander Afanasyev740812e2014-10-30 15:37:45 -070077
78@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 """
Davide Pesavento29db0fd2017-08-29 13:32:00 -040083 if len(cxxflags) == 0:
84 return
85
Alexander Afanasyev740812e2014-10-30 15:37:45 -070086 self.start_msg('Checking supported CXXFLAGS')
87
88 supportedFlags = []
Davide Pesavento64cafaa2018-05-24 00:50:41 -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 Afanasyev740812e2014-10-30 15:37:45 -070093
94 self.end_msg(' '.join(supportedFlags))
Davide Pesavento29db0fd2017-08-29 13:32:00 -040095 self.env.prepend_value('CXXFLAGS', supportedFlags)
Alexander Afanasyev740812e2014-10-30 15:37:45 -070096
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 """
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400102 if len(linkflags) == 0:
103 return
104
Alexander Afanasyev740812e2014-10-30 15:37:45 -0700105 self.start_msg('Checking supported LINKFLAGS')
106
107 supportedFlags = []
Davide Pesavento64cafaa2018-05-24 00:50:41 -0400108 for flags in linkflags:
109 flags = Utils.to_list(flags)
110 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
111 supportedFlags += flags
Alexander Afanasyev740812e2014-10-30 15:37:45 -0700112
113 self.end_msg(' '.join(supportedFlags))
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400114 self.env.prepend_value('LINKFLAGS', supportedFlags)
115
116
117class CompilerFlags(object):
Davide Pesavento64cafaa2018-05-24 00:50:41 -0400118 def getCompilerVersion(self, conf):
119 return tuple(int(i) for i in conf.env.CC_VERSION)
120
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400121 def getGeneralFlags(self, conf):
122 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
123 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
124
125 def getDebugFlags(self, conf):
126 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
127 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
128
129 def getOptimizedFlags(self, conf):
130 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
131 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
132
133class GccBasicFlags(CompilerFlags):
134 """
135 This class defines basic flags that work for both gcc and clang compilers
136 """
137 def getGeneralFlags(self, conf):
138 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
Davide Pesaventoef064892022-04-05 02:26:03 -0400139 flags['CXXFLAGS'] += ['-std=c++17']
Davide Pesaventof855ec12022-11-16 16:08:35 -0500140 if Utils.unversioned_sys_platform() != 'darwin':
Davide Pesavento7eaed5c2019-01-09 20:03:43 -0500141 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400142 return flags
143
144 def getDebugFlags(self, conf):
145 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Davide Pesaventoab6ac5d2021-10-05 23:23:53 -0400146 flags['CXXFLAGS'] += ['-Og',
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400147 '-g3',
148 '-pedantic',
149 '-Wall',
150 '-Wextra',
151 '-Werror',
Davide Pesaventoab6ac5d2021-10-05 23:23:53 -0400152 '-Wcatch-value=2',
153 '-Wextra-semi',
Davide Pesavento64cafaa2018-05-24 00:50:41 -0400154 '-Wnon-virtual-dtor',
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400155 '-Wno-error=deprecated-declarations', # Bug #3795
156 '-Wno-error=maybe-uninitialized', # Bug #1615
157 '-Wno-unused-parameter',
158 ]
Davide Pesavento7eaed5c2019-01-09 20:03:43 -0500159 flags['LINKFLAGS'] += ['-Wl,-O1']
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400160 return flags
161
162 def getOptimizedFlags(self, conf):
163 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
164 flags['CXXFLAGS'] += ['-O2',
165 '-g',
166 '-pedantic',
167 '-Wall',
168 '-Wextra',
Davide Pesaventoab6ac5d2021-10-05 23:23:53 -0400169 '-Wcatch-value=2',
170 '-Wextra-semi',
Davide Pesavento64cafaa2018-05-24 00:50:41 -0400171 '-Wnon-virtual-dtor',
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400172 '-Wno-unused-parameter',
173 ]
Davide Pesavento7eaed5c2019-01-09 20:03:43 -0500174 flags['LINKFLAGS'] += ['-Wl,-O1']
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400175 return flags
176
177class GccFlags(GccBasicFlags):
178 def getDebugFlags(self, conf):
179 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesaventoab6ac5d2021-10-05 23:23:53 -0400180 flags['CXXFLAGS'] += ['-fdiagnostics-color',
181 '-Wredundant-tags',
182 ]
Davide Pesavento92669062022-03-10 19:09:44 -0500183 if platform.machine() == 'armv7l':
Davide Pesaventoab6ac5d2021-10-05 23:23:53 -0400184 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400185 return flags
186
187 def getOptimizedFlags(self, conf):
188 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesaventoab6ac5d2021-10-05 23:23:53 -0400189 flags['CXXFLAGS'] += ['-fdiagnostics-color',
190 '-Wredundant-tags',
191 ]
Davide Pesavento92669062022-03-10 19:09:44 -0500192 if platform.machine() == 'armv7l':
Davide Pesaventoab6ac5d2021-10-05 23:23:53 -0400193 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400194 return flags
195
196class ClangFlags(GccBasicFlags):
Davide Pesavento64cafaa2018-05-24 00:50:41 -0400197 def getGeneralFlags(self, conf):
198 flags = super(ClangFlags, self).getGeneralFlags(conf)
Davide Pesaventoab6ac5d2021-10-05 23:23:53 -0400199 if Utils.unversioned_sys_platform() == 'darwin':
Davide Pesavento64cafaa2018-05-24 00:50:41 -0400200 # Bug #4296
Davide Pesaventof5006be2022-08-19 17:36:36 -0400201 brewdir = '/opt/homebrew' if platform.machine() == 'arm64' else '/usr/local'
202 flags['CXXFLAGS'] += [['-isystem', f'{brewdir}/include'], # for Homebrew
Davide Pesavento64cafaa2018-05-24 00:50:41 -0400203 ['-isystem', '/opt/local/include']] # for MacPorts
Davide Pesaventoab6ac5d2021-10-05 23:23:53 -0400204 elif Utils.unversioned_sys_platform() == 'freebsd':
205 # Bug #4790
206 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Davide Pesavento64cafaa2018-05-24 00:50:41 -0400207 return flags
208
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400209 def getDebugFlags(self, conf):
210 flags = super(ClangFlags, self).getDebugFlags(conf)
Davide Pesavento7eaed5c2019-01-09 20:03:43 -0500211 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesavento7eaed5c2019-01-09 20:03:43 -0500212 '-Wundefined-func-template',
213 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
214 ]
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400215 return flags
216
217 def getOptimizedFlags(self, conf):
218 flags = super(ClangFlags, self).getOptimizedFlags(conf)
Davide Pesavento7eaed5c2019-01-09 20:03:43 -0500219 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesavento7eaed5c2019-01-09 20:03:43 -0500220 '-Wundefined-func-template',
221 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
222 ]
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400223 return flags