The main makefile can call mkbuiltins from multiple different codepaths in parallel.
When called, it moves the existing files out the way and creates new ones, then
compares which will break the build if timing is unlucky.

The root of the problem is mkbuiltins.c creating a file but also referencing that
file under the same name. By modifing it to allow the final name and the temp name
to be specified, we can avoid the original reason for the moving of files around.
This allows them to be created under a new name and then replaced if changed,
removing any race windows around accessing the files whilst they've been
moved or are being rewritten.

See [YOCTO #14227]

Upstream-Status: Pending
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>

Index: bash-5.1.8/builtins/Makefile.in
===================================================================
--- bash-5.1.8.orig/builtins/Makefile.in
+++ bash-5.1.8/builtins/Makefile.in
@@ -185,19 +185,17 @@ gen-helpfiles:	tmpbuiltins.o gen-helpfil
 	$(CC_FOR_BUILD) ${CCFLAGS_FOR_BUILD} $(LDFLAGS_FOR_BUILD) -o $@ gen-helpfiles.o tmpbuiltins.o $(LIBS_FOR_BUILD)
 
 builtext.h builtins.c: $(MKBUILTINS) $(DEFSRC)
-	@-if test -f builtins.c; then mv -f builtins.c old-builtins.c; fi
-	@-if test -f builtext.h; then mv -f builtext.h old-builtext.h; fi
-	./$(MKBUILTINS) -externfile builtext.h -structfile builtins.c \
+	./$(MKBUILTINS) -externfile builtext-new.h -externfinalfile builtext.h -structfile builtins-new.c \
 	    -noproduction $(DIRECTDEFINE) $(HELPDIRDEFINE) $(HELPSTRINGS) $(DEFSRC)
-	@-if cmp -s old-builtext.h builtext.h 2>/dev/null; then \
-		mv old-builtext.h builtext.h; \
+	@-if ! cmp -s builtext.h builtext-new.h 2>/dev/null; then \
+		mv builtext-new.h builtext.h; \
 	 else \
-		$(RM) old-builtext.h; \
+		$(RM) builtext-new.h; \
 	 fi
-	@-if cmp -s old-builtins.c builtins.c 2>/dev/null; then \
-		mv old-builtins.c builtins.c; \
+	@-if ! cmp -s builtins.c builtins-new.c 2>/dev/null; then \
+		mv builtins-new.c builtins.c; \
 	 else \
-		$(RM) old-builtins.c; \
+		$(RM) builtins-new.c; \
 	 fi
 
 helpdoc:	gen-helpfiles
Index: bash-5.1.8/builtins/mkbuiltins.c
===================================================================
--- bash-5.1.8.orig/builtins/mkbuiltins.c
+++ bash-5.1.8/builtins/mkbuiltins.c
@@ -113,6 +113,9 @@ char *struct_filename = (char *)NULL;
 /* The name of the external declaration file. */
 char *extern_filename = (char *)NULL;
 
+/* The final name of the external declaration file. */
+char *extern_final_filename = (char *)NULL;
+
 /* Here is a structure for manipulating arrays of data. */
 typedef struct {
   int size;		/* Number of slots allocated to array. */
@@ -230,6 +233,8 @@ main (argc, argv)
 
       if (strcmp (arg, "-externfile") == 0)
 	extern_filename = argv[arg_index++];
+      else if (strcmp (arg, "-externfinalfile") == 0)
+	extern_final_filename = argv[arg_index++];
       else if (strcmp (arg, "-structfile") == 0)
 	struct_filename = argv[arg_index++];
       else if (strcmp (arg, "-noproduction") == 0)
@@ -273,6 +278,9 @@ main (argc, argv)
 	}
     }
 
+  if (!extern_final_filename)
+    extern_final_filename = extern_filename;
+
   /* If there are no files to process, just quit now. */
   if (arg_index == argc)
     exit (0);
@@ -1174,7 +1182,7 @@ write_file_headers (structfile, externfi
 	fprintf (structfile, "%s\n", structfile_header[i]);
 
       fprintf (structfile, "#include \"%s\"\n",
-	       extern_filename ? extern_filename : "builtext.h");
+	       extern_final_filename ? extern_final_filename : "builtext.h");
 
       fprintf (structfile, "#include \"bashintl.h\"\n");
 
@@ -1184,7 +1192,7 @@ write_file_headers (structfile, externfi
   if (externfile)
     fprintf (externfile,
 	     "/* %s - The list of builtins found in libbuiltins.a. */\n",
-	     extern_filename ? extern_filename : "builtext.h");
+	     extern_final_filename ? extern_final_filename : "builtext.h");
 }
 
 /* Write out any necessary closing information for
