cmake: bump version to 3.7.1
[buildroot-gz.git] / toolchain / toolchain-wrapper.c
blobd59629b0489ba06d21b46f3088ab204c664d3f9b
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_NO_FUSED_MADD
70 "-mno-fused-madd",
71 #endif
72 #ifdef BR_BINFMT_FLAT
73 "-Wl,-elf2flt",
74 #endif
75 #ifdef BR_MIPS_TARGET_LITTLE_ENDIAN
76 "-EL",
77 #endif
78 #if defined(BR_MIPS_TARGET_BIG_ENDIAN) || defined(BR_ARC_TARGET_BIG_ENDIAN)
79 "-EB",
80 #endif
81 #ifdef BR_ADDITIONAL_CFLAGS
82 BR_ADDITIONAL_CFLAGS
83 #endif
86 struct unsafe_opt_s {
87 const char *arg;
88 size_t len;
91 /* Unsafe options are options that specify a potentialy unsafe path,
92 * that will be checked by check_unsafe_path(), below.
94 * sizeof() on a string literal includes the terminating \0.
96 #define UNSAFE_OPT(o) { #o, sizeof(#o)-1 }
97 static const struct unsafe_opt_s unsafe_opts[] = {
98 UNSAFE_OPT(-I),
99 UNSAFE_OPT(-idirafter),
100 UNSAFE_OPT(-iquote),
101 UNSAFE_OPT(-isystem),
102 UNSAFE_OPT(-L),
103 { NULL, 0 },
106 /* Check if path is unsafe for cross-compilation. Unsafe paths are those
107 * pointing to the standard native include or library paths.
109 * We print the arguments leading to the failure. For some options, gcc
110 * accepts the path to be concatenated to the argument (e.g. -I/foo/bar)
111 * or separated (e.g. -I /foo/bar). In the first case, we need only print
112 * the argument as it already contains the path (arg_has_path), while in
113 * the second case we need to print both (!arg_has_path).
115 * If paranoid, exit in error instead of just printing a warning.
117 static void check_unsafe_path(const char *arg,
118 const char *path,
119 int paranoid,
120 int arg_has_path)
122 char **c;
123 static char *unsafe_paths[] = {
124 "/lib", "/usr/include", "/usr/lib", "/usr/local/include", "/usr/local/lib", NULL,
127 for (c = unsafe_paths; *c != NULL; c++) {
128 if (strncmp(path, *c, strlen(*c)))
129 continue;
130 fprintf(stderr,
131 "%s: %s: unsafe header/library path used in cross-compilation: '%s%s%s'\n",
132 program_invocation_short_name,
133 paranoid ? "ERROR" : "WARNING",
134 arg,
135 arg_has_path ? "" : "' '", /* close single-quote, space, open single-quote */
136 arg_has_path ? "" : path); /* so that arg and path are properly quoted. */
137 if (paranoid)
138 exit(1);
142 int main(int argc, char **argv)
144 char **args, **cur, **exec_args;
145 char *relbasedir, *absbasedir;
146 char *progpath = argv[0];
147 char *basename;
148 char *env_debug;
149 char *paranoid_wrapper;
150 int paranoid;
151 int ret, i, count = 0, debug;
153 /* Calculate the relative paths */
154 basename = strrchr(progpath, '/');
155 if (basename) {
156 *basename = '\0';
157 basename++;
158 relbasedir = malloc(strlen(progpath) + 7);
159 if (relbasedir == NULL) {
160 perror(__FILE__ ": malloc");
161 return 2;
163 sprintf(relbasedir, "%s/../..", argv[0]);
164 absbasedir = realpath(relbasedir, NULL);
165 } else {
166 basename = progpath;
167 absbasedir = malloc(PATH_MAX + 1);
168 ret = readlink("/proc/self/exe", absbasedir, PATH_MAX);
169 if (ret < 0) {
170 perror(__FILE__ ": readlink");
171 return 2;
173 absbasedir[ret] = '\0';
174 for (i = ret; i > 0; i--) {
175 if (absbasedir[i] == '/') {
176 absbasedir[i] = '\0';
177 if (++count == 3)
178 break;
182 if (absbasedir == NULL) {
183 perror(__FILE__ ": realpath");
184 return 2;
187 /* Fill in the relative paths */
188 #ifdef BR_CROSS_PATH_REL
189 ret = snprintf(path, sizeof(path), "%s/" BR_CROSS_PATH_REL "/%s" BR_CROSS_PATH_SUFFIX, absbasedir, basename);
190 #elif defined(BR_CROSS_PATH_ABS)
191 ret = snprintf(path, sizeof(path), BR_CROSS_PATH_ABS "/%s" BR_CROSS_PATH_SUFFIX, basename);
192 #else
193 ret = snprintf(path, sizeof(path), "%s/usr/bin/%s" BR_CROSS_PATH_SUFFIX, absbasedir, basename);
194 #endif
195 if (ret >= sizeof(path)) {
196 perror(__FILE__ ": overflow");
197 return 3;
199 #ifdef BR_CCACHE
200 ret = snprintf(ccache_path, sizeof(ccache_path), "%s/usr/bin/ccache", absbasedir);
201 if (ret >= sizeof(ccache_path)) {
202 perror(__FILE__ ": overflow");
203 return 3;
205 #endif
206 ret = snprintf(sysroot, sizeof(sysroot), "%s/" BR_SYSROOT, absbasedir);
207 if (ret >= sizeof(sysroot)) {
208 perror(__FILE__ ": overflow");
209 return 3;
212 cur = args = malloc(sizeof(predef_args) +
213 (sizeof(char *) * (argc + EXCLUSIVE_ARGS)));
214 if (args == NULL) {
215 perror(__FILE__ ": malloc");
216 return 2;
219 /* start with predefined args */
220 memcpy(cur, predef_args, sizeof(predef_args));
221 cur += sizeof(predef_args) / sizeof(predef_args[0]);
223 #ifdef BR_FLOAT_ABI
224 /* add float abi if not overridden in args */
225 for (i = 1; i < argc; i++) {
226 if (!strncmp(argv[i], "-mfloat-abi=", strlen("-mfloat-abi=")) ||
227 !strcmp(argv[i], "-msoft-float") ||
228 !strcmp(argv[i], "-mhard-float"))
229 break;
232 if (i == argc)
233 *cur++ = "-mfloat-abi=" BR_FLOAT_ABI;
234 #endif
236 #if defined(BR_ARCH) || \
237 defined(BR_CPU)
238 /* Add our -march/cpu flags, but only if none of
239 * -march/mtune/mcpu are already specified on the commandline
241 for (i = 1; i < argc; i++) {
242 if (!strncmp(argv[i], "-march=", strlen("-march=")) ||
243 !strncmp(argv[i], "-mtune=", strlen("-mtune=")) ||
244 !strncmp(argv[i], "-mcpu=", strlen("-mcpu=" )))
245 break;
247 if (i == argc) {
248 #ifdef BR_ARCH
249 *cur++ = "-march=" BR_ARCH;
250 #endif
251 #ifdef BR_CPU
252 *cur++ = "-mcpu=" BR_CPU;
253 #endif
255 #endif /* ARCH || CPU */
257 paranoid_wrapper = getenv("BR_COMPILER_PARANOID_UNSAFE_PATH");
258 if (paranoid_wrapper && strlen(paranoid_wrapper) > 0)
259 paranoid = 1;
260 else
261 paranoid = 0;
263 /* Check for unsafe library and header paths */
264 for (i = 1; i < argc; i++) {
265 const struct unsafe_opt_s *opt;
266 for (opt=unsafe_opts; opt->arg; opt++ ) {
267 /* Skip any non-unsafe option. */
268 if (strncmp(argv[i], opt->arg, opt->len))
269 continue;
271 /* Handle both cases:
272 * - path is a separate argument,
273 * - path is concatenated with option.
275 if (argv[i][opt->len] == '\0') {
276 i++;
277 if (i == argc)
278 break;
279 check_unsafe_path(argv[i-1], argv[i], paranoid, 0);
280 } else
281 check_unsafe_path(argv[i], argv[i] + opt->len, paranoid, 1);
285 /* append forward args */
286 memcpy(cur, &argv[1], sizeof(char *) * (argc - 1));
287 cur += argc - 1;
289 /* finish with NULL termination */
290 *cur = NULL;
292 exec_args = args;
293 #ifdef BR_CCACHE
294 if (getenv("BR_NO_CCACHE"))
295 /* Skip the ccache call */
296 exec_args++;
297 #endif
299 /* Debug the wrapper to see actual arguments passed to
300 * the compiler:
301 * unset, empty, or 0: do not trace
302 * set to 1 : trace all arguments on a single line
303 * set to 2 : trace one argument per line
305 if ((env_debug = getenv("BR2_DEBUG_WRAPPER"))) {
306 debug = atoi(env_debug);
307 if (debug > 0) {
308 fprintf(stderr, "Toolchain wrapper executing:");
309 #ifdef BR_CCACHE_HASH
310 fprintf(stderr, "%sCCACHE_COMPILERCHECK='string:" BR_CCACHE_HASH "'",
311 (debug == 2) ? "\n " : " ");
312 #endif
313 #ifdef BR_CCACHE_BASEDIR
314 fprintf(stderr, "%sCCACHE_BASEDIR='" BR_CCACHE_BASEDIR "'",
315 (debug == 2) ? "\n " : " ");
316 #endif
317 for (i = 0; exec_args[i]; i++)
318 fprintf(stderr, "%s'%s'",
319 (debug == 2) ? "\n " : " ", exec_args[i]);
320 fprintf(stderr, "\n");
324 #ifdef BR_CCACHE_HASH
325 /* Allow compilercheck to be overridden through the environment */
326 if (setenv("CCACHE_COMPILERCHECK", "string:" BR_CCACHE_HASH, 0)) {
327 perror(__FILE__ ": Failed to set CCACHE_COMPILERCHECK");
328 return 3;
330 #endif
331 #ifdef BR_CCACHE_BASEDIR
332 /* Allow compilercheck to be overridden through the environment */
333 if (setenv("CCACHE_BASEDIR", BR_CCACHE_BASEDIR, 0)) {
334 perror(__FILE__ ": Failed to set CCACHE_BASEDIR");
335 return 3;
337 #endif
339 if (execv(exec_args[0], exec_args))
340 perror(path);
342 free(args);
344 return 2;