Peter Korsgaard | 85dc57f | 2011-04-29 13:09:26 +0200 | [diff] [blame] | 1 | /** |
Arnout Vandecappelle | f6ae243 | 2015-10-04 13:28:41 +0100 | [diff] [blame] | 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. |
Arnout Vandecappelle | befb9a3 | 2012-07-15 01:12:05 +0000 | [diff] [blame] | 5 | * The hardcoded path arguments are defined relative to the actual location |
| 6 | * of the binary. |
Peter Korsgaard | 85dc57f | 2011-04-29 13:09:26 +0200 | [diff] [blame] | 7 | * |
| 8 | * (C) 2011 Peter Korsgaard <jacmet@sunsite.dk> |
Daniel Nyström | e8c46b1 | 2011-06-21 21:54:27 +0200 | [diff] [blame] | 9 | * (C) 2011 Daniel Nyström <daniel.nystrom@timeterminal.se> |
Arnout Vandecappelle | befb9a3 | 2012-07-15 01:12:05 +0000 | [diff] [blame] | 10 | * (C) 2012 Arnout Vandecappelle (Essensium/Mind) <arnout@mind.be> |
Spenser Gilliland | aa86b52 | 2013-07-19 18:31:58 -0500 | [diff] [blame] | 11 | * (C) 2013 Spenser Gilliland <spenser@gillilanding.com> |
Peter Korsgaard | 85dc57f | 2011-04-29 13:09:26 +0200 | [diff] [blame] | 12 | * |
| 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. |
| 16 | */ |
| 17 | |
Thomas Petazzoni | 5aa205e | 2014-12-10 23:53:46 +0100 | [diff] [blame] | 18 | #define _GNU_SOURCE |
Peter Korsgaard | 85dc57f | 2011-04-29 13:09:26 +0200 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <string.h> |
| 21 | #include <limits.h> |
| 22 | #include <unistd.h> |
Daniel Nyström | e8c46b1 | 2011-06-21 21:54:27 +0200 | [diff] [blame] | 23 | #include <stdlib.h> |
Thomas Petazzoni | 5aa205e | 2014-12-10 23:53:46 +0100 | [diff] [blame] | 24 | #include <errno.h> |
Yann E. MORIN | 6393b69 | 2017-10-21 22:31:02 +0200 | [diff] [blame^] | 25 | #include <time.h> |
| 26 | #include <stdbool.h> |
Peter Korsgaard | 85dc57f | 2011-04-29 13:09:26 +0200 | [diff] [blame] | 27 | |
Arnout Vandecappelle | d82f69c | 2015-10-04 16:23:56 +0100 | [diff] [blame] | 28 | #ifdef BR_CCACHE |
| 29 | static char ccache_path[PATH_MAX]; |
| 30 | #endif |
Arnout Vandecappelle | befb9a3 | 2012-07-15 01:12:05 +0000 | [diff] [blame] | 31 | static char path[PATH_MAX]; |
| 32 | static char sysroot[PATH_MAX]; |
Yann E. MORIN | 6393b69 | 2017-10-21 22:31:02 +0200 | [diff] [blame^] | 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\"")]; |
Peter Korsgaard | 85dc57f | 2011-04-29 13:09:26 +0200 | [diff] [blame] | 38 | |
Spenser Gilliland | aa86b52 | 2013-07-19 18:31:58 -0500 | [diff] [blame] | 39 | /** |
| 40 | * GCC errors out with certain combinations of arguments (examples are |
Baruch Siach | 5c76842 | 2014-04-10 08:46:25 +0300 | [diff] [blame] | 41 | * -mfloat-abi={hard|soft} and -m{little|big}-endian), so we have to ensure |
Spenser Gilliland | aa86b52 | 2013-07-19 18:31:58 -0500 | [diff] [blame] | 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 |
Yann E. MORIN | 6393b69 | 2017-10-21 22:31:02 +0200 | [diff] [blame^] | 45 | * Currently, we may have: |
Yann E. MORIN | 2c1dc32 | 2014-01-07 23:46:05 +0100 | [diff] [blame] | 46 | * -mfloat-abi= |
| 47 | * -march= |
Yann E. MORIN | 2c1dc32 | 2014-01-07 23:46:05 +0100 | [diff] [blame] | 48 | * -mcpu= |
Yann E. MORIN | 6393b69 | 2017-10-21 22:31:02 +0200 | [diff] [blame^] | 49 | * -D__TIME__= |
| 50 | * -D__DATE__= |
| 51 | * -Wno-builtin-macro-redefined |
Spenser Gilliland | aa86b52 | 2013-07-19 18:31:58 -0500 | [diff] [blame] | 52 | */ |
Yann E. MORIN | 6393b69 | 2017-10-21 22:31:02 +0200 | [diff] [blame^] | 53 | #define EXCLUSIVE_ARGS 6 |
Spenser Gilliland | aa86b52 | 2013-07-19 18:31:58 -0500 | [diff] [blame] | 54 | |
Daniel Nyström | e8c46b1 | 2011-06-21 21:54:27 +0200 | [diff] [blame] | 55 | static char *predef_args[] = { |
Arnout Vandecappelle | d82f69c | 2015-10-04 16:23:56 +0100 | [diff] [blame] | 56 | #ifdef BR_CCACHE |
| 57 | ccache_path, |
| 58 | #endif |
Peter Korsgaard | 85dc57f | 2011-04-29 13:09:26 +0200 | [diff] [blame] | 59 | path, |
Arnout Vandecappelle | befb9a3 | 2012-07-15 01:12:05 +0000 | [diff] [blame] | 60 | "--sysroot", sysroot, |
Peter Korsgaard | 85dc57f | 2011-04-29 13:09:26 +0200 | [diff] [blame] | 61 | #ifdef BR_ABI |
| 62 | "-mabi=" BR_ABI, |
| 63 | #endif |
Vicente Olivert Riera | 2d8f3fc | 2017-06-28 16:17:10 +0100 | [diff] [blame] | 64 | #ifdef BR_NAN |
| 65 | "-mnan=" BR_NAN, |
| 66 | #endif |
Spenser Gilliland | 27c9370 | 2013-07-19 18:31:57 -0500 | [diff] [blame] | 67 | #ifdef BR_FPU |
Thomas Petazzoni | d774551 | 2013-07-16 10:03:12 +0200 | [diff] [blame] | 68 | "-mfpu=" BR_FPU, |
| 69 | #endif |
Peter Korsgaard | 85dc57f | 2011-04-29 13:09:26 +0200 | [diff] [blame] | 70 | #ifdef BR_SOFTFLOAT |
| 71 | "-msoft-float", |
| 72 | #endif /* BR_SOFTFLOAT */ |
Thomas Petazzoni | 85d0769 | 2013-07-16 10:03:22 +0200 | [diff] [blame] | 73 | #ifdef BR_MODE |
| 74 | "-m" BR_MODE, |
| 75 | #endif |
Arnout Vandecappelle (Essensium/Mind) | a22dc0f | 2012-03-13 23:30:00 +0100 | [diff] [blame] | 76 | #ifdef BR_64 |
| 77 | "-m64", |
| 78 | #endif |
Ray Kinsella | 968f5d5 | 2015-10-19 11:02:52 +0000 | [diff] [blame] | 79 | #ifdef BR_OMIT_LOCK_PREFIX |
| 80 | "-Wa,-momit-lock-prefix=yes", |
| 81 | #endif |
Vicente Olivert Riera | 240564a | 2016-11-09 16:16:57 +0000 | [diff] [blame] | 82 | #ifdef BR_NO_FUSED_MADD |
| 83 | "-mno-fused-madd", |
| 84 | #endif |
Sonic Zhang | 5713382 | 2013-05-03 00:39:34 +0000 | [diff] [blame] | 85 | #ifdef BR_BINFMT_FLAT |
| 86 | "-Wl,-elf2flt", |
| 87 | #endif |
Markos Chandras | f3a2b80 | 2013-10-14 10:52:25 +0100 | [diff] [blame] | 88 | #ifdef BR_MIPS_TARGET_LITTLE_ENDIAN |
| 89 | "-EL", |
| 90 | #endif |
Alexey Brodkin | 2b93fe5 | 2015-03-10 14:50:24 +0300 | [diff] [blame] | 91 | #if defined(BR_MIPS_TARGET_BIG_ENDIAN) || defined(BR_ARC_TARGET_BIG_ENDIAN) |
Markos Chandras | f3a2b80 | 2013-10-14 10:52:25 +0100 | [diff] [blame] | 92 | "-EB", |
| 93 | #endif |
Thomas Petazzoni | b95e436 | 2011-12-31 12:09:33 +0100 | [diff] [blame] | 94 | #ifdef BR_ADDITIONAL_CFLAGS |
| 95 | BR_ADDITIONAL_CFLAGS |
| 96 | #endif |
Peter Korsgaard | 85dc57f | 2011-04-29 13:09:26 +0200 | [diff] [blame] | 97 | }; |
| 98 | |
Yann E. MORIN | 31c093e | 2016-12-04 10:21:55 +0100 | [diff] [blame] | 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' |
| 102 | */ |
| 103 | struct str_len_s { |
| 104 | const char *str; |
Yann E. MORIN | 61cb120 | 2016-08-29 17:53:59 +0200 | [diff] [blame] | 105 | size_t len; |
| 106 | }; |
| 107 | |
Yann E. MORIN | 31c093e | 2016-12-04 10:21:55 +0100 | [diff] [blame] | 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. |
| 111 | */ |
| 112 | #define STR_LEN(s) { #s, sizeof(#s)-1 } |
| 113 | |
Yann E. MORIN | 3eccf76 | 2016-12-04 10:21:56 +0100 | [diff] [blame] | 114 | /* List of paths considered unsafe for cross-compilation. |
| 115 | * |
| 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. |
| 118 | */ |
| 119 | static const struct str_len_s unsafe_paths[] = { |
| 120 | STR_LEN(/lib), |
| 121 | STR_LEN(/usr/include), |
| 122 | STR_LEN(/usr/lib), |
| 123 | STR_LEN(/usr/local/include), |
| 124 | STR_LEN(/usr/local/lib), |
| 125 | { NULL, 0 }, |
| 126 | }; |
| 127 | |
Yann E. MORIN | 61cb120 | 2016-08-29 17:53:59 +0200 | [diff] [blame] | 128 | /* Unsafe options are options that specify a potentialy unsafe path, |
| 129 | * that will be checked by check_unsafe_path(), below. |
Yann E. MORIN | 61cb120 | 2016-08-29 17:53:59 +0200 | [diff] [blame] | 130 | */ |
Yann E. MORIN | 31c093e | 2016-12-04 10:21:55 +0100 | [diff] [blame] | 131 | static const struct str_len_s unsafe_opts[] = { |
| 132 | STR_LEN(-I), |
| 133 | STR_LEN(-idirafter), |
| 134 | STR_LEN(-iquote), |
| 135 | STR_LEN(-isystem), |
| 136 | STR_LEN(-L), |
Yann E. MORIN | 61cb120 | 2016-08-29 17:53:59 +0200 | [diff] [blame] | 137 | { NULL, 0 }, |
| 138 | }; |
| 139 | |
Yann E. MORIN | 105a8c1 | 2016-08-29 17:53:58 +0200 | [diff] [blame] | 140 | /* Check if path is unsafe for cross-compilation. Unsafe paths are those |
| 141 | * pointing to the standard native include or library paths. |
| 142 | * |
| 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). |
| 148 | * |
| 149 | * If paranoid, exit in error instead of just printing a warning. |
| 150 | */ |
| 151 | static void check_unsafe_path(const char *arg, |
| 152 | const char *path, |
| 153 | int paranoid, |
| 154 | int arg_has_path) |
Thomas Petazzoni | 5aa205e | 2014-12-10 23:53:46 +0100 | [diff] [blame] | 155 | { |
Yann E. MORIN | 3eccf76 | 2016-12-04 10:21:56 +0100 | [diff] [blame] | 156 | const struct str_len_s *p; |
Thomas Petazzoni | 5aa205e | 2014-12-10 23:53:46 +0100 | [diff] [blame] | 157 | |
Yann E. MORIN | 3eccf76 | 2016-12-04 10:21:56 +0100 | [diff] [blame] | 158 | for (p=unsafe_paths; p->str; p++) { |
| 159 | if (strncmp(path, p->str, p->len)) |
Thomas Petazzoni | 5aa205e | 2014-12-10 23:53:46 +0100 | [diff] [blame] | 160 | continue; |
Yann E. MORIN | 105a8c1 | 2016-08-29 17:53:58 +0200 | [diff] [blame] | 161 | fprintf(stderr, |
| 162 | "%s: %s: unsafe header/library path used in cross-compilation: '%s%s%s'\n", |
| 163 | program_invocation_short_name, |
| 164 | paranoid ? "ERROR" : "WARNING", |
| 165 | arg, |
| 166 | arg_has_path ? "" : "' '", /* close single-quote, space, open single-quote */ |
| 167 | arg_has_path ? "" : path); /* so that arg and path are properly quoted. */ |
| 168 | if (paranoid) |
| 169 | exit(1); |
Thomas Petazzoni | 5aa205e | 2014-12-10 23:53:46 +0100 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
Yann E. MORIN | 6393b69 | 2017-10-21 22:31:02 +0200 | [diff] [blame^] | 173 | /* Returns false if SOURCE_DATE_EPOCH was not defined in the environment. |
| 174 | * |
| 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_. |
| 178 | * |
| 179 | * Aborts if SOURCE_DATE_EPOCH was set in the environment but did not |
| 180 | * contain a valid timestamp. |
| 181 | * |
| 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. |
| 185 | */ |
| 186 | bool parse_source_date_epoch_from_env(void) |
| 187 | { |
| 188 | char *epoch_env, *endptr; |
| 189 | time_t epoch; |
| 190 | struct tm epoch_tm; |
| 191 | |
| 192 | if ((epoch_env = getenv("SOURCE_DATE_EPOCH")) == NULL) |
| 193 | return false; |
| 194 | errno = 0; |
| 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. |
| 198 | */ |
| 199 | if ((errno != 0) || !*epoch_env || *endptr || (epoch < 0)) { |
| 200 | fprintf(stderr, "%s: invalid SOURCE_DATE_EPOCH='%s'\n", |
| 201 | program_invocation_short_name, |
| 202 | epoch_env); |
| 203 | exit(1); |
| 204 | } |
| 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")); |
| 210 | exit(1); |
| 211 | } |
| 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")); |
| 216 | exit(1); |
| 217 | } |
| 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")); |
| 222 | exit(1); |
| 223 | } |
| 224 | return true; |
| 225 | } |
| 226 | |
Peter Korsgaard | 85dc57f | 2011-04-29 13:09:26 +0200 | [diff] [blame] | 227 | int main(int argc, char **argv) |
| 228 | { |
Arnout Vandecappelle | 792f127 | 2015-10-04 13:28:56 +0100 | [diff] [blame] | 229 | char **args, **cur, **exec_args; |
Arnout Vandecappelle | befb9a3 | 2012-07-15 01:12:05 +0000 | [diff] [blame] | 230 | char *relbasedir, *absbasedir; |
| 231 | char *progpath = argv[0]; |
| 232 | char *basename; |
Yann E. MORIN | 60cb290 | 2013-09-21 00:00:30 +0200 | [diff] [blame] | 233 | char *env_debug; |
Thomas Petazzoni | 5aa205e | 2014-12-10 23:53:46 +0100 | [diff] [blame] | 234 | char *paranoid_wrapper; |
| 235 | int paranoid; |
Yann E. MORIN | 60cb290 | 2013-09-21 00:00:30 +0200 | [diff] [blame] | 236 | int ret, i, count = 0, debug; |
Arnout Vandecappelle | befb9a3 | 2012-07-15 01:12:05 +0000 | [diff] [blame] | 237 | |
| 238 | /* Calculate the relative paths */ |
| 239 | basename = strrchr(progpath, '/'); |
| 240 | if (basename) { |
| 241 | *basename = '\0'; |
| 242 | basename++; |
| 243 | relbasedir = malloc(strlen(progpath) + 7); |
| 244 | if (relbasedir == NULL) { |
| 245 | perror(__FILE__ ": malloc"); |
| 246 | return 2; |
| 247 | } |
Arnout Vandecappelle | 14151d7 | 2017-07-04 16:03:53 +0200 | [diff] [blame] | 248 | sprintf(relbasedir, "%s/..", argv[0]); |
Arnout Vandecappelle | befb9a3 | 2012-07-15 01:12:05 +0000 | [diff] [blame] | 249 | absbasedir = realpath(relbasedir, NULL); |
| 250 | } else { |
| 251 | basename = progpath; |
Patrick Ziegler | 74ae7af | 2013-05-28 23:41:19 +0000 | [diff] [blame] | 252 | absbasedir = malloc(PATH_MAX + 1); |
| 253 | ret = readlink("/proc/self/exe", absbasedir, PATH_MAX); |
| 254 | if (ret < 0) { |
| 255 | perror(__FILE__ ": readlink"); |
| 256 | return 2; |
| 257 | } |
| 258 | absbasedir[ret] = '\0'; |
| 259 | for (i = ret; i > 0; i--) { |
| 260 | if (absbasedir[i] == '/') { |
| 261 | absbasedir[i] = '\0'; |
Arnout Vandecappelle | 015d68c | 2017-07-07 09:43:30 +0200 | [diff] [blame] | 262 | if (++count == 2) |
Patrick Ziegler | 74ae7af | 2013-05-28 23:41:19 +0000 | [diff] [blame] | 263 | break; |
| 264 | } |
| 265 | } |
Arnout Vandecappelle | befb9a3 | 2012-07-15 01:12:05 +0000 | [diff] [blame] | 266 | } |
| 267 | if (absbasedir == NULL) { |
| 268 | perror(__FILE__ ": realpath"); |
| 269 | return 2; |
| 270 | } |
| 271 | |
| 272 | /* Fill in the relative paths */ |
| 273 | #ifdef BR_CROSS_PATH_REL |
Arnout Vandecappelle | 5ce73dc | 2015-10-14 23:05:55 +0200 | [diff] [blame] | 274 | ret = snprintf(path, sizeof(path), "%s/" BR_CROSS_PATH_REL "/%s" BR_CROSS_PATH_SUFFIX, absbasedir, basename); |
Peter Korsgaard | ccdb179 | 2015-10-05 08:25:17 +0200 | [diff] [blame] | 275 | #elif defined(BR_CROSS_PATH_ABS) |
Arnout Vandecappelle | 5ce73dc | 2015-10-14 23:05:55 +0200 | [diff] [blame] | 276 | ret = snprintf(path, sizeof(path), BR_CROSS_PATH_ABS "/%s" BR_CROSS_PATH_SUFFIX, basename); |
| 277 | #else |
Arnout Vandecappelle | 2a47bd3 | 2017-07-10 01:21:18 +0200 | [diff] [blame] | 278 | ret = snprintf(path, sizeof(path), "%s/bin/%s" BR_CROSS_PATH_SUFFIX, absbasedir, basename); |
Arnout Vandecappelle | befb9a3 | 2012-07-15 01:12:05 +0000 | [diff] [blame] | 279 | #endif |
| 280 | if (ret >= sizeof(path)) { |
| 281 | perror(__FILE__ ": overflow"); |
| 282 | return 3; |
| 283 | } |
Arnout Vandecappelle | d82f69c | 2015-10-04 16:23:56 +0100 | [diff] [blame] | 284 | #ifdef BR_CCACHE |
Arnout Vandecappelle | 2a47bd3 | 2017-07-10 01:21:18 +0200 | [diff] [blame] | 285 | ret = snprintf(ccache_path, sizeof(ccache_path), "%s/bin/ccache", absbasedir); |
Arnout Vandecappelle | d82f69c | 2015-10-04 16:23:56 +0100 | [diff] [blame] | 286 | if (ret >= sizeof(ccache_path)) { |
| 287 | perror(__FILE__ ": overflow"); |
| 288 | return 3; |
| 289 | } |
| 290 | #endif |
Arnout Vandecappelle | befb9a3 | 2012-07-15 01:12:05 +0000 | [diff] [blame] | 291 | ret = snprintf(sysroot, sizeof(sysroot), "%s/" BR_SYSROOT, absbasedir); |
| 292 | if (ret >= sizeof(sysroot)) { |
| 293 | perror(__FILE__ ": overflow"); |
| 294 | return 3; |
| 295 | } |
Peter Korsgaard | 85dc57f | 2011-04-29 13:09:26 +0200 | [diff] [blame] | 296 | |
Spenser Gilliland | aa86b52 | 2013-07-19 18:31:58 -0500 | [diff] [blame] | 297 | cur = args = malloc(sizeof(predef_args) + |
| 298 | (sizeof(char *) * (argc + EXCLUSIVE_ARGS))); |
Daniel Nyström | e8c46b1 | 2011-06-21 21:54:27 +0200 | [diff] [blame] | 299 | if (args == NULL) { |
| 300 | perror(__FILE__ ": malloc"); |
| 301 | return 2; |
Peter Korsgaard | 85dc57f | 2011-04-29 13:09:26 +0200 | [diff] [blame] | 302 | } |
| 303 | |
Daniel Nyström | e8c46b1 | 2011-06-21 21:54:27 +0200 | [diff] [blame] | 304 | /* start with predefined args */ |
| 305 | memcpy(cur, predef_args, sizeof(predef_args)); |
| 306 | cur += sizeof(predef_args) / sizeof(predef_args[0]); |
| 307 | |
Spenser Gilliland | aa86b52 | 2013-07-19 18:31:58 -0500 | [diff] [blame] | 308 | #ifdef BR_FLOAT_ABI |
| 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")) |
| 314 | break; |
| 315 | } |
| 316 | |
| 317 | if (i == argc) |
| 318 | *cur++ = "-mfloat-abi=" BR_FLOAT_ABI; |
| 319 | #endif |
| 320 | |
Vicente Olivert Riera | 9a0a0a9 | 2017-06-28 16:17:11 +0100 | [diff] [blame] | 321 | #ifdef BR_FP32_MODE |
| 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")) |
| 326 | add_fp32_mode = 0; |
| 327 | else if (!strcmp(argv[i], "-mhard-float")) |
| 328 | add_fp32_mode = 1; |
| 329 | } |
| 330 | |
| 331 | if (add_fp32_mode == 1) |
| 332 | *cur++ = "-mfp" BR_FP32_MODE; |
| 333 | #endif |
| 334 | |
Yann E. MORIN | 2c1dc32 | 2014-01-07 23:46:05 +0100 | [diff] [blame] | 335 | #if defined(BR_ARCH) || \ |
Yann E. MORIN | 2c1dc32 | 2014-01-07 23:46:05 +0100 | [diff] [blame] | 336 | defined(BR_CPU) |
Thomas De Schampheleire | b62cb78 | 2015-07-26 12:53:07 +0200 | [diff] [blame] | 337 | /* Add our -march/cpu flags, but only if none of |
| 338 | * -march/mtune/mcpu are already specified on the commandline |
Yann E. MORIN | 2c1dc32 | 2014-01-07 23:46:05 +0100 | [diff] [blame] | 339 | */ |
| 340 | for (i = 1; i < argc; i++) { |
| 341 | if (!strncmp(argv[i], "-march=", strlen("-march=")) || |
Thomas De Schampheleire | b62cb78 | 2015-07-26 12:53:07 +0200 | [diff] [blame] | 342 | !strncmp(argv[i], "-mtune=", strlen("-mtune=")) || |
Yann E. MORIN | 2c1dc32 | 2014-01-07 23:46:05 +0100 | [diff] [blame] | 343 | !strncmp(argv[i], "-mcpu=", strlen("-mcpu=" ))) |
| 344 | break; |
| 345 | } |
| 346 | if (i == argc) { |
| 347 | #ifdef BR_ARCH |
| 348 | *cur++ = "-march=" BR_ARCH; |
| 349 | #endif |
Yann E. MORIN | 2c1dc32 | 2014-01-07 23:46:05 +0100 | [diff] [blame] | 350 | #ifdef BR_CPU |
| 351 | *cur++ = "-mcpu=" BR_CPU; |
| 352 | #endif |
| 353 | } |
Thomas Petazzoni | 5715d2d | 2014-10-21 22:27:16 +0200 | [diff] [blame] | 354 | #endif /* ARCH || CPU */ |
Yann E. MORIN | 2c1dc32 | 2014-01-07 23:46:05 +0100 | [diff] [blame] | 355 | |
Yann E. MORIN | 6393b69 | 2017-10-21 22:31:02 +0200 | [diff] [blame^] | 356 | if (parse_source_date_epoch_from_env()) { |
| 357 | *cur++ = _time_; |
| 358 | *cur++ = _date_; |
| 359 | /* This has existed since gcc-4.4.0. */ |
| 360 | *cur++ = "-Wno-builtin-macro-redefined"; |
| 361 | } |
| 362 | |
Thomas Petazzoni | 5aa205e | 2014-12-10 23:53:46 +0100 | [diff] [blame] | 363 | paranoid_wrapper = getenv("BR_COMPILER_PARANOID_UNSAFE_PATH"); |
| 364 | if (paranoid_wrapper && strlen(paranoid_wrapper) > 0) |
| 365 | paranoid = 1; |
| 366 | else |
| 367 | paranoid = 0; |
| 368 | |
| 369 | /* Check for unsafe library and header paths */ |
| 370 | for (i = 1; i < argc; i++) { |
Yann E. MORIN | 31c093e | 2016-12-04 10:21:55 +0100 | [diff] [blame] | 371 | const struct str_len_s *opt; |
| 372 | for (opt=unsafe_opts; opt->str; opt++ ) { |
Yann E. MORIN | 61cb120 | 2016-08-29 17:53:59 +0200 | [diff] [blame] | 373 | /* Skip any non-unsafe option. */ |
Yann E. MORIN | 31c093e | 2016-12-04 10:21:55 +0100 | [diff] [blame] | 374 | if (strncmp(argv[i], opt->str, opt->len)) |
Thomas Petazzoni | 5aa205e | 2014-12-10 23:53:46 +0100 | [diff] [blame] | 375 | continue; |
Yann E. MORIN | 61cb120 | 2016-08-29 17:53:59 +0200 | [diff] [blame] | 376 | |
| 377 | /* Handle both cases: |
| 378 | * - path is a separate argument, |
| 379 | * - path is concatenated with option. |
| 380 | */ |
| 381 | if (argv[i][opt->len] == '\0') { |
| 382 | i++; |
| 383 | if (i == argc) |
| 384 | break; |
| 385 | check_unsafe_path(argv[i-1], argv[i], paranoid, 0); |
| 386 | } else |
| 387 | check_unsafe_path(argv[i], argv[i] + opt->len, paranoid, 1); |
Thomas Petazzoni | 5aa205e | 2014-12-10 23:53:46 +0100 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
Daniel Nyström | e8c46b1 | 2011-06-21 21:54:27 +0200 | [diff] [blame] | 391 | /* append forward args */ |
| 392 | memcpy(cur, &argv[1], sizeof(char *) * (argc - 1)); |
| 393 | cur += argc - 1; |
| 394 | |
| 395 | /* finish with NULL termination */ |
| 396 | *cur = NULL; |
Peter Korsgaard | 85dc57f | 2011-04-29 13:09:26 +0200 | [diff] [blame] | 397 | |
Arnout Vandecappelle | 792f127 | 2015-10-04 13:28:56 +0100 | [diff] [blame] | 398 | exec_args = args; |
| 399 | #ifdef BR_CCACHE |
| 400 | if (getenv("BR_NO_CCACHE")) |
| 401 | /* Skip the ccache call */ |
| 402 | exec_args++; |
| 403 | #endif |
| 404 | |
Yann E. MORIN | 60cb290 | 2013-09-21 00:00:30 +0200 | [diff] [blame] | 405 | /* Debug the wrapper to see actual arguments passed to |
| 406 | * the compiler: |
| 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 |
| 410 | */ |
Yann E. MORIN | 35c666e | 2014-03-03 23:55:05 +0100 | [diff] [blame] | 411 | if ((env_debug = getenv("BR2_DEBUG_WRAPPER"))) { |
Yann E. MORIN | 60cb290 | 2013-09-21 00:00:30 +0200 | [diff] [blame] | 412 | debug = atoi(env_debug); |
| 413 | if (debug > 0) { |
| 414 | fprintf(stderr, "Toolchain wrapper executing:"); |
Arnout Vandecappelle | f4682cf | 2015-10-04 16:25:00 +0100 | [diff] [blame] | 415 | #ifdef BR_CCACHE_HASH |
| 416 | fprintf(stderr, "%sCCACHE_COMPILERCHECK='string:" BR_CCACHE_HASH "'", |
| 417 | (debug == 2) ? "\n " : " "); |
| 418 | #endif |
Arnout Vandecappelle | 1e97b27 | 2015-10-04 16:25:32 +0100 | [diff] [blame] | 419 | #ifdef BR_CCACHE_BASEDIR |
| 420 | fprintf(stderr, "%sCCACHE_BASEDIR='" BR_CCACHE_BASEDIR "'", |
| 421 | (debug == 2) ? "\n " : " "); |
| 422 | #endif |
Arnout Vandecappelle | 792f127 | 2015-10-04 13:28:56 +0100 | [diff] [blame] | 423 | for (i = 0; exec_args[i]; i++) |
Yann E. MORIN | 60cb290 | 2013-09-21 00:00:30 +0200 | [diff] [blame] | 424 | fprintf(stderr, "%s'%s'", |
Arnout Vandecappelle | 792f127 | 2015-10-04 13:28:56 +0100 | [diff] [blame] | 425 | (debug == 2) ? "\n " : " ", exec_args[i]); |
Yann E. MORIN | 60cb290 | 2013-09-21 00:00:30 +0200 | [diff] [blame] | 426 | fprintf(stderr, "\n"); |
| 427 | } |
Yann E. MORIN | aaa06aa | 2013-07-18 23:54:50 +0200 | [diff] [blame] | 428 | } |
| 429 | |
Arnout Vandecappelle | f4682cf | 2015-10-04 16:25:00 +0100 | [diff] [blame] | 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"); |
| 434 | return 3; |
| 435 | } |
| 436 | #endif |
Arnout Vandecappelle | 1e97b27 | 2015-10-04 16:25:32 +0100 | [diff] [blame] | 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"); |
| 441 | return 3; |
| 442 | } |
| 443 | #endif |
Arnout Vandecappelle | f4682cf | 2015-10-04 16:25:00 +0100 | [diff] [blame] | 444 | |
Arnout Vandecappelle | 792f127 | 2015-10-04 13:28:56 +0100 | [diff] [blame] | 445 | if (execv(exec_args[0], exec_args)) |
Peter Korsgaard | 85dc57f | 2011-04-29 13:09:26 +0200 | [diff] [blame] | 446 | perror(path); |
| 447 | |
Daniel Nyström | e8c46b1 | 2011-06-21 21:54:27 +0200 | [diff] [blame] | 448 | free(args); |
| 449 | |
Peter Korsgaard | 85dc57f | 2011-04-29 13:09:26 +0200 | [diff] [blame] | 450 | return 2; |
| 451 | } |