1#!/usr/bin/env python
2
3# Copyright (c) 2012 Google Inc. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7"""
8Verifies that xcode-style GCC_... settings are handled properly.
9"""
10
11import TestGyp
12
13import os
14import subprocess
15import sys
16
17def IgnoreOutput(string, expected_string):
18  return True
19
20def CompilerVersion(compiler):
21  stdout = subprocess.check_output([compiler, '-v'], stderr=subprocess.STDOUT)
22  return stdout.rstrip('\n')
23
24def CompilerSupportsWarnAboutInvalidOffsetOfMacro(test):
25  # "clang" does not support the "-Winvalid-offsetof" flag, and silently
26  # ignore it. Starting with Xcode 5.0.0, "gcc" is just a "clang" binary with
27  # some hard-coded include path hack, so use the output of "-v" to detect if
28  # the compiler supports the flag or not.
29  return 'clang' not in CompilerVersion('/usr/bin/cc')
30
31if sys.platform == 'darwin':
32  test = TestGyp.TestGyp(formats=['ninja', 'make', 'xcode'])
33
34  if test.format == 'xcode-ninja':
35    test.skip_test()
36
37  CHDIR = 'xcode-gcc'
38  test.run_gyp('test.gyp', chdir=CHDIR)
39
40  # List of targets that'll pass. It expects targets of the same name with
41  # '-fail' appended that'll fail to build.
42  targets = [
43    'warn_about_missing_newline',
44  ]
45
46  # clang doesn't warn on invalid offsetofs, it silently ignores
47  # -Wno-invalid-offsetof.
48  if CompilerSupportsWarnAboutInvalidOffsetOfMacro(test):
49    targets.append('warn_about_invalid_offsetof_macro')
50
51  for target in targets:
52    test.build('test.gyp', target, chdir=CHDIR)
53    test.built_file_must_exist(target, chdir=CHDIR)
54    fail_target = target + '-fail'
55    test.build('test.gyp', fail_target, chdir=CHDIR, status=None,
56               stderr=None, match=IgnoreOutput)
57    test.built_file_must_not_exist(fail_target, chdir=CHDIR)
58
59  test.pass_test()
60