Alexander Afanasyev | 7eb5911 | 2014-07-02 14:21:11 -0700 | [diff] [blame] | 1 | # -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*- |
Alexander Afanasyev | 7eb5911 | 2014-07-02 14:21:11 -0700 | [diff] [blame] | 2 | |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 3 | from waflib import Logs, Configure, Utils |
Alexander Afanasyev | 7eb5911 | 2014-07-02 14:21:11 -0700 | [diff] [blame] | 4 | |
| 5 | def options(opt): |
| 6 | opt.add_option('--debug', '--with-debug', action='store_true', default=False, dest='debug', |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 7 | help='''Compile in debugging mode without optimizations (-O0 or -Og)''') |
Alexander Afanasyev | 7eb5911 | 2014-07-02 14:21:11 -0700 | [diff] [blame] | 8 | |
| 9 | def configure(conf): |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 10 | cxx = conf.env['CXX_NAME'] # CXX_NAME represents generic name of the compiler |
| 11 | if cxx == 'gcc': |
| 12 | flags = GccFlags() |
| 13 | elif cxx == 'clang': |
| 14 | flags = ClangFlags() |
| 15 | else: |
| 16 | flags = CompilerFlags() |
| 17 | Logs.warn('The code has not yet been tested with %s compiler' % cxx) |
Alexander Afanasyev | 7eb5911 | 2014-07-02 14:21:11 -0700 | [diff] [blame] | 18 | |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 19 | areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0) |
| 20 | |
| 21 | # General flags are always applied (e.g., selecting C++11 mode) |
| 22 | generalFlags = flags.getGeneralFlags(conf) |
| 23 | conf.add_supported_cxxflags(generalFlags['CXXFLAGS']) |
| 24 | conf.add_supported_linkflags(generalFlags['LINKFLAGS']) |
| 25 | conf.env.DEFINES += generalFlags['DEFINES'] |
| 26 | |
| 27 | # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the |
| 28 | # corresponding environment variables are not set. |
| 29 | # DEFINES are always applied. |
Alexander Afanasyev | 7eb5911 | 2014-07-02 14:21:11 -0700 | [diff] [blame] | 30 | if conf.options.debug: |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 31 | extraFlags = flags.getDebugFlags(conf) |
Alexander Afanasyev | 7eb5911 | 2014-07-02 14:21:11 -0700 | [diff] [blame] | 32 | if areCustomCxxflagsPresent: |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 33 | missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS] |
Alexander Afanasyev | 7eb5911 | 2014-07-02 14:21:11 -0700 | [diff] [blame] | 34 | if len(missingFlags) > 0: |
| 35 | Logs.warn("Selected debug mode, but CXXFLAGS is set to a custom value '%s'" |
| 36 | % " ".join(conf.env.CXXFLAGS)) |
| 37 | Logs.warn("Default flags '%s' are not activated" % " ".join(missingFlags)) |
Alexander Afanasyev | 7eb5911 | 2014-07-02 14:21:11 -0700 | [diff] [blame] | 38 | else: |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 39 | extraFlags = flags.getOptimizedFlags(conf) |
Alexander Afanasyev | 7eb5911 | 2014-07-02 14:21:11 -0700 | [diff] [blame] | 40 | |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 41 | if not areCustomCxxflagsPresent: |
| 42 | conf.add_supported_cxxflags(extraFlags['CXXFLAGS']) |
| 43 | conf.add_supported_linkflags(extraFlags['LINKFLAGS']) |
| 44 | |
| 45 | conf.env.DEFINES += extraFlags['DEFINES'] |
Yingdi Yu | 906c2ea | 2014-10-31 11:24:50 -0700 | [diff] [blame] | 46 | |
Alexander Afanasyev | 7eb5911 | 2014-07-02 14:21:11 -0700 | [diff] [blame] | 47 | @Configure.conf |
| 48 | def add_supported_cxxflags(self, cxxflags): |
| 49 | """ |
| 50 | Check which cxxflags are supported by compiler and add them to env.CXXFLAGS variable |
| 51 | """ |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 52 | if len(cxxflags) == 0: |
| 53 | return |
| 54 | |
Yingdi Yu | 906c2ea | 2014-10-31 11:24:50 -0700 | [diff] [blame] | 55 | self.start_msg('Checking supported CXXFLAGS') |
Alexander Afanasyev | 7eb5911 | 2014-07-02 14:21:11 -0700 | [diff] [blame] | 56 | |
| 57 | supportedFlags = [] |
| 58 | for flag in cxxflags: |
| 59 | if self.check_cxx(cxxflags=['-Werror', flag], mandatory=False): |
| 60 | supportedFlags += [flag] |
| 61 | |
| 62 | self.end_msg(' '.join(supportedFlags)) |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 63 | self.env.prepend_value('CXXFLAGS', supportedFlags) |
Yingdi Yu | 906c2ea | 2014-10-31 11:24:50 -0700 | [diff] [blame] | 64 | |
| 65 | @Configure.conf |
| 66 | def add_supported_linkflags(self, linkflags): |
| 67 | """ |
| 68 | Check which linkflags are supported by compiler and add them to env.LINKFLAGS variable |
| 69 | """ |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 70 | if len(linkflags) == 0: |
| 71 | return |
| 72 | |
Yingdi Yu | 906c2ea | 2014-10-31 11:24:50 -0700 | [diff] [blame] | 73 | self.start_msg('Checking supported LINKFLAGS') |
| 74 | |
| 75 | supportedFlags = [] |
| 76 | for flag in linkflags: |
| 77 | if self.check_cxx(linkflags=['-Werror', flag], mandatory=False): |
| 78 | supportedFlags += [flag] |
| 79 | |
| 80 | self.end_msg(' '.join(supportedFlags)) |
Alexander Afanasyev | f3192eb | 2016-12-19 17:11:20 -0800 | [diff] [blame] | 81 | self.env.prepend_value('LINKFLAGS', supportedFlags) |
| 82 | |
| 83 | |
| 84 | class CompilerFlags(object): |
| 85 | def getGeneralFlags(self, conf): |
| 86 | """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed""" |
| 87 | return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []} |
| 88 | |
| 89 | def getDebugFlags(self, conf): |
| 90 | """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in debug mode""" |
| 91 | return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']} |
| 92 | |
| 93 | def getOptimizedFlags(self, conf): |
| 94 | """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are needed only in optimized mode""" |
| 95 | return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']} |
| 96 | |
| 97 | class GccBasicFlags(CompilerFlags): |
| 98 | """ |
| 99 | This class defines basic flags that work for both gcc and clang compilers |
| 100 | """ |
| 101 | def getDebugFlags(self, conf): |
| 102 | flags = super(GccBasicFlags, self).getDebugFlags(conf) |
| 103 | flags['CXXFLAGS'] += ['-O0', |
| 104 | '-g3', |
| 105 | '-pedantic', |
| 106 | '-Wall', |
| 107 | '-Wextra', |
| 108 | '-Werror', |
| 109 | '-Wno-unused-parameter', |
| 110 | '-Wno-error=maybe-uninitialized', # Bug #1615 |
| 111 | '-Wno-error=deprecated-declarations', # Bug #3795 |
| 112 | ] |
| 113 | return flags |
| 114 | |
| 115 | def getOptimizedFlags(self, conf): |
| 116 | flags = super(GccBasicFlags, self).getOptimizedFlags(conf) |
| 117 | flags['CXXFLAGS'] += ['-O2', |
| 118 | '-g', |
| 119 | '-pedantic', |
| 120 | '-Wall', |
| 121 | '-Wextra', |
| 122 | '-Wno-unused-parameter', |
| 123 | ] |
| 124 | return flags |
| 125 | |
| 126 | class GccFlags(GccBasicFlags): |
| 127 | def getGeneralFlags(self, conf): |
| 128 | flags = super(GccFlags, self).getGeneralFlags(conf) |
| 129 | version = tuple(int(i) for i in conf.env['CC_VERSION']) |
| 130 | if version < (4, 8, 2): |
| 131 | conf.fatal('The version of gcc you are using (%s) is too old.\n' % |
| 132 | '.'.join(conf.env['CC_VERSION']) + |
| 133 | 'The minimum supported gcc version is 4.8.2.') |
| 134 | else: |
| 135 | flags['CXXFLAGS'] += ['-std=c++11'] |
| 136 | return flags |
| 137 | |
| 138 | def getDebugFlags(self, conf): |
| 139 | flags = super(GccFlags, self).getDebugFlags(conf) |
| 140 | version = tuple(int(i) for i in conf.env['CC_VERSION']) |
| 141 | if version < (5, 1, 0): |
| 142 | flags['CXXFLAGS'] += ['-Wno-missing-field-initializers'] |
| 143 | flags['CXXFLAGS'] += ['-Og', # gcc >= 4.8 |
| 144 | '-fdiagnostics-color', # gcc >= 4.9 |
| 145 | ] |
| 146 | return flags |
| 147 | |
| 148 | def getOptimizedFlags(self, conf): |
| 149 | flags = super(GccFlags, self).getOptimizedFlags(conf) |
| 150 | version = tuple(int(i) for i in conf.env['CC_VERSION']) |
| 151 | if version < (5, 1, 0): |
| 152 | flags['CXXFLAGS'] += ['-Wno-missing-field-initializers'] |
| 153 | flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9 |
| 154 | return flags |
| 155 | |
| 156 | class ClangFlags(GccBasicFlags): |
| 157 | def getGeneralFlags(self, conf): |
| 158 | flags = super(ClangFlags, self).getGeneralFlags(conf) |
| 159 | flags['CXXFLAGS'] += ['-std=c++11'] |
| 160 | if Utils.unversioned_sys_platform() == 'darwin': |
| 161 | flags['CXXFLAGS'] += ['-stdlib=libc++'] |
| 162 | flags['LINKFLAGS'] += ['-stdlib=libc++'] |
| 163 | return flags |
| 164 | |
| 165 | def getDebugFlags(self, conf): |
| 166 | flags = super(ClangFlags, self).getDebugFlags(conf) |
| 167 | flags['CXXFLAGS'] += ['-fcolor-diagnostics', |
| 168 | '-Wno-unused-local-typedef', # Bugs #2657 and #3209 |
| 169 | '-Wno-error=unneeded-internal-declaration', # Bug #1588 |
| 170 | '-Wno-error=deprecated-register', |
| 171 | '-Wno-error=keyword-macro', # Bug #3235 |
| 172 | '-Wno-error=infinite-recursion', # Bug #3358 |
| 173 | ] |
| 174 | return flags |
| 175 | |
| 176 | def getOptimizedFlags(self, conf): |
| 177 | flags = super(ClangFlags, self).getOptimizedFlags(conf) |
| 178 | flags['CXXFLAGS'] += ['-fcolor-diagnostics', |
| 179 | '-Wno-unused-local-typedef', # Bugs #2657 and #3209 |
| 180 | ] |
| 181 | return flags |