]> git.karo-electronics.de Git - karo-tx-linux.git/blob - tools/perf/arch/common.c
Merge branch 'perf/urgent' into perf/core, to pick up fixes before merging new changes
[karo-tx-linux.git] / tools / perf / arch / common.c
1 #include <stdio.h>
2 #include <sys/utsname.h>
3 #include "common.h"
4 #include "../util/debug.h"
5
6 const char *const arm_triplets[] = {
7         "arm-eabi-",
8         "arm-linux-androideabi-",
9         "arm-unknown-linux-",
10         "arm-unknown-linux-gnu-",
11         "arm-unknown-linux-gnueabi-",
12         "arm-linux-gnu-",
13         "arm-linux-gnueabihf-",
14         "arm-none-eabi-",
15         NULL
16 };
17
18 const char *const arm64_triplets[] = {
19         "aarch64-linux-android-",
20         "aarch64-linux-gnu-",
21         NULL
22 };
23
24 const char *const powerpc_triplets[] = {
25         "powerpc-unknown-linux-gnu-",
26         "powerpc64-unknown-linux-gnu-",
27         "powerpc64-linux-gnu-",
28         "powerpc64le-linux-gnu-",
29         NULL
30 };
31
32 const char *const s390_triplets[] = {
33         "s390-ibm-linux-",
34         "s390x-linux-gnu-",
35         NULL
36 };
37
38 const char *const sh_triplets[] = {
39         "sh-unknown-linux-gnu-",
40         "sh64-unknown-linux-gnu-",
41         "sh-linux-gnu-",
42         "sh64-linux-gnu-",
43         NULL
44 };
45
46 const char *const sparc_triplets[] = {
47         "sparc-unknown-linux-gnu-",
48         "sparc64-unknown-linux-gnu-",
49         "sparc64-linux-gnu-",
50         NULL
51 };
52
53 const char *const x86_triplets[] = {
54         "x86_64-pc-linux-gnu-",
55         "x86_64-unknown-linux-gnu-",
56         "i686-pc-linux-gnu-",
57         "i586-pc-linux-gnu-",
58         "i486-pc-linux-gnu-",
59         "i386-pc-linux-gnu-",
60         "i686-linux-android-",
61         "i686-android-linux-",
62         "x86_64-linux-gnu-",
63         "i586-linux-gnu-",
64         NULL
65 };
66
67 const char *const mips_triplets[] = {
68         "mips-unknown-linux-gnu-",
69         "mipsel-linux-android-",
70         "mips-linux-gnu-",
71         "mips64-linux-gnu-",
72         "mips64el-linux-gnuabi64-",
73         "mips64-linux-gnuabi64-",
74         "mipsel-linux-gnu-",
75         NULL
76 };
77
78 static bool lookup_path(char *name)
79 {
80         bool found = false;
81         char *path, *tmp = NULL;
82         char buf[PATH_MAX];
83         char *env = getenv("PATH");
84
85         if (!env)
86                 return false;
87
88         env = strdup(env);
89         if (!env)
90                 return false;
91
92         path = strtok_r(env, ":", &tmp);
93         while (path) {
94                 scnprintf(buf, sizeof(buf), "%s/%s", path, name);
95                 if (access(buf, F_OK) == 0) {
96                         found = true;
97                         break;
98                 }
99                 path = strtok_r(NULL, ":", &tmp);
100         }
101         free(env);
102         return found;
103 }
104
105 static int lookup_triplets(const char *const *triplets, const char *name)
106 {
107         int i;
108         char buf[PATH_MAX];
109
110         for (i = 0; triplets[i] != NULL; i++) {
111                 scnprintf(buf, sizeof(buf), "%s%s", triplets[i], name);
112                 if (lookup_path(buf))
113                         return i;
114         }
115         return -1;
116 }
117
118 /*
119  * Return architecture name in a normalized form.
120  * The conversion logic comes from the Makefile.
121  */
122 const char *normalize_arch(char *arch)
123 {
124         if (!strcmp(arch, "x86_64"))
125                 return "x86";
126         if (arch[0] == 'i' && arch[2] == '8' && arch[3] == '6')
127                 return "x86";
128         if (!strcmp(arch, "sun4u") || !strncmp(arch, "sparc", 5))
129                 return "sparc";
130         if (!strcmp(arch, "aarch64") || !strcmp(arch, "arm64"))
131                 return "arm64";
132         if (!strncmp(arch, "arm", 3) || !strcmp(arch, "sa110"))
133                 return "arm";
134         if (!strncmp(arch, "s390", 4))
135                 return "s390";
136         if (!strncmp(arch, "parisc", 6))
137                 return "parisc";
138         if (!strncmp(arch, "powerpc", 7) || !strncmp(arch, "ppc", 3))
139                 return "powerpc";
140         if (!strncmp(arch, "mips", 4))
141                 return "mips";
142         if (!strncmp(arch, "sh", 2) && isdigit(arch[2]))
143                 return "sh";
144
145         return arch;
146 }
147
148 static int perf_env__lookup_binutils_path(struct perf_env *env,
149                                           const char *name, const char **path)
150 {
151         int idx;
152         const char *arch, *cross_env;
153         struct utsname uts;
154         const char *const *path_list;
155         char *buf = NULL;
156
157         arch = normalize_arch(env->arch);
158
159         if (uname(&uts) < 0)
160                 goto out;
161
162         /*
163          * We don't need to try to find objdump path for native system.
164          * Just use default binutils path (e.g.: "objdump").
165          */
166         if (!strcmp(normalize_arch(uts.machine), arch))
167                 goto out;
168
169         cross_env = getenv("CROSS_COMPILE");
170         if (cross_env) {
171                 if (asprintf(&buf, "%s%s", cross_env, name) < 0)
172                         goto out_error;
173                 if (buf[0] == '/') {
174                         if (access(buf, F_OK) == 0)
175                                 goto out;
176                         goto out_error;
177                 }
178                 if (lookup_path(buf))
179                         goto out;
180                 zfree(&buf);
181         }
182
183         if (!strcmp(arch, "arm"))
184                 path_list = arm_triplets;
185         else if (!strcmp(arch, "arm64"))
186                 path_list = arm64_triplets;
187         else if (!strcmp(arch, "powerpc"))
188                 path_list = powerpc_triplets;
189         else if (!strcmp(arch, "sh"))
190                 path_list = sh_triplets;
191         else if (!strcmp(arch, "s390"))
192                 path_list = s390_triplets;
193         else if (!strcmp(arch, "sparc"))
194                 path_list = sparc_triplets;
195         else if (!strcmp(arch, "x86"))
196                 path_list = x86_triplets;
197         else if (!strcmp(arch, "mips"))
198                 path_list = mips_triplets;
199         else {
200                 ui__error("binutils for %s not supported.\n", arch);
201                 goto out_error;
202         }
203
204         idx = lookup_triplets(path_list, name);
205         if (idx < 0) {
206                 ui__error("Please install %s for %s.\n"
207                           "You can add it to PATH, set CROSS_COMPILE or "
208                           "override the default using --%s.\n",
209                           name, arch, name);
210                 goto out_error;
211         }
212
213         if (asprintf(&buf, "%s%s", path_list[idx], name) < 0)
214                 goto out_error;
215
216 out:
217         *path = buf;
218         return 0;
219 out_error:
220         free(buf);
221         *path = NULL;
222         return -1;
223 }
224
225 int perf_env__lookup_objdump(struct perf_env *env)
226 {
227         /*
228          * For live mode, env->arch will be NULL and we can use
229          * the native objdump tool.
230          */
231         if (env->arch == NULL)
232                 return 0;
233
234         return perf_env__lookup_binutils_path(env, "objdump", &objdump_path);
235 }