blob: f82fe100cbf2a5b9af0329962114552685405264 [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()
Alexander Afanasyevc7c99002015-10-09 17:27:30 -070017 Logs.warn('The code has not yet been 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
Alexander Afanasyevc7c99002015-10-09 17:27:30 -070021 # General flags are always applied (e.g., selecting C++11 mode)
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -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
Alexander Afanasyevc7c99002015-10-09 17:27:30 -070027 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080028 # corresponding environment variables are not set.
Alexander Afanasyevc7c99002015-10-09 17:27:30 -070029 # DEFINES are 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)
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070032 if areCustomCxxflagsPresent:
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080033 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -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 Afanasyev4ffcff22014-09-02 15:39:20 -070038 else:
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080039 extraFlags = flags.getOptimizedFlags(conf)
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070040
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080041 if not areCustomCxxflagsPresent:
42 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
43 conf.add_supported_cxxflags(extraFlags['LINKFLAGS'])
44
45 conf.env.DEFINES += extraFlags['DEFINES']
Shock Jiang2d8483c2014-10-30 10:15:37 -070046
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -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 Afanasyev83b7c2f2015-01-23 13:08:34 -080052 if len(cxxflags) == 0:
53 return
54
Shock Jiang2d8483c2014-10-30 10:15:37 -070055 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070056
57 supportedFlags = []
58 for flag in cxxflags:
59 if self.check_cxx(cxxflags=['-Werror', flag], mandatory=False):
60 supportedFlags += [flag]
61
62 self.end_msg(' '.join(supportedFlags))
63 self.env.CXXFLAGS = supportedFlags + self.env.CXXFLAGS
Shock Jiang2d8483c2014-10-30 10:15:37 -070064
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 Afanasyev83b7c2f2015-01-23 13:08:34 -080070 if len(linkflags) == 0:
71 return
72
Shock Jiang2d8483c2014-10-30 10:15:37 -070073 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))
81 self.env.LINKFLAGS = supportedFlags + self.env.LINKFLAGS
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -080082
83
84class CompilerFlags(object):
85 def getGeneralFlags(self, conf):
86 """Get dict {'CXXFLAGS':[...], LINKFLAGS:[...], DEFINES:[...]} that are always needed"""
87 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
88
89 def getDebugFlags(self, conf):
90 """Get tuple {CXXFLAGS, LINKFLAGS, DEFINES} that are needed in debug mode"""
91 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
92
93 def getOptimizedFlags(self, conf):
94 """Get tuple {CXXFLAGS, LINKFLAGS, DEFINES} that are needed in optimized mode"""
95 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
96
97class GccBasicFlags(CompilerFlags):
Alexander Afanasyevc7c99002015-10-09 17:27:30 -070098 """
99 This class defines basic flags that work for both gcc and clang compilers
100 """
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800101 def getDebugFlags(self, conf):
102 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Alexander Afanasyevc7c99002015-10-09 17:27:30 -0700103 flags['CXXFLAGS'] += ['-pedantic',
104 '-Wall',
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800105 '-O0',
106 '-g3',
107 '-Werror',
108 '-Wno-error=maybe-uninitialized', # Bug #1615
Alexander Afanasyevc7c99002015-10-09 17:27:30 -0700109 ]
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800110 return flags
111
112 def getOptimizedFlags(self, conf):
113 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
Alexander Afanasyevc7c99002015-10-09 17:27:30 -0700114 flags['CXXFLAGS'] += ['-pedantic',
115 '-Wall',
116 '-O2',
117 '-g',
118 ]
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800119 return flags
120
121class GccFlags(GccBasicFlags):
122 def getGeneralFlags(self, conf):
123 flags = super(GccFlags, self).getGeneralFlags(conf)
124 version = tuple(int(i) for i in conf.env['CC_VERSION'])
125 if version < (4, 6, 0):
126 conf.fatal('The version of gcc you are using (%s) is too old.\n' %
127 '.'.join(conf.env['CC_VERSION']) +
128 'The minimum supported gcc version is 4.6.0.')
129 elif version < (4, 7, 0):
130 flags['CXXFLAGS'] += ['-std=c++0x']
131 else:
132 flags['CXXFLAGS'] += ['-std=c++11']
Alexander Afanasyevc7c99002015-10-09 17:27:30 -0700133 if version < (4, 8, 0):
134 flags['DEFINES'] += ['_GLIBCXX_USE_NANOSLEEP'] # Bug #2499
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800135 return flags
136
137 def getDebugFlags(self, conf):
138 flags = super(GccFlags, self).getDebugFlags(conf)
139 flags['CXXFLAGS'] += ['-Og', # gcc >= 4.8
140 '-fdiagnostics-color', # gcc >= 4.9
Alexander Afanasyevc7c99002015-10-09 17:27:30 -0700141 ]
142 return flags
143
144 def getOptimizedFlags(self, conf):
145 flags = super(GccFlags, self).getOptimizedFlags(conf)
146 flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800147 return flags
148
149class ClangFlags(GccBasicFlags):
150 def getGeneralFlags(self, conf):
151 flags = super(ClangFlags, self).getGeneralFlags(conf)
Alexander Afanasyevc7c99002015-10-09 17:27:30 -0700152 flags['CXXFLAGS'] += ['-std=c++11']
153 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800154 flags['CXXFLAGS'] += ['-stdlib=libc++']
155 flags['LINKFLAGS'] += ['-stdlib=libc++']
156 return flags
157
158 def getDebugFlags(self, conf):
159 flags = super(ClangFlags, self).getDebugFlags(conf)
Alexander Afanasyevc7c99002015-10-09 17:27:30 -0700160 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
161 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
162 '-Wno-error=unneeded-internal-declaration', # Bug #1588
163 '-Wno-error=deprecated-register',
164 '-Wno-error=keyword-macro', # Bug #3235
165 ]
166 return flags
167
168 def getOptimizedFlags(self, conf):
169 flags = super(ClangFlags, self).getOptimizedFlags(conf)
170 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
171 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
172 ]
Alexander Afanasyev83b7c2f2015-01-23 13:08:34 -0800173 return flags