gstreamer1/gst1-rtsp-server: bump version to 1.10.1
[buildroot-gz.git] / toolchain / toolchain-wrapper.c
blob925d01394c34f09c55e5d8b1326b5cc0c4335b07
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 struct unsafe_opt_s {
84 const char *arg;
85 size_t len;
88 /* Unsafe options are options that specify a potentialy unsafe path,
89 * that will be checked by check_unsafe_path(), below.
91 * sizeof() on a string literal includes the terminating \0.
93 #define UNSAFE_OPT(o) { #o, sizeof(#o)-1 }
94 static const struct unsafe_opt_s unsafe_opts[] = {
95 UNSAFE_OPT(-I),
96 UNSAFE_OPT(-idirafter),
97 UNSAFE_OPT(-iquote),
98 UNSAFE_OPT(-isystem),
99 UNSAFE_OPT(-L),
100 { NULL, 0 },
103 /* Check if path is unsafe for cross-compilation. Unsafe paths are those
104 * pointing to the standard native include or library paths.
106 * We print the arguments leading to the failure. For some options, gcc
107 * accepts the path to be concatenated to the argument (e.g. -I/foo/bar)
108 * or separated (e.g. -I /foo/bar). In the first case, we need only print
109 * the argument as it already contains the path (arg_has_path), while in
110 * the second case we need to print both (!arg_has_path).
112 * If paranoid, exit in error instead of just printing a warning.
114 static void check_unsafe_path(const char *arg,
115 const char *path,
116 int paranoid,
117 int arg_has_path)
119 char **c;
120 static char *unsafe_paths[] = {
121 "/lib", "/usr/include", "/usr/lib", "/usr/local/include", "/usr/local/lib", NULL,
124 for (c = unsafe_paths; *c != NULL; c++) {
125 if (strncmp(path, *c, strlen(*c)))
126 continue;
127 fprintf(stderr,
128 "%s: %s: unsafe header/library path used in cross-compilation: '%s%s%s'\n",
129 program_invocation_short_name,
130 paranoid ? "ERROR" : "WARNING",
131 arg,
132 arg_has_path ? "" : "' '", /* close single-quote, space, open single-quote */
133 arg_has_path ? "" : path); /* so that arg and path are properly quoted. */
134 if (paranoid)
135 exit(1);
139 int main(int argc, char **argv)
141 char **args, **cur, **exec_args;
142 char *relbasedir, *absbasedir;
143 char *progpath = argv[0];
144 char *basename;
145 char *env_debug;
146 char *paranoid_wrapper;
147 int paranoid;
148 int ret, i, count = 0, debug;
150 /* Calculate the relative paths */
151 basename = strrchr(progpath, '/');
152 if (basename) {
153 *basename = '\0';
154 basename++;
155 relbasedir = malloc(strlen(progpath) + 7);
156 if (relbasedir == NULL) {
157 perror(__FILE__ ": malloc");
158 return 2;
160 sprintf(relbasedir, "%s/../..", argv[0]);
161 absbasedir = realpath(relbasedir, NULL);
162 } else {
163 basename = progpath;
164 absbasedir = malloc(PATH_MAX + 1);
165 ret = readlink("/proc/self/exe", absbasedir, PATH_MAX);
166 if (ret < 0) {
167 perror(__FILE__ ": readlink");
168 return 2;
170 absbasedir[ret] = '\0';
171 for (i = ret; i > 0; i--) {
172 if (absbasedir[i] == '/') {
173 absbasedir[i] = '\0';
174 if (++count == 3)
175 break;
179 if (absbasedir == NULL) {
180 perror(__FILE__ ": realpath");
181 return 2;
184 /* Fill in the relative paths */
185 #ifdef BR_CROSS_PATH_REL
186 ret = snprintf(path, sizeof(path), "%s/" BR_CROSS_PATH_REL "/%s" BR_CROSS_PATH_SUFFIX, absbasedir, basename);
187 #elif defined(BR_CROSS_PATH_ABS)
188 ret = snprintf(path, sizeof(path), BR_CROSS_PATH_ABS "/%s" BR_CROSS_PATH_SUFFIX, basename);
189 #else
190 ret = snprintf(path, sizeof(path), "%s/usr/bin/%s" BR_CROSS_PATH_SUFFIX, absbasedir, basename);
191 #endif
192 if (ret >= sizeof(path)) {
193 perror(__FILE__ ": overflow");
194 return 3;
196 #ifdef BR_CCACHE
197 ret = snprintf(ccache_path, sizeof(ccache_path), "%s/usr/bin/ccache", absbasedir);
198 if (ret >= sizeof(ccache_path)) {
199 perror(__FILE__ ": overflow");
200 return 3;
202 #endif
203 ret = snprintf(sysroot, sizeof(sysroot), "%s/" BR_SYSROOT, absbasedir);
204 if (ret >= sizeof(sysroot)) {
205 perror(__FILE__ ": overflow");
206 return 3;
209 cur = args = malloc(sizeof(predef_args) +
210 (sizeof(char *) * (argc + EXCLUSIVE_ARGS)));
211 if (args == NULL) {
212 perror(__FILE__ ": malloc");
213 return 2;
216 /* start with predefined args */
217 memcpy(cur, predef_args, sizeof(predef_args));
218 cur += sizeof(predef_args) / sizeof(predef_args[0]);
220 #ifdef BR_FLOAT_ABI
221 /* add float abi if not overridden in args */
222 for (i = 1; i < argc; i++) {
223 if (!strncmp(argv[i], "-mfloat-abi=", strlen("-mfloat-abi=")) ||
224 !strcmp(argv[i], "-msoft-float") ||
225 !strcmp(argv[i], "-mhard-float"))
226 break;
229 if (i == argc)
230 *cur++ = "-mfloat-abi=" BR_FLOAT_ABI;
231 #endif
233 #if defined(BR_ARCH) || \
234 defined(BR_CPU)
235 /* Add our -march/cpu flags, but only if none of
236 * -march/mtune/mcpu are already specified on the commandline
238 for (i = 1; i < argc; i++) {
239 if (!strncmp(argv[i], "-march=", strlen("-march=")) ||
240 !strncmp(argv[i], "-mtune=", strlen("-mtune=")) ||
241 !strncmp(argv[i], "-mcpu=", strlen("-mcpu=" )))
242 break;
244 if (i == argc) {
245 #ifdef BR_ARCH
246 *cur++ = "-march=" BR_ARCH;
247 #endif
248 #ifdef BR_CPU
249 *cur++ = "-mcpu=" BR_CPU;
250 #endif
252 #endif /* ARCH || CPU */
254 paranoid_wrapper = getenv("BR_COMPILER_PARANOID_UNSAFE_PATH");
255 if (paranoid_wrapper && strlen(paranoid_wrapper) > 0)
256 paranoid = 1;
257 else
258 paranoid = 0;
260 /* Check for unsafe library and header paths */
261 for (i = 1; i < argc; i++) {
262 const struct unsafe_opt_s *opt;
263 for (opt=unsafe_opts; opt->arg; opt++ ) {
264 /* Skip any non-unsafe option. */
265 if (strncmp(argv[i], opt->arg, opt->len))
266 continue;
268 /* Handle both cases:
269 * - path is a separate argument,
270 * - path is concatenated with option.
272 if (argv[i][opt->len] == '\0') {
273 i++;
274 if (i == argc)
275 break;
276 check_unsafe_path(argv[i-1], argv[i], paranoid, 0);
277 } else
278 check_unsafe_path(argv[i], argv[i] + opt->len, paranoid, 1);
282 /* append forward args */
283 memcpy(cur, &argv[1], sizeof(char *) * (argc - 1));
284 cur += argc - 1;
286 /* finish with NULL termination */
287 *cur = NULL;
289 exec_args = args;
290 #ifdef BR_CCACHE
291 if (getenv("BR_NO_CCACHE"))
292 /* Skip the ccache call */
293 exec_args++;
294 #endif
296 /* Debug the wrapper to see actual arguments passed to
297 * the compiler:
298 * unset, empty, or 0: do not trace
299 * set to 1 : trace all arguments on a single line
300 * set to 2 : trace one argument per line
302 if ((env_debug = getenv("BR2_DEBUG_WRAPPER"))) {
303 debug = atoi(env_debug);
304 if (debug > 0) {
305 fprintf(stderr, "Toolchain wrapper executing:");
306 #ifdef BR_CCACHE_HASH
307 fprintf(stderr, "%sCCACHE_COMPILERCHECK='string:" BR_CCACHE_HASH "'",
308 (debug == 2) ? "\n " : " ");
309 #endif
310 #ifdef BR_CCACHE_BASEDIR
311 fprintf(stderr, "%sCCACHE_BASEDIR='" BR_CCACHE_BASEDIR "'",
312 (debug == 2) ? "\n " : " ");
313 #endif
314 for (i = 0; exec_args[i]; i++)
315 fprintf(stderr, "%s'%s'",
316 (debug == 2) ? "\n " : " ", exec_args[i]);
317 fprintf(stderr, "\n");
321 #ifdef BR_CCACHE_HASH
322 /* Allow compilercheck to be overridden through the environment */
323 if (setenv("CCACHE_COMPILERCHECK", "string:" BR_CCACHE_HASH, 0)) {
324 perror(__FILE__ ": Failed to set CCACHE_COMPILERCHECK");
325 return 3;
327 #endif
328 #ifdef BR_CCACHE_BASEDIR
329 /* Allow compilercheck to be overridden through the environment */
330 if (setenv("CCACHE_BASEDIR", BR_CCACHE_BASEDIR, 0)) {
331 perror(__FILE__ ": Failed to set CCACHE_BASEDIR");
332 return 3;
334 #endif
336 if (execv(exec_args[0], exec_args))
337 perror(path);
339 free(args);
341 return 2;