blob: e6902902933a557a1ab26f89ee3473513c87f3eb [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 Pesavento98ff2bc2018-05-24 00:10:31 -04003from waflib import Configure, Logs, Utils
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -07004
Shuo Chen478204c2014-03-18 18:27:04 -07005def options(opt):
Davide Pesavento98ff2bc2018-05-24 00:10:31 -04006 opt.add_option('--debug', '--with-debug', action='store_true', default=False,
7 help='Compile in debugging mode with minimal optimizations (-O0 or -Og)')
Shuo Chen478204c2014-03-18 18:27:04 -07008
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -07009def configure(conf):
Davide Pesaventob545aac2017-09-22 23:54:10 -040010 conf.start_msg('Checking C++ compiler version')
11
Davide Pesavento98ff2bc2018-05-24 00:10:31 -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)
Davide Pesaventob545aac2017-09-22 23:54:10 -040015 errmsg = ''
16 warnmsg = ''
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050017 if cxx == 'gcc':
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040018 if ccver < (5, 3, 0):
Davide Pesaventob545aac2017-09-22 23:54:10 -040019 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040020 'The minimum supported gcc version is 5.3.0.')
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000021 conf.flags = GccFlags()
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050022 elif cxx == 'clang':
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040023 if ccver < (3, 5, 0):
Davide Pesaventob545aac2017-09-22 23:54:10 -040024 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040025 'The minimum supported clang version is 3.5.0.')
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000026 conf.flags = ClangFlags()
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050027 else:
Davide Pesaventob545aac2017-09-22 23:54:10 -040028 warnmsg = 'Note: %s compiler is unsupported' % cxx
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000029 conf.flags = CompilerFlags()
Davide Pesaventob545aac2017-09-22 23:54:10 -040030
31 if errmsg:
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040032 conf.end_msg(ccverstr, color='RED')
Davide Pesaventob545aac2017-09-22 23:54:10 -040033 conf.fatal(errmsg)
34 elif warnmsg:
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040035 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesaventob545aac2017-09-22 23:54:10 -040036 Logs.warn(warnmsg)
37 else:
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040038 conf.end_msg(ccverstr)
Shuo Chen478204c2014-03-18 18:27:04 -070039
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000040 conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050041
Davide Pesavento98ff2bc2018-05-24 00:10:31 -040042 # General flags are always applied (e.g., selecting C++ language standard)
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000043 generalFlags = conf.flags.getGeneralFlags(conf)
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050044 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
45 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
46 conf.env.DEFINES += generalFlags['DEFINES']
47
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000048@Configure.conf
49def check_compiler_flags(conf):
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050050 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
51 # corresponding environment variables are not set.
52 # DEFINES are always applied.
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070053 if conf.options.debug:
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000054 extraFlags = conf.flags.getDebugFlags(conf)
55 if conf.areCustomCxxflagsPresent:
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050056 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Davide Pesavento98ff2bc2018-05-24 00:10:31 -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 Afanasyev3fd14f02014-03-26 14:34:39 -070061 else:
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000062 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070063
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000064 if not conf.areCustomCxxflagsPresent:
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050065 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
66 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
67
68 conf.env.DEFINES += extraFlags['DEFINES']
Wentao Shanga8f3c402014-10-30 14:03:27 -070069
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -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 Afanasyevd352cce2015-11-20 14:15:11 -050075 if len(cxxflags) == 0:
76 return
77
Wentao Shanga8f3c402014-10-30 14:03:27 -070078 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070079
80 supportedFlags = []
Alexander Afanasyevbb058c02018-02-15 22:49:24 +000081 for flags in cxxflags:
82 flags = Utils.to_list(flags)
83 if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
84 supportedFlags += flags
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070085
Shuo Chen478204c2014-03-18 18:27:04 -070086 self.end_msg(' '.join(supportedFlags))
Junxiao Shie1801312017-07-22 19:16:49 +000087 self.env.prepend_value('CXXFLAGS', supportedFlags)
Wentao Shanga8f3c402014-10-30 14:03:27 -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 Afanasyevd352cce2015-11-20 14:15:11 -050094 if len(linkflags) == 0:
95 return
96
Wentao Shanga8f3c402014-10-30 14:03:27 -070097 self.start_msg('Checking supported LINKFLAGS')
98
99 supportedFlags = []
Alexander Afanasyevbb058c02018-02-15 22:49:24 +0000100 for flags in linkflags:
101 flags = Utils.to_list(flags)
102 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
103 supportedFlags += flags
Wentao Shanga8f3c402014-10-30 14:03:27 -0700104
105 self.end_msg(' '.join(supportedFlags))
Junxiao Shie1801312017-07-22 19:16:49 +0000106 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500107
108
109class CompilerFlags(object):
Davide Pesavento98ff2bc2018-05-24 00:10:31 -0400110 def getCompilerVersion(self, conf):
111 return tuple(int(i) for i in conf.env.CC_VERSION)
112
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500113 def getGeneralFlags(self, conf):
114 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
115 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
116
117 def getDebugFlags(self, conf):
118 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
119 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
120
121 def getOptimizedFlags(self, conf):
122 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
123 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
124
125class GccBasicFlags(CompilerFlags):
126 """
127 This class defines basic flags that work for both gcc and clang compilers
128 """
Junxiao Shie1801312017-07-22 19:16:49 +0000129 def getGeneralFlags(self, conf):
130 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
Davide Pesavento98ff2bc2018-05-24 00:10:31 -0400131 flags['CXXFLAGS'] += ['-std=c++14']
Junxiao Shie1801312017-07-22 19:16:49 +0000132 return flags
133
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500134 def getDebugFlags(self, conf):
135 flags = super(GccBasicFlags, self).getDebugFlags(conf)
136 flags['CXXFLAGS'] += ['-O0',
Junxiao Shie1801312017-07-22 19:16:49 +0000137 '-Og', # gcc >= 4.8, clang >= 4.0
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500138 '-g3',
139 '-pedantic',
140 '-Wall',
141 '-Wextra',
142 '-Werror',
Junxiao Shie1801312017-07-22 19:16:49 +0000143 '-Wnon-virtual-dtor',
Junxiao Shie1801312017-07-22 19:16:49 +0000144 '-Wno-error=deprecated-declarations', # Bug #3795
Davide Pesaventob545aac2017-09-22 23:54:10 -0400145 '-Wno-error=maybe-uninitialized', # Bug #1615
146 '-Wno-unused-parameter',
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500147 ]
Junxiao Shie1801312017-07-22 19:16:49 +0000148 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500149 return flags
150
151 def getOptimizedFlags(self, conf):
152 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
153 flags['CXXFLAGS'] += ['-O2',
154 '-g',
155 '-pedantic',
156 '-Wall',
157 '-Wextra',
Junxiao Shie1801312017-07-22 19:16:49 +0000158 '-Wnon-virtual-dtor',
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500159 '-Wno-unused-parameter',
160 ]
Junxiao Shie1801312017-07-22 19:16:49 +0000161 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500162 return flags
163
164class GccFlags(GccBasicFlags):
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500165 def getDebugFlags(self, conf):
166 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesavento98ff2bc2018-05-24 00:10:31 -0400167 flags['CXXFLAGS'] += ['-fdiagnostics-color']
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500168 return flags
169
170 def getOptimizedFlags(self, conf):
171 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesavento98ff2bc2018-05-24 00:10:31 -0400172 flags['CXXFLAGS'] += ['-fdiagnostics-color']
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500173 return flags
174
175class ClangFlags(GccBasicFlags):
176 def getGeneralFlags(self, conf):
177 flags = super(ClangFlags, self).getGeneralFlags(conf)
Davide Pesavento98ff2bc2018-05-24 00:10:31 -0400178 if Utils.unversioned_sys_platform() == 'darwin' and self.getCompilerVersion(conf) >= (9, 0, 0):
179 # Bug #4296
Alexander Afanasyevbb058c02018-02-15 22:49:24 +0000180 flags['CXXFLAGS'] += [['-isystem', '/usr/local/include'], # for Homebrew
181 ['-isystem', '/opt/local/include']] # for MacPorts
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500182 return flags
183
184 def getDebugFlags(self, conf):
185 flags = super(ClangFlags, self).getDebugFlags(conf)
186 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Alexander Afanasyevbb058c02018-02-15 22:49:24 +0000187 '-Wextra-semi',
188 '-Wundefined-func-template',
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500189 '-Wno-error=deprecated-register',
Junxiao Shie1801312017-07-22 19:16:49 +0000190 '-Wno-error=infinite-recursion', # Bug #3358
Davide Pesaventob545aac2017-09-22 23:54:10 -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 Afanasyevd352cce2015-11-20 14:15:11 -0500194 ]
Davide Pesavento98ff2bc2018-05-24 00:10:31 -0400195 version = self.getCompilerVersion(conf)
Alexander Afanasyevbb058c02018-02-15 22:49:24 +0000196 if version < (3, 9, 0) or (Utils.unversioned_sys_platform() == 'darwin' and version < (8, 1, 0)):
197 flags['CXXFLAGS'] += ['-Wno-unknown-pragmas']
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500198 return flags
199
200 def getOptimizedFlags(self, conf):
201 flags = super(ClangFlags, self).getOptimizedFlags(conf)
202 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
Alexander Afanasyevbb058c02018-02-15 22:49:24 +0000203 '-Wextra-semi',
204 '-Wundefined-func-template',
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500205 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
206 ]
Davide Pesavento98ff2bc2018-05-24 00:10:31 -0400207 version = self.getCompilerVersion(conf)
Alexander Afanasyevbb058c02018-02-15 22:49:24 +0000208 if version < (3, 9, 0) or (Utils.unversioned_sys_platform() == 'darwin' and version < (8, 1, 0)):
209 flags['CXXFLAGS'] += ['-Wno-unknown-pragmas']
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500210 return flags