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.
29 static char ccache_path[PATH_MAX];
31 static char path[PATH_MAX];
32 static char sysroot[PATH_MAX];
33 /* As would be defined by gcc:
34 * https://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html
35 * sizeof() on string literals includes the terminating \0. */
36 static char _time_[sizeof("-D__TIME__=\"HH:MM:SS\"")];
37 static char _date_[sizeof("-D__DATE__=\"MMM DD YYYY\"")];
40 * GCC errors out with certain combinations of arguments (examples are
41 * -mfloat-abi={hard|soft} and -m{little|big}-endian), so we have to ensure
42 * that we only pass the predefined one to the real compiler if the inverse
43 * option isn't in the argument list.
44 * This specifies the worst case number of extra arguments we might pass
45 * Currently, we may have:
51 * -Wno-builtin-macro-redefined
53 #define EXCLUSIVE_ARGS 6
55 static char *predef_args[] = {
72 #endif /* BR_SOFTFLOAT */
79 #ifdef BR_OMIT_LOCK_PREFIX
80 "-Wa,-momit-lock-prefix=yes",
82 #ifdef BR_NO_FUSED_MADD
88 #ifdef BR_MIPS_TARGET_LITTLE_ENDIAN
91 #if defined(BR_MIPS_TARGET_BIG_ENDIAN) || defined(BR_ARC_TARGET_BIG_ENDIAN)
94 #ifdef BR_ADDITIONAL_CFLAGS
99 /* A {string,length} tuple, to avoid computing strlen() on constants.
100 * - str must be a \0-terminated string
101 * - len does not account for the terminating '\0'
108 /* Define a {string,length} tuple. Takes an unquoted constant string as
109 * parameter. sizeof() on a string literal includes the terminating \0,
110 * but we don't want to count it.
112 #define STR_LEN(s) { #s, sizeof(#s)-1 }
114 /* List of paths considered unsafe for cross-compilation.
116 * An unsafe path is one that points to a directory with libraries or
117 * headers for the build machine, which are not suitable for the target.
119 static const struct str_len_s unsafe_paths[] = {
121 STR_LEN(/usr/include),
123 STR_LEN(/usr/local/include),
124 STR_LEN(/usr/local/lib),
128 /* Unsafe options are options that specify a potentialy unsafe path,
129 * that will be checked by check_unsafe_path(), below.
131 static const struct str_len_s unsafe_opts[] = {
140 /* Check if path is unsafe for cross-compilation. Unsafe paths are those
141 * pointing to the standard native include or library paths.
143 * We print the arguments leading to the failure. For some options, gcc
144 * accepts the path to be concatenated to the argument (e.g. -I/foo/bar)
145 * or separated (e.g. -I /foo/bar). In the first case, we need only print
146 * the argument as it already contains the path (arg_has_path), while in
147 * the second case we need to print both (!arg_has_path).
149 * If paranoid, exit in error instead of just printing a warning.
151 static void check_unsafe_path(const char *arg,
156 const struct str_len_s *p;
158 for (p=unsafe_paths; p->str; p++) {
159 if (strncmp(path, p->str, p->len))
162 "%s: %s: unsafe header/library path used in cross-compilation: '%s%s%s'\n",
163 program_invocation_short_name,
164 paranoid ? "ERROR" : "WARNING",
166 arg_has_path ? "" : "' '", /* close single-quote, space, open single-quote */
167 arg_has_path ? "" : path); /* so that arg and path are properly quoted. */
173 /* Returns false if SOURCE_DATE_EPOCH was not defined in the environment.
175 * Returns true if SOURCE_DATE_EPOCH is in the environment and represent
176 * a valid timestamp, in which case the timestamp is formatted into the
177 * global variables _date_ and _time_.
179 * Aborts if SOURCE_DATE_EPOCH was set in the environment but did not
180 * contain a valid timestamp.
182 * Valid values are defined in the spec:
183 * https://reproducible-builds.org/specs/source-date-epoch/
184 * but we further restrict them to be positive or null.
186 bool parse_source_date_epoch_from_env(void)
188 char *epoch_env, *endptr;
192 if ((epoch_env = getenv("SOURCE_DATE_EPOCH")) == NULL)
195 epoch = (time_t) strtoll(epoch_env, &endptr, 10);
196 /* We just need to test if it is incorrect, but we do not
197 * care why it is incorrect.
199 if ((errno != 0) || !*epoch_env || *endptr || (epoch < 0)) {
200 fprintf(stderr, "%s: invalid SOURCE_DATE_EPOCH='%s'\n",
201 program_invocation_short_name,
205 tzset(); /* For localtime_r(), below. */
206 if (localtime_r(&epoch, &epoch_tm) == NULL) {
207 fprintf(stderr, "%s: cannot parse SOURCE_DATE_EPOCH=%s\n",
208 program_invocation_short_name,
209 getenv("SOURCE_DATE_EPOCH"));
212 if (!strftime(_time_, sizeof(_time_), "-D__TIME__=\"%T\"", &epoch_tm)) {
213 fprintf(stderr, "%s: cannot set time from SOURCE_DATE_EPOCH=%s\n",
214 program_invocation_short_name,
215 getenv("SOURCE_DATE_EPOCH"));
218 if (!strftime(_date_, sizeof(_date_), "-D__DATE__=\"%b %e %Y\"", &epoch_tm)) {
219 fprintf(stderr, "%s: cannot set date from SOURCE_DATE_EPOCH=%s\n",
220 program_invocation_short_name,
221 getenv("SOURCE_DATE_EPOCH"));
227 int main(int argc, char **argv)
229 char **args, **cur, **exec_args;
230 char *relbasedir, *absbasedir;
231 char *progpath = argv[0];
234 char *paranoid_wrapper;
236 int ret, i, count = 0, debug;
238 /* Calculate the relative paths */
239 basename = strrchr(progpath, '/');
243 relbasedir = malloc(strlen(progpath) + 7);
244 if (relbasedir == NULL) {
245 perror(__FILE__ ": malloc");
248 sprintf(relbasedir, "%s/..", argv[0]);
249 absbasedir = realpath(relbasedir, NULL);
252 absbasedir = malloc(PATH_MAX + 1);
253 ret = readlink("/proc/self/exe", absbasedir, PATH_MAX);
255 perror(__FILE__ ": readlink");
258 absbasedir[ret] = '\0';
259 for (i = ret; i > 0; i--) {
260 if (absbasedir[i] == '/') {
261 absbasedir[i] = '\0';
267 if (absbasedir == NULL) {
268 perror(__FILE__ ": realpath");
272 /* Fill in the relative paths */
273 #ifdef BR_CROSS_PATH_REL
274 ret = snprintf(path, sizeof(path), "%s/" BR_CROSS_PATH_REL "/%s" BR_CROSS_PATH_SUFFIX, absbasedir, basename);
275 #elif defined(BR_CROSS_PATH_ABS)
276 ret = snprintf(path, sizeof(path), BR_CROSS_PATH_ABS "/%s" BR_CROSS_PATH_SUFFIX, basename);
278 ret = snprintf(path, sizeof(path), "%s/bin/%s" BR_CROSS_PATH_SUFFIX, absbasedir, basename);
280 if (ret >= sizeof(path)) {
281 perror(__FILE__ ": overflow");
285 ret = snprintf(ccache_path, sizeof(ccache_path), "%s/bin/ccache", absbasedir);
286 if (ret >= sizeof(ccache_path)) {
287 perror(__FILE__ ": overflow");
291 ret = snprintf(sysroot, sizeof(sysroot), "%s/" BR_SYSROOT, absbasedir);
292 if (ret >= sizeof(sysroot)) {
293 perror(__FILE__ ": overflow");
297 cur = args = malloc(sizeof(predef_args) +
298 (sizeof(char *) * (argc + EXCLUSIVE_ARGS)));
300 perror(__FILE__ ": malloc");
304 /* start with predefined args */
305 memcpy(cur, predef_args, sizeof(predef_args));
306 cur += sizeof(predef_args) / sizeof(predef_args[0]);
309 /* add float abi if not overridden in args */
310 for (i = 1; i < argc; i++) {
311 if (!strncmp(argv[i], "-mfloat-abi=", strlen("-mfloat-abi=")) ||
312 !strcmp(argv[i], "-msoft-float") ||
313 !strcmp(argv[i], "-mhard-float"))
318 *cur++ = "-mfloat-abi=" BR_FLOAT_ABI;
322 /* add fp32 mode if soft-float is not args or hard-float overrides soft-float */
323 int add_fp32_mode = 1;
324 for (i = 1; i < argc; i++) {
325 if (!strcmp(argv[i], "-msoft-float"))
327 else if (!strcmp(argv[i], "-mhard-float"))
331 if (add_fp32_mode == 1)
332 *cur++ = "-mfp" BR_FP32_MODE;
335 #if defined(BR_ARCH) || \
337 /* Add our -march/cpu flags, but only if none of
338 * -march/mtune/mcpu are already specified on the commandline
340 for (i = 1; i < argc; i++) {
341 if (!strncmp(argv[i], "-march=", strlen("-march=")) ||
342 !strncmp(argv[i], "-mtune=", strlen("-mtune=")) ||
343 !strncmp(argv[i], "-mcpu=", strlen("-mcpu=" )))
348 *cur++ = "-march=" BR_ARCH;
351 *cur++ = "-mcpu=" BR_CPU;
354 #endif /* ARCH || CPU */
356 if (parse_source_date_epoch_from_env()) {
359 /* This has existed since gcc-4.4.0. */
360 *cur++ = "-Wno-builtin-macro-redefined";
363 paranoid_wrapper = getenv("BR_COMPILER_PARANOID_UNSAFE_PATH");
364 if (paranoid_wrapper && strlen(paranoid_wrapper) > 0)
369 /* Check for unsafe library and header paths */
370 for (i = 1; i < argc; i++) {
371 const struct str_len_s *opt;
372 for (opt=unsafe_opts; opt->str; opt++ ) {
373 /* Skip any non-unsafe option. */
374 if (strncmp(argv[i], opt->str, opt->len))
377 /* Handle both cases:
378 * - path is a separate argument,
379 * - path is concatenated with option.
381 if (argv[i][opt->len] == '\0') {
385 check_unsafe_path(argv[i-1], argv[i], paranoid, 0);
387 check_unsafe_path(argv[i], argv[i] + opt->len, paranoid, 1);
391 /* append forward args */
392 memcpy(cur, &argv[1], sizeof(char *) * (argc - 1));
395 /* finish with NULL termination */
400 if (getenv("BR_NO_CCACHE"))
401 /* Skip the ccache call */
405 /* Debug the wrapper to see actual arguments passed to
407 * unset, empty, or 0: do not trace
408 * set to 1 : trace all arguments on a single line
409 * set to 2 : trace one argument per line
411 if ((env_debug = getenv("BR2_DEBUG_WRAPPER"))) {
412 debug = atoi(env_debug);
414 fprintf(stderr, "Toolchain wrapper executing:");
415 #ifdef BR_CCACHE_HASH
416 fprintf(stderr, "%sCCACHE_COMPILERCHECK='string:" BR_CCACHE_HASH "'",
417 (debug == 2) ? "\n " : " ");
419 #ifdef BR_CCACHE_BASEDIR
420 fprintf(stderr, "%sCCACHE_BASEDIR='" BR_CCACHE_BASEDIR "'",
421 (debug == 2) ? "\n " : " ");
423 for (i = 0; exec_args[i]; i++)
424 fprintf(stderr, "%s'%s'",
425 (debug == 2) ? "\n " : " ", exec_args[i]);
426 fprintf(stderr, "\n");
430 #ifdef BR_CCACHE_HASH
431 /* Allow compilercheck to be overridden through the environment */
432 if (setenv("CCACHE_COMPILERCHECK", "string:" BR_CCACHE_HASH, 0)) {
433 perror(__FILE__ ": Failed to set CCACHE_COMPILERCHECK");
437 #ifdef BR_CCACHE_BASEDIR
438 /* Allow compilercheck to be overridden through the environment */
439 if (setenv("CCACHE_BASEDIR", BR_CCACHE_BASEDIR, 0)) {
440 perror(__FILE__ ": Failed to set CCACHE_BASEDIR");
445 if (execv(exec_args[0], exec_args))