2 #include <sys/utsname.h>
4 #include "../util/debug.h"
6 const char *const arm_triplets
[] = {
8 "arm-linux-androideabi-",
10 "arm-unknown-linux-gnu-",
11 "arm-unknown-linux-gnueabi-",
15 const char *const arm64_triplets
[] = {
16 "aarch64-linux-android-",
20 const char *const powerpc_triplets
[] = {
21 "powerpc-unknown-linux-gnu-",
22 "powerpc64-unknown-linux-gnu-",
26 const char *const s390_triplets
[] = {
31 const char *const sh_triplets
[] = {
32 "sh-unknown-linux-gnu-",
33 "sh64-unknown-linux-gnu-",
37 const char *const sparc_triplets
[] = {
38 "sparc-unknown-linux-gnu-",
39 "sparc64-unknown-linux-gnu-",
43 const char *const x86_triplets
[] = {
44 "x86_64-pc-linux-gnu-",
45 "x86_64-unknown-linux-gnu-",
50 "i686-linux-android-",
51 "i686-android-linux-",
55 const char *const mips_triplets
[] = {
56 "mips-unknown-linux-gnu-",
57 "mipsel-linux-android-",
61 static bool lookup_path(char *name
)
64 char *path
, *tmp
= NULL
;
66 char *env
= getenv("PATH");
75 path
= strtok_r(env
, ":", &tmp
);
77 scnprintf(buf
, sizeof(buf
), "%s/%s", path
, name
);
78 if (access(buf
, F_OK
) == 0) {
82 path
= strtok_r(NULL
, ":", &tmp
);
88 static int lookup_triplets(const char *const *triplets
, const char *name
)
93 for (i
= 0; triplets
[i
] != NULL
; i
++) {
94 scnprintf(buf
, sizeof(buf
), "%s%s", triplets
[i
], name
);
102 * Return architecture name in a normalized form.
103 * The conversion logic comes from the Makefile.
105 static const char *normalize_arch(char *arch
)
107 if (!strcmp(arch
, "x86_64"))
109 if (arch
[0] == 'i' && arch
[2] == '8' && arch
[3] == '6')
111 if (!strcmp(arch
, "sun4u") || !strncmp(arch
, "sparc", 5))
113 if (!strcmp(arch
, "aarch64") || !strcmp(arch
, "arm64"))
115 if (!strncmp(arch
, "arm", 3) || !strcmp(arch
, "sa110"))
117 if (!strncmp(arch
, "s390", 4))
119 if (!strncmp(arch
, "parisc", 6))
121 if (!strncmp(arch
, "powerpc", 7) || !strncmp(arch
, "ppc", 3))
123 if (!strncmp(arch
, "mips", 4))
125 if (!strncmp(arch
, "sh", 2) && isdigit(arch
[2]))
131 static int perf_env__lookup_binutils_path(struct perf_env
*env
,
132 const char *name
, const char **path
)
135 const char *arch
, *cross_env
;
137 const char *const *path_list
;
140 arch
= normalize_arch(env
->arch
);
146 * We don't need to try to find objdump path for native system.
147 * Just use default binutils path (e.g.: "objdump").
149 if (!strcmp(normalize_arch(uts
.machine
), arch
))
152 cross_env
= getenv("CROSS_COMPILE");
154 if (asprintf(&buf
, "%s%s", cross_env
, name
) < 0)
157 if (access(buf
, F_OK
) == 0)
161 if (lookup_path(buf
))
166 if (!strcmp(arch
, "arm"))
167 path_list
= arm_triplets
;
168 else if (!strcmp(arch
, "arm64"))
169 path_list
= arm64_triplets
;
170 else if (!strcmp(arch
, "powerpc"))
171 path_list
= powerpc_triplets
;
172 else if (!strcmp(arch
, "sh"))
173 path_list
= sh_triplets
;
174 else if (!strcmp(arch
, "s390"))
175 path_list
= s390_triplets
;
176 else if (!strcmp(arch
, "sparc"))
177 path_list
= sparc_triplets
;
178 else if (!strcmp(arch
, "x86"))
179 path_list
= x86_triplets
;
180 else if (!strcmp(arch
, "mips"))
181 path_list
= mips_triplets
;
183 ui__error("binutils for %s not supported.\n", arch
);
187 idx
= lookup_triplets(path_list
, name
);
189 ui__error("Please install %s for %s.\n"
190 "You can add it to PATH, set CROSS_COMPILE or "
191 "override the default using --%s.\n",
196 if (asprintf(&buf
, "%s%s", path_list
[idx
], name
) < 0)
208 int perf_env__lookup_objdump(struct perf_env
*env
)
211 * For live mode, env->arch will be NULL and we can use
212 * the native objdump tool.
214 if (env
->arch
== NULL
)
217 return perf_env__lookup_binutils_path(env
, "objdump", &objdump_path
);