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