gyptest-compiler-env.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. import TestGyp
  10. import os
  11. import copy
  12. import sys
  13. here = os.path.dirname(os.path.abspath(__file__))
  14. if sys.platform == 'win32':
  15. # cross compiling not supported by ninja on windows
  16. # and make not supported on windows at all.
  17. sys.exit(0)
  18. # Clear any existing compiler related env vars.
  19. for key in ['CC', 'CXX', 'LINK', 'CC_host', 'CXX_host', 'LINK_host']:
  20. if key in os.environ:
  21. del os.environ[key]
  22. def CheckCompiler(test, gypfile, check_for, run_gyp):
  23. if run_gyp:
  24. test.run_gyp(gypfile)
  25. test.build(gypfile)
  26. test.must_contain_all_lines(test.stdout(), check_for)
  27. test = TestGyp.TestGyp(formats=['ninja', 'make'])
  28. def TestTargetOveride():
  29. expected = ['my_cc.py', 'my_cxx.py', 'FOO' ]
  30. # ninja just uses $CC / $CXX as linker.
  31. if test.format not in ['ninja', 'xcode-ninja']:
  32. expected.append('FOO_LINK')
  33. # Check that CC, CXX and LD set target compiler
  34. oldenv = os.environ.copy()
  35. try:
  36. os.environ['CC'] = 'python %s/my_cc.py FOO' % here
  37. os.environ['CXX'] = 'python %s/my_cxx.py FOO' % here
  38. os.environ['LINK'] = 'python %s/my_ld.py FOO_LINK' % here
  39. CheckCompiler(test, 'compiler-exe.gyp', expected, True)
  40. finally:
  41. os.environ.clear()
  42. os.environ.update(oldenv)
  43. # Run the same tests once the eviron has been restored. The
  44. # generated should have embedded all the settings in the
  45. # project files so the results should be the same.
  46. CheckCompiler(test, 'compiler-exe.gyp', expected, False)
  47. def TestTargetOverideCompilerOnly():
  48. # Same test again but with that CC, CXX and not LD
  49. oldenv = os.environ.copy()
  50. try:
  51. os.environ['CC'] = 'python %s/my_cc.py FOO' % here
  52. os.environ['CXX'] = 'python %s/my_cxx.py FOO' % here
  53. CheckCompiler(test, 'compiler-exe.gyp',
  54. ['my_cc.py', 'my_cxx.py', 'FOO'],
  55. True)
  56. finally:
  57. os.environ.clear()
  58. os.environ.update(oldenv)
  59. # Run the same tests once the eviron has been restored. The
  60. # generated should have embedded all the settings in the
  61. # project files so the results should be the same.
  62. CheckCompiler(test, 'compiler-exe.gyp',
  63. ['my_cc.py', 'my_cxx.py', 'FOO'],
  64. False)
  65. def TestHostOveride():
  66. expected = ['my_cc.py', 'my_cxx.py', 'HOST' ]
  67. if test.format != 'ninja': # ninja just uses $CC / $CXX as linker.
  68. expected.append('HOST_LINK')
  69. # Check that CC_host sets host compilee
  70. oldenv = os.environ.copy()
  71. try:
  72. os.environ['CC_host'] = 'python %s/my_cc.py HOST' % here
  73. os.environ['CXX_host'] = 'python %s/my_cxx.py HOST' % here
  74. os.environ['LINK_host'] = 'python %s/my_ld.py HOST_LINK' % here
  75. CheckCompiler(test, 'compiler-host.gyp', expected, True)
  76. finally:
  77. os.environ.clear()
  78. os.environ.update(oldenv)
  79. # Run the same tests once the eviron has been restored. The
  80. # generated should have embedded all the settings in the
  81. # project files so the results should be the same.
  82. CheckCompiler(test, 'compiler-host.gyp', expected, False)
  83. TestTargetOveride()
  84. TestTargetOverideCompilerOnly()
  85. test.pass_test()