blob: 10754eb6537a8db16068cdbb77eab36fa773dac6 [file] [log] [blame]
Nico Weber09e0b382019-03-11 16:54:07 +00001# 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 Pranke112a77f2019-03-12 01:50:42 +00007# 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 Tambreb946b232019-03-26 14:48:46 +000010
Edward Lemur84b5f9a2020-01-08 20:06:51 +000011import logging
Nico Weber09e0b382019-03-11 16:54:07 +000012import os
Dirk Pranke112a77f2019-03-12 01:50:42 +000013import sys
Nico Weber09e0b382019-03-11 16:54:07 +000014
Gavin Mak65c49b12023-08-24 18:06:42 +000015import gclient_utils
16import subprocess2
17
Nico Weber09e0b382019-03-11 16:54:07 +000018
19def FindGclientRoot(from_dir, filename='.gclient'):
20 """Tries to find the gclient root."""
Robert Liaob8887802021-05-13 23:20:50 +000021 real_from_dir = os.path.abspath(from_dir)
Nico Weber09e0b382019-03-11 16:54:07 +000022 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 Lemur84b5f9a2020-01-08 20:06:51 +000029 logging.info('Found gclient root at ' + path)
30
31 if path == real_from_dir:
32 return path
33
Nico Weber09e0b382019-03-11 16:54:07 +000034 # 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 Lemur84b5f9a2020-01-08 20:06:51 +000036 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 Weber09e0b382019-03-11 16:54:07 +000047
Edward Lemur84b5f9a2020-01-08 20:06:51 +000048 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 Weber09e0b382019-03-11 16:54:07 +000063
64
65def GetPrimarySolutionPath():
66 """Returns the full path to the primary solution. (gclient_root + src)"""
67
68 gclient_root = FindGclientRoot(os.getcwd())
Edward Lemur84b5f9a2020-01-08 20:06:51 +000069 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 Weber09e0b382019-03-11 16:54:07 +000073
Edward Lemur84b5f9a2020-01-08 20:06:51 +000074 # 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 Changbc5906f2023-04-03 19:50:41 +000078 top_dir = subprocess2.check_output(['git', 'rev-parse', '--show-toplevel'],
79 stderr=subprocess2.DEVNULL)
Gavin Mak65c49b12023-08-24 18:06:42 +000080 top_dir = top_dir.decode('utf-8', 'replace')
Edward Lemur84b5f9a2020-01-08 20:06:51 +000081 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 Weber09e0b382019-03-11 16:54:07 +000088
89
90def 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 Lemur84b5f9a2020-01-08 20:06:51 +0000103
Nico Weber09e0b382019-03-11 16:54:07 +0000104 buildtools_path = os.path.join(primary_solution, 'buildtools')
Edward Lemur84b5f9a2020-01-08 20:06:51 +0000105 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 Weber09e0b382019-03-11 16:54:07 +0000115
116
117def 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 Tambreb946b232019-03-26 14:48:46 +0000128 subdir = 'linux64'
Nico Weber09e0b382019-03-11 16:54:07 +0000129 else:
Edward Lemur84b5f9a2020-01-08 20:06:51 +0000130 raise gclient_utils.Error('Unknown platform: ' + sys.platform)
Nico Weber09e0b382019-03-11 16:54:07 +0000131 return os.path.join(buildtools_path, subdir)
132
133
134def GetExeSuffix():
135 """Returns '' or '.exe' depending on how executables work on this platform."""
Nico Weber09e0b382019-03-11 16:54:07 +0000136 if sys.platform.startswith(('cygwin', 'win')):
137 return '.exe'
138 return ''
139
140
141def 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 Lemur84b5f9a2020-01-08 20:06:51 +0000144 gclient_config_contents = gclient_utils.FileRead(gclient_config_file)
Nico Weber09e0b382019-03-11 16:54:07 +0000145 env = {}
Edward Lemur84b5f9a2020-01-08 20:06:51 +0000146 exec(gclient_config_contents, env)
Nico Weber09e0b382019-03-11 16:54:07 +0000147 solutions = env.get('solutions', [])
148 if solutions:
149 return solutions[0].get('name')
150 return None