From e451134e233d1c9844f62e61a844ce42dc7c36a1 Mon Sep 17 00:00:00 2001 From: Gorilla Sapiens Date: Tue, 27 May 2025 05:03:17 +0000 Subject: [PATCH 1/9] fixes issue #2446 --- src/cl65/main.c | 14 +++++++++++++- src/common/fname.c | 21 +++++++++++++++++++++ src/common/fname.h | 6 ++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/cl65/main.c b/src/cl65/main.c index 31d805181..4a39263af 100644 --- a/src/cl65/main.c +++ b/src/cl65/main.c @@ -620,6 +620,9 @@ static void Assemble (const char* File) static void Compile (const char* File) /* Compile the given file */ { + /* A temporary file name passed to the assembler */ + char *TmpFile = NULL; + /* Remember the current compiler argument count */ unsigned ArgCount = CC65.ArgCount; @@ -657,6 +660,12 @@ static void Compile (const char* File) /* Add the file as argument for the compiler */ CmdAddArg (&CC65, File); + if (DoAssemble && DoLink) { + /* set a temporary output file name */ + TmpFile = MakeTmpFilename(File, ".s"); + CmdSetOutput (&CC65, TmpFile); + } + /* Add a NULL pointer to terminate the argument list */ CmdAddArg (&CC65, 0); @@ -671,7 +680,10 @@ static void Compile (const char* File) */ if (DoAssemble) { /* Assemble the intermediate file and remove it */ - AssembleIntermediate (File); + AssembleIntermediate (TmpFile ? TmpFile : File); + if (TmpFile) { + xfree(TmpFile); + } } } diff --git a/src/common/fname.c b/src/common/fname.c index 4e4f7c7fa..c5000bc72 100644 --- a/src/common/fname.c +++ b/src/common/fname.c @@ -33,6 +33,7 @@ +#include #include #include "xmalloc.h" @@ -115,3 +116,23 @@ char* MakeFilename (const char* Origin, const char* Ext) } return Out; } + + + +char* MakeTmpFilename (const char* Origin, const char* Ext) +/* Make a new temporary file name from Ext. tmpnam(3) is called +** and Ext is appended to generate the filename. Origin is ignored. +** The result is placed in a malloc'ed buffer and returned. +*/ +{ + char* Out; + char Buffer[L_tmpnam * 2]; /* a lazy way to ensure we have space for Ext */ + + tmpnam(Buffer); + strcat(Buffer, Ext); + + Out = xmalloc (strlen (Buffer) + 1); + strcpy (Out, Buffer); + + return Out; +} diff --git a/src/common/fname.h b/src/common/fname.h index 1b94d270c..1dc985f93 100644 --- a/src/common/fname.h +++ b/src/common/fname.h @@ -59,6 +59,12 @@ char* MakeFilename (const char* Origin, const char* Ext); ** The function may be used to create "foo.o" from "foo.s". */ +char* MakeTmpFilename (const char* Origin, const char* Ext); +/* Make a new temporary file name from Ext. tmpnam(3) is called +** and Ext is appended to generate the filename. Origin is ignored. +** The result is placed in a malloc'ed buffer and returned. +*/ + /* End of fname.h */ From 11d3338282f815cb579906edc40da179803d8c44 Mon Sep 17 00:00:00 2001 From: Gorilla Sapiens Date: Tue, 27 May 2025 06:00:24 +0000 Subject: [PATCH 2/9] bugfixes --- src/cl65/main.c | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/cl65/main.c b/src/cl65/main.c index 4a39263af..900602c87 100644 --- a/src/cl65/main.c +++ b/src/cl65/main.c @@ -526,12 +526,15 @@ static void Link (void) -static void AssembleFile (const char* File, unsigned ArgCount) +static void AssembleFile (const char* File, const char* TmpFile, unsigned ArgCount) /* Common routine to assemble a file. Will be called by Assemble() and ** AssembleIntermediate(). Adds options common for both routines and ** assembles the file. Will remove excess arguments after assembly. */ { + /* ObjName may be used for temporary or real filename */ + char *ObjName; + /* Set the target system */ CmdSetTarget (&CA65, Target); @@ -541,7 +544,8 @@ static void AssembleFile (const char* File, unsigned ArgCount) ** to the file list of the linker. The name of the output ** file is that of the input file with ".s" replaced by ".o". */ - char* ObjName = MakeFilename (File, ".o"); + ObjName = MakeFilename (TmpFile ? TmpFile : File, ".o"); + CmdSetOutput (&CA65, ObjName); CmdAddFile (&LD65, ObjName); /* This is just a temporary file, schedule it for removal */ CmdAddFile (&RM, ObjName); @@ -551,10 +555,15 @@ static void AssembleFile (const char* File, unsigned ArgCount) if (OutputName) { CmdSetOutput (&CA65, OutputName); } + else { + ObjName = MakeFilename (File, ".o"); + CmdSetOutput (&CA65, ObjName); + xfree (ObjName); + } } /* Add the file as argument for the assembler */ - CmdAddArg (&CA65, File); + CmdAddArg (&CA65, TmpFile ? TmpFile : File); /* Add a NULL pointer to terminate the argument list */ CmdAddArg (&CA65, 0); @@ -568,7 +577,7 @@ static void AssembleFile (const char* File, unsigned ArgCount) -static void AssembleIntermediate (const char* SourceFile) +static void AssembleIntermediate (const char* SourceFile, const char* TmpFile) /* Assemble an intermediate file which was generated by a previous processing ** step with SourceFile as input. The -dep options won't be added and ** the intermediate assembler file is removed after assembly. @@ -578,18 +587,20 @@ static void AssembleIntermediate (const char* SourceFile) ** name. It's the same name with the extension replaced by ".s" */ char* AsmName = MakeFilename (SourceFile, ".s"); + char* AsmTmpName = TmpFile ? MakeFilename(TmpFile, ".s") : NULL; /* Assemble the intermediate assembler file */ - AssembleFile (AsmName, CA65.ArgCount); + AssembleFile (AsmName, AsmTmpName, CA65.ArgCount); /* Remove the input file */ - if (remove (AsmName) < 0) { + if (remove (AsmTmpName ? AsmTmpName : AsmName) < 0) { Warning ("Cannot remove temporary file '%s': %s", - AsmName, strerror (errno)); + AsmTmpName ? AsmTmpName : AsmName, strerror (errno)); } /* Free the assembler file name which was allocated from the heap */ xfree (AsmName); + xfree (AsmTmpName); } @@ -612,7 +623,7 @@ static void Assemble (const char* File) } /* Use the common routine */ - AssembleFile (File, ArgCount); + AssembleFile (File, NULL, ArgCount); } @@ -660,7 +671,7 @@ static void Compile (const char* File) /* Add the file as argument for the compiler */ CmdAddArg (&CC65, File); - if (DoAssemble && DoLink) { + if (DoAssemble) { /* set a temporary output file name */ TmpFile = MakeTmpFilename(File, ".s"); CmdSetOutput (&CC65, TmpFile); @@ -680,7 +691,7 @@ static void Compile (const char* File) */ if (DoAssemble) { /* Assemble the intermediate file and remove it */ - AssembleIntermediate (TmpFile ? TmpFile : File); + AssembleIntermediate (File, TmpFile); if (TmpFile) { xfree(TmpFile); } @@ -717,7 +728,7 @@ static void CompileRes (const char* File) */ if (DoAssemble) { /* Assemble the intermediate file and remove it */ - AssembleIntermediate (File); + AssembleIntermediate (File, NULL); } } @@ -753,7 +764,7 @@ static void ConvertO65 (const char* File) */ if (DoAssemble) { /* Assemble the intermediate file and remove it */ - AssembleIntermediate (File); + AssembleIntermediate (File, NULL); } } From 9a2f754e8d5ecea6fc17ade5165e64b4f66897c6 Mon Sep 17 00:00:00 2001 From: Gorilla Sapiens Date: Tue, 27 May 2025 06:18:24 +0000 Subject: [PATCH 3/9] fixes problems found by github autobuild --- src/cl65/main.c | 2 +- src/common/fname.c | 16 +++++++++++++--- src/common/fname.h | 2 +- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/cl65/main.c b/src/cl65/main.c index 900602c87..8d8aac932 100644 --- a/src/cl65/main.c +++ b/src/cl65/main.c @@ -673,7 +673,7 @@ static void Compile (const char* File) if (DoAssemble) { /* set a temporary output file name */ - TmpFile = MakeTmpFilename(File, ".s"); + TmpFile = MakeTmpFilename(".s"); CmdSetOutput (&CC65, TmpFile); } diff --git a/src/common/fname.c b/src/common/fname.c index c5000bc72..f026f776d 100644 --- a/src/common/fname.c +++ b/src/common/fname.c @@ -119,16 +119,26 @@ char* MakeFilename (const char* Origin, const char* Ext) -char* MakeTmpFilename (const char* Origin, const char* Ext) +char* MakeTmpFilename (const char* Ext) /* Make a new temporary file name from Ext. tmpnam(3) is called -** and Ext is appended to generate the filename. Origin is ignored. +** and Ext is appended to generate the filename. ** The result is placed in a malloc'ed buffer and returned. */ { char* Out; char Buffer[L_tmpnam * 2]; /* a lazy way to ensure we have space for Ext */ - tmpnam(Buffer); + /* + ** gcc emits the following warning here: + ** + ** warning: the use of `tmpnam' is dangerous, better use `mkstemp' + ** + ** however, mkstemp actually opens a file, which we do not want. + ** we could write our own version, but then we would have to struggle + ** with supporting multiple build environments. tmpnam(3) is fine + ** here. + */ + (void) tmpnam(Buffer); strcat(Buffer, Ext); Out = xmalloc (strlen (Buffer) + 1); diff --git a/src/common/fname.h b/src/common/fname.h index 1dc985f93..cd163e4e7 100644 --- a/src/common/fname.h +++ b/src/common/fname.h @@ -59,7 +59,7 @@ char* MakeFilename (const char* Origin, const char* Ext); ** The function may be used to create "foo.o" from "foo.s". */ -char* MakeTmpFilename (const char* Origin, const char* Ext); +char* MakeTmpFilename (const char* Ext); /* Make a new temporary file name from Ext. tmpnam(3) is called ** and Ext is appended to generate the filename. Origin is ignored. ** The result is placed in a malloc'ed buffer and returned. From ca8b0726088083d8e5e08d0472915d6489efd867 Mon Sep 17 00:00:00 2001 From: Gorilla Sapiens Date: Tue, 27 May 2025 06:30:12 +0000 Subject: [PATCH 4/9] fixed another pedantic github build problem --- src/common/fname.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/common/fname.c b/src/common/fname.c index f026f776d..4dc7006f7 100644 --- a/src/common/fname.c +++ b/src/common/fname.c @@ -134,12 +134,17 @@ char* MakeTmpFilename (const char* Ext) ** warning: the use of `tmpnam' is dangerous, better use `mkstemp' ** ** however, mkstemp actually opens a file, which we do not want. + ** tmpfile() is unsuitable for the same reason. + ** ** we could write our own version, but then we would have to struggle - ** with supporting multiple build environments. tmpnam(3) is fine - ** here. + ** with supporting multiple build environments. + ** + ** tmpnam(3) is safe here, because ca65 / cc65 / ld65 will simply clobber + ** an existing file, or exit if with an error if they are unable to. + ** + ** gcc will also complain, if you don't use the return value from tmpnam(3) */ - (void) tmpnam(Buffer); - strcat(Buffer, Ext); + strcat(tmpnam(Buffer), Ext); Out = xmalloc (strlen (Buffer) + 1); strcpy (Out, Buffer); From a1139aa6b8696063fa32e3696295f46db48b2b11 Mon Sep 17 00:00:00 2001 From: Gorilla Sapiens Date: Tue, 27 May 2025 06:31:54 +0000 Subject: [PATCH 5/9] removed dangling space --- src/common/fname.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/fname.c b/src/common/fname.c index 4dc7006f7..e67470b33 100644 --- a/src/common/fname.c +++ b/src/common/fname.c @@ -138,7 +138,7 @@ char* MakeTmpFilename (const char* Ext) ** ** we could write our own version, but then we would have to struggle ** with supporting multiple build environments. - ** + ** ** tmpnam(3) is safe here, because ca65 / cc65 / ld65 will simply clobber ** an existing file, or exit if with an error if they are unable to. ** From 9ae0eaafcc97b25fd145320bac4120c8bd6bb0dd Mon Sep 17 00:00:00 2001 From: Gorilla Sapiens Date: Tue, 27 May 2025 14:13:52 +0000 Subject: [PATCH 6/9] fixed inaccurate comment --- src/common/fname.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/fname.h b/src/common/fname.h index cd163e4e7..852c4ae56 100644 --- a/src/common/fname.h +++ b/src/common/fname.h @@ -61,7 +61,7 @@ char* MakeFilename (const char* Origin, const char* Ext); char* MakeTmpFilename (const char* Ext); /* Make a new temporary file name from Ext. tmpnam(3) is called -** and Ext is appended to generate the filename. Origin is ignored. +** and Ext is appended to generate the filename. ** The result is placed in a malloc'ed buffer and returned. */ From 842ec4b481513ea54f829132ec2fe842c1c6dbc5 Mon Sep 17 00:00:00 2001 From: Gorilla Sapiens Date: Thu, 29 May 2025 14:26:25 +0000 Subject: [PATCH 7/9] tmpfiles for the *.grc -> *.s -> *.o -> exe path --- src/cl65/main.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/cl65/main.c b/src/cl65/main.c index 8d8aac932..d68e7c7e7 100644 --- a/src/cl65/main.c +++ b/src/cl65/main.c @@ -378,6 +378,14 @@ static void CmdSetOutput (CmdDesc* Cmd, const char* File) +static void CmdSetAsmOutput (CmdDesc* Cmd, const char* File) +/* Set the output asm file in a command desc for grc65 */ +{ + CmdAddArg2 (Cmd, "-s", File); +} + + + static void CmdSetTarget (CmdDesc* Cmd, target_t Target) /* Set the output file in a command desc */ { @@ -703,6 +711,9 @@ static void Compile (const char* File) static void CompileRes (const char* File) /* Compile the given geos resource file */ { + /* tmp Asm file name, if needed */ + char* AsmName = NULL; + /* Remember the current assembler argument count */ unsigned ArgCount = GRC.ArgCount; @@ -711,6 +722,14 @@ static void CompileRes (const char* File) */ CmdSetTarget (&GRC, Target); + /* Changes to output file name must come + ** BEFORE adding the file + */ + if (DoAssemble && DoLink) { + AsmName = MakeTmpFilename(".s"); + CmdSetAsmOutput(&GRC, AsmName); + } + /* Add the file as argument for the resource compiler */ CmdAddArg (&GRC, File); @@ -728,7 +747,10 @@ static void CompileRes (const char* File) */ if (DoAssemble) { /* Assemble the intermediate file and remove it */ - AssembleIntermediate (File, NULL); + AssembleIntermediate (File, AsmName); + if (AsmName) { + xfree(AsmName); + } } } From 779c393e65fae1c5a64115f8e4673817cdcc5be7 Mon Sep 17 00:00:00 2001 From: Gorilla Sapiens Date: Thu, 29 May 2025 15:05:03 +0000 Subject: [PATCH 8/9] fixes *.s -> *.o -> exe path --- src/cl65/main.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/cl65/main.c b/src/cl65/main.c index d68e7c7e7..b48841149 100644 --- a/src/cl65/main.c +++ b/src/cl65/main.c @@ -552,7 +552,12 @@ static void AssembleFile (const char* File, const char* TmpFile, unsigned ArgCou ** to the file list of the linker. The name of the output ** file is that of the input file with ".s" replaced by ".o". */ - ObjName = MakeFilename (TmpFile ? TmpFile : File, ".o"); + if (TmpFile) { + ObjName = MakeFilename (TmpFile, ".o"); + } + else { + ObjName = MakeTmpFilename (".o"); + } CmdSetOutput (&CA65, ObjName); CmdAddFile (&LD65, ObjName); /* This is just a temporary file, schedule it for removal */ From df99b9a107b96eb2c43f934866b7c76680496950 Mon Sep 17 00:00:00 2001 From: Bob Andrews Date: Sat, 31 May 2025 19:00:11 +0200 Subject: [PATCH 9/9] fix codestyle --- src/cl65/main.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/cl65/main.c b/src/cl65/main.c index b48841149..42126e6d7 100644 --- a/src/cl65/main.c +++ b/src/cl65/main.c @@ -554,8 +554,7 @@ static void AssembleFile (const char* File, const char* TmpFile, unsigned ArgCou */ if (TmpFile) { ObjName = MakeFilename (TmpFile, ".o"); - } - else { + } else { ObjName = MakeTmpFilename (".o"); } CmdSetOutput (&CA65, ObjName); @@ -567,8 +566,7 @@ static void AssembleFile (const char* File, const char* TmpFile, unsigned ArgCou /* This is the final step. If an output name is given, set it */ if (OutputName) { CmdSetOutput (&CA65, OutputName); - } - else { + } else { ObjName = MakeFilename (File, ".o"); CmdSetOutput (&CA65, ObjName); xfree (ObjName);