]> rtime.felk.cvut.cz Git - coffee/buildroot.git/blob - toolchain/toolchain-wrapper.c
d52115f51abcd9bb0c652c8d06a94e063e8a3f67
[coffee/buildroot.git] / toolchain / toolchain-wrapper.c
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
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>
25
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];
31
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=
42  */
43 #define EXCLUSIVE_ARGS  3
44
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_BINFMT_FLAT
67         "-Wl,-elf2flt",
68 #endif
69 #ifdef BR_MIPS_TARGET_LITTLE_ENDIAN
70         "-EL",
71 #endif
72 #if defined(BR_MIPS_TARGET_BIG_ENDIAN) || defined(BR_ARC_TARGET_BIG_ENDIAN)
73         "-EB",
74 #endif
75 #ifdef BR_ADDITIONAL_CFLAGS
76         BR_ADDITIONAL_CFLAGS
77 #endif
78 };
79
80 static void check_unsafe_path(const char *path, int paranoid)
81 {
82         char **c;
83         static char *unsafe_paths[] = {
84                 "/lib", "/usr/include", "/usr/lib", "/usr/local/include", "/usr/local/lib", NULL,
85         };
86
87         for (c = unsafe_paths; *c != NULL; c++) {
88                 if (!strncmp(path, *c, strlen(*c))) {
89                         fprintf(stderr, "%s: %s: unsafe header/library path used in cross-compilation: '%s'\n",
90                                 program_invocation_short_name,
91                                 paranoid ? "ERROR" : "WARNING", path);
92                         if (paranoid)
93                                 exit(1);
94                         continue;
95                 }
96         }
97 }
98
99 int main(int argc, char **argv)
100 {
101         char **args, **cur, **exec_args;
102         char *relbasedir, *absbasedir;
103         char *progpath = argv[0];
104         char *basename;
105         char *env_debug;
106         char *paranoid_wrapper;
107         int paranoid;
108         int ret, i, count = 0, debug;
109
110         /* Calculate the relative paths */
111         basename = strrchr(progpath, '/');
112         if (basename) {
113                 *basename = '\0';
114                 basename++;
115                 relbasedir = malloc(strlen(progpath) + 7);
116                 if (relbasedir == NULL) {
117                         perror(__FILE__ ": malloc");
118                         return 2;
119                 }
120                 sprintf(relbasedir, "%s/../..", argv[0]);
121                 absbasedir = realpath(relbasedir, NULL);
122         } else {
123                 basename = progpath;
124                 absbasedir = malloc(PATH_MAX + 1);
125                 ret = readlink("/proc/self/exe", absbasedir, PATH_MAX);
126                 if (ret < 0) {
127                         perror(__FILE__ ": readlink");
128                         return 2;
129                 }
130                 absbasedir[ret] = '\0';
131                 for (i = ret; i > 0; i--) {
132                         if (absbasedir[i] == '/') {
133                                 absbasedir[i] = '\0';
134                                 if (++count == 3)
135                                         break;
136                         }
137                 }
138         }
139         if (absbasedir == NULL) {
140                 perror(__FILE__ ": realpath");
141                 return 2;
142         }
143
144         /* Fill in the relative paths */
145 #ifdef BR_CROSS_PATH_REL
146         ret = snprintf(path, sizeof(path), "%s/" BR_CROSS_PATH_REL "/%s", absbasedir, basename);
147 #elif BR_CROSS_PATH_ABS
148         ret = snprintf(path, sizeof(path), BR_CROSS_PATH_ABS "/%s", basename);
149 #else /* BR_CROSS_PATH_SUFFIX */
150         ret = snprintf(path, sizeof(path), "%s/usr/bin/%s" BR_CROSS_PATH_SUFFIX, absbasedir, basename);
151 #endif
152         if (ret >= sizeof(path)) {
153                 perror(__FILE__ ": overflow");
154                 return 3;
155         }
156 #ifdef BR_CCACHE
157         ret = snprintf(ccache_path, sizeof(ccache_path), "%s/usr/bin/ccache", absbasedir);
158         if (ret >= sizeof(ccache_path)) {
159                 perror(__FILE__ ": overflow");
160                 return 3;
161         }
162 #endif
163         ret = snprintf(sysroot, sizeof(sysroot), "%s/" BR_SYSROOT, absbasedir);
164         if (ret >= sizeof(sysroot)) {
165                 perror(__FILE__ ": overflow");
166                 return 3;
167         }
168
169         cur = args = malloc(sizeof(predef_args) +
170                             (sizeof(char *) * (argc + EXCLUSIVE_ARGS)));
171         if (args == NULL) {
172                 perror(__FILE__ ": malloc");
173                 return 2;
174         }
175
176         /* start with predefined args */
177         memcpy(cur, predef_args, sizeof(predef_args));
178         cur += sizeof(predef_args) / sizeof(predef_args[0]);
179
180 #ifdef BR_FLOAT_ABI
181         /* add float abi if not overridden in args */
182         for (i = 1; i < argc; i++) {
183                 if (!strncmp(argv[i], "-mfloat-abi=", strlen("-mfloat-abi=")) ||
184                     !strcmp(argv[i], "-msoft-float") ||
185                     !strcmp(argv[i], "-mhard-float"))
186                         break;
187         }
188
189         if (i == argc)
190                 *cur++ = "-mfloat-abi=" BR_FLOAT_ABI;
191 #endif
192
193 #if defined(BR_ARCH) || \
194     defined(BR_CPU)
195         /* Add our -march/cpu flags, but only if none of
196          * -march/mtune/mcpu are already specified on the commandline
197          */
198         for (i = 1; i < argc; i++) {
199                 if (!strncmp(argv[i], "-march=", strlen("-march=")) ||
200                     !strncmp(argv[i], "-mtune=", strlen("-mtune=")) ||
201                     !strncmp(argv[i], "-mcpu=",  strlen("-mcpu=" )))
202                         break;
203         }
204         if (i == argc) {
205 #ifdef BR_ARCH
206                 *cur++ = "-march=" BR_ARCH;
207 #endif
208 #ifdef BR_CPU
209                 *cur++ = "-mcpu=" BR_CPU;
210 #endif
211         }
212 #endif /* ARCH || CPU */
213
214         paranoid_wrapper = getenv("BR_COMPILER_PARANOID_UNSAFE_PATH");
215         if (paranoid_wrapper && strlen(paranoid_wrapper) > 0)
216                 paranoid = 1;
217         else
218                 paranoid = 0;
219
220         /* Check for unsafe library and header paths */
221         for (i = 1; i < argc; i++) {
222
223                 /* Skip options that do not start with -I and -L */
224                 if (strncmp(argv[i], "-I", 2) && strncmp(argv[i], "-L", 2))
225                         continue;
226
227                 /* We handle two cases: first the case where -I/-L and
228                  * the path are separated by one space and therefore
229                  * visible as two separate options, and then the case
230                  * where they are stuck together forming one single
231                  * option.
232                  */
233                 if (argv[i][2] == '\0') {
234                         i++;
235                         if (i == argc)
236                                 continue;
237                         check_unsafe_path(argv[i], paranoid);
238                 } else {
239                         check_unsafe_path(argv[i] + 2, paranoid);
240                 }
241         }
242
243         /* append forward args */
244         memcpy(cur, &argv[1], sizeof(char *) * (argc - 1));
245         cur += argc - 1;
246
247         /* finish with NULL termination */
248         *cur = NULL;
249
250         exec_args = args;
251 #ifdef BR_CCACHE
252         if (getenv("BR_NO_CCACHE"))
253                 /* Skip the ccache call */
254                 exec_args++;
255 #endif
256
257         /* Debug the wrapper to see actual arguments passed to
258          * the compiler:
259          * unset, empty, or 0: do not trace
260          * set to 1          : trace all arguments on a single line
261          * set to 2          : trace one argument per line
262          */
263         if ((env_debug = getenv("BR2_DEBUG_WRAPPER"))) {
264                 debug = atoi(env_debug);
265                 if (debug > 0) {
266                         fprintf(stderr, "Toolchain wrapper executing:");
267 #ifdef BR_CCACHE_HASH
268                         fprintf(stderr, "%sCCACHE_COMPILERCHECK='string:" BR_CCACHE_HASH "'",
269                                 (debug == 2) ? "\n    " : " ");
270 #endif
271                         for (i = 0; exec_args[i]; i++)
272                                 fprintf(stderr, "%s'%s'",
273                                         (debug == 2) ? "\n    " : " ", exec_args[i]);
274                         fprintf(stderr, "\n");
275                 }
276         }
277
278 #ifdef BR_CCACHE_HASH
279         /* Allow compilercheck to be overridden through the environment */
280         if (setenv("CCACHE_COMPILERCHECK", "string:" BR_CCACHE_HASH, 0)) {
281                 perror(__FILE__ ": Failed to set CCACHE_COMPILERCHECK");
282                 return 3;
283         }
284 #endif
285
286         if (execv(exec_args[0], exec_args))
287                 perror(path);
288
289         free(args);
290
291         return 2;
292 }