Alexander Afanasyev | 97e4cac | 2014-03-28 10:55:11 -0700 | [diff] [blame] | 1 | # -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*- |
Alexander Afanasyev | 97e4cac | 2014-03-28 10:55:11 -0700 | [diff] [blame] | 2 | |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 3 | from waflib import Logs, Configure, Utils |
Alexander Afanasyev | 97e4cac | 2014-03-28 10:55:11 -0700 | [diff] [blame] | 4 | |
| 5 | def options(opt): |
| 6 | opt.add_option('--debug', '--with-debug', action='store_true', default=False, dest='debug', |
Davide Pesavento | ab1e8f2 | 2014-10-21 22:45:33 +0200 | [diff] [blame] | 7 | help='''Compile in debugging mode without optimizations (-O0 or -Og)''') |
Alexander Afanasyev | 97e4cac | 2014-03-28 10:55:11 -0700 | [diff] [blame] | 8 | |
| 9 | def configure(conf): |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -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() |
Davide Pesavento | f485d89 | 2015-10-02 02:00:54 +0200 | [diff] [blame^] | 17 | Logs.warn('The code has not yet been tested with %s compiler' % cxx) |
Davide Pesavento | 1bdef28 | 2014-04-08 20:59:50 +0200 | [diff] [blame] | 18 | |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 19 | areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0) |
| 20 | |
Davide Pesavento | f485d89 | 2015-10-02 02:00:54 +0200 | [diff] [blame^] | 21 | # General flags are always applied (e.g., selecting C++11 mode) |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 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 | |
Davide Pesavento | f485d89 | 2015-10-02 02:00:54 +0200 | [diff] [blame^] | 27 | # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 28 | # corresponding environment variables are not set. |
Davide Pesavento | f485d89 | 2015-10-02 02:00:54 +0200 | [diff] [blame^] | 29 | # DEFINES are always applied. |
Alexander Afanasyev | 97e4cac | 2014-03-28 10:55:11 -0700 | [diff] [blame] | 30 | if conf.options.debug: |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 31 | extraFlags = flags.getDebugFlags(conf) |
Alexander Afanasyev | 97e4cac | 2014-03-28 10:55:11 -0700 | [diff] [blame] | 32 | if areCustomCxxflagsPresent: |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 33 | missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS] |
Alexander Afanasyev | 97e4cac | 2014-03-28 10:55: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 | 97e4cac | 2014-03-28 10:55:11 -0700 | [diff] [blame] | 38 | else: |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 39 | extraFlags = flags.getOptimizedFlags(conf) |
Alexander Afanasyev | 97e4cac | 2014-03-28 10:55:11 -0700 | [diff] [blame] | 40 | |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 41 | if not areCustomCxxflagsPresent: |
| 42 | conf.add_supported_cxxflags(extraFlags['CXXFLAGS']) |
| 43 | conf.add_supported_cxxflags(extraFlags['LINKFLAGS']) |
| 44 | |
| 45 | conf.env.DEFINES += extraFlags['DEFINES'] |
Davide Pesavento | ab1e8f2 | 2014-10-21 22:45:33 +0200 | [diff] [blame] | 46 | |
Alexander Afanasyev | 97e4cac | 2014-03-28 10:55: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 | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 52 | if len(cxxflags) == 0: |
| 53 | return |
| 54 | |
Davide Pesavento | ab1e8f2 | 2014-10-21 22:45:33 +0200 | [diff] [blame] | 55 | self.start_msg('Checking supported CXXFLAGS') |
Alexander Afanasyev | 97e4cac | 2014-03-28 10:55:11 -0700 | [diff] [blame] | 56 | |
| 57 | supportedFlags = [] |
| 58 | for flag in cxxflags: |
Alexander Afanasyev | d35176c | 2014-04-25 19:09:41 -0700 | [diff] [blame] | 59 | if self.check_cxx(cxxflags=['-Werror', flag], mandatory=False): |
Alexander Afanasyev | 97e4cac | 2014-03-28 10:55:11 -0700 | [diff] [blame] | 60 | supportedFlags += [flag] |
| 61 | |
Davide Pesavento | 1bdef28 | 2014-04-08 20:59:50 +0200 | [diff] [blame] | 62 | self.end_msg(' '.join(supportedFlags)) |
Alexander Afanasyev | 97e4cac | 2014-03-28 10:55:11 -0700 | [diff] [blame] | 63 | self.env.CXXFLAGS = supportedFlags + self.env.CXXFLAGS |
Davide Pesavento | ab1e8f2 | 2014-10-21 22:45:33 +0200 | [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 | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 70 | if len(linkflags) == 0: |
| 71 | return |
| 72 | |
Davide Pesavento | ab1e8f2 | 2014-10-21 22:45:33 +0200 | [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)) |
| 81 | self.env.LINKFLAGS = supportedFlags + self.env.LINKFLAGS |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 82 | |
| 83 | |
| 84 | class CompilerFlags(object): |
| 85 | def getGeneralFlags(self, conf): |
| 86 | """Get dict {'CXXFLAGS':[...], LINKFLAGS:[...], DEFINES:[...]} that are always needed""" |
| 87 | return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []} |
| 88 | |
| 89 | def getDebugFlags(self, conf): |
| 90 | """Get tuple {CXXFLAGS, LINKFLAGS, DEFINES} that are needed in debug mode""" |
| 91 | return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']} |
| 92 | |
| 93 | def getOptimizedFlags(self, conf): |
| 94 | """Get tuple {CXXFLAGS, LINKFLAGS, DEFINES} that are needed in optimized mode""" |
| 95 | return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']} |
| 96 | |
| 97 | class GccBasicFlags(CompilerFlags): |
Davide Pesavento | f485d89 | 2015-10-02 02:00:54 +0200 | [diff] [blame^] | 98 | """ |
| 99 | This class defines basic flags that work for both gcc and clang compilers |
| 100 | """ |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 101 | def getDebugFlags(self, conf): |
| 102 | flags = super(GccBasicFlags, self).getDebugFlags(conf) |
Davide Pesavento | f485d89 | 2015-10-02 02:00:54 +0200 | [diff] [blame^] | 103 | flags['CXXFLAGS'] += ['-pedantic', |
| 104 | '-Wall', |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 105 | '-O0', |
| 106 | '-g3', |
| 107 | '-Werror', |
| 108 | '-Wno-error=maybe-uninitialized', # Bug #1615 |
Davide Pesavento | f485d89 | 2015-10-02 02:00:54 +0200 | [diff] [blame^] | 109 | ] |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 110 | return flags |
| 111 | |
| 112 | def getOptimizedFlags(self, conf): |
| 113 | flags = super(GccBasicFlags, self).getOptimizedFlags(conf) |
Davide Pesavento | f485d89 | 2015-10-02 02:00:54 +0200 | [diff] [blame^] | 114 | flags['CXXFLAGS'] += ['-pedantic', |
| 115 | '-Wall', |
| 116 | '-O2', |
| 117 | '-g', |
| 118 | ] |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 119 | return flags |
| 120 | |
| 121 | class GccFlags(GccBasicFlags): |
| 122 | def getGeneralFlags(self, conf): |
| 123 | flags = super(GccFlags, self).getGeneralFlags(conf) |
| 124 | version = tuple(int(i) for i in conf.env['CC_VERSION']) |
| 125 | if version < (4, 6, 0): |
| 126 | conf.fatal('The version of gcc you are using (%s) is too old.\n' % |
| 127 | '.'.join(conf.env['CC_VERSION']) + |
| 128 | 'The minimum supported gcc version is 4.6.0.') |
| 129 | elif version < (4, 7, 0): |
| 130 | flags['CXXFLAGS'] += ['-std=c++0x'] |
| 131 | else: |
| 132 | flags['CXXFLAGS'] += ['-std=c++11'] |
| 133 | return flags |
| 134 | |
| 135 | def getDebugFlags(self, conf): |
| 136 | flags = super(GccFlags, self).getDebugFlags(conf) |
| 137 | flags['CXXFLAGS'] += ['-Og', # gcc >= 4.8 |
| 138 | '-fdiagnostics-color', # gcc >= 4.9 |
Davide Pesavento | f485d89 | 2015-10-02 02:00:54 +0200 | [diff] [blame^] | 139 | ] |
| 140 | return flags |
| 141 | |
| 142 | def getOptimizedFlags(self, conf): |
| 143 | flags = super(GccFlags, self).getOptimizedFlags(conf) |
| 144 | flags['CXXFLAGS'] += ['-fdiagnostics-color'] # gcc >= 4.9 |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 145 | return flags |
| 146 | |
| 147 | class ClangFlags(GccBasicFlags): |
| 148 | def getGeneralFlags(self, conf): |
| 149 | flags = super(ClangFlags, self).getGeneralFlags(conf) |
Davide Pesavento | f485d89 | 2015-10-02 02:00:54 +0200 | [diff] [blame^] | 150 | flags['CXXFLAGS'] += ['-std=c++11'] |
| 151 | if Utils.unversioned_sys_platform() == 'darwin': |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 152 | flags['CXXFLAGS'] += ['-stdlib=libc++'] |
| 153 | flags['LINKFLAGS'] += ['-stdlib=libc++'] |
| 154 | return flags |
| 155 | |
| 156 | def getDebugFlags(self, conf): |
| 157 | flags = super(ClangFlags, self).getDebugFlags(conf) |
Davide Pesavento | f485d89 | 2015-10-02 02:00:54 +0200 | [diff] [blame^] | 158 | flags['CXXFLAGS'] += ['-fcolor-diagnostics', |
| 159 | '-Wno-unused-local-typedef', # Bugs #2657 and #3209 |
| 160 | '-Wno-error=unneeded-internal-declaration', # Bug #1588 |
| 161 | '-Wno-error=deprecated-register', |
| 162 | ] |
| 163 | return flags |
| 164 | |
| 165 | def getOptimizedFlags(self, conf): |
| 166 | flags = super(ClangFlags, self).getOptimizedFlags(conf) |
| 167 | flags['CXXFLAGS'] += ['-fcolor-diagnostics', |
| 168 | '-Wno-unused-local-typedef', # Bugs #2657 and #3209 |
| 169 | ] |
Alexander Afanasyev | 7681863 | 2015-01-26 21:30:45 -0800 | [diff] [blame] | 170 | return flags |