blob: fb23878a1dd659fb827056d739ab443dd2b3a8b4 [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"
Jonas Devlieghere60cf3f82018-11-01 17:35:31 +000011#include "lldb/Host/FileSystem.h"
Tamas Berghammer00e305d2015-02-12 18:13:44 +000012#include "lldb/Host/linux/HostInfoLinux.h"
Tamas Berghammerdad4db72015-03-13 11:16:08 +000013#include "llvm/ADT/SmallVector.h"
14#include "llvm/ADT/StringRef.h"
Tamas Berghammer00e305d2015-02-12 18:13:44 +000015
16using namespace lldb_private;
Tamas Berghammerdad4db72015-03-13 11:16:08 +000017using namespace llvm;
Tamas Berghammer00e305d2015-02-12 18:13:44 +000018
Kate Stoneb9c1b512016-09-06 20:57:50 +000019void HostInfoAndroid::ComputeHostArchitectureSupport(ArchSpec &arch_32,
20 ArchSpec &arch_64) {
21 HostInfoLinux::ComputeHostArchitectureSupport(arch_32, arch_64);
Tamas Berghammer00e305d2015-02-12 18:13:44 +000022
Kate Stoneb9c1b512016-09-06 20:57:50 +000023 if (arch_32.IsValid()) {
24 arch_32.GetTriple().setEnvironment(llvm::Triple::Android);
25 }
26 if (arch_64.IsValid()) {
27 arch_64.GetTriple().setEnvironment(llvm::Triple::Android);
28 }
Tamas Berghammer00e305d2015-02-12 18:13:44 +000029}
Oleksiy Vyalovfb9015d2015-02-26 02:50:14 +000030
Kate Stoneb9c1b512016-09-06 20:57:50 +000031FileSpec HostInfoAndroid::GetDefaultShell() {
32 return FileSpec("/system/bin/sh", false);
Tamas Berghammerbd051082015-03-03 12:14:45 +000033}
Tamas Berghammerdad4db72015-03-13 11:16:08 +000034
Kate Stoneb9c1b512016-09-06 20:57:50 +000035FileSpec HostInfoAndroid::ResolveLibraryPath(const std::string &module_path,
36 const ArchSpec &arch) {
37 static const char *const ld_library_path_separator = ":";
38 static const char *const default_lib32_path[] = {"/vendor/lib", "/system/lib",
39 nullptr};
40 static const char *const default_lib64_path[] = {"/vendor/lib64",
41 "/system/lib64", nullptr};
Tamas Berghammerdad4db72015-03-13 11:16:08 +000042
Kate Stoneb9c1b512016-09-06 20:57:50 +000043 if (module_path.empty() || module_path[0] == '/')
44 return FileSpec(module_path.c_str(), true);
Tamas Berghammerdad4db72015-03-13 11:16:08 +000045
Kate Stoneb9c1b512016-09-06 20:57:50 +000046 SmallVector<StringRef, 4> ld_paths;
Tamas Berghammerdad4db72015-03-13 11:16:08 +000047
Kate Stoneb9c1b512016-09-06 20:57:50 +000048 if (const char *ld_library_path = ::getenv("LD_LIBRARY_PATH"))
49 StringRef(ld_library_path)
50 .split(ld_paths, StringRef(ld_library_path_separator), -1, false);
Tamas Berghammerdad4db72015-03-13 11:16:08 +000051
Kate Stoneb9c1b512016-09-06 20:57:50 +000052 const char *const *default_lib_path = nullptr;
53 switch (arch.GetAddressByteSize()) {
54 case 4:
55 default_lib_path = default_lib32_path;
56 break;
57 case 8:
58 default_lib_path = default_lib64_path;
59 break;
60 default:
61 assert(false && "Unknown address byte size");
Tamas Berghammerdad4db72015-03-13 11:16:08 +000062 return FileSpec();
Kate Stoneb9c1b512016-09-06 20:57:50 +000063 }
64
65 for (const char *const *it = default_lib_path; *it; ++it)
66 ld_paths.push_back(StringRef(*it));
67
68 for (const StringRef &path : ld_paths) {
69 FileSpec file_candidate(path.str().c_str(), true);
70 file_candidate.AppendPathComponent(module_path.c_str());
71
Jonas Devlieghere60cf3f82018-11-01 17:35:31 +000072 if (FileSystem::Instance().Exists(file_candidate))
Kate Stoneb9c1b512016-09-06 20:57:50 +000073 return file_candidate;
74 }
75
76 return FileSpec();
Tamas Berghammerdad4db72015-03-13 11:16:08 +000077}
Tamas Berghammerf3a24322015-05-08 12:46:26 +000078
Kate Stoneb9c1b512016-09-06 20:57:50 +000079bool HostInfoAndroid::ComputeTempFileBaseDirectory(FileSpec &file_spec) {
80 bool success = HostInfoLinux::ComputeTempFileBaseDirectory(file_spec);
Tamas Berghammerf3a24322015-05-08 12:46:26 +000081
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 // On Android, there is no path which is guaranteed to be writable. If the
Adrian Prantl05097242018-04-30 16:49:04 +000083 // user has not provided a path via an environment variable, the generic
84 // algorithm will deduce /tmp, which is plain wrong. In that case we have an
85 // invalid directory, we substitute the path with /data/local/tmp, which is
86 // correct at least in some cases (i.e., when running as shell user).
Jonas Devlieghere60cf3f82018-11-01 17:35:31 +000087 if (!success || !FileSystem::Instance().Exists(file_spec))
Kate Stoneb9c1b512016-09-06 20:57:50 +000088 file_spec = FileSpec("/data/local/tmp", false);
Pavel Labath33183142015-10-16 09:32:05 +000089
Jonas Devlieghere60cf3f82018-11-01 17:35:31 +000090 return FileSystem::Instance().Exists(file_spec);
Tamas Berghammerf3a24322015-05-08 12:46:26 +000091}