blob: 886dd2aaff0d81533468ad60db70abbd23465ec3 [file] [log] [blame]
Irina Tirdea68e94f42012-10-16 02:33:38 +03001#include <stdio.h>
2#include <sys/utsname.h>
3#include "common.h"
Arnaldo Carvalho de Melo175729f2016-07-07 11:38:09 -03004#include "../util/util.h"
Irina Tirdea68e94f42012-10-16 02:33:38 +03005#include "../util/debug.h"
6
7const char *const arm_triplets[] = {
8 "arm-eabi-",
9 "arm-linux-androideabi-",
10 "arm-unknown-linux-",
11 "arm-unknown-linux-gnu-",
12 "arm-unknown-linux-gnueabi-",
Ravi Bangoria78f69b52016-06-24 17:24:00 +053013 "arm-linux-gnu-",
14 "arm-linux-gnueabihf-",
15 "arm-none-eabi-",
Irina Tirdea68e94f42012-10-16 02:33:38 +030016 NULL
17};
18
Elliott Hughesc4d2df42014-08-14 12:39:20 -070019const char *const arm64_triplets[] = {
20 "aarch64-linux-android-",
Ravi Bangoria78f69b52016-06-24 17:24:00 +053021 "aarch64-linux-gnu-",
Elliott Hughesc4d2df42014-08-14 12:39:20 -070022 NULL
23};
24
Irina Tirdea68e94f42012-10-16 02:33:38 +030025const char *const powerpc_triplets[] = {
26 "powerpc-unknown-linux-gnu-",
27 "powerpc64-unknown-linux-gnu-",
Ravi Bangoria78f69b52016-06-24 17:24:00 +053028 "powerpc64-linux-gnu-",
29 "powerpc64le-linux-gnu-",
Irina Tirdea68e94f42012-10-16 02:33:38 +030030 NULL
31};
32
33const char *const s390_triplets[] = {
34 "s390-ibm-linux-",
Ravi Bangoria78f69b52016-06-24 17:24:00 +053035 "s390x-linux-gnu-",
Irina Tirdea68e94f42012-10-16 02:33:38 +030036 NULL
37};
38
39const char *const sh_triplets[] = {
40 "sh-unknown-linux-gnu-",
41 "sh64-unknown-linux-gnu-",
Ravi Bangoria78f69b52016-06-24 17:24:00 +053042 "sh-linux-gnu-",
43 "sh64-linux-gnu-",
Irina Tirdea68e94f42012-10-16 02:33:38 +030044 NULL
45};
46
47const char *const sparc_triplets[] = {
48 "sparc-unknown-linux-gnu-",
49 "sparc64-unknown-linux-gnu-",
Ravi Bangoria78f69b52016-06-24 17:24:00 +053050 "sparc64-linux-gnu-",
Irina Tirdea68e94f42012-10-16 02:33:38 +030051 NULL
52};
53
54const char *const x86_triplets[] = {
55 "x86_64-pc-linux-gnu-",
56 "x86_64-unknown-linux-gnu-",
57 "i686-pc-linux-gnu-",
58 "i586-pc-linux-gnu-",
59 "i486-pc-linux-gnu-",
60 "i386-pc-linux-gnu-",
61 "i686-linux-android-",
62 "i686-android-linux-",
Ravi Bangoria78f69b52016-06-24 17:24:00 +053063 "x86_64-linux-gnu-",
64 "i586-linux-gnu-",
Irina Tirdea68e94f42012-10-16 02:33:38 +030065 NULL
66};
67
68const char *const mips_triplets[] = {
69 "mips-unknown-linux-gnu-",
70 "mipsel-linux-android-",
Ravi Bangoria78f69b52016-06-24 17:24:00 +053071 "mips-linux-gnu-",
72 "mips64-linux-gnu-",
73 "mips64el-linux-gnuabi64-",
74 "mips64-linux-gnuabi64-",
75 "mipsel-linux-gnu-",
Irina Tirdea68e94f42012-10-16 02:33:38 +030076 NULL
77};
78
79static bool lookup_path(char *name)
80{
81 bool found = false;
Martin Liška5bcaaca2015-05-26 11:41:37 -030082 char *path, *tmp = NULL;
Irina Tirdea68e94f42012-10-16 02:33:38 +030083 char buf[PATH_MAX];
84 char *env = getenv("PATH");
85
86 if (!env)
87 return false;
88
89 env = strdup(env);
90 if (!env)
91 return false;
92
93 path = strtok_r(env, ":", &tmp);
94 while (path) {
95 scnprintf(buf, sizeof(buf), "%s/%s", path, name);
96 if (access(buf, F_OK) == 0) {
97 found = true;
98 break;
99 }
100 path = strtok_r(NULL, ":", &tmp);
101 }
102 free(env);
103 return found;
104}
105
106static int lookup_triplets(const char *const *triplets, const char *name)
107{
108 int i;
109 char buf[PATH_MAX];
110
111 for (i = 0; triplets[i] != NULL; i++) {
112 scnprintf(buf, sizeof(buf), "%s%s", triplets[i], name);
113 if (lookup_path(buf))
114 return i;
115 }
116 return -1;
117}
118
Namhyung Kim48ed0ec2012-11-02 14:50:04 +0900119/*
120 * Return architecture name in a normalized form.
121 * The conversion logic comes from the Makefile.
122 */
He Kuang940e69872016-06-03 03:33:18 +0000123const char *normalize_arch(char *arch)
Namhyung Kim48ed0ec2012-11-02 14:50:04 +0900124{
125 if (!strcmp(arch, "x86_64"))
126 return "x86";
127 if (arch[0] == 'i' && arch[2] == '8' && arch[3] == '6')
128 return "x86";
129 if (!strcmp(arch, "sun4u") || !strncmp(arch, "sparc", 5))
130 return "sparc";
Elliott Hughesc4d2df42014-08-14 12:39:20 -0700131 if (!strcmp(arch, "aarch64") || !strcmp(arch, "arm64"))
132 return "arm64";
Namhyung Kim48ed0ec2012-11-02 14:50:04 +0900133 if (!strncmp(arch, "arm", 3) || !strcmp(arch, "sa110"))
134 return "arm";
135 if (!strncmp(arch, "s390", 4))
136 return "s390";
137 if (!strncmp(arch, "parisc", 6))
138 return "parisc";
139 if (!strncmp(arch, "powerpc", 7) || !strncmp(arch, "ppc", 3))
140 return "powerpc";
141 if (!strncmp(arch, "mips", 4))
142 return "mips";
143 if (!strncmp(arch, "sh", 2) && isdigit(arch[2]))
144 return "sh";
145
146 return arch;
147}
148
Arnaldo Carvalho de Meloeebd0bf2015-09-08 15:52:20 -0300149static int perf_env__lookup_binutils_path(struct perf_env *env,
150 const char *name, const char **path)
Irina Tirdea68e94f42012-10-16 02:33:38 +0300151{
152 int idx;
Namhyung Kim48ed0ec2012-11-02 14:50:04 +0900153 const char *arch, *cross_env;
Irina Tirdea68e94f42012-10-16 02:33:38 +0300154 struct utsname uts;
155 const char *const *path_list;
156 char *buf = NULL;
157
Namhyung Kim48ed0ec2012-11-02 14:50:04 +0900158 arch = normalize_arch(env->arch);
159
Irina Tirdea68e94f42012-10-16 02:33:38 +0300160 if (uname(&uts) < 0)
161 goto out;
162
163 /*
164 * We don't need to try to find objdump path for native system.
165 * Just use default binutils path (e.g.: "objdump").
166 */
Namhyung Kim48ed0ec2012-11-02 14:50:04 +0900167 if (!strcmp(normalize_arch(uts.machine), arch))
Irina Tirdea68e94f42012-10-16 02:33:38 +0300168 goto out;
169
170 cross_env = getenv("CROSS_COMPILE");
171 if (cross_env) {
172 if (asprintf(&buf, "%s%s", cross_env, name) < 0)
173 goto out_error;
174 if (buf[0] == '/') {
175 if (access(buf, F_OK) == 0)
176 goto out;
177 goto out_error;
178 }
179 if (lookup_path(buf))
180 goto out;
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300181 zfree(&buf);
Irina Tirdea68e94f42012-10-16 02:33:38 +0300182 }
183
Irina Tirdea68e94f42012-10-16 02:33:38 +0300184 if (!strcmp(arch, "arm"))
185 path_list = arm_triplets;
Elliott Hughesc4d2df42014-08-14 12:39:20 -0700186 else if (!strcmp(arch, "arm64"))
187 path_list = arm64_triplets;
Irina Tirdea68e94f42012-10-16 02:33:38 +0300188 else if (!strcmp(arch, "powerpc"))
189 path_list = powerpc_triplets;
190 else if (!strcmp(arch, "sh"))
191 path_list = sh_triplets;
192 else if (!strcmp(arch, "s390"))
193 path_list = s390_triplets;
194 else if (!strcmp(arch, "sparc"))
195 path_list = sparc_triplets;
Namhyung Kim48ed0ec2012-11-02 14:50:04 +0900196 else if (!strcmp(arch, "x86"))
Irina Tirdea68e94f42012-10-16 02:33:38 +0300197 path_list = x86_triplets;
198 else if (!strcmp(arch, "mips"))
199 path_list = mips_triplets;
200 else {
201 ui__error("binutils for %s not supported.\n", arch);
202 goto out_error;
203 }
204
205 idx = lookup_triplets(path_list, name);
206 if (idx < 0) {
207 ui__error("Please install %s for %s.\n"
208 "You can add it to PATH, set CROSS_COMPILE or "
209 "override the default using --%s.\n",
210 name, arch, name);
211 goto out_error;
212 }
213
214 if (asprintf(&buf, "%s%s", path_list[idx], name) < 0)
215 goto out_error;
216
217out:
218 *path = buf;
219 return 0;
220out_error:
221 free(buf);
222 *path = NULL;
223 return -1;
224}
225
Arnaldo Carvalho de Meloeebd0bf2015-09-08 15:52:20 -0300226int perf_env__lookup_objdump(struct perf_env *env)
Irina Tirdea68e94f42012-10-16 02:33:38 +0300227{
Namhyung Kimff6f7772012-11-07 10:30:15 +0900228 /*
229 * For live mode, env->arch will be NULL and we can use
230 * the native objdump tool.
231 */
232 if (env->arch == NULL)
233 return 0;
234
Arnaldo Carvalho de Meloeebd0bf2015-09-08 15:52:20 -0300235 return perf_env__lookup_binutils_path(env, "objdump", &objdump_path);
Irina Tirdea68e94f42012-10-16 02:33:38 +0300236}