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