blob: 2be9d90419018273839ba835a56f7cc591f0bb6f [file] [log] [blame]
Junxiao Shif7191242015-03-19 05:53:41 -07001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
2
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -05003from waflib import Configure, Logs, Utils
Junxiao Shif7191242015-03-19 05:53:41 -07004
5def options(opt):
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -05006 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
7 help='Compile in debugging mode with minimal optimizations (-O0 or -Og)')
Junxiao Shif7191242015-03-19 05:53:41 -07008
9def configure(conf):
Davide Pesaventoc0702702017-08-24 22:04:00 -040010 conf.start_msg('Checking C++ compiler version')
11
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -050012 cxx = conf.env.CXX_NAME # generic name of the compiler
13 ccver = tuple(int(i) for i in conf.env.CC_VERSION)
14 ccverstr = '.'.join(conf.env.CC_VERSION)
Davide Pesaventoc0702702017-08-24 22:04:00 -040015 errmsg = ''
16 warnmsg = ''
Junxiao Shif7191242015-03-19 05:53:41 -070017 if cxx == 'gcc':
Davide Pesavento26ea1ac2018-05-10 20:20:03 -040018 if ccver < (5, 3, 0):
Davide Pesaventoc0702702017-08-24 22:04:00 -040019 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesavento26ea1ac2018-05-10 20:20:03 -040020 'The minimum supported gcc version is 5.3.0.')
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040021 conf.flags = GccFlags()
Junxiao Shif7191242015-03-19 05:53:41 -070022 elif cxx == 'clang':
Davide Pesavento302941e2018-07-21 18:55:56 -040023 if ccver < (3, 6, 0):
Davide Pesaventoc0702702017-08-24 22:04:00 -040024 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesavento302941e2018-07-21 18:55:56 -040025 'The minimum supported clang version is 3.6.0.')
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040026 conf.flags = ClangFlags()
Junxiao Shif7191242015-03-19 05:53:41 -070027 else:
Davide Pesaventoc0702702017-08-24 22:04:00 -040028 warnmsg = 'Note: %s compiler is unsupported' % cxx
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040029 conf.flags = CompilerFlags()
Davide Pesaventoc0702702017-08-24 22:04:00 -040030
31 if errmsg:
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -050032 conf.end_msg(ccverstr, color='RED')
Davide Pesaventoc0702702017-08-24 22:04:00 -040033 conf.fatal(errmsg)
34 elif warnmsg:
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -050035 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesaventoc0702702017-08-24 22:04:00 -040036 Logs.warn(warnmsg)
37 else:
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -050038 conf.end_msg(ccverstr)
Junxiao Shif7191242015-03-19 05:53:41 -070039
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040040 conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
Junxiao Shif7191242015-03-19 05:53:41 -070041
Davide Pesavento60f8cc12018-05-10 22:05:21 -040042 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040043 generalFlags = conf.flags.getGeneralFlags(conf)
Junxiao Shif7191242015-03-19 05:53:41 -070044 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
45 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
46 conf.env.DEFINES += generalFlags['DEFINES']
47
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040048@Configure.conf
49def check_compiler_flags(conf):
Junxiao Shic8a0a252015-10-05 07:25:02 -070050 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Junxiao Shif7191242015-03-19 05:53:41 -070051 # corresponding environment variables are not set.
Junxiao Shic8a0a252015-10-05 07:25:02 -070052 # DEFINES are always applied.
Junxiao Shif7191242015-03-19 05:53:41 -070053 if conf.options.debug:
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040054 extraFlags = conf.flags.getDebugFlags(conf)
55 if conf.areCustomCxxflagsPresent:
Junxiao Shif7191242015-03-19 05:53:41 -070056 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -050057 if missingFlags:
58 Logs.warn('Selected debug mode, but CXXFLAGS is set to a custom value "%s"'
59 % ' '.join(conf.env.CXXFLAGS))
60 Logs.warn('Default flags "%s" will not be used' % ' '.join(missingFlags))
Junxiao Shif7191242015-03-19 05:53:41 -070061 else:
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040062 extraFlags = conf.flags.getOptimizedFlags(conf)
Junxiao Shif7191242015-03-19 05:53:41 -070063
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040064 if not conf.areCustomCxxflagsPresent:
Junxiao Shif7191242015-03-19 05:53:41 -070065 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Davide Pesavento3e79c9c2015-11-01 20:38:28 +010066 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Junxiao Shif7191242015-03-19 05:53:41 -070067
68 conf.env.DEFINES += extraFlags['DEFINES']
69
70@Configure.conf
71def add_supported_cxxflags(self, cxxflags):
72 """
73 Check which cxxflags are supported by compiler and add them to env.CXXFLAGS variable
74 """
75 if len(cxxflags) == 0:
76 return
77
78 self.start_msg('Checking supported CXXFLAGS')
79
80 supportedFlags = []
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -040081 for flags in cxxflags:
82 flags = Utils.to_list(flags)
83 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
84 supportedFlags += flags
Junxiao Shif7191242015-03-19 05:53:41 -070085
86 self.end_msg(' '.join(supportedFlags))
Davide Pesavento89d91752016-08-14 11:34:09 +020087 self.env.prepend_value('CXXFLAGS', supportedFlags)
Junxiao Shif7191242015-03-19 05:53:41 -070088
89@Configure.conf
90def add_supported_linkflags(self, linkflags):
91 """
92 Check which linkflags are supported by compiler and add them to env.LINKFLAGS variable
93 """
94 if len(linkflags) == 0:
95 return
96
97 self.start_msg('Checking supported LINKFLAGS')
98
99 supportedFlags = []
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -0400100 for flags in linkflags:
101 flags = Utils.to_list(flags)
102 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
103 supportedFlags += flags
Junxiao Shif7191242015-03-19 05:53:41 -0700104
105 self.end_msg(' '.join(supportedFlags))
Davide Pesavento89d91752016-08-14 11:34:09 +0200106 self.env.prepend_value('LINKFLAGS', supportedFlags)
Junxiao Shif7191242015-03-19 05:53:41 -0700107
108
109class CompilerFlags(object):
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -0500110 def getCompilerVersion(self, conf):
111 return tuple(int(i) for i in conf.env.CC_VERSION)
112
Junxiao Shif7191242015-03-19 05:53:41 -0700113 def getGeneralFlags(self, conf):
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100114 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Junxiao Shif7191242015-03-19 05:53:41 -0700115 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
116
117 def getDebugFlags(self, conf):
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100118 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Junxiao Shif7191242015-03-19 05:53:41 -0700119 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
120
121 def getOptimizedFlags(self, conf):
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100122 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Junxiao Shif7191242015-03-19 05:53:41 -0700123 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
124
125class GccBasicFlags(CompilerFlags):
Junxiao Shic8a0a252015-10-05 07:25:02 -0700126 """
127 This class defines basic flags that work for both gcc and clang compilers
128 """
Davide Pesaventodd0e1952017-06-24 13:37:13 -0400129 def getGeneralFlags(self, conf):
130 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
Davide Pesavento60f8cc12018-05-10 22:05:21 -0400131 flags['CXXFLAGS'] += ['-std=c++14']
Davide Pesaventodd0e1952017-06-24 13:37:13 -0400132 return flags
133
Junxiao Shif7191242015-03-19 05:53:41 -0700134 def getDebugFlags(self, conf):
135 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100136 flags['CXXFLAGS'] += ['-O0',
Davide Pesaventodd0e1952017-06-24 13:37:13 -0400137 '-Og', # gcc >= 4.8, clang >= 4.0
Junxiao Shif7191242015-03-19 05:53:41 -0700138 '-g3',
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100139 '-pedantic',
140 '-Wall',
141 '-Wextra',
Junxiao Shif7191242015-03-19 05:53:41 -0700142 '-Werror',
Davide Pesavento887336c2017-09-19 15:25:23 -0400143 '-Wnon-virtual-dtor',
Alexander Afanasyevfc4aa0d2016-10-15 00:57:33 +0000144 '-Wno-error=deprecated-declarations', # Bug #3795
Davide Pesaventoc0702702017-08-24 22:04:00 -0400145 '-Wno-error=maybe-uninitialized', # Bug #1615
146 '-Wno-unused-parameter',
Junxiao Shic8a0a252015-10-05 07:25:02 -0700147 ]
Davide Pesaventodd0e1952017-06-24 13:37:13 -0400148 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Junxiao Shif7191242015-03-19 05:53:41 -0700149 return flags
150
151 def getOptimizedFlags(self, conf):
152 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100153 flags['CXXFLAGS'] += ['-O2',
Junxiao Shic8a0a252015-10-05 07:25:02 -0700154 '-g',
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100155 '-pedantic',
156 '-Wall',
157 '-Wextra',
Davide Pesavento887336c2017-09-19 15:25:23 -0400158 '-Wnon-virtual-dtor',
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100159 '-Wno-unused-parameter',
Junxiao Shic8a0a252015-10-05 07:25:02 -0700160 ]
Davide Pesaventodd0e1952017-06-24 13:37:13 -0400161 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Junxiao Shif7191242015-03-19 05:53:41 -0700162 return flags
163
164class GccFlags(GccBasicFlags):
Junxiao Shif7191242015-03-19 05:53:41 -0700165 def getDebugFlags(self, conf):
166 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesavento26ea1ac2018-05-10 20:20:03 -0400167 flags['CXXFLAGS'] += ['-fdiagnostics-color']
Junxiao Shic8a0a252015-10-05 07:25:02 -0700168 return flags
169
170 def getOptimizedFlags(self, conf):
171 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesavento26ea1ac2018-05-10 20:20:03 -0400172 flags['CXXFLAGS'] += ['-fdiagnostics-color']
Junxiao Shif7191242015-03-19 05:53:41 -0700173 return flags
174
175class ClangFlags(GccBasicFlags):
176 def getGeneralFlags(self, conf):
177 flags = super(ClangFlags, self).getGeneralFlags(conf)
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -0500178 if Utils.unversioned_sys_platform() == 'darwin' and self.getCompilerVersion(conf) >= (9, 0, 0):
179 # Bug #4296
Alexander Afanasyev11e74eb2017-09-21 19:01:54 -0400180 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include'], # for Homebrew
181 ['-isystem', '/opt/local/include']] # for MacPorts
Junxiao Shif7191242015-03-19 05:53:41 -0700182 return flags
183
184 def getDebugFlags(self, conf):
185 flags = super(ClangFlags, self).getDebugFlags(conf)
Davide Pesaventoc0702702017-08-24 22:04:00 -0400186 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
187 '-Wextra-semi',
188 '-Wundefined-func-template',
189 '-Wno-error=deprecated-register',
190 '-Wno-error=infinite-recursion', # Bug #3358
191 '-Wno-error=keyword-macro', # Bug #3235
192 '-Wno-error=unneeded-internal-declaration', # Bug #1588
193 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
194 ]
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -0500195 version = self.getCompilerVersion(conf)
Davide Pesaventoc0702702017-08-24 22:04:00 -0400196 if version < (3, 9, 0) or (Utils.unversioned_sys_platform() == 'darwin' and version < (8, 1, 0)):
197 flags['CXXFLAGS'] += ['-Wno-unknown-pragmas']
Junxiao Shic8a0a252015-10-05 07:25:02 -0700198 return flags
199
200 def getOptimizedFlags(self, conf):
201 flags = super(ClangFlags, self).getOptimizedFlags(conf)
202 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesaventoc0702702017-08-24 22:04:00 -0400203 '-Wextra-semi',
204 '-Wundefined-func-template',
Junxiao Shic8a0a252015-10-05 07:25:02 -0700205 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
206 ]
Alexander Afanasyev20c85cb2018-03-09 17:50:14 -0500207 version = self.getCompilerVersion(conf)
Davide Pesaventoc0702702017-08-24 22:04:00 -0400208 if version < (3, 9, 0) or (Utils.unversioned_sys_platform() == 'darwin' and version < (8, 1, 0)):
209 flags['CXXFLAGS'] += ['-Wno-unknown-pragmas']
Junxiao Shif7191242015-03-19 05:53:41 -0700210 return flags