blob: f086c17a890f994504117bf1e8ba8a1d8622506f [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 Pesaventoe4b74bd2022-03-09 18:56:37 -050021 'The minimum supported gcc version is 7.4.')
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000022 conf.flags = GccFlags()
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050023 elif cxx == 'clang':
Davide Pesaventoe4b74bd2022-03-09 18:56:37 -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 Pesaventob545aac2017-09-22 23:54:10 -040032 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesaventoe4b74bd2022-03-09 18:56:37 -050033 'The minimum supported clang version is 6.0.')
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000034 conf.flags = ClangFlags()
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050035 else:
Davide Pesavento333e3eb2021-10-12 19:30:51 -040036 warnmsg = '%s compiler is unsupported' % cxx
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000037 conf.flags = CompilerFlags()
Davide Pesaventob545aac2017-09-22 23:54:10 -040038
39 if errmsg:
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040040 conf.end_msg(ccverstr, color='RED')
Davide Pesaventob545aac2017-09-22 23:54:10 -040041 conf.fatal(errmsg)
42 elif warnmsg:
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040043 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento333e3eb2021-10-12 19:30:51 -040044 Logs.warn('WARNING: ' + warnmsg)
Davide Pesaventob545aac2017-09-22 23:54:10 -040045 else:
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040046 conf.end_msg(ccverstr)
Shuo Chen478204c2014-03-18 18:27:04 -070047
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000048 conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050049
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040050 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000051 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050052 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
53 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
54 conf.env.DEFINES += generalFlags['DEFINES']
55
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000056@Configure.conf
57def check_compiler_flags(conf):
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050058 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
59 # corresponding environment variables are not set.
60 # DEFINES are always applied.
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070061 if conf.options.debug:
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000062 extraFlags = conf.flags.getDebugFlags(conf)
63 if conf.areCustomCxxflagsPresent:
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050064 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Davide Pesavento98ff2bc2018-05-24 00:10:31 -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 Afanasyev3fd14f02014-03-26 14:34:39 -070069 else:
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000070 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070071
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000072 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050073 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
74 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
75
76 conf.env.DEFINES += extraFlags['DEFINES']
Wentao Shanga8f3c402014-10-30 14:03:27 -070077
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -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 Afanasyevd352cce2015-11-20 14:15:11 -050083 if len(cxxflags) == 0:
84 return
85
Wentao Shanga8f3c402014-10-30 14:03:27 -070086 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070087
88 supportedFlags = []
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000089 for flags in cxxflags:
90 flags = Utils.to_list(flags)
91 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
92 supportedFlags += flags
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070093
Shuo Chen478204c2014-03-18 18:27:04 -070094 self.end_msg(' '.join(supportedFlags))
Junxiao Shie1801312017-07-22 19:16:49 +000095 self.env.prepend_value('CXXFLAGS', supportedFlags)
Wentao Shanga8f3c402014-10-30 14:03:27 -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 """
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500102 if len(linkflags) == 0:
103 return
104
Wentao Shanga8f3c402014-10-30 14:03:27 -0700105 self.start_msg('Checking supported LINKFLAGS')
106
107 supportedFlags = []
Alexander Afanasyevbb058c02018-02-15 22:49:24 +0000108 for flags in linkflags:
109 flags = Utils.to_list(flags)
110 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
111 supportedFlags += flags
Wentao Shanga8f3c402014-10-30 14:03:27 -0700112
113 self.end_msg(' '.join(supportedFlags))
Junxiao Shie1801312017-07-22 19:16:49 +0000114 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500115
116
117class CompilerFlags(object):
Davide Pesavento98ff2bc2018-05-24 00:10:31 -0400118 def getCompilerVersion(self, conf):
119 return tuple(int(i) for i in conf.env.CC_VERSION)
120
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500121 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 """
Junxiao Shie1801312017-07-22 19:16:49 +0000137 def getGeneralFlags(self, conf):
138 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
Davide Pesavento98ff2bc2018-05-24 00:10:31 -0400139 flags['CXXFLAGS'] += ['-std=c++14']
Davide Pesaventoed2f4762019-01-25 00:40:46 -0500140 if Utils.unversioned_sys_platform() == 'linux':
141 flags['LINKFLAGS'] += ['-fuse-ld=gold']
142 elif Utils.unversioned_sys_platform() == 'freebsd':
143 flags['LINKFLAGS'] += ['-fuse-ld=lld']
Junxiao Shie1801312017-07-22 19:16:49 +0000144 return flags
145
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500146 def getDebugFlags(self, conf):
147 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400148 flags['CXXFLAGS'] += ['-Og',
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500149 '-g3',
150 '-pedantic',
151 '-Wall',
152 '-Wextra',
153 '-Werror',
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400154 '-Wcatch-value=2',
155 '-Wextra-semi',
Junxiao Shie1801312017-07-22 19:16:49 +0000156 '-Wnon-virtual-dtor',
Junxiao Shie1801312017-07-22 19:16:49 +0000157 '-Wno-error=deprecated-declarations', # Bug #3795
Davide Pesaventob545aac2017-09-22 23:54:10 -0400158 '-Wno-error=maybe-uninitialized', # Bug #1615
159 '-Wno-unused-parameter',
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500160 ]
Davide Pesaventoed2f4762019-01-25 00:40:46 -0500161 flags['LINKFLAGS'] += ['-Wl,-O1']
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500162 return flags
163
164 def getOptimizedFlags(self, conf):
165 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
166 flags['CXXFLAGS'] += ['-O2',
167 '-g',
168 '-pedantic',
169 '-Wall',
170 '-Wextra',
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400171 '-Wcatch-value=2',
172 '-Wextra-semi',
Junxiao Shie1801312017-07-22 19:16:49 +0000173 '-Wnon-virtual-dtor',
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500174 '-Wno-unused-parameter',
175 ]
Davide Pesaventoed2f4762019-01-25 00:40:46 -0500176 flags['LINKFLAGS'] += ['-Wl,-O1']
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500177 return flags
178
179class GccFlags(GccBasicFlags):
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500180 def getDebugFlags(self, conf):
181 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400182 flags['CXXFLAGS'] += ['-fdiagnostics-color',
183 '-Wredundant-tags',
184 ]
Davide Pesaventoe4b74bd2022-03-09 18:56:37 -0500185 if platform.machine() == 'armv7l':
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400186 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500187 return flags
188
189 def getOptimizedFlags(self, conf):
190 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400191 flags['CXXFLAGS'] += ['-fdiagnostics-color',
192 '-Wredundant-tags',
193 ]
Davide Pesaventoe4b74bd2022-03-09 18:56:37 -0500194 if platform.machine() == 'armv7l':
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400195 flags['CXXFLAGS'] += ['-Wno-psabi'] # Bug #5106
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500196 return flags
197
198class ClangFlags(GccBasicFlags):
199 def getGeneralFlags(self, conf):
200 flags = super(ClangFlags, self).getGeneralFlags(conf)
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400201 if Utils.unversioned_sys_platform() == 'darwin':
Davide Pesavento98ff2bc2018-05-24 00:10:31 -0400202 # Bug #4296
Alexander Afanasyevbb058c02018-02-15 22:49:24 +0000203 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include'], # for Homebrew
204 ['-isystem', '/opt/local/include']] # for MacPorts
Davide Pesavento333e3eb2021-10-12 19:30:51 -0400205 elif Utils.unversioned_sys_platform() == 'freebsd':
206 # Bug #4790
207 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']]
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500208 return flags
209
210 def getDebugFlags(self, conf):
211 flags = super(ClangFlags, self).getDebugFlags(conf)
212 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Alexander Afanasyevbb058c02018-02-15 22:49:24 +0000213 '-Wundefined-func-template',
Davide Pesaventob545aac2017-09-22 23:54:10 -0400214 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500215 ]
216 return flags
217
218 def getOptimizedFlags(self, conf):
219 flags = super(ClangFlags, self).getOptimizedFlags(conf)
220 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Alexander Afanasyevbb058c02018-02-15 22:49:24 +0000221 '-Wundefined-func-template',
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500222 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
223 ]
224 return flags