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