2 * Buildroot wrapper for external toolchains. This simply executes the real
3 * toolchain with a number of arguments (sysroot/arch/..) hardcoded,
4 * to ensure the external 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.
26 static char path[PATH_MAX];
27 static char sysroot[PATH_MAX];
30 * GCC errors out with certain combinations of arguments (examples are
31 * -mfloat-abi={hard|soft} and -m{little|big}-endian), so we have to ensure
32 * that we only pass the predefined one to the real compiler if the inverse
33 * option isn't in the argument list.
34 * This specifies the worst case number of extra arguments we might pass
40 #define EXCLUSIVE_ARGS 3
42 static char *predef_args[] = {
53 #endif /* BR_SOFTFLOAT */
63 #ifdef BR_MIPS_TARGET_LITTLE_ENDIAN
66 #if defined(BR_MIPS_TARGET_BIG_ENDIAN) || defined(BR_ARC_TARGET_BIG_ENDIAN)
69 #ifdef BR_ADDITIONAL_CFLAGS
74 static void check_unsafe_path(const char *path, int paranoid)
77 static char *unsafe_paths[] = {
78 "/lib", "/usr/include", "/usr/lib", "/usr/local/include", "/usr/local/lib", NULL,
81 for (c = unsafe_paths; *c != NULL; c++) {
82 if (!strncmp(path, *c, strlen(*c))) {
83 fprintf(stderr, "%s: %s: unsafe header/library path used in cross-compilation: '%s'\n",
84 program_invocation_short_name,
85 paranoid ? "ERROR" : "WARNING", path);
93 int main(int argc, char **argv)
96 char *relbasedir, *absbasedir;
97 char *progpath = argv[0];
100 char *paranoid_wrapper;
102 int ret, i, count = 0, debug;
104 /* Calculate the relative paths */
105 basename = strrchr(progpath, '/');
109 relbasedir = malloc(strlen(progpath) + 7);
110 if (relbasedir == NULL) {
111 perror(__FILE__ ": malloc");
114 sprintf(relbasedir, "%s/../..", argv[0]);
115 absbasedir = realpath(relbasedir, NULL);
118 absbasedir = malloc(PATH_MAX + 1);
119 ret = readlink("/proc/self/exe", absbasedir, PATH_MAX);
121 perror(__FILE__ ": readlink");
124 absbasedir[ret] = '\0';
125 for (i = ret; i > 0; i--) {
126 if (absbasedir[i] == '/') {
127 absbasedir[i] = '\0';
133 if (absbasedir == NULL) {
134 perror(__FILE__ ": realpath");
138 /* Fill in the relative paths */
139 #ifdef BR_CROSS_PATH_REL
140 ret = snprintf(path, sizeof(path), "%s/" BR_CROSS_PATH_REL "/%s", absbasedir, basename);
141 #else /* BR_CROSS_PATH_ABS */
142 ret = snprintf(path, sizeof(path), BR_CROSS_PATH_ABS "/%s", basename);
144 if (ret >= sizeof(path)) {
145 perror(__FILE__ ": overflow");
148 ret = snprintf(sysroot, sizeof(sysroot), "%s/" BR_SYSROOT, absbasedir);
149 if (ret >= sizeof(sysroot)) {
150 perror(__FILE__ ": overflow");
154 cur = args = malloc(sizeof(predef_args) +
155 (sizeof(char *) * (argc + EXCLUSIVE_ARGS)));
157 perror(__FILE__ ": malloc");
161 /* start with predefined args */
162 memcpy(cur, predef_args, sizeof(predef_args));
163 cur += sizeof(predef_args) / sizeof(predef_args[0]);
166 /* add float abi if not overridden in args */
167 for (i = 1; i < argc; i++) {
168 if (!strncmp(argv[i], "-mfloat-abi=", strlen("-mfloat-abi=")) ||
169 !strcmp(argv[i], "-msoft-float") ||
170 !strcmp(argv[i], "-mhard-float"))
175 *cur++ = "-mfloat-abi=" BR_FLOAT_ABI;
178 #if defined(BR_ARCH) || \
180 /* Add our -march/cpu/abi flags, but only if none are
181 * already specified on the commandline
183 for (i = 1; i < argc; i++) {
184 if (!strncmp(argv[i], "-march=", strlen("-march=")) ||
185 !strncmp(argv[i], "-mcpu=", strlen("-mcpu=" )))
190 *cur++ = "-march=" BR_ARCH;
193 *cur++ = "-mcpu=" BR_CPU;
196 #endif /* ARCH || CPU */
198 paranoid_wrapper = getenv("BR_COMPILER_PARANOID_UNSAFE_PATH");
199 if (paranoid_wrapper && strlen(paranoid_wrapper) > 0)
204 /* Check for unsafe library and header paths */
205 for (i = 1; i < argc; i++) {
207 /* Skip options that do not start with -I and -L */
208 if (strncmp(argv[i], "-I", 2) && strncmp(argv[i], "-L", 2))
211 /* We handle two cases: first the case where -I/-L and
212 * the path are separated by one space and therefore
213 * visible as two separate options, and then the case
214 * where they are stuck together forming one single
217 if (argv[i][2] == '\0') {
221 check_unsafe_path(argv[i], paranoid);
223 check_unsafe_path(argv[i] + 2, paranoid);
227 /* append forward args */
228 memcpy(cur, &argv[1], sizeof(char *) * (argc - 1));
231 /* finish with NULL termination */
234 /* Debug the wrapper to see actual arguments passed to
236 * unset, empty, or 0: do not trace
237 * set to 1 : trace all arguments on a single line
238 * set to 2 : trace one argument per line
240 if ((env_debug = getenv("BR2_DEBUG_WRAPPER"))) {
241 debug = atoi(env_debug);
243 fprintf(stderr, "Toolchain wrapper executing:");
244 for (i = 0; args[i]; i++)
245 fprintf(stderr, "%s'%s'",
246 (debug == 2) ? "\n " : " ", args[i]);
247 fprintf(stderr, "\n");
251 if (execv(path, args))