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