ext-toolchain-wrapper.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. int ret, i, count = 0;
  73. /* Calculate the relative paths */
  74. basename = strrchr(progpath, '/');
  75. if (basename) {
  76. *basename = '\0';
  77. basename++;
  78. relbasedir = malloc(strlen(progpath) + 7);
  79. if (relbasedir == NULL) {
  80. perror(__FILE__ ": malloc");
  81. return 2;
  82. }
  83. sprintf(relbasedir, "%s/../..", argv[0]);
  84. absbasedir = realpath(relbasedir, NULL);
  85. } else {
  86. basename = progpath;
  87. absbasedir = malloc(PATH_MAX + 1);
  88. ret = readlink("/proc/self/exe", absbasedir, PATH_MAX);
  89. if (ret < 0) {
  90. perror(__FILE__ ": readlink");
  91. return 2;
  92. }
  93. absbasedir[ret] = '\0';
  94. for (i = ret; i > 0; i--) {
  95. if (absbasedir[i] == '/') {
  96. absbasedir[i] = '\0';
  97. if (++count == 3)
  98. break;
  99. }
  100. }
  101. }
  102. if (absbasedir == NULL) {
  103. perror(__FILE__ ": realpath");
  104. return 2;
  105. }
  106. /* Fill in the relative paths */
  107. #ifdef BR_CROSS_PATH_REL
  108. ret = snprintf(path, sizeof(path), "%s/" BR_CROSS_PATH_REL "/%s", absbasedir, basename);
  109. #else /* BR_CROSS_PATH_ABS */
  110. ret = snprintf(path, sizeof(path), BR_CROSS_PATH_ABS "/%s", basename);
  111. #endif
  112. if (ret >= sizeof(path)) {
  113. perror(__FILE__ ": overflow");
  114. return 3;
  115. }
  116. ret = snprintf(sysroot, sizeof(sysroot), "%s/" BR_SYSROOT, absbasedir);
  117. if (ret >= sizeof(sysroot)) {
  118. perror(__FILE__ ": overflow");
  119. return 3;
  120. }
  121. cur = args = malloc(sizeof(predef_args) +
  122. (sizeof(char *) * (argc + EXCLUSIVE_ARGS)));
  123. if (args == NULL) {
  124. perror(__FILE__ ": malloc");
  125. return 2;
  126. }
  127. /* start with predefined args */
  128. memcpy(cur, predef_args, sizeof(predef_args));
  129. cur += sizeof(predef_args) / sizeof(predef_args[0]);
  130. #ifdef BR_FLOAT_ABI
  131. /* add float abi if not overridden in args */
  132. for (i = 1; i < argc; i++) {
  133. if (!strncmp(argv[i], "-mfloat-abi=", strlen("-mfloat-abi=")) ||
  134. !strcmp(argv[i], "-msoft-float") ||
  135. !strcmp(argv[i], "-mhard-float"))
  136. break;
  137. }
  138. if (i == argc)
  139. *cur++ = "-mfloat-abi=" BR_FLOAT_ABI;
  140. #endif
  141. /* append forward args */
  142. memcpy(cur, &argv[1], sizeof(char *) * (argc - 1));
  143. cur += argc - 1;
  144. /* finish with NULL termination */
  145. *cur = NULL;
  146. if (getenv("BR_DEBUG_WRAPPER")) {
  147. fprintf(stderr, "Executing");
  148. for (i = 0; args[i]; i++)
  149. fprintf(stderr, " %s", args[i]);
  150. fprintf(stderr, "\n");
  151. }
  152. if (execv(path, args))
  153. perror(path);
  154. free(args);
  155. return 2;
  156. }