blob: 5ee8dfa26f5d43fe5d1921f3c87bc3b6c69b09c9 [file] [log] [blame]
Alexander Afanasyev740812e2014-10-30 15:37:45 -07001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
2
Davide Pesavento64cafaa2018-05-24 00:50:41 -04003from waflib import Configure, Logs, Utils
Alexander Afanasyev740812e2014-10-30 15:37:45 -07004
5def options(opt):
Davide Pesavento64cafaa2018-05-24 00:50:41 -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 Afanasyev740812e2014-10-30 15:37:45 -07008
9def configure(conf):
Davide Pesavento29db0fd2017-08-29 13:32:00 -040010 conf.start_msg('Checking C++ compiler version')
Alexander Afanasyev740812e2014-10-30 15:37:45 -070011
Davide Pesavento64cafaa2018-05-24 00:50:41 -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 Pesavento29db0fd2017-08-29 13:32:00 -040015 errmsg = ''
16 warnmsg = ''
17 if cxx == 'gcc':
Davide Pesavento64cafaa2018-05-24 00:50:41 -040018 if ccver < (5, 3, 0):
Davide Pesavento29db0fd2017-08-29 13:32:00 -040019 errmsg = ('The version of gcc you are using is too old.\n'
Davide Pesavento64cafaa2018-05-24 00:50:41 -040020 'The minimum supported gcc version is 5.3.0.')
21 conf.flags = GccFlags()
Davide Pesavento29db0fd2017-08-29 13:32:00 -040022 elif cxx == 'clang':
Davide Pesavento64cafaa2018-05-24 00:50:41 -040023 if ccver < (3, 5, 0):
Davide Pesavento29db0fd2017-08-29 13:32:00 -040024 errmsg = ('The version of clang you are using is too old.\n'
Davide Pesavento64cafaa2018-05-24 00:50:41 -040025 'The minimum supported clang version is 3.5.0.')
26 conf.flags = ClangFlags()
Davide Pesavento29db0fd2017-08-29 13:32:00 -040027 else:
28 warnmsg = 'Note: %s compiler is unsupported' % cxx
Davide Pesavento64cafaa2018-05-24 00:50:41 -040029 conf.flags = CompilerFlags()
Davide Pesavento29db0fd2017-08-29 13:32:00 -040030
31 if errmsg:
Davide Pesavento64cafaa2018-05-24 00:50:41 -040032 conf.end_msg(ccverstr, color='RED')
Davide Pesavento29db0fd2017-08-29 13:32:00 -040033 conf.fatal(errmsg)
34 elif warnmsg:
Davide Pesavento64cafaa2018-05-24 00:50:41 -040035 conf.end_msg(ccverstr, color='YELLOW')
Davide Pesavento29db0fd2017-08-29 13:32:00 -040036 Logs.warn(warnmsg)
37 else:
Davide Pesavento64cafaa2018-05-24 00:50:41 -040038 conf.end_msg(ccverstr)
Davide Pesavento29db0fd2017-08-29 13:32:00 -040039
Davide Pesavento64cafaa2018-05-24 00:50:41 -040040 conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
Davide Pesavento29db0fd2017-08-29 13:32:00 -040041
Davide Pesavento64cafaa2018-05-24 00:50:41 -040042 # General flags are always applied (e.g., selecting C++ language standard)
43 generalFlags = conf.flags.getGeneralFlags(conf)
Davide Pesavento29db0fd2017-08-29 13:32:00 -040044 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
45 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
46 conf.env.DEFINES += generalFlags['DEFINES']
47
Davide Pesavento64cafaa2018-05-24 00:50:41 -040048@Configure.conf
49def check_compiler_flags(conf):
Davide Pesavento29db0fd2017-08-29 13:32:00 -040050 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
51 # corresponding environment variables are not set.
52 # DEFINES are always applied.
Alexander Afanasyev740812e2014-10-30 15:37:45 -070053 if conf.options.debug:
Davide Pesavento64cafaa2018-05-24 00:50:41 -040054 extraFlags = conf.flags.getDebugFlags(conf)
55 if conf.areCustomCxxflagsPresent:
Davide Pesavento29db0fd2017-08-29 13:32:00 -040056 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Davide Pesavento64cafaa2018-05-24 00:50:41 -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 Afanasyev740812e2014-10-30 15:37:45 -070061 else:
Davide Pesavento64cafaa2018-05-24 00:50:41 -040062 extraFlags = conf.flags.getOptimizedFlags(conf)
Alexander Afanasyev740812e2014-10-30 15:37:45 -070063
Davide Pesavento64cafaa2018-05-24 00:50:41 -040064 if not conf.areCustomCxxflagsPresent:
Davide Pesavento29db0fd2017-08-29 13:32:00 -040065 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
66 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
67
68 conf.env.DEFINES += extraFlags['DEFINES']
Alexander Afanasyev740812e2014-10-30 15:37:45 -070069
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 """
Davide Pesavento29db0fd2017-08-29 13:32:00 -040075 if len(cxxflags) == 0:
76 return
77
Alexander Afanasyev740812e2014-10-30 15:37:45 -070078 self.start_msg('Checking supported CXXFLAGS')
79
80 supportedFlags = []
Davide Pesavento64cafaa2018-05-24 00:50:41 -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 Afanasyev740812e2014-10-30 15:37:45 -070085
86 self.end_msg(' '.join(supportedFlags))
Davide Pesavento29db0fd2017-08-29 13:32:00 -040087 self.env.prepend_value('CXXFLAGS', supportedFlags)
Alexander Afanasyev740812e2014-10-30 15:37:45 -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 """
Davide Pesavento29db0fd2017-08-29 13:32:00 -040094 if len(linkflags) == 0:
95 return
96
Alexander Afanasyev740812e2014-10-30 15:37:45 -070097 self.start_msg('Checking supported LINKFLAGS')
98
99 supportedFlags = []
Davide Pesavento64cafaa2018-05-24 00:50:41 -0400100 for flags in linkflags:
101 flags = Utils.to_list(flags)
102 if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
103 supportedFlags += flags
Alexander Afanasyev740812e2014-10-30 15:37:45 -0700104
105 self.end_msg(' '.join(supportedFlags))
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400106 self.env.prepend_value('LINKFLAGS', supportedFlags)
107
108
109class CompilerFlags(object):
Davide Pesavento64cafaa2018-05-24 00:50:41 -0400110 def getCompilerVersion(self, conf):
111 return tuple(int(i) for i in conf.env.CC_VERSION)
112
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400113 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 """
129 def getGeneralFlags(self, conf):
130 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
Davide Pesavento64cafaa2018-05-24 00:50:41 -0400131 flags['CXXFLAGS'] += ['-std=c++14']
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400132 return flags
133
134 def getDebugFlags(self, conf):
135 flags = super(GccBasicFlags, self).getDebugFlags(conf)
136 flags['CXXFLAGS'] += ['-O0',
137 '-Og', # gcc >= 4.8, clang >= 4.0
138 '-g3',
139 '-pedantic',
140 '-Wall',
141 '-Wextra',
142 '-Werror',
Davide Pesavento64cafaa2018-05-24 00:50:41 -0400143 '-Wnon-virtual-dtor',
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400144 '-Wno-error=deprecated-declarations', # Bug #3795
145 '-Wno-error=maybe-uninitialized', # Bug #1615
146 '-Wno-unused-parameter',
147 ]
148 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
149 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',
Davide Pesavento64cafaa2018-05-24 00:50:41 -0400158 '-Wnon-virtual-dtor',
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400159 '-Wno-unused-parameter',
160 ]
161 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
162 return flags
163
164class GccFlags(GccBasicFlags):
165 def getDebugFlags(self, conf):
166 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesavento64cafaa2018-05-24 00:50:41 -0400167 flags['CXXFLAGS'] += ['-fdiagnostics-color']
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400168 return flags
169
170 def getOptimizedFlags(self, conf):
171 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesavento64cafaa2018-05-24 00:50:41 -0400172 flags['CXXFLAGS'] += ['-fdiagnostics-color']
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400173 return flags
174
175class ClangFlags(GccBasicFlags):
Davide Pesavento64cafaa2018-05-24 00:50:41 -0400176 def getGeneralFlags(self, conf):
177 flags = super(ClangFlags, self).getGeneralFlags(conf)
178 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
182 return flags
183
Davide Pesavento29db0fd2017-08-29 13:32:00 -0400184 def getDebugFlags(self, conf):
185 flags = super(ClangFlags, self).getDebugFlags(conf)
186 flags['CXXFLAGS'] += ['-fcolor-diagnostics']
187 return flags
188
189 def getOptimizedFlags(self, conf):
190 flags = super(ClangFlags, self).getOptimizedFlags(conf)
191 flags['CXXFLAGS'] += ['-fcolor-diagnostics']
192 return flags