From da65f1e21a24f0a173ee5ae08f00db91cba5ffda Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 31 Aug 2025 00:28:00 +0900 Subject: feat: support -MMD flag --- src/main.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 5704367..b064f39 100644 --- a/src/main.c +++ b/src/main.c @@ -2,6 +2,7 @@ #include "cli.h" #include "codegen.h" #include "common.h" +#include "fs.h" #include "io.h" #include "parse.h" #include "preprocess.h" @@ -16,7 +17,11 @@ int main(int argc, char** argv) { } InFile* source = infile_open(cli_args->input_filename); - TokenArray* pp_tokens = preprocess(source); + + StrArray included_files; + strings_init(&included_files); + + TokenArray* pp_tokens = preprocess(source, &included_files); TokenArray* tokens = tokenize(pp_tokens); Program* prog = parse(tokens); @@ -45,4 +50,18 @@ int main(int argc, char** argv) { fatal_error("gcc failed: %d", result); } } + + if (cli_args->generate_deps && cli_args->only_compile && cli_args->output_filename) { + const char* dep_filename = replace_extension(cli_args->output_filename, ".d"); + + FILE* dep_file = fopen(dep_filename, "w"); + if (!dep_file) { + fatal_error("Cannot open dependency file: %s", dep_filename); + } + fprintf(dep_file, "%s:", cli_args->output_filename); + for (size_t i = 0; i < included_files.len; ++i) { + fprintf(dep_file, " \\\n %s", included_files.data[i]); + } + fprintf(dep_file, "\n"); + } } -- cgit v1.2.3-70-g09d2