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 | |
Alexander Afanasyev | 20c85cb | 2018-03-09 17:50:14 -0500 | [diff] [blame] | 3 | from waflib import Configure, Logs, Utils |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 4 | |
| 5 | def options(opt): |
Alexander Afanasyev | 20c85cb | 2018-03-09 17:50:14 -0500 | [diff] [blame] | 6 | opt.add_option('--debug', '--with-debug', action='store_true', default=False, |
| 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): |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 10 | conf.start_msg('Checking C++ compiler version') |
| 11 | |
Alexander Afanasyev | 20c85cb | 2018-03-09 17:50:14 -0500 | [diff] [blame] | 12 | cxx = conf.env.CXX_NAME # generic name of the compiler |
| 13 | ccver = tuple(int(i) for i in conf.env.CC_VERSION) |
| 14 | ccverstr = '.'.join(conf.env.CC_VERSION) |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 15 | errmsg = '' |
| 16 | warnmsg = '' |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 17 | if cxx == 'gcc': |
Davide Pesavento | 26ea1ac | 2018-05-10 20:20:03 -0400 | [diff] [blame] | 18 | if ccver < (5, 3, 0): |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 19 | errmsg = ('The version of gcc you are using is too old.\n' |
Davide Pesavento | 26ea1ac | 2018-05-10 20:20:03 -0400 | [diff] [blame] | 20 | 'The minimum supported gcc version is 5.3.0.') |
Alexander Afanasyev | 11e74eb | 2017-09-21 19:01:54 -0400 | [diff] [blame] | 21 | conf.flags = GccFlags() |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 22 | elif cxx == 'clang': |
Davide Pesavento | 302941e | 2018-07-21 18:55:56 -0400 | [diff] [blame] | 23 | if ccver < (3, 6, 0): |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 24 | errmsg = ('The version of clang you are using is too old.\n' |
Davide Pesavento | 302941e | 2018-07-21 18:55:56 -0400 | [diff] [blame] | 25 | 'The minimum supported clang version is 3.6.0.') |
Alexander Afanasyev | 11e74eb | 2017-09-21 19:01:54 -0400 | [diff] [blame] | 26 | conf.flags = ClangFlags() |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 27 | else: |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 28 | warnmsg = 'Note: %s compiler is unsupported' % cxx |
Alexander Afanasyev | 11e74eb | 2017-09-21 19:01:54 -0400 | [diff] [blame] | 29 | conf.flags = CompilerFlags() |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 30 | |
| 31 | if errmsg: |
Alexander Afanasyev | 20c85cb | 2018-03-09 17:50:14 -0500 | [diff] [blame] | 32 | conf.end_msg(ccverstr, color='RED') |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 33 | conf.fatal(errmsg) |
| 34 | elif warnmsg: |
Alexander Afanasyev | 20c85cb | 2018-03-09 17:50:14 -0500 | [diff] [blame] | 35 | conf.end_msg(ccverstr, color='YELLOW') |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 36 | Logs.warn(warnmsg) |
| 37 | else: |
Alexander Afanasyev | 20c85cb | 2018-03-09 17:50:14 -0500 | [diff] [blame] | 38 | conf.end_msg(ccverstr) |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 39 | |
Alexander Afanasyev | 11e74eb | 2017-09-21 19:01:54 -0400 | [diff] [blame] | 40 | conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0) |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 41 | |
Davide Pesavento | 60f8cc1 | 2018-05-10 22:05:21 -0400 | [diff] [blame] | 42 | # General flags are always applied (e.g., selecting C++ language standard) |
Alexander Afanasyev | 11e74eb | 2017-09-21 19:01:54 -0400 | [diff] [blame] | 43 | generalFlags = conf.flags.getGeneralFlags(conf) |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 44 | conf.add_supported_cxxflags(generalFlags['CXXFLAGS']) |
| 45 | conf.add_supported_linkflags(generalFlags['LINKFLAGS']) |
| 46 | conf.env.DEFINES += generalFlags['DEFINES'] |
| 47 | |
Alexander Afanasyev | 11e74eb | 2017-09-21 19:01:54 -0400 | [diff] [blame] | 48 | @Configure.conf |
| 49 | def check_compiler_flags(conf): |
Junxiao Shi | c8a0a25 | 2015-10-05 07:25:02 -0700 | [diff] [blame] | 50 | # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 51 | # corresponding environment variables are not set. |
Junxiao Shi | c8a0a25 | 2015-10-05 07:25:02 -0700 | [diff] [blame] | 52 | # DEFINES are always applied. |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 53 | if conf.options.debug: |
Alexander Afanasyev | 11e74eb | 2017-09-21 19:01:54 -0400 | [diff] [blame] | 54 | extraFlags = conf.flags.getDebugFlags(conf) |
| 55 | if conf.areCustomCxxflagsPresent: |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 56 | missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS] |
Alexander Afanasyev | 20c85cb | 2018-03-09 17:50:14 -0500 | [diff] [blame] | 57 | if missingFlags: |
| 58 | Logs.warn('Selected debug mode, but CXXFLAGS is set to a custom value "%s"' |
| 59 | % ' '.join(conf.env.CXXFLAGS)) |
| 60 | Logs.warn('Default flags "%s" will not be used' % ' '.join(missingFlags)) |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 61 | else: |
Alexander Afanasyev | 11e74eb | 2017-09-21 19:01:54 -0400 | [diff] [blame] | 62 | extraFlags = conf.flags.getOptimizedFlags(conf) |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 63 | |
Alexander Afanasyev | 11e74eb | 2017-09-21 19:01:54 -0400 | [diff] [blame] | 64 | if not conf.areCustomCxxflagsPresent: |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 65 | conf.add_supported_cxxflags(extraFlags['CXXFLAGS']) |
Davide Pesavento | 3e79c9c | 2015-11-01 20:38:28 +0100 | [diff] [blame] | 66 | conf.add_supported_linkflags(extraFlags['LINKFLAGS']) |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 67 | |
| 68 | conf.env.DEFINES += extraFlags['DEFINES'] |
| 69 | |
| 70 | @Configure.conf |
| 71 | def add_supported_cxxflags(self, cxxflags): |
| 72 | """ |
| 73 | Check which cxxflags are supported by compiler and add them to env.CXXFLAGS variable |
| 74 | """ |
| 75 | if len(cxxflags) == 0: |
| 76 | return |
| 77 | |
| 78 | self.start_msg('Checking supported CXXFLAGS') |
| 79 | |
| 80 | supportedFlags = [] |
Alexander Afanasyev | 11e74eb | 2017-09-21 19:01:54 -0400 | [diff] [blame] | 81 | for flags in cxxflags: |
| 82 | flags = Utils.to_list(flags) |
| 83 | if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False): |
| 84 | supportedFlags += flags |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 85 | |
| 86 | self.end_msg(' '.join(supportedFlags)) |
Davide Pesavento | 89d9175 | 2016-08-14 11:34:09 +0200 | [diff] [blame] | 87 | self.env.prepend_value('CXXFLAGS', supportedFlags) |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 88 | |
| 89 | @Configure.conf |
| 90 | def add_supported_linkflags(self, linkflags): |
| 91 | """ |
| 92 | Check which linkflags are supported by compiler and add them to env.LINKFLAGS variable |
| 93 | """ |
| 94 | if len(linkflags) == 0: |
| 95 | return |
| 96 | |
| 97 | self.start_msg('Checking supported LINKFLAGS') |
| 98 | |
| 99 | supportedFlags = [] |
Alexander Afanasyev | 11e74eb | 2017-09-21 19:01:54 -0400 | [diff] [blame] | 100 | for flags in linkflags: |
| 101 | flags = Utils.to_list(flags) |
| 102 | if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False): |
| 103 | supportedFlags += flags |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 104 | |
| 105 | self.end_msg(' '.join(supportedFlags)) |
Davide Pesavento | 89d9175 | 2016-08-14 11:34:09 +0200 | [diff] [blame] | 106 | self.env.prepend_value('LINKFLAGS', supportedFlags) |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 107 | |
| 108 | |
| 109 | class CompilerFlags(object): |
Alexander Afanasyev | 20c85cb | 2018-03-09 17:50:14 -0500 | [diff] [blame] | 110 | def getCompilerVersion(self, conf): |
| 111 | return tuple(int(i) for i in conf.env.CC_VERSION) |
| 112 | |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 113 | def getGeneralFlags(self, conf): |
Davide Pesavento | 3e79c9c | 2015-11-01 20:38:28 +0100 | [diff] [blame] | 114 | """Get dict of CXXFLAGS, LINKFLAGS, and DEFINES that are always needed""" |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 115 | return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': []} |
| 116 | |
| 117 | def getDebugFlags(self, conf): |
Davide Pesavento | 3e79c9c | 2015-11-01 20:38:28 +0100 | [diff] [blame] | 118 | """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] | 119 | return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['_DEBUG']} |
| 120 | |
| 121 | def getOptimizedFlags(self, conf): |
Davide Pesavento | 3e79c9c | 2015-11-01 20:38:28 +0100 | [diff] [blame] | 122 | """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] | 123 | return {'CXXFLAGS': [], 'LINKFLAGS': [], 'DEFINES': ['NDEBUG']} |
| 124 | |
| 125 | class GccBasicFlags(CompilerFlags): |
Junxiao Shi | c8a0a25 | 2015-10-05 07:25:02 -0700 | [diff] [blame] | 126 | """ |
| 127 | This class defines basic flags that work for both gcc and clang compilers |
| 128 | """ |
Davide Pesavento | dd0e195 | 2017-06-24 13:37:13 -0400 | [diff] [blame] | 129 | def getGeneralFlags(self, conf): |
| 130 | flags = super(GccBasicFlags, self).getGeneralFlags(conf) |
Davide Pesavento | 60f8cc1 | 2018-05-10 22:05:21 -0400 | [diff] [blame] | 131 | flags['CXXFLAGS'] += ['-std=c++14'] |
Davide Pesavento | e1b18a6 | 2018-12-19 01:17:42 -0500 | [diff] [blame] | 132 | if Utils.unversioned_sys_platform() == 'linux': |
| 133 | flags['LINKFLAGS'] += ['-fuse-ld=gold'] |
| 134 | elif Utils.unversioned_sys_platform() == 'freebsd': |
| 135 | flags['LINKFLAGS'] += ['-fuse-ld=lld'] |
Davide Pesavento | dd0e195 | 2017-06-24 13:37:13 -0400 | [diff] [blame] | 136 | return flags |
| 137 | |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 138 | def getDebugFlags(self, conf): |
| 139 | flags = super(GccBasicFlags, self).getDebugFlags(conf) |
Davide Pesavento | 3e79c9c | 2015-11-01 20:38:28 +0100 | [diff] [blame] | 140 | flags['CXXFLAGS'] += ['-O0', |
Davide Pesavento | dd0e195 | 2017-06-24 13:37:13 -0400 | [diff] [blame] | 141 | '-Og', # gcc >= 4.8, clang >= 4.0 |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 142 | '-g3', |
Davide Pesavento | 3e79c9c | 2015-11-01 20:38:28 +0100 | [diff] [blame] | 143 | '-pedantic', |
| 144 | '-Wall', |
| 145 | '-Wextra', |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 146 | '-Werror', |
Davide Pesavento | 887336c | 2017-09-19 15:25:23 -0400 | [diff] [blame] | 147 | '-Wnon-virtual-dtor', |
Alexander Afanasyev | fc4aa0d | 2016-10-15 00:57:33 +0000 | [diff] [blame] | 148 | '-Wno-error=deprecated-declarations', # Bug #3795 |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 149 | '-Wno-error=maybe-uninitialized', # Bug #1615 |
| 150 | '-Wno-unused-parameter', |
Junxiao Shi | c8a0a25 | 2015-10-05 07:25:02 -0700 | [diff] [blame] | 151 | ] |
Davide Pesavento | e1b18a6 | 2018-12-19 01:17:42 -0500 | [diff] [blame] | 152 | flags['LINKFLAGS'] += ['-Wl,-O1'] |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 153 | return flags |
| 154 | |
| 155 | def getOptimizedFlags(self, conf): |
| 156 | flags = super(GccBasicFlags, self).getOptimizedFlags(conf) |
Davide Pesavento | 3e79c9c | 2015-11-01 20:38:28 +0100 | [diff] [blame] | 157 | flags['CXXFLAGS'] += ['-O2', |
Junxiao Shi | c8a0a25 | 2015-10-05 07:25:02 -0700 | [diff] [blame] | 158 | '-g', |
Davide Pesavento | 3e79c9c | 2015-11-01 20:38:28 +0100 | [diff] [blame] | 159 | '-pedantic', |
| 160 | '-Wall', |
| 161 | '-Wextra', |
Davide Pesavento | 887336c | 2017-09-19 15:25:23 -0400 | [diff] [blame] | 162 | '-Wnon-virtual-dtor', |
Davide Pesavento | 3e79c9c | 2015-11-01 20:38:28 +0100 | [diff] [blame] | 163 | '-Wno-unused-parameter', |
Junxiao Shi | c8a0a25 | 2015-10-05 07:25:02 -0700 | [diff] [blame] | 164 | ] |
Davide Pesavento | e1b18a6 | 2018-12-19 01:17:42 -0500 | [diff] [blame] | 165 | flags['LINKFLAGS'] += ['-Wl,-O1'] |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 166 | return flags |
| 167 | |
| 168 | class GccFlags(GccBasicFlags): |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 169 | def getDebugFlags(self, conf): |
| 170 | flags = super(GccFlags, self).getDebugFlags(conf) |
Davide Pesavento | 26ea1ac | 2018-05-10 20:20:03 -0400 | [diff] [blame] | 171 | flags['CXXFLAGS'] += ['-fdiagnostics-color'] |
Junxiao Shi | c8a0a25 | 2015-10-05 07:25:02 -0700 | [diff] [blame] | 172 | return flags |
| 173 | |
| 174 | def getOptimizedFlags(self, conf): |
| 175 | flags = super(GccFlags, self).getOptimizedFlags(conf) |
Davide Pesavento | 26ea1ac | 2018-05-10 20:20:03 -0400 | [diff] [blame] | 176 | flags['CXXFLAGS'] += ['-fdiagnostics-color'] |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 177 | return flags |
| 178 | |
| 179 | class ClangFlags(GccBasicFlags): |
| 180 | def getGeneralFlags(self, conf): |
| 181 | flags = super(ClangFlags, self).getGeneralFlags(conf) |
Alexander Afanasyev | 20c85cb | 2018-03-09 17:50:14 -0500 | [diff] [blame] | 182 | if Utils.unversioned_sys_platform() == 'darwin' and self.getCompilerVersion(conf) >= (9, 0, 0): |
| 183 | # Bug #4296 |
Alexander Afanasyev | 11e74eb | 2017-09-21 19:01:54 -0400 | [diff] [blame] | 184 | flags['CXXFLAGS'] += [['-isystem', '/usr/local/include'], # for Homebrew |
| 185 | ['-isystem', '/opt/local/include']] # for MacPorts |
Davide Pesavento | e1b18a6 | 2018-12-19 01:17:42 -0500 | [diff] [blame] | 186 | if Utils.unversioned_sys_platform() == 'freebsd': |
| 187 | flags['CXXFLAGS'] += [['-isystem', '/usr/local/include']] # Bug #4790 |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 188 | return flags |
| 189 | |
| 190 | def getDebugFlags(self, conf): |
| 191 | flags = super(ClangFlags, self).getDebugFlags(conf) |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 192 | flags['CXXFLAGS'] += ['-fcolor-diagnostics', |
| 193 | '-Wextra-semi', |
| 194 | '-Wundefined-func-template', |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 195 | '-Wno-unused-local-typedef', # Bugs #2657 and #3209 |
| 196 | ] |
Alexander Afanasyev | 20c85cb | 2018-03-09 17:50:14 -0500 | [diff] [blame] | 197 | version = self.getCompilerVersion(conf) |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 198 | if version < (3, 9, 0) or (Utils.unversioned_sys_platform() == 'darwin' and version < (8, 1, 0)): |
| 199 | flags['CXXFLAGS'] += ['-Wno-unknown-pragmas'] |
Davide Pesavento | 88beb4d | 2018-09-11 18:14:18 -0400 | [diff] [blame] | 200 | if version < (6, 0, 0): |
| 201 | flags['CXXFLAGS'] += ['-Wno-missing-braces'] # Bug #4721 |
Junxiao Shi | c8a0a25 | 2015-10-05 07:25:02 -0700 | [diff] [blame] | 202 | return flags |
| 203 | |
| 204 | def getOptimizedFlags(self, conf): |
| 205 | flags = super(ClangFlags, self).getOptimizedFlags(conf) |
| 206 | flags['CXXFLAGS'] += ['-fcolor-diagnostics', |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 207 | '-Wextra-semi', |
| 208 | '-Wundefined-func-template', |
Junxiao Shi | c8a0a25 | 2015-10-05 07:25:02 -0700 | [diff] [blame] | 209 | '-Wno-unused-local-typedef', # Bugs #2657 and #3209 |
| 210 | ] |
Alexander Afanasyev | 20c85cb | 2018-03-09 17:50:14 -0500 | [diff] [blame] | 211 | version = self.getCompilerVersion(conf) |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 212 | if version < (3, 9, 0) or (Utils.unversioned_sys_platform() == 'darwin' and version < (8, 1, 0)): |
| 213 | flags['CXXFLAGS'] += ['-Wno-unknown-pragmas'] |
Davide Pesavento | 88beb4d | 2018-09-11 18:14:18 -0400 | [diff] [blame] | 214 | if version < (6, 0, 0): |
| 215 | flags['CXXFLAGS'] += ['-Wno-missing-braces'] # Bug #4721 |
Junxiao Shi | f719124 | 2015-03-19 05:53:41 -0700 | [diff] [blame] | 216 | return flags |