Repository navigation
Expand file tree
/
Copy pathandroid_tools.cpp
More file actions
370 lines (309 loc) · 10.6 KB
/
Copy pathandroid_tools.cpp
File metadata and controls
370 lines (309 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2018-2026 Baldur Karlsson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
#include "common/formatting.h"
#include "core/core.h"
#include "core/settings.h"
#include "strings/string_utils.h"
#include "android_utils.h"
RDOC_CONFIG(rdcstr, Android_SDKDirPath, "",
"The location of the root of the Android SDK. This path "
"should contain folders such as build-tools and platform-tools.");
RDOC_CONFIG(rdcstr, Android_JDKDirPath, "",
"The location of the root of the Java JDK. This path "
"should contain folders such as bin and lib.");
namespace Android
{
static bool adbKillServer = false;
bool toolExists(const rdcstr &path)
{
if(path.empty())
return false;
return FileIO::exists(path) || FileIO::exists(path + ".exe");
}
rdcstr getToolInSDK(ToolDir subdir, const rdcstr &jdkroot, const rdcstr &sdkroot,
const rdcstr &toolname)
{
rdcstr toolpath;
switch(subdir)
{
case ToolDir::None:
{
// This indicates the file is not a standard tool and will not exist anywhere but our
// distributed folder.
break;
}
case ToolDir::Java:
{
// if no path is configured, abort
if(jdkroot.empty())
break;
toolpath = jdkroot + "/bin/" + toolname;
if(toolExists(toolpath))
return toolpath;
break;
}
case ToolDir::BuildTools:
case ToolDir::BuildToolsLib:
case ToolDir::PlatformTools:
{
// if no path is configured, abort
if(sdkroot.empty())
break;
// if it's in platform tools it's easy, just concatenate the path
if(subdir == ToolDir::PlatformTools)
{
toolpath = sdkroot + "/platform-tools/" + toolname;
}
else
{
// otherwise we need to find the build-tools versioned folder
toolpath = sdkroot + "/build-tools/";
rdcarray<PathEntry> paths;
FileIO::GetFilesInDirectory(toolpath, paths);
if(paths.empty())
break;
uint32_t bestversion = 0;
rdcstr bestpath;
for(const PathEntry &path : paths)
{
// skip non-directories
if(!(path.flags & PathProperty::Directory))
continue;
uint32_t version = 0;
bool valid = true;
for(char c : path.filename)
{
// add digits to the version
if(c >= '0' && c <= '9')
{
int digit = int(c) - int('0');
version *= 10;
version += digit;
continue;
}
// ignore .s
if(c == '.')
continue;
// if any char is not in [.0-9] then this filename is invalid
valid = false;
break;
}
// skip non-valid directories
if(!valid)
continue;
// if this directory is a higher version, prefer it
if(version > bestversion)
{
bestversion = version;
bestpath = path.filename;
}
}
// if we didn't find a version at all, abort
if(bestversion == 0)
break;
toolpath += bestpath + "/";
if(subdir == ToolDir::BuildToolsLib)
toolpath += "lib/";
toolpath += toolname;
}
if(toolExists(toolpath))
return toolpath;
break;
}
}
return "";
}
struct ToolPathCache
{
rdcstr sdk, jdk;
std::map<rdcstr, rdcstr> paths;
};
static ToolPathCache &getCache()
{
static ToolPathCache *cache = new ToolPathCache;
return *cache;
}
rdcstr getToolPath(ToolDir subdir, const rdcstr &toolname, bool checkExist)
{
// search path for tools:
// 1. First look relative to the configured paths, these come from the user manually setting them
// so they always have priority.
// 2. Next we try to auto-locate it.
// - First check if the tool is in the path, assuming the user configured it to their system.
// - Otherwise check environment variables or default locations
// 3. Finally if those paths don't exist or the tool isn't found, we search relative to our
// executable looking for an android/ subfolder, and look for the tool in there.
//
// The main reason we check our bundled folder last is because adb requires a *precise* match in
// its client-server setup, so if we run our bundled adb that might be newer than the user's, they
// will then get fighting back and forth when trying to run their own.
rdcstr sdk = Android_SDKDirPath();
rdcstr jdk = Android_JDKDirPath();
ToolPathCache &cache = getCache();
// invalidate the cache when these settings change
if(sdk != cache.sdk || jdk != cache.jdk)
{
cache.paths.clear();
cache.sdk = sdk;
cache.jdk = jdk;
}
// if we have the path cached and it's still valid, return it
if(toolExists(cache.paths[toolname]))
return cache.paths[toolname];
rdcstr &toolpath = cache.paths[toolname];
// first try according to the configured paths
toolpath = getToolInSDK(subdir, jdk, sdk, toolname);
if(toolExists(toolpath))
return toolpath;
// need to try to auto-guess the tool's location
// first try in PATH
if(subdir != ToolDir::None)
{
toolpath = FileIO::FindFileInPath(toolname);
if(toolExists(toolpath))
return toolpath;
// if the tool name contains a .jar then try stripping that and look for the non-.jar version in
// the PATH.
if(toolname.contains(".jar"))
{
toolpath = strip_extension(toolname);
toolpath = FileIO::FindFileInPath(toolpath);
if(toolExists(toolpath))
return toolpath;
}
}
// now try to find it based on heuristics/environment variables
jdk = Process::GetEnvVariable("JAVA_HOME");
sdk = Process::GetEnvVariable("ANDROID_HOME");
if(sdk.empty() || !FileIO::exists(sdk))
{
sdk = Process::GetEnvVariable("ANDROID_SDK_ROOT");
}
if(sdk.empty() || !FileIO::exists(sdk))
{
sdk = Process::GetEnvVariable("ANDROID_SDK");
}
if(sdk.empty() || !FileIO::exists(sdk))
{
sdk = Process::GetEnvVariable("ANDROID_SDK_HOME");
}
#if ENABLED(RDOC_APPLE)
// on macOS it's common not to have the environment variable globally available, so try the home
// Library folder first, then the global folder
if(sdk.empty() || !FileIO::exists(sdk))
{
rdcstr librarySDK = FileIO::GetHomeFolderFilename() + "/Library/Android/sdk";
sdk = FileIO::exists(librarySDK) ? librarySDK : "";
}
if(sdk.empty() || !FileIO::exists(sdk))
{
rdcstr librarySDK = "/Library/Android/sdk";
sdk = FileIO::exists(librarySDK) ? librarySDK : "";
}
#endif
// maybe in future we can try to search in common install locations.
toolpath = getToolInSDK(subdir, jdk, sdk, toolname);
if(toolExists(toolpath))
return toolpath;
// finally try to locate it in our own distributed android subfolder
{
rdcstr libpath;
FileIO::GetLibraryFilename(libpath);
rdcstr libdir = get_dirname(FileIO::GetFullPathname(libpath));
toolpath = libdir + "/plugins/android/" + toolname;
if(toolExists(toolpath))
{
if(toolname == "adb")
{
// if we're using our own adb, we should kill the server upon shutdown
adbKillServer = true;
}
return toolpath;
}
}
toolpath = "";
// if we're checking for existence, we have failed so return empty string.
if(checkExist)
return toolpath;
// otherwise we at least return the tool name so that there's something to try and run
return toolname;
}
Process::ProcessResult execScript(const rdcstr &script, const rdcstr &args, const rdcstr &workDir,
bool silent)
{
if(!silent)
RDCLOG("SCRIPT: %s", script.c_str());
Process::ProcessResult result;
Process::LaunchScript(script, workDir, args, true, &result);