diff options
author | Slendi <slendi@socopon.com> | 2024-01-13 19:44:35 +0200 |
---|---|---|
committer | Slendi <slendi@socopon.com> | 2024-01-13 19:44:35 +0200 |
commit | 37290711cce69cfaa36299e8a73345b018413855 (patch) | |
tree | cff537042a5295d03fc910b9f6c4b7706108e22d | |
parent | ccbbca12f594562a59e5584f8c632a3fa0726090 (diff) |
Do not rebuild files unless cleaned first.
Signed-off-by: Slendi <slendi@socopon.com>
-rw-r--r-- | cbuild_impl.c | 49 |
1 files changed, 42 insertions, 7 deletions
diff --git a/cbuild_impl.c b/cbuild_impl.c index 71bed29..67e92f4 100644 --- a/cbuild_impl.c +++ b/cbuild_impl.c @@ -8,6 +8,7 @@ #include <sys/stat.h> #include <sys/types.h> #include <sys/wait.h> +#include <time.h> #ifdef __TINYC__ #define __STDC_NO_VLA__ 1 #endif @@ -19,10 +20,12 @@ #include <unistd.h> #endif -bool file_exists(char *filename) { +bool file_exists(char *filename, time_t *mtime) { struct stat path_stat; if (stat(filename, &path_stat) == -1) return false; + if (mtime) + *mtime = path_stat.st_mtime; return S_ISREG(path_stat.st_mode); } @@ -51,7 +54,7 @@ executable_t create_executable(char *name, char *sources) { for (size_t off = 0; exe.header.sources[off]; off += strlen(exe.header.sources) + 1) { char *path = exe.header.sources + off; - if (!file_exists(path)) { + if (!file_exists(path, NULL)) { fprintf(stderr, "File %s does not exist\n", path); exit(1); } @@ -103,6 +106,7 @@ void build(project_t build) { if (build.language & LANG_CXX) fprintf(stderr, " - C++\n"); size_t sources_count = 0; + size_t sources_count_real = 0; size_t count_operations = 0; for (size_t i = 0; i < build.target_count; i++) { target_header_t *target = build.targets[i]; @@ -111,13 +115,30 @@ void build(project_t build) { count_operations++; // Linking for (size_t off = 0; exe->header.sources[off]; off += strlen(exe->header.sources) + 1) { - sources_count++; - count_operations++; + sources_count_real++; + time_t mtime_source; + if (!file_exists(exe->header.sources + off, &mtime_source)) { + fprintf(stderr, "File %s does not exist\n", + exe->header.sources + off); + exit(1); + } + char *dot_o = change_ext_to_dot_o(exe->header.sources + off); + time_t mtime_dot_o = 0; + if (!file_exists(dot_o, &mtime_dot_o)) { + } + free(dot_o); + if (mtime_source > mtime_dot_o) { + sources_count++; + count_operations++; + } } } } if (sources_count == 0) { - fprintf(stderr, "No sources specified\n"); + if (sources_count_real == 0) + fprintf(stderr, "No sources specified\n"); + else + fprintf(stderr, "Nothing to do\n"); exit(1); } @@ -145,8 +166,22 @@ void build(project_t build) { off += strlen(exe->header.sources) + 1) { char *source = exe->header.sources + off; char *dot_o = change_ext_to_dot_o(source); - fprintf(stderr, "\33[2K\r(%ld/%ld) Building \33[1m%s\33[0m", count, + fprintf(stderr, "\33[2K\r(%ld/%ld) Building \33[1m%s\33[0m", count++, count_operations, source); + + time_t mtime_source; + if (!file_exists(source, &mtime_source)) { + fprintf(stderr, "File %s does not exist\n", source); + exit(1); + } + time_t mtime_dot_o = 0; + if (!file_exists(dot_o, &mtime_dot_o)) { + } + if (mtime_source <= mtime_dot_o) { + free(dot_o); + continue; + } + if (processes_alive == processing_units) { int status; waitpid(-1, &status, 0); @@ -208,7 +243,7 @@ void build(project_t build) { for (int j = 0; j < processing_units; j++) waitpid(-1, NULL, 0); - fprintf(stderr, "\33[2K\r(%ld/%ld) Linking \33[1m%s\33[0m", count, + fprintf(stderr, "\33[2K\r(%ld/%ld) Linking \33[1m%s\33[0m", count++, count_operations, exe->header.name); char *argv[4 + sources_count]; argv[0] = "gcc"; |