toolchain-wrapper.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /**
  2. * Buildroot wrapper for toolchains. This simply executes the real toolchain
  3. * with a number of arguments (sysroot/arch/..) hardcoded, to ensure the
  4. * 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. #define _GNU_SOURCE
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <limits.h>
  21. #include <unistd.h>
  22. #include <stdlib.h>
  23. #include <errno.h>
  24. #ifdef BR_CCACHE
  25. static char ccache_path[PATH_MAX];
  26. #endif
  27. static char path[PATH_MAX];
  28. static char sysroot[PATH_MAX];
  29. /**
  30. * GCC errors out with certain combinations of arguments (examples are
  31. * -mfloat-abi={hard|soft} and -m{little|big}-endian), so we have to ensure
  32. * that we only pass the predefined one to the real compiler if the inverse
  33. * option isn't in the argument list.
  34. * This specifies the worst case number of extra arguments we might pass
  35. * Currently, we have:
  36. * -mfloat-abi=
  37. * -march=
  38. * -mcpu=
  39. */
  40. #define EXCLUSIVE_ARGS 3
  41. static char *predef_args[] = {
  42. #ifdef BR_CCACHE
  43. ccache_path,
  44. #endif
  45. path,
  46. "--sysroot", sysroot,
  47. #ifdef BR_ABI
  48. "-mabi=" BR_ABI,
  49. #endif
  50. #ifdef BR_FPU
  51. "-mfpu=" BR_FPU,
  52. #endif
  53. #ifdef BR_SOFTFLOAT
  54. "-msoft-float",
  55. #endif /* BR_SOFTFLOAT */
  56. #ifdef BR_MODE
  57. "-m" BR_MODE,
  58. #endif
  59. #ifdef BR_64
  60. "-m64",
  61. #endif
  62. #ifdef BR_OMIT_LOCK_PREFIX
  63. "-Wa,-momit-lock-prefix=yes",
  64. #endif
  65. #ifdef BR_BINFMT_FLAT
  66. "-Wl,-elf2flt",
  67. #endif
  68. #ifdef BR_MIPS_TARGET_LITTLE_ENDIAN
  69. "-EL",
  70. #endif
  71. #if defined(BR_MIPS_TARGET_BIG_ENDIAN) || defined(BR_ARC_TARGET_BIG_ENDIAN)
  72. "-EB",
  73. #endif
  74. #ifdef BR_ADDITIONAL_CFLAGS
  75. BR_ADDITIONAL_CFLAGS
  76. #endif
  77. };
  78. static void check_unsafe_path(const char *path, int paranoid)
  79. {
  80. char **c;
  81. static char *unsafe_paths[] = {
  82. "/lib", "/usr/include", "/usr/lib", "/usr/local/include", "/usr/local/lib", NULL,
  83. };
  84. for (c = unsafe_paths; *c != NULL; c++) {
  85. if (!strncmp(path, *c, strlen(*c))) {
  86. fprintf(stderr, "%s: %s: unsafe header/library path used in cross-compilation: '%s'\n",
  87. program_invocation_short_name,
  88. paranoid ? "ERROR" : "WARNING", path);
  89. if (paranoid)
  90. exit(1);
  91. continue;
  92. }
  93. }
  94. }
  95. int main(int argc, char **argv)
  96. {
  97. char **args, **cur, **exec_args;
  98. char *relbasedir, *absbasedir;
  99. char *progpath = argv[0];
  100. char *basename;
  101. char *env_debug;
  102. char *paranoid_wrapper;
  103. int paranoid;
  104. int ret, i, count = 0, debug;
  105. /* Calculate the relative paths */
  106. basename = strrchr(progpath, '/');
  107. if (basename) {
  108. *basename = '\0';
  109. basename++;
  110. relbasedir = malloc(strlen(progpath) + 7);
  111. if (relbasedir == NULL) {
  112. perror(__FILE__ ": malloc");
  113. return 2;
  114. }
  115. sprintf(relbasedir, "%s/../..", argv[0]);
  116. absbasedir = realpath(relbasedir, NULL);
  117. } else {
  118. basename = progpath;
  119. absbasedir = malloc(PATH_MAX + 1);
  120. ret = readlink("/proc/self/exe", absbasedir, PATH_MAX);
  121. if (ret < 0) {
  122. perror(__FILE__ ": readlink");
  123. return 2;
  124. }
  125. absbasedir[ret] = '\0';
  126. for (i = ret; i > 0; i--) {
  127. if (absbasedir[i] == '/') {
  128. absbasedir[i] = '\0';
  129. if (++count == 3)
  130. break;
  131. }
  132. }
  133. }
  134. if (absbasedir == NULL) {
  135. perror(__FILE__ ": realpath");
  136. return 2;
  137. }
  138. /* Fill in the relative paths */
  139. #ifdef BR_CROSS_PATH_REL
  140. ret = snprintf(path, sizeof(path), "%s/" BR_CROSS_PATH_REL "/%s" BR_CROSS_PATH_SUFFIX, absbasedir, basename);
  141. #elif defined(BR_CROSS_PATH_ABS)
  142. ret = snprintf(path, sizeof(path), BR_CROSS_PATH_ABS "/%s" BR_CROSS_PATH_SUFFIX, basename);
  143. #else
  144. ret = snprintf(path, sizeof(path), "%s/usr/bin/%s" BR_CROSS_PATH_SUFFIX, absbasedir, basename);
  145. #endif
  146. if (ret >= sizeof(path)) {
  147. perror(__FILE__ ": overflow");
  148. return 3;
  149. }
  150. #ifdef BR_CCACHE
  151. ret = snprintf(ccache_path, sizeof(ccache_path), "%s/usr/bin/ccache", absbasedir);
  152. if (ret >= sizeof(ccache_path)) {
  153. perror(__FILE__ ": overflow");
  154. return 3;
  155. }
  156. #endif
  157. ret = snprintf(sysroot, sizeof(sysroot), "%s/" BR_SYSROOT, absbasedir);
  158. if (ret >= sizeof(sysroot)) {
  159. perror(__FILE__ ": overflow");
  160. return 3;
  161. }
  162. cur = args = malloc(sizeof(predef_args) +
  163. (sizeof(char *) * (argc + EXCLUSIVE_ARGS)));
  164. if (args == NULL) {
  165. perror(__FILE__ ": malloc");
  166. return 2;
  167. }
  168. /* start with predefined args */
  169. memcpy(cur, predef_args, sizeof(predef_args));
  170. cur += sizeof(predef_args) / sizeof(predef_args[0]);
  171. #ifdef BR_FLOAT_ABI
  172. /* add float abi if not overridden in args */
  173. for (i = 1; i < argc; i++) {
  174. if (!strncmp(argv[i], "-mfloat-abi=", strlen("-mfloat-abi=")) ||
  175. !strcmp(argv[i], "-msoft-float") ||
  176. !strcmp(argv[i], "-mhard-float"))
  177. break;
  178. }
  179. if (i == argc)
  180. *cur++ = "-mfloat-abi=" BR_FLOAT_ABI;
  181. #endif
  182. #if defined(BR_ARCH) || \
  183. defined(BR_CPU)
  184. /* Add our -march/cpu flags, but only if none of
  185. * -march/mtune/mcpu are already specified on the commandline
  186. */
  187. for (i = 1; i < argc; i++) {
  188. if (!strncmp(argv[i], "-march=", strlen("-march=")) ||
  189. !strncmp(argv[i], "-mtune=", strlen("-mtune=")) ||
  190. !strncmp(argv[i], "-mcpu=", strlen("-mcpu=" )))
  191. break;
  192. }
  193. if (i == argc) {
  194. #ifdef BR_ARCH
  195. *cur++ = "-march=" BR_ARCH;
  196. #endif
  197. #ifdef BR_CPU
  198. *cur++ = "-mcpu=" BR_CPU;
  199. #endif
  200. }
  201. #endif /* ARCH || CPU */
  202. paranoid_wrapper = getenv("BR_COMPILER_PARANOID_UNSAFE_PATH");
  203. if (paranoid_wrapper && strlen(paranoid_wrapper) > 0)
  204. paranoid = 1;
  205. else
  206. paranoid = 0;
  207. /* Check for unsafe library and header paths */
  208. for (i = 1; i < argc; i++) {
  209. /* Skip options that do not start with -I and -L */
  210. if (strncmp(argv[i], "-I", 2) && strncmp(argv[i], "-L", 2))
  211. continue;
  212. /* We handle two cases: first the case where -I/-L and
  213. * the path are separated by one space and therefore
  214. * visible as two separate options, and then the case
  215. * where they are stuck together forming one single
  216. * option.
  217. */
  218. if (argv[i][2] == '\0') {
  219. i++;
  220. if (i == argc)
  221. continue;
  222. check_unsafe_path(argv[i], paranoid);
  223. } else {
  224. check_unsafe_path(argv[i] + 2, paranoid);
  225. }
  226. }
  227. /* append forward args */
  228. memcpy(cur, &argv[1], sizeof(char *) * (argc - 1));
  229. cur += argc - 1;
  230. /* finish with NULL termination */
  231. *cur = NULL;
  232. exec_args = args;
  233. #ifdef BR_CCACHE
  234. if (getenv("BR_NO_CCACHE"))
  235. /* Skip the ccache call */
  236. exec_args++;
  237. #endif
  238. /* Debug the wrapper to see actual arguments passed to
  239. * the compiler:
  240. * unset, empty, or 0: do not trace
  241. * set to 1 : trace all arguments on a single line
  242. * set to 2 : trace one argument per line
  243. */
  244. if ((env_debug = getenv("BR2_DEBUG_WRAPPER"))) {
  245. debug = atoi(env_debug);
  246. if (debug > 0) {
  247. fprintf(stderr, "Toolchain wrapper executing:");
  248. #ifdef BR_CCACHE_HASH
  249. fprintf(stderr, "%sCCACHE_COMPILERCHECK='string:" BR_CCACHE_HASH "'",
  250. (debug == 2) ? "\n " : " ");
  251. #endif
  252. #ifdef BR_CCACHE_BASEDIR
  253. fprintf(stderr, "%sCCACHE_BASEDIR='" BR_CCACHE_BASEDIR "'",
  254. (debug == 2) ? "\n " : " ");
  255. #endif
  256. for (i = 0; exec_args[i]; i++)
  257. fprintf(stderr, "%s'%s'",
  258. (debug == 2) ? "\n " : " ", exec_args[i]);
  259. fprintf(stderr, "\n");
  260. }
  261. }
  262. #ifdef BR_CCACHE_HASH
  263. /* Allow compilercheck to be overridden through the environment */
  264. if (setenv("CCACHE_COMPILERCHECK", "string:" BR_CCACHE_HASH, 0)) {
  265. perror(__FILE__ ": Failed to set CCACHE_COMPILERCHECK");
  266. return 3;
  267. }
  268. #endif
  269. #ifdef BR_CCACHE_BASEDIR
  270. /* Allow compilercheck to be overridden through the environment */
  271. if (setenv("CCACHE_BASEDIR", BR_CCACHE_BASEDIR, 0)) {
  272. perror(__FILE__ ": Failed to set CCACHE_BASEDIR");
  273. return 3;
  274. }
  275. #endif
  276. if (execv(exec_args[0], exec_args))
  277. perror(path);
  278. free(args);
  279. return 2;
  280. }