ext-toolchain-wrapper.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. *
  12. * This file is licensed under the terms of the GNU General Public License
  13. * version 2. This program is licensed "as is" without any warranty of any
  14. * kind, whether express or implied.
  15. */
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <limits.h>
  19. #include <unistd.h>
  20. #include <stdlib.h>
  21. static char path[PATH_MAX];
  22. static char sysroot[PATH_MAX];
  23. static char *predef_args[] = {
  24. path,
  25. "--sysroot", sysroot,
  26. #ifdef BR_ARCH
  27. "-march=" BR_ARCH,
  28. #endif /* BR_ARCH */
  29. #ifdef BR_TUNE
  30. "-mtune=" BR_TUNE,
  31. #endif /* BR_TUNE */
  32. #ifdef BR_CPU
  33. "-mcpu=" BR_CPU,
  34. #endif
  35. #ifdef BR_ABI
  36. "-mabi=" BR_ABI,
  37. #endif
  38. #ifdef BR_SOFTFLOAT
  39. "-msoft-float",
  40. #endif /* BR_SOFTFLOAT */
  41. #ifdef BR_VFPFLOAT
  42. "-mfpu=vfp",
  43. #endif /* BR_VFPFLOAT */
  44. #ifdef BR_64
  45. "-m64",
  46. #endif
  47. #ifdef BR_BINFMT_FLAT
  48. "-Wl,-elf2flt",
  49. #endif
  50. #ifdef BR_ADDITIONAL_CFLAGS
  51. BR_ADDITIONAL_CFLAGS
  52. #endif
  53. };
  54. int main(int argc, char **argv)
  55. {
  56. char **args, **cur;
  57. char *relbasedir, *absbasedir;
  58. char *progpath = argv[0];
  59. char *basename;
  60. int ret;
  61. /* Calculate the relative paths */
  62. basename = strrchr(progpath, '/');
  63. if (basename) {
  64. *basename = '\0';
  65. basename++;
  66. relbasedir = malloc(strlen(progpath) + 7);
  67. if (relbasedir == NULL) {
  68. perror(__FILE__ ": malloc");
  69. return 2;
  70. }
  71. sprintf(relbasedir, "%s/../..", argv[0]);
  72. absbasedir = realpath(relbasedir, NULL);
  73. } else {
  74. basename = progpath;
  75. absbasedir = realpath("../..", NULL);
  76. }
  77. if (absbasedir == NULL) {
  78. perror(__FILE__ ": realpath");
  79. return 2;
  80. }
  81. /* Fill in the relative paths */
  82. #ifdef BR_CROSS_PATH_REL
  83. ret = snprintf(path, sizeof(path), "%s/" BR_CROSS_PATH_REL "/%s", absbasedir, basename);
  84. #else /* BR_CROSS_PATH_ABS */
  85. ret = snprintf(path, sizeof(path), BR_CROSS_PATH_ABS "/%s", basename);
  86. #endif
  87. if (ret >= sizeof(path)) {
  88. perror(__FILE__ ": overflow");
  89. return 3;
  90. }
  91. ret = snprintf(sysroot, sizeof(sysroot), "%s/" BR_SYSROOT, absbasedir);
  92. if (ret >= sizeof(sysroot)) {
  93. perror(__FILE__ ": overflow");
  94. return 3;
  95. }
  96. cur = args = malloc(sizeof(predef_args) + (sizeof(char *) * argc));
  97. if (args == NULL) {
  98. perror(__FILE__ ": malloc");
  99. return 2;
  100. }
  101. /* start with predefined args */
  102. memcpy(cur, predef_args, sizeof(predef_args));
  103. cur += sizeof(predef_args) / sizeof(predef_args[0]);
  104. /* append forward args */
  105. memcpy(cur, &argv[1], sizeof(char *) * (argc - 1));
  106. cur += argc - 1;
  107. /* finish with NULL termination */
  108. *cur = NULL;
  109. if (execv(path, args))
  110. perror(path);
  111. free(args);
  112. return 2;
  113. }