]> git.sur5r.net Git - cc65/blob - src/cl65/main.c
New --config directive passed to the linker
[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-define sym[=v]\tDefine an assembler symbol\n"
624              "  --asm-include-dir dir\tSet an assembler include directory\n"
625              "  --bss-label name\tDefine and export a BSS segment label\n"
626              "  --bss-name seg\tSet the name of the BSS segment\n"
627              "  --cfg-path path\tSpecify a config file search path\n"
628              "  --check-stack\t\tGenerate stack overflow checks\n"
629              "  --code-label name\tDefine and export a CODE segment label\n"
630              "  --code-name seg\tSet the name of the CODE segment\n"
631              "  --codesize x\t\tAccept larger code by factor x\n"
632              "  --config name\t\tUse linker config file\n"
633              "  --cpu type\t\tSet cpu type\n"
634              "  --create-dep\t\tCreate a make dependency file\n"
635              "  --data-label name\tDefine and export a DATA segment label\n"
636              "  --data-name seg\tSet the name of the DATA segment\n"
637              "  --debug\t\tDebug mode\n"
638              "  --debug-info\t\tAdd debug info\n"
639              "  --feature name\tSet an emulation feature\n"
640              "  --forget-inc-paths\tForget include search paths (compiler)\n"
641              "  --help\t\tHelp (this text)\n"
642              "  --include-dir dir\tSet a compiler include directory path\n"
643              "  --lib file\t\tLink this library\n"
644              "  --lib-path path\tSpecify a library search path\n"
645              "  --listing\t\tCreate an assembler listing\n"
646              "  --mapfile name\tCreate a map file\n"
647              "  --module\t\tLink as a module\n"
648              "  --module-id id\tSpecify a module id for the linker\n"
649              "  --o65-model model\tOverride the o65 model\n"
650              "  --obj file\t\tLink this object file\n"
651              "  --obj-path path\tSpecify an object file search path\n"
652              "  --register-space b\tSet space available for register variables\n"
653              "  --register-vars\tEnable register variables\n"
654              "  --rodata-name seg\tSet the name of the RODATA segment\n"
655              "  --signed-chars\tDefault characters are signed\n"
656              "  --start-addr addr\tSet the default start address\n"
657              "  --static-locals\tMake local variables static\n"
658              "  --target sys\t\tSet the target system\n"
659              "  --version\t\tPrint the version number\n"
660              "  --verbose\t\tVerbose mode\n"
661              "  --zeropage-label name\tDefine and export a ZEROPAGE segment label\n"
662              "  --zeropage-name seg\tSet the name of the ZEROPAGE segment\n",
663              ProgName);
664 }
665
666
667
668 static void OptAddSource (const char* Opt attribute ((unused)),
669                           const char* Arg attribute ((unused)))
670 /* Strict source code as comments to the generated asm code */
671 {
672     CmdAddArg (&CC65, "-T");
673 }
674
675
676
677 static void OptAnsi (const char* Opt attribute ((unused)),
678                      const char* Arg attribute ((unused)))
679 /* Strict ANSI mode (compiler) */
680 {
681     CmdAddArg (&CC65, "-A");
682 }
683
684
685
686 static void OptAsmDefine (const char* Opt attribute ((unused)), const char* Arg)
687 /* Define an assembler symbol (assembler) */
688 {
689     CmdAddArg2 (&CA65, "-D", Arg);
690 }
691
692
693
694 static void OptAsmIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
695 /* Include directory (assembler) */
696 {
697     CmdAddArg2 (&CA65, "-I", Arg);
698 }
699
700
701
702 static void OptBssLabel (const char* Opt attribute ((unused)), const char* Arg)
703 /* Handle the --bss-label option */
704 {
705     CmdAddArg2 (&CO65, "--bss-label", Arg);
706 }
707
708
709
710 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
711 /* Handle the --bss-name option */
712 {
713     CmdAddArg2 (&CC65, "--bss-name", Arg);
714     CmdAddArg2 (&CO65, "--bss-name", Arg);
715 }
716
717
718
719 static void OptCfgPath (const char* Opt attribute ((unused)), const char* Arg)
720 /* Config file search path (linker) */
721 {
722     CmdAddArg2 (&LD65, "--cfg-path", Arg);
723 }
724
725
726
727 static void OptCheckStack (const char* Opt attribute ((unused)),
728                            const char* Arg attribute ((unused)))
729 /* Handle the --check-stack option */
730 {
731     CmdAddArg (&CC65, "--check-stack");
732 }
733
734
735
736 static void OptCodeLabel (const char* Opt attribute ((unused)), const char* Arg)
737 /* Handle the --code-label option */
738 {
739     CmdAddArg2 (&CO65, "--code-label", Arg);
740 }
741
742
743
744 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
745 /* Handle the --code-name option */
746 {
747     CmdAddArg2 (&CC65, "--code-name", Arg);
748     CmdAddArg2 (&CO65, "--code-name", Arg);
749 }
750
751
752
753 static void OptCodeSize (const char* Opt attribute ((unused)), const char* Arg)
754 /* Handle the --codesize option */
755 {
756     CmdAddArg2 (&CC65, "--codesize", Arg);
757 }
758
759
760
761 static void OptConfig (const char* Opt attribute ((unused)), const char* Arg)
762 /* Config file (linker) */
763 {
764     if (LinkerConfig) {
765         Error ("Cannot specify -C/--config twice");
766     }
767     LinkerConfig = Arg;
768 }
769
770
771
772 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
773 /* Handle the --cpu option */
774 {
775     /* Add the cpu type to the assembler and compiler */
776     CmdAddArg2 (&CA65, "--cpu", Arg);
777     CmdAddArg2 (&CC65, "--cpu", Arg);
778 }
779
780
781
782 static void OptCreateDep (const char* Opt attribute ((unused)),
783                           const char* Arg attribute ((unused)))
784 /* Handle the --create-dep option */
785 {
786     CmdAddArg (&CC65, "--create-dep");
787 }
788
789
790
791 static void OptDataLabel (const char* Opt attribute ((unused)), const char* Arg)
792 /* Handle the --data-label option */
793 {
794     CmdAddArg2 (&CO65, "--data-label", Arg);
795 }
796
797
798
799 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
800 /* Handle the --data-name option */
801 {
802     CmdAddArg2 (&CC65, "--data-name", Arg);
803     CmdAddArg2 (&CO65, "--data-name", Arg);
804 }
805
806
807
808 static void OptDebug (const char* Opt attribute ((unused)),
809                       const char* Arg attribute ((unused)))
810 /* Debug mode (compiler and cl65 utility) */
811 {
812     CmdAddArg (&CC65, "-d");
813     CmdAddArg (&CO65, "-d");
814     Debug = 1;
815 }
816
817
818
819 static void OptDebugInfo (const char* Opt attribute ((unused)),
820                           const char* Arg attribute ((unused)))
821 /* Debug Info - add to compiler and assembler */
822 {
823     CmdAddArg (&CC65, "-g");
824     CmdAddArg (&CA65, "-g");
825     CmdAddArg (&CO65, "-g");
826 }
827
828
829
830 static void OptFeature (const char* Opt attribute ((unused)), const char* Arg)
831 /* Emulation features for the assembler */
832 {
833     CmdAddArg2 (&CA65, "--feature", Arg);
834 }
835
836
837
838 static void OptForgetIncPaths (const char* Opt attribute ((unused)), const char* Arg)
839 /* Forget all currently defined include paths */
840 {
841     CmdAddArg2 (&CC65, "--forget-inc-paths", Arg);
842 }
843
844
845
846 static void OptHelp (const char* Opt attribute ((unused)),
847                      const char* Arg attribute ((unused)))
848 /* Print help - cl65 */
849 {
850     Usage ();
851     exit (EXIT_SUCCESS);
852 }
853
854
855
856 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
857 /* Include directory (compiler) */
858 {
859     CmdAddArg2 (&CC65, "-I", Arg);
860 }
861
862
863
864 static void OptLib (const char* Opt attribute ((unused)), const char* Arg)
865 /* Library file follows (linker) */
866 {
867     CmdAddArg2 (&LD65, "--lib", Arg);
868 }
869
870
871
872 static void OptLibPath (const char* Opt attribute ((unused)), const char* Arg)
873 /* Library search path (linker) */
874 {
875     CmdAddArg2 (&LD65, "--lib-path", Arg);
876 }
877
878
879
880 static void OptListing (const char* Opt attribute ((unused)),
881                         const char* Arg attribute ((unused)))
882 /* Create an assembler listing */
883 {
884     CmdAddArg (&CA65, "-l");
885 }
886
887
888
889 static void OptMapFile (const char* Opt attribute ((unused)), const char* Arg)
890 /* Create a map file */
891 {
892     /* Create a map file (linker) */
893     CmdAddArg2 (&LD65, "-m", Arg);
894 }
895
896
897
898 static void OptModule (const char* Opt attribute ((unused)),
899                        const char* Arg attribute ((unused)))
900 /* Link as a module */
901 {
902     Module = 1;
903 }
904
905
906
907 static void OptModuleId (const char* Opt attribute ((unused)), const char* Arg)
908 /* Specify a module if for the linker */
909 {
910     /* Pass it straight to the linker */
911     CmdAddArg2 (&LD65, "--module-id", Arg);
912 }
913
914
915
916 static void OptO65Model (const char* Opt attribute ((unused)), const char* Arg)
917 /* Handle the --o65-model option */
918 {
919     CmdAddArg2 (&CO65, "-m", Arg);
920 }
921
922
923
924 static void OptObj (const char* Opt attribute ((unused)), const char* Arg)
925 /* Object file follows (linker) */
926 {
927     CmdAddArg2 (&LD65, "--obj", Arg);
928 }
929
930
931
932 static void OptObjPath (const char* Opt attribute ((unused)), const char* Arg)
933 /* Object file search path (linker) */
934 {
935     CmdAddArg2 (&LD65, "--obj-path", Arg);
936 }
937
938
939
940 static void OptRegisterSpace (const char* Opt attribute ((unused)), const char* Arg)
941 /* Handle the --register-space option */
942 {
943     CmdAddArg2 (&CC65, "--register-space", Arg);
944 }
945
946
947
948 static void OptRegisterVars (const char* Opt attribute ((unused)),
949                              const char* Arg attribute ((unused)))
950 /* Handle the --register-vars option */
951 {
952     CmdAddArg (&CC65, "-r");
953 }
954
955
956
957 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
958 /* Handle the --rodata-name option */
959 {
960     CmdAddArg2 (&CC65, "--rodata-name", Arg);
961 }
962
963
964
965 static void OptSignedChars (const char* Opt attribute ((unused)),
966                             const char* Arg attribute ((unused)))
967 /* Make default characters signed */
968 {
969     CmdAddArg (&CC65, "-j");
970 }
971
972
973
974 static void OptStartAddr (const char* Opt attribute ((unused)), const char* Arg)
975 /* Set the default start address */
976 {
977     CmdAddArg2 (&LD65, "-S", Arg);
978 }
979
980
981
982 static void OptStaticLocals (const char* Opt attribute ((unused)),
983                              const char* Arg attribute ((unused)))
984 /* Place local variables in static storage */
985 {
986     CmdAddArg (&CC65, "-Cl");
987 }
988
989
990
991 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
992 /* Set the target system */
993 {
994     Target = FindTarget (Arg);
995     if (Target == TGT_UNKNOWN) {
996         Error ("No such target system: `%s'", Arg);
997     } else if (Target == TGT_MODULE) {
998         Error ("Cannot use `module' as target, use --module instead");
999     }
1000 }
1001
1002
1003
1004 static void OptVerbose (const char* Opt attribute ((unused)),
1005                         const char* Arg attribute ((unused)))
1006 /* Verbose mode (compiler, assembler, linker) */
1007 {
1008     CmdAddArg (&CC65, "-v");
1009     CmdAddArg (&CA65, "-v");
1010     CmdAddArg (&CO65, "-v");
1011     CmdAddArg (&LD65, "-v");
1012 }
1013
1014
1015
1016 static void OptVersion (const char* Opt attribute ((unused)),
1017                         const char* Arg attribute ((unused)))
1018 /* Print version number */
1019 {
1020     fprintf (stderr,
1021              "cl65 V%u.%u.%u - (C) Copyright 1998-2003 Ullrich von Bassewitz\n",
1022              VER_MAJOR, VER_MINOR, VER_PATCH);
1023 }
1024
1025         
1026
1027 static void OptZeropageLabel (const char* Opt attribute ((unused)), const char* Arg)
1028 /* Handle the --zeropage-label option */
1029 {
1030     CmdAddArg2 (&CO65, "--zeropage-label", Arg);
1031 }
1032
1033
1034
1035 static void OptZeropageName (const char* Opt attribute ((unused)), const char* Arg)
1036 /* Handle the --zeropage-name option */
1037 {
1038     CmdAddArg2 (&CO65, "--zeropage-name", Arg);
1039 }
1040
1041
1042
1043 int main (int argc, char* argv [])
1044 /* Utility main program */
1045 {
1046     /* Program long options */
1047     static const LongOpt OptTab[] = {
1048         { "--add-source",       0,      OptAddSource            },
1049         { "--ansi",             0,      OptAnsi                 },
1050         { "--asm-define",       1,      OptAsmDefine            },
1051         { "--asm-include-dir",  1,      OptAsmIncludeDir        },
1052         { "--bss-label",        1,      OptBssLabel             },
1053         { "--bss-name",         1,      OptBssName              },
1054         { "--cfg-path",         1,      OptCfgPath              },
1055         { "--check-stack",      0,      OptCheckStack           },
1056         { "--code-label",       1,      OptCodeLabel            },
1057         { "--code-name",        1,      OptCodeName             },
1058         { "--codesize",         1,      OptCodeSize             },
1059         { "--config",           1,      OptConfig               },
1060         { "--cpu",              1,      OptCPU                  },
1061         { "--create-dep",       0,      OptCreateDep            },
1062         { "--data-label",       1,      OptDataLabel            },
1063         { "--data-name",        1,      OptDataName             },
1064         { "--debug",            0,      OptDebug                },
1065         { "--debug-info",       0,      OptDebugInfo            },
1066         { "--feature",          1,      OptFeature              },
1067         { "--forget-inc-paths", 0,      OptForgetIncPaths       },
1068         { "--help",             0,      OptHelp                 },
1069         { "--include-dir",      1,      OptIncludeDir           },
1070         { "--lib",              1,      OptLib                  },
1071         { "--lib-path",         1,      OptLibPath              },
1072         { "--listing",          0,      OptListing              },
1073         { "--mapfile",          1,      OptMapFile              },
1074         { "--module",           0,      OptModule               },
1075         { "--module-id",        1,      OptModuleId             },
1076         { "--o65-model",        1,      OptO65Model             },
1077         { "--obj",              1,      OptObj                  },
1078         { "--obj-path",         1,      OptObjPath              },
1079         { "--register-space",   1,      OptRegisterSpace        },
1080         { "--register-vars",    0,      OptRegisterVars         },
1081         { "--rodata-name",      1,      OptRodataName           },
1082         { "--signed-chars",     0,      OptSignedChars          },
1083         { "--start-addr",       1,      OptStartAddr            },
1084         { "--static-locals",    0,      OptStaticLocals         },
1085         { "--target",           1,      OptTarget               },
1086         { "--verbose",          0,      OptVerbose              },
1087         { "--version",          0,      OptVersion              },
1088         { "--zeropage-label",   1,      OptZeropageLabel        },
1089         { "--zeropage-name",    1,      OptZeropageName         },
1090     };
1091
1092     unsigned I;
1093
1094     /* Initialize the cmdline module */
1095     InitCmdLine (&argc, &argv, "cl65");
1096
1097     /* Initialize the command descriptors */
1098     CmdInit (&CC65, "cc65");
1099     CmdInit (&CA65, "ca65");
1100     CmdInit (&CO65, "co65");
1101     CmdInit (&LD65, "ld65");
1102     CmdInit (&GRC,  "grc");
1103
1104     /* Our default target is the C64 instead of "none" */
1105     Target = TGT_C64;
1106
1107     /* Check the parameters */
1108     I = 1;
1109     while (I < ArgCount) {
1110
1111         /* Get the argument */
1112         const char* Arg = ArgVec[I];
1113
1114         /* Check for an option */
1115         if (Arg [0] == '-') {
1116
1117             switch (Arg [1]) {
1118
1119                 case '-':
1120                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
1121                     break;
1122
1123                 case 'A':
1124                     /* Strict ANSI mode (compiler) */
1125                     OptAnsi (Arg, 0);
1126                     break;
1127
1128                 case 'C':
1129                     if (Arg[2] == 'l' && Arg[3] == '\0') {
1130                         /* Make local variables static */
1131                         OptStaticLocals (Arg, 0);
1132                     } else {
1133                         /* Specify linker config file */
1134                         OptConfig (Arg, GetArg (&I, 2));
1135                     }
1136                     break;
1137
1138                 case 'D':
1139                     /* Define a preprocessor symbol (compiler) */
1140                     CmdAddArg2 (&CC65, "-D", GetArg (&I, 2));
1141                     break;
1142
1143                 case 'I':
1144                     /* Include directory (compiler) */
1145                     OptIncludeDir (Arg, GetArg (&I, 2));
1146                     break;
1147
1148                 case 'L':
1149                     if (Arg[2] == 'n' && Arg[3] == '\0') {
1150                         /* VICE label file (linker) */
1151                         CmdAddArg2 (&LD65, "-Ln", GetArg (&I, 3));
1152                     } else {
1153                         /* Library search path (linker) */
1154                         OptLibPath (Arg, GetArg (&I, 2));
1155                     }
1156                     break;
1157
1158                 case 'O':
1159                     /* Optimize code (compiler, also covers -Oi and others) */
1160                     CmdAddArg (&CC65, Arg);
1161                     break;
1162
1163                 case 'S':
1164                     /* Dont assemble and link the created files */
1165                     DontLink = DontAssemble = 1;
1166                     break;
1167
1168                 case 'T':
1169                     /* Include source as comment (compiler) */
1170                     OptAddSource (Arg, 0);
1171                     break;
1172
1173                 case 'V':
1174                     /* Print version number */
1175                     OptVersion (Arg, 0);
1176                     break;
1177
1178                 case 'W':
1179                     /* Suppress warnings - compiler and assembler */
1180                     CmdAddArg (&CC65, "-W");
1181                     CmdAddArg2 (&CA65, "-W", "0");
1182                     break;
1183
1184                 case 'c':
1185                     /* Don't link the resulting files */
1186                     DontLink = 1;
1187                     break;
1188
1189                 case 'd':
1190                     /* Debug mode (compiler) */
1191                     OptDebug (Arg, 0);
1192                     break;
1193
1194                 case 'g':
1195                     /* Debugging - add to compiler and assembler */
1196                     OptDebugInfo (Arg, 0);
1197                     break;
1198
1199                 case 'h':
1200                 case '?':
1201                     /* Print help - cl65 */
1202                     OptHelp (Arg, 0);
1203                     break;
1204
1205                 case 'j':
1206                     /* Default characters are signed */
1207                     OptSignedChars (Arg, 0);
1208                     break;
1209
1210                 case 'l':
1211                     /* Create an assembler listing */
1212                     OptListing (Arg, 0);
1213                     break;
1214
1215                 case 'm':
1216                     /* Create a map file (linker) */
1217                     OptMapFile (Arg, GetArg (&I, 2));
1218                     break;
1219
1220                 case 'o':
1221                     /* Name the output file */
1222                     OutputName = GetArg (&I, 2);
1223                     break;
1224
1225                 case 'r':
1226                     /* Enable register variables */
1227                     OptRegisterVars (Arg, 0);
1228                     break;
1229
1230                 case 't':
1231                     /* Set target system - compiler, assembler and linker */
1232                     OptTarget (Arg, GetArg (&I, 2));
1233                     break;
1234
1235                 case 'v':
1236                     if (Arg [2] == 'm') {
1237                         /* Verbose map file (linker) */
1238                         CmdAddArg (&LD65, "-vm");
1239                     } else {
1240                         /* Verbose mode (compiler, assembler, linker) */
1241                         OptVerbose (Arg, 0);
1242                     }
1243                     break;
1244
1245                 default:
1246                     UnknownOption (Arg);
1247             }
1248         } else {
1249
1250             /* Remember the first file name */
1251             if (FirstInput == 0) {
1252                 FirstInput = Arg;
1253             }
1254
1255             /* Determine the file type by the extension */
1256             switch (GetFileType (Arg)) {
1257
1258                 case FILETYPE_C:
1259                     /* Compile the file */
1260                     Compile (Arg);
1261                     break;
1262
1263                 case FILETYPE_ASM:
1264                     /* Assemble the file */
1265                     if (!DontAssemble) {
1266                         Assemble (Arg);
1267                     }
1268                     break;
1269
1270                 case FILETYPE_OBJ:
1271                 case FILETYPE_LIB:
1272                     /* Add to the linker files */
1273                     CmdAddFile (&LD65, Arg);
1274                     break;
1275
1276                 case FILETYPE_GR:
1277                     /* Add to the resource compiler files */
1278                     CompileRes (Arg);
1279                     break;
1280
1281                 case FILETYPE_O65:
1282                     /* Add the the object file converter files */
1283                     ConvertO65 (Arg);
1284                     break;
1285
1286                 default:
1287                     Error ("Don't know what to do with `%s'", Arg);
1288
1289             }
1290
1291         }
1292
1293         /* Next argument */
1294         ++I;
1295     }
1296
1297     /* Check if we had any input files */
1298     if (FirstInput == 0) {
1299         Warning ("No input files");
1300     }
1301
1302     /* Link the given files if requested and if we have any */
1303     if (DontLink == 0 && LD65.FileCount > 0) {
1304         Link ();
1305     }
1306
1307     /* Return an apropriate exit code */
1308     return EXIT_SUCCESS;
1309 }
1310
1311
1312