gclient_paths.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. import logging
  10. import os
  11. import sys
  12. import gclient_utils
  13. import subprocess2
  14. # TODO: Should fix these warnings.
  15. # pylint: disable=line-too-long
  16. def FindGclientRoot(from_dir, filename='.gclient'):
  17. """Tries to find the gclient root."""
  18. real_from_dir = os.path.abspath(from_dir)
  19. path = real_from_dir
  20. while not os.path.exists(os.path.join(path, filename)):
  21. split_path = os.path.split(path)
  22. if not split_path[1]:
  23. return None
  24. path = split_path[0]
  25. logging.info('Found gclient root at ' + path)
  26. if path == real_from_dir:
  27. return path
  28. # If we did not find the file in the current directory, make sure we are in
  29. # a sub directory that is controlled by this configuration.
  30. entries_filename = os.path.join(path, filename + '_entries')
  31. if not os.path.exists(entries_filename):
  32. # If .gclient_entries does not exist, a previous call to gclient sync
  33. # might have failed. In that case, we cannot verify that the .gclient
  34. # is the one we want to use. In order to not to cause too much trouble,
  35. # just issue a warning and return the path anyway.
  36. print(
  37. "%s missing, %s file in parent directory %s might not be the file "
  38. "you want to use." % (entries_filename, filename, path),
  39. file=sys.stderr)
  40. return path
  41. entries_content = gclient_utils.FileRead(entries_filename)
  42. scope = {}
  43. try:
  44. exec(entries_content, scope)
  45. except (SyntaxError, Exception) as e:
  46. gclient_utils.SyntaxErrorToError(filename, e)
  47. all_directories = scope['entries'].keys()
  48. path_to_check = os.path.relpath(real_from_dir, path)
  49. while path_to_check:
  50. if path_to_check in all_directories:
  51. return path
  52. path_to_check = os.path.dirname(path_to_check)
  53. return None
  54. def GetPrimarySolutionPath():
  55. """Returns the full path to the primary solution. (gclient_root + src)"""
  56. gclient_root = FindGclientRoot(os.getcwd())
  57. if gclient_root:
  58. # Some projects' top directory is not named 'src'.
  59. source_dir_name = GetGClientPrimarySolutionName(gclient_root) or 'src'
  60. return os.path.join(gclient_root, source_dir_name)
  61. # Some projects might not use .gclient. Try to see whether we're in a git
  62. # checkout that contains a 'buildtools' subdir.
  63. top_dir = os.getcwd()
  64. try:
  65. top_dir = subprocess2.check_output(
  66. ['git', 'rev-parse', '--show-toplevel'], stderr=subprocess2.DEVNULL)
  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
  78. # may 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