blob: ae2058836d86c0769a479b6e23089db03f658f7e [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
Alexander Afanasyevd352cce2015-11-20 14:15:11 -05003from waflib import Logs, Configure, Utils
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -07004
Shuo Chen478204c2014-03-18 18:27:04 -07005def options(opt):
Shuo Chen478204c2014-03-18 18:27:04 -07006 opt.add_option('--debug', '--with-debug', action='store_true', default=False, dest='debug',
Junxiao Shie1801312017-07-22 19:16:49 +00007 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):
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050010 cxx = conf.env['CXX_NAME'] # CXX_NAME represents generic name of the compiler
Junxiao Shie1801312017-07-22 19:16:49 +000011 ccver = tuple(int(i) for i in conf.env['CC_VERSION'])
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050012 if cxx == 'gcc':
Junxiao Shie1801312017-07-22 19:16:49 +000013 if ccver < (4, 8, 2):
14 conf.fatal('The version of gcc you are using (%s) is too old.\n'
15 'The minimum supported gcc version is 4.8.2.' %
16 '.'.join(conf.env['CC_VERSION']))
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050017 flags = GccFlags()
18 elif cxx == 'clang':
Junxiao Shie1801312017-07-22 19:16:49 +000019 if ccver < (3, 4, 0):
20 conf.fatal('The version of clang you are using (%s) is too old.\n'
21 'The minimum supported clang version is 3.4.0.' %
22 '.'.join(conf.env['CC_VERSION']))
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050023 flags = ClangFlags()
24 else:
25 flags = CompilerFlags()
26 Logs.warn('The code has not yet been tested with %s compiler' % cxx)
Shuo Chen478204c2014-03-18 18:27:04 -070027
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050028 areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
29
30 # General flags are always applied (e.g., selecting C++11 mode)
31 generalFlags = flags.getGeneralFlags(conf)
32 conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
33 conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
34 conf.env.DEFINES += generalFlags['DEFINES']
35
36 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
37 # corresponding environment variables are not set.
38 # DEFINES are always applied.
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070039 if conf.options.debug:
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050040 extraFlags = flags.getDebugFlags(conf)
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070041 if areCustomCxxflagsPresent:
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050042 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070043 if len(missingFlags) > 0:
44 Logs.warn("Selected debug mode, but CXXFLAGS is set to a custom value '%s'"
Shuo Chen478204c2014-03-18 18:27:04 -070045 % " ".join(conf.env.CXXFLAGS))
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070046 Logs.warn("Default flags '%s' are not activated" % " ".join(missingFlags))
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070047 else:
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050048 extraFlags = flags.getOptimizedFlags(conf)
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070049
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050050 if not areCustomCxxflagsPresent:
51 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
52 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
53
54 conf.env.DEFINES += extraFlags['DEFINES']
Wentao Shanga8f3c402014-10-30 14:03:27 -070055
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070056@Configure.conf
57def add_supported_cxxflags(self, cxxflags):
58 """
59 Check which cxxflags are supported by compiler and add them to env.CXXFLAGS variable
60 """
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050061 if len(cxxflags) == 0:
62 return
63
Wentao Shanga8f3c402014-10-30 14:03:27 -070064 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070065
66 supportedFlags = []
67 for flag in cxxflags:
Wentao Shanga8f3c402014-10-30 14:03:27 -070068 if self.check_cxx(cxxflags=['-Werror', flag], mandatory=False):
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070069 supportedFlags += [flag]
70
Shuo Chen478204c2014-03-18 18:27:04 -070071 self.end_msg(' '.join(supportedFlags))
Junxiao Shie1801312017-07-22 19:16:49 +000072 self.env.prepend_value('CXXFLAGS', supportedFlags)
Wentao Shanga8f3c402014-10-30 14:03:27 -070073
74@Configure.conf
75def add_supported_linkflags(self, linkflags):
76 """
77 Check which linkflags are supported by compiler and add them to env.LINKFLAGS variable
78 """
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050079 if len(linkflags) == 0:
80 return
81
Wentao Shanga8f3c402014-10-30 14:03:27 -070082 self.start_msg('Checking supported LINKFLAGS')
83
84 supportedFlags = []
85 for flag in linkflags:
86 if self.check_cxx(linkflags=['-Werror', flag], mandatory=False):
87 supportedFlags += [flag]
88
89 self.end_msg(' '.join(supportedFlags))
Junxiao Shie1801312017-07-22 19:16:49 +000090 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyevd352cce2015-11-20 14:15:11 -050091
92
93class CompilerFlags(object):
94 def getGeneralFlags(self, conf):
95 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
96 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
97
98 def getDebugFlags(self, conf):
99 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
100 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
101
102 def getOptimizedFlags(self, conf):
103 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
104 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
105
106class GccBasicFlags(CompilerFlags):
107 """
108 This class defines basic flags that work for both gcc and clang compilers
109 """
Junxiao Shie1801312017-07-22 19:16:49 +0000110 def getGeneralFlags(self, conf):
111 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
112 flags['CXXFLAGS'] += ['-std=c++11']
113 return flags
114
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500115 def getDebugFlags(self, conf):
116 flags = super(GccBasicFlags, self).getDebugFlags(conf)
117 flags['CXXFLAGS'] += ['-O0',
Junxiao Shie1801312017-07-22 19:16:49 +0000118 '-Og', # gcc >= 4.8, clang >= 4.0
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500119 '-g3',
120 '-pedantic',
121 '-Wall',
122 '-Wextra',
123 '-Werror',
Junxiao Shie1801312017-07-22 19:16:49 +0000124 '-Wnon-virtual-dtor',
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500125 '-Wno-unused-parameter',
126 '-Wno-error=maybe-uninitialized', # Bug #1615
Junxiao Shie1801312017-07-22 19:16:49 +0000127 '-Wno-error=deprecated-declarations', # Bug #3795
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500128 ]
Junxiao Shie1801312017-07-22 19:16:49 +0000129 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500130 return flags
131
132 def getOptimizedFlags(self, conf):
133 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
134 flags['CXXFLAGS'] += ['-O2',
135 '-g',
136 '-pedantic',
137 '-Wall',
138 '-Wextra',
Junxiao Shie1801312017-07-22 19:16:49 +0000139 '-Wnon-virtual-dtor',
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500140 '-Wno-unused-parameter',
141 ]
Junxiao Shie1801312017-07-22 19:16:49 +0000142 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500143 return flags
144
145class GccFlags(GccBasicFlags):
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500146 def getDebugFlags(self, conf):
147 flags = super(GccFlags, self).getDebugFlags(conf)
148 version = tuple(int(i) for i in conf.env['CC_VERSION'])
149 if version < (5, 1, 0):
150 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
Junxiao Shie1801312017-07-22 19:16:49 +0000151 flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500152 return flags
153
154 def getOptimizedFlags(self, conf):
155 flags = super(GccFlags, self).getOptimizedFlags(conf)
156 version = tuple(int(i) for i in conf.env['CC_VERSION'])
157 if version < (5, 1, 0):
158 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
159 flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
160 return flags
161
162class ClangFlags(GccBasicFlags):
163 def getGeneralFlags(self, conf):
164 flags = super(ClangFlags, self).getGeneralFlags(conf)
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500165 if Utils.unversioned_sys_platform() == 'darwin':
166 flags['CXXFLAGS'] += ['-stdlib=libc++']
167 flags['LINKFLAGS'] += ['-stdlib=libc++']
168 return flags
169
170 def getDebugFlags(self, conf):
171 flags = super(ClangFlags, self).getDebugFlags(conf)
172 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
173 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
174 '-Wno-error=unneeded-internal-declaration', # Bug #1588
175 '-Wno-error=deprecated-register',
176 '-Wno-error=keyword-macro', # Bug #3235
Junxiao Shie1801312017-07-22 19:16:49 +0000177 '-Wno-error=infinite-recursion', # Bug #3358
Alexander Afanasyevd352cce2015-11-20 14:15:11 -0500178 ]
179 return flags
180
181 def getOptimizedFlags(self, conf):
182 flags = super(ClangFlags, self).getOptimizedFlags(conf)
183 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
184 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
185 ]
186 return flags