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
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.
27 static char ccache_path
[PATH_MAX
];
29 static char path
[PATH_MAX
];
30 static char sysroot
[PATH_MAX
];
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
43 #define EXCLUSIVE_ARGS 3
45 static char *predef_args
[] = {
59 #endif /* BR_SOFTFLOAT */
66 #ifdef BR_OMIT_LOCK_PREFIX
67 "-Wa,-momit-lock-prefix=yes",
69 #ifdef BR_NO_FUSED_MADD
75 #ifdef BR_MIPS_TARGET_LITTLE_ENDIAN
78 #if defined(BR_MIPS_TARGET_BIG_ENDIAN) || defined(BR_ARC_TARGET_BIG_ENDIAN)
81 #ifdef BR_ADDITIONAL_CFLAGS
86 /* A {string,length} tuple, to avoid computing strlen() on constants.
87 * - str must be a \0-terminated string
88 * - len does not account for the terminating '\0'
95 /* Define a {string,length} tuple. Takes an unquoted constant string as
96 * parameter. sizeof() on a string literal includes the terminating \0,
97 * but we don't want to count it.
99 #define STR_LEN(s) { #s, sizeof(#s)-1 }
101 /* List of paths considered unsafe for cross-compilation.
103 * An unsafe path is one that points to a directory with libraries or
104 * headers for the build machine, which are not suitable for the target.
106 static const struct str_len_s unsafe_paths
[] = {
108 STR_LEN(/usr
/include
),
110 STR_LEN(/usr
/local
/include
),
111 STR_LEN(/usr
/local
/lib
),
115 /* Unsafe options are options that specify a potentialy unsafe path,
116 * that will be checked by check_unsafe_path(), below.
118 static const struct str_len_s unsafe_opts
[] = {
127 /* Check if path is unsafe for cross-compilation. Unsafe paths are those
128 * pointing to the standard native include or library paths.
130 * We print the arguments leading to the failure. For some options, gcc
131 * accepts the path to be concatenated to the argument (e.g. -I/foo/bar)
132 * or separated (e.g. -I /foo/bar). In the first case, we need only print
133 * the argument as it already contains the path (arg_has_path), while in
134 * the second case we need to print both (!arg_has_path).
136 * If paranoid, exit in error instead of just printing a warning.
138 static void check_unsafe_path(const char *arg
,
143 const struct str_len_s
*p
;
145 for (p
=unsafe_paths
; p
->str
; p
++) {
146 if (strncmp(path
, p
->str
, p
->len
))
149 "%s: %s: unsafe header/library path used in cross-compilation: '%s%s%s'\n",
150 program_invocation_short_name
,
151 paranoid
? "ERROR" : "WARNING",
153 arg_has_path
? "" : "' '", /* close single-quote, space, open single-quote */
154 arg_has_path
? "" : path
); /* so that arg and path are properly quoted. */
160 int main(int argc
, char **argv
)
162 char **args
, **cur
, **exec_args
;
163 char *relbasedir
, *absbasedir
;
164 char *progpath
= argv
[0];
167 char *paranoid_wrapper
;
169 int ret
, i
, count
= 0, debug
;
171 /* Calculate the relative paths */
172 basename
= strrchr(progpath
, '/');
176 relbasedir
= malloc(strlen(progpath
) + 7);
177 if (relbasedir
== NULL
) {
178 perror(__FILE__
": malloc");
181 sprintf(relbasedir
, "%s/../..", argv
[0]);
182 absbasedir
= realpath(relbasedir
, NULL
);
185 absbasedir
= malloc(PATH_MAX
+ 1);
186 ret
= readlink("/proc/self/exe", absbasedir
, PATH_MAX
);
188 perror(__FILE__
": readlink");
191 absbasedir
[ret
] = '\0';
192 for (i
= ret
; i
> 0; i
--) {
193 if (absbasedir
[i
] == '/') {
194 absbasedir
[i
] = '\0';
200 if (absbasedir
== NULL
) {
201 perror(__FILE__
": realpath");
205 /* Fill in the relative paths */
206 #ifdef BR_CROSS_PATH_REL
207 ret
= snprintf(path
, sizeof(path
), "%s/" BR_CROSS_PATH_REL
"/%s" BR_CROSS_PATH_SUFFIX
, absbasedir
, basename
);
208 #elif defined(BR_CROSS_PATH_ABS)
209 ret
= snprintf(path
, sizeof(path
), BR_CROSS_PATH_ABS
"/%s" BR_CROSS_PATH_SUFFIX
, basename
);
211 ret
= snprintf(path
, sizeof(path
), "%s/usr/bin/%s" BR_CROSS_PATH_SUFFIX
, absbasedir
, basename
);
213 if (ret
>= sizeof(path
)) {
214 perror(__FILE__
": overflow");
218 ret
= snprintf(ccache_path
, sizeof(ccache_path
), "%s/usr/bin/ccache", absbasedir
);
219 if (ret
>= sizeof(ccache_path
)) {
220 perror(__FILE__
": overflow");
224 ret
= snprintf(sysroot
, sizeof(sysroot
), "%s/" BR_SYSROOT
, absbasedir
);
225 if (ret
>= sizeof(sysroot
)) {
226 perror(__FILE__
": overflow");
230 cur
= args
= malloc(sizeof(predef_args
) +
231 (sizeof(char *) * (argc
+ EXCLUSIVE_ARGS
)));
233 perror(__FILE__
": malloc");
237 /* start with predefined args */
238 memcpy(cur
, predef_args
, sizeof(predef_args
));
239 cur
+= sizeof(predef_args
) / sizeof(predef_args
[0]);
242 /* add float abi if not overridden in args */
243 for (i
= 1; i
< argc
; i
++) {
244 if (!strncmp(argv
[i
], "-mfloat-abi=", strlen("-mfloat-abi=")) ||
245 !strcmp(argv
[i
], "-msoft-float") ||
246 !strcmp(argv
[i
], "-mhard-float"))
251 *cur
++ = "-mfloat-abi=" BR_FLOAT_ABI
;
254 #if defined(BR_ARCH) || \
256 /* Add our -march/cpu flags, but only if none of
257 * -march/mtune/mcpu are already specified on the commandline
259 for (i
= 1; i
< argc
; i
++) {
260 if (!strncmp(argv
[i
], "-march=", strlen("-march=")) ||
261 !strncmp(argv
[i
], "-mtune=", strlen("-mtune=")) ||
262 !strncmp(argv
[i
], "-mcpu=", strlen("-mcpu=" )))
267 *cur
++ = "-march=" BR_ARCH
;
270 *cur
++ = "-mcpu=" BR_CPU
;
273 #endif /* ARCH || CPU */
275 paranoid_wrapper
= getenv("BR_COMPILER_PARANOID_UNSAFE_PATH");
276 if (paranoid_wrapper
&& strlen(paranoid_wrapper
) > 0)
281 /* Check for unsafe library and header paths */
282 for (i
= 1; i
< argc
; i
++) {
283 const struct str_len_s
*opt
;
284 for (opt
=unsafe_opts
; opt
->str
; opt
++ ) {
285 /* Skip any non-unsafe option. */
286 if (strncmp(argv
[i
], opt
->str
, opt
->len
))
289 /* Handle both cases:
290 * - path is a separate argument,
291 * - path is concatenated with option.
293 if (argv
[i
][opt
->len
] == '\0') {
297 check_unsafe_path(argv
[i
-1], argv
[i
], paranoid
, 0);
299 check_unsafe_path(argv
[i
], argv
[i
] + opt
->len
, paranoid
, 1);
303 /* append forward args */
304 memcpy(cur
, &argv
[1], sizeof(char *) * (argc
- 1));
307 /* finish with NULL termination */
312 if (getenv("BR_NO_CCACHE"))
313 /* Skip the ccache call */
317 /* Debug the wrapper to see actual arguments passed to
319 * unset, empty, or 0: do not trace
320 * set to 1 : trace all arguments on a single line
321 * set to 2 : trace one argument per line
323 if ((env_debug
= getenv("BR2_DEBUG_WRAPPER"))) {
324 debug
= atoi(env_debug
);
326 fprintf(stderr
, "Toolchain wrapper executing:");
327 #ifdef BR_CCACHE_HASH
328 fprintf(stderr
, "%sCCACHE_COMPILERCHECK='string:" BR_CCACHE_HASH
"'",
329 (debug
== 2) ? "\n " : " ");
331 #ifdef BR_CCACHE_BASEDIR
332 fprintf(stderr
, "%sCCACHE_BASEDIR='" BR_CCACHE_BASEDIR
"'",
333 (debug
== 2) ? "\n " : " ");
335 for (i
= 0; exec_args
[i
]; i
++)
336 fprintf(stderr
, "%s'%s'",
337 (debug
== 2) ? "\n " : " ", exec_args
[i
]);
338 fprintf(stderr
, "\n");
342 #ifdef BR_CCACHE_HASH
343 /* Allow compilercheck to be overridden through the environment */
344 if (setenv("CCACHE_COMPILERCHECK", "string:" BR_CCACHE_HASH
, 0)) {
345 perror(__FILE__
": Failed to set CCACHE_COMPILERCHECK");
349 #ifdef BR_CCACHE_BASEDIR
350 /* Allow compilercheck to be overridden through the environment */
351 if (setenv("CCACHE_BASEDIR", BR_CCACHE_BASEDIR
, 0)) {
352 perror(__FILE__
": Failed to set CCACHE_BASEDIR");
357 if (execv(exec_args
[0], exec_args
))