blob: 28340ed8670b54fcd0173cf1e3cf98d22e0579f0 [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 Pesavento8eb5f8f2017-06-24 02:19:42 -04007 help='''Compile in debugging mode with minimal 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
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -040011 ccver = tuple(int(i) for i in conf.env['CC_VERSION'])
Alexander Afanasyev76818632015-01-26 21:30:45 -080012 if cxx == 'gcc':
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -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']))
Alexander Afanasyev76818632015-01-26 21:30:45 -080017 flags = GccFlags()
18 elif cxx == 'clang':
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -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']))
Alexander Afanasyev76818632015-01-26 21:30:45 -080023 flags = ClangFlags()
24 else:
25 flags = CompilerFlags()
Davide Pesaventof485d892015-10-02 02:00:54 +020026 Logs.warn('The code has not yet been tested with %s compiler' % cxx)
Davide Pesavento1bdef282014-04-08 20:59:50 +020027
Alexander Afanasyev76818632015-01-26 21:30:45 -080028 areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
29
Davide Pesaventof485d892015-10-02 02:00:54 +020030 # General flags are always applied (e.g., selecting C++11 mode)
Alexander Afanasyev76818632015-01-26 21:30:45 -080031 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
Davide Pesaventof485d892015-10-02 02:00:54 +020036 # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
Alexander Afanasyev76818632015-01-26 21:30:45 -080037 # corresponding environment variables are not set.
Davide Pesaventof485d892015-10-02 02:00:54 +020038 # DEFINES are always applied.
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070039 if conf.options.debug:
Alexander Afanasyev76818632015-01-26 21:30:45 -080040 extraFlags = flags.getDebugFlags(conf)
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070041 if areCustomCxxflagsPresent:
Alexander Afanasyev76818632015-01-26 21:30:45 -080042 missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070043 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))
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070047 else:
Alexander Afanasyev76818632015-01-26 21:30:45 -080048 extraFlags = flags.getOptimizedFlags(conf)
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070049
Alexander Afanasyev76818632015-01-26 21:30:45 -080050 if not areCustomCxxflagsPresent:
51 conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
Davide Pesaventoed89ccc2015-11-01 20:35:04 +010052 conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
Alexander Afanasyev76818632015-01-26 21:30:45 -080053
54 conf.env.DEFINES += extraFlags['DEFINES']
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020055
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -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 Afanasyev76818632015-01-26 21:30:45 -080061 if len(cxxflags) == 0:
62 return
63
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020064 self.start_msg('Checking supported CXXFLAGS')
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070065
66 supportedFlags = []
67 for flag in cxxflags:
Alexander Afanasyevd35176c2014-04-25 19:09:41 -070068 if self.check_cxx(cxxflags=['-Werror', flag], mandatory=False):
Alexander Afanasyev97e4cac2014-03-28 10:55:11 -070069 supportedFlags += [flag]
70
Davide Pesavento1bdef282014-04-08 20:59:50 +020071 self.end_msg(' '.join(supportedFlags))
Davide Pesavento67f30272016-08-10 01:55:16 +000072 self.env.prepend_value('CXXFLAGS', supportedFlags)
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020073
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 Afanasyev76818632015-01-26 21:30:45 -080079 if len(linkflags) == 0:
80 return
81
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020082 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 Pesavento67f30272016-08-10 01:55:16 +000090 self.env.prepend_value('LINKFLAGS', supportedFlags)
Alexander Afanasyev76818632015-01-26 21:30:45 -080091
92
93class CompilerFlags(object):
94 def getGeneralFlags(self, conf):
Davide Pesaventoed89ccc2015-11-01 20:35:04 +010095 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed"""
Alexander Afanasyev76818632015-01-26 21:30:45 -080096 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []}
97
98 def getDebugFlags(self, conf):
Davide Pesaventoed89ccc2015-11-01 20:35:04 +010099 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode"""
Alexander Afanasyev76818632015-01-26 21:30:45 -0800100 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']}
101
102 def getOptimizedFlags(self, conf):
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100103 """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode"""
Alexander Afanasyev76818632015-01-26 21:30:45 -0800104 return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']}
105
106class GccBasicFlags(CompilerFlags):
Davide Pesaventof485d892015-10-02 02:00:54 +0200107 """
108 This class defines basic flags that work for both gcc and clang compilers
109 """
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -0400110 def getGeneralFlags(self, conf):
111 flags = super(GccBasicFlags, self).getGeneralFlags(conf)
112 flags['CXXFLAGS'] += ['-std=c++11']
113 return flags
114
Alexander Afanasyev76818632015-01-26 21:30:45 -0800115 def getDebugFlags(self, conf):
116 flags = super(GccBasicFlags, self).getDebugFlags(conf)
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100117 flags['CXXFLAGS'] += ['-O0',
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -0400118 '-Og', # gcc >= 4.8, clang >= 4.0
Alexander Afanasyev76818632015-01-26 21:30:45 -0800119 '-g3',
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100120 '-pedantic',
121 '-Wall',
122 '-Wextra',
Alexander Afanasyev76818632015-01-26 21:30:45 -0800123 '-Werror',
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100124 '-Wno-unused-parameter',
Alexander Afanasyev76818632015-01-26 21:30:45 -0800125 '-Wno-error=maybe-uninitialized', # Bug #1615
Alexander Afanasyev436f3bd2016-10-11 16:37:45 -0700126 '-Wno-error=deprecated-declarations', # Bug #3795
Davide Pesaventof485d892015-10-02 02:00:54 +0200127 ]
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -0400128 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Alexander Afanasyev76818632015-01-26 21:30:45 -0800129 return flags
130
131 def getOptimizedFlags(self, conf):
132 flags = super(GccBasicFlags, self).getOptimizedFlags(conf)
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100133 flags['CXXFLAGS'] += ['-O2',
Davide Pesaventof485d892015-10-02 02:00:54 +0200134 '-g',
Davide Pesaventoed89ccc2015-11-01 20:35:04 +0100135 '-pedantic',
136 '-Wall',
137 '-Wextra',
138 '-Wno-unused-parameter',
Davide Pesaventof485d892015-10-02 02:00:54 +0200139 ]
Davide Pesavento8eb5f8f2017-06-24 02:19:42 -0400140 flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1']
Alexander Afanasyev76818632015-01-26 21:30:45 -0800141 return flags
142
143class GccFlags(GccBasicFlags):
Alexander Afanasyev76818632015-01-26 21:30:45 -0800144 def getDebugFlags(self, conf):
145 flags = super(GccFlags, self).getDebugFlags(conf)
Davide Pesaventoed89ccc2015-11-01 20:35:04 +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 Pesavento8eb5f8f2017-06-24 02:19:42 -0400149 flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
Davide Pesaventof485d892015-10-02 02:00:54 +0200150 return flags
151
152 def getOptimizedFlags(self, conf):
153 flags = super(GccFlags, self).getOptimizedFlags(conf)
Davide Pesaventoed89ccc2015-11-01 20:35:04 +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']
Davide Pesaventof485d892015-10-02 02:00:54 +0200157 flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9
Alexander Afanasyev76818632015-01-26 21:30:45 -0800158 return flags
159
160class ClangFlags(GccBasicFlags):
161 def getGeneralFlags(self, conf):
162 flags = super(ClangFlags, self).getGeneralFlags(conf)
Davide Pesaventof485d892015-10-02 02:00:54 +0200163 if Utils.unversioned_sys_platform() == 'darwin':
Alexander Afanasyev76818632015-01-26 21:30:45 -0800164 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)
Davide Pesaventof485d892015-10-02 02:00:54 +0200170 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
171 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
172 '-Wno-error=unneeded-internal-declaration', # Bug #1588
173 '-Wno-error=deprecated-register',
Junxiao Shi747b67d2015-10-04 16:59:41 -0700174 '-Wno-error=keyword-macro', # Bug #3235
Alexander Afanasyev0fc97482016-09-13 18:33:15 +0000175 '-Wno-error=infinite-recursion', # Bug #3358
Davide Pesaventof485d892015-10-02 02:00:54 +0200176 ]
177 return flags
178
179 def getOptimizedFlags(self, conf):
180 flags = super(ClangFlags, self).getOptimizedFlags(conf)
181 flags['CXXFLAGS'] += ['-fcolor-diagnostics',
182 '-Wno-unused-local-typedef', # Bugs #2657 and #3209
183 ]
Alexander Afanasyev76818632015-01-26 21:30:45 -0800184 return flags