]> git.sur5r.net Git - cc65/blob - src/cl65/main.c
The check for illegal storage classes on globals was wrong
[cc65] / src / cl65 / main.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  main.c                                   */
4 /*                                                                           */
5 /*             Main module for the cl65 compile and link utility             */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1999-2002 Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@musoftware.de                                            */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 /* Check out if we have a spawn() function on the system, or if we must use
37  * our own.
38  */
39 #if defined(__WATCOMC__) || defined(_MSC_VER) || defined(__MINGW32__) || defined(__DJGPP__)
40 #  define HAVE_SPAWN    1
41 #else
42 #  define NEED_SPAWN   1
43 #endif
44
45
46
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <ctype.h>
51 #include <errno.h>
52 #ifdef HAVE_SPAWN
53 #  include <process.h>
54 #endif
55
56 /* common */
57 #include "attrib.h"
58 #include "cmdline.h"
59 #include "fname.h"
60 #include "strbuf.h"
61 #include "target.h"
62 #include "version.h"
63 #include "xmalloc.h"
64
65 /* cl65 */
66 #include "global.h"
67 #include "error.h"
68
69
70
71 /*****************************************************************************/
72 /*                                   Data                                    */
73 /*****************************************************************************/
74
75
76
77 /* Struct that describes a command */
78 typedef struct CmdDesc CmdDesc;
79 struct CmdDesc {
80     char*       Name;           /* The command name */
81
82     unsigned    ArgCount;       /* Count of arguments */
83     unsigned    ArgMax;         /* Maximum count of arguments */
84     char**      Args;           /* The arguments */
85
86     unsigned    FileCount;      /* Count of files to translate */
87     unsigned    FileMax;        /* Maximum count of files */
88     char**      Files;          /* The files */
89 };
90
91 /* Command descriptors for the different programs */
92 static CmdDesc CC65 = { 0, 0, 0, 0, 0, 0, 0 };
93 static CmdDesc CA65 = { 0, 0, 0, 0, 0, 0, 0 };
94 static CmdDesc LD65 = { 0, 0, 0, 0, 0, 0, 0 };
95 static CmdDesc GRC  = { 0, 0, 0, 0, 0, 0, 0 };
96
97 /* File types */
98 enum {
99     FILETYPE_UNKNOWN,
100     FILETYPE_C,
101     FILETYPE_ASM,
102     FILETYPE_OBJ,
103     FILETYPE_LIB,
104     FILETYPE_GR                 /* GEOS resource file */
105 };
106
107 /* Default file type, used if type unknown */
108 static unsigned DefaultFileType = FILETYPE_UNKNOWN;
109
110 /* Variables controlling the steps we're doing */
111 static int DontLink     = 0;
112 static int DontAssemble = 0;
113
114 /* The name of the output file, NULL if none given */
115 static const char* OutputName = 0;
116
117 /* The name of the linker configuration file if given */
118 static const char* LinkerConfig = 0;
119
120 /* The name of the first input file. This will be used to construct the
121  * executable file name if no explicit name is given.
122  */
123 static const char* FirstInput = 0;
124
125 /* Remember if we should link a module */
126 static int Module = 0;
127
128 /* Extension used for a module */
129 #define MODULE_EXT      ".o65"
130
131 /* Name of the crt0 object file and the runtime library */
132 static char* TargetCRT0 = 0;
133 static char* TargetLib  = 0;
134
135
136
137 /*****************************************************************************/
138 /*                Include the system specific spawn function                 */
139 /*****************************************************************************/
140
141
142
143 #if defined(NEED_SPAWN)
144 #  if defined(SPAWN_UNIX)
145 #    include "spawn-unix.inc"
146 #  elif defined(SPAWN_AMIGA)
147 #    include "spawn-amiga.inc"
148 #  else
149 #    error "Don't know which spawn module to include!"
150 #  endif
151 #endif
152
153
154
155 /*****************************************************************************/
156 /*                           Determine a file type                           */
157 /*****************************************************************************/
158
159
160
161 static unsigned GetFileType (const char* File)
162 /* Determine the type of the given file */
163 {
164     /* Table mapping extensions to file types */
165     static const struct {
166         const char*     Ext;
167         unsigned        Type;
168     } FileTypes [] = {
169         {   ".c",       FILETYPE_C      },
170         {   ".s",       FILETYPE_ASM    },
171         {   ".asm",     FILETYPE_ASM    },
172         {   ".a65",     FILETYPE_ASM    },
173         {   ".o",       FILETYPE_OBJ    },
174         {   ".obj",     FILETYPE_OBJ    },
175         {   ".a",       FILETYPE_LIB    },
176         {   ".lib",     FILETYPE_LIB    },
177         {   ".grc",     FILETYPE_GR     },
178     };
179
180     unsigned I;
181
182     /* Determine the file type by the extension */
183     const char* Ext = FindExt (File);
184
185     /* Do we have an extension? */
186     if (Ext == 0) {
187         return DefaultFileType;
188     }
189
190     /* Check for known extensions */
191     for (I = 0; I < sizeof (FileTypes) / sizeof (FileTypes [0]); ++I) {
192         if (strcmp (FileTypes [I].Ext, Ext) == 0) {
193             /* Found */
194             return FileTypes [I].Type;
195         }
196     }
197
198     /* Not found, return the default */
199     return DefaultFileType;
200 }
201
202
203
204 /*****************************************************************************/
205 /*                        Command structure handling                         */
206 /*****************************************************************************/
207
208
209
210 static void CmdAddArg (CmdDesc* Cmd, const char* Arg)
211 /* Add a new argument to the command */
212 {
213     /* Expand the argument vector if needed */
214     if (Cmd->ArgCount == Cmd->ArgMax) {
215         unsigned NewMax  = Cmd->ArgMax + 10;
216         char**   NewArgs = xmalloc (NewMax * sizeof (char*));
217         memcpy (NewArgs, Cmd->Args, Cmd->ArgMax * sizeof (char*));
218         xfree (Cmd->Args);
219         Cmd->Args   = NewArgs;
220         Cmd->ArgMax = NewMax;
221     }
222
223     /* Add a copy of the new argument, allow a NULL pointer */
224     if (Arg) {
225         Cmd->Args [Cmd->ArgCount++] = xstrdup (Arg);
226     } else {
227         Cmd->Args [Cmd->ArgCount++] = 0;
228     }
229 }
230
231
232
233 static void CmdDelArgs (CmdDesc* Cmd, unsigned LastValid)
234 /* Remove all arguments with an index greater than LastValid */
235 {
236     while (Cmd->ArgCount > LastValid) {
237         Cmd->ArgCount--;
238         xfree (Cmd->Args [Cmd->ArgCount]);
239         Cmd->Args [Cmd->ArgCount] = 0;
240     }
241 }
242
243
244
245 static void CmdAddFile (CmdDesc* Cmd, const char* File)
246 /* Add a new file to the command */
247 {
248     /* Expand the file vector if needed */
249     if (Cmd->FileCount == Cmd->FileMax) {
250         unsigned NewMax   = Cmd->FileMax + 10;
251         char**   NewFiles = xmalloc (NewMax * sizeof (char*));
252         memcpy (NewFiles, Cmd->Files, Cmd->FileMax * sizeof (char*));
253         xfree (Cmd->Files);
254         Cmd->Files   = NewFiles;
255         Cmd->FileMax = NewMax;
256     }
257
258     /* If the file name is not NULL (which is legal and is used to terminate
259      * the file list), check if the file name does already exist in the file
260      * list and print a warning if so. Regardless of the search result, add
261      * the file.
262      */
263     if (File) {
264         unsigned I;
265         for (I = 0; I < Cmd->FileCount; ++I) {
266             if (strcmp (Cmd->Files[I], File) == 0) {
267                 /* Duplicate file */
268                 Warning ("Duplicate file in argument list: `%s'", File);
269                 /* No need to search further */
270                 break;
271             }
272         }
273
274         /* Add the file */
275         Cmd->Files [Cmd->FileCount++] = xstrdup (File);
276     } else {
277         /* Add a NULL pointer */
278         Cmd->Files [Cmd->FileCount++] = 0;
279     }
280 }
281
282
283
284 static void CmdInit (CmdDesc* Cmd, const char* Path)
285 /* Initialize the command using the given path to the executable */
286 {
287     /* Remember the command */
288     Cmd->Name = xstrdup (Path);
289
290     /* Use the command name as first argument */
291     CmdAddArg (Cmd, Path);
292 }
293
294
295
296 static void CmdSetOutput (CmdDesc* Cmd, const char* File)
297 /* Set the output file in a command desc */
298 {
299     CmdAddArg (Cmd, "-o");
300     CmdAddArg (Cmd, File);
301 }
302
303
304
305 static void CmdSetTarget (CmdDesc* Cmd, target_t Target)
306 /* Set the output file in a command desc */
307 {
308     CmdAddArg (Cmd, "-t");
309     CmdAddArg (Cmd, TargetNames[Target]);
310 }
311
312
313
314 static void CmdPrint (CmdDesc* Cmd, FILE* F)
315 /* Output the command line encoded in the command desc */
316 {
317     unsigned I;
318     for (I = 0; I < Cmd->ArgCount && Cmd->Args[I] != 0; ++I) {
319         fprintf (F, "%s ", Cmd->Args[I]);
320     }
321 }
322
323
324
325 /*****************************************************************************/
326 /*                              Target handling                              */
327 /*****************************************************************************/
328
329
330
331 static void SetTargetFiles (void)
332 /* Set the target system files */
333 {
334     /* Determine the names of the default startup and library file */
335     if (Target != TGT_NONE) {
336
337         /* Get a pointer to the system name and its length */
338         const char* TargetName = TargetNames [Target];
339         unsigned    TargetNameLen = strlen (TargetName);
340
341         /* Set the startup file */
342         TargetCRT0 = xmalloc (TargetNameLen + 2 + 1);
343         strcpy (TargetCRT0, TargetName);
344         strcat (TargetCRT0, ".o");
345
346         /* Set the library file */
347         TargetLib = xmalloc (TargetNameLen + 4 + 1);
348         strcpy (TargetLib, TargetName);
349         strcat (TargetLib, ".lib");
350
351     }
352 }
353
354
355
356 /*****************************************************************************/
357 /*                               Subprocesses                                */
358 /*****************************************************************************/
359
360
361
362 static void ExecProgram (CmdDesc* Cmd)
363 /* Execute a subprocess with the given name/parameters. Exit on errors. */
364 {
365     int Status;
366
367     /* If in debug mode, output the command line we will execute */
368     if (Debug) {
369         printf ("Executing: ");
370         CmdPrint (Cmd, stdout);
371         printf ("\n");
372     }
373
374     /* Call the program */
375     Status = spawnvp (P_WAIT, Cmd->Name, Cmd->Args);
376
377     /* Check the result code */
378     if (Status < 0) {
379         /* Error executing the program */
380         Error ("Cannot execute `%s': %s", Cmd->Name, strerror (errno));
381     } else if (Status != 0) {
382         /* Called program had an error */
383         exit (Status);
384     }
385 }
386
387
388
389 static void Link (void)
390 /* Link the resulting executable */
391 {
392     unsigned I;
393
394     /* If we have a linker config file given, add it to the command line.
395      * Otherwise pass the target to the linker if we have one.
396      */
397     if (LinkerConfig) {
398         if (Module) {
399             Error ("Cannot use -C and --module together");
400         }
401         CmdAddArg (&LD65, "-C");
402         CmdAddArg (&LD65, LinkerConfig);
403     } else if (Module) {
404         CmdSetTarget (&LD65, TGT_MODULE);
405     } else if (Target != TGT_NONE) {
406         CmdSetTarget (&LD65, Target);
407     }
408
409     /* Determine which target libraries are needed */
410     SetTargetFiles ();
411
412     /* Since linking is always the final step, if we have an output file name
413      * given, set it here. If we don't have an explicit output name given,
414      * try to build one from the name of the first input file.
415      */
416     if (OutputName) {
417
418         CmdSetOutput (&LD65, OutputName);
419
420     } else if (FirstInput && FindExt (FirstInput)) {  /* Only if ext present! */
421
422         const char* Extension = Module? MODULE_EXT : "";
423         char* Output = MakeFilename (FirstInput, Extension);
424         CmdSetOutput (&LD65, Output);
425         xfree (Output);
426
427     }
428
429     /* If we have a startup file and if we are not linking a module, add its
430      * name as a parameter
431      */
432     if (TargetCRT0 && !Module) {
433         CmdAddArg (&LD65, TargetCRT0);
434     }
435
436     /* Add all object files as parameters */
437     for (I = 0; I < LD65.FileCount; ++I) {
438         CmdAddArg (&LD65, LD65.Files [I]);
439     }
440
441     /* Add the system runtime library */
442     if (TargetLib) {
443         CmdAddArg (&LD65, TargetLib);
444     }
445
446     /* Terminate the argument list with a NULL pointer */
447     CmdAddArg (&LD65, 0);
448
449     /* Call the linker */
450     ExecProgram (&LD65);
451 }
452
453
454
455 static void Assemble (const char* File)
456 /* Assemble the given file */
457 {
458     /* Remember the current assembler argument count */
459     unsigned ArgCount = CA65.ArgCount;
460
461     /* Set the target system */
462     CmdSetTarget (&CA65, Target);
463
464     /* If we won't link, this is the final step. In this case, set the
465      * output name.
466      */
467     if (DontLink && OutputName) {
468         CmdSetOutput (&CA65, OutputName);
469     } else {
470         /* The object file name will be the name of the source file
471          * with .s replaced by ".o". Add this file to the list of
472          * linker files.
473          */
474         char* ObjName = MakeFilename (File, ".o");
475         CmdAddFile (&LD65, ObjName);
476         xfree (ObjName);
477     }
478
479     /* Add the file as argument for the assembler */
480     CmdAddArg (&CA65, File);
481
482     /* Add a NULL pointer to terminate the argument list */
483     CmdAddArg (&CA65, 0);
484
485     /* Run the assembler */
486     ExecProgram (&CA65);
487
488     /* Remove the excess arguments */
489     CmdDelArgs (&CA65, ArgCount);
490 }
491
492
493
494 static void Compile (const char* File)
495 /* Compile the given file */
496 {
497     char* AsmName = 0;
498
499     /* Remember the current assembler argument count */
500     unsigned ArgCount = CC65.ArgCount;
501
502     /* Set the target system */
503     CmdSetTarget (&CC65, Target);
504
505     /* If we won't link, this is the final step. In this case, set the
506      * output name.
507      */
508     if (DontAssemble && OutputName) {
509         CmdSetOutput (&CC65, OutputName);
510     } else {
511         /* The assembler file name will be the name of the source file
512          * with .c replaced by ".s".
513          */
514         AsmName = MakeFilename (File, ".s");
515     }
516
517     /* Add the file as argument for the compiler */
518     CmdAddArg (&CC65, File);
519
520     /* Add a NULL pointer to terminate the argument list */
521     CmdAddArg (&CC65, 0);
522
523     /* Run the compiler */
524     ExecProgram (&CC65);
525
526     /* Remove the excess arguments */
527     CmdDelArgs (&CC65, ArgCount);
528
529     /* If this is not the final step, assemble the generated file, then
530      * remove it
531      */
532     if (!DontAssemble) {
533         Assemble (AsmName);
534         if (remove (AsmName) < 0) {
535             Warning ("Cannot remove temporary file `%s': %s",
536                      AsmName, strerror (errno));
537         }
538         xfree (AsmName);
539     }
540 }
541
542
543
544 static void CompileRes (const char* File)
545 /* Compile the given geos resource file */
546 {
547     char* AsmName = 0;
548
549     /* Remember the current assembler argument count */
550     unsigned ArgCount = GRC.ArgCount;
551
552     /* The assembler file name will be the name of the source file
553      * with .grc replaced by ".s".
554      */
555     AsmName = MakeFilename (File, ".s");
556
557     /* Add the file as argument for the resource compiler */
558     CmdAddArg (&GRC, File);
559
560     /* Add a NULL pointer to terminate the argument list */
561     CmdAddArg (&GRC, 0);
562
563     /* Run the compiler */
564     ExecProgram (&GRC);
565
566     /* Remove the excess arguments */
567     CmdDelArgs (&GRC, ArgCount);
568
569     /* If this is not the final step, assemble the generated file, then
570      * remove it
571      */
572     if (!DontAssemble) {
573         Assemble (AsmName);
574         if (remove (AsmName) < 0) {
575             Warning ("Cannot remove temporary file `%s': %s",
576                      AsmName, strerror (errno));
577         }
578     }
579
580     /* Free the assembler file name which was allocated from the heap */
581     xfree (AsmName);
582 }
583
584
585
586 /*****************************************************************************/
587 /*                                   Code                                    */
588 /*****************************************************************************/
589
590
591
592 static void Usage (void)
593 /* Print usage information and exit */
594 {
595     fprintf (stderr,
596              "Usage: %s [options] file [...]\n"
597              "Short options:\n"
598              "  -c\t\t\tCompile and assemble but don't link\n"
599              "  -d\t\t\tDebug mode\n"
600              "  -g\t\t\tAdd debug info\n"
601              "  -h\t\t\tHelp (this text)\n"
602              "  -l\t\t\tCreate an assembler listing\n"
603              "  -m name\t\tCreate a map file\n"
604              "  -o name\t\tName the output file\n"
605              "  -r\t\t\tEnable register variables\n"
606              "  -t sys\t\tSet the target system\n"
607              "  -v\t\t\tVerbose mode\n"
608              "  -vm\t\t\tVerbose map file\n"
609              "  -A\t\t\tStrict ANSI mode\n"
610              "  -C name\t\tUse linker config file\n"
611              "  -Cl\t\t\tMake local variables static\n"
612              "  -D sym[=defn]\t\tDefine a preprocessor symbol\n"
613              "  -I dir\t\tSet a compiler include directory path\n"
614              "  -Ln name\t\tCreate a VICE label file\n"
615              "  -O\t\t\tOptimize code\n"
616              "  -Oi\t\t\tOptimize code, inline functions\n"
617              "  -Or\t\t\tOptimize code, honour the register keyword\n"
618              "  -Os\t\t\tOptimize code, inline known C funtions\n"
619              "  -S\t\t\tCompile but don't assemble and link\n"
620              "  -T\t\t\tInclude source as comment\n"
621              "  -V\t\t\tPrint the version number\n"
622              "  -W\t\t\tSuppress warnings\n"
623              "\n"
624              "Long options:\n"
625              "  --add-source\t\tInclude source as comment\n"
626              "  --ansi\t\tStrict ANSI mode\n"
627              "  --asm-include-dir dir\tSet an assembler include directory\n"
628              "  --bss-name seg\tSet the name of the BSS segment\n"
629              "  --check-stack\t\tGenerate stack overflow checks\n"
630              "  --code-name seg\tSet the name of the CODE segment\n"
631              "  --codesize x\t\tAccept larger code by factor x\n"
632              "  --cpu type\t\tSet cpu type\n"
633              "  --create-dep\t\tCreate a make dependency file\n"
634              "  --data-name seg\tSet the name of the DATA segment\n"
635              "  --debug\t\tDebug mode\n"
636              "  --debug-info\t\tAdd debug info\n"
637              "  --feature name\tSet an emulation feature\n"
638              "  --help\t\tHelp (this text)\n"
639              "  --include-dir dir\tSet a compiler include directory path\n"
640              "  --listing\t\tCreate an assembler listing\n"
641              "  --mapfile name\tCreate a map file\n"
642              "  --module\t\tLink as a module\n"
643              "  --module-id id\tSpecify a module id for the linker\n"
644              "  --register-space b\tSet space available for register variables\n"
645              "  --register-vars\tEnable register variables\n"
646              "  --rodata-name seg\tSet the name of the RODATA segment\n"
647              "  --signed-chars\tDefault characters are signed\n"
648              "  --start-addr addr\tSet the default start address\n"
649              "  --static-locals\tMake local variables static\n"
650              "  --target sys\t\tSet the target system\n"
651              "  --version\t\tPrint the version number\n"
652              "  --verbose\t\tVerbose mode\n",
653              ProgName);
654 }
655
656
657
658 static void OptAddSource (const char* Opt attribute ((unused)),
659                           const char* Arg attribute ((unused)))
660 /* Strict source code as comments to the generated asm code */
661 {
662     CmdAddArg (&CC65, "-T");
663 }
664
665
666
667 static void OptAnsi (const char* Opt attribute ((unused)),
668                      const char* Arg attribute ((unused)))
669 /* Strict ANSI mode (compiler) */
670 {
671     CmdAddArg (&CC65, "-A");
672 }
673
674
675
676 static void OptAsmIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
677 /* Include directory (assembler) */
678 {
679     CmdAddArg (&CA65, "-I");
680     CmdAddArg (&CA65, Arg);
681 }
682
683
684
685 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
686 /* Handle the --bss-name option */
687 {
688     CmdAddArg (&CC65, "--bss-name");
689     CmdAddArg (&CC65, Arg);
690 }
691
692
693
694 static void OptCheckStack (const char* Opt attribute ((unused)),
695                            const char* Arg attribute ((unused)))
696 /* Handle the --check-stack option */
697 {
698     CmdAddArg (&CC65, "--check-stack");
699 }
700
701
702
703 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
704 /* Handle the --code-name option */
705 {
706     CmdAddArg (&CC65, "--code-name");
707     CmdAddArg (&CC65, Arg);
708 }
709
710
711
712 static void OptCodeSize (const char* Opt attribute ((unused)), const char* Arg)
713 /* Handle the --codesize option */
714 {
715     CmdAddArg (&CC65, "--codesize");
716     CmdAddArg (&CC65, Arg);
717 }
718
719
720
721 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
722 /* Handle the --cpu option */
723 {
724     /* Add the cpu type to the assembler and compiler */
725     CmdAddArg (&CA65, "--cpu");
726     CmdAddArg (&CA65, Arg);
727     CmdAddArg (&CC65, "--cpu");
728     CmdAddArg (&CC65, Arg);
729 }
730
731
732
733 static void OptCreateDep (const char* Opt attribute ((unused)),
734                           const char* Arg attribute ((unused)))
735 /* Handle the --create-dep option */
736 {
737     CmdAddArg (&CC65, "--create-dep");
738 }
739
740
741
742 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
743 /* Handle the --data-name option */
744 {
745     CmdAddArg (&CC65, "--data-name");
746     CmdAddArg (&CC65, Arg);
747 }
748
749
750
751 static void OptDebug (const char* Opt attribute ((unused)),
752                       const char* Arg attribute ((unused)))
753 /* Debug mode (compiler and cl65 utility) */
754 {
755     CmdAddArg (&CC65, "-d");
756     Debug = 1;
757 }
758
759
760
761 static void OptDebugInfo (const char* Opt attribute ((unused)),
762                           const char* Arg attribute ((unused)))
763 /* Debug Info - add to compiler and assembler */
764 {
765     CmdAddArg (&CC65, "-g");
766     CmdAddArg (&CA65, "-g");
767 }
768
769
770
771 static void OptFeature (const char* Opt attribute ((unused)), const char* Arg)
772 /* Emulation features for the assembler */
773 {
774     CmdAddArg (&CA65, "--feature");
775     CmdAddArg (&CA65, Arg);
776 }
777
778
779
780 static void OptHelp (const char* Opt attribute ((unused)),
781                      const char* Arg attribute ((unused)))
782 /* Print help - cl65 */
783 {
784     Usage ();
785     exit (EXIT_SUCCESS);
786 }
787
788
789
790 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
791 /* Include directory (compiler) */
792 {
793     CmdAddArg (&CC65, "-I");
794     CmdAddArg (&CC65, Arg);
795 }
796
797
798
799 static void OptListing (const char* Opt attribute ((unused)),
800                         const char* Arg attribute ((unused)))
801 /* Create an assembler listing */
802 {
803     CmdAddArg (&CA65, "-l");
804 }
805
806
807
808 static void OptMapFile (const char* Opt attribute ((unused)), const char* Arg)
809 /* Create a map file */
810 {
811     /* Create a map file (linker) */
812     CmdAddArg (&LD65, "-m");
813     CmdAddArg (&LD65, Arg);
814 }
815
816
817
818 static void OptModule (const char* Opt attribute ((unused)),
819                        const char* Arg attribute ((unused)))
820 /* Link as a module */
821 {
822     Module = 1;
823 }
824
825
826
827 static void OptModuleId (const char* Opt attribute ((unused)), const char* Arg)
828 /* Specify a module if for the linker */
829 {
830     /* Pass it straight to the linker */
831     CmdAddArg (&LD65, "--module-id");
832     CmdAddArg (&LD65, Arg);
833 }
834
835
836
837 static void OptRegisterSpace (const char* Opt attribute ((unused)), const char* Arg)
838 /* Handle the --register-space option */
839 {
840     CmdAddArg (&CC65, "--register-space");
841     CmdAddArg (&CC65, Arg);
842 }
843
844
845
846 static void OptRegisterVars (const char* Opt attribute ((unused)),
847                              const char* Arg attribute ((unused)))
848 /* Handle the --register-vars option */
849 {
850     CmdAddArg (&CC65, "-r");
851 }
852
853
854
855 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
856 /* Handle the --rodata-name option */
857 {
858     CmdAddArg (&CC65, "--rodata-name");
859     CmdAddArg (&CC65, Arg);
860 }
861
862
863
864 static void OptSignedChars (const char* Opt attribute ((unused)),
865                             const char* Arg attribute ((unused)))
866 /* Make default characters signed */
867 {
868     CmdAddArg (&CC65, "-j");
869 }
870
871
872
873 static void OptStartAddr (const char* Opt attribute ((unused)), const char* Arg)
874 /* Set the default start address */
875 {
876     CmdAddArg (&LD65, "-S");
877     CmdAddArg (&LD65, Arg);
878 }
879
880
881
882 static void OptStaticLocals (const char* Opt attribute ((unused)),
883                              const char* Arg attribute ((unused)))
884 /* Place local variables in static storage */
885 {
886     CmdAddArg (&CC65, "-Cl");
887 }
888
889
890
891 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
892 /* Set the target system */
893 {
894     Target = FindTarget (Arg);
895     if (Target == TGT_UNKNOWN) {
896         Error ("No such target system: `%s'", Arg);
897     } else if (Target == TGT_MODULE) {
898         Error ("Cannot use `module' as target, use --module instead");
899     }
900 }
901
902
903
904 static void OptVerbose (const char* Opt attribute ((unused)),
905                         const char* Arg attribute ((unused)))
906 /* Verbose mode (compiler, assembler, linker) */
907 {
908     CmdAddArg (&CC65, "-v");
909     CmdAddArg (&CA65, "-v");
910     CmdAddArg (&LD65, "-v");
911 }
912
913
914
915 static void OptVersion (const char* Opt attribute ((unused)),
916                         const char* Arg attribute ((unused)))
917 /* Print version number */
918 {
919     fprintf (stderr,
920              "cl65 V%u.%u.%u - (C) Copyright 1998-2002 Ullrich von Bassewitz\n",
921              VER_MAJOR, VER_MINOR, VER_PATCH);
922 }
923
924
925
926 int main (int argc, char* argv [])
927 /* Utility main program */
928 {
929     /* Program long options */
930     static const LongOpt OptTab[] = {
931         { "--add-source",       0,      OptAddSource            },
932         { "--ansi",             0,      OptAnsi                 },
933         { "--asm-include-dir",  1,      OptAsmIncludeDir        },
934         { "--bss-name",         1,      OptBssName              },
935         { "--check-stack",      0,      OptCheckStack           },
936         { "--code-name",        1,      OptCodeName             },
937         { "--codesize",         1,      OptCodeSize             },
938         { "--cpu",              1,      OptCPU                  },
939         { "--create-dep",       0,      OptCreateDep            },
940         { "--data-name",        1,      OptDataName             },
941         { "--debug",            0,      OptDebug                },
942         { "--debug-info",       0,      OptDebugInfo            },
943         { "--feature",          1,      OptFeature              },
944         { "--help",             0,      OptHelp                 },
945         { "--include-dir",      1,      OptIncludeDir           },
946         { "--listing",          0,      OptListing              },
947         { "--mapfile",          1,      OptMapFile              },
948         { "--module",           0,      OptModule               },
949         { "--module-id",        1,      OptModuleId             },
950         { "--register-space",   1,      OptRegisterSpace        },
951         { "--register-vars",    0,      OptRegisterVars         },
952         { "--rodata-name",      1,      OptRodataName           },
953         { "--signed-chars",     0,      OptSignedChars          },
954         { "--start-addr",       1,      OptStartAddr            },
955         { "--static-locals",    0,      OptStaticLocals         },
956         { "--target",           1,      OptTarget               },
957         { "--verbose",          0,      OptVerbose              },
958         { "--version",          0,      OptVersion              },
959     };
960
961     unsigned I;
962
963     /* Initialize the cmdline module */
964     InitCmdLine (&argc, &argv, "cl65");
965
966     /* Initialize the command descriptors */
967     CmdInit (&CC65, "cc65");
968     CmdInit (&CA65, "ca65");
969     CmdInit (&LD65, "ld65");
970     CmdInit (&GRC,  "grc");
971
972     /* Our default target is the C64 instead of "none" */
973     Target = TGT_C64;
974
975     /* Check the parameters */
976     I = 1;
977     while (I < ArgCount) {
978
979         /* Get the argument */
980         const char* Arg = ArgVec[I];
981
982         /* Check for an option */
983         if (Arg [0] == '-') {
984
985             switch (Arg [1]) {
986
987                 case '-':
988                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
989                     break;
990
991                 case 'A':
992                     /* Strict ANSI mode (compiler) */
993                     OptAnsi (Arg, 0);
994                     break;
995
996                 case 'C':
997                     if (Arg[2] == 'l' && Arg[3] == '\0') {
998                         /* Make local variables static */
999                         OptStaticLocals (Arg, 0);
1000                     } else {
1001                         /* Specify linker config file */
1002                         LinkerConfig = GetArg (&I, 2);
1003                     }
1004                     break;
1005
1006                 case 'D':
1007                     /* Define a preprocessor symbol (compiler) */
1008                     CmdAddArg (&CC65, "-D");
1009                     CmdAddArg (&CC65, GetArg (&I, 2));
1010                     break;
1011
1012                 case 'I':
1013                     /* Include directory (compiler) */
1014                     OptIncludeDir (Arg, GetArg (&I, 2));
1015                     break;
1016
1017                 case 'L':
1018                     if (Arg[2] == 'n') {
1019                         /* VICE label file (linker) */
1020                         CmdAddArg (&LD65, "-Ln");
1021                         CmdAddArg (&LD65, GetArg (&I, 3));
1022                     } else {
1023                         UnknownOption (Arg);
1024                     }
1025                     break;
1026
1027                 case 'O':
1028                     /* Optimize code (compiler, also covers -Oi and others) */
1029                     CmdAddArg (&CC65, Arg);
1030                     break;
1031
1032                 case 'S':
1033                     /* Dont assemble and link the created files */
1034                     DontLink = DontAssemble = 1;
1035                     break;
1036
1037                 case 'T':
1038                     /* Include source as comment (compiler) */
1039                     OptAddSource (Arg, 0);
1040                     break;
1041
1042                 case 'V':
1043                     /* Print version number */
1044                     OptVersion (Arg, 0);
1045                     break;
1046
1047                 case 'W':
1048                     /* Suppress warnings - compiler and assembler */
1049                     CmdAddArg (&CC65, "-W");
1050                     CmdAddArg (&CA65, "-W");
1051                     CmdAddArg (&CA65, "0");
1052                     break;
1053
1054                 case 'c':
1055                     /* Don't link the resulting files */
1056                     DontLink = 1;
1057                     break;
1058
1059                 case 'd':
1060                     /* Debug mode (compiler) */
1061                     OptDebug (Arg, 0);
1062                     break;
1063
1064                 case 'g':
1065                     /* Debugging - add to compiler and assembler */
1066                     OptDebugInfo (Arg, 0);
1067                     break;
1068
1069                 case 'h':
1070                 case '?':
1071                     /* Print help - cl65 */
1072                     OptHelp (Arg, 0);
1073                     break;
1074
1075                 case 'j':
1076                     /* Default characters are signed */
1077                     OptSignedChars (Arg, 0);
1078                     break;
1079
1080                 case 'l':
1081                     /* Create an assembler listing */
1082                     OptListing (Arg, 0);
1083                     break;
1084
1085                 case 'm':
1086                     /* Create a map file (linker) */
1087                     OptMapFile (Arg, GetArg (&I, 2));
1088                     break;
1089
1090                 case 'o':
1091                     /* Name the output file */
1092                     OutputName = GetArg (&I, 2);
1093                     break;
1094
1095                 case 'r':
1096                     /* Enable register variables */
1097                     OptRegisterVars (Arg, 0);
1098                     break;
1099
1100                 case 't':
1101                     /* Set target system - compiler, assembler and linker */
1102                     OptTarget (Arg, GetArg (&I, 2));
1103                     break;
1104
1105                 case 'v':
1106                     if (Arg [2] == 'm') {
1107                         /* Verbose map file (linker) */
1108                         CmdAddArg (&LD65, "-vm");
1109                     } else {
1110                         /* Verbose mode (compiler, assembler, linker) */
1111                         OptVerbose (Arg, 0);
1112                     }
1113                     break;
1114
1115                 default:
1116                     UnknownOption (Arg);
1117             }
1118         } else {
1119
1120             /* Remember the first file name */
1121             if (FirstInput == 0) {
1122                 FirstInput = Arg;
1123             }
1124
1125             /* Determine the file type by the extension */
1126             switch (GetFileType (Arg)) {
1127
1128                 case FILETYPE_C:
1129                     /* Compile the file */
1130                     Compile (Arg);
1131                     break;
1132
1133                 case FILETYPE_ASM:
1134                     /* Assemble the file */
1135                     if (!DontAssemble) {
1136                         Assemble (Arg);
1137                     }
1138                     break;
1139
1140                 case FILETYPE_OBJ:
1141                 case FILETYPE_LIB:
1142                     /* Add to the linker files */
1143                     CmdAddFile (&LD65, Arg);
1144                     break;
1145
1146                 case FILETYPE_GR:
1147                     /* Add to the resource compiler files */
1148                     CompileRes (Arg);
1149                     break;
1150
1151                 default:
1152                     Error ("Don't know what to do with `%s'", Arg);
1153
1154             }
1155
1156         }
1157
1158         /* Next argument */
1159         ++I;
1160     }
1161
1162     /* Check if we had any input files */
1163     if (FirstInput == 0) {
1164         Warning ("No input files");
1165     }
1166
1167     /* Link the given files if requested and if we have any */
1168     if (DontLink == 0 && LD65.FileCount > 0) {
1169         Link ();
1170     }
1171
1172     /* Return an apropriate exit code */
1173     return EXIT_SUCCESS;
1174 }
1175
1176
1177
1178