1 //===-- 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"
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/ADT/StringRef.h"
14 
15 using namespace lldb_private;
16 using namespace llvm;
17 
18 void
19 HostInfoAndroid::ComputeHostArchitectureSupport(ArchSpec &arch_32, ArchSpec &arch_64)
20 {
21     HostInfoLinux::ComputeHostArchitectureSupport(arch_32, arch_64);
22 
23     if (arch_32.IsValid())
24     {
25         arch_32.GetTriple().setEnvironment(llvm::Triple::Android);
26     }
27     if (arch_64.IsValid())
28     {
29         arch_64.GetTriple().setEnvironment(llvm::Triple::Android);
30     }
31 }
32 
33 FileSpec
34 HostInfoAndroid::GetDefaultShell()
35 {
36     return FileSpec("/system/bin/sh", false);
37 }
38 
39 FileSpec
40 HostInfoAndroid::ResolveLibraryPath(const std::string& module_path, const ArchSpec& arch)
41 {
42     static const char* const ld_library_path_separator = ":";
43     static const char* const default_lib32_path[] = {
44         "/vendor/lib",
45         "/system/lib",
46         nullptr
47     };
48     static const char* const default_lib64_path[] = {
49         "/vendor/lib64",
50         "/system/lib64",
51         nullptr
52     };
53 
54     if (module_path.empty() || module_path[0] == '/')
55         return FileSpec(module_path.c_str(), true);
56 
57     SmallVector<StringRef, 4> ld_paths;
58 
59     if (const char* ld_library_path = ::getenv("LD_LIBRARY_PATH"))
60         StringRef(ld_library_path).split(ld_paths, StringRef(ld_library_path_separator), -1, false);
61 
62     const char* const* default_lib_path = nullptr;
63     switch (arch.GetAddressByteSize())
64     {
65         case 4:
66             default_lib_path = default_lib32_path;
67             break;
68         case 8:
69             default_lib_path = default_lib64_path;
70             break;
71         default:
72             assert(false && "Unknown address byte size");
73             return FileSpec();
74     }
75 
76     for(const char* const* it = default_lib_path; *it; ++it)
77         ld_paths.push_back(StringRef(*it));
78 
79     for (const StringRef& path : ld_paths)
80     {
81         FileSpec file_candidate(path.str().c_str(), true);
82         file_candidate.AppendPathComponent(module_path.c_str());
83 
84         if (file_candidate.Exists())
85             return file_candidate;
86     }
87 
88     return FileSpec();
89 }
90 
91 bool
92 HostInfoAndroid::ComputeTempFileBaseDirectory(FileSpec &file_spec)
93 {
94     if (HostInfoLinux::ComputeTempFileBaseDirectory(file_spec))
95         return true;
96 
97     // If the default mechanism for computing the temp directory failed then
98     // fall back to /data/local/tmp
99     file_spec = FileSpec("/data/local/tmp", false);
100     return true;
101 }
102