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