blob: e9fdcc61b460699fd78a20e503e642122f677e15 [file] [log] [blame]
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -07001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -07002
Alexander Afanasyev5560fd42018-03-07 17:03:03 -05003from waflib import Configure, Logs, Utils
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -07004
5def options(opt):
Alexander Afanasyev5560fd42018-03-07 17:03:03 -05006 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
7 help='Compile in debugging mode with minimal optimizations (-O0 or -Og)')
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -07008
9def configure(conf):
Davide Pesavento88a0d812017-08-19 21:31:42 -040010 conf.start_msg('Checking C++ compiler version')
11
Alexander Afanasyev5560fd42018-03-07 17:03:03 -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 Pesavento88a0d812017-08-19 21:31:42 -040015 errmsg = ''
16 warnmsg = ''
Alexander Afanasyev01515792014-12-16 14:20:01 -080017 if cxx == 'gcc':
Davide Pesavento844b0932018-05-07 01:00:16 -040018 if ccver < (5, 3, 0):
Davide Pesavento88a0d812017-08-19 21:31:42 -040019 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesavento844b0932018-05-07 01:00:16 -040020 'The minimum supported gcc version is 5.3.0.')
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040021 conf.flags = GccFlags()
Alexander Afanasyev01515792014-12-16 14:20:01 -080022 elif cxx == 'clang':
Davide Pesavento844b0932018-05-07 01:00:16 -040023 if ccver < (3, 5, 0):
Davide Pesavento88a0d812017-08-19 21:31:42 -040024 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesavento844b0932018-05-07 01:00:16 -040025 'The minimum supported clang version is 3.5.0.')
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040026 conf.flags = ClangFlags()
Alexander Afanasyev01515792014-12-16 14:20:01 -080027 else:
Davide Pesavento88a0d812017-08-19 21:31:42 -040028 warnmsg = 'Note: %s compiler is unsupported' % cxx
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040029 conf.flags = CompilerFlags()
Davide Pesavento88a0d812017-08-19 21:31:42 -040030
31 if errmsg:
Alexander Afanasyev5560fd42018-03-07 17:03:03 -050032 conf.end_msg(ccverstr, color='RED')
Davide Pesavento88a0d812017-08-19 21:31:42 -040033 conf.fatal(errmsg)
34 elif warnmsg:
Alexander Afanasyev5560fd42018-03-07 17:03:03 -050035 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento88a0d812017-08-19 21:31:42 -040036 Logs.warn(warnmsg)
37 else:
Alexander Afanasyev5560fd42018-03-07 17:03:03 -050038 conf.end_msg(ccverstr)
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -070039
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040040 conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
Alexander Afanasyev01515792014-12-16 14:20:01 -080041
Davide Pesaventod06ea052015-10-02 01:48:24 +020042 # General flags are always applied (e.g., selecting C++11 mode)
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040043 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyev01515792014-12-16 14:20:01 -080044 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
45 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
46 conf.env.DEFINES += generalFlags['DEFINES']
47
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040048@Configure.conf
49def check_compiler_flags(conf):
Davide Pesaventod06ea052015-10-02 01:48:24 +020050 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Alexander Afanasyev01515792014-12-16 14:20:01 -080051 # corresponding environment variables are not set.
Davide Pesaventod06ea052015-10-02 01:48:24 +020052 # DEFINES are always applied.
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070053 if conf.options.debug:
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040054 extraFlags = conf.flags.getDebugFlags(conf)
55 if conf.areCustomCxxflagsPresent:
Alexander Afanasyev01515792014-12-16 14:20:01 -080056 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev5560fd42018-03-07 17:03:03 -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))
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070061 else:
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040062 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070063
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040064 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyev01515792014-12-16 14:20:01 -080065 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Davide Pesavento7c02b472015-10-28 20:50:17 +010066 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Alexander Afanasyev01515792014-12-16 14:20:01 -080067
68 conf.env.DEFINES += extraFlags['DEFINES']
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020069
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070070@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 """
Alexander Afanasyev01515792014-12-16 14:20:01 -080075 if len(cxxflags) == 0:
76 return
77
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020078 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070079
80 supportedFlags = []
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -040081 for flags in cxxflags:
82 flags = Utils.to_list(flags)
83 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
84 supportedFlags += flags
Alexander Afanasyev5e1288e2014-03-28 11:11:48 -070085
Alexander Afanasyevb78bc4d2014-04-09 21:20:52 -070086 self.end_msg(' '.join(supportedFlags))
Davide Pesavento1349d2d2016-08-09 06:43:57 +020087 self.env.prepend_value('CXXFLAGS', supportedFlags)
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020088
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 """
Alexander Afanasyev01515792014-12-16 14:20:01 -080094 if len(linkflags) == 0:
95 return
96
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +020097 self.start_msg('Checking supported LINKFLAGS')
98
99 supportedFlags = []
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -0400100 for flags in linkflags:
101 flags = Utils.to_list(flags)
102 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
103 supportedFlags += flags
Davide Pesaventodfe9c6b2014-08-25 21:17:10 +0200104
105 self.end_msg(' '.join(supportedFlags))
Davide Pesavento1349d2d2016-08-09 06:43:57 +0200106 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyev01515792014-12-16 14:20:01 -0800107
108
109class CompilerFlags(object):
Alexander Afanasyev5560fd42018-03-07 17:03:03 -0500110 def getCompilerVersion(self, conf):
111 return tuple(int(i) for i in conf.env.CC_VERSION)
112
Alexander Afanasyev01515792014-12-16 14:20:01 -0800113 def getGeneralFlags(self, conf):
Davide Pesavento7c02b472015-10-28 20:50:17 +0100114 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Alexander Afanasyev01515792014-12-16 14:20:01 -0800115 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
116
117 def getDebugFlags(self, conf):
Davide Pesavento7c02b472015-10-28 20:50:17 +0100118 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Alexander Afanasyev01515792014-12-16 14:20:01 -0800119 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
120
121 def getOptimizedFlags(self, conf):
Davide Pesavento7c02b472015-10-28 20:50:17 +0100122 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Alexander Afanasyev01515792014-12-16 14:20:01 -0800123 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
124
125class GccBasicFlags(CompilerFlags):
Davide Pesaventod06ea052015-10-02 01:48:24 +0200126 """
127 This class defines basic flags that work for both gcc and clang compilers
128 """
Davide Pesavento724d65a2017-06-23 17:14:08 -0400129 def getGeneralFlags(self, conf):
130 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
131 flags['CXXFLAGS'] += ['-std=c++11']
132 return flags
133
Alexander Afanasyev01515792014-12-16 14:20:01 -0800134 def getDebugFlags(self, conf):
135 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Davide Pesavento7c02b472015-10-28 20:50:17 +0100136 flags['CXXFLAGS'] += ['-O0',
Davide Pesaventof714c1f2017-04-12 23:53:40 -0400137 '-Og', # gcc >= 4.8, clang >= 4.0
Alexander Afanasyev01515792014-12-16 14:20:01 -0800138 '-g3',
Davide Pesavento7c02b472015-10-28 20:50:17 +0100139 '-pedantic',
140 '-Wall',
141 '-Wextra',
Alexander Afanasyev01515792014-12-16 14:20:01 -0800142 '-Werror',
Davide Pesavento7f20d6e2017-01-16 14:43:58 -0500143 '-Wnon-virtual-dtor',
Alexander Afanasyev465c9732016-10-11 16:35:32 -0700144 '-Wno-error=deprecated-declarations', # Bug #3795
Davide Pesavento88a0d812017-08-19 21:31:42 -0400145 '-Wno-error=maybe-uninitialized', # Bug #1615
146 '-Wno-unused-parameter',
Davide Pesavento1aafba42015-09-17 17:22:31 -0700147 ]
Davide Pesavento37271c52017-06-23 00:58:50 -0400148 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Alexander Afanasyev01515792014-12-16 14:20:01 -0800149 return flags
150
151 def getOptimizedFlags(self, conf):
152 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
Davide Pesavento7c02b472015-10-28 20:50:17 +0100153 flags['CXXFLAGS'] += ['-O2',
Davide Pesavento1aafba42015-09-17 17:22:31 -0700154 '-g',
Davide Pesavento7c02b472015-10-28 20:50:17 +0100155 '-pedantic',
156 '-Wall',
157 '-Wextra',
Davide Pesavento7f20d6e2017-01-16 14:43:58 -0500158 '-Wnon-virtual-dtor',
Davide Pesavento7c02b472015-10-28 20:50:17 +0100159 '-Wno-unused-parameter',
Davide Pesavento1aafba42015-09-17 17:22:31 -0700160 ]
Davide Pesavento37271c52017-06-23 00:58:50 -0400161 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Alexander Afanasyev01515792014-12-16 14:20:01 -0800162 return flags
163
164class GccFlags(GccBasicFlags):
Alexander Afanasyev01515792014-12-16 14:20:01 -0800165 def getDebugFlags(self, conf):
166 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesavento844b0932018-05-07 01:00:16 -0400167 flags['CXXFLAGS'] += ['-fdiagnostics-color']
Alexander Afanasyev01515792014-12-16 14:20:01 -0800168 return flags
169
Davide Pesaventod06ea052015-10-02 01:48:24 +0200170 def getOptimizedFlags(self, conf):
171 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesavento844b0932018-05-07 01:00:16 -0400172 flags['CXXFLAGS'] += ['-fdiagnostics-color']
Davide Pesaventod06ea052015-10-02 01:48:24 +0200173 return flags
174
Alexander Afanasyev01515792014-12-16 14:20:01 -0800175class ClangFlags(GccBasicFlags):
176 def getGeneralFlags(self, conf):
177 flags = super(ClangFlags, self).getGeneralFlags(conf)
Alexander Afanasyev5560fd42018-03-07 17:03:03 -0500178 if Utils.unversioned_sys_platform() == 'darwin' and self.getCompilerVersion(conf) >= (9, 0, 0):
179 # Bug #4296
Alexander Afanasyevb82d8c32017-09-21 11:35:07 -0400180 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include'], # for Homebrew
181 ['-isystem', '/opt/local/include']] # for MacPorts
Alexander Afanasyev01515792014-12-16 14:20:01 -0800182 return flags
183
184 def getDebugFlags(self, conf):
185 flags = super(ClangFlags, self).getDebugFlags(conf)
Davide Pesavento1aafba42015-09-17 17:22:31 -0700186 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesavento88a0d812017-08-19 21:31:42 -0400187 '-Wextra-semi',
188 '-Wundefined-func-template',
Davide Pesavento1aafba42015-09-17 17:22:31 -0700189 '-Wno-error=deprecated-register',
Alexander Afanasyevefa22d62015-12-04 13:39:17 -0800190 '-Wno-error=infinite-recursion', # Bug #3358
Davide Pesavento88a0d812017-08-19 21:31:42 -0400191 '-Wno-error=keyword-macro', # Bug #3235
192 '-Wno-error=unneeded-internal-declaration', # Bug #1588
193 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Davide Pesaventod06ea052015-10-02 01:48:24 +0200194 ]
Alexander Afanasyev5560fd42018-03-07 17:03:03 -0500195 version = self.getCompilerVersion(conf)
Davide Pesavento88a0d812017-08-19 21:31:42 -0400196 if version < (3, 9, 0) or (Utils.unversioned_sys_platform() == 'darwin' and version < (8, 1, 0)):
197 flags['CXXFLAGS'] += ['-Wno-unknown-pragmas']
Davide Pesaventod06ea052015-10-02 01:48:24 +0200198 return flags
199
200 def getOptimizedFlags(self, conf):
201 flags = super(ClangFlags, self).getOptimizedFlags(conf)
202 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Davide Pesavento88a0d812017-08-19 21:31:42 -0400203 '-Wextra-semi',
204 '-Wundefined-func-template',
Davide Pesaventod06ea052015-10-02 01:48:24 +0200205 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Davide Pesavento1aafba42015-09-17 17:22:31 -0700206 ]
Alexander Afanasyev5560fd42018-03-07 17:03:03 -0500207 version = self.getCompilerVersion(conf)
Davide Pesavento88a0d812017-08-19 21:31:42 -0400208 if version < (3, 9, 0) or (Utils.unversioned_sys_platform() == 'darwin' and version < (8, 1, 0)):
209 flags['CXXFLAGS'] += ['-Wno-unknown-pragmas']
Alexander Afanasyev01515792014-12-16 14:20:01 -0800210 return flags