102717b7ddda2293e7152c260999035d122b21c09dimator@google.com#!/usr/bin/env python
202717b7ddda2293e7152c260999035d122b21c09dimator@google.com# Copyright (c) 2012 Google Inc. All rights reserved.
302717b7ddda2293e7152c260999035d122b21c09dimator@google.com# Use of this source code is governed by a BSD-style license that can be
402717b7ddda2293e7152c260999035d122b21c09dimator@google.com# found in the LICENSE file.
502717b7ddda2293e7152c260999035d122b21c09dimator@google.com"""
602717b7ddda2293e7152c260999035d122b21c09dimator@google.comVerifies that the user can override the compiler and linker using
702717b7ddda2293e7152c260999035d122b21c09dimator@google.comCC/CXX/NM/READELF environment variables.
802717b7ddda2293e7152c260999035d122b21c09dimator@google.com"""
902717b7ddda2293e7152c260999035d122b21c09dimator@google.com
1002717b7ddda2293e7152c260999035d122b21c09dimator@google.comimport TestGyp
1102717b7ddda2293e7152c260999035d122b21c09dimator@google.comimport os
1202717b7ddda2293e7152c260999035d122b21c09dimator@google.comimport copy
1302717b7ddda2293e7152c260999035d122b21c09dimator@google.comimport sys
1402717b7ddda2293e7152c260999035d122b21c09dimator@google.com
1502717b7ddda2293e7152c260999035d122b21c09dimator@google.comhere = os.path.dirname(os.path.abspath(__file__))
1602717b7ddda2293e7152c260999035d122b21c09dimator@google.com
1702717b7ddda2293e7152c260999035d122b21c09dimator@google.comif sys.platform == 'win32':
1802717b7ddda2293e7152c260999035d122b21c09dimator@google.com  # cross compiling not supported by ninja on windows
1902717b7ddda2293e7152c260999035d122b21c09dimator@google.com  # and make not supported on windows at all.
2002717b7ddda2293e7152c260999035d122b21c09dimator@google.com  sys.exit(0)
2102717b7ddda2293e7152c260999035d122b21c09dimator@google.com
2202717b7ddda2293e7152c260999035d122b21c09dimator@google.com# Clear any existing compiler related env vars.
2302717b7ddda2293e7152c260999035d122b21c09dimator@google.comfor key in ['CC', 'CXX', 'LINK', 'CC_host', 'CXX_host', 'LINK_host',
2402717b7ddda2293e7152c260999035d122b21c09dimator@google.com            'NM_target', 'READELF_target']:
2502717b7ddda2293e7152c260999035d122b21c09dimator@google.com  if key in os.environ:
2602717b7ddda2293e7152c260999035d122b21c09dimator@google.com    del os.environ[key]
2702717b7ddda2293e7152c260999035d122b21c09dimator@google.com
2802717b7ddda2293e7152c260999035d122b21c09dimator@google.com
2902717b7ddda2293e7152c260999035d122b21c09dimator@google.comdef CheckCompiler(test, gypfile, check_for, run_gyp):
3002717b7ddda2293e7152c260999035d122b21c09dimator@google.com  if run_gyp:
3102717b7ddda2293e7152c260999035d122b21c09dimator@google.com    test.run_gyp(gypfile)
3202717b7ddda2293e7152c260999035d122b21c09dimator@google.com  test.build(gypfile)
3302717b7ddda2293e7152c260999035d122b21c09dimator@google.com
3402717b7ddda2293e7152c260999035d122b21c09dimator@google.com  test.must_contain_all_lines(test.stdout(), check_for)
3502717b7ddda2293e7152c260999035d122b21c09dimator@google.com
3602717b7ddda2293e7152c260999035d122b21c09dimator@google.com
3702717b7ddda2293e7152c260999035d122b21c09dimator@google.comtest = TestGyp.TestGyp(formats=['ninja'])
3802717b7ddda2293e7152c260999035d122b21c09dimator@google.com# Must set the test format to something with a flavor (the part after the '-')
3902717b7ddda2293e7152c260999035d122b21c09dimator@google.com# in order to test the desired behavior. Since we want to run a non-host
4002717b7ddda2293e7152c260999035d122b21c09dimator@google.com# toolchain, we have to set the flavor to something that the ninja generator
4102717b7ddda2293e7152c260999035d122b21c09dimator@google.com# doesn't know about, so it doesn't default to the host-specific tools (e.g.,
4202717b7ddda2293e7152c260999035d122b21c09dimator@google.com# 'otool' on mac to generate the .TOC).
4302717b7ddda2293e7152c260999035d122b21c09dimator@google.com#
4402717b7ddda2293e7152c260999035d122b21c09dimator@google.com# Note that we can't just pass format=['ninja-some_toolchain'] to the
4502717b7ddda2293e7152c260999035d122b21c09dimator@google.com# constructor above, because then this test wouldn't be recognized as a ninja
4602717b7ddda2293e7152c260999035d122b21c09dimator@google.com# format test.
4702717b7ddda2293e7152c260999035d122b21c09dimator@google.comtest.formats = ['ninja-some_flavor']
4802717b7ddda2293e7152c260999035d122b21c09dimator@google.com
4902717b7ddda2293e7152c260999035d122b21c09dimator@google.com
5002717b7ddda2293e7152c260999035d122b21c09dimator@google.comdef TestTargetOverideSharedLib():
5102717b7ddda2293e7152c260999035d122b21c09dimator@google.com  # The std output from nm and readelf is redirected to files, so we can't
5202717b7ddda2293e7152c260999035d122b21c09dimator@google.com  # expect their output to appear. Instead, check for the files they create to
5302717b7ddda2293e7152c260999035d122b21c09dimator@google.com  # see if they actually ran.
5402717b7ddda2293e7152c260999035d122b21c09dimator@google.com  expected = ['my_cc.py', 'my_cxx.py', 'FOO']
5502717b7ddda2293e7152c260999035d122b21c09dimator@google.com
5602717b7ddda2293e7152c260999035d122b21c09dimator@google.com  # Check that CC, CXX, NM, READELF, set target compiler
5702717b7ddda2293e7152c260999035d122b21c09dimator@google.com  env = {'CC': 'python %s/my_cc.py FOO' % here,
5802717b7ddda2293e7152c260999035d122b21c09dimator@google.com         'CXX': 'python %s/my_cxx.py FOO' % here,
5902717b7ddda2293e7152c260999035d122b21c09dimator@google.com         'NM': 'python %s/my_nm.py' % here,
6002717b7ddda2293e7152c260999035d122b21c09dimator@google.com         'READELF': 'python %s/my_readelf.py' % here}
6102717b7ddda2293e7152c260999035d122b21c09dimator@google.com
6202717b7ddda2293e7152c260999035d122b21c09dimator@google.com  with TestGyp.LocalEnv(env):
6302717b7ddda2293e7152c260999035d122b21c09dimator@google.com    CheckCompiler(test, 'compiler-shared-lib.gyp', expected, True)
6402717b7ddda2293e7152c260999035d122b21c09dimator@google.com    test.must_contain(test.built_file_path('RAN_MY_NM'), 'RAN_MY_NM')
6502717b7ddda2293e7152c260999035d122b21c09dimator@google.com    test.must_contain(test.built_file_path('RAN_MY_READELF'), 'RAN_MY_READELF')
6602717b7ddda2293e7152c260999035d122b21c09dimator@google.com    test.unlink(test.built_file_path('RAN_MY_NM'))
6702717b7ddda2293e7152c260999035d122b21c09dimator@google.com    test.unlink(test.built_file_path('RAN_MY_READELF'))
6802717b7ddda2293e7152c260999035d122b21c09dimator@google.com
6902717b7ddda2293e7152c260999035d122b21c09dimator@google.com  # Run the same tests once the eviron has been restored.  The generated
7002717b7ddda2293e7152c260999035d122b21c09dimator@google.com  # projects should have embedded all the settings in the project files so the
7102717b7ddda2293e7152c260999035d122b21c09dimator@google.com  # results should be the same.
7202717b7ddda2293e7152c260999035d122b21c09dimator@google.com  CheckCompiler(test, 'compiler-shared-lib.gyp', expected, False)
7302717b7ddda2293e7152c260999035d122b21c09dimator@google.com  test.must_contain(test.built_file_path('RAN_MY_NM'), 'RAN_MY_NM')
7402717b7ddda2293e7152c260999035d122b21c09dimator@google.com  test.must_contain(test.built_file_path('RAN_MY_READELF'), 'RAN_MY_READELF')
7502717b7ddda2293e7152c260999035d122b21c09dimator@google.com
7602717b7ddda2293e7152c260999035d122b21c09dimator@google.com
7702717b7ddda2293e7152c260999035d122b21c09dimator@google.comTestTargetOverideSharedLib()
7802717b7ddda2293e7152c260999035d122b21c09dimator@google.comtest.pass_test()
79