gclient_paths.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # Copyright 2019 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. # This file is imported by various thin wrappers (around gn, clang-format, ...),
  5. # so it's meant to import very quickly. To keep it that way don't add more
  6. # code, and even more importantly don't add more toplevel import statements,
  7. # particularly for modules that are not builtin (see sys.builtin_modules_names,
  8. # os isn't built in, but it's essential to this file).
  9. from __future__ import print_function
  10. import gclient_utils
  11. import logging
  12. import os
  13. import subprocess2
  14. import sys
  15. def FindGclientRoot(from_dir, filename='.gclient'):
  16. """Tries to find the gclient root."""
  17. real_from_dir = os.path.abspath(from_dir)
  18. path = real_from_dir
  19. while not os.path.exists(os.path.join(path, filename)):
  20. split_path = os.path.split(path)
  21. if not split_path[1]:
  22. return None
  23. path = split_path[0]
  24. logging.info('Found gclient root at ' + path)
  25. if path == real_from_dir:
  26. return path
  27. # If we did not find the file in the current directory, make sure we are in a
  28. # sub directory that is controlled by this configuration.
  29. entries_filename = os.path.join(path, filename + '_entries')
  30. if not os.path.exists(entries_filename):
  31. # If .gclient_entries does not exist, a previous call to gclient sync
  32. # might have failed. In that case, we cannot verify that the .gclient
  33. # is the one we want to use. In order to not to cause too much trouble,
  34. # just issue a warning and return the path anyway.
  35. print(
  36. "%s missing, %s file in parent directory %s might not be the file "
  37. "you want to use." % (entries_filename, filename, path),
  38. file=sys.stderr)
  39. return path
  40. entries_content = gclient_utils.FileRead(entries_filename)
  41. scope = {}
  42. try:
  43. exec(entries_content, scope)
  44. except (SyntaxError, Exception) as e:
  45. gclient_utils.SyntaxErrorToError(filename, e)
  46. all_directories = scope['entries'].keys()
  47. path_to_check = os.path.relpath(real_from_dir, path)
  48. while path_to_check:
  49. if path_to_check in all_directories:
  50. return path
  51. path_to_check = os.path.dirname(path_to_check)
  52. return None
  53. def GetPrimarySolutionPath():
  54. """Returns the full path to the primary solution. (gclient_root + src)"""
  55. gclient_root = FindGclientRoot(os.getcwd())
  56. if gclient_root:
  57. # Some projects' top directory is not named 'src'.
  58. source_dir_name = GetGClientPrimarySolutionName(gclient_root) or 'src'
  59. return os.path.join(gclient_root, source_dir_name)
  60. # Some projects might not use .gclient. Try to see whether we're in a git
  61. # checkout that contains a 'buildtools' subdir.
  62. top_dir = os.getcwd()
  63. try:
  64. top_dir = subprocess2.check_output(['git', 'rev-parse', '--show-toplevel'],
  65. stderr=subprocess2.DEVNULL)
  66. if sys.version_info.major == 3:
  67. top_dir = top_dir.decode('utf-8', 'replace')
  68. top_dir = os.path.normpath(top_dir.strip())
  69. except subprocess2.CalledProcessError:
  70. pass
  71. if os.path.exists(os.path.join(top_dir, 'buildtools')):
  72. return top_dir
  73. return None
  74. def GetBuildtoolsPath():
  75. """Returns the full path to the buildtools directory.
  76. This is based on the root of the checkout containing the current directory."""
  77. # Overriding the build tools path by environment is highly unsupported and may
  78. # break without warning. Do not rely on this for anything important.
  79. override = os.environ.get('CHROMIUM_BUILDTOOLS_PATH')
  80. if override is not None:
  81. return override
  82. primary_solution = GetPrimarySolutionPath()
  83. if not primary_solution:
  84. return None
  85. buildtools_path = os.path.join(primary_solution, 'buildtools')
  86. if os.path.exists(buildtools_path):
  87. return buildtools_path
  88. # buildtools may be in the gclient root.
  89. gclient_root = FindGclientRoot(os.getcwd())
  90. buildtools_path = os.path.join(gclient_root, 'buildtools')
  91. if os.path.exists(buildtools_path):
  92. return buildtools_path
  93. return None
  94. def GetBuildtoolsPlatformBinaryPath():
  95. """Returns the full path to the binary directory for the current platform."""
  96. buildtools_path = GetBuildtoolsPath()
  97. if not buildtools_path:
  98. return None
  99. if sys.platform.startswith(('cygwin', 'win')):
  100. subdir = 'win'
  101. elif sys.platform == 'darwin':
  102. subdir = 'mac'
  103. elif sys.platform.startswith('linux'):
  104. subdir = 'linux64'
  105. else:
  106. raise gclient_utils.Error('Unknown platform: ' + sys.platform)
  107. return os.path.join(buildtools_path, subdir)
  108. def GetExeSuffix():
  109. """Returns '' or '.exe' depending on how executables work on this platform."""
  110. if sys.platform.startswith(('cygwin', 'win')):
  111. return '.exe'
  112. return ''
  113. def GetGClientPrimarySolutionName(gclient_root_dir_path):
  114. """Returns the name of the primary solution in the .gclient file specified."""
  115. gclient_config_file = os.path.join(gclient_root_dir_path, '.gclient')
  116. gclient_config_contents = gclient_utils.FileRead(gclient_config_file)
  117. env = {}
  118. exec(gclient_config_contents, env)
  119. solutions = env.get('solutions', [])
  120. if solutions:
  121. return solutions[0].get('name')
  122. return None