2
1

ext-toolchain-wrapper.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. * -mabi-float={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. */
  31. #define EXCLUSIVE_ARGS 1
  32. static char *predef_args[] = {
  33. path,
  34. "--sysroot", sysroot,
  35. #ifdef BR_ARCH
  36. "-march=" BR_ARCH,
  37. #endif /* BR_ARCH */
  38. #ifdef BR_TUNE
  39. "-mtune=" BR_TUNE,
  40. #endif /* BR_TUNE */
  41. #ifdef BR_CPU
  42. "-mcpu=" BR_CPU,
  43. #endif
  44. #ifdef BR_ABI
  45. "-mabi=" BR_ABI,
  46. #endif
  47. #ifdef BR_FPU
  48. "-mfpu=" BR_FPU,
  49. #endif
  50. #ifdef BR_SOFTFLOAT
  51. "-msoft-float",
  52. #endif /* BR_SOFTFLOAT */
  53. #ifdef BR_MODE
  54. "-m" BR_MODE,
  55. #endif
  56. #ifdef BR_64
  57. "-m64",
  58. #endif
  59. #ifdef BR_BINFMT_FLAT
  60. "-Wl,-elf2flt",
  61. #endif
  62. #ifdef BR_ADDITIONAL_CFLAGS
  63. BR_ADDITIONAL_CFLAGS
  64. #endif
  65. };
  66. int main(int argc, char **argv)
  67. {
  68. char **args, **cur;
  69. char *relbasedir, *absbasedir;
  70. char *progpath = argv[0];
  71. char *basename;
  72. char *env_debug;
  73. int ret, i, count = 0, debug;
  74. /* Calculate the relative paths */
  75. basename = strrchr(progpath, '/');
  76. if (basename) {
  77. *basename = '\0';
  78. basename++;
  79. relbasedir = malloc(strlen(progpath) + 7);
  80. if (relbasedir == NULL) {
  81. perror(__FILE__ ": malloc");
  82. return 2;
  83. }
  84. sprintf(relbasedir, "%s/../..", argv[0]);
  85. absbasedir = realpath(relbasedir, NULL);
  86. } else {
  87. basename = progpath;
  88. absbasedir = malloc(PATH_MAX + 1);
  89. ret = readlink("/proc/self/exe", absbasedir, PATH_MAX);
  90. if (ret < 0) {
  91. perror(__FILE__ ": readlink");
  92. return 2;
  93. }
  94. absbasedir[ret] = '\0';
  95. for (i = ret; i > 0; i--) {
  96. if (absbasedir[i] == '/') {
  97. absbasedir[i] = '\0';
  98. if (++count == 3)
  99. break;
  100. }
  101. }
  102. }
  103. if (absbasedir == NULL) {
  104. perror(__FILE__ ": realpath");
  105. return 2;
  106. }
  107. /* Fill in the relative paths */
  108. #ifdef BR_CROSS_PATH_REL
  109. ret = snprintf(path, sizeof(path), "%s/" BR_CROSS_PATH_REL "/%s", absbasedir, basename);
  110. #else /* BR_CROSS_PATH_ABS */
  111. ret = snprintf(path, sizeof(path), BR_CROSS_PATH_ABS "/%s", basename);
  112. #endif
  113. if (ret >= sizeof(path)) {
  114. perror(__FILE__ ": overflow");
  115. return 3;
  116. }
  117. ret = snprintf(sysroot, sizeof(sysroot), "%s/" BR_SYSROOT, absbasedir);
  118. if (ret >= sizeof(sysroot)) {
  119. perror(__FILE__ ": overflow");
  120. return 3;
  121. }
  122. cur = args = malloc(sizeof(predef_args) +
  123. (sizeof(char *) * (argc + EXCLUSIVE_ARGS)));
  124. if (args == NULL) {
  125. perror(__FILE__ ": malloc");
  126. return 2;
  127. }
  128. /* start with predefined args */
  129. memcpy(cur, predef_args, sizeof(predef_args));
  130. cur += sizeof(predef_args) / sizeof(predef_args[0]);
  131. #ifdef BR_FLOAT_ABI
  132. /* add float abi if not overridden in args */
  133. for (i = 1; i < argc; i++) {
  134. if (!strncmp(argv[i], "-mfloat-abi=", strlen("-mfloat-abi=")) ||
  135. !strcmp(argv[i], "-msoft-float") ||
  136. !strcmp(argv[i], "-mhard-float"))
  137. break;
  138. }
  139. if (i == argc)
  140. *cur++ = "-mfloat-abi=" BR_FLOAT_ABI;
  141. #endif
  142. /* append forward args */
  143. memcpy(cur, &argv[1], sizeof(char *) * (argc - 1));
  144. cur += argc - 1;
  145. /* finish with NULL termination */
  146. *cur = NULL;
  147. /* Debug the wrapper to see actual arguments passed to
  148. * the compiler:
  149. * unset, empty, or 0: do not trace
  150. * set to 1 : trace all arguments on a single line
  151. * set to 2 : trace one argument per line
  152. */
  153. if ((env_debug = getenv("BR_DEBUG_WRAPPER"))) {
  154. debug = atoi(env_debug);
  155. if (debug > 0) {
  156. fprintf(stderr, "Toolchain wrapper executing:");
  157. for (i = 0; args[i]; i++)
  158. fprintf(stderr, "%s'%s'",
  159. (debug == 2) ? "\n " : " ", args[i]);
  160. fprintf(stderr, "\n");
  161. }
  162. }
  163. if (execv(path, args))
  164. perror(path);
  165. free(args);
  166. return 2;
  167. }