gn.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env python
  2. # Copyright 2013 The Chromium Authors. 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. """This script is a wrapper around the GN binary that is pulled from Google
  6. Cloud Storage when you sync Chrome. The binaries go into platform-specific
  7. subdirectories in the source tree.
  8. This script makes there be one place for forwarding to the correct platform's
  9. binary. It will also automatically try to find the gn binary when run inside
  10. the chrome source tree, so users can just type "gn" on the command line
  11. (normally depot_tools is on the path)."""
  12. from __future__ import print_function
  13. import gclient_paths
  14. import os
  15. import subprocess
  16. import sys
  17. def PruneVirtualEnv():
  18. # Set by VirtualEnv, no need to keep it.
  19. os.environ.pop('VIRTUAL_ENV', None)
  20. # Set by VPython, if scripts want it back they have to set it explicitly.
  21. os.environ.pop('PYTHONNOUSERSITE', None)
  22. # Look for "activate_this.py" in this path, which is installed by VirtualEnv.
  23. # This mechanism is used by vpython as well to sanitize VirtualEnvs from
  24. # $PATH.
  25. os.environ['PATH'] = os.pathsep.join([
  26. p for p in os.environ.get('PATH', '').split(os.pathsep)
  27. if not os.path.isfile(os.path.join(p, 'activate_this.py'))
  28. ])
  29. def main(args):
  30. # Prune all evidence of VPython/VirtualEnv out of the environment. This means
  31. # that we 'unwrap' vpython VirtualEnv path/env manipulation. Invocations of
  32. # `python` from GN should never inherit the gn.py's own VirtualEnv. This also
  33. # helps to ensure that generated ninja files do not reference python.exe from
  34. # the VirtualEnv generated from depot_tools' own .vpython file (or lack
  35. # thereof), but instead reference the default python from the PATH.
  36. PruneVirtualEnv()
  37. # Try in primary solution location first, with the gn binary having been
  38. # downloaded by cipd in the projects DEPS.
  39. primary_solution_path = gclient_paths.GetPrimarySolutionPath()
  40. if primary_solution_path:
  41. gn_path = os.path.join(primary_solution_path, 'third_party',
  42. 'gn', 'gn' + gclient_paths.GetExeSuffix())
  43. if os.path.exists(gn_path):
  44. return subprocess.call([gn_path] + args[1:])
  45. # Otherwise try the old .sha1 and download_from_google_storage locations
  46. # inside of buildtools.
  47. bin_path = gclient_paths.GetBuildtoolsPlatformBinaryPath()
  48. if not bin_path:
  49. print('gn.py: Could not find checkout in any parent of the current path.\n'
  50. 'This must be run inside a checkout.', file=sys.stderr)
  51. return 1
  52. gn_path = os.path.join(bin_path, 'gn' + gclient_paths.GetExeSuffix())
  53. if not os.path.exists(gn_path):
  54. print(
  55. 'gn.py: Could not find gn executable at: %s' % gn_path, file=sys.stderr)
  56. return 2
  57. else:
  58. return subprocess.call([gn_path] + args[1:])
  59. if __name__ == '__main__':
  60. try:
  61. sys.exit(main(sys.argv))
  62. except KeyboardInterrupt:
  63. sys.stderr.write('interrupted\n')
  64. sys.exit(1)