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