183
178
* Whether we should use the optimization of passing the already existing
184
179
* preprocessed source code to the compiler.
186
static int compile_preprocessed_source_code;
189
* Supported file extensions and corresponding languages (as in parameter to
192
static const struct {
193
const char *extension;
194
const char *language;
197
{".i", "cpp-output"},
198
{".ii", "c++-cpp-output"},
199
{".mi", "objc-cpp-output"},
200
{".mii", "objc++-cpp-output"},
212
{".m", "objective-c"},
213
{".M", "objective-c++"},
214
{".mm", "objective-c++"},
218
* Supported languages and corresponding preprocessed languages.
220
static const struct {
221
const char *language;
222
const char *p_language;
225
{"cpp-output", "cpp-output"},
226
{"c++", "c++-cpp-output"},
227
{"c++-cpp-output", "c++-cpp-output"},
228
{"objective-c", "objc-cpp-output"},
229
{"objc-cpp-output", "objc-cpp-output"},
230
{"objective-c++", "objc++-cpp-output"},
231
{"objc++-cpp-output", "objc++-cpp-output"},
181
static bool compile_preprocessed_source_code;
183
/* Whether the output is a precompiled header */
184
static bool output_is_precompiled_header = false;
187
* Whether we are using a precompiled header (either via -include or #include).
189
static bool using_precompiled_header = false;
191
/* How long (in microseconds) to wait before breaking a stale lock. */
192
unsigned lock_staleness_limit = 2000000;
234
194
enum fromcache_call_mode {
235
195
FROMCACHE_DIRECT_MODE,
247
207
static const char HASH_PREFIX[] = "3";
250
something went badly wrong - just execute the real compiler
252
static void failed(void)
209
/* Something went badly wrong - just execute the real compiler. */
256
/* delete intermediate pre-processor file if needed */
258
if (!direct_i_file) {
265
/* delete the cpp stderr file if necessary */
272
215
/* strip any local args */
273
216
args_strip(orig_args, "--ccache-");
275
if ((e=getenv("CCACHE_PREFIX"))) {
218
if ((e = getenv("CCACHE_PREFIX"))) {
276
219
char *p = find_executable(e, MYNAME);
221
fatal("%s: %s", e, strerror(errno));
281
223
args_add_prefix(orig_args, p);
284
226
cc_log("Failed; falling back to running the real compiler");
285
cc_log_executed_command(orig_args->argv);
227
cc_log_argv("Executing ", orig_args->argv);
286
229
execv(orig_args->argv[0], orig_args->argv);
287
cc_log("execv returned (%s)!", strerror(errno));
288
perror(orig_args->argv[0]);
230
fatal("%s: execv returned (%s)", orig_args->argv[0], strerror(errno));
236
/* delete intermediate pre-processor file if needed */
238
if (!direct_i_file) {
239
tmp_unlink(i_tmpfile);
245
/* delete the cpp stderr file if necessary */
247
tmp_unlink(cpp_stderr);
293
254
* Transform a name to a full path into the cache directory, creating needed
294
255
* sublevels if needed. Caller frees.
296
static char *get_path_in_cache(const char *name, const char *suffix)
258
get_path_in_cache(const char *name, const char *suffix)
313
x_asprintf(&result, "%s/%s%s", path, name + nlevels, suffix);
274
result = format("%s/%s%s", path, name + nlevels, suffix);
319
280
* This function hashes an include file and stores the path and hash in the
320
* global included_files variable. Takes over ownership of path.
281
* global included_files variable. If the include file is a PCH, cpp_hash is
282
* also updated. Takes over ownership of path.
322
static void remember_include_file(char *path, size_t path_len)
285
remember_include_file(char *path, size_t path_len, struct mdfour *cpp_hash)
325
287
struct mdfour fhash;
328
char *data = (char *)-1;
332
if (!included_files) {
336
294
if (path_len >= 2 && (path[0] == '<' && path[path_len - 1] == '>')) {
337
295
/* Typically <built-in> or <command-line>. */
341
if (strcmp(path, input_file) == 0) {
299
if (str_eq(path, input_file)) {
342
300
/* Don't remember the input file. */
351
/* Let's hash the include file. */
352
fd = open(path, O_RDONLY|O_BINARY);
354
cc_log("Failed to open include file %s", path);
357
if (fstat(fd, &st) != 0) {
358
cc_log("Failed to fstat include file %s", path);
309
if (stat(path, &st) != 0) {
310
cc_log("Failed to stat include file %s", path);
361
313
if (S_ISDIR(st.st_mode)) {
362
314
/* Ignore directory, typically $PWD. */
317
if (!S_ISREG(st.st_mode)) {
318
/* Device, pipe, socket or other strange creature. */
319
cc_log("Non-regular include file %s", path);
323
/* Let's hash the include file. */
365
324
if (!(sloppiness & SLOPPY_INCLUDE_FILE_MTIME)
366
325
&& st.st_mtime >= time_of_compilation) {
367
326
cc_log("Include file %s too new", path);
370
if (st.st_size > 0) {
371
data = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
372
if (data == (char *)-1) {
373
cc_log("Failed to mmap %s", path);
332
is_pch = is_precompiled_header(path);
334
struct file_hash pch_hash;
335
if (!hash_file(&fhash, path)) {
338
hash_result_as_bytes(&fhash, pch_hash.hash);
339
pch_hash.size = fhash.totalN;
340
hash_delimiter(cpp_hash, "pch_hash");
341
hash_buffer(cpp_hash, pch_hash.hash, sizeof(pch_hash.hash));
346
if (!is_pch) { /* else: the file has already been hashed. */
347
if (st.st_size > 0) {
348
if (!read_file(path, st.st_size, &source, &size)) {
352
source = x_strdup("");
356
result = hash_source_code_string(&fhash, source, size, path);
357
if (result & HASH_SOURCE_CODE_ERROR
358
|| result & HASH_SOURCE_CODE_FOUND_TIME) {
363
h = x_malloc(sizeof(*h));
364
hash_result_as_bytes(&fhash, h->hash);
365
h->size = fhash.totalN;
366
hashtable_insert(included_files, path, h);
383
result = hash_source_code_string(&fhash, source, st.st_size, path);
384
if (result & HASH_SOURCE_CODE_ERROR
385
|| result & HASH_SOURCE_CODE_FOUND_TIME) {
389
h = x_malloc(sizeof(*h));
390
hash_result_as_bytes(&fhash, h->hash);
391
h->size = fhash.totalN;
392
hashtable_insert(included_files, path, h);
393
munmap(data, st.st_size);
397
375
cc_log("Disabling direct mode");
376
enable_direct = false;
399
377
/* Fall through. */
402
if (data != (char *)-1) {
403
munmap(data, st.st_size);
411
384
* Make a relative path from CCACHE_BASEDIR to path. Takes over ownership of
412
385
* path. Caller frees.
414
static char *make_relative_path(char *path)
388
make_relative_path(char *path)
418
if (!base_dir || strncmp(path, base_dir, strlen(base_dir)) != 0) {
392
if (!base_dir || !str_startswith(path, base_dir)) {
433
407
* - Stores the paths and hashes of included files in the global variable
434
408
* included_files.
436
static int process_preprocessed_file(struct mdfour *hash, const char *path)
411
process_preprocessed_file(struct mdfour *hash, const char *path)
440
414
char *p, *q, *end;
444
fd = open(path, O_RDONLY);
446
cc_log("Failed to open %s", path);
449
if (fstat(fd, &st) != 0) {
450
cc_log("Failed to fstat %s", path);
454
data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
456
if (data == (void *)-1) {
457
cc_log("Failed to mmap %s", path);
462
included_files = create_hashtable(1000, hash_from_string,
417
if (!read_file(path, 0, &data, &size)) {
421
included_files = create_hashtable(1000, hash_from_string, strings_equal);
466
423
/* Bytes between p and q are pending to be hashed. */
467
424
end = data + size;
527
493
hash_buffer(hash, p, (end - p));
533
* Guess the language of a file based on its extension. Returns NULL if the
534
* extension is unknown.
537
language_for_file(const char *fname)
542
p = get_extension(fname);
543
for (i = 0; extensions[i].extension; i++) {
544
if (strcmp(p, extensions[i].extension) == 0) {
545
return extensions[i].language;
552
* Return the preprocessed language for a given language, or NULL if unknown.
555
p_language_for_language(const char *language)
562
for (i = 0; languages[i].language; ++i) {
563
if (strcmp(language, languages[i].language) == 0) {
564
return languages[i].p_language;
571
* Return the default file extension (including dot) for a language, or NULL if
575
extension_for_language(const char *language)
582
for (i = 0; extensions[i].extension; i++) {
583
if (strcmp(language, extensions[i].language) == 0) {
584
return extensions[i].extension;
591
language_is_supported(const char *language)
593
return p_language_for_language(language) != NULL;
597
language_is_preprocessed(const char *language)
599
return strcmp(language, p_language_for_language(language)) == 0;
602
498
/* run the real compiler and put the result in cache */
603
static void to_cache(ARGS *args)
500
to_cache(struct args *args)
605
502
char *tmp_stdout, *tmp_stderr, *tmp_obj;
651
548
int fd_cpp_stderr;
652
549
int fd_real_stderr;
553
tmp_stderr2 = format("%s.tmp.stderr2.%s", cached_obj, tmp_string());
554
if (x_rename(tmp_stderr, tmp_stderr2)) {
555
cc_log("Failed to rename %s to %s", tmp_stderr, tmp_stderr2);
655
558
fd_cpp_stderr = open(cpp_stderr, O_RDONLY | O_BINARY);
656
559
if (fd_cpp_stderr == -1) {
657
560
cc_log("Failed opening %s", cpp_stderr);
660
fd_real_stderr = open(tmp_stderr, O_RDONLY | O_BINARY);
563
fd_real_stderr = open(tmp_stderr2, O_RDONLY | O_BINARY);
661
564
if (fd_real_stderr == -1) {
662
cc_log("Failed opening %s", tmp_stderr);
565
cc_log("Failed opening %s", tmp_stderr2);
666
fd_result = open(tmp_stderr,
667
O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
568
fd_result = open(tmp_stderr, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
669
569
if (fd_result == -1) {
670
570
cc_log("Failed opening %s", tmp_stderr);
987
886
struct file_hash *object_hash = NULL;
989
888
/* first the arguments */
990
for (i=1;i<args->argc;i++) {
889
for (i = 1; i < args->argc; i++) {
991
890
/* -L doesn't affect compilation. */
992
if (i < args->argc-1 && strcmp(args->argv[i], "-L") == 0) {
891
if (i < args->argc-1 && str_eq(args->argv[i], "-L")) {
996
if (strncmp(args->argv[i], "-L", 2) == 0) {
895
if (str_startswith(args->argv[i], "-L")) {
1000
899
/* When using the preprocessor, some arguments don't contribute
1001
900
to the hash. The theory is that these arguments will change
1002
901
the output of -E if they are going to have any effect at
1005
if (i < args->argc-1) {
1006
if (strcmp(args->argv[i], "-D") == 0 ||
1007
strcmp(args->argv[i], "-I") == 0 ||
1008
strcmp(args->argv[i], "-U") == 0 ||
1009
strcmp(args->argv[i], "-idirafter") == 0 ||
1010
strcmp(args->argv[i], "-imacros") == 0 ||
1011
strcmp(args->argv[i], "-imultilib") == 0 ||
1012
strcmp(args->argv[i], "-include") == 0 ||
1013
strcmp(args->argv[i], "-iprefix") == 0 ||
1014
strcmp(args->argv[i], "-iquote") == 0 ||
1015
strcmp(args->argv[i], "-isysroot") == 0 ||
1016
strcmp(args->argv[i], "-isystem") == 0 ||
1017
strcmp(args->argv[i], "-iwithprefix") == 0 ||
1018
strcmp(args->argv[i], "-iwithprefixbefore") == 0 ||
1019
strcmp(args->argv[i], "-nostdinc") == 0 ||
1020
strcmp(args->argv[i], "-nostdinc++") == 0) {
1021
/* Skip from hash. */
902
all. For precompiled headers this might not be the case. */
903
if (!direct_mode && !output_is_precompiled_header
904
&& !using_precompiled_header) {
905
if (compopt_affects_cpp(args->argv[i])) {
1026
if (strncmp(args->argv[i], "-D", 2) == 0 ||
1027
strncmp(args->argv[i], "-I", 2) == 0 ||
1028
strncmp(args->argv[i], "-U", 2) == 0) {
1029
/* Skip from hash. */
909
if (compopt_short(compopt_affects_cpp, args->argv[i])) {
1034
if (strncmp(args->argv[i], "--specs=", 8) == 0 &&
914
if (str_startswith(args->argv[i], "--specs=") &&
1035
915
stat(args->argv[i] + 8, &st) == 0) {
1036
916
/* If given a explicit specs file, then hash that file,
1037
917
but don't include the path to it in the hash. */
1318
1177
stats_update(STATS_COMPILER);
1319
1178
fatal("Could not find compiler \"%s\" in PATH", base);
1321
if (strcmp(compiler, argv[0]) == 0) {
1322
fatal("Recursive invocation (the name of the ccache binary"
1323
" must be \"%s\")", MYNAME);
1180
if (str_eq(compiler, argv[0])) {
1181
fatal("Recursive invocation (the name of the ccache binary must be \"%s\")",
1325
1184
orig_args->argv[0] = compiler;
1188
is_precompiled_header(const char *path)
1190
return str_eq(get_extension(path), ".gch");
1329
process the compiler options to form the correct set of options
1330
for obtaining the preprocessor output
1332
static void process_args(int argc, char **argv, ARGS **preprocessor_args,
1333
ARGS **compiler_args)
1194
* Process the compiler options into options suitable for passing to the
1195
* preprocessor and the real compiler. The preprocessor options don't include
1196
* -E; this is added later. Returns true on success, otherwise false.
1199
cc_process_args(struct args *orig_args, struct args **preprocessor_args,
1200
struct args **compiler_args)
1336
int found_c_opt = 0;
1337
int found_S_opt = 0;
1338
int found_arch_opt = 0;
1203
bool found_c_opt = false;
1204
bool found_S_opt = false;
1205
bool found_arch_opt = false;
1206
bool found_pch = false;
1207
bool found_fpch_preprocess = false;
1339
1208
const char *explicit_language = NULL; /* As specified with -x. */
1340
1209
const char *file_language; /* As deduced from file extension. */
1341
1210
const char *actual_language; /* Language to actually use. */
1342
1211
const char *input_charset = NULL;
1343
1212
struct stat st;
1344
1213
/* is the dependency makefile name overridden with -MF? */
1345
int dependency_filename_specified = 0;
1214
bool dependency_filename_specified = false;
1346
1215
/* is the dependency makefile target name specified with -MT or -MQ? */
1347
int dependency_target_specified = 0;
1348
ARGS *stripped_args;
1216
bool dependency_target_specified = false;
1217
struct args *stripped_args = NULL, *dep_args = NULL;
1218
int argc = orig_args->argc;
1219
char **argv = orig_args->argv;
1350
1222
stripped_args = args_init(0, NULL);
1223
dep_args = args_init(0, NULL);
1352
1225
args_add(stripped_args, argv[0]);
1354
for (i=1; i<argc; i++) {
1355
/* some options will never work ... */
1356
if (strcmp(argv[i], "-E") == 0) {
1357
cc_log("Compiler option -E is unsupported");
1358
stats_update(STATS_UNSUPPORTED);
1227
for (i = 1; i < argc; i++) {
1228
/* The user knows best: just swallow the next arg */
1229
if (str_eq(argv[i], "--ccache-skip")) {
1232
cc_log("--ccache-skip lacks an argument");
1236
args_add(stripped_args, argv[i]);
1362
/* these are too hard */
1363
if (strncmp(argv[i], "@", 1) == 0 ||
1364
strcmp(argv[i], "--coverage") == 0 ||
1365
strcmp(argv[i], "-M") == 0 ||
1366
strcmp(argv[i], "-MM") == 0 ||
1367
strcmp(argv[i], "-fbranch-probabilities") == 0 ||
1368
strcmp(argv[i], "-fprofile-arcs") == 0 ||
1369
strcmp(argv[i], "-fprofile-generate") == 0 ||
1370
strcmp(argv[i], "-fprofile-use") == 0 ||
1371
strcmp(argv[i], "-frepo") == 0 ||
1372
strcmp(argv[i], "-ftest-coverage") == 0 ||
1373
strcmp(argv[i], "-save-temps") == 0) {
1240
/* These are always too hard. */
1241
if (compopt_too_hard(argv[i])
1242
|| str_startswith(argv[i], "@")
1243
|| str_startswith(argv[i], "-fdump-")) {
1374
1244
cc_log("Compiler option %s is unsupported", argv[i]);
1375
1245
stats_update(STATS_UNSUPPORTED);
1380
1250
/* These are too hard in direct mode. */
1381
1251
if (enable_direct) {
1382
if (strcmp(argv[i], "-Xpreprocessor") == 0) {
1383
cc_log("Unsupported compiler option for direct"
1384
" mode: %s", argv[i]);
1252
if (compopt_too_hard_for_direct_mode(argv[i])) {
1253
cc_log("Unsupported compiler option for direct mode: %s", argv[i]);
1254
enable_direct = false;
1389
1258
/* Multiple -arch options are too hard. */
1390
if (strcmp(argv[i], "-arch") == 0) {
1259
if (str_eq(argv[i], "-arch")) {
1391
1260
if (found_arch_opt) {
1392
cc_log("More than one -arch compiler option"
1261
cc_log("More than one -arch compiler option is unsupported");
1394
1262
stats_update(STATS_UNSUPPORTED);
1266
found_arch_opt = true;
1270
if (str_eq(argv[i], "-fpch-preprocess")) {
1271
found_fpch_preprocess = true;
1401
1274
/* we must have -c */
1402
if (strcmp(argv[i], "-c") == 0) {
1275
if (str_eq(argv[i], "-c")) {
1403
1276
args_add(stripped_args, argv[i]);
1408
1281
/* -S changes the default extension */
1409
if (strcmp(argv[i], "-S") == 0) {
1282
if (str_eq(argv[i], "-S")) {
1410
1283
args_add(stripped_args, argv[i]);
1456
1331
/* debugging is handled specially, so that we know if we
1457
1332
can strip line number info
1459
if (strncmp(argv[i], "-g", 2) == 0) {
1334
if (str_startswith(argv[i], "-g")) {
1460
1335
args_add(stripped_args, argv[i]);
1461
if (enable_unify && strcmp(argv[i], "-g0") != 0) {
1462
cc_log("%s used; disabling unify mode",
1336
if (enable_unify && !str_eq(argv[i], "-g0")) {
1337
cc_log("%s used; disabling unify mode", argv[i]);
1338
enable_unify = false;
1466
if (strcmp(argv[i], "-g3") == 0) {
1340
if (str_eq(argv[i], "-g3")) {
1468
1342
* Fix for bug 7190 ("commandline macros (-D)
1469
1343
* have non-zero lineno when using -g3").
1471
cc_log("%s used; not compiling preprocessed"
1473
compile_preprocessed_source_code = 0;
1478
/* The user knows best: just swallow the next arg */
1479
if (strcmp(argv[i], "--ccache-skip") == 0) {
1482
cc_log("--ccache-skip lacks an argument");
1485
args_add(stripped_args, argv[i]);
1345
cc_log("%s used; not compiling preprocessed code", argv[i]);
1346
compile_preprocessed_source_code = false;
1489
1351
/* These options require special handling, because they
1490
1352
behave differently with gcc -E, when the output
1491
1353
file is not specified. */
1492
if (strcmp(argv[i], "-MD") == 0
1493
|| strcmp(argv[i], "-MMD") == 0) {
1494
generating_dependencies = 1;
1354
if (str_eq(argv[i], "-MD") || str_eq(argv[i], "-MMD")) {
1355
generating_dependencies = true;
1356
args_add(dep_args, argv[i]);
1496
1359
if (i < argc - 1) {
1497
if (strcmp(argv[i], "-MF") == 0) {
1498
dependency_filename_specified = 1;
1499
output_dep = make_relative_path(
1500
x_strdup(argv[i + 1]));
1501
} else if (strcmp(argv[i], "-MQ") == 0
1502
|| strcmp(argv[i], "-MT") == 0) {
1503
dependency_target_specified = 1;
1360
if (str_startswith(argv[i], "-MF")) {
1362
dependency_filename_specified = true;
1364
args_add(dep_args, argv[i]);
1365
if (strlen(argv[i]) == 3) {
1368
args_add(dep_args, argv[i + 1]);
1374
output_dep = make_relative_path(x_strdup(arg));
1376
} else if (str_startswith(argv[i], "-MQ")
1377
|| str_startswith(argv[i], "-MT")) {
1378
dependency_target_specified = true;
1379
args_add(dep_args, argv[i]);
1380
if (strlen(argv[i]) == 3) {
1381
/* -MQ arg or -MT arg */
1382
args_add(dep_args, argv[i + 1]);
1507
if (strncmp(argv[i], "-Wp,", 4) == 0) {
1508
if (strncmp(argv[i], "-Wp,-MD,", 8) == 0
1509
&& !strchr(argv[i] + 8, ',')) {
1510
generating_dependencies = 1;
1511
dependency_filename_specified = 1;
1512
output_dep = make_relative_path(
1513
x_strdup(argv[i] + 8));
1514
} else if (strncmp(argv[i], "-Wp,-MMD,", 9) == 0
1515
&& !strchr(argv[i] + 9, ',')) {
1516
generating_dependencies = 1;
1517
dependency_filename_specified = 1;
1518
output_dep = make_relative_path(
1519
x_strdup(argv[i] + 9));
1388
if (str_startswith(argv[i], "-Wp,")) {
1389
if (str_startswith(argv[i], "-Wp,-MD,") && !strchr(argv[i] + 8, ',')) {
1390
generating_dependencies = true;
1391
dependency_filename_specified = true;
1393
output_dep = make_relative_path(x_strdup(argv[i] + 8));
1394
args_add(dep_args, argv[i]);
1396
} else if (str_startswith(argv[i], "-Wp,-MMD,")
1397
&& !strchr(argv[i] + 9, ',')) {
1398
generating_dependencies = true;
1399
dependency_filename_specified = true;
1401
output_dep = make_relative_path(x_strdup(argv[i] + 9));
1402
args_add(dep_args, argv[i]);
1520
1404
} else if (enable_direct) {
1522
1406
* -Wp, can be used to pass too hard options to
1523
1407
* the preprocessor. Hence, disable direct
1526
cc_log("Unsupported compiler option for direct mode: %s",
1410
cc_log("Unsupported compiler option for direct mode: %s", argv[i]);
1411
enable_direct = false;
1414
if (str_eq(argv[i], "-MP")) {
1415
args_add(dep_args, argv[i]);
1532
1419
/* Input charset needs to be handled specially. */
1533
if (strncmp(argv[i], "-finput-charset=", 16) == 0) {
1420
if (str_startswith(argv[i], "-finput-charset=")) {
1534
1421
input_charset = argv[i];
1541
1428
* is that paths in the standard error output produced by the
1542
1429
* compiler will be normalized.
1545
const char *opts[] = {
1546
"-I", "-idirafter", "-imacros", "-include",
1547
"-iprefix", "-isystem", NULL
1431
if (compopt_takes_path(argv[i])) {
1551
for (j = 0; opts[j]; j++) {
1552
if (strcmp(argv[i], opts[j]) == 0) {
1554
cc_log("Missing argument to %s",
1556
stats_update(STATS_ARGS);
1560
args_add(stripped_args, argv[i]);
1561
relpath = make_relative_path(x_strdup(argv[i+1]));
1562
args_add(stripped_args, relpath);
1435
cc_log("Missing argument to %s", argv[i]);
1436
stats_update(STATS_ARGS);
1441
args_add(stripped_args, argv[i]);
1442
relpath = make_relative_path(x_strdup(argv[i+1]));
1443
args_add(stripped_args, relpath);
1445
/* Try to be smart about detecting precompiled headers */
1446
pchpath = format("%s.gch", argv[i+1]);
1447
if (stat(pchpath, &st) == 0) {
1448
cc_log("Detected use of precompiled header: %s", pchpath);
1573
1458
/* Same as above but options with concatenated argument. */
1575
const char *opts[] = {"-I", NULL};
1459
if (compopt_short(compopt_takes_path, argv[i])) {
1579
for (j = 0; opts[j]; j++) {
1580
if (strncmp(argv[i], opts[j], strlen(opts[j])) == 0) {
1581
relpath = make_relative_path(
1582
x_strdup(argv[i] + strlen(opts[j])));
1583
x_asprintf(&option, "%s%s", opts[j], relpath);
1584
args_add(stripped_args, option);
1462
relpath = make_relative_path(x_strdup(argv[i] + 2));
1463
option = format("-%c%s", argv[i][1], relpath);
1464
args_add(stripped_args, option);
1595
1470
/* options that take an argument */
1597
const char *opts[] = {
1613
"-iwithprefixbefore",
1618
for (j = 0; opts[j]; j++) {
1619
if (strcmp(argv[i], opts[j]) == 0) {
1621
cc_log("Missing argument to %s",
1623
stats_update(STATS_ARGS);
1627
args_add(stripped_args, argv[i]);
1628
args_add(stripped_args, argv[i+1]);
1471
if (compopt_takes_arg(argv[i])) {
1473
cc_log("Missing argument to %s", argv[i]);
1474
stats_update(STATS_ARGS);
1633
if (opts[j]) continue;
1478
args_add(stripped_args, argv[i]);
1479
args_add(stripped_args, argv[i+1]);
1636
1484
/* other options */
1675
1523
if (!input_file) {
1676
1524
cc_log("No input file found");
1677
1525
stats_update(STATS_NOINPUT);
1682
cc_log("No -c option found");
1683
/* I find that having a separate statistic for autoconf tests is useful,
1684
as they are the dominant form of "called for link" in many cases */
1685
if (strstr(input_file, "conftest.")) {
1686
stats_update(STATS_CONFTEST);
1688
stats_update(STATS_LINK);
1530
if (found_pch || found_fpch_preprocess) {
1531
using_precompiled_header = true;
1532
if (!(sloppiness & SLOPPY_TIME_MACROS)) {
1533
cc_log("You have to specify \"time_macros\" sloppiness when using"
1534
" precompiled headers to get direct hits");
1535
cc_log("Disabling direct mode");
1536
stats_update(STATS_CANTUSEPCH);
1693
if (explicit_language && strcmp(explicit_language, "none") == 0) {
1542
if (explicit_language && str_eq(explicit_language, "none")) {
1694
1543
explicit_language = NULL;
1696
1545
file_language = language_for_file(input_file);
1698
1547
if (!language_is_supported(explicit_language)) {
1699
1548
cc_log("Unsupported language: %s", explicit_language);
1700
1549
stats_update(STATS_SOURCELANG);
1703
1553
actual_language = explicit_language;
1705
1555
actual_language = file_language;
1558
output_is_precompiled_header =
1559
actual_language && strstr(actual_language, "-header") != NULL;
1561
if (!found_c_opt && !output_is_precompiled_header) {
1562
cc_log("No -c option found");
1563
/* I find that having a separate statistic for autoconf tests is useful,
1564
as they are the dominant form of "called for link" in many cases */
1565
if (strstr(input_file, "conftest.")) {
1566
stats_update(STATS_CONFTEST);
1568
stats_update(STATS_LINK);
1707
1574
if (!actual_language) {
1708
1575
cc_log("Unsupported source extension: %s", input_file);
1709
1576
stats_update(STATS_SOURCELANG);
1712
1581
direct_i_file = language_is_preprocessed(actual_language);
1583
if (output_is_precompiled_header) {
1584
/* It doesn't work to create the .gch from preprocessed source. */
1585
cc_log("Creating precompiled header; not compiling preprocessed code");
1586
compile_preprocessed_source_code = false;
1714
1589
i_extension = getenv("CCACHE_EXTENSION");
1715
1590
if (!i_extension) {
1716
1591
const char *p_language = p_language_for_language(actual_language);
1720
1595
/* don't try to second guess the compilers heuristics for stdout handling */
1721
if (output_obj && strcmp(output_obj, "-") == 0) {
1596
if (output_obj && str_eq(output_obj, "-")) {
1722
1597
stats_update(STATS_OUTSTDOUT);
1723
1598
cc_log("Output file is -");
1727
1603
if (!output_obj) {
1729
output_obj = x_strdup(input_file);
1730
if ((p = strrchr(output_obj, '/'))) {
1733
p = strrchr(output_obj, '.');
1735
cc_log("Badly formed object filename");
1736
stats_update(STATS_ARGS);
1739
p[1] = found_S_opt ? 's' : 'o';
1743
/* If dependencies are generated, configure the preprocessor */
1745
if (generating_dependencies) {
1746
if (!dependency_filename_specified) {
1747
char *default_depfile_name;
1750
base_name = remove_extension(output_obj);
1751
x_asprintf(&default_depfile_name, "%s.d", base_name);
1753
args_add(stripped_args, "-MF");
1754
args_add(stripped_args, default_depfile_name);
1755
output_dep = make_relative_path(
1756
x_strdup(default_depfile_name));
1759
if (!dependency_target_specified) {
1760
args_add(stripped_args, "-MT");
1761
args_add(stripped_args, output_obj);
1604
if (output_is_precompiled_header) {
1605
output_obj = format("%s.gch", input_file);
1608
output_obj = x_strdup(input_file);
1609
if ((p = strrchr(output_obj, '/'))) {
1612
p = strrchr(output_obj, '.');
1614
cc_log("Badly formed object filename");
1615
stats_update(STATS_ARGS);
1619
p[1] = found_S_opt ? 's' : 'o';
1765
1624
/* cope with -o /dev/null */
1766
if (strcmp(output_obj,"/dev/null") != 0
1625
if (!str_eq(output_obj,"/dev/null")
1767
1626
&& stat(output_obj, &st) == 0
1768
1627
&& !S_ISREG(st.st_mode)) {
1769
1628
cc_log("Not a regular file: %s", output_obj);
1770
1629
stats_update(STATS_DEVICE);
1782
1642
if (input_charset) {
1783
1643
args_add(*preprocessor_args, input_charset);
1646
args_add(*preprocessor_args, "-fpch-preprocess");
1785
1648
if (explicit_language) {
1786
1649
args_add(*preprocessor_args, "-x");
1787
1650
args_add(*preprocessor_args, explicit_language);
1654
* Add flags for dependency generation only to the preprocessor command line.
1656
if (generating_dependencies) {
1657
if (!dependency_filename_specified) {
1658
char *default_depfile_name;
1661
base_name = remove_extension(output_obj);
1662
default_depfile_name = format("%s.d", base_name);
1664
args_add(dep_args, "-MF");
1665
args_add(dep_args, default_depfile_name);
1666
output_dep = make_relative_path(x_strdup(default_depfile_name));
1669
if (!dependency_target_specified) {
1670
args_add(dep_args, "-MT");
1671
args_add(dep_args, output_obj);
1789
1675
if (compile_preprocessed_source_code) {
1790
1676
*compiler_args = args_copy(stripped_args);
1791
1677
if (explicit_language) {
1801
1687
*compiler_args = args_copy(*preprocessor_args);
1691
* Only pass dependency arguments to the preprocesor since Intel's C++
1692
* compiler doesn't produce a correct .d file when compiling preprocessed
1695
args_extend(*preprocessor_args, dep_args);
1803
1698
args_free(stripped_args);
1806
static unsigned parse_sloppiness(char *p)
1699
args_free(dep_args);
1703
/* Reset the global state. Used by the test suite. */
1707
free(current_working_dir); current_working_dir = NULL;
1708
free(cache_dir); cache_dir = NULL;
1709
cache_logfile = NULL;
1711
args_free(orig_args); orig_args = NULL;
1712
free(input_file); input_file = NULL;
1714
free(output_dep); output_dep = NULL;
1715
free(cached_obj_hash); cached_obj_hash = NULL;
1716
free(cached_obj); cached_obj = NULL;
1717
free(cached_stderr); cached_stderr = NULL;
1718
free(cached_dep); cached_dep = NULL;
1719
free(manifest_path); manifest_path = NULL;
1720
time_of_compilation = 0;
1722
if (included_files) {
1723
hashtable_destroy(included_files, 1); included_files = NULL;
1725
generating_dependencies = false;
1728
direct_i_file = false;
1729
free(cpp_stderr); cpp_stderr = NULL;
1730
free(stats_file); stats_file = NULL;
1731
enable_unify = false;
1732
enable_direct = true;
1733
enable_compression = false;
1735
compile_preprocessed_source_code = false;
1736
output_is_precompiled_header = false;
1740
parse_sloppiness(char *p)
1808
1742
unsigned result = 0;
1743
char *word, *q, *saveptr = NULL;
1814
1748
p = x_strdup(p);
1816
while ((word = strtok(q, ", "))) {
1817
if (strcmp(word, "file_macro") == 0) {
1750
while ((word = strtok_r(q, ", ", &saveptr))) {
1751
if (str_eq(word, "file_macro")) {
1818
1752
cc_log("Being sloppy about __FILE__");
1819
1753
result |= SLOPPY_FILE_MACRO;
1821
if (strcmp(word, "include_file_mtime") == 0) {
1755
if (str_eq(word, "include_file_mtime")) {
1822
1756
cc_log("Being sloppy about include file mtime");
1823
1757
result |= SLOPPY_INCLUDE_FILE_MTIME;
1825
if (strcmp(word, "time_macros") == 0) {
1759
if (str_eq(word, "time_macros")) {
1826
1760
cc_log("Being sloppy about __DATE__ and __TIME__");
1827
1761
result |= SLOPPY_TIME_MACROS;
2188
2125
/* make sure the cache dir exists */
2189
2126
if (create_dir(cache_dir) != 0) {
2190
fprintf(stderr,"ccache: failed to create %s (%s)\n",
2191
cache_dir, strerror(errno));
2128
"ccache: failed to create %s (%s)\n",
2129
cache_dir, strerror(errno));
2195
2133
/* make sure the temp dir exists */
2196
2134
if (create_dir(temp_dir) != 0) {
2197
fprintf(stderr,"ccache: failed to create %s (%s)\n",
2198
temp_dir, strerror(errno));
2136
"ccache: failed to create %s (%s)\n",
2137
temp_dir, strerror(errno));
2202
2141
if (!getenv("CCACHE_READONLY")) {
2203
2142
if (create_cachedirtag(cache_dir) != 0) {
2204
fprintf(stderr,"ccache: failed to create %s/CACHEDIR.TAG (%s)\n",
2205
cache_dir, strerror(errno));
2144
"ccache: failed to create %s/CACHEDIR.TAG (%s)\n",
2145
cache_dir, strerror(errno));