Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 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 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. |
| 23 | for key in ['CC', 'CXX', 'LINK', 'CC_host', 'CXX_host', 'LINK_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 | test.must_contain_all_lines(test.stdout(), check_for) |
| 34 | |
| 35 | |
| 36 | test = TestGyp.TestGyp(formats=['ninja', 'make']) |
| 37 | |
| 38 | def TestTargetOveride(): |
| 39 | expected = ['my_cc.py', 'my_cxx.py', 'FOO' ] |
| 40 | |
| 41 | # ninja just uses $CC / $CXX as linker. |
| 42 | if test.format not in ['ninja', 'xcode-ninja']: |
| 43 | expected.append('FOO_LINK') |
| 44 | |
| 45 | # Check that CC, CXX and LD set target compiler |
| 46 | oldenv = os.environ.copy() |
| 47 | try: |
| 48 | os.environ['CC'] = 'python %s/my_cc.py FOO' % here |
| 49 | os.environ['CXX'] = 'python %s/my_cxx.py FOO' % here |
| 50 | os.environ['LINK'] = 'python %s/my_ld.py FOO_LINK' % here |
| 51 | |
| 52 | CheckCompiler(test, 'compiler-exe.gyp', expected, True) |
| 53 | finally: |
| 54 | os.environ.clear() |
| 55 | os.environ.update(oldenv) |
| 56 | |
| 57 | # Run the same tests once the eviron has been restored. The |
| 58 | # generated should have embedded all the settings in the |
| 59 | # project files so the results should be the same. |
| 60 | CheckCompiler(test, 'compiler-exe.gyp', expected, False) |
| 61 | |
| 62 | |
| 63 | def TestTargetOverideCompilerOnly(): |
| 64 | # Same test again but with that CC, CXX and not LD |
| 65 | oldenv = os.environ.copy() |
| 66 | try: |
| 67 | os.environ['CC'] = 'python %s/my_cc.py FOO' % here |
| 68 | os.environ['CXX'] = 'python %s/my_cxx.py FOO' % here |
| 69 | |
| 70 | CheckCompiler(test, 'compiler-exe.gyp', |
| 71 | ['my_cc.py', 'my_cxx.py', 'FOO'], |
| 72 | True) |
| 73 | finally: |
| 74 | os.environ.clear() |
| 75 | os.environ.update(oldenv) |
| 76 | |
| 77 | # Run the same tests once the eviron has been restored. The |
| 78 | # generated should have embedded all the settings in the |
| 79 | # project files so the results should be the same. |
| 80 | CheckCompiler(test, 'compiler-exe.gyp', |
| 81 | ['my_cc.py', 'my_cxx.py', 'FOO'], |
| 82 | False) |
| 83 | |
| 84 | |
| 85 | def TestHostOveride(): |
| 86 | expected = ['my_cc.py', 'my_cxx.py', 'HOST' ] |
| 87 | if test.format != 'ninja': # ninja just uses $CC / $CXX as linker. |
| 88 | expected.append('HOST_LINK') |
| 89 | |
| 90 | # Check that CC_host sets host compilee |
| 91 | oldenv = os.environ.copy() |
| 92 | try: |
| 93 | os.environ['CC_host'] = 'python %s/my_cc.py HOST' % here |
| 94 | os.environ['CXX_host'] = 'python %s/my_cxx.py HOST' % here |
| 95 | os.environ['LINK_host'] = 'python %s/my_ld.py HOST_LINK' % here |
| 96 | CheckCompiler(test, 'compiler-host.gyp', expected, True) |
| 97 | finally: |
| 98 | os.environ.clear() |
| 99 | os.environ.update(oldenv) |
| 100 | |
| 101 | # Run the same tests once the eviron has been restored. The |
| 102 | # generated should have embedded all the settings in the |
| 103 | # project files so the results should be the same. |
| 104 | CheckCompiler(test, 'compiler-host.gyp', expected, False) |
| 105 | |
| 106 | |
| 107 | TestTargetOveride() |
| 108 | TestTargetOverideCompilerOnly() |
| 109 | |
| 110 | test.pass_test() |