blob: ee6966812a5a4bfb7d0eb0ffe169b5935f23f308 [file] [log] [blame]
Irina Tirdea68e94f42012-10-16 02:33:38 +03001#include <stdio.h>
2#include <sys/utsname.h>
3#include "common.h"
4#include "../util/debug.h"
5
6const 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-",
Ravi Bangoria78f69b52016-06-24 17:24:00 +053012 "arm-linux-gnu-",
13 "arm-linux-gnueabihf-",
14 "arm-none-eabi-",
Irina Tirdea68e94f42012-10-16 02:33:38 +030015 NULL
16};
17
Elliott Hughesc4d2df42014-08-14 12:39:20 -070018const char *const arm64_triplets[] = {
19 "aarch64-linux-android-",
Ravi Bangoria78f69b52016-06-24 17:24:00 +053020 "aarch64-linux-gnu-",
Elliott Hughesc4d2df42014-08-14 12:39:20 -070021 NULL
22};
23
Irina Tirdea68e94f42012-10-16 02:33:38 +030024const char *const powerpc_triplets[] = {
25 "powerpc-unknown-linux-gnu-",
26 "powerpc64-unknown-linux-gnu-",
Ravi Bangoria78f69b52016-06-24 17:24:00 +053027 "powerpc64-linux-gnu-",
28 "powerpc64le-linux-gnu-",
Irina Tirdea68e94f42012-10-16 02:33:38 +030029 NULL
30};
31
32const char *const s390_triplets[] = {
33 "s390-ibm-linux-",
Ravi Bangoria78f69b52016-06-24 17:24:00 +053034 "s390x-linux-gnu-",
Irina Tirdea68e94f42012-10-16 02:33:38 +030035 NULL
36};
37
38const char *const sh_triplets[] = {
39 "sh-unknown-linux-gnu-",
40 "sh64-unknown-linux-gnu-",
Ravi Bangoria78f69b52016-06-24 17:24:00 +053041 "sh-linux-gnu-",
42 "sh64-linux-gnu-",
Irina Tirdea68e94f42012-10-16 02:33:38 +030043 NULL
44};
45
46const char *const sparc_triplets[] = {
47 "sparc-unknown-linux-gnu-",
48 "sparc64-unknown-linux-gnu-",
Ravi Bangoria78f69b52016-06-24 17:24:00 +053049 "sparc64-linux-gnu-",
Irina Tirdea68e94f42012-10-16 02:33:38 +030050 NULL
51};
52
53const 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-",
Ravi Bangoria78f69b52016-06-24 17:24:00 +053062 "x86_64-linux-gnu-",
63 "i586-linux-gnu-",
Irina Tirdea68e94f42012-10-16 02:33:38 +030064 NULL
65};
66
67const char *const mips_triplets[] = {
68 "mips-unknown-linux-gnu-",
69 "mipsel-linux-android-",
Ravi Bangoria78f69b52016-06-24 17:24:00 +053070 "mips-linux-gnu-",
71 "mips64-linux-gnu-",
72 "mips64el-linux-gnuabi64-",
73 "mips64-linux-gnuabi64-",
74 "mipsel-linux-gnu-",
Irina Tirdea68e94f42012-10-16 02:33:38 +030075 NULL
76};
77
78static bool lookup_path(char *name)
79{
80 bool found = false;
Martin Liška5bcaaca2015-05-26 11:41:37 -030081 char *path, *tmp = NULL;
Irina Tirdea68e94f42012-10-16 02:33:38 +030082 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
105static 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
Namhyung Kim48ed0ec2012-11-02 14:50:04 +0900118/*
119 * Return architecture name in a normalized form.
120 * The conversion logic comes from the Makefile.
121 */
He Kuang940e69872016-06-03 03:33:18 +0000122const char *normalize_arch(char *arch)
Namhyung Kim48ed0ec2012-11-02 14:50:04 +0900123{
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";
Elliott Hughesc4d2df42014-08-14 12:39:20 -0700130 if (!strcmp(arch, "aarch64") || !strcmp(arch, "arm64"))
131 return "arm64";
Namhyung Kim48ed0ec2012-11-02 14:50:04 +0900132 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
Arnaldo Carvalho de Meloeebd0bf2015-09-08 15:52:20 -0300148static int perf_env__lookup_binutils_path(struct perf_env *env,
149 const char *name, const char **path)
Irina Tirdea68e94f42012-10-16 02:33:38 +0300150{
151 int idx;
Namhyung Kim48ed0ec2012-11-02 14:50:04 +0900152 const char *arch, *cross_env;
Irina Tirdea68e94f42012-10-16 02:33:38 +0300153 struct utsname uts;
154 const char *const *path_list;
155 char *buf = NULL;
156
Namhyung Kim48ed0ec2012-11-02 14:50:04 +0900157 arch = normalize_arch(env->arch);
158
Irina Tirdea68e94f42012-10-16 02:33:38 +0300159 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 */
Namhyung Kim48ed0ec2012-11-02 14:50:04 +0900166 if (!strcmp(normalize_arch(uts.machine), arch))
Irina Tirdea68e94f42012-10-16 02:33:38 +0300167 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;
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300180 zfree(&buf);
Irina Tirdea68e94f42012-10-16 02:33:38 +0300181 }
182
Irina Tirdea68e94f42012-10-16 02:33:38 +0300183 if (!strcmp(arch, "arm"))
184 path_list = arm_triplets;
Elliott Hughesc4d2df42014-08-14 12:39:20 -0700185 else if (!strcmp(arch, "arm64"))
186 path_list = arm64_triplets;
Irina Tirdea68e94f42012-10-16 02:33:38 +0300187 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;
Namhyung Kim48ed0ec2012-11-02 14:50:04 +0900195 else if (!strcmp(arch, "x86"))
Irina Tirdea68e94f42012-10-16 02:33:38 +0300196 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
216out:
217 *path = buf;
218 return 0;
219out_error:
220 free(buf);
221 *path = NULL;
222 return -1;
223}
224
Arnaldo Carvalho de Meloeebd0bf2015-09-08 15:52:20 -0300225int perf_env__lookup_objdump(struct perf_env *env)
Irina Tirdea68e94f42012-10-16 02:33:38 +0300226{
Namhyung Kimff6f7772012-11-07 10:30:15 +0900227 /*
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
Arnaldo Carvalho de Meloeebd0bf2015-09-08 15:52:20 -0300234 return perf_env__lookup_binutils_path(env, "objdump", &objdump_path);
Irina Tirdea68e94f42012-10-16 02:33:38 +0300235}