Nico Weber | 09e0b38 | 2019-03-11 16:54:07 +0000 | [diff] [blame] | 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 | |
| 5 | # This file is imported by various thin wrappers (around gn, clang-format, ...), |
| 6 | # so it's meant to import very quickly. To keep it that way don't add more |
Dirk Pranke | 112a77f | 2019-03-12 01:50:42 +0000 | [diff] [blame] | 7 | # code, and even more importantly don't add more toplevel import statements, |
| 8 | # particularly for modules that are not builtin (see sys.builtin_modules_names, |
| 9 | # os isn't built in, but it's essential to this file). |
Raul Tambre | b946b23 | 2019-03-26 14:48:46 +0000 | [diff] [blame] | 10 | |
Edward Lemur | 84b5f9a | 2020-01-08 20:06:51 +0000 | [diff] [blame] | 11 | import logging |
Nico Weber | 09e0b38 | 2019-03-11 16:54:07 +0000 | [diff] [blame] | 12 | import os |
Dirk Pranke | 112a77f | 2019-03-12 01:50:42 +0000 | [diff] [blame] | 13 | import sys |
Nico Weber | 09e0b38 | 2019-03-11 16:54:07 +0000 | [diff] [blame] | 14 | |
Gavin Mak | 65c49b1 | 2023-08-24 18:06:42 +0000 | [diff] [blame] | 15 | import gclient_utils |
| 16 | import subprocess2 |
| 17 | |
Nico Weber | 09e0b38 | 2019-03-11 16:54:07 +0000 | [diff] [blame] | 18 | |
| 19 | def FindGclientRoot(from_dir, filename='.gclient'): |
| 20 | """Tries to find the gclient root.""" |
Robert Liao | b888780 | 2021-05-13 23:20:50 +0000 | [diff] [blame] | 21 | real_from_dir = os.path.abspath(from_dir) |
Nico Weber | 09e0b38 | 2019-03-11 16:54:07 +0000 | [diff] [blame] | 22 | path = real_from_dir |
| 23 | while not os.path.exists(os.path.join(path, filename)): |
| 24 | split_path = os.path.split(path) |
| 25 | if not split_path[1]: |
| 26 | return None |
| 27 | path = split_path[0] |
| 28 | |
Edward Lemur | 84b5f9a | 2020-01-08 20:06:51 +0000 | [diff] [blame] | 29 | logging.info('Found gclient root at ' + path) |
| 30 | |
| 31 | if path == real_from_dir: |
| 32 | return path |
| 33 | |
Nico Weber | 09e0b38 | 2019-03-11 16:54:07 +0000 | [diff] [blame] | 34 | # If we did not find the file in the current directory, make sure we are in a |
| 35 | # sub directory that is controlled by this configuration. |
Edward Lemur | 84b5f9a | 2020-01-08 20:06:51 +0000 | [diff] [blame] | 36 | entries_filename = os.path.join(path, filename + '_entries') |
| 37 | if not os.path.exists(entries_filename): |
| 38 | # If .gclient_entries does not exist, a previous call to gclient sync |
| 39 | # might have failed. In that case, we cannot verify that the .gclient |
| 40 | # is the one we want to use. In order to not to cause too much trouble, |
| 41 | # just issue a warning and return the path anyway. |
| 42 | print( |
| 43 | "%s missing, %s file in parent directory %s might not be the file " |
| 44 | "you want to use." % (entries_filename, filename, path), |
| 45 | file=sys.stderr) |
| 46 | return path |
Nico Weber | 09e0b38 | 2019-03-11 16:54:07 +0000 | [diff] [blame] | 47 | |
Edward Lemur | 84b5f9a | 2020-01-08 20:06:51 +0000 | [diff] [blame] | 48 | entries_content = gclient_utils.FileRead(entries_filename) |
| 49 | scope = {} |
| 50 | try: |
| 51 | exec(entries_content, scope) |
| 52 | except (SyntaxError, Exception) as e: |
| 53 | gclient_utils.SyntaxErrorToError(filename, e) |
| 54 | |
| 55 | all_directories = scope['entries'].keys() |
| 56 | path_to_check = os.path.relpath(real_from_dir, path) |
| 57 | while path_to_check: |
| 58 | if path_to_check in all_directories: |
| 59 | return path |
| 60 | path_to_check = os.path.dirname(path_to_check) |
| 61 | |
| 62 | return None |
Nico Weber | 09e0b38 | 2019-03-11 16:54:07 +0000 | [diff] [blame] | 63 | |
| 64 | |
| 65 | def GetPrimarySolutionPath(): |
| 66 | """Returns the full path to the primary solution. (gclient_root + src)""" |
| 67 | |
| 68 | gclient_root = FindGclientRoot(os.getcwd()) |
Edward Lemur | 84b5f9a | 2020-01-08 20:06:51 +0000 | [diff] [blame] | 69 | if gclient_root: |
| 70 | # Some projects' top directory is not named 'src'. |
| 71 | source_dir_name = GetGClientPrimarySolutionName(gclient_root) or 'src' |
| 72 | return os.path.join(gclient_root, source_dir_name) |
Nico Weber | 09e0b38 | 2019-03-11 16:54:07 +0000 | [diff] [blame] | 73 | |
Edward Lemur | 84b5f9a | 2020-01-08 20:06:51 +0000 | [diff] [blame] | 74 | # Some projects might not use .gclient. Try to see whether we're in a git |
| 75 | # checkout that contains a 'buildtools' subdir. |
| 76 | top_dir = os.getcwd() |
| 77 | try: |
Wan-Teh Chang | bc5906f | 2023-04-03 19:50:41 +0000 | [diff] [blame] | 78 | top_dir = subprocess2.check_output(['git', 'rev-parse', '--show-toplevel'], |
| 79 | stderr=subprocess2.DEVNULL) |
Gavin Mak | 65c49b1 | 2023-08-24 18:06:42 +0000 | [diff] [blame] | 80 | top_dir = top_dir.decode('utf-8', 'replace') |
Edward Lemur | 84b5f9a | 2020-01-08 20:06:51 +0000 | [diff] [blame] | 81 | top_dir = os.path.normpath(top_dir.strip()) |
| 82 | except subprocess2.CalledProcessError: |
| 83 | pass |
| 84 | |
| 85 | if os.path.exists(os.path.join(top_dir, 'buildtools')): |
| 86 | return top_dir |
| 87 | return None |
Nico Weber | 09e0b38 | 2019-03-11 16:54:07 +0000 | [diff] [blame] | 88 | |
| 89 | |
| 90 | def GetBuildtoolsPath(): |
| 91 | """Returns the full path to the buildtools directory. |
| 92 | This is based on the root of the checkout containing the current directory.""" |
| 93 | |
| 94 | # Overriding the build tools path by environment is highly unsupported and may |
| 95 | # break without warning. Do not rely on this for anything important. |
| 96 | override = os.environ.get('CHROMIUM_BUILDTOOLS_PATH') |
| 97 | if override is not None: |
| 98 | return override |
| 99 | |
| 100 | primary_solution = GetPrimarySolutionPath() |
| 101 | if not primary_solution: |
| 102 | return None |
Edward Lemur | 84b5f9a | 2020-01-08 20:06:51 +0000 | [diff] [blame] | 103 | |
Nico Weber | 09e0b38 | 2019-03-11 16:54:07 +0000 | [diff] [blame] | 104 | buildtools_path = os.path.join(primary_solution, 'buildtools') |
Edward Lemur | 84b5f9a | 2020-01-08 20:06:51 +0000 | [diff] [blame] | 105 | if os.path.exists(buildtools_path): |
| 106 | return buildtools_path |
| 107 | |
| 108 | # buildtools may be in the gclient root. |
| 109 | gclient_root = FindGclientRoot(os.getcwd()) |
| 110 | buildtools_path = os.path.join(gclient_root, 'buildtools') |
| 111 | if os.path.exists(buildtools_path): |
| 112 | return buildtools_path |
| 113 | |
| 114 | return None |
Nico Weber | 09e0b38 | 2019-03-11 16:54:07 +0000 | [diff] [blame] | 115 | |
| 116 | |
| 117 | def GetBuildtoolsPlatformBinaryPath(): |
| 118 | """Returns the full path to the binary directory for the current platform.""" |
| 119 | buildtools_path = GetBuildtoolsPath() |
| 120 | if not buildtools_path: |
| 121 | return None |
| 122 | |
| 123 | if sys.platform.startswith(('cygwin', 'win')): |
| 124 | subdir = 'win' |
| 125 | elif sys.platform == 'darwin': |
| 126 | subdir = 'mac' |
| 127 | elif sys.platform.startswith('linux'): |
Raul Tambre | b946b23 | 2019-03-26 14:48:46 +0000 | [diff] [blame] | 128 | subdir = 'linux64' |
Nico Weber | 09e0b38 | 2019-03-11 16:54:07 +0000 | [diff] [blame] | 129 | else: |
Edward Lemur | 84b5f9a | 2020-01-08 20:06:51 +0000 | [diff] [blame] | 130 | raise gclient_utils.Error('Unknown platform: ' + sys.platform) |
Nico Weber | 09e0b38 | 2019-03-11 16:54:07 +0000 | [diff] [blame] | 131 | return os.path.join(buildtools_path, subdir) |
| 132 | |
| 133 | |
| 134 | def GetExeSuffix(): |
| 135 | """Returns '' or '.exe' depending on how executables work on this platform.""" |
Nico Weber | 09e0b38 | 2019-03-11 16:54:07 +0000 | [diff] [blame] | 136 | if sys.platform.startswith(('cygwin', 'win')): |
| 137 | return '.exe' |
| 138 | return '' |
| 139 | |
| 140 | |
| 141 | def GetGClientPrimarySolutionName(gclient_root_dir_path): |
| 142 | """Returns the name of the primary solution in the .gclient file specified.""" |
| 143 | gclient_config_file = os.path.join(gclient_root_dir_path, '.gclient') |
Edward Lemur | 84b5f9a | 2020-01-08 20:06:51 +0000 | [diff] [blame] | 144 | gclient_config_contents = gclient_utils.FileRead(gclient_config_file) |
Nico Weber | 09e0b38 | 2019-03-11 16:54:07 +0000 | [diff] [blame] | 145 | env = {} |
Edward Lemur | 84b5f9a | 2020-01-08 20:06:51 +0000 | [diff] [blame] | 146 | exec(gclient_config_contents, env) |
Nico Weber | 09e0b38 | 2019-03-11 16:54:07 +0000 | [diff] [blame] | 147 | solutions = env.get('solutions', []) |
| 148 | if solutions: |
| 149 | return solutions[0].get('name') |
| 150 | return None |