From 12f2f654dd9e524048b37654760b0f5d9edc3d13 Mon Sep 17 00:00:00 2001 From: ISSOtm Date: Tue, 10 Sep 2019 15:09:24 +0200 Subject: [PATCH] Add -MG This option allows for automatic dependency detection and generation: as soon as a missing file is found, it is output to the dep file, and assembly immediately aborts. (No .o file is produced, even if `-o` was speicified.) This doesn't cause an error, either; the point is that once the file is added to the dep file, the Makefile is re-parsed, and this time the file will be generated, so the dep list builds up automatically. This mimicks GCC's option and behavior. --- include/asm/main.h | 3 ++- src/asm/asmy.y | 6 ++++++ src/asm/fstack.c | 33 +++++++++++++++++++-------------- src/asm/main.c | 17 ++++++++++++++--- src/asm/output.c | 14 ++++++++++++-- 5 files changed, 53 insertions(+), 20 deletions(-) diff --git a/include/asm/main.h b/include/asm/main.h index 4484a819..bc057906 100644 --- a/include/asm/main.h +++ b/include/asm/main.h @@ -36,7 +36,8 @@ extern struct sOptions CurrentOptions; extern FILE *dependfile; extern char *tzTargetFileName; - +extern bool oGeneratedMissingIncludes; +extern bool oFailedOnMissingInclude; extern bool oGeneratePhonyDeps; void opt_Push(void); diff --git a/src/asm/asmy.y b/src/asm/asmy.y index fe36cdf3..ff7373ff 100644 --- a/src/asm/asmy.y +++ b/src/asm/asmy.y @@ -1004,16 +1004,22 @@ set : T_LABEL T_POP_SET const include : T_POP_INCLUDE string { fstk_RunInclude($2); + if (oFailedOnMissingInclude) + YYACCEPT; } ; incbin : T_POP_INCBIN string { out_BinaryFile($2); + if (oFailedOnMissingInclude) + YYACCEPT; } | T_POP_INCBIN string comma uconst comma uconst { out_BinaryFileSlice($2, $4, $6); + if (oFailedOnMissingInclude) + YYACCEPT; } ; diff --git a/src/asm/fstack.c b/src/asm/fstack.c index 0ded999e..11675bd4 100644 --- a/src/asm/fstack.c +++ b/src/asm/fstack.c @@ -325,6 +325,15 @@ void fstk_AddIncludePath(char *s) fatalerror("Include path too long '%s'", s); } +static void printdep(const char *fileName) +{ + if (dependfile) { + fprintf(dependfile, "%s: %s\n", tzTargetFileName, fileName); + if (oGeneratePhonyDeps) + fprintf(dependfile, "%s:\n", fileName); + } +} + FILE *fstk_FindFile(char *fname, char **incPathUsed) { char path[_MAX_PATH]; @@ -337,13 +346,7 @@ FILE *fstk_FindFile(char *fname, char **incPathUsed) f = fopen(fname, "rb"); if (f != NULL || errno != ENOENT) { - if (dependfile) { - fprintf(dependfile, "%s: %s\n", tzTargetFileName, - fname); - if (oGeneratePhonyDeps) - fprintf(dependfile, "%s:\n", fname); - } - + printdep(fname); return f; } @@ -366,12 +369,7 @@ FILE *fstk_FindFile(char *fname, char **incPathUsed) f = fopen(path, "rb"); if (f != NULL || errno != ENOENT) { - if (dependfile) { - fprintf(dependfile, "%s: %s\n", - tzTargetFileName, fname); - if (oGeneratePhonyDeps) - fprintf(dependfile, "%s:\n", fname); - } + printdep(fname); if (incPathUsed) *incPathUsed = IncludePaths[i]; @@ -380,6 +378,8 @@ FILE *fstk_FindFile(char *fname, char **incPathUsed) } errno = ENOENT; + if (oGeneratedMissingIncludes) + printdep(fname); return NULL; } @@ -391,8 +391,13 @@ void fstk_RunInclude(char *tzFileName) char *incPathUsed = ""; FILE *f = fstk_FindFile(tzFileName, &incPathUsed); - if (f == NULL) + if (f == NULL) { + if (oGeneratedMissingIncludes) { + oFailedOnMissingInclude = true; + return; + } err(1, "Unable to open included file '%s'", tzFileName); + } pushcontext(); nLineNo = 1; diff --git a/src/asm/main.c b/src/asm/main.c index 41a54a28..35db6dd4 100644 --- a/src/asm/main.c +++ b/src/asm/main.c @@ -47,7 +47,8 @@ uint32_t unionStart[128], unionSize[128]; /* extern int yydebug; */ FILE *dependfile; - +bool oGeneratedMissingIncludes; +bool oFailedOnMissingInclude; bool oGeneratePhonyDeps; char *tzTargetFileName; @@ -294,7 +295,7 @@ static void print_usage(void) { fputs( "Usage: rgbasm [-EhLVvw] [-b chars] [-D name[=value]] [-g chars] [-i path]\n" -" [-M depend_file] [-MP] [-MT target_file] [-MQ target_file]\n" +" [-M depend_file] [-MG] [-MP] [-MT target_file] [-MQ target_file]\n" " [-o out_file] [-p pad_value] [-r depth] [-W warning] ...\n" "Useful options:\n" " -E, --export-all export all labels\n" @@ -331,6 +332,8 @@ int main(int argc, char *argv[]) nMaxRecursionDepth = 64; oGeneratePhonyDeps = false; + oGeneratedMissingIncludes = true; + oFailedOnMissingInclude = false; tzTargetFileName = NULL; size_t nTargetFileNameLen = 0; @@ -388,7 +391,7 @@ int main(int argc, char *argv[]) newopt.optimizeloads = false; break; case 'M': - ep = strchr("PQT", optarg[0]); + ep = strchr("GPQT", optarg[0]); if (!ep || !*ep || optarg[1]) { dependfile = fopen(optarg, "w"); if (dependfile == NULL) @@ -396,6 +399,9 @@ int main(int argc, char *argv[]) optarg); } else { switch (optarg[0]) { + case 'G': + oGeneratedMissingIncludes = true; + break; case 'P': oGeneratePhonyDeps = true; break; @@ -511,6 +517,8 @@ int main(int argc, char *argv[]) if (yyparse() != 0 || nbErrors != 0) errx(1, "Assembly aborted (%ld errors)!", nbErrors); + if (dependfile) + fclose(dependfile); if (nIFDepth != 0) errx(1, "Unterminated IF construct (%ld levels)!", nIFDepth); @@ -535,6 +543,9 @@ int main(int argc, char *argv[]) (int)(60 / timespent * nTotalLines)); } + if (oFailedOnMissingInclude) + return 0; + /* If no path specified, don't write file */ if (tzObjectname != NULL) out_WriteObject(); diff --git a/src/asm/output.c b/src/asm/output.c index 1a019682..8db62bda 100644 --- a/src/asm/output.c +++ b/src/asm/output.c @@ -880,8 +880,13 @@ void out_BinaryFile(char *s) FILE *f; f = fstk_FindFile(s, NULL); - if (f == NULL) + if (f == NULL) { + if (oGeneratedMissingIncludes) { + oFailedOnMissingInclude = true; + return; + } err(1, "Unable to open incbin file '%s'", s); + } int32_t fsize; @@ -915,8 +920,13 @@ void out_BinaryFileSlice(char *s, int32_t start_pos, int32_t length) fatalerror("Number of bytes to read must be greater than zero"); f = fstk_FindFile(s, NULL); - if (f == NULL) + if (f == NULL) { + if (oGeneratedMissingIncludes) { + oFailedOnMissingInclude = true; + return; + } err(1, "Unable to open included file '%s'", s); + } int32_t fsize;