blob: fd15dbbe8b8196892360641bf7830bda0a24ce8f [file] [log] [blame]
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -07001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -07002
Alexander Afanasyev76818632015-01-26 21:30:45 -08003from waflib import Logs, Configure, Utils
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -07004
5def options(opt):
6 opt.add_option('--debug', '--with-debug', action='store_true', default=False, dest='debug',
Davide Pesaventoab1e8f22014-10-21 22:45:33 +02007 help='''Compile in debugging mode without optimizations (-O0 or -Og)''')
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -07008
9def configure(conf):
Alexander Afanasyev76818632015-01-26 21:30:45 -080010 cxx = conf.env['CXX_NAME'] # CXX_NAME represents generic name of the compiler
11 if cxx == 'gcc':
12 flags = GccFlags()
13 elif cxx == 'clang':
14 flags = ClangFlags()
15 else:
16 flags = CompilerFlags()
Davide Pesaventof485d892015-10-02 02:00:54 +020017 Logs.warn('The code has not yet been tested with %s compiler' % cxx)
Davide Pesavento1bdef282014-04-08 20:59:50 +020018
Alexander Afanasyev76818632015-01-26 21:30:45 -080019 areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
20
Davide Pesaventof485d892015-10-02 02:00:54 +020021 # General flags are always applied (e.g., selecting C++11 mode)
Alexander Afanasyev76818632015-01-26 21:30:45 -080022 generalFlags = flags.getGeneralFlags(conf)
23 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
24 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
25 conf.env.DEFINES += generalFlags['DEFINES']
26
Davide Pesaventof485d892015-10-02 02:00:54 +020027 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Alexander Afanasyev76818632015-01-26 21:30:45 -080028 # corresponding environment variables are not set.
Davide Pesaventof485d892015-10-02 02:00:54 +020029 # DEFINES are always applied.
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070030 if conf.options.debug:
Alexander Afanasyev76818632015-01-26 21:30:45 -080031 extraFlags = flags.getDebugFlags(conf)
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070032 if areCustomCxxflagsPresent:
Alexander Afanasyev76818632015-01-26 21:30:45 -080033 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070034 if len(missingFlags) > 0:
35 Logs.warn("Selected debug mode, but CXXFLAGS is set to a custom value '%s'"
36 % " ".join(conf.env.CXXFLAGS))
37 Logs.warn("Default flags '%s' are not activated" % " ".join(missingFlags))
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070038 else:
Alexander Afanasyev76818632015-01-26 21:30:45 -080039 extraFlags = flags.getOptimizedFlags(conf)
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070040
Alexander Afanasyev76818632015-01-26 21:30:45 -080041 if not areCustomCxxflagsPresent:
42 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Davide Pesaventoed89ccc2015-11-01 20:35:04 +010043 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Alexander Afanasyev76818632015-01-26 21:30:45 -080044
45 conf.env.DEFINES += extraFlags['DEFINES']
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020046
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070047@Configure.conf
48def add_supported_cxxflags(self, cxxflags):
49 """
50 Check which cxxflags are supported by compiler and add them to env.CXXFLAGS variable
51 """
Alexander Afanasyev76818632015-01-26 21:30:45 -080052 if len(cxxflags) == 0:
53 return
54
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020055 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070056
57 supportedFlags = []
58 for flag in cxxflags:
Alexander Afanasyevd35176c2014-04-25 19:09:41 -070059 if self.check_cxx(cxxflags=['-Werror', flag], mandatory=False):
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070060 supportedFlags += [flag]
61
Davide Pesavento1bdef282014-04-08 20:59:50 +020062 self.end_msg(' '.join(supportedFlags))
Davide Pesavento67f30272016-08-10 01:55:16 +000063 self.env.prepend_value('CXXFLAGS', supportedFlags)
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020064
65@Configure.conf
66def add_supported_linkflags(self, linkflags):
67 """
68 Check which linkflags are supported by compiler and add them to env.LINKFLAGS variable
69 """
Alexander Afanasyev76818632015-01-26 21:30:45 -080070 if len(linkflags) == 0:
71 return
72
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020073 self.start_msg('Checking supported LINKFLAGS')
74
75 supportedFlags = []
76 for flag in linkflags:
77 if self.check_cxx(linkflags=['-Werror', flag], mandatory=False):
78 supportedFlags += [flag]
79
80 self.end_msg(' '.join(supportedFlags))
Davide Pesavento67f30272016-08-10 01:55:16 +000081 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyev76818632015-01-26 21:30:45 -080082
83
84class CompilerFlags(object):
85 def getGeneralFlags(self, conf):
Davide Pesaventoed89ccc2015-11-01 20:35:04 +010086 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Alexander Afanasyev76818632015-01-26 21:30:45 -080087 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
88
89 def getDebugFlags(self, conf):
Davide Pesaventoed89ccc2015-11-01 20:35:04 +010090 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Alexander Afanasyev76818632015-01-26 21:30:45 -080091 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
92
93 def getOptimizedFlags(self, conf):
Davide Pesaventoed89ccc2015-11-01 20:35:04 +010094 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Alexander Afanasyev76818632015-01-26 21:30:45 -080095 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
96
97class GccBasicFlags(CompilerFlags):
Davide Pesaventof485d892015-10-02 02:00:54 +020098 """
99 This class defines basic flags that work for both gcc and clang compilers
100 """
Alexander Afanasyev76818632015-01-26 21:30:45 -0800101 def getDebugFlags(self, conf):
102 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100103 flags['CXXFLAGS'] += ['-O0',
Alexander Afanasyev76818632015-01-26 21:30:45 -0800104 '-g3',
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100105 '-pedantic',
106 '-Wall',
107 '-Wextra',
Alexander Afanasyev76818632015-01-26 21:30:45 -0800108 '-Werror',
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100109 '-Wno-unused-parameter',
Alexander Afanasyev76818632015-01-26 21:30:45 -0800110 '-Wno-error=maybe-uninitialized', # Bug #1615
Davide Pesaventof485d892015-10-02 02:00:54 +0200111 ]
Alexander Afanasyev76818632015-01-26 21:30:45 -0800112 return flags
113
114 def getOptimizedFlags(self, conf):
115 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100116 flags['CXXFLAGS'] += ['-O2',
Davide Pesaventof485d892015-10-02 02:00:54 +0200117 '-g',
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100118 '-pedantic',
119 '-Wall',
120 '-Wextra',
121 '-Wno-unused-parameter',
Davide Pesaventof485d892015-10-02 02:00:54 +0200122 ]
Alexander Afanasyev76818632015-01-26 21:30:45 -0800123 return flags
124
125class GccFlags(GccBasicFlags):
126 def getGeneralFlags(self, conf):
127 flags = super(GccFlags, self).getGeneralFlags(conf)
128 version = tuple(int(i) for i in conf.env['CC_VERSION'])
Davide Pesaventocafae242016-04-22 02:21:35 +0200129 if version < (4, 8, 2):
Alexander Afanasyev76818632015-01-26 21:30:45 -0800130 conf.fatal('The version of gcc you are using (%s) is too old.\n' %
131 '.'.join(conf.env['CC_VERSION']) +
Davide Pesaventocafae242016-04-22 02:21:35 +0200132 'The minimum supported gcc version is 4.8.2.')
Alexander Afanasyev76818632015-01-26 21:30:45 -0800133 else:
134 flags['CXXFLAGS'] += ['-std=c++11']
135 return flags
136
137 def getDebugFlags(self, conf):
138 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100139 version = tuple(int(i) for i in conf.env['CC_VERSION'])
140 if version < (5, 1, 0):
141 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
Alexander Afanasyev76818632015-01-26 21:30:45 -0800142 flags['CXXFLAGS'] += ['-Og', # gcc >= 4.8
143 '-fdiagnostics-color', # gcc >= 4.9
Davide Pesaventof485d892015-10-02 02:00:54 +0200144 ]
145 return flags
146
147 def getOptimizedFlags(self, conf):
148 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100149 version = tuple(int(i) for i in conf.env['CC_VERSION'])
150 if version < (5, 1, 0):
151 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
Davide Pesaventof485d892015-10-02 02:00:54 +0200152 flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
Alexander Afanasyev76818632015-01-26 21:30:45 -0800153 return flags
154
155class ClangFlags(GccBasicFlags):
156 def getGeneralFlags(self, conf):
157 flags = super(ClangFlags, self).getGeneralFlags(conf)
Davide Pesaventof485d892015-10-02 02:00:54 +0200158 flags['CXXFLAGS'] += ['-std=c++11']
159 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev76818632015-01-26 21:30:45 -0800160 flags['CXXFLAGS'] += ['-stdlib=libc++']
161 flags['LINKFLAGS'] += ['-stdlib=libc++']
162 return flags
163
164 def getDebugFlags(self, conf):
165 flags = super(ClangFlags, self).getDebugFlags(conf)
Davide Pesaventof485d892015-10-02 02:00:54 +0200166 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
167 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
168 '-Wno-error=unneeded-internal-declaration', # Bug #1588
169 '-Wno-error=deprecated-register',
Junxiao Shi747b67d2015-10-04 16:59:41 -0700170 '-Wno-error=keyword-macro', # Bug #3235
Alexander Afanasyev0fc97482016-09-13 18:33:15 +0000171 '-Wno-error=infinite-recursion', # Bug #3358
Davide Pesaventof485d892015-10-02 02:00:54 +0200172 ]
173 return flags
174
175 def getOptimizedFlags(self, conf):
176 flags = super(ClangFlags, self).getOptimizedFlags(conf)
177 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
178 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
179 ]
Alexander Afanasyev76818632015-01-26 21:30:45 -0800180 return flags