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