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