tcctools.c: reduce duplicated code for -MP option.

Generate list of escaped dependencies as a separate
first step.
This commit is contained in:
Reimar Döffinger 2023-10-30 18:33:35 +01:00
parent 70328621f1
commit c45cb650fc

View File

@ -597,8 +597,9 @@ static char *escape_target_dep(const char *s) {
ST_FUNC int gen_makedeps(TCCState *s1, const char *target, const char *filename) ST_FUNC int gen_makedeps(TCCState *s1, const char *target, const char *filename)
{ {
FILE *depout; FILE *depout;
char buf[1024], *escaped_target; char buf[1024];
int i, k; char **escaped_targets;
int i, k, num_targets;
if (!filename) { if (!filename) {
/* compute filename automatically: dir/file.o -> dir/file.d */ /* compute filename automatically: dir/file.o -> dir/file.d */
@ -617,33 +618,30 @@ ST_FUNC int gen_makedeps(TCCState *s1, const char *target, const char *filename)
if (s1->verbose) if (s1->verbose)
printf("<- %s\n", filename); printf("<- %s\n", filename);
fprintf(depout, "%s:", target); escaped_targets = tcc_malloc(s1->nb_target_deps * sizeof(*escaped_targets));
num_targets = 0;
for (i = 0; i<s1->nb_target_deps; ++i) { for (i = 0; i<s1->nb_target_deps; ++i) {
for (k = 0; k < i; ++k) for (k = 0; k < i; ++k)
if (0 == strcmp(s1->target_deps[i], s1->target_deps[k])) if (0 == strcmp(s1->target_deps[i], s1->target_deps[k]))
goto next; goto next;
escaped_target = escape_target_dep(s1->target_deps[i]); escaped_targets[num_targets++] = escape_target_dep(s1->target_deps[i]);
fprintf(depout, " \\\n %s", escaped_target);
tcc_free(escaped_target);
next:; next:;
} }
fprintf(depout, "%s:", target);
for (i = 0; i < num_targets; ++i)
fprintf(depout, " \\\n %s", escaped_targets[i]);
fprintf(depout, "\n"); fprintf(depout, "\n");
if (s1->gen_phony_deps) { if (s1->gen_phony_deps) {
/* Skip first file, which is the c file. /* Skip first file, which is the c file.
* This will still print any additional c files specified * Only works for single file give on command-line,
* on command-line, but e.g. clang produces broken dependency * but other compilers have the same limitation */
* files in this case as well, printing only dependencies for last for (i = 1; i < num_targets; ++i)
* file in command line. So ignore this case. */ fprintf(depout, "%s:\n", escaped_targets[i]);
for (i = 1; i<s1->nb_target_deps; ++i) {
for (k = 0; k < i; ++k)
if (0 == strcmp(s1->target_deps[i], s1->target_deps[k]))
goto next2;
escaped_target = escape_target_dep(s1->target_deps[i]);
fprintf(depout, "%s:\n", escaped_target);
tcc_free(escaped_target);
next2:;
}
} }
for (i = 0; i < num_targets; ++i)
tcc_free(escaped_targets[i]);
tcc_free(escaped_targets);
fclose(depout); fclose(depout);
return 0; return 0;
} }