blob: dd78c1c0dfb312df38efb7fefbdc7844e63ce49b [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 Afanasyev83b7c2f2015-01-23 13:08:34 -08003from waflib import Logs, Configure, Utils
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -07004
5def options(opt):
6 opt.add_option('--debug', '--with-debug', action='store_true', default=False, dest='debug',
Shock Jiang2d8483c2014-10-30 10:15:37 -07007 help='''Compile in debugging mode without optimizations (-O0 or -Og)''')
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -07008
9def configure(conf):
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -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()
17 Logs.warn('The code has not been yet tested with %s compiler' % cxx)
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070018
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080019 areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
20
21 # General flags will alway be applied (e.g., selecting C++11 mode)
22 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
27 # Debug or optimization CXXFLAGS and LINKFLAGS will be applied only if the
28 # corresponding environment variables are not set.
29 # DEFINES will be always applied
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070030 if conf.options.debug:
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080031 extraFlags = flags.getDebugFlags(conf)
32
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070033 if areCustomCxxflagsPresent:
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080034 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070035 if len(missingFlags) > 0:
36 Logs.warn("Selected debug mode, but CXXFLAGS is set to a custom value '%s'"
37 % " ".join(conf.env.CXXFLAGS))
38 Logs.warn("Default flags '%s' are not activated" % " ".join(missingFlags))
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070039 else:
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080040 extraFlags = flags.getOptimizedFlags(conf)
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070041
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080042 if not areCustomCxxflagsPresent:
43 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
44 conf.add_supported_cxxflags(extraFlags['LINKFLAGS'])
45
46 conf.env.DEFINES += extraFlags['DEFINES']
Shock Jiang2d8483c2014-10-30 10:15:37 -070047
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070048@Configure.conf
49def add_supported_cxxflags(self, cxxflags):
50 """
51 Check which cxxflags are supported by compiler and add them to env.CXXFLAGS variable
52 """
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080053 if len(cxxflags) == 0:
54 return
55
Shock Jiang2d8483c2014-10-30 10:15:37 -070056 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070057
58 supportedFlags = []
59 for flag in cxxflags:
60 if self.check_cxx(cxxflags=['-Werror', flag], mandatory=False):
61 supportedFlags += [flag]
62
63 self.end_msg(' '.join(supportedFlags))
64 self.env.CXXFLAGS = supportedFlags + self.env.CXXFLAGS
Shock Jiang2d8483c2014-10-30 10:15:37 -070065
66@Configure.conf
67def add_supported_linkflags(self, linkflags):
68 """
69 Check which linkflags are supported by compiler and add them to env.LINKFLAGS variable
70 """
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080071 if len(linkflags) == 0:
72 return
73
Shock Jiang2d8483c2014-10-30 10:15:37 -070074 self.start_msg('Checking supported LINKFLAGS')
75
76 supportedFlags = []
77 for flag in linkflags:
78 if self.check_cxx(linkflags=['-Werror', flag], mandatory=False):
79 supportedFlags += [flag]
80
81 self.end_msg(' '.join(supportedFlags))
82 self.env.LINKFLAGS = supportedFlags + self.env.LINKFLAGS
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080083
84
85class CompilerFlags(object):
86 def getGeneralFlags(self, conf):
87 """Get dict {'CXXFLAGS':[...], LINKFLAGS:[...], DEFINES:[...]} that are always needed"""
88 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
89
90 def getDebugFlags(self, conf):
91 """Get tuple {CXXFLAGS, LINKFLAGS, DEFINES} that are needed in debug mode"""
92 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
93
94 def getOptimizedFlags(self, conf):
95 """Get tuple {CXXFLAGS, LINKFLAGS, DEFINES} that are needed in optimized mode"""
96 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
97
98class GccBasicFlags(CompilerFlags):
99 """This class defines base flags that work for gcc and clang compiler"""
100 def getDebugFlags(self, conf):
101 flags = super(GccBasicFlags, self).getDebugFlags(conf)
102 flags['CXXFLAGS'] += ['-Wall',
103 '-O0',
104 '-g3',
105 '-Werror',
106 '-Wno-error=maybe-uninitialized', # Bug #1615
107 ]
108 return flags
109
110 def getOptimizedFlags(self, conf):
111 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
112 flags['CXXFLAGS'] += ['-Wall', '-O2', '-g']
113 return flags
114
115class GccFlags(GccBasicFlags):
116 def getGeneralFlags(self, conf):
117 flags = super(GccFlags, self).getGeneralFlags(conf)
118 version = tuple(int(i) for i in conf.env['CC_VERSION'])
119 if version < (4, 6, 0):
120 conf.fatal('The version of gcc you are using (%s) is too old.\n' %
121 '.'.join(conf.env['CC_VERSION']) +
122 'The minimum supported gcc version is 4.6.0.')
123 elif version < (4, 7, 0):
124 flags['CXXFLAGS'] += ['-std=c++0x']
125 else:
126 flags['CXXFLAGS'] += ['-std=c++11']
127 return flags
128
129 def getDebugFlags(self, conf):
130 flags = super(GccFlags, self).getDebugFlags(conf)
131 flags['CXXFLAGS'] += ['-Og', # gcc >= 4.8
132 '-fdiagnostics-color', # gcc >= 4.9
133 ]
134 return flags
135
136class ClangFlags(GccBasicFlags):
137 def getGeneralFlags(self, conf):
138 flags = super(ClangFlags, self).getGeneralFlags(conf)
139 flags['CXXFLAGS'] += ['-std=c++11',
140 '-Wno-error=unneeded-internal-declaration', # Bug #1588
141 '-Wno-error=deprecated-register',
142 ]
143 if Utils.unversioned_sys_platform() == "darwin":
144 flags['CXXFLAGS'] += ['-stdlib=libc++']
145 flags['LINKFLAGS'] += ['-stdlib=libc++']
146 return flags
147
148 def getDebugFlags(self, conf):
149 flags = super(ClangFlags, self).getDebugFlags(conf)
150 flags['CXXFLAGS'] += ['-fcolor-diagnostics']
151 return flags