blob: 1cfa213f121dbe4ecf64e951835f01244e6fb7b6 [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
Mike Frysinger124bb8e2023-09-06 05:48:55 +000018# TODO: Should fix these warnings.
19# pylint: disable=line-too-long
20
Nico Weber09e0b382019-03-11 16:54:07 +000021
22def FindGclientRoot(from_dir, filename='.gclient'):
Mike Frysinger124bb8e2023-09-06 05:48:55 +000023 """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 Weber09e0b382019-03-11 16:54:07 +000031
Mike Frysinger124bb8e2023-09-06 05:48:55 +000032 logging.info('Found gclient root at ' + path)
Edward Lemur84b5f9a2020-01-08 20:06:51 +000033
Mike Frysinger124bb8e2023-09-06 05:48:55 +000034 if path == real_from_dir:
35 return path
Edward Lemur84b5f9a2020-01-08 20:06:51 +000036
Mike Frysinger124bb8e2023-09-06 05:48:55 +000037 # 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 Weber09e0b382019-03-11 16:54:07 +000050
Mike Frysinger124bb8e2023-09-06 05:48:55 +000051 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 Lemur84b5f9a2020-01-08 20:06:51 +000057
Mike Frysinger124bb8e2023-09-06 05:48:55 +000058 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 Lemur84b5f9a2020-01-08 20:06:51 +000064
Mike Frysinger124bb8e2023-09-06 05:48:55 +000065 return None
Nico Weber09e0b382019-03-11 16:54:07 +000066
67
68def GetPrimarySolutionPath():
Mike Frysinger124bb8e2023-09-06 05:48:55 +000069 """Returns the full path to the primary solution. (gclient_root + src)"""
Nico Weber09e0b382019-03-11 16:54:07 +000070
Mike Frysinger124bb8e2023-09-06 05:48:55 +000071 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 Weber09e0b382019-03-11 16:54:07 +000076
Mike Frysinger124bb8e2023-09-06 05:48:55 +000077 # 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 Lemur84b5f9a2020-01-08 20:06:51 +000087
Mike Frysinger124bb8e2023-09-06 05:48:55 +000088 if os.path.exists(os.path.join(top_dir, 'buildtools')):
89 return top_dir
90 return None
Nico Weber09e0b382019-03-11 16:54:07 +000091
92
93def GetBuildtoolsPath():
Mike Frysinger124bb8e2023-09-06 05:48:55 +000094 """Returns the full path to the buildtools directory.
Nico Weber09e0b382019-03-11 16:54:07 +000095 This is based on the root of the checkout containing the current directory."""
96
Mike Frysinger124bb8e2023-09-06 05:48:55 +000097 # 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 Weber09e0b382019-03-11 16:54:07 +0000102
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000103 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 Weber09e0b382019-03-11 16:54:07 +0000117 return None
Edward Lemur84b5f9a2020-01-08 20:06:51 +0000118
Nico Weber09e0b382019-03-11 16:54:07 +0000119
120def GetBuildtoolsPlatformBinaryPath():
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000121 """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 Weber09e0b382019-03-11 16:54:07 +0000125
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000126 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 Weber09e0b382019-03-11 16:54:07 +0000135
136
137def GetExeSuffix():
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000138 """Returns '' or '.exe' depending on how executables work on this platform."""
139 if sys.platform.startswith(('cygwin', 'win')):
140 return '.exe'
141 return ''
Nico Weber09e0b382019-03-11 16:54:07 +0000142
143
144def GetGClientPrimarySolutionName(gclient_root_dir_path):
Mike Frysinger124bb8e2023-09-06 05:48:55 +0000145 """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