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