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