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