/* run the real compiler and put the result in cache */ static void to_cache(ARGS *args) { char *path_stderr; char *tmp_stdout, *tmp_stderr, *tmp_hashname; struct stat st1, st2; int status; x_asprintf(&tmp_stdout, "%s/tmp.stdout.%s", temp_dir, tmp_string()); x_asprintf(&tmp_stderr, "%s/tmp.stderr.%s", temp_dir, tmp_string()); x_asprintf(&tmp_hashname, "%s/tmp.hash.%s.o", temp_dir, tmp_string()); args_add(args, "-o"); args_add(args, tmp_hashname); /* Turn off DEPENDENCIES_OUTPUT when running cc1, because * otherwise it will emit a line like * * tmp.stdout.vexed.732.o: /home/mbp/.ccache/tmp.stdout.vexed.732.i * * unsetenv() is on BSD and Linux but not portable. */ putenv("DEPENDENCIES_OUTPUT"); if (getenv("CCACHE_CPP2")) { args_add(args, input_file); } else { args_add(args, i_tmpfile); } status = execute(args->argv, tmp_stdout, tmp_stderr); args_pop(args, 3); if (stat(tmp_stdout, &st1) != 0 || st1.st_size != 0) { cc_log("compiler produced stdout for %s\n", output_file); stats_update(STATS_STDOUT); unlink(tmp_stdout); unlink(tmp_stderr); unlink(tmp_hashname); failed(); } unlink(tmp_stdout); if (status != 0) { int fd; cc_log("compile of %s gave status = %d\n", output_file, status); stats_update(STATS_STATUS); fd = open(tmp_stderr, O_RDONLY | O_BINARY); if (fd != -1) { if (strcmp(output_file, "/dev/null") == 0 || rename(tmp_hashname, output_file) == 0 || errno == ENOENT) { if (cpp_stderr) { /* we might have some stderr from cpp */ int fd2 = open(cpp_stderr, O_RDONLY | O_BINARY); if (fd2 != -1) { copy_fd(fd2, 2); close(fd2); unlink(cpp_stderr); cpp_stderr = NULL; } } /* we can use a quick method of getting the failed output */ copy_fd(fd, 2); close(fd); unlink(tmp_stderr); if (i_tmpfile && !direct_i_file) { unlink(i_tmpfile); } exit(status); } } unlink(tmp_stderr); unlink(tmp_hashname); failed(); } x_asprintf(&path_stderr, "%s.stderr", hashname); if (stat(tmp_stderr, &st1) != 0 || stat(tmp_hashname, &st2) != 0 || rename(tmp_hashname, hashname) != 0 || rename(tmp_stderr, path_stderr) != 0) { cc_log("failed to rename tmp files - %s\n", strerror(errno)); stats_update(STATS_ERROR); failed(); } cc_log("Placed %s into cache\n", output_file); stats_tocache(file_size(&st1) + file_size(&st2)); free(tmp_hashname); free(tmp_stderr); free(tmp_stdout); free(path_stderr); }
/* run the real compiler and put the result in cache */ static void to_cache(ARGS *args) { char *path_stderr; char *tmp_stdout, *tmp_stderr, *tmp_outfiles; struct stat st1; int status; int cached_files_count = 0; int files_size = 0; x_asprintf(&tmp_stdout, "%s/tmp.stdout.%s", temp_dir, tmp_string()); x_asprintf(&tmp_stderr, "%s/tmp.stderr.%s", temp_dir, tmp_string()); x_asprintf(&tmp_outfiles, "%s/tmp.outfiles.%s", temp_dir, tmp_string()); if (strip_c_option && !swig) { args_add(stripped_args, "-c"); } if (output_file) { args_add(args, "-o"); args_add(args, output_file); } /* Turn off DEPENDENCIES_OUTPUT when running cc1, because * otherwise it will emit a line like * * tmp.stdout.vexed.732.o: /home/mbp/.ccache/tmp.stdout.vexed.732.i * * unsetenv() is on BSD and Linux but not portable. */ putenv("DEPENDENCIES_OUTPUT"); /* Give SWIG a filename for it to create and populate with a list of files that it generates */ if (swig) { char *ccache_outfiles; x_asprintf(&ccache_outfiles, "CCACHE_OUTFILES=%s", tmp_outfiles); unlink(tmp_outfiles); if (getenv("CCACHE_OUTFILES") || putenv(ccache_outfiles) == -1) { cc_log("CCACHE_OUTFILES env variable already set or could not be set\n"); stats_update(STATS_ERROR); failed(); } } if (getenv("CCACHE_CPP2")) { args_add(args, input_file); } else { if (swig) { args_add(args, "-nopreprocess"); } args_add(args, i_tmpfile); } status = execute(args->argv, tmp_stdout, tmp_stderr); args_pop(args, 3); if (stat(tmp_stdout, &st1) != 0 || st1.st_size != 0) { cc_log("compiler produced stdout for %s\n", input_file); stats_update(STATS_STDOUT); unlink(tmp_stdout); unlink(tmp_stderr); unlink(tmp_outfiles); if (!swig) unlink(output_file); failed(); } unlink(tmp_stdout); if (status != 0) { int fd; cc_log("compile of %s gave status = %d\n", input_file, status); stats_update(STATS_STATUS); fd = open(tmp_stderr, O_RDONLY | O_BINARY); if (fd != -1) { if (cpp_stderr) { /* we might have some stderr from cpp */ int fd2 = open(cpp_stderr, O_RDONLY | O_BINARY); if (fd2 != -1) { copy_fd(fd2, 2); close(fd2); unlink(cpp_stderr); cpp_stderr = NULL; } } /* we can use a quick method of getting the failed output */ copy_fd(fd, 2); close(fd); unlink(tmp_stderr); if (i_tmpfile && !direct_i_file) { unlink(i_tmpfile); } exit(status); } unlink(tmp_stderr); unlink(tmp_outfiles); if (!swig) unlink(output_file); failed(); } else { int hardlink = (getenv("CCACHE_NOCOMPRESS") != 0) && (getenv("CCACHE_HARDLINK") != 0); if (swig) { /* read the list of generated files and copy each of them into the cache */ FILE *file; file = fopen(tmp_outfiles, "r"); if (file) { char out_filename[FILENAME_MAX + 1]; char out_filename_cache[FILENAME_MAX + 1]; while (fgets(out_filename, FILENAME_MAX, file)) { char *linefeed = strchr(out_filename, '\n'); if (linefeed) { char *potential_cr = linefeed - 1; if (potential_cr >= out_filename && *potential_cr == '\r') *potential_cr = 0; *linefeed = 0; if (cached_files_count == 0) { strcpy(out_filename_cache, hashname); } else { sprintf(out_filename_cache, "%s.%d", hashname, cached_files_count); } if (commit_to_cache(out_filename, out_filename_cache, hardlink) != 0) { fclose(file); unlink(tmp_outfiles); failed(); } to_cache_stats_helper(&st1, out_filename_cache, tmp_outfiles, &files_size, &cached_files_count); } else { cached_files_count = 0; break; } } fclose(file); if (cached_files_count == 0) { cc_log("failed to copy output files to cache - internal error\n"); stats_update(STATS_ERROR); unlink(tmp_outfiles); failed(); } /* also copy the (uncompressed) file containing the list of generated files into the cache */ sprintf(out_filename_cache, "%s.outfiles", hashname); if (stat(tmp_outfiles, &st1) != 0 || safe_rename(tmp_outfiles, out_filename_cache) != 0) { cc_log("failed to copy outfiles file to cache - %s\n", strerror(errno)); stats_update(STATS_ERROR); unlink(tmp_outfiles); failed(); } to_cache_stats_helper(&st1, out_filename_cache, tmp_outfiles, &files_size, &cached_files_count); unlink(tmp_outfiles); } else { cc_log("failed to open temp outfiles file - %s\n", strerror(errno)); stats_update(STATS_ERROR); failed(); } } else { if (commit_to_cache(output_file, hashname, hardlink) != 0) { failed(); } to_cache_stats_helper(&st1, hashname, 0, &files_size, &cached_files_count); } } x_asprintf(&path_stderr, "%s.stderr", hashname); if (stat(tmp_stderr, &st1) != 0 || move_file(tmp_stderr, path_stderr) != 0) { cc_log("failed to rename tmp files - %s\n", strerror(errno)); stats_update(STATS_ERROR); failed(); } to_cache_stats_helper(&st1, path_stderr, 0, &files_size, &cached_files_count); cc_log("Placed %d files for %s into cache\n", cached_files_count, input_file); stats_tocache(files_size, cached_files_count); free(tmp_stderr); free(tmp_stdout); free(tmp_outfiles); free(path_stderr); }