2 #include <sys/utsname.h>
4 #include "../util/util.h"
5 #include "../util/debug.h"
7 const char *const arm_triplets
[] = {
9 "arm-linux-androideabi-",
11 "arm-unknown-linux-gnu-",
12 "arm-unknown-linux-gnueabi-",
14 "arm-linux-gnueabihf-",
19 const char *const arm64_triplets
[] = {
20 "aarch64-linux-android-",
25 const char *const powerpc_triplets
[] = {
26 "powerpc-unknown-linux-gnu-",
27 "powerpc64-unknown-linux-gnu-",
28 "powerpc64-linux-gnu-",
29 "powerpc64le-linux-gnu-",
33 const char *const s390_triplets
[] = {
39 const char *const sh_triplets
[] = {
40 "sh-unknown-linux-gnu-",
41 "sh64-unknown-linux-gnu-",
47 const char *const sparc_triplets
[] = {
48 "sparc-unknown-linux-gnu-",
49 "sparc64-unknown-linux-gnu-",
54 const char *const x86_triplets
[] = {
55 "x86_64-pc-linux-gnu-",
56 "x86_64-unknown-linux-gnu-",
61 "i686-linux-android-",
62 "i686-android-linux-",
68 const char *const mips_triplets
[] = {
69 "mips-unknown-linux-gnu-",
70 "mipsel-linux-android-",
73 "mips64el-linux-gnuabi64-",
74 "mips64-linux-gnuabi64-",
79 static bool lookup_path(char *name
)
82 char *path
, *tmp
= NULL
;
84 char *env
= getenv("PATH");
93 path
= strtok_r(env
, ":", &tmp
);
95 scnprintf(buf
, sizeof(buf
), "%s/%s", path
, name
);
96 if (access(buf
, F_OK
) == 0) {
100 path
= strtok_r(NULL
, ":", &tmp
);
106 static int lookup_triplets(const char *const *triplets
, const char *name
)
111 for (i
= 0; triplets
[i
] != NULL
; i
++) {
112 scnprintf(buf
, sizeof(buf
), "%s%s", triplets
[i
], name
);
113 if (lookup_path(buf
))
120 * Return architecture name in a normalized form.
121 * The conversion logic comes from the Makefile.
123 const char *normalize_arch(char *arch
)
125 if (!strcmp(arch
, "x86_64"))
127 if (arch
[0] == 'i' && arch
[2] == '8' && arch
[3] == '6')
129 if (!strcmp(arch
, "sun4u") || !strncmp(arch
, "sparc", 5))
131 if (!strcmp(arch
, "aarch64") || !strcmp(arch
, "arm64"))
133 if (!strncmp(arch
, "arm", 3) || !strcmp(arch
, "sa110"))
135 if (!strncmp(arch
, "s390", 4))
137 if (!strncmp(arch
, "parisc", 6))
139 if (!strncmp(arch
, "powerpc", 7) || !strncmp(arch
, "ppc", 3))
141 if (!strncmp(arch
, "mips", 4))
143 if (!strncmp(arch
, "sh", 2) && isdigit(arch
[2]))
149 static int perf_env__lookup_binutils_path(struct perf_env
*env
,
150 const char *name
, const char **path
)
153 const char *arch
, *cross_env
;
155 const char *const *path_list
;
158 arch
= normalize_arch(env
->arch
);
164 * We don't need to try to find objdump path for native system.
165 * Just use default binutils path (e.g.: "objdump").
167 if (!strcmp(normalize_arch(uts
.machine
), arch
))
170 cross_env
= getenv("CROSS_COMPILE");
172 if (asprintf(&buf
, "%s%s", cross_env
, name
) < 0)
175 if (access(buf
, F_OK
) == 0)
179 if (lookup_path(buf
))
184 if (!strcmp(arch
, "arm"))
185 path_list
= arm_triplets
;
186 else if (!strcmp(arch
, "arm64"))
187 path_list
= arm64_triplets
;
188 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
;
196 else if (!strcmp(arch
, "x86"))
197 path_list
= x86_triplets
;
198 else if (!strcmp(arch
, "mips"))
199 path_list
= mips_triplets
;
201 ui__error("binutils for %s not supported.\n", arch
);
205 idx
= lookup_triplets(path_list
, name
);
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",
214 if (asprintf(&buf
, "%s%s", path_list
[idx
], name
) < 0)
226 int perf_env__lookup_objdump(struct perf_env
*env
)
229 * For live mode, env->arch will be NULL and we can use
230 * the native objdump tool.
232 if (env
->arch
== NULL
)
235 return perf_env__lookup_binutils_path(env
, "objdump", &objdump_path
);