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