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