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",
72 #ifdef BR_MIPS_TARGET_LITTLE_ENDIAN
75 #if defined(BR_MIPS_TARGET_BIG_ENDIAN) || defined(BR_ARC_TARGET_BIG_ENDIAN)
78 #ifdef BR_ADDITIONAL_CFLAGS
83 static void check_unsafe_path(const char *path
, int paranoid
)
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
);
102 int main(int argc
, char **argv
)
104 char **args
, **cur
, **exec_args
;
105 char *relbasedir
, *absbasedir
;
106 char *progpath
= argv
[0];
109 char *paranoid_wrapper
;
111 int ret
, i
, count
= 0, debug
;
113 /* Calculate the relative paths */
114 basename
= strrchr(progpath
, '/');
118 relbasedir
= malloc(strlen(progpath
) + 7);
119 if (relbasedir
== NULL
) {
120 perror(__FILE__
": malloc");
123 sprintf(relbasedir
, "%s/../..", argv
[0]);
124 absbasedir
= realpath(relbasedir
, NULL
);
127 absbasedir
= malloc(PATH_MAX
+ 1);
128 ret
= readlink("/proc/self/exe", absbasedir
, PATH_MAX
);
130 perror(__FILE__
": readlink");
133 absbasedir
[ret
] = '\0';
134 for (i
= ret
; i
> 0; i
--) {
135 if (absbasedir
[i
] == '/') {
136 absbasedir
[i
] = '\0';
142 if (absbasedir
== NULL
) {
143 perror(__FILE__
": realpath");
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
);
153 ret
= snprintf(path
, sizeof(path
), "%s/usr/bin/%s" BR_CROSS_PATH_SUFFIX
, absbasedir
, basename
);
155 if (ret
>= sizeof(path
)) {
156 perror(__FILE__
": overflow");
160 ret
= snprintf(ccache_path
, sizeof(ccache_path
), "%s/usr/bin/ccache", absbasedir
);
161 if (ret
>= sizeof(ccache_path
)) {
162 perror(__FILE__
": overflow");
166 ret
= snprintf(sysroot
, sizeof(sysroot
), "%s/" BR_SYSROOT
, absbasedir
);
167 if (ret
>= sizeof(sysroot
)) {
168 perror(__FILE__
": overflow");
172 cur
= args
= malloc(sizeof(predef_args
) +
173 (sizeof(char *) * (argc
+ EXCLUSIVE_ARGS
)));
175 perror(__FILE__
": malloc");
179 /* start with predefined args */
180 memcpy(cur
, predef_args
, sizeof(predef_args
));
181 cur
+= sizeof(predef_args
) / sizeof(predef_args
[0]);
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"))
193 *cur
++ = "-mfloat-abi=" BR_FLOAT_ABI
;
196 #if defined(BR_ARCH) || \
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=" )))
209 *cur
++ = "-march=" BR_ARCH
;
212 *cur
++ = "-mcpu=" BR_CPU
;
215 #endif /* ARCH || CPU */
217 paranoid_wrapper
= getenv("BR_COMPILER_PARANOID_UNSAFE_PATH");
218 if (paranoid_wrapper
&& strlen(paranoid_wrapper
) > 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))
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
236 if (argv
[i
][2] == '\0') {
240 check_unsafe_path(argv
[i
], paranoid
);
242 check_unsafe_path(argv
[i
] + 2, paranoid
);
246 /* append forward args */
247 memcpy(cur
, &argv
[1], sizeof(char *) * (argc
- 1));
250 /* finish with NULL termination */
255 if (getenv("BR_NO_CCACHE"))
256 /* Skip the ccache call */
260 /* Debug the wrapper to see actual arguments passed to
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
);
269 fprintf(stderr
, "Toolchain wrapper executing:");
270 #ifdef BR_CCACHE_HASH
271 fprintf(stderr
, "%sCCACHE_COMPILERCHECK='string:" BR_CCACHE_HASH
"'",
272 (debug
== 2) ? "\n " : " ");
274 #ifdef BR_CCACHE_BASEDIR
275 fprintf(stderr
, "%sCCACHE_BASEDIR='" BR_CCACHE_BASEDIR
"'",
276 (debug
== 2) ? "\n " : " ");
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");
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");
300 if (execv(exec_args
[0], exec_args
))