blob: a960332832126d91f68a0fb41793ea0be978bd17 [file] [log] [blame]
Tamas Berghammer00e305d2015-02-12 18:13:44 +00001//===-- HostInfoAndroid.cpp -------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Host/android/HostInfoAndroid.h"
11#include "lldb/Host/linux/HostInfoLinux.h"
Tamas Berghammerdad4db72015-03-13 11:16:08 +000012#include "llvm/ADT/SmallVector.h"
13#include "llvm/ADT/StringRef.h"
Tamas Berghammer00e305d2015-02-12 18:13:44 +000014
15using namespace lldb_private;
Tamas Berghammerdad4db72015-03-13 11:16:08 +000016using namespace llvm;
Tamas Berghammer00e305d2015-02-12 18:13:44 +000017
Kate Stoneb9c1b512016-09-06 20:57:50 +000018void HostInfoAndroid::ComputeHostArchitectureSupport(ArchSpec &arch_32,
19 ArchSpec &arch_64) {
20 HostInfoLinux::ComputeHostArchitectureSupport(arch_32, arch_64);
Tamas Berghammer00e305d2015-02-12 18:13:44 +000021
Kate Stoneb9c1b512016-09-06 20:57:50 +000022 if (arch_32.IsValid()) {
23 arch_32.GetTriple().setEnvironment(llvm::Triple::Android);
24 }
25 if (arch_64.IsValid()) {
26 arch_64.GetTriple().setEnvironment(llvm::Triple::Android);
27 }
Tamas Berghammer00e305d2015-02-12 18:13:44 +000028}
Oleksiy Vyalovfb9015d2015-02-26 02:50:14 +000029
Kate Stoneb9c1b512016-09-06 20:57:50 +000030FileSpec HostInfoAndroid::GetDefaultShell() {
31 return FileSpec("/system/bin/sh", false);
Tamas Berghammerbd051082015-03-03 12:14:45 +000032}
Tamas Berghammerdad4db72015-03-13 11:16:08 +000033
Kate Stoneb9c1b512016-09-06 20:57:50 +000034FileSpec HostInfoAndroid::ResolveLibraryPath(const std::string &module_path,
35 const ArchSpec &arch) {
36 static const char *const ld_library_path_separator = ":";
37 static const char *const default_lib32_path[] = {"/vendor/lib", "/system/lib",
38 nullptr};
39 static const char *const default_lib64_path[] = {"/vendor/lib64",
40 "/system/lib64", nullptr};
Tamas Berghammerdad4db72015-03-13 11:16:08 +000041
Kate Stoneb9c1b512016-09-06 20:57:50 +000042 if (module_path.empty() || module_path[0] == '/')
43 return FileSpec(module_path.c_str(), true);
Tamas Berghammerdad4db72015-03-13 11:16:08 +000044
Kate Stoneb9c1b512016-09-06 20:57:50 +000045 SmallVector<StringRef, 4> ld_paths;
Tamas Berghammerdad4db72015-03-13 11:16:08 +000046
Kate Stoneb9c1b512016-09-06 20:57:50 +000047 if (const char *ld_library_path = ::getenv("LD_LIBRARY_PATH"))
48 StringRef(ld_library_path)
49 .split(ld_paths, StringRef(ld_library_path_separator), -1, false);
Tamas Berghammerdad4db72015-03-13 11:16:08 +000050
Kate Stoneb9c1b512016-09-06 20:57:50 +000051 const char *const *default_lib_path = nullptr;
52 switch (arch.GetAddressByteSize()) {
53 case 4:
54 default_lib_path = default_lib32_path;
55 break;
56 case 8:
57 default_lib_path = default_lib64_path;
58 break;
59 default:
60 assert(false && "Unknown address byte size");
Tamas Berghammerdad4db72015-03-13 11:16:08 +000061 return FileSpec();
Kate Stoneb9c1b512016-09-06 20:57:50 +000062 }
63
64 for (const char *const *it = default_lib_path; *it; ++it)
65 ld_paths.push_back(StringRef(*it));
66
67 for (const StringRef &path : ld_paths) {
68 FileSpec file_candidate(path.str().c_str(), true);
69 file_candidate.AppendPathComponent(module_path.c_str());
70
71 if (file_candidate.Exists())
72 return file_candidate;
73 }
74
75 return FileSpec();
Tamas Berghammerdad4db72015-03-13 11:16:08 +000076}
Tamas Berghammerf3a24322015-05-08 12:46:26 +000077
Kate Stoneb9c1b512016-09-06 20:57:50 +000078bool HostInfoAndroid::ComputeTempFileBaseDirectory(FileSpec &file_spec) {
79 bool success = HostInfoLinux::ComputeTempFileBaseDirectory(file_spec);
Tamas Berghammerf3a24322015-05-08 12:46:26 +000080
Kate Stoneb9c1b512016-09-06 20:57:50 +000081 // On Android, there is no path which is guaranteed to be writable. If the
Adrian Prantl05097242018-04-30 16:49:04 +000082 // user has not provided a path via an environment variable, the generic
83 // algorithm will deduce /tmp, which is plain wrong. In that case we have an
84 // invalid directory, we substitute the path with /data/local/tmp, which is
85 // correct at least in some cases (i.e., when running as shell user).
Kate Stoneb9c1b512016-09-06 20:57:50 +000086 if (!success || !file_spec.Exists())
87 file_spec = FileSpec("/data/local/tmp", false);
Pavel Labath33183142015-10-16 09:32:05 +000088
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 return file_spec.Exists();
Tamas Berghammerf3a24322015-05-08 12:46:26 +000090}