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 7CC/CXX/NM/READELF environment 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 'NM_target', 'READELF_target']: 25 if key in os.environ: 26 del os.environ[key] 27 28 29def CheckCompiler(test, gypfile, check_for, run_gyp): 30 if run_gyp: 31 test.run_gyp(gypfile) 32 test.build(gypfile) 33 34 test.must_contain_all_lines(test.stdout(), check_for) 35 36 37test = TestGyp.TestGyp(formats=['ninja']) 38# Must set the test format to something with a flavor (the part after the '-') 39# in order to test the desired behavior. Since we want to run a non-host 40# toolchain, we have to set the flavor to something that the ninja generator 41# doesn't know about, so it doesn't default to the host-specific tools (e.g., 42# 'otool' on mac to generate the .TOC). 43# 44# Note that we can't just pass format=['ninja-some_toolchain'] to the 45# constructor above, because then this test wouldn't be recognized as a ninja 46# format test. 47test.formats = ['ninja-some_flavor'] 48 49 50def TestTargetOverideSharedLib(): 51 # The std output from nm and readelf is redirected to files, so we can't 52 # expect their output to appear. Instead, check for the files they create to 53 # see if they actually ran. 54 expected = ['my_cc.py', 'my_cxx.py', 'FOO'] 55 56 # Check that CC, CXX, NM, READELF, set target compiler 57 env = {'CC': 'python %s/my_cc.py FOO' % here, 58 'CXX': 'python %s/my_cxx.py FOO' % here, 59 'NM': 'python %s/my_nm.py' % here, 60 'READELF': 'python %s/my_readelf.py' % here} 61 62 with TestGyp.LocalEnv(env): 63 CheckCompiler(test, 'compiler-shared-lib.gyp', expected, True) 64 test.must_contain(test.built_file_path('RAN_MY_NM'), 'RAN_MY_NM') 65 test.must_contain(test.built_file_path('RAN_MY_READELF'), 'RAN_MY_READELF') 66 test.unlink(test.built_file_path('RAN_MY_NM')) 67 test.unlink(test.built_file_path('RAN_MY_READELF')) 68 69 # Run the same tests once the eviron has been restored. The generated 70 # projects should have embedded all the settings in the project files so the 71 # results should be the same. 72 CheckCompiler(test, 'compiler-shared-lib.gyp', expected, False) 73 test.must_contain(test.built_file_path('RAN_MY_NM'), 'RAN_MY_NM') 74 test.must_contain(test.built_file_path('RAN_MY_READELF'), 'RAN_MY_READELF') 75 76 77TestTargetOverideSharedLib() 78test.pass_test() 79