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 */
69 #ifdef BR_MIPS_TARGET_LITTLE_ENDIAN
72 #if defined(BR_MIPS_TARGET_BIG_ENDIAN) || defined(BR_ARC_TARGET_BIG_ENDIAN)
75 #ifdef BR_ADDITIONAL_CFLAGS
80 static void check_unsafe_path(const char *path, int paranoid)
83 static char *unsafe_paths[] = {
84 "/lib", "/usr/include", "/usr/lib", "/usr/local/include", "/usr/local/lib", NULL,
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);
99 int main(int argc, char **argv)
101 char **args, **cur, **exec_args;
102 char *relbasedir, *absbasedir;
103 char *progpath = argv[0];
106 char *paranoid_wrapper;
108 int ret, i, count = 0, debug;
110 /* Calculate the relative paths */
111 basename = strrchr(progpath, '/');
115 relbasedir = malloc(strlen(progpath) + 7);
116 if (relbasedir == NULL) {
117 perror(__FILE__ ": malloc");
120 sprintf(relbasedir, "%s/../..", argv[0]);
121 absbasedir = realpath(relbasedir, NULL);
124 absbasedir = malloc(PATH_MAX + 1);
125 ret = readlink("/proc/self/exe", absbasedir, PATH_MAX);
127 perror(__FILE__ ": readlink");
130 absbasedir[ret] = '\0';
131 for (i = ret; i > 0; i--) {
132 if (absbasedir[i] == '/') {
133 absbasedir[i] = '\0';
139 if (absbasedir == NULL) {
140 perror(__FILE__ ": realpath");
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);
152 if (ret >= sizeof(path)) {
153 perror(__FILE__ ": overflow");
157 ret = snprintf(ccache_path, sizeof(ccache_path), "%s/usr/bin/ccache", absbasedir);
158 if (ret >= sizeof(ccache_path)) {
159 perror(__FILE__ ": overflow");
163 ret = snprintf(sysroot, sizeof(sysroot), "%s/" BR_SYSROOT, absbasedir);
164 if (ret >= sizeof(sysroot)) {
165 perror(__FILE__ ": overflow");
169 cur = args = malloc(sizeof(predef_args) +
170 (sizeof(char *) * (argc + EXCLUSIVE_ARGS)));
172 perror(__FILE__ ": malloc");
176 /* start with predefined args */
177 memcpy(cur, predef_args, sizeof(predef_args));
178 cur += sizeof(predef_args) / sizeof(predef_args[0]);
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"))
190 *cur++ = "-mfloat-abi=" BR_FLOAT_ABI;
193 #if defined(BR_ARCH) || \
195 /* Add our -march/cpu flags, but only if none of
196 * -march/mtune/mcpu are already specified on the commandline
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=" )))
206 *cur++ = "-march=" BR_ARCH;
209 *cur++ = "-mcpu=" BR_CPU;
212 #endif /* ARCH || CPU */
214 paranoid_wrapper = getenv("BR_COMPILER_PARANOID_UNSAFE_PATH");
215 if (paranoid_wrapper && strlen(paranoid_wrapper) > 0)
220 /* Check for unsafe library and header paths */
221 for (i = 1; i < argc; i++) {
223 /* Skip options that do not start with -I and -L */
224 if (strncmp(argv[i], "-I", 2) && strncmp(argv[i], "-L", 2))
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
233 if (argv[i][2] == '\0') {
237 check_unsafe_path(argv[i], paranoid);
239 check_unsafe_path(argv[i] + 2, paranoid);
243 /* append forward args */
244 memcpy(cur, &argv[1], sizeof(char *) * (argc - 1));
247 /* finish with NULL termination */
252 if (getenv("BR_NO_CCACHE"))
253 /* Skip the ccache call */
257 /* Debug the wrapper to see actual arguments passed to
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
263 if ((env_debug = getenv("BR2_DEBUG_WRAPPER"))) {
264 debug = atoi(env_debug);
266 fprintf(stderr, "Toolchain wrapper executing:");
267 #ifdef BR_CCACHE_HASH
268 fprintf(stderr, "%sCCACHE_COMPILERCHECK='string:" BR_CCACHE_HASH "'",
269 (debug == 2) ? "\n " : " ");
271 for (i = 0; exec_args[i]; i++)
272 fprintf(stderr, "%s'%s'",
273 (debug == 2) ? "\n " : " ", exec_args[i]);
274 fprintf(stderr, "\n");
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");
286 if (execv(exec_args[0], exec_args))