ext-toolchain-wrapper.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * Buildroot wrapper for external toolchains. This simply executes the real
  3. * toolchain with a number of arguments (sysroot/arch/..) hardcoded,
  4. * to ensure the external toolchain uses the correct configuration.
  5. * The hardcoded path arguments are defined relative to the actual location
  6. * of the binary.
  7. *
  8. * (C) 2011 Peter Korsgaard <jacmet@sunsite.dk>
  9. * (C) 2011 Daniel Nyström <daniel.nystrom@timeterminal.se>
  10. * (C) 2012 Arnout Vandecappelle (Essensium/Mind) <arnout@mind.be>
  11. * (C) 2013 Spenser Gilliland <spenser@gillilanding.com>
  12. *
  13. * This file is licensed under the terms of the GNU General Public License
  14. * version 2. This program is licensed "as is" without any warranty of any
  15. * kind, whether express or implied.
  16. */
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <limits.h>
  20. #include <unistd.h>
  21. #include <stdlib.h>
  22. static char path[PATH_MAX];
  23. static char sysroot[PATH_MAX];
  24. /**
  25. * GCC errors out with certain combinations of arguments (examples are
  26. * -mfloat-abi={hard|soft} and -m{little|big}-endian), so we have to ensure
  27. * that we only pass the predefined one to the real compiler if the inverse
  28. * option isn't in the argument list.
  29. * This specifies the worst case number of extra arguments we might pass
  30. * Currently, we have:
  31. * -mfloat-abi=
  32. * -march=
  33. * -mcpu=
  34. */
  35. #define EXCLUSIVE_ARGS 3
  36. static char *predef_args[] = {
  37. path,
  38. "--sysroot", sysroot,
  39. #ifdef BR_ABI
  40. "-mabi=" BR_ABI,
  41. #endif
  42. #ifdef BR_FPU
  43. "-mfpu=" BR_FPU,
  44. #endif
  45. #ifdef BR_SOFTFLOAT
  46. "-msoft-float",
  47. #endif /* BR_SOFTFLOAT */
  48. #ifdef BR_MODE
  49. "-m" BR_MODE,
  50. #endif
  51. #ifdef BR_64
  52. "-m64",
  53. #endif
  54. #ifdef BR_BINFMT_FLAT
  55. "-Wl,-elf2flt",
  56. #endif
  57. #ifdef BR_MIPS_TARGET_LITTLE_ENDIAN
  58. "-EL",
  59. #endif
  60. #ifdef BR_MIPS_TARGET_BIG_ENDIAN
  61. "-EB",
  62. #endif
  63. #ifdef BR_ADDITIONAL_CFLAGS
  64. BR_ADDITIONAL_CFLAGS
  65. #endif
  66. };
  67. int main(int argc, char **argv)
  68. {
  69. char **args, **cur;
  70. char *relbasedir, *absbasedir;
  71. char *progpath = argv[0];
  72. char *basename;
  73. char *env_debug;
  74. int ret, i, count = 0, debug;
  75. /* Calculate the relative paths */
  76. basename = strrchr(progpath, '/');
  77. if (basename) {
  78. *basename = '\0';
  79. basename++;
  80. relbasedir = malloc(strlen(progpath) + 7);
  81. if (relbasedir == NULL) {
  82. perror(__FILE__ ": malloc");
  83. return 2;
  84. }
  85. sprintf(relbasedir, "%s/../..", argv[0]);
  86. absbasedir = realpath(relbasedir, NULL);
  87. } else {
  88. basename = progpath;
  89. absbasedir = malloc(PATH_MAX + 1);
  90. ret = readlink("/proc/self/exe", absbasedir, PATH_MAX);
  91. if (ret < 0) {
  92. perror(__FILE__ ": readlink");
  93. return 2;
  94. }
  95. absbasedir[ret] = '\0';
  96. for (i = ret; i > 0; i--) {
  97. if (absbasedir[i] == '/') {
  98. absbasedir[i] = '\0';
  99. if (++count == 3)
  100. break;
  101. }
  102. }
  103. }
  104. if (absbasedir == NULL) {
  105. perror(__FILE__ ": realpath");
  106. return 2;
  107. }
  108. /* Fill in the relative paths */
  109. #ifdef BR_CROSS_PATH_REL
  110. ret = snprintf(path, sizeof(path), "%s/" BR_CROSS_PATH_REL "/%s", absbasedir, basename);
  111. #else /* BR_CROSS_PATH_ABS */
  112. ret = snprintf(path, sizeof(path), BR_CROSS_PATH_ABS "/%s", basename);
  113. #endif
  114. if (ret >= sizeof(path)) {
  115. perror(__FILE__ ": overflow");
  116. return 3;
  117. }
  118. ret = snprintf(sysroot, sizeof(sysroot), "%s/" BR_SYSROOT, absbasedir);
  119. if (ret >= sizeof(sysroot)) {
  120. perror(__FILE__ ": overflow");
  121. return 3;
  122. }
  123. cur = args = malloc(sizeof(predef_args) +
  124. (sizeof(char *) * (argc + EXCLUSIVE_ARGS)));
  125. if (args == NULL) {
  126. perror(__FILE__ ": malloc");
  127. return 2;
  128. }
  129. /* start with predefined args */
  130. memcpy(cur, predef_args, sizeof(predef_args));
  131. cur += sizeof(predef_args) / sizeof(predef_args[0]);
  132. #ifdef BR_FLOAT_ABI
  133. /* add float abi if not overridden in args */
  134. for (i = 1; i < argc; i++) {
  135. if (!strncmp(argv[i], "-mfloat-abi=", strlen("-mfloat-abi=")) ||
  136. !strcmp(argv[i], "-msoft-float") ||
  137. !strcmp(argv[i], "-mhard-float"))
  138. break;
  139. }
  140. if (i == argc)
  141. *cur++ = "-mfloat-abi=" BR_FLOAT_ABI;
  142. #endif
  143. #if defined(BR_ARCH) || \
  144. defined(BR_CPU)
  145. /* Add our -march/cpu/abi flags, but only if none are
  146. * already specified on the commandline
  147. */
  148. for (i = 1; i < argc; i++) {
  149. if (!strncmp(argv[i], "-march=", strlen("-march=")) ||
  150. !strncmp(argv[i], "-mcpu=", strlen("-mcpu=" )))
  151. break;
  152. }
  153. if (i == argc) {
  154. #ifdef BR_ARCH
  155. *cur++ = "-march=" BR_ARCH;
  156. #endif
  157. #ifdef BR_CPU
  158. *cur++ = "-mcpu=" BR_CPU;
  159. #endif
  160. }
  161. #endif /* ARCH || CPU */
  162. /* append forward args */
  163. memcpy(cur, &argv[1], sizeof(char *) * (argc - 1));
  164. cur += argc - 1;
  165. /* finish with NULL termination */
  166. *cur = NULL;
  167. /* Debug the wrapper to see actual arguments passed to
  168. * the compiler:
  169. * unset, empty, or 0: do not trace
  170. * set to 1 : trace all arguments on a single line
  171. * set to 2 : trace one argument per line
  172. */
  173. if ((env_debug = getenv("BR2_DEBUG_WRAPPER"))) {
  174. debug = atoi(env_debug);
  175. if (debug > 0) {
  176. fprintf(stderr, "Toolchain wrapper executing:");
  177. for (i = 0; args[i]; i++)
  178. fprintf(stderr, "%s'%s'",
  179. (debug == 2) ? "\n " : " ", args[i]);
  180. fprintf(stderr, "\n");
  181. }
  182. }
  183. if (execv(path, args))
  184. perror(path);
  185. free(args);
  186. return 2;
  187. }