1 /*****************************************************************************/
5 /* Main module for the cl65 compile and link utility */
9 /* (C) 1999-2000 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
41 #if defined(__WATCOMC__) || defined(_MSC_VER)
42 # include <process.h> /* DOS, OS/2 and Windows */
44 # include "spawn.h" /* All others */
60 /*****************************************************************************/
62 /*****************************************************************************/
66 /* Struct that describes a command */
67 typedef struct CmdDesc_ CmdDesc;
69 char* Name; /* The command name */
71 unsigned ArgCount; /* Count of arguments */
72 unsigned ArgMax; /* Maximum count of arguments */
73 char** Args; /* The arguments */
75 unsigned FileCount; /* Count of files to translate */
76 unsigned FileMax; /* Maximum count of files */
77 char** Files; /* The files */
80 /* Command descriptors for the different programs */
81 static CmdDesc CC65 = { 0, 0, 0, 0, 0, 0, 0 };
82 static CmdDesc CA65 = { 0, 0, 0, 0, 0, 0, 0 };
83 static CmdDesc LD65 = { 0, 0, 0, 0, 0, 0, 0 };
84 static CmdDesc GRC = { 0, 0, 0, 0, 0, 0, 0 };
93 FILETYPE_GR /* GEOS resource file */
96 /* Default file type, used if type unknown */
97 static unsigned DefaultFileType = FILETYPE_UNKNOWN;
99 /* Variables controlling the steps we're doing */
100 static int DontLink = 0;
101 static int DontAssemble = 0;
103 /* The name of the output file, NULL if none given */
104 static const char* OutputName = 0;
106 /* The name of the linker configuration file if given */
107 static const char* LinkerConfig = 0;
109 /* The name of the first input file. This will be used to construct the
110 * executable file name if no explicit name is given.
112 static const char* FirstInput = 0;
114 /* Name of the crt0 object file and the runtime library */
115 static char* TargetCRT0 = 0;
116 static char* TargetLib = 0;
120 /*****************************************************************************/
121 /* Determine a file type */
122 /*****************************************************************************/
126 static unsigned GetFileType (const char* File)
127 /* Determine the type of the given file */
129 /* Table mapping extensions to file types */
130 static const struct {
134 { ".c", FILETYPE_C },
135 { ".s", FILETYPE_ASM },
136 { ".asm", FILETYPE_ASM },
137 { ".a65", FILETYPE_ASM },
138 { ".o", FILETYPE_OBJ },
139 { ".obj", FILETYPE_OBJ },
140 { ".a", FILETYPE_LIB },
141 { ".lib", FILETYPE_LIB },
142 { ".grc", FILETYPE_GR },
147 /* Determine the file type by the extension */
148 const char* Ext = FindExt (File);
150 /* Do we have an extension? */
152 return DefaultFileType;
155 /* Check for known extensions */
156 for (I = 0; I < sizeof (FileTypes) / sizeof (FileTypes [0]); ++I) {
157 if (strcmp (FileTypes [I].Ext, Ext) == 0) {
159 return FileTypes [I].Type;
163 /* Not found, return the default */
164 return DefaultFileType;
169 /*****************************************************************************/
170 /* Command structure handling */
171 /*****************************************************************************/
175 static void CmdAddArg (CmdDesc* Cmd, const char* Arg)
176 /* Add a new argument to the command */
178 /* Expand the argument vector if needed */
179 if (Cmd->ArgCount == Cmd->ArgMax) {
180 unsigned NewMax = Cmd->ArgMax + 10;
181 char** NewArgs = xmalloc (NewMax * sizeof (char*));
182 memcpy (NewArgs, Cmd->Args, Cmd->ArgMax * sizeof (char*));
185 Cmd->ArgMax = NewMax;
188 /* Add a copy of the new argument, allow a NULL pointer */
190 Cmd->Args [Cmd->ArgCount++] = xstrdup (Arg);
192 Cmd->Args [Cmd->ArgCount++] = 0;
198 static void CmdDelArgs (CmdDesc* Cmd, unsigned LastValid)
199 /* Remove all arguments with an index greater than LastValid */
201 while (Cmd->ArgCount > LastValid) {
203 xfree (Cmd->Args [Cmd->ArgCount]);
204 Cmd->Args [Cmd->ArgCount] = 0;
210 static void CmdAddFile (CmdDesc* Cmd, const char* File)
211 /* Add a new file to the command */
213 /* Expand the file vector if needed */
214 if (Cmd->FileCount == Cmd->FileMax) {
215 unsigned NewMax = Cmd->FileMax + 10;
216 char** NewFiles = xmalloc (NewMax * sizeof (char*));
217 memcpy (NewFiles, Cmd->Files, Cmd->FileMax * sizeof (char*));
219 Cmd->Files = NewFiles;
220 Cmd->FileMax = NewMax;
223 /* If the file name is not NULL (which is legal and is used to terminate
224 * the file list), check if the file name does already exist in the file
225 * list and print a warning if so. Regardless of the search result, add
230 for (I = 0; I < Cmd->FileCount; ++I) {
231 if (strcmp (Cmd->Files[I], File) == 0) {
233 Warning ("Duplicate file in argument list: `%s'", File);
234 /* No need to search further */
240 Cmd->Files [Cmd->FileCount++] = xstrdup (File);
242 /* Add a NULL pointer */
243 Cmd->Files [Cmd->FileCount++] = 0;
249 static void CmdInit (CmdDesc* Cmd, const char* Path)
250 /* Initialize the command using the given path to the executable */
252 /* Remember the command */
253 Cmd->Name = xstrdup (Path);
255 /* Use the command name as first argument */
256 CmdAddArg (Cmd, Path);
261 static void CmdSetOutput (CmdDesc* Cmd, const char* File)
262 /* Set the output file in a command desc */
264 CmdAddArg (Cmd, "-o");
265 CmdAddArg (Cmd, File);
270 static void CmdSetTarget (CmdDesc* Cmd, target_t Target)
271 /* Set the output file in a command desc */
273 CmdAddArg (Cmd, "-t");
274 CmdAddArg (Cmd, TargetNames[Target]);
279 /*****************************************************************************/
280 /* Target handling */
281 /*****************************************************************************/
285 static void SetTargetFiles (void)
286 /* Set the target system files */
288 /* Determine the names of the default startup and library file */
289 if (Target != TGT_NONE) {
291 /* Get a pointer to the system name and its length */
292 const char* TargetName = TargetNames [Target];
293 unsigned TargetNameLen = strlen (TargetName);
295 /* Set the startup file */
296 TargetCRT0 = xmalloc (TargetNameLen + 2 + 1);
297 strcpy (TargetCRT0, TargetName);
298 strcat (TargetCRT0, ".o");
300 /* Set the library file */
301 TargetLib = xmalloc (TargetNameLen + 4 + 1);
302 strcpy (TargetLib, TargetName);
303 strcat (TargetLib, ".lib");
310 /*****************************************************************************/
312 /*****************************************************************************/
316 static void ExecProgram (CmdDesc* Cmd)
317 /* Execute a subprocess with the given name/parameters. Exit on errors. */
319 /* Call the program */
320 int Status = spawnvp (P_WAIT, Cmd->Name, Cmd->Args);
322 /* Check the result code */
324 /* Error executing the program */
325 Error ("Cannot execute `%s': %s", Cmd->Name, strerror (errno));
326 } else if (Status != 0) {
327 /* Called program had an error */
334 static void Link (void)
335 /* Link the resulting executable */
339 /* If we have a linker config file given, add it to the command line.
340 * Otherwise pass the target to the linker if we have one.
343 CmdAddArg (&LD65, "-C");
344 CmdAddArg (&LD65, LinkerConfig);
345 } else if (Target != TGT_NONE) {
346 CmdSetTarget (&LD65, Target);
349 /* Determine which target libraries are needed */
352 /* Since linking is always the final step, if we have an output file name
353 * given, set it here. If we don't have an explicit output name given,
354 * try to build one from the name of the first input file.
358 CmdAddArg (&LD65, "-o");
359 CmdAddArg (&LD65, OutputName);
361 } else if (FirstInput && FindExt (FirstInput)) { /* Only if ext present! */
363 char* Output = MakeFilename (FirstInput, "");
364 CmdAddArg (&LD65, "-o");
365 CmdAddArg (&LD65, Output);
370 /* If we have a startup file, add its name as a parameter */
372 CmdAddArg (&LD65, TargetCRT0);
375 /* Add all object files as parameters */
376 for (I = 0; I < LD65.FileCount; ++I) {
377 CmdAddArg (&LD65, LD65.Files [I]);
380 /* Add the system runtime library */
382 CmdAddArg (&LD65, TargetLib);
385 /* Terminate the argument list with a NULL pointer */
386 CmdAddArg (&LD65, 0);
388 /* Call the linker */
394 static void Assemble (const char* File)
395 /* Assemble the given file */
397 /* Remember the current assembler argument count */
398 unsigned ArgCount = CA65.ArgCount;
400 /* Set the target system */
401 CmdSetTarget (&CA65, Target);
403 /* If we won't link, this is the final step. In this case, set the
406 if (DontLink && OutputName) {
407 CmdSetOutput (&CA65, OutputName);
409 /* The object file name will be the name of the source file
410 * with .s replaced by ".o". Add this file to the list of
413 char* ObjName = MakeFilename (File, ".o");
414 CmdAddFile (&LD65, ObjName);
418 /* Add the file as argument for the assembler */
419 CmdAddArg (&CA65, File);
421 /* Add a NULL pointer to terminate the argument list */
422 CmdAddArg (&CA65, 0);
424 /* Run the assembler */
427 /* Remove the excess arguments */
428 CmdDelArgs (&CA65, ArgCount);
433 static void Compile (const char* File)
434 /* Compile the given file */
438 /* Remember the current assembler argument count */
439 unsigned ArgCount = CC65.ArgCount;
441 /* Set the target system */
442 CmdSetTarget (&CC65, Target);
444 /* If we won't link, this is the final step. In this case, set the
447 if (DontAssemble && OutputName) {
448 CmdSetOutput (&CC65, OutputName);
450 /* The assembler file name will be the name of the source file
451 * with .c replaced by ".s".
453 AsmName = MakeFilename (File, ".s");
456 /* Add the file as argument for the compiler */
457 CmdAddArg (&CC65, File);
459 /* Add a NULL pointer to terminate the argument list */
460 CmdAddArg (&CC65, 0);
462 /* Run the compiler */
465 /* Remove the excess arguments */
466 CmdDelArgs (&CC65, ArgCount);
468 /* If this is not the final step, assemble the generated file, then
473 if (remove (AsmName) < 0) {
474 Warning ("Cannot remove temporary file `%s': %s",
475 AsmName, strerror (errno));
483 static void CompileRes (const char* File)
484 /* Compile the given geos resource file */
488 /* Remember the current assembler argument count */
489 unsigned ArgCount = GRC.ArgCount;
491 /* The assembler file name will be the name of the source file
492 * with .grc replaced by ".s".
494 AsmName = MakeFilename (File, ".s");
496 /* Add the file as argument for the resource compiler */
497 CmdAddArg (&GRC, File);
499 /* Add a NULL pointer to terminate the argument list */
502 /* Run the compiler */
505 /* Remove the excess arguments */
506 CmdDelArgs (&GRC, ArgCount);
508 /* If this is not the final step, assemble the generated file, then
513 if (remove (AsmName) < 0) {
514 Warning ("Cannot remove temporary file `%s': %s",
515 AsmName, strerror (errno));
519 /* Free the assembler file name which was allocated from the heap */
525 /*****************************************************************************/
527 /*****************************************************************************/
531 static void Usage (void)
532 /* Print usage information and exit */
535 "Usage: %s [options] file\n"
537 " -A\t\t\tStrict ANSI mode\n"
538 " -C name\t\tUse linker config file\n"
539 " -Cl\t\t\tMake local variables static\n"
540 " -D sym[=defn]\t\tDefine a preprocessor symbol\n"
541 " -I dir\t\tSet a compiler include directory path\n"
542 " -Ln name\t\tCreate a VICE label file\n"
543 " -O\t\t\tOptimize code\n"
544 " -Oi\t\t\tOptimize code, inline functions\n"
545 " -Or\t\t\tOptimize code, honour the register keyword\n"
546 " -Os\t\t\tOptimize code, inline known C funtions\n"
547 " -S\t\t\tCompile but don't assemble and link\n"
548 " -V\t\t\tPrint the version number\n"
549 " -W\t\t\tSuppress warnings\n"
550 " -c\t\t\tCompiler and assemble but don't link\n"
551 " -d\t\t\tDebug mode\n"
552 " -g\t\t\tAdd debug info\n"
553 " -h\t\t\tHelp (this text)\n"
554 " -l\t\t\tCreate an assembler listing\n"
555 " -m name\t\tCreate a map file\n"
556 " -o name\t\tName the output file\n"
557 " -t sys\t\tSet the target system\n"
558 " -v\t\t\tVerbose mode\n"
559 " -vm\t\t\tVerbose map file\n"
562 " --ansi\t\tStrict ANSI mode\n"
563 " --asm-include-dir dir\tSet an assembler include directory\n"
564 " --debug\t\tDebug mode\n"
565 " --debug-info\t\tAdd debug info\n"
566 " --feature name\tSet an emulation feature\n"
567 " --help\t\tHelp (this text)\n"
568 " --include-dir dir\tSet a compiler include directory path\n"
569 " --listing\t\tCreate an assembler listing\n"
570 " --mapfile name\tCreate a map file\n"
571 " --start-addr addr\tSet the default start address\n"
572 " --target sys\t\tSet the target system\n"
573 " --version\t\tPrint the version number\n"
574 " --verbose\t\tVerbose mode\n",
580 static void OptAnsi (const char* Opt, const char* Arg)
581 /* Strict ANSI mode (compiler) */
583 CmdAddArg (&CC65, "-A");
588 static void OptAsmIncludeDir (const char* Opt, const char* Arg)
589 /* Include directory (assembler) */
591 CmdAddArg (&CA65, "-I");
592 CmdAddArg (&CA65, Arg);
597 static void OptDebug (const char* Opt, const char* Arg)
598 /* Debug mode (compiler) */
600 CmdAddArg (&CC65, "-d");
605 static void OptDebugInfo (const char* Opt, const char* Arg)
606 /* Debug Info - add to compiler and assembler */
608 CmdAddArg (&CC65, "-g");
609 CmdAddArg (&CA65, "-g");
614 static void OptFeature (const char* Opt, const char* Arg)
615 /* Emulation features for the assembler */
617 CmdAddArg (&CA65, "--feature");
618 CmdAddArg (&CA65, Arg);
623 static void OptHelp (const char* Opt, const char* Arg)
624 /* Print help - cl65 */
632 static void OptIncludeDir (const char* Opt, const char* Arg)
633 /* Include directory (compiler) */
635 CmdAddArg (&CC65, "-I");
636 CmdAddArg (&CC65, Arg);
641 static void OptListing (const char* Opt, const char* Arg)
642 /* Create an assembler listing */
644 CmdAddArg (&CA65, "-l");
649 static void OptMapFile (const char* Opt, const char* Arg)
650 /* Create a map file */
652 /* Create a map file (linker) */
653 CmdAddArg (&LD65, "-m");
654 CmdAddArg (&LD65, Arg);
659 static void OptStartAddr (const char* Opt, const char* Arg)
660 /* Set the default start address */
662 CmdAddArg (&LD65, "-S");
663 CmdAddArg (&LD65, Arg);
668 static void OptTarget (const char* Opt, const char* Arg)
669 /* Set the target system */
671 Target = FindTarget (Arg);
672 if (Target == TGT_UNKNOWN) {
673 Error ("No such target system: `%s'", Arg);
679 static void OptVerbose (const char* Opt, const char* Arg)
680 /* Verbose mode (compiler, assembler, linker) */
682 CmdAddArg (&CC65, "-v");
683 CmdAddArg (&CA65, "-v");
684 CmdAddArg (&LD65, "-v");
689 static void OptVersion (const char* Opt, const char* Arg)
690 /* Print version number */
693 "cl65 V%u.%u.%u - (C) Copyright 1998-2000 Ullrich von Bassewitz\n",
694 VER_MAJOR, VER_MINOR, VER_PATCH);
699 int main (int argc, char* argv [])
700 /* Utility main program */
702 /* Program long options */
703 static const LongOpt OptTab[] = {
704 { "--ansi", 0, OptAnsi },
705 { "--asm-include-dir", 1, OptAsmIncludeDir },
706 { "--debug", 0, OptDebug },
707 { "--debug-info", 0, OptDebugInfo },
708 { "--feature", 1, OptFeature },
709 { "--help", 0, OptHelp },
710 { "--include-dir", 1, OptIncludeDir },
711 { "--listing", 0, OptListing },
712 { "--mapfile", 1, OptMapFile },
713 { "--start-addr", 1, OptStartAddr },
714 { "--target", 1, OptTarget },
715 { "--verbose", 0, OptVerbose },
716 { "--version", 0, OptVersion },
721 /* Initialize the cmdline module */
722 InitCmdLine (argc, argv, "cl65");
724 /* Initialize the command descriptors */
725 CmdInit (&CC65, "cc65");
726 CmdInit (&CA65, "ca65");
727 CmdInit (&LD65, "ld65");
728 CmdInit (&GRC, "grc");
730 /* Our default target is the C64 instead of "none" */
733 /* Check the parameters */
737 /* Get the argument */
738 const char* Arg = argv [I];
740 /* Check for an option */
741 if (Arg [0] == '-') {
746 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
750 /* Strict ANSI mode (compiler) */
755 if (Arg[2] == 'l' && Arg[3] == '\0') {
756 /* Make local variables static */
757 CmdAddArg (&CC65, "-Cl");
759 /* Specify linker config file */
760 LinkerConfig = GetArg (&I, 2);
765 /* Define a preprocessor symbol (compiler) */
766 CmdAddArg (&CC65, "-D");
767 CmdAddArg (&CC65, GetArg (&I, 2));
771 /* Include directory (compiler) */
772 OptIncludeDir (Arg, GetArg (&I, 2));
777 /* VICE label file (linker) */
778 CmdAddArg (&LD65, "-Ln");
779 CmdAddArg (&LD65, GetArg (&I, 3));
786 /* Optimize code (compiler, also covers -Oi and others) */
787 CmdAddArg (&CC65, Arg);
791 /* Dont assemble and link the created files */
792 DontLink = DontAssemble = 1;
796 /* Include source as comment (compiler) */
797 CmdAddArg (&CC65, "-T");
801 /* Print version number */
806 /* Suppress warnings - compiler and assembler */
807 CmdAddArg (&CC65, "-W");
808 CmdAddArg (&CA65, "-W");
809 CmdAddArg (&CA65, "0");
813 /* Don't link the resulting files */
818 /* Debug mode (compiler) */
823 /* Debugging - add to compiler and assembler */
824 OptDebugInfo (Arg, 0);
829 /* Print help - cl65 */
834 /* Create an assembler listing */
839 /* Create a map file (linker) */
840 OptMapFile (Arg, GetArg (&I, 2));
844 /* Name the output file */
845 OutputName = GetArg (&I, 2);
849 /* Set target system - compiler, assembler and linker */
850 OptTarget (Arg, GetArg (&I, 2));
854 if (Arg [2] == 'm') {
855 /* Verbose map file (linker) */
856 CmdAddArg (&LD65, "-vm");
858 /* Verbose mode (compiler, assembler, linker) */
868 /* Remember the first file name */
869 if (FirstInput == 0) {
873 /* Determine the file type by the extension */
874 switch (GetFileType (Arg)) {
877 /* Compile the file */
882 /* Assemble the file */
890 /* Add to the linker files */
891 CmdAddFile (&LD65, Arg);
895 /* Add to the resource compiler files */
900 Error ("Don't know what to do with `%s'", Arg);
910 /* Check if we had any input files */
911 if (FirstInput == 0) {
912 Warning ("No input files");
915 /* Link the given files if requested and if we have any */
916 if (DontLink == 0 && LD65.FileCount > 0) {
920 /* Return an apropriate exit code */