]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - source/Host/android/HostInfoAndroid.cpp
Vendor import of lldb trunk r256945:
[FreeBSD/FreeBSD.git] / source / Host / android / HostInfoAndroid.cpp
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     bool success = HostInfoLinux::ComputeTempFileBaseDirectory(file_spec);
95
96     // On Android, there is no path which is guaranteed to be writable. If the user has not
97     // provided a path via an environment variable, the generic algorithm will deduce /tmp, which
98     // is plain wrong. In that case we have an invalid directory, we substitute the path with
99     // /data/local/tmp, which is correct at least in some cases (i.e., when running as shell user).
100     if (!success || !file_spec.Exists())
101         file_spec = FileSpec("/data/local/tmp", false);
102
103     return file_spec.Exists();
104 }