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 | |
| 3 | from waflib import Logs, Configure |
| 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', |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 7 | help='''Compile in debugging mode without 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): |
| 10 | areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0) |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 11 | defaultFlags = ['-std=c++0x', '-std=c++11', |
| 12 | '-stdlib=libc++', # clang on OSX < 10.9 by default uses gcc's |
| 13 | # libstdc++, which is not C++11 compatible |
| 14 | '-pedantic', '-Wall'] |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 15 | |
Alexander Afanasyev | 3fd14f0 | 2014-03-26 14:34:39 -0700 | [diff] [blame] | 16 | if conf.options.debug: |
| 17 | conf.define('_DEBUG', 1) |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 18 | defaultFlags += ['-O0', |
| 19 | '-Og', # gcc >= 4.8 |
| 20 | '-g3', |
| 21 | '-fcolor-diagnostics', # clang |
| 22 | '-fdiagnostics-color', # gcc >= 4.9 |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 23 | '-Werror', |
| 24 | '-Wno-error=deprecated-register', |
Alexander Afanasyev | 3fd14f0 | 2014-03-26 14:34:39 -0700 | [diff] [blame] | 25 | ] |
Alexander Afanasyev | 3fd14f0 | 2014-03-26 14:34:39 -0700 | [diff] [blame] | 26 | if areCustomCxxflagsPresent: |
| 27 | missingFlags = [x for x in defaultFlags if x not in conf.env.CXXFLAGS] |
| 28 | if len(missingFlags) > 0: |
| 29 | 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] | 30 | % " ".join(conf.env.CXXFLAGS)) |
Alexander Afanasyev | 3fd14f0 | 2014-03-26 14:34:39 -0700 | [diff] [blame] | 31 | Logs.warn("Default flags '%s' are not activated" % " ".join(missingFlags)) |
| 32 | else: |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 33 | conf.add_supported_cxxflags(defaultFlags) |
Alexander Afanasyev | 3fd14f0 | 2014-03-26 14:34:39 -0700 | [diff] [blame] | 34 | else: |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 35 | defaultFlags += ['-O2', '-g'] |
Alexander Afanasyev | 3fd14f0 | 2014-03-26 14:34:39 -0700 | [diff] [blame] | 36 | if not areCustomCxxflagsPresent: |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 37 | conf.add_supported_cxxflags(defaultFlags) |
Alexander Afanasyev | 3fd14f0 | 2014-03-26 14:34:39 -0700 | [diff] [blame] | 38 | |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 39 | # clang on OSX < 10.9 by default uses gcc's libstdc++, which is not C++11 compatible |
| 40 | conf.add_supported_linkflags(['-stdlib=libc++']) |
| 41 | |
Alexander Afanasyev | 3fd14f0 | 2014-03-26 14:34:39 -0700 | [diff] [blame] | 42 | @Configure.conf |
| 43 | def add_supported_cxxflags(self, cxxflags): |
| 44 | """ |
| 45 | Check which cxxflags are supported by compiler and add them to env.CXXFLAGS variable |
| 46 | """ |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 47 | self.start_msg('Checking supported CXXFLAGS') |
Alexander Afanasyev | 3fd14f0 | 2014-03-26 14:34:39 -0700 | [diff] [blame] | 48 | |
| 49 | supportedFlags = [] |
| 50 | for flag in cxxflags: |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 51 | if self.check_cxx(cxxflags=['-Werror', flag], mandatory=False): |
Alexander Afanasyev | 3fd14f0 | 2014-03-26 14:34:39 -0700 | [diff] [blame] | 52 | supportedFlags += [flag] |
| 53 | |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 54 | self.end_msg(' '.join(supportedFlags)) |
| 55 | self.env.CXXFLAGS = supportedFlags + self.env.CXXFLAGS |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 56 | |
| 57 | @Configure.conf |
| 58 | def add_supported_linkflags(self, linkflags): |
| 59 | """ |
| 60 | Check which linkflags are supported by compiler and add them to env.LINKFLAGS variable |
| 61 | """ |
| 62 | self.start_msg('Checking supported LINKFLAGS') |
| 63 | |
| 64 | supportedFlags = [] |
| 65 | for flag in linkflags: |
| 66 | if self.check_cxx(linkflags=['-Werror', flag], mandatory=False): |
| 67 | supportedFlags += [flag] |
| 68 | |
| 69 | self.end_msg(' '.join(supportedFlags)) |
| 70 | self.env.LINKFLAGS = supportedFlags + self.env.LINKFLAGS |