]> git.sur5r.net Git - cc65/blob - src/cl65/main.c
replaced tabs with spaces which accidently were introduced.
[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-2013, Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 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(_WIN32)
40 #  define HAVE_SPAWN 1
41 #else
42 #  define NEED_SPAWN 1
43 #endif
44
45 /* GCC strictly follows http://c-faq.com/ansi/constmismatch.html and issues an
46 ** 'incompatible pointer type' warning - that can't be suppressed via #pragma.
47 ** The spawnvp() prototype of MinGW (http://www.mingw.org/) differs from the
48 ** one of MinGW-w64 (http://mingw-w64.sourceforge.net/) regarding constness.
49 ** So there's no alternative to actually distinguish these environments :-(
50 */
51 #define SPAWN_ARGV_CONST_CAST
52 #if defined(__MINGW32__)
53 #  include <_mingw.h>
54 #  if !defined(__MINGW64_VERSION_MAJOR)
55 #    undef  SPAWN_ARGV_CONST_CAST
56 #    define SPAWN_ARGV_CONST_CAST (const char* const *)
57 #  endif
58 #endif
59
60
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <ctype.h>
65 #include <errno.h>
66 #if defined(HAVE_SPAWN)
67 #  include <process.h>
68 #endif
69
70 /* common */
71 #include "attrib.h"
72 #include "cmdline.h"
73 #include "filetype.h"
74 #include "fname.h"
75 #include "mmodel.h"
76 #include "searchpath.h"
77 #include "strbuf.h"
78 #include "target.h"
79 #include "version.h"
80 #include "xmalloc.h"
81
82 /* cl65 */
83 #include "global.h"
84 #include "error.h"
85
86
87
88 /*****************************************************************************/
89 /*                                   Data                                    */
90 /*****************************************************************************/
91
92
93
94 /* Struct that describes a command */
95 typedef struct CmdDesc CmdDesc;
96 struct CmdDesc {
97     char*       Name;           /* The command name */
98
99     unsigned    ArgCount;       /* Count of arguments */
100     unsigned    ArgMax;         /* Maximum count of arguments */
101     char**      Args;           /* The arguments */
102
103     unsigned    FileCount;      /* Count of files to translate */
104     unsigned    FileMax;        /* Maximum count of files */
105     char**      Files;          /* The files */
106 };
107
108 /* Command descriptors for the different programs */
109 static CmdDesc CC65 = { 0, 0, 0, 0, 0, 0, 0 };
110 static CmdDesc CA65 = { 0, 0, 0, 0, 0, 0, 0 };
111 static CmdDesc CO65 = { 0, 0, 0, 0, 0, 0, 0 };
112 static CmdDesc LD65 = { 0, 0, 0, 0, 0, 0, 0 };
113 static CmdDesc GRC  = { 0, 0, 0, 0, 0, 0, 0 };
114
115 /* Variables controlling the steps we're doing */
116 static int DoLink       = 1;
117 static int DoAssemble   = 1;
118
119 /* The name of the output file, NULL if none given */
120 static const char* OutputName = 0;
121
122 /* The name of the linker configuration file if given */
123 static const char* LinkerConfig = 0;
124
125 /* The name of the first input file. This will be used to construct the
126 ** executable file name if no explicit name is given.
127 */
128 static const char* FirstInput = 0;
129
130 /* The names of the files for dependency generation */
131 static const char* DepName = 0;
132 static const char* FullDepName = 0;
133
134 /* Remember if we should link a module */
135 static int Module = 0;
136
137 /* Extension used for a module */
138 #define MODULE_EXT      ".o65"
139
140 /* Name of the target specific runtime library */
141 static char* TargetLib  = 0;
142
143
144
145 /*****************************************************************************/
146 /*                Include the system specific spawn function                 */
147 /*****************************************************************************/
148
149
150
151 #if defined(NEED_SPAWN)
152 #  if defined(_AMIGA)
153 #    include "spawn-amiga.inc"
154 #  else
155 #    include "spawn-unix.inc"
156 #  endif
157 #endif
158
159 /*****************************************************************************/
160 /*                        Credential functions                               */
161 /*****************************************************************************/
162 void DisableAssembling(void){
163     DoAssemble = 0;
164 }
165
166 void DisableLinking(void){
167     DoLink = 0;
168 }
169
170 void DisableAssemblingAndLinking(void){
171     DisableAssembling();
172     DisableLinking();
173 }
174
175 /*****************************************************************************/
176 /*                        Command structure handling                         */
177 /*****************************************************************************/
178
179
180
181 static void CmdExpand (CmdDesc* Cmd)
182 /* Expand the argument vector */
183 {
184     unsigned NewMax  = Cmd->ArgMax + 10;
185     char**       NewArgs = xmalloc (NewMax * sizeof (char*));
186     memcpy (NewArgs, Cmd->Args, Cmd->ArgMax * sizeof (char*));
187     xfree (Cmd->Args);
188     Cmd->Args   = NewArgs;
189     Cmd->ArgMax = NewMax;
190 }
191
192
193
194 static void CmdAddArg (CmdDesc* Cmd, const char* Arg)
195 /* Add a new argument to the command */
196 {
197     /* Expand the argument vector if needed */
198     if (Cmd->ArgCount >= Cmd->ArgMax) {
199         CmdExpand (Cmd);
200     }
201
202     /* Add a copy of the new argument, allow a NULL pointer */
203     if (Arg) {
204         Cmd->Args[Cmd->ArgCount++] = xstrdup (Arg);
205     } else {
206         Cmd->Args[Cmd->ArgCount++] = 0;
207     }
208 }
209
210
211
212 static void CmdAddArg2 (CmdDesc* Cmd, const char* Arg1, const char* Arg2)
213 /* Add a new argument pair to the command */
214 {
215     CmdAddArg (Cmd, Arg1);
216     CmdAddArg (Cmd, Arg2);
217 }
218
219
220
221 static void CmdAddArgList (CmdDesc* Cmd, const char* ArgList)
222 /* Add a list of arguments separated by commas */
223 {
224     const char* Arg = ArgList;
225     const char* P   = Arg;
226
227     while (1) {
228         if (*P == '\0' || *P == ',') {
229
230             /* End of argument, add it */
231             unsigned Len = P - Arg;
232
233             /* Expand the argument vector if needed */
234             if (Cmd->ArgCount >= Cmd->ArgMax) {
235                 CmdExpand (Cmd);
236             }
237
238             /* Add the new argument */
239             Cmd->Args[Cmd->ArgCount] = memcpy (xmalloc (Len + 1), Arg, Len);
240             Cmd->Args[Cmd->ArgCount][Len] = '\0';
241             ++Cmd->ArgCount;
242
243             /* If the argument was terminated by a comma, skip it, otherwise
244             ** we're done.
245             */
246             if (*P == ',') {
247                 /* Start over at next char */
248                 Arg = ++P;
249             } else {
250                 break;
251             }
252         } else {
253             /* Skip other chars */
254             ++P;
255         }
256     }
257
258 }
259
260
261
262 static void CmdDelArgs (CmdDesc* Cmd, unsigned LastValid)
263 /* Remove all arguments with an index greater than LastValid */
264 {
265     while (Cmd->ArgCount > LastValid) {
266         Cmd->ArgCount--;
267         xfree (Cmd->Args [Cmd->ArgCount]);
268         Cmd->Args [Cmd->ArgCount] = 0;
269     }
270 }
271
272
273
274 static void CmdAddFile (CmdDesc* Cmd, const char* File)
275 /* Add a new file to the command */
276 {
277     /* Expand the file vector if needed */
278     if (Cmd->FileCount == Cmd->FileMax) {
279         unsigned NewMax   = Cmd->FileMax + 10;
280         char**   NewFiles = xmalloc (NewMax * sizeof (char*));
281         memcpy (NewFiles, Cmd->Files, Cmd->FileMax * sizeof (char*));
282         xfree (Cmd->Files);
283         Cmd->Files   = NewFiles;
284         Cmd->FileMax = NewMax;
285     }
286
287     /* If the file name is not NULL (which is legal and is used to terminate
288     ** the file list), check if the file name does already exist in the file
289     ** list and print a warning if so. Regardless of the search result, add
290     ** the file.
291     */
292     if (File) {
293         unsigned I;
294         for (I = 0; I < Cmd->FileCount; ++I) {
295             if (strcmp (Cmd->Files[I], File) == 0) {
296                 /* Duplicate file */
297                 Warning ("Duplicate file in argument list: `%s'", File);
298                 /* No need to search further */
299                 break;
300             }
301         }
302
303         /* Add the file */
304         Cmd->Files [Cmd->FileCount++] = xstrdup (File);
305     } else {
306         /* Add a NULL pointer */
307         Cmd->Files [Cmd->FileCount++] = 0;
308     }
309 }
310
311
312
313 static void CmdInit (CmdDesc* Cmd, const char* Path, const char* Name)
314 /* Initialize the command using the given path and name of the executable */
315 {
316     char* FullName;
317
318     FullName = (char*) xmalloc (strlen (Path) + strlen (Name) + 1);
319     strcpy (FullName, Path);
320     strcat (FullName, Name);
321
322     /* Remember the command */
323     Cmd->Name = xstrdup (FullName);
324
325     /* Use the command name as first argument */
326     CmdAddArg (Cmd, FullName);
327
328     xfree (FullName);
329 }
330
331
332
333 static void CmdSetOutput (CmdDesc* Cmd, const char* File)
334 /* Set the output file in a command desc */
335 {
336     CmdAddArg2 (Cmd, "-o", File);
337 }
338
339
340
341 static void CmdSetTarget (CmdDesc* Cmd, target_t Target)
342 /* Set the output file in a command desc */
343 {
344     CmdAddArg2 (Cmd, "-t", GetTargetName (Target));
345 }
346
347
348
349 static void CmdPrint (CmdDesc* Cmd, FILE* F)
350 /* Output the command line encoded in the command desc */
351 {
352     unsigned I;
353     for (I = 0; I < Cmd->ArgCount && Cmd->Args[I] != 0; ++I) {
354         fprintf (F, "%s ", Cmd->Args[I]);
355     }
356 }
357
358
359
360 /*****************************************************************************/
361 /*                              Target handling                              */
362 /*****************************************************************************/
363
364
365
366 static void SetTargetFiles (void)
367 /* Set the target system files */
368 {
369     /* Determine the names of the target specific library file */
370     if (Target != TGT_NONE) {
371
372         /* Get a pointer to the system name and its length */
373         const char* TargetName = GetTargetName (Target);
374         unsigned    TargetNameLen = strlen (TargetName);
375
376         /* Set the library file */
377         TargetLib = xmalloc (TargetNameLen + 4 + 1);
378         memcpy (TargetLib, TargetName, TargetNameLen);
379         strcpy (TargetLib + TargetNameLen, ".lib");
380
381     }
382 }
383
384
385
386 /*****************************************************************************/
387 /*                               Subprocesses                                */
388 /*****************************************************************************/
389
390
391
392 static void ExecProgram (CmdDesc* Cmd)
393 /* Execute a subprocess with the given name/parameters. Exit on errors. */
394 {
395     int Status;
396
397     /* If in debug mode, output the command line we will execute */
398     if (Debug) {
399         printf ("Executing: ");
400         CmdPrint (Cmd, stdout);
401         printf ("\n");
402     }
403
404     /* Call the program */
405     Status = spawnvp (P_WAIT, Cmd->Name, SPAWN_ARGV_CONST_CAST Cmd->Args);
406
407     /* Check the result code */
408     if (Status < 0) {
409         /* Error executing the program */
410         Error ("Cannot execute `%s': %s", Cmd->Name, strerror (errno));
411     } else if (Status != 0) {
412         /* Called program had an error */
413         exit (Status);
414     }
415 }
416
417
418
419 static void Link (void)
420 /* Link the resulting executable */
421 {
422     unsigned I;
423
424     /* Since linking is always the final step, if we have an output file name
425     ** given, set it here. If we don't have an explicit output name given,
426     ** try to build one from the name of the first input file.
427     */
428     if (OutputName) {
429
430         CmdSetOutput (&LD65, OutputName);
431
432     } else if (FirstInput && FindExt (FirstInput)) {  /* Only if ext present! */
433
434         const char* Extension = Module? MODULE_EXT : "";
435         char* Output = MakeFilename (FirstInput, Extension);
436         CmdSetOutput (&LD65, Output);
437         xfree (Output);
438
439     }
440
441     /* If we have a linker config file given, add it to the command line.
442     ** Otherwise pass the target to the linker if we have one.
443     */
444     if (LinkerConfig) {
445         if (Module) {
446             Error ("Cannot use -C and --module together");
447         }
448         CmdAddArg2 (&LD65, "-C", LinkerConfig);
449     } else if (Module) {
450         CmdSetTarget (&LD65, TGT_MODULE);
451     } else {
452         CmdSetTarget (&LD65, Target);
453     }
454
455     /* Determine which target libraries are needed */
456     SetTargetFiles ();
457
458     /* Add all object files as parameters */
459     for (I = 0; I < LD65.FileCount; ++I) {
460         CmdAddArg (&LD65, LD65.Files [I]);
461     }
462
463     /* Add the system runtime library */
464     if (TargetLib) {
465         CmdAddArg (&LD65, TargetLib);
466     }
467
468     /* Terminate the argument list with a NULL pointer */
469     CmdAddArg (&LD65, 0);
470
471     /* Call the linker */
472     ExecProgram (&LD65);
473 }
474
475
476
477 static void AssembleFile (const char* File, unsigned ArgCount)
478 /* Common routine to assemble a file. Will be called by Assemble() and
479 ** AssembleIntermediate(). Adds options common for both routines and
480 ** assembles the file. Will remove excess arguments after assembly.
481 */
482 {
483     /* Set the target system */
484     CmdSetTarget (&CA65, Target);
485
486     /* Check if this is the last processing step */
487     if (DoLink) {
488         /* We're linking later. Add the output file of the assembly
489         ** the the file list of the linker. The name of the output
490         ** file is that of the input file with ".s" replaced by ".o".
491         */
492         char* ObjName = MakeFilename (File, ".o");
493         CmdAddFile (&LD65, ObjName);
494         xfree (ObjName);
495     } else {
496         /* This is the final step. If an output name is given, set it */
497         if (OutputName) {
498             CmdSetOutput (&CA65, OutputName);
499         }
500     }
501
502     /* Add the file as argument for the assembler */
503     CmdAddArg (&CA65, File);
504
505     /* Add a NULL pointer to terminate the argument list */
506     CmdAddArg (&CA65, 0);
507
508     /* Run the assembler */
509     ExecProgram (&CA65);
510
511     /* Remove the excess arguments */
512     CmdDelArgs (&CA65, ArgCount);
513 }
514
515
516
517 static void AssembleIntermediate (const char* SourceFile)
518 /* Assemble an intermediate file which was generated by a previous processing
519 ** step with SourceFile as input. The -dep options won't be added and
520 ** the intermediate assembler file is removed after assembly.
521 */
522 {
523     /* Generate the name of the assembler output file from the source file
524     ** name. It's the same name with the extension replaced by ".s"
525     */
526     char* AsmName = MakeFilename (SourceFile, ".s");
527
528     /* Assemble the intermediate assembler file */
529     AssembleFile (AsmName, CA65.ArgCount);
530
531     /* Remove the input file */
532     if (remove (AsmName) < 0) {
533         Warning ("Cannot remove temporary file `%s': %s",
534                  AsmName, strerror (errno));
535     }
536
537     /* Free the assembler file name which was allocated from the heap */
538     xfree (AsmName);
539 }
540
541
542
543 static void Assemble (const char* File)
544 /* Assemble the given file */
545 {
546     /* Remember the current assembler argument count */
547     unsigned ArgCount = CA65.ArgCount;
548
549     /* We aren't assembling an intermediate file, but one requested by the
550     ** user. So add a few options here if they were given on the command
551     ** line.
552     */
553     if (DepName && *DepName) {
554         CmdAddArg2 (&CA65, "--create-dep", DepName);
555     }
556     if (FullDepName && *FullDepName) {
557         CmdAddArg2 (&CA65, "--create-full-dep", FullDepName);
558     }
559
560     /* Use the common routine */
561     AssembleFile (File, ArgCount);
562 }
563
564
565
566 static void Compile (const char* File)
567 /* Compile the given file */
568 {
569     /* Remember the current compiler argument count */
570     unsigned ArgCount = CC65.ArgCount;
571
572     /* Set the target system */
573     CmdSetTarget (&CC65, Target);
574
575     /* Check if this is the final step */
576     if (DoAssemble) {
577         /* We will assemble this file later. If a dependency file is to be
578         ** generated, set the dependency target to be the final object file,
579         ** not the intermediate assembler file. But beware: There may be an
580         ** output name specified for the assembler.
581         */
582         if (DepName || FullDepName) {
583             /* Was an output name for the assembler specified? */
584             if (!DoLink && OutputName) {
585                 /* Use this name as the dependency target */
586                 CmdAddArg2 (&CC65, "--dep-target", OutputName);
587             } else {
588                 /* Use the object file name as the dependency target */
589                 char* ObjName = MakeFilename (File, ".o");
590                 CmdAddArg2 (&CC65, "--dep-target", ObjName);
591                 xfree (ObjName);
592             }
593         }
594     } else {
595         /* If we won't assemble, this is the final step. In this case, set
596         ** the output name if it was given.
597         */
598         if (OutputName) {
599             CmdSetOutput (&CC65, OutputName);
600         }
601     }
602
603     /* Add the file as argument for the compiler */
604     CmdAddArg (&CC65, File);
605
606     /* Add a NULL pointer to terminate the argument list */
607     CmdAddArg (&CC65, 0);
608
609     /* Run the compiler */
610     ExecProgram (&CC65);
611
612     /* Remove the excess arguments */
613     CmdDelArgs (&CC65, ArgCount);
614
615     /* If this is not the final step, assemble the generated file, then
616     ** remove it
617     */
618     if (DoAssemble) {
619         /* Assemble the intermediate file and remove it */
620         AssembleIntermediate (File);
621     }
622 }
623
624
625
626 static void CompileRes (const char* File)
627 /* Compile the given geos resource file */
628 {
629     /* Remember the current assembler argument count */
630     unsigned ArgCount = GRC.ArgCount;
631
632     /* Resource files need an geos-apple or geos-cbm target but this
633     ** is checked within grc65.
634     */
635     CmdSetTarget (&GRC, Target);
636
637     /* Add the file as argument for the resource compiler */
638     CmdAddArg (&GRC, File);
639
640     /* Add a NULL pointer to terminate the argument list */
641     CmdAddArg (&GRC, 0);
642
643     /* Run the compiler */
644     ExecProgram (&GRC);
645
646     /* Remove the excess arguments */
647     CmdDelArgs (&GRC, ArgCount);
648
649     /* If this is not the final step, assemble the generated file, then
650     ** remove it
651     */
652     if (DoAssemble) {
653         /* Assemble the intermediate file and remove it */
654         AssembleIntermediate (File);
655     }
656 }
657
658
659
660 static void ConvertO65 (const char* File)
661 /* Convert an o65 object file into an assembler file */
662 {
663     /* Remember the current converter argument count */
664     unsigned ArgCount = CO65.ArgCount;
665
666     /* If we won't assemble, this is the final step. In this case, set the
667     ** output name.
668     */
669     if (!DoAssemble && OutputName) {
670         CmdSetOutput (&CO65, OutputName);
671     }
672
673     /* Add the file as argument for the object file converter */
674     CmdAddArg (&CO65, File);
675
676     /* Add a NULL pointer to terminate the argument list */
677     CmdAddArg (&CO65, 0);
678
679     /* Run the converter */
680     ExecProgram (&CO65);
681
682     /* Remove the excess arguments */
683     CmdDelArgs (&CO65, ArgCount);
684
685     /* If this is not the final step, assemble the generated file, then
686     ** remove it
687     */
688     if (DoAssemble) {
689         /* Assemble the intermediate file and remove it */
690         AssembleIntermediate (File);
691     }
692 }
693
694
695
696 /*****************************************************************************/
697 /*                                   Code                                    */
698 /*****************************************************************************/
699
700
701
702 static void Usage (void)
703 /* Print usage information and exit */
704 {
705     printf ("Usage: %s [options] file [...]\n"
706             "Short options:\n"
707             "  -c\t\t\t\tCompile and assemble, but don't link\n"
708             "  -d\t\t\t\tDebug mode\n"
709             "  -g\t\t\t\tAdd debug info\n"
710             "  -h\t\t\t\tHelp (this text)\n"
711             "  -l name\t\t\tCreate an assembler listing file\n"
712             "  -m name\t\t\tCreate a map file\n"
713             "  -mm model\t\t\tSet the memory model\n"
714             "  -o name\t\t\tName the output file\n"
715             "  -r\t\t\t\tEnable register variables\n"
716             "  -t sys\t\t\tSet the target system\n"
717             "  -u sym\t\t\tForce an import of symbol `sym'\n"
718             "  -v\t\t\t\tVerbose mode\n"
719             "  -vm\t\t\t\tVerbose map file\n"
720             "  -C name\t\t\tUse linker config file\n"
721             "  -Cl\t\t\t\tMake local variables static\n"
722             "  -D sym[=defn]\t\t\tDefine a preprocessor symbol\n"
723             "  -E Stop after the preprocessing stage\n"
724             "  -I dir\t\t\tSet a compiler include directory path\n"
725             "  -L path\t\t\tSpecify a library search path\n"
726             "  -Ln name\t\t\tCreate a VICE label file\n"
727             "  -O\t\t\t\tOptimize code\n"
728             "  -Oi\t\t\t\tOptimize code, inline runtime functions\n"
729             "  -Or\t\t\t\tOptimize code, honour the register keyword\n"
730             "  -Os\t\t\t\tOptimize code, inline known C functions\n"
731             "  -S\t\t\t\tCompile, but don't assemble and link\n"
732             "  -T\t\t\t\tInclude source as comment\n"
733             "  -V\t\t\t\tPrint the version number\n"
734             "  -W name[,...]\t\t\tSuppress compiler warnings\n"
735             "  -Wa options\t\t\tPass options to the assembler\n"
736             "  -Wc options\t\t\tPass options to the compiler\n"
737             "  -Wl options\t\t\tPass options to the linker\n"
738             "\n"
739             "Long options:\n"
740             "  --add-source\t\t\tInclude source as comment\n"
741             "  --asm-args options\t\tPass options to the assembler\n"
742             "  --asm-define sym[=v]\t\tDefine an assembler symbol\n"
743             "  --asm-include-dir dir\t\tSet an assembler include directory\n"
744             "  --bin-include-dir dir\t\tSet an assembler binary include directory\n"
745             "  --bss-label name\t\tDefine and export a BSS segment label\n"
746             "  --bss-name seg\t\tSet the name of the BSS segment\n"
747             "  --cc-args options\t\tPass options to the compiler\n"
748             "  --cfg-path path\t\tSpecify a config file search path\n"
749             "  --check-stack\t\t\tGenerate stack overflow checks\n"
750             "  --code-label name\t\tDefine and export a CODE segment label\n"
751             "  --code-name seg\t\tSet the name of the CODE segment\n"
752             "  --codesize x\t\t\tAccept larger code by factor x\n"
753             "  --config name\t\t\tUse linker config file\n"
754             "  --cpu type\t\t\tSet CPU type\n"
755             "  --create-dep name\t\tCreate a make dependency file\n"
756             "  --create-full-dep name\tCreate a full make dependency file\n"
757             "  --data-label name\t\tDefine and export a DATA segment label\n"
758             "  --data-name seg\t\tSet the name of the DATA segment\n"
759             "  --debug\t\t\tDebug mode\n"
760             "  --debug-info\t\t\tAdd debug info\n"
761             "  --feature name\t\tSet an emulation feature\n"
762             "  --force-import sym\t\tForce an import of symbol `sym'\n"
763             "  --help\t\t\tHelp (this text)\n"
764             "  --include-dir dir\t\tSet a compiler include directory path\n"
765             "  --ld-args options\t\tPass options to the linker\n"
766             "  --lib file\t\t\tLink this library\n"
767             "  --lib-path path\t\tSpecify a library search path\n"
768             "  --list-targets\t\tList all available targets\n"
769             "  --listing name\t\tCreate an assembler listing file\n"
770             "  --list-bytes n\t\tNumber of bytes per assembler listing line\n"
771             "  --mapfile name\t\tCreate a map file\n"
772             "  --memory-model model\t\tSet the memory model\n"
773             "  --module\t\t\tLink as a module\n"
774             "  --module-id id\t\tSpecify a module ID for the linker\n"
775             "  --o65-model model\t\tOverride the o65 model\n"
776             "  --obj file\t\t\tLink this object file\n"
777             "  --obj-path path\t\tSpecify an object file search path\n"
778             "  --print-target-path\t\tPrint the target file path\n"
779             "  --register-space b\t\tSet space available for register variables\n"
780             "  --register-vars\t\tEnable register variables\n"
781             "  --rodata-name seg\t\tSet the name of the RODATA segment\n"
782             "  --signed-chars\t\tDefault characters are signed\n"
783             "  --standard std\t\tLanguage standard (c89, c99, cc65)\n"
784             "  --start-addr addr\t\tSet the default start address\n"
785             "  --static-locals\t\tMake local variables static\n"
786             "  --target sys\t\t\tSet the target system\n"
787             "  --version\t\t\tPrint the version number\n"
788             "  --verbose\t\t\tVerbose mode\n"
789             "  --zeropage-label name\t\tDefine and export a ZEROPAGE segment label\n"
790             "  --zeropage-name seg\t\tSet the name of the ZEROPAGE segment\n",
791             ProgName);
792 }
793
794
795
796 static void OptAddSource (const char* Opt attribute ((unused)),
797                           const char* Arg attribute ((unused)))
798 /* Strict source code as comments to the generated asm code */
799 {
800     CmdAddArg (&CC65, "-T");
801 }
802
803
804
805 static void OptAsmArgs (const char* Opt attribute ((unused)), const char* Arg)
806 /* Pass arguments to the assembler */
807 {
808     CmdAddArgList (&CA65, Arg);
809 }
810
811
812
813 static void OptAsmDefine (const char* Opt attribute ((unused)), const char* Arg)
814 /* Define an assembler symbol (assembler) */
815 {
816     CmdAddArg2 (&CA65, "-D", Arg);
817 }
818
819
820
821 static void OptAsmIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
822 /* Include directory (assembler) */
823 {
824     CmdAddArg2 (&CA65, "-I", Arg);
825 }
826
827
828
829 static void OptBinIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
830 /* Binary include directory (assembler) */
831 {
832     CmdAddArg2 (&CA65, "--bin-include-dir", Arg);
833 }
834
835
836
837 static void OptBssLabel (const char* Opt attribute ((unused)), const char* Arg)
838 /* Handle the --bss-label option */
839 {
840     CmdAddArg2 (&CO65, "--bss-label", Arg);
841 }
842
843
844
845 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
846 /* Handle the --bss-name option */
847 {
848     CmdAddArg2 (&CC65, "--bss-name", Arg);
849     CmdAddArg2 (&CO65, "--bss-name", Arg);
850 }
851
852
853
854 static void OptCCArgs (const char* Opt attribute ((unused)), const char* Arg)
855 /* Pass arguments to the compiler */
856 {
857     CmdAddArgList (&CC65, Arg);
858 }
859
860
861
862 static void OptCfgPath (const char* Opt attribute ((unused)), const char* Arg)
863 /* Config file search path (linker) */
864 {
865     CmdAddArg2 (&LD65, "--cfg-path", Arg);
866 }
867
868
869
870 static void OptCheckStack (const char* Opt attribute ((unused)),
871                            const char* Arg attribute ((unused)))
872 /* Handle the --check-stack option */
873 {
874     CmdAddArg (&CC65, "--check-stack");
875 }
876
877
878
879 static void OptCodeLabel (const char* Opt attribute ((unused)), const char* Arg)
880 /* Handle the --code-label option */
881 {
882     CmdAddArg2 (&CO65, "--code-label", Arg);
883 }
884
885
886
887 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
888 /* Handle the --code-name option */
889 {
890     CmdAddArg2 (&CC65, "--code-name", Arg);
891     CmdAddArg2 (&CO65, "--code-name", Arg);
892 }
893
894
895
896 static void OptCodeSize (const char* Opt attribute ((unused)), const char* Arg)
897 /* Handle the --codesize option */
898 {
899     CmdAddArg2 (&CC65, "--codesize", Arg);
900 }
901
902
903
904 static void OptConfig (const char* Opt attribute ((unused)), const char* Arg)
905 /* Config file (linker) */
906 {
907     if (LinkerConfig) {
908         Error ("Cannot specify -C/--config twice");
909     }
910     LinkerConfig = Arg;
911 }
912
913
914
915 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
916 /* Handle the --cpu option */
917 {
918     /* Add the cpu type to the assembler and compiler */
919     CmdAddArg2 (&CA65, "--cpu", Arg);
920     CmdAddArg2 (&CC65, "--cpu", Arg);
921 }
922
923
924
925 static void OptCreateDep (const char* Opt attribute ((unused)), const char* Arg)
926 /* Handle the --create-dep option */
927 {
928     /* Add the file name to the compiler */
929     CmdAddArg2 (&CC65, "--create-dep", Arg);
930
931     /* Remember the file name for the assembler */
932     DepName = Arg;
933 }
934
935
936
937 static void OptCreateFullDep (const char* Opt attribute ((unused)), const char* Arg)
938 /* Handle the --create-full-dep option */
939 {
940     /* Add the file name to the compiler */
941     CmdAddArg2 (&CC65, "--create-full-dep", Arg);
942
943     /* Remember the file name for the assembler */
944     FullDepName = Arg;
945 }
946
947
948
949 static void OptDataLabel (const char* Opt attribute ((unused)), const char* Arg)
950 /* Handle the --data-label option */
951 {
952     CmdAddArg2 (&CO65, "--data-label", Arg);
953 }
954
955
956
957 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
958 /* Handle the --data-name option */
959 {
960     CmdAddArg2 (&CC65, "--data-name", Arg);
961     CmdAddArg2 (&CO65, "--data-name", Arg);
962 }
963
964
965
966 static void OptDebug (const char* Opt attribute ((unused)),
967                       const char* Arg attribute ((unused)))
968 /* Debug mode (compiler and cl65 utility) */
969 {
970     CmdAddArg (&CC65, "-d");
971     CmdAddArg (&CO65, "-d");
972     Debug = 1;
973 }
974
975
976
977 static void OptDebugInfo (const char* Opt attribute ((unused)),
978                           const char* Arg attribute ((unused)))
979 /* Debug Info - add to compiler and assembler */
980 {
981     CmdAddArg (&CC65, "-g");
982     CmdAddArg (&CA65, "-g");
983     CmdAddArg (&CO65, "-g");
984 }
985
986
987
988 static void OptFeature (const char* Opt attribute ((unused)), const char* Arg)
989 /* Emulation features for the assembler */
990 {
991     CmdAddArg2 (&CA65, "--feature", Arg);
992 }
993
994
995
996 static void OptForceImport (const char* Opt attribute ((unused)), const char* Arg)
997 /* Emulation features for the assembler */
998 {
999     CmdAddArg2 (&LD65, "-u", Arg);
1000 }
1001
1002
1003
1004 static void OptHelp (const char* Opt attribute ((unused)),
1005                      const char* Arg attribute ((unused)))
1006 /* Print help - cl65 */
1007 {
1008     Usage ();
1009     exit (EXIT_SUCCESS);
1010 }
1011
1012
1013
1014 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
1015 /* Include directory (compiler) */
1016 {
1017     CmdAddArg2 (&CC65, "-I", Arg);
1018 }
1019
1020
1021
1022 static void OptLdArgs (const char* Opt attribute ((unused)), const char* Arg)
1023 /* Pass arguments to the linker */
1024 {
1025     CmdAddArgList (&LD65, Arg);
1026 }
1027
1028
1029
1030 static void OptLib (const char* Opt attribute ((unused)), const char* Arg)
1031 /* Library file follows (linker) */
1032 {
1033     CmdAddArg2 (&LD65, "--lib", Arg);
1034 }
1035
1036
1037
1038 static void OptLibPath (const char* Opt attribute ((unused)), const char* Arg)
1039 /* Library search path (linker) */
1040 {
1041     CmdAddArg2 (&LD65, "--lib-path", Arg);
1042 }
1043
1044
1045
1046 static void OptListBytes (const char* Opt attribute ((unused)), const char* Arg)
1047 /* Set the maximum number of bytes per asm listing line */
1048 {
1049     CmdAddArg2 (&CA65, "--list-bytes", Arg);
1050 }
1051
1052
1053
1054 static void OptListing (const char* Opt attribute ((unused)), const char* Arg)
1055 /* Create an assembler listing */
1056 {
1057     CmdAddArg2 (&CA65, "-l", Arg);
1058 }
1059
1060
1061
1062 static void OptListTargets (const char* Opt attribute ((unused)),
1063                             const char* Arg attribute ((unused)))
1064 /* List all targets */
1065 {
1066     target_t T;
1067
1068     /* List the targets */
1069     for (T = TGT_NONE; T < TGT_COUNT; ++T) {
1070         printf ("%s\n", GetTargetName (T));
1071     }
1072
1073     /* Terminate */
1074     exit (EXIT_SUCCESS);
1075 }
1076
1077
1078
1079 static void OptMapFile (const char* Opt attribute ((unused)), const char* Arg)
1080 /* Create a map file */
1081 {
1082     /* Create a map file (linker) */
1083     CmdAddArg2 (&LD65, "-m", Arg);
1084 }
1085
1086
1087
1088 static void OptMemoryModel (const char* Opt attribute ((unused)), const char* Arg)
1089 /* Set the memory model */
1090 {
1091     mmodel_t MemoryModel = FindMemoryModel (Arg);
1092     if (MemoryModel == MMODEL_UNKNOWN) {
1093         Error ("Unknown memory model: %s", Arg);
1094     } else if (MemoryModel == MMODEL_HUGE) {
1095         Error ("Unsupported memory model: %s", Arg);
1096     } else {
1097         CmdAddArg2 (&CA65, "-mm", Arg);
1098         CmdAddArg2 (&CC65, "-mm", Arg);
1099     }
1100 }
1101
1102
1103
1104 static void OptModule (const char* Opt attribute ((unused)),
1105                        const char* Arg attribute ((unused)))
1106 /* Link as a module */
1107 {
1108     Module = 1;
1109 }
1110
1111
1112
1113 static void OptModuleId (const char* Opt attribute ((unused)), const char* Arg)
1114 /* Specify a module if for the linker */
1115 {
1116     /* Pass it straight to the linker */
1117     CmdAddArg2 (&LD65, "--module-id", Arg);
1118 }
1119
1120
1121
1122 static void OptO65Model (const char* Opt attribute ((unused)), const char* Arg)
1123 /* Handle the --o65-model option */
1124 {
1125     CmdAddArg2 (&CO65, "-m", Arg);
1126 }
1127
1128
1129
1130 static void OptObj (const char* Opt attribute ((unused)), const char* Arg)
1131 /* Object file follows (linker) */
1132 {
1133     CmdAddArg2 (&LD65, "--obj", Arg);
1134 }
1135
1136
1137
1138 static void OptObjPath (const char* Opt attribute ((unused)), const char* Arg)
1139 /* Object file search path (linker) */
1140 {
1141     CmdAddArg2 (&LD65, "--obj-path", Arg);
1142 }
1143
1144
1145
1146 static void OptPrintTargetPath (const char* Opt attribute ((unused)),
1147                                 const char* Arg attribute ((unused)))
1148 /* Print the target file path */
1149 {
1150     SearchPaths* TargetPath = NewSearchPath ();
1151     AddSubSearchPathFromEnv (TargetPath, "CC65_HOME", "target");
1152 #if defined(CL65_TGT) && !defined(_WIN32)
1153     AddSearchPath (TargetPath, STRINGIZE (CL65_TGT));
1154 #endif
1155     AddSubSearchPathFromWinBin (TargetPath, "target");
1156
1157     printf ("%s\n", GetSearchPath (TargetPath, 0));
1158     exit (EXIT_SUCCESS);
1159 }
1160
1161
1162
1163 static void OptRegisterSpace (const char* Opt attribute ((unused)), const char* Arg)
1164 /* Handle the --register-space option */
1165 {
1166     CmdAddArg2 (&CC65, "--register-space", Arg);
1167 }
1168
1169
1170
1171 static void OptRegisterVars (const char* Opt attribute ((unused)),
1172                              const char* Arg attribute ((unused)))
1173 /* Handle the --register-vars option */
1174 {
1175     CmdAddArg (&CC65, "-r");
1176 }
1177
1178
1179
1180 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
1181 /* Handle the --rodata-name option */
1182 {
1183     CmdAddArg2 (&CC65, "--rodata-name", Arg);
1184 }
1185
1186
1187
1188 static void OptSignedChars (const char* Opt attribute ((unused)),
1189                             const char* Arg attribute ((unused)))
1190 /* Make default characters signed */
1191 {
1192     CmdAddArg (&CC65, "-j");
1193 }
1194
1195
1196
1197 static void OptStandard (const char* Opt attribute ((unused)), const char* Arg)
1198 /* Set the language standard */
1199 {
1200     CmdAddArg2 (&CC65, "--standard", Arg);
1201 }
1202
1203
1204
1205 static void OptStartAddr (const char* Opt attribute ((unused)), const char* Arg)
1206 /* Set the default start address */
1207 {
1208     CmdAddArg2 (&LD65, "-S", Arg);
1209 }
1210
1211
1212
1213 static void OptStaticLocals (const char* Opt attribute ((unused)),
1214                              const char* Arg attribute ((unused)))
1215 /* Place local variables in static storage */
1216 {
1217     CmdAddArg (&CC65, "-Cl");
1218 }
1219
1220
1221
1222 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
1223 /* Set the target system */
1224 {
1225     Target = FindTarget (Arg);
1226     if (Target == TGT_UNKNOWN) {
1227         Error ("No such target system: `%s'", Arg);
1228     } else if (Target == TGT_MODULE) {
1229         Error ("Cannot use `module' as target, use --module instead");
1230     }
1231 }
1232
1233
1234
1235 static void OptVerbose (const char* Opt attribute ((unused)),
1236                         const char* Arg attribute ((unused)))
1237 /* Verbose mode (compiler, assembler, linker) */
1238 {
1239     CmdAddArg (&CC65, "-v");
1240     CmdAddArg (&CA65, "-v");
1241     CmdAddArg (&CO65, "-v");
1242     CmdAddArg (&LD65, "-v");
1243 }
1244
1245
1246
1247 static void OptVersion (const char* Opt attribute ((unused)),
1248                         const char* Arg attribute ((unused)))
1249 /* Print version number */
1250 {
1251     fprintf (stderr, "%s V%s\n", ProgName, GetVersionAsString ());
1252     exit(EXIT_SUCCESS);
1253 }
1254
1255
1256
1257 static void OptZeropageLabel (const char* Opt attribute ((unused)), const char* Arg)
1258 /* Handle the --zeropage-label option */
1259 {
1260     CmdAddArg2 (&CO65, "--zeropage-label", Arg);
1261 }
1262
1263
1264
1265 static void OptZeropageName (const char* Opt attribute ((unused)), const char* Arg)
1266 /* Handle the --zeropage-name option */
1267 {
1268     CmdAddArg2 (&CO65, "--zeropage-name", Arg);
1269 }
1270
1271
1272
1273 int main (int argc, char* argv [])
1274 /* Utility main program */
1275 {
1276     /* Program long options */
1277     static const LongOpt OptTab[] = {
1278         { "--add-source",        0, OptAddSource      },
1279         { "--asm-args",          1, OptAsmArgs        },
1280         { "--asm-define",        1, OptAsmDefine      },
1281         { "--asm-include-dir",   1, OptAsmIncludeDir  },
1282         { "--bin-include-dir",   1, OptBinIncludeDir  },
1283         { "--bss-label",         1, OptBssLabel       },
1284         { "--bss-name",          1, OptBssName        },
1285         { "--cc-args",           1, OptCCArgs         },
1286         { "--cfg-path",          1, OptCfgPath        },
1287         { "--check-stack",       0, OptCheckStack     },
1288         { "--code-label",        1, OptCodeLabel      },
1289         { "--code-name",         1, OptCodeName       },
1290         { "--codesize",          1, OptCodeSize       },
1291         { "--config",            1, OptConfig         },
1292         { "--cpu",               1, OptCPU            },
1293         { "--create-dep",        1, OptCreateDep      },
1294         { "--create-full-dep",   1, OptCreateFullDep  },
1295         { "--data-label",        1, OptDataLabel      },
1296         { "--data-name",         1, OptDataName       },
1297         { "--debug",             0, OptDebug          },
1298         { "--debug-info",        0, OptDebugInfo      },
1299         { "--feature",           1, OptFeature        },
1300         { "--force-import",      1, OptForceImport    },
1301         { "--help",              0, OptHelp           },
1302         { "--include-dir",       1, OptIncludeDir     },
1303         { "--ld-args",           1, OptLdArgs         },
1304         { "--lib",               1, OptLib            },
1305         { "--lib-path",          1, OptLibPath        },
1306         { "--list-targets",      0, OptListTargets    },
1307         { "--listing",           1, OptListing        },
1308         { "--list-bytes",        1, OptListBytes      },
1309         { "--mapfile",           1, OptMapFile        },
1310         { "--memory-model",      1, OptMemoryModel    },
1311         { "--module",            0, OptModule         },
1312         { "--module-id",         1, OptModuleId       },
1313         { "--o65-model",         1, OptO65Model       },
1314         { "--obj",               1, OptObj            },
1315         { "--obj-path",          1, OptObjPath        },
1316         { "--print-target-path", 0, OptPrintTargetPath},
1317         { "--register-space",    1, OptRegisterSpace  },
1318         { "--register-vars",     0, OptRegisterVars   },
1319         { "--rodata-name",       1, OptRodataName     },
1320         { "--signed-chars",      0, OptSignedChars    },
1321         { "--standard",          1, OptStandard       },
1322         { "--start-addr",        1, OptStartAddr      },
1323         { "--static-locals",     0, OptStaticLocals   },
1324         { "--target",            1, OptTarget         },
1325         { "--verbose",           0, OptVerbose        },
1326         { "--version",           0, OptVersion        },
1327         { "--zeropage-label",    1, OptZeropageLabel  },
1328         { "--zeropage-name",     1, OptZeropageName   },
1329     };
1330
1331     char* CmdPath;
1332     unsigned I;
1333
1334     /* Initialize the cmdline module */
1335     InitCmdLine (&argc, &argv, "cl65");
1336
1337     /* Initialize the command descriptors */
1338     if (argc == 0) {
1339         CmdPath = xstrdup ("");
1340     } else {
1341         char* Ptr;
1342         CmdPath = xstrdup (argv[0]);
1343         Ptr = strrchr (CmdPath, '/');
1344         if (Ptr == 0) {
1345             Ptr = strrchr (CmdPath, '\\');
1346         }
1347         if (Ptr == 0) {
1348             *CmdPath = '\0';
1349         } else {
1350             *(Ptr + 1) = '\0';
1351         }
1352     }
1353     CmdInit (&CC65, CmdPath, "cc65");
1354     CmdInit (&CA65, CmdPath, "ca65");
1355     CmdInit (&CO65, CmdPath, "co65");
1356     CmdInit (&LD65, CmdPath, "ld65");
1357     CmdInit (&GRC,  CmdPath, "grc65");
1358     xfree (CmdPath);
1359
1360     /* Our default target is the C64 instead of "none" */
1361     Target = TGT_C64;
1362
1363     /* Check the parameters */
1364     I = 1;
1365     while (I < ArgCount) {
1366
1367         /* Get the argument */
1368         const char* Arg = ArgVec[I];
1369
1370         /* Check for an option */
1371         if (Arg [0] == '-') {
1372
1373             switch (Arg [1]) {
1374
1375                 case '-':
1376                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
1377                     break;
1378
1379                 case 'C':
1380                     if (Arg[2] == 'l' && Arg[3] == '\0') {
1381                         /* Make local variables static */
1382                         OptStaticLocals (Arg, 0);
1383                     } else {
1384                         /* Specify linker config file */
1385                         OptConfig (Arg, GetArg (&I, 2));
1386                     }
1387                     break;
1388
1389                 case 'D':
1390                     /* Define a preprocessor symbol (compiler) */
1391                     CmdAddArg2 (&CC65, "-D", GetArg (&I, 2));
1392                     break;
1393
1394                 case 'I':
1395                     /* Include directory (compiler) */
1396                     OptIncludeDir (Arg, GetArg (&I, 2));
1397                     break;
1398
1399                 case 'L':
1400                     if (Arg[2] == 'n' && Arg[3] == '\0') {
1401                         /* VICE label file (linker) */
1402                         CmdAddArg2 (&LD65, "-Ln", GetArg (&I, 3));
1403                     } else {
1404                         /* Library search path (linker) */
1405                         OptLibPath (Arg, GetArg (&I, 2));
1406                     }
1407                     break;
1408
1409                 case 'O':
1410                     /* Optimize code (compiler, also covers -Oi and others) */
1411                     CmdAddArg (&CC65, Arg);
1412                     break;
1413
1414                 case 'S':
1415                     /* Dont assemble and link the created files */
1416                     DisableAssemblingAndLinking();
1417                     break;
1418
1419                 case 'T':
1420                     /* Include source as comment (compiler) */
1421                     OptAddSource (Arg, 0);
1422                     break;
1423
1424                 case 'V':
1425                     /* Print version number */
1426                     OptVersion (Arg, 0);
1427                     break;
1428                 
1429                 case 'E':
1430                     /*Forward -E to compiler*/
1431                     CmdAddArg (&CC65, Arg);  
1432                     DisableAssemblingAndLinking();
1433                     break;
1434                 case 'W':
1435                     if (Arg[2] == 'a' && Arg[3] == '\0') {
1436                         /* -Wa: Pass options to assembler */
1437                         OptAsmArgs (Arg, GetArg (&I, 3));
1438                     } else if (Arg[2] == 'c' && Arg[3] == '\0') {
1439                         /* -Wc: Pass options to compiler */
1440                         /* Remember -Wc sub arguments in cc65 arg struct */ 
1441                         OptCCArgs (Arg, GetArg (&I, 3));
1442                     } else if (Arg[2] == 'l' && Arg[3] == '\0') {
1443                         /* -Wl: Pass options to linker */
1444                         OptLdArgs (Arg, GetArg (&I, 3));
1445                     } else {
1446                         /* Anything else: Suppress warnings (compiler) */
1447                         CmdAddArg2 (&CC65, "-W", GetArg (&I, 2));
1448                     }
1449                     break;
1450
1451                 case 'c':
1452                     /* Don't link the resulting files */
1453                     DisableLinking();
1454                     break;
1455
1456                 case 'd':
1457                     /* Debug mode (compiler) */
1458                     OptDebug (Arg, 0);
1459                     break;
1460
1461                 case 'g':
1462                     /* Debugging - add to compiler and assembler */
1463                     OptDebugInfo (Arg, 0);
1464                     break;
1465
1466                 case 'h':
1467                 case '?':
1468                     /* Print help - cl65 */
1469                     OptHelp (Arg, 0);
1470                     break;
1471
1472                 case 'j':
1473                     /* Default characters are signed */
1474                     OptSignedChars (Arg, 0);
1475                     break;
1476
1477                 case 'l':
1478                     /* Create an assembler listing */
1479                     OptListing (Arg, GetArg (&I, 2));
1480                     break;
1481
1482                 case 'm':
1483                     /* Create a map file (linker) */
1484                     OptMapFile (Arg, GetArg (&I, 2));
1485                     break;
1486
1487                 case 'o':
1488                     /* Name the output file */
1489                     OutputName = GetArg (&I, 2);
1490                     break;
1491
1492                 case 'r':
1493                     /* Enable register variables */
1494                     OptRegisterVars (Arg, 0);
1495                     break;
1496
1497                 case 't':
1498                     /* Set target system - compiler, assembler and linker */
1499                     OptTarget (Arg, GetArg (&I, 2));
1500                     break;
1501
1502                 case 'u':
1503                     /* Force an import (linker) */
1504                     OptForceImport (Arg, GetArg (&I, 2));
1505                     break;
1506
1507                 case 'v':
1508                     if (Arg [2] == 'm') {
1509                         /* Verbose map file (linker) */
1510                         CmdAddArg (&LD65, "-vm");
1511                     } else {
1512                         /* Verbose mode (compiler, assembler, linker) */
1513                         OptVerbose (Arg, 0);
1514                     }
1515                     break;
1516
1517                 default:
1518                     UnknownOption (Arg);
1519             }
1520         } else {
1521
1522             /* Remember the first file name */
1523             if (FirstInput == 0) {
1524                 FirstInput = Arg;
1525             }
1526
1527             /* Determine the file type by the extension */
1528             switch (GetFileType (Arg)) {
1529
1530                 case FILETYPE_C:
1531                     /* Compile the file */
1532                     Compile (Arg);
1533                     break;
1534
1535                 case FILETYPE_ASM:
1536                     /* Assemble the file */
1537                     if (DoAssemble) {
1538                         Assemble (Arg);
1539                     }
1540                     break;
1541
1542                 case FILETYPE_OBJ:
1543                 case FILETYPE_LIB:
1544                     /* Add to the linker files */
1545                     CmdAddFile (&LD65, Arg);
1546                     break;
1547
1548                 case FILETYPE_GR:
1549                     /* Add to the resource compiler files */
1550                     CompileRes (Arg);
1551                     break;
1552
1553                 case FILETYPE_O65:
1554                     /* Add the the object file converter files */
1555                     ConvertO65 (Arg);
1556                     break;
1557
1558                 default:
1559                     Error ("Don't know what to do with `%s'", Arg);
1560
1561             }
1562
1563         }
1564
1565         /* Next argument */
1566         ++I;
1567     }
1568
1569     /* Check if we had any input files */
1570     if (FirstInput == 0) {
1571         Warning ("No input files");
1572     }
1573
1574     /* Link the given files if requested and if we have any */
1575     if (DoLink && LD65.FileCount > 0) {
1576         Link ();
1577     }
1578
1579     /* Return an apropriate exit code */
1580     return EXIT_SUCCESS;
1581 }