Alexander Afanasyev | 3fd14f0 | 2014-03-26 14:34:39 -0700 | [diff] [blame] | 1 | # -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*- |
| 2 | |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame] | 3 | from waflib import Logs, Configure, Utils |
Alexander Afanasyev | 3fd14f0 | 2014-03-26 14:34:39 -0700 | [diff] [blame] | 4 | |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 5 | def options(opt): |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 6 | opt.add_option('--debug', '--with-debug', action='store_true', default=False, dest='debug', |
Junxiao Shi | e180131 | 2017-07-22 19:16:49 +0000 | [diff] [blame] | 7 | help='''Compile in debugging mode with minimal optimizations (-O0 or -Og)''') |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 8 | |
Alexander Afanasyev | 3fd14f0 | 2014-03-26 14:34:39 -0700 | [diff] [blame] | 9 | def configure(conf): |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame] | 10 | cxx = conf.env['CXX_NAME'] # CXX_NAME represents generic name of the compiler |
Junxiao Shi | e180131 | 2017-07-22 19:16:49 +0000 | [diff] [blame] | 11 | ccver = tuple(int(i) for i in conf.env['CC_VERSION']) |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame] | 12 | if cxx == 'gcc': |
Junxiao Shi | e180131 | 2017-07-22 19:16:49 +0000 | [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'])) |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame] | 17 | flags = GccFlags() |
| 18 | elif cxx == 'clang': |
Junxiao Shi | e180131 | 2017-07-22 19:16:49 +0000 | [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'])) |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame] | 23 | flags = ClangFlags() |
| 24 | else: |
| 25 | flags = CompilerFlags() |
| 26 | Logs.warn('The code has not yet been tested with %s compiler' % cxx) |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 27 | |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame] | 28 | 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 Afanasyev | 3fd14f0 | 2014-03-26 14:34:39 -0700 | [diff] [blame] | 39 | if conf.options.debug: |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame] | 40 | extraFlags = flags.getDebugFlags(conf) |
Alexander Afanasyev | 3fd14f0 | 2014-03-26 14:34:39 -0700 | [diff] [blame] | 41 | if areCustomCxxflagsPresent: |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame] | 42 | missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS] |
Alexander Afanasyev | 3fd14f0 | 2014-03-26 14:34:39 -0700 | [diff] [blame] | 43 | if len(missingFlags) > 0: |
| 44 | Logs.warn("Selected debug mode, but CXXFLAGS is set to a custom value '%s'" |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 45 | % " ".join(conf.env.CXXFLAGS)) |
Alexander Afanasyev | 3fd14f0 | 2014-03-26 14:34:39 -0700 | [diff] [blame] | 46 | Logs.warn("Default flags '%s' are not activated" % " ".join(missingFlags)) |
Alexander Afanasyev | 3fd14f0 | 2014-03-26 14:34:39 -0700 | [diff] [blame] | 47 | else: |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame] | 48 | extraFlags = flags.getOptimizedFlags(conf) |
Alexander Afanasyev | 3fd14f0 | 2014-03-26 14:34:39 -0700 | [diff] [blame] | 49 | |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame] | 50 | 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 Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 55 | |
Alexander Afanasyev | 3fd14f0 | 2014-03-26 14:34:39 -0700 | [diff] [blame] | 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 | """ |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame] | 61 | if len(cxxflags) == 0: |
| 62 | return |
| 63 | |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 64 | self.start_msg('Checking supported CXXFLAGS') |
Alexander Afanasyev | 3fd14f0 | 2014-03-26 14:34:39 -0700 | [diff] [blame] | 65 | |
| 66 | supportedFlags = [] |
| 67 | for flag in cxxflags: |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 68 | if self.check_cxx(cxxflags=['-Werror', flag], mandatory=False): |
Alexander Afanasyev | 3fd14f0 | 2014-03-26 14:34:39 -0700 | [diff] [blame] | 69 | supportedFlags += [flag] |
| 70 | |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 71 | self.end_msg(' '.join(supportedFlags)) |
Junxiao Shi | e180131 | 2017-07-22 19:16:49 +0000 | [diff] [blame] | 72 | self.env.prepend_value('CXXFLAGS', supportedFlags) |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -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 | """ |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame] | 79 | if len(linkflags) == 0: |
| 80 | return |
| 81 | |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 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)) |
Junxiao Shi | e180131 | 2017-07-22 19:16:49 +0000 | [diff] [blame] | 90 | self.env.prepend_value('LINKFLAGS', supportedFlags) |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame] | 91 | |
| 92 | |
| 93 | class 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 | |
| 106 | class GccBasicFlags(CompilerFlags): |
| 107 | """ |
| 108 | This class defines basic flags that work for both gcc and clang compilers |
| 109 | """ |
Junxiao Shi | e180131 | 2017-07-22 19:16:49 +0000 | [diff] [blame] | 110 | def getGeneralFlags(self, conf): |
| 111 | flags = super(GccBasicFlags, self).getGeneralFlags(conf) |
| 112 | flags['CXXFLAGS'] += ['-std=c++11'] |
| 113 | return flags |
| 114 | |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame] | 115 | def getDebugFlags(self, conf): |
| 116 | flags = super(GccBasicFlags, self).getDebugFlags(conf) |
| 117 | flags['CXXFLAGS'] += ['-O0', |
Junxiao Shi | e180131 | 2017-07-22 19:16:49 +0000 | [diff] [blame] | 118 | '-Og', # gcc >= 4.8, clang >= 4.0 |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame] | 119 | '-g3', |
| 120 | '-pedantic', |
| 121 | '-Wall', |
| 122 | '-Wextra', |
| 123 | '-Werror', |
Junxiao Shi | e180131 | 2017-07-22 19:16:49 +0000 | [diff] [blame] | 124 | '-Wnon-virtual-dtor', |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame] | 125 | '-Wno-unused-parameter', |
| 126 | '-Wno-error=maybe-uninitialized', # Bug #1615 |
Junxiao Shi | e180131 | 2017-07-22 19:16:49 +0000 | [diff] [blame] | 127 | '-Wno-error=deprecated-declarations', # Bug #3795 |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame] | 128 | ] |
Junxiao Shi | e180131 | 2017-07-22 19:16:49 +0000 | [diff] [blame] | 129 | flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1'] |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame] | 130 | 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 Shi | e180131 | 2017-07-22 19:16:49 +0000 | [diff] [blame] | 139 | '-Wnon-virtual-dtor', |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame] | 140 | '-Wno-unused-parameter', |
| 141 | ] |
Junxiao Shi | e180131 | 2017-07-22 19:16:49 +0000 | [diff] [blame] | 142 | flags['LINKFLAGS'] += ['-fuse-ld=gold', '-Wl,-O1'] |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame] | 143 | return flags |
| 144 | |
| 145 | class GccFlags(GccBasicFlags): |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame] | 146 | 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 Shi | e180131 | 2017-07-22 19:16:49 +0000 | [diff] [blame] | 151 | flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9 |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame] | 152 | 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 | |
| 162 | class ClangFlags(GccBasicFlags): |
| 163 | def getGeneralFlags(self, conf): |
| 164 | flags = super(ClangFlags, self).getGeneralFlags(conf) |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame] | 165 | 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 Shi | e180131 | 2017-07-22 19:16:49 +0000 | [diff] [blame] | 177 | '-Wno-error=infinite-recursion', # Bug #3358 |
Alexander Afanasyev | d352cce | 2015-11-20 14:15:11 -0500 | [diff] [blame] | 178 | ] |
| 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 |