blob: f946d0d3a9f2feb6e6f06876715995e4a9e2025d [file] [log] [blame]
Junxiao Shif7191242015-03-19 05:53:41 -07001# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
2
3from waflib import Logs, Configure, Utils
4
5def options(opt):
6 opt.add_option('--debug', '--with-debug', action='store_true', default=False, dest='debug',
Davide Pesaventodd0e1952017-06-24 13:37:13 -04007 help='''Compile in debugging mode with minimal optimizations (-O0 or -Og)''')
Junxiao Shif7191242015-03-19 05:53:41 -07008
9def configure(conf):
10 cxx = conf.env['CXX_NAME'] # CXX_NAME represents generic name of the compiler
Davide Pesaventodd0e1952017-06-24 13:37:13 -040011 ccver = tuple(int(i) for i in conf.env['CC_VERSION'])
Junxiao Shif7191242015-03-19 05:53:41 -070012 if cxx == 'gcc':
Davide Pesaventodd0e1952017-06-24 13:37:13 -040013 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']))
Junxiao Shif7191242015-03-19 05:53:41 -070017 flags = GccFlags()
18 elif cxx == 'clang':
Davide Pesaventodd0e1952017-06-24 13:37:13 -040019 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']))
Junxiao Shif7191242015-03-19 05:53:41 -070023 flags = ClangFlags()
24 else:
25 flags = CompilerFlags()
Junxiao Shic8a0a252015-10-05 07:25:02 -070026 Logs.warn('The code has not yet been tested with %s compiler' % cxx)
Junxiao Shif7191242015-03-19 05:53:41 -070027
28 areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
29
Junxiao Shic8a0a252015-10-05 07:25:02 -070030 # General flags are always applied (e.g., selecting C++11 mode)
Junxiao Shif7191242015-03-19 05:53:41 -070031 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
Junxiao Shic8a0a252015-10-05 07:25:02 -070036 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Junxiao Shif7191242015-03-19 05:53:41 -070037 # corresponding environment variables are not set.
Junxiao Shic8a0a252015-10-05 07:25:02 -070038 # DEFINES are always applied.
Junxiao Shif7191242015-03-19 05:53:41 -070039 if conf.options.debug:
40 extraFlags = flags.getDebugFlags(conf)
Junxiao Shif7191242015-03-19 05:53:41 -070041 if areCustomCxxflagsPresent:
42 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
43 if len(missingFlags) > 0:
44 Logs.warn("Selected debug mode, but CXXFLAGS is set to a custom value '%s'"
45 % " ".join(conf.env.CXXFLAGS))
46 Logs.warn("Default flags '%s' are not activated" % " ".join(missingFlags))
47 else:
48 extraFlags = flags.getOptimizedFlags(conf)
49
50 if not areCustomCxxflagsPresent:
51 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Davide Pesavento3e79c9c2015-11-01 20:38:28 +010052 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Junxiao Shif7191242015-03-19 05:53:41 -070053
54 conf.env.DEFINES += extraFlags['DEFINES']
55
56@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 """
61 if len(cxxflags) == 0:
62 return
63
64 self.start_msg('Checking supported CXXFLAGS')
65
66 supportedFlags = []
67 for flag in cxxflags:
68 if self.check_cxx(cxxflags=['-Werror', flag], mandatory=False):
69 supportedFlags += [flag]
70
71 self.end_msg(' '.join(supportedFlags))
Davide Pesavento89d91752016-08-14 11:34:09 +020072 self.env.prepend_value('CXXFLAGS', supportedFlags)
Junxiao Shif7191242015-03-19 05:53:41 -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 """
79 if len(linkflags) == 0:
80 return
81
82 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))
Davide Pesavento89d91752016-08-14 11:34:09 +020090 self.env.prepend_value('LINKFLAGS', supportedFlags)
Junxiao Shif7191242015-03-19 05:53:41 -070091
92
93class CompilerFlags(object):
94 def getGeneralFlags(self, conf):
Davide Pesavento3e79c9c2015-11-01 20:38:28 +010095 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Junxiao Shif7191242015-03-19 05:53:41 -070096 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
97
98 def getDebugFlags(self, conf):
Davide Pesavento3e79c9c2015-11-01 20:38:28 +010099 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Junxiao Shif7191242015-03-19 05:53:41 -0700100 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
101
102 def getOptimizedFlags(self, conf):
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100103 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Junxiao Shif7191242015-03-19 05:53:41 -0700104 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
105
106class GccBasicFlags(CompilerFlags):
Junxiao Shic8a0a252015-10-05 07:25:02 -0700107 """
108 This class defines basic flags that work for both gcc and clang compilers
109 """
Davide Pesaventodd0e1952017-06-24 13:37:13 -0400110 def getGeneralFlags(self, conf):
111 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
112 flags['CXXFLAGS'] += ['-std=c++11']
113 return flags
114
Junxiao Shif7191242015-03-19 05:53:41 -0700115 def getDebugFlags(self, conf):
116 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100117 flags['CXXFLAGS'] += ['-O0',
Davide Pesaventodd0e1952017-06-24 13:37:13 -0400118 '-Og', # gcc >= 4.8, clang >= 4.0
Junxiao Shif7191242015-03-19 05:53:41 -0700119 '-g3',
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100120 '-pedantic',
121 '-Wall',
122 '-Wextra',
Junxiao Shif7191242015-03-19 05:53:41 -0700123 '-Werror',
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100124 '-Wno-unused-parameter',
Junxiao Shif7191242015-03-19 05:53:41 -0700125 '-Wno-error=maybe-uninitialized', # Bug #1615
Alexander Afanasyevfc4aa0d2016-10-15 00:57:33 +0000126 '-Wno-error=deprecated-declarations', # Bug #3795
Junxiao Shic8a0a252015-10-05 07:25:02 -0700127 ]
Davide Pesaventodd0e1952017-06-24 13:37:13 -0400128 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Junxiao Shif7191242015-03-19 05:53:41 -0700129 return flags
130
131 def getOptimizedFlags(self, conf):
132 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100133 flags['CXXFLAGS'] += ['-O2',
Junxiao Shic8a0a252015-10-05 07:25:02 -0700134 '-g',
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100135 '-pedantic',
136 '-Wall',
137 '-Wextra',
138 '-Wno-unused-parameter',
Junxiao Shic8a0a252015-10-05 07:25:02 -0700139 ]
Davide Pesaventodd0e1952017-06-24 13:37:13 -0400140 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Junxiao Shif7191242015-03-19 05:53:41 -0700141 return flags
142
143class GccFlags(GccBasicFlags):
Junxiao Shif7191242015-03-19 05:53:41 -0700144 def getDebugFlags(self, conf):
145 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100146 version = tuple(int(i) for i in conf.env['CC_VERSION'])
147 if version < (5, 1, 0):
148 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
Davide Pesaventodd0e1952017-06-24 13:37:13 -0400149 flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
Junxiao Shic8a0a252015-10-05 07:25:02 -0700150 return flags
151
152 def getOptimizedFlags(self, conf):
153 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesavento3e79c9c2015-11-01 20:38:28 +0100154 version = tuple(int(i) for i in conf.env['CC_VERSION'])
155 if version < (5, 1, 0):
156 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
Junxiao Shic8a0a252015-10-05 07:25:02 -0700157 flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
Junxiao Shif7191242015-03-19 05:53:41 -0700158 return flags
159
160class ClangFlags(GccBasicFlags):
161 def getGeneralFlags(self, conf):
162 flags = super(ClangFlags, self).getGeneralFlags(conf)
Junxiao Shic8a0a252015-10-05 07:25:02 -0700163 if Utils.unversioned_sys_platform() == 'darwin':
Junxiao Shif7191242015-03-19 05:53:41 -0700164 flags['CXXFLAGS'] += ['-stdlib=libc++']
165 flags['LINKFLAGS'] += ['-stdlib=libc++']
166 return flags
167
168 def getDebugFlags(self, conf):
169 flags = super(ClangFlags, self).getDebugFlags(conf)
Vince Lehman277ecf02016-02-10 16:37:48 -0600170 version = tuple(int(i) for i in conf.env['CC_VERSION'])
171 if Utils.unversioned_sys_platform() == 'darwin' and version < (7, 0, 0):
172 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
Junxiao Shic8a0a252015-10-05 07:25:02 -0700173 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
174 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
175 '-Wno-error=unneeded-internal-declaration', # Bug #1588
176 '-Wno-error=deprecated-register',
177 '-Wno-error=keyword-macro', # Bug #3235
Davide Pesaventodd0e1952017-06-24 13:37:13 -0400178 '-Wno-error=infinite-recursion', # Bug #3358
Junxiao Shic8a0a252015-10-05 07:25:02 -0700179 ]
180 return flags
181
182 def getOptimizedFlags(self, conf):
183 flags = super(ClangFlags, self).getOptimizedFlags(conf)
Vince Lehman277ecf02016-02-10 16:37:48 -0600184 version = tuple(int(i) for i in conf.env['CC_VERSION'])
185 if Utils.unversioned_sys_platform() == 'darwin' and version < (7, 0, 0):
186 flags['CXXFLAGS'] += ['-Wno-missing-field-initializers']
Junxiao Shic8a0a252015-10-05 07:25:02 -0700187 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
188 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
189 ]
Junxiao Shif7191242015-03-19 05:53:41 -0700190 return flags