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