Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 1 | # -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*- |
| 2 | |
| 3 | from waflib import Logs, Configure, Utils |
| 4 | |
| 5 | def options(opt): |
| 6 | opt.add_option('--debug', '--with-debug', action='store_true', default=False, dest='debug', |
Davide Pesavento | dd0e195 | 2017-06-24 13:37:13 -0400 | [diff] [blame^] | 7 | help='''Compile in debugging mode with minimal optimizations (-O0 or -Og)''') |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 8 | |
| 9 | def configure(conf): |
| 10 | cxx = conf.env['CXX_NAME'] # CXX_NAME represents generic name of the compiler |
Davide Pesavento | dd0e195 | 2017-06-24 13:37:13 -0400 | [diff] [blame^] | 11 | ccver = tuple(int(i) for i in conf.env['CC_VERSION']) |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 12 | if cxx == 'gcc': |
Davide Pesavento | dd0e195 | 2017-06-24 13:37:13 -0400 | [diff] [blame^] | 13 | 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 Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 17 | flags = GccFlags() |
| 18 | elif cxx == 'clang': |
Davide Pesavento | dd0e195 | 2017-06-24 13:37:13 -0400 | [diff] [blame^] | 19 | 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 Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 23 | flags = ClangFlags() |
| 24 | else: |
| 25 | flags = CompilerFlags() |
Junxiao Shi | c8a0a25 | 2015-10-05 07:25:02 -0700 | [diff] [blame] | 26 | Logs.warn('The code has not yet been tested with %s compiler' % cxx) |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 27 | |
| 28 | areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0) |
| 29 | |
Junxiao Shi | c8a0a25 | 2015-10-05 07:25:02 -0700 | [diff] [blame] | 30 | # General flags are always applied (e.g., selecting C++11 mode) |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 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 | |
Junxiao Shi | c8a0a25 | 2015-10-05 07:25:02 -0700 | [diff] [blame] | 36 | # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 37 | # corresponding environment variables are not set. |
Junxiao Shi | c8a0a25 | 2015-10-05 07:25:02 -0700 | [diff] [blame] | 38 | # DEFINES are always applied. |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 39 | if conf.options.debug: |
| 40 | extraFlags = flags.getDebugFlags(conf) |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 41 | 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 Pesavento | 3e79c9c | 2015-11-01 20:38:28 +0100 | [diff] [blame] | 52 | conf.add_supported_linkflags(extraFlags['LINKFLAGS']) |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 53 | |
| 54 | conf.env.DEFINES += extraFlags['DEFINES'] |
| 55 | |
| 56 | @Configure.conf |
| 57 | def 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 Pesavento | 89d9175 | 2016-08-14 11:34:09 +0200 | [diff] [blame] | 72 | self.env.prepend_value('CXXFLAGS', supportedFlags) |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 73 | |
| 74 | @Configure.conf |
| 75 | def 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 Pesavento | 89d9175 | 2016-08-14 11:34:09 +0200 | [diff] [blame] | 90 | self.env.prepend_value('LINKFLAGS', supportedFlags) |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 91 | |
| 92 | |
| 93 | class CompilerFlags(object): |
| 94 | def getGeneralFlags(self, conf): |
Davide Pesavento | 3e79c9c | 2015-11-01 20:38:28 +0100 | [diff] [blame] | 95 | """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed""" |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 96 | return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []} |
| 97 | |
| 98 | def getDebugFlags(self, conf): |
Davide Pesavento | 3e79c9c | 2015-11-01 20:38:28 +0100 | [diff] [blame] | 99 | """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode""" |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 100 | return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']} |
| 101 | |
| 102 | def getOptimizedFlags(self, conf): |
Davide Pesavento | 3e79c9c | 2015-11-01 20:38:28 +0100 | [diff] [blame] | 103 | """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode""" |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 104 | return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']} |
| 105 | |
| 106 | class GccBasicFlags(CompilerFlags): |
Junxiao Shi | c8a0a25 | 2015-10-05 07:25:02 -0700 | [diff] [blame] | 107 | """ |
| 108 | This class defines basic flags that work for both gcc and clang compilers |
| 109 | """ |
Davide Pesavento | dd0e195 | 2017-06-24 13:37:13 -0400 | [diff] [blame^] | 110 | def getGeneralFlags(self, conf): |
| 111 | flags = super(GccBasicFlags, self).getGeneralFlags(conf) |
| 112 | flags['CXXFLAGS'] += ['-std=c++11'] |
| 113 | return flags |
| 114 | |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 115 | def getDebugFlags(self, conf): |
| 116 | flags = super(GccBasicFlags, self).getDebugFlags(conf) |
Davide Pesavento | 3e79c9c | 2015-11-01 20:38:28 +0100 | [diff] [blame] | 117 | flags['CXXFLAGS'] += ['-O0', |
Davide Pesavento | dd0e195 | 2017-06-24 13:37:13 -0400 | [diff] [blame^] | 118 | '-Og', # gcc >= 4.8, clang >= 4.0 |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 119 | '-g3', |
Davide Pesavento | 3e79c9c | 2015-11-01 20:38:28 +0100 | [diff] [blame] | 120 | '-pedantic', |
| 121 | '-Wall', |
| 122 | '-Wextra', |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 123 | '-Werror', |
Davide Pesavento | 3e79c9c | 2015-11-01 20:38:28 +0100 | [diff] [blame] | 124 | '-Wno-unused-parameter', |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 125 | '-Wno-error=maybe-uninitialized', # Bug #1615 |
Alexander Afanasyev | fc4aa0d | 2016-10-15 00:57:33 +0000 | [diff] [blame] | 126 | '-Wno-error=deprecated-declarations', # Bug #3795 |
Junxiao Shi | c8a0a25 | 2015-10-05 07:25:02 -0700 | [diff] [blame] | 127 | ] |
Davide Pesavento | dd0e195 | 2017-06-24 13:37:13 -0400 | [diff] [blame^] | 128 | flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1'] |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 129 | return flags |
| 130 | |
| 131 | def getOptimizedFlags(self, conf): |
| 132 | flags = super(GccBasicFlags, self).getOptimizedFlags(conf) |
Davide Pesavento | 3e79c9c | 2015-11-01 20:38:28 +0100 | [diff] [blame] | 133 | flags['CXXFLAGS'] += ['-O2', |
Junxiao Shi | c8a0a25 | 2015-10-05 07:25:02 -0700 | [diff] [blame] | 134 | '-g', |
Davide Pesavento | 3e79c9c | 2015-11-01 20:38:28 +0100 | [diff] [blame] | 135 | '-pedantic', |
| 136 | '-Wall', |
| 137 | '-Wextra', |
| 138 | '-Wno-unused-parameter', |
Junxiao Shi | c8a0a25 | 2015-10-05 07:25:02 -0700 | [diff] [blame] | 139 | ] |
Davide Pesavento | dd0e195 | 2017-06-24 13:37:13 -0400 | [diff] [blame^] | 140 | flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1'] |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 141 | return flags |
| 142 | |
| 143 | class GccFlags(GccBasicFlags): |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 144 | def getDebugFlags(self, conf): |
| 145 | flags = super(GccFlags, self).getDebugFlags(conf) |
Davide Pesavento | 3e79c9c | 2015-11-01 20:38:28 +0100 | [diff] [blame] | 146 | 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 Pesavento | dd0e195 | 2017-06-24 13:37:13 -0400 | [diff] [blame^] | 149 | flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9 |
Junxiao Shi | c8a0a25 | 2015-10-05 07:25:02 -0700 | [diff] [blame] | 150 | return flags |
| 151 | |
| 152 | def getOptimizedFlags(self, conf): |
| 153 | flags = super(GccFlags, self).getOptimizedFlags(conf) |
Davide Pesavento | 3e79c9c | 2015-11-01 20:38:28 +0100 | [diff] [blame] | 154 | 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 Shi | c8a0a25 | 2015-10-05 07:25:02 -0700 | [diff] [blame] | 157 | flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9 |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 158 | return flags |
| 159 | |
| 160 | class ClangFlags(GccBasicFlags): |
| 161 | def getGeneralFlags(self, conf): |
| 162 | flags = super(ClangFlags, self).getGeneralFlags(conf) |
Junxiao Shi | c8a0a25 | 2015-10-05 07:25:02 -0700 | [diff] [blame] | 163 | if Utils.unversioned_sys_platform() == 'darwin': |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 164 | 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 Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 170 | 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 Shi | c8a0a25 | 2015-10-05 07:25:02 -0700 | [diff] [blame] | 173 | 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 Pesavento | dd0e195 | 2017-06-24 13:37:13 -0400 | [diff] [blame^] | 178 | '-Wno-error=infinite-recursion', # Bug #3358 |
Junxiao Shi | c8a0a25 | 2015-10-05 07:25:02 -0700 | [diff] [blame] | 179 | ] |
| 180 | return flags |
| 181 | |
| 182 | def getOptimizedFlags(self, conf): |
| 183 | flags = super(ClangFlags, self).getOptimizedFlags(conf) |
Vince Lehman | 277ecf0 | 2016-02-10 16:37:48 -0600 | [diff] [blame] | 184 | 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 Shi | c8a0a25 | 2015-10-05 07:25:02 -0700 | [diff] [blame] | 187 | flags['CXXFLAGS'] += ['-fcolor-diagnostics', |
| 188 | '-Wno-unused-local-typedef', # Bugs #2657 and #3209 |
| 189 | ] |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 190 | return flags |