1#!/usr/bin/env python
2# Copyright (c) 2012 Google Inc. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5"""
6Verifies that the user can override the compiler and linker using CC/CXX/LD
7environment variables.
8"""
9
10import TestGyp
11import os
12import copy
13import sys
14
15here = os.path.dirname(os.path.abspath(__file__))
16
17if sys.platform == 'win32':
18  # cross compiling not supported by ninja on windows
19  # and make not supported on windows at all.
20  sys.exit(0)
21
22# Clear any existing compiler related env vars.
23for key in ['CC', 'CXX', 'LINK', 'CC_host', 'CXX_host', 'LINK_host']:
24  if key in os.environ:
25    del os.environ[key]
26
27
28def CheckCompiler(test, gypfile, check_for, run_gyp):
29  if run_gyp:
30    test.run_gyp(gypfile)
31  test.build(gypfile)
32
33  test.must_contain_all_lines(test.stdout(), check_for)
34
35
36test = TestGyp.TestGyp(formats=['ninja', 'make'])
37
38def TestTargetOveride():
39  expected = ['my_cc.py', 'my_cxx.py', 'FOO' ]
40  if test.format != 'ninja':  # ninja just uses $CC / $CXX as linker.
41    expected.append('FOO_LINK')
42
43  # Check that CC, CXX and LD set target compiler
44  oldenv = os.environ.copy()
45  try:
46    os.environ['CC'] = 'python %s/my_cc.py FOO' % here
47    os.environ['CXX'] = 'python %s/my_cxx.py FOO' % here
48    os.environ['LINK'] = 'python %s/my_ld.py FOO_LINK' % here
49
50    CheckCompiler(test, 'compiler-exe.gyp', expected, True)
51  finally:
52    os.environ.clear()
53    os.environ.update(oldenv)
54
55  # Run the same tests once the eviron has been restored.  The
56  # generated should have embedded all the settings in the
57  # project files so the results should be the same.
58  CheckCompiler(test, 'compiler-exe.gyp', expected, False)
59
60
61def TestTargetOverideCompilerOnly():
62  # Same test again but with that CC, CXX and not LD
63  oldenv = os.environ.copy()
64  try:
65    os.environ['CC'] = 'python %s/my_cc.py FOO' % here
66    os.environ['CXX'] = 'python %s/my_cxx.py FOO' % here
67
68    CheckCompiler(test, 'compiler-exe.gyp',
69                  ['my_cc.py', 'my_cxx.py', 'FOO'],
70                  True)
71  finally:
72    os.environ.clear()
73    os.environ.update(oldenv)
74
75  # Run the same tests once the eviron has been restored.  The
76  # generated should have embedded all the settings in the
77  # project files so the results should be the same.
78  CheckCompiler(test, 'compiler-exe.gyp',
79                ['my_cc.py', 'my_cxx.py', 'FOO'],
80                False)
81
82
83def TestHostOveride():
84  expected = ['my_cc.py', 'my_cxx.py', 'HOST' ]
85  if test.format != 'ninja':  # ninja just uses $CC / $CXX as linker.
86    expected.append('HOST_LINK')
87
88  # Check that CC_host sets host compilee
89  oldenv = os.environ.copy()
90  try:
91    os.environ['CC_host'] = 'python %s/my_cc.py HOST' % here
92    os.environ['CXX_host'] = 'python %s/my_cxx.py HOST' % here
93    os.environ['LINK_host'] = 'python %s/my_ld.py HOST_LINK' % here
94    CheckCompiler(test, 'compiler-host.gyp', expected, True)
95  finally:
96    os.environ.clear()
97    os.environ.update(oldenv)
98
99  # Run the same tests once the eviron has been restored.  The
100  # generated should have embedded all the settings in the
101  # project files so the results should be the same.
102  CheckCompiler(test, 'compiler-host.gyp', expected, False)
103
104
105TestTargetOveride()
106TestTargetOverideCompilerOnly()
107TestHostOveride()
108
109test.pass_test()
110