blob: e6902902933a557a1ab26f89ee3473513c87f3eb [file] [log] [blame]
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -07001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -07002
Alexander Afanasyev08d18742018-03-15 16:31:28 -04003from waflib import Configure, Logs, Utils
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -07004
5def options(opt):
Alexander Afanasyev08d18742018-03-15 16:31:28 -04006 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
7 help='Compile in debugging mode with minimal optimizations (-O0 or -Og)')
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -07008
9def configure(conf):
Alexander Afanasyev08d18742018-03-15 16:31:28 -040010 conf.start_msg('Checking C++ compiler version')
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070011
Alexander Afanasyev08d18742018-03-15 16:31:28 -040012 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)
15 errmsg = ''
16 warnmsg = ''
17 if cxx == 'gcc':
Davide Pesavento90034832018-05-30 10:10:31 -040018 if ccver < (5, 3, 0):
Alexander Afanasyev08d18742018-03-15 16:31:28 -040019 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesavento90034832018-05-30 10:10:31 -040020 'The minimum supported gcc version is 5.3.0.')
Alexander Afanasyev08d18742018-03-15 16:31:28 -040021 conf.flags = GccFlags()
22 elif cxx == 'clang':
Davide Pesavento90034832018-05-30 10:10:31 -040023 if ccver < (3, 5, 0):
Alexander Afanasyev08d18742018-03-15 16:31:28 -040024 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesavento90034832018-05-30 10:10:31 -040025 'The minimum supported clang version is 3.5.0.')
Alexander Afanasyev08d18742018-03-15 16:31:28 -040026 conf.flags = ClangFlags()
27 else:
28 warnmsg = 'Note: %s compiler is unsupported' % cxx
29 conf.flags = CompilerFlags()
30
31 if errmsg:
32 conf.end_msg(ccverstr, color='RED')
33 conf.fatal(errmsg)
34 elif warnmsg:
35 conf.end_msg(ccverstr, color='YELLOW')
36 Logs.warn(warnmsg)
37 else:
38 conf.end_msg(ccverstr)
39
40 conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080041
Davide Pesavento90034832018-05-30 10:10:31 -040042 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyev08d18742018-03-15 16:31:28 -040043 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080044 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
45 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
46 conf.env.DEFINES += generalFlags['DEFINES']
47
Alexander Afanasyev08d18742018-03-15 16:31:28 -040048@Configure.conf
49def check_compiler_flags(conf):
Alexander Afanasyevc7c99002015-10-09 17:27:30 -070050 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080051 # corresponding environment variables are not set.
Alexander Afanasyevc7c99002015-10-09 17:27:30 -070052 # DEFINES are always applied.
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070053 if conf.options.debug:
Alexander Afanasyev08d18742018-03-15 16:31:28 -040054 extraFlags = conf.flags.getDebugFlags(conf)
55 if conf.areCustomCxxflagsPresent:
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080056 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev08d18742018-03-15 16:31:28 -040057 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 Afanasyev4ffcff22014-09-02 15:39:20 -070061 else:
Alexander Afanasyev08d18742018-03-15 16:31:28 -040062 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070063
Alexander Afanasyev08d18742018-03-15 16:31:28 -040064 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080065 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -080066 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080067
68 conf.env.DEFINES += extraFlags['DEFINES']
Shock Jiang2d8483c2014-10-30 10:15:37 -070069
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -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 Afanasyev83b7c2f2015-01-23 13:08:34 -080075 if len(cxxflags) == 0:
76 return
77
Shock Jiang2d8483c2014-10-30 10:15:37 -070078 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070079
80 supportedFlags = []
Alexander Afanasyev08d18742018-03-15 16:31:28 -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 Afanasyev4ffcff22014-09-02 15:39:20 -070085
86 self.end_msg(' '.join(supportedFlags))
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -080087 self.env.prepend_value('CXXFLAGS', supportedFlags)
Shock Jiang2d8483c2014-10-30 10:15:37 -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 """
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080094 if len(linkflags) == 0:
95 return
96
Shock Jiang2d8483c2014-10-30 10:15:37 -070097 self.start_msg('Checking supported LINKFLAGS')
98
99 supportedFlags = []
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400100 for flags in linkflags:
101 flags = Utils.to_list(flags)
102 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
103 supportedFlags += flags
Shock Jiang2d8483c2014-10-30 10:15:37 -0700104
105 self.end_msg(' '.join(supportedFlags))
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -0800106 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800107
108
109class CompilerFlags(object):
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400110 def getCompilerVersion(self, conf):
111 return tuple(int(i) for i in conf.env.CC_VERSION)
112
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800113 def getGeneralFlags(self, conf):
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -0800114 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800115 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
116
117 def getDebugFlags(self, conf):
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -0800118 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800119 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
120
121 def getOptimizedFlags(self, conf):
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -0800122 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800123 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
124
125class GccBasicFlags(CompilerFlags):
Alexander Afanasyevc7c99002015-10-09 17:27:30 -0700126 """
127 This class defines basic flags that work for both gcc and clang compilers
128 """
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400129 def getGeneralFlags(self, conf):
130 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
Davide Pesavento90034832018-05-30 10:10:31 -0400131 flags['CXXFLAGS'] += ['-std=c++14']
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400132 return flags
133
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800134 def getDebugFlags(self, conf):
135 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -0800136 flags['CXXFLAGS'] += ['-O0',
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400137 '-Og', # gcc >= 4.8, clang >= 4.0
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800138 '-g3',
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -0800139 '-pedantic',
140 '-Wall',
141 '-Wextra',
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800142 '-Werror',
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400143 '-Wnon-virtual-dtor',
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -0800144 '-Wno-error=deprecated-declarations', # Bug #3795
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400145 '-Wno-error=maybe-uninitialized', # Bug #1615
146 '-Wno-unused-parameter',
Alexander Afanasyevc7c99002015-10-09 17:27:30 -0700147 ]
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400148 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800149 return flags
150
151 def getOptimizedFlags(self, conf):
152 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -0800153 flags['CXXFLAGS'] += ['-O2',
Alexander Afanasyevc7c99002015-10-09 17:27:30 -0700154 '-g',
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -0800155 '-pedantic',
156 '-Wall',
157 '-Wextra',
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400158 '-Wnon-virtual-dtor',
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -0800159 '-Wno-unused-parameter',
Alexander Afanasyevc7c99002015-10-09 17:27:30 -0700160 ]
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400161 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800162 return flags
163
164class GccFlags(GccBasicFlags):
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800165 def getDebugFlags(self, conf):
166 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesavento90034832018-05-30 10:10:31 -0400167 flags['CXXFLAGS'] += ['-fdiagnostics-color']
Alexander Afanasyevc7c99002015-10-09 17:27:30 -0700168 return flags
169
170 def getOptimizedFlags(self, conf):
171 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesavento90034832018-05-30 10:10:31 -0400172 flags['CXXFLAGS'] += ['-fdiagnostics-color']
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800173 return flags
174
175class ClangFlags(GccBasicFlags):
176 def getGeneralFlags(self, conf):
177 flags = super(ClangFlags, self).getGeneralFlags(conf)
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400178 if Utils.unversioned_sys_platform() == 'darwin' and self.getCompilerVersion(conf) >= (9, 0, 0):
179 # Bug #4296
180 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include'], # for Homebrew
181 ['-isystem', '/opt/local/include']] # for MacPorts
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800182 return flags
183
184 def getDebugFlags(self, conf):
185 flags = super(ClangFlags, self).getDebugFlags(conf)
Alexander Afanasyevc7c99002015-10-09 17:27:30 -0700186 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400187 '-Wextra-semi',
188 '-Wundefined-func-template',
Alexander Afanasyevc7c99002015-10-09 17:27:30 -0700189 '-Wno-error=deprecated-register',
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -0800190 '-Wno-error=infinite-recursion', # Bug #3358
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400191 '-Wno-error=keyword-macro', # Bug #3235
192 '-Wno-error=unneeded-internal-declaration', # Bug #1588
193 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
Alexander Afanasyevc7c99002015-10-09 17:27:30 -0700194 ]
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400195 version = self.getCompilerVersion(conf)
196 if version < (3, 9, 0) or (Utils.unversioned_sys_platform() == 'darwin' and version < (8, 1, 0)):
197 flags['CXXFLAGS'] += ['-Wno-unknown-pragmas']
Alexander Afanasyevc7c99002015-10-09 17:27:30 -0700198 return flags
199
200 def getOptimizedFlags(self, conf):
201 flags = super(ClangFlags, self).getOptimizedFlags(conf)
202 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400203 '-Wextra-semi',
204 '-Wundefined-func-template',
Alexander Afanasyevc7c99002015-10-09 17:27:30 -0700205 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
206 ]
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400207 version = self.getCompilerVersion(conf)
208 if version < (3, 9, 0) or (Utils.unversioned_sys_platform() == 'darwin' and version < (8, 1, 0)):
209 flags['CXXFLAGS'] += ['-Wno-unknown-pragmas']
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800210 return flags