1 // SPDX-License-Identifier: GPL-2.0
3 #include <sys/utsname.h>
5 #include "../util/util.h"
6 #include "../util/debug.h"
8 #include "sane_ctype.h"
10 const char *const arm_triplets
[] = {
12 "arm-linux-androideabi-",
14 "arm-unknown-linux-gnu-",
15 "arm-unknown-linux-gnueabi-",
17 "arm-linux-gnueabihf-",
22 const char *const arm64_triplets
[] = {
23 "aarch64-linux-android-",
28 const char *const powerpc_triplets
[] = {
29 "powerpc-unknown-linux-gnu-",
31 "powerpc64-unknown-linux-gnu-",
32 "powerpc64-linux-gnu-",
33 "powerpc64le-linux-gnu-",
37 const char *const s390_triplets
[] = {
43 const char *const sh_triplets
[] = {
44 "sh-unknown-linux-gnu-",
45 "sh64-unknown-linux-gnu-",
51 const char *const sparc_triplets
[] = {
52 "sparc-unknown-linux-gnu-",
53 "sparc64-unknown-linux-gnu-",
58 const char *const x86_triplets
[] = {
59 "x86_64-pc-linux-gnu-",
60 "x86_64-unknown-linux-gnu-",
65 "i686-linux-android-",
66 "i686-android-linux-",
72 const char *const mips_triplets
[] = {
73 "mips-unknown-linux-gnu-",
74 "mipsel-linux-android-",
77 "mips64el-linux-gnuabi64-",
78 "mips64-linux-gnuabi64-",
83 static bool lookup_path(char *name
)
86 char *path
, *tmp
= NULL
;
88 char *env
= getenv("PATH");
97 path
= strtok_r(env
, ":", &tmp
);
99 scnprintf(buf
, sizeof(buf
), "%s/%s", path
, name
);
100 if (access(buf
, F_OK
) == 0) {
104 path
= strtok_r(NULL
, ":", &tmp
);
110 static int lookup_triplets(const char *const *triplets
, const char *name
)
115 for (i
= 0; triplets
[i
] != NULL
; i
++) {
116 scnprintf(buf
, sizeof(buf
), "%s%s", triplets
[i
], name
);
117 if (lookup_path(buf
))
124 * Return architecture name in a normalized form.
125 * The conversion logic comes from the Makefile.
127 const char *normalize_arch(char *arch
)
129 if (!strcmp(arch
, "x86_64"))
131 if (arch
[0] == 'i' && arch
[2] == '8' && arch
[3] == '6')
133 if (!strcmp(arch
, "sun4u") || !strncmp(arch
, "sparc", 5))
135 if (!strcmp(arch
, "aarch64") || !strcmp(arch
, "arm64"))
137 if (!strncmp(arch
, "arm", 3) || !strcmp(arch
, "sa110"))
139 if (!strncmp(arch
, "s390", 4))
141 if (!strncmp(arch
, "parisc", 6))
143 if (!strncmp(arch
, "powerpc", 7) || !strncmp(arch
, "ppc", 3))
145 if (!strncmp(arch
, "mips", 4))
147 if (!strncmp(arch
, "sh", 2) && isdigit(arch
[2]))
153 static int perf_env__lookup_binutils_path(struct perf_env
*env
,
154 const char *name
, const char **path
)
157 const char *arch
, *cross_env
;
159 const char *const *path_list
;
162 arch
= normalize_arch(env
->arch
);
168 * We don't need to try to find objdump path for native system.
169 * Just use default binutils path (e.g.: "objdump").
171 if (!strcmp(normalize_arch(uts
.machine
), arch
))
174 cross_env
= getenv("CROSS_COMPILE");
176 if (asprintf(&buf
, "%s%s", cross_env
, name
) < 0)
179 if (access(buf
, F_OK
) == 0)
183 if (lookup_path(buf
))
188 if (!strcmp(arch
, "arm"))
189 path_list
= arm_triplets
;
190 else if (!strcmp(arch
, "arm64"))
191 path_list
= arm64_triplets
;
192 else if (!strcmp(arch
, "powerpc"))
193 path_list
= powerpc_triplets
;
194 else if (!strcmp(arch
, "sh"))
195 path_list
= sh_triplets
;
196 else if (!strcmp(arch
, "s390"))
197 path_list
= s390_triplets
;
198 else if (!strcmp(arch
, "sparc"))
199 path_list
= sparc_triplets
;
200 else if (!strcmp(arch
, "x86"))
201 path_list
= x86_triplets
;
202 else if (!strcmp(arch
, "mips"))
203 path_list
= mips_triplets
;
205 ui__error("binutils for %s not supported.\n", arch
);
209 idx
= lookup_triplets(path_list
, name
);
211 ui__error("Please install %s for %s.\n"
212 "You can add it to PATH, set CROSS_COMPILE or "
213 "override the default using --%s.\n",
218 if (asprintf(&buf
, "%s%s", path_list
[idx
], name
) < 0)
230 int perf_env__lookup_objdump(struct perf_env
*env
)
233 * For live mode, env->arch will be NULL and we can use
234 * the native objdump tool.
236 if (env
->arch
== NULL
)
239 return perf_env__lookup_binutils_path(env
, "objdump", &objdump_path
);