shithub: rgbds

Download patch

ref: 12f2f654dd9e524048b37654760b0f5d9edc3d13
parent: 0649b360fb4b6b195641b74031c70047d981b11e
author: ISSOtm <eldredhabert0@gmail.com>
date: Tue Sep 10 11:09:24 EDT 2019

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.

--- a/include/asm/main.h
+++ b/include/asm/main.h
@@ -36,7 +36,8 @@
 
 extern FILE *dependfile;
 extern char *tzTargetFileName;
-
+extern bool oGeneratedMissingIncludes;
+extern bool oFailedOnMissingInclude;
 extern bool oGeneratePhonyDeps;
 
 void opt_Push(void);
--- a/src/asm/asmy.y
+++ b/src/asm/asmy.y
@@ -1004,6 +1004,8 @@
 include		: T_POP_INCLUDE string
 		{
 			fstk_RunInclude($2);
+			if (oFailedOnMissingInclude)
+				YYACCEPT;
 		}
 ;
 
@@ -1010,10 +1012,14 @@
 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;
 		}
 ;
 
--- a/src/asm/fstack.c
+++ b/src/asm/fstack.c
@@ -325,6 +325,15 @@
 		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 @@
 	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 @@
 		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 @@
 	}
 
 	errno = ENOENT;
+	if (oGeneratedMissingIncludes)
+		printdep(fname);
 	return NULL;
 }
 
@@ -391,8 +391,13 @@
 	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;
--- a/src/asm/main.c
+++ b/src/asm/main.c
@@ -47,7 +47,8 @@
 /* extern int yydebug; */
 
 FILE *dependfile;
-
+bool oGeneratedMissingIncludes;
+bool oFailedOnMissingInclude;
 bool oGeneratePhonyDeps;
 char *tzTargetFileName;
 
@@ -294,7 +295,7 @@
 {
 	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] <file> ...\n"
 "Useful options:\n"
 "    -E, --export-all         export all labels\n"
@@ -331,6 +332,8 @@
 
 	nMaxRecursionDepth = 64;
 	oGeneratePhonyDeps = false;
+	oGeneratedMissingIncludes = true;
+	oFailedOnMissingInclude = false;
 	tzTargetFileName = NULL;
 	size_t nTargetFileNameLen = 0;
 
@@ -388,7 +391,7 @@
 			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 @@
 					    optarg);
 			} else {
 				switch (optarg[0]) {
+				case 'G':
+					oGeneratedMissingIncludes = true;
+					break;
 				case 'P':
 					oGeneratePhonyDeps = true;
 					break;
@@ -511,6 +517,8 @@
 
 	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);
@@ -534,6 +542,9 @@
 			printf("(%d lines/minute)\n",
 			       (int)(60 / timespent * nTotalLines));
 	}
+
+	if (oFailedOnMissingInclude)
+		return 0;
 
 	/* If no path specified, don't write file */
 	if (tzObjectname != NULL)
--- a/src/asm/output.c
+++ b/src/asm/output.c
@@ -880,8 +880,13 @@
 	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 @@
 		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;