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