common.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include <stdio.h>
  2. #include <sys/utsname.h>
  3. #include "common.h"
  4. #include "../util/debug.h"
  5. const char *const arm_triplets[] = {
  6. "arm-eabi-",
  7. "arm-linux-androideabi-",
  8. "arm-unknown-linux-",
  9. "arm-unknown-linux-gnu-",
  10. "arm-unknown-linux-gnueabi-",
  11. NULL
  12. };
  13. const char *const powerpc_triplets[] = {
  14. "powerpc-unknown-linux-gnu-",
  15. "powerpc64-unknown-linux-gnu-",
  16. NULL
  17. };
  18. const char *const s390_triplets[] = {
  19. "s390-ibm-linux-",
  20. NULL
  21. };
  22. const char *const sh_triplets[] = {
  23. "sh-unknown-linux-gnu-",
  24. "sh64-unknown-linux-gnu-",
  25. NULL
  26. };
  27. const char *const sparc_triplets[] = {
  28. "sparc-unknown-linux-gnu-",
  29. "sparc64-unknown-linux-gnu-",
  30. NULL
  31. };
  32. const char *const x86_triplets[] = {
  33. "x86_64-pc-linux-gnu-",
  34. "x86_64-unknown-linux-gnu-",
  35. "i686-pc-linux-gnu-",
  36. "i586-pc-linux-gnu-",
  37. "i486-pc-linux-gnu-",
  38. "i386-pc-linux-gnu-",
  39. "i686-linux-android-",
  40. "i686-android-linux-",
  41. NULL
  42. };
  43. const char *const mips_triplets[] = {
  44. "mips-unknown-linux-gnu-",
  45. "mipsel-linux-android-",
  46. NULL
  47. };
  48. static bool lookup_path(char *name)
  49. {
  50. bool found = false;
  51. char *path, *tmp;
  52. char buf[PATH_MAX];
  53. char *env = getenv("PATH");
  54. if (!env)
  55. return false;
  56. env = strdup(env);
  57. if (!env)
  58. return false;
  59. path = strtok_r(env, ":", &tmp);
  60. while (path) {
  61. scnprintf(buf, sizeof(buf), "%s/%s", path, name);
  62. if (access(buf, F_OK) == 0) {
  63. found = true;
  64. break;
  65. }
  66. path = strtok_r(NULL, ":", &tmp);
  67. }
  68. free(env);
  69. return found;
  70. }
  71. static int lookup_triplets(const char *const *triplets, const char *name)
  72. {
  73. int i;
  74. char buf[PATH_MAX];
  75. for (i = 0; triplets[i] != NULL; i++) {
  76. scnprintf(buf, sizeof(buf), "%s%s", triplets[i], name);
  77. if (lookup_path(buf))
  78. return i;
  79. }
  80. return -1;
  81. }
  82. static int perf_session_env__lookup_binutils_path(struct perf_session_env *env,
  83. const char *name,
  84. const char **path)
  85. {
  86. int idx;
  87. char *arch, *cross_env;
  88. struct utsname uts;
  89. const char *const *path_list;
  90. char *buf = NULL;
  91. if (uname(&uts) < 0)
  92. goto out;
  93. /*
  94. * We don't need to try to find objdump path for native system.
  95. * Just use default binutils path (e.g.: "objdump").
  96. */
  97. if (!strcmp(uts.machine, env->arch))
  98. goto out;
  99. cross_env = getenv("CROSS_COMPILE");
  100. if (cross_env) {
  101. if (asprintf(&buf, "%s%s", cross_env, name) < 0)
  102. goto out_error;
  103. if (buf[0] == '/') {
  104. if (access(buf, F_OK) == 0)
  105. goto out;
  106. goto out_error;
  107. }
  108. if (lookup_path(buf))
  109. goto out;
  110. free(buf);
  111. }
  112. arch = env->arch;
  113. if (!strcmp(arch, "arm"))
  114. path_list = arm_triplets;
  115. else if (!strcmp(arch, "powerpc"))
  116. path_list = powerpc_triplets;
  117. else if (!strcmp(arch, "sh"))
  118. path_list = sh_triplets;
  119. else if (!strcmp(arch, "s390"))
  120. path_list = s390_triplets;
  121. else if (!strcmp(arch, "sparc"))
  122. path_list = sparc_triplets;
  123. else if (!strcmp(arch, "x86") || !strcmp(arch, "i386") ||
  124. !strcmp(arch, "i486") || !strcmp(arch, "i586") ||
  125. !strcmp(arch, "i686"))
  126. path_list = x86_triplets;
  127. else if (!strcmp(arch, "mips"))
  128. path_list = mips_triplets;
  129. else {
  130. ui__error("binutils for %s not supported.\n", arch);
  131. goto out_error;
  132. }
  133. idx = lookup_triplets(path_list, name);
  134. if (idx < 0) {
  135. ui__error("Please install %s for %s.\n"
  136. "You can add it to PATH, set CROSS_COMPILE or "
  137. "override the default using --%s.\n",
  138. name, arch, name);
  139. goto out_error;
  140. }
  141. if (asprintf(&buf, "%s%s", path_list[idx], name) < 0)
  142. goto out_error;
  143. out:
  144. *path = buf;
  145. return 0;
  146. out_error:
  147. free(buf);
  148. *path = NULL;
  149. return -1;
  150. }
  151. int perf_session_env__lookup_objdump(struct perf_session_env *env)
  152. {
  153. return perf_session_env__lookup_binutils_path(env, "objdump",
  154. &objdump_path);
  155. }