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 """ 6 Verifies that the user can override the compiler and linker using CC/CXX/LD 7 environment variables. 8 """ 9 10 import TestGyp 11 import os 12 import copy 13 import sys 14 15 here = os.path.dirname(os.path.abspath(__file__)) 16 17 if sys.platform == 'win32': 18 # cross compiling not support 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. 23 for key in 'CC', 'CXX', 'LD', 'CC_host', 'CXX_host', 'LD_host': 24 if key in os.environ: 25 del os.environ[key] 26 27 28 def CheckCompiler(test, gypfile, check_for, run_gyp): 29 if run_gyp: 30 test.run_gyp(gypfile) 31 test.build(gypfile) 32 33 # We can't test to presence of my_ld.py in the output since 34 # ninja will use CXX_target as the linker regardless 35 test.must_contain_all_lines(test.stdout(), check_for) 36 37 38 test = TestGyp.TestGyp(formats=['ninja', 'make']) 39 40 def TestTargetOveride(): 41 # Check that CC, CXX and LD set target compiler 42 oldenv = os.environ.copy() 43 try: 44 os.environ['CC'] = 'python %s/my_cc.py FOO' % here 45 os.environ['CXX'] = 'python %s/my_cxx.py FOO' % here 46 os.environ['LD'] = 'python %s/my_ld.py FOO_LINK' % here 47 48 CheckCompiler(test, 'compiler.gyp', 49 ['my_cc.py', 'my_cxx.py', 'FOO', 'FOO_LINK'], 50 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.gyp', 59 ['my_cc.py', 'my_cxx.py', 'FOO', 'FOO_LINK'], 60 False) 61 62 def TestTargetOverideCompilerOnly(): 63 # Same test again but with that CC, CXX and not LD 64 oldenv = os.environ.copy() 65 try: 66 os.environ['CC'] = 'python %s/my_cc.py FOO' % here 67 os.environ['CXX'] = 'python %s/my_cxx.py FOO' % here 68 69 CheckCompiler(test, 'compiler.gyp', 70 ['my_cc.py', 'my_cxx.py', 'FOO'], 71 True) 72 finally: 73 os.environ.clear() 74 os.environ.update(oldenv) 75 76 # Run the same tests once the eviron has been restored. The 77 # generated should have embedded all the settings in the 78 # project files so the results should be the same. 79 CheckCompiler(test, 'compiler.gyp', 80 ['my_cc.py', 'my_cxx.py', 'FOO'], 81 False) 82 83 84 def TestHostOveride(): 85 # Check that CC_host sets host compilee 86 oldenv = os.environ.copy() 87 try: 88 os.environ['CC_host'] = 'python %s/my_cc.py HOST' % here 89 os.environ['CXX_host'] = 'python %s/my_cxx.py HOST' % here 90 os.environ['LD_host'] = 'python %s/my_ld.py HOST_LINK' % here 91 CheckCompiler(test, 'compiler-host.gyp', 92 ['my_cc.py', 'my_cxx.py', 'HOST', 'HOST_LINK'], 93 True) 94 finally: 95 os.environ.clear() 96 os.environ.update(oldenv) 97 98 # Run the same tests once the eviron has been restored. The 99 # generated should have embedded all the settings in the 100 # project files so the results should be the same. 101 CheckCompiler(test, 'compiler-host.gyp', 102 ['my_cc.py', 'my_cxx.py', 'HOST', 'HOST_LINK'], 103 False) 104 105 106 TestTargetOveride() 107 TestTargetOverideCompilerOnly() 108 TestHostOveride() 109 110 test.pass_test() 111