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