1 /*****************************************************************************/
5 /* Main module for the cl65 compile and link utility */
9 /* (C) 1999-2003 Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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. */
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: */
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 */
32 /*****************************************************************************/
36 /* Check out if we have a spawn() function on the system, or if we must use
39 #if defined(__WATCOMC__) || defined(_MSC_VER) || defined(__MINGW32__) || defined(__DJGPP__)
72 /*****************************************************************************/
74 /*****************************************************************************/
78 /* Struct that describes a command */
79 typedef struct CmdDesc CmdDesc;
81 char* Name; /* The command name */
83 unsigned ArgCount; /* Count of arguments */
84 unsigned ArgMax; /* Maximum count of arguments */
85 char** Args; /* The arguments */
87 unsigned FileCount; /* Count of files to translate */
88 unsigned FileMax; /* Maximum count of files */
89 char** Files; /* The files */
92 /* Command descriptors for the different programs */
93 static CmdDesc CC65 = { 0, 0, 0, 0, 0, 0, 0 };
94 static CmdDesc CA65 = { 0, 0, 0, 0, 0, 0, 0 };
95 static CmdDesc CO65 = { 0, 0, 0, 0, 0, 0, 0 };
96 static CmdDesc LD65 = { 0, 0, 0, 0, 0, 0, 0 };
97 static CmdDesc GRC = { 0, 0, 0, 0, 0, 0, 0 };
99 /* Variables controlling the steps we're doing */
100 static int DontLink = 0;
101 static int DontAssemble = 0;
103 /* The name of the output file, NULL if none given */
104 static const char* OutputName = 0;
106 /* The name of the linker configuration file if given */
107 static const char* LinkerConfig = 0;
109 /* The name of the first input file. This will be used to construct the
110 * executable file name if no explicit name is given.
112 static const char* FirstInput = 0;
114 /* Remember if we should link a module */
115 static int Module = 0;
117 /* Extension used for a module */
118 #define MODULE_EXT ".o65"
120 /* Name of the crt0 object file and the runtime library */
121 static char* TargetCRT0 = 0;
122 static char* TargetLib = 0;
126 /*****************************************************************************/
127 /* Include the system specific spawn function */
128 /*****************************************************************************/
132 #if defined(NEED_SPAWN)
133 # if defined(SPAWN_UNIX)
134 # include "spawn-unix.inc"
135 # elif defined(SPAWN_AMIGA)
136 # include "spawn-amiga.inc"
138 # error "Don't know which spawn module to include!"
144 /*****************************************************************************/
145 /* Command structure handling */
146 /*****************************************************************************/
150 static void CmdAddArg (CmdDesc* Cmd, const char* Arg)
151 /* Add a new argument to the command */
153 /* Expand the argument vector if needed */
154 if (Cmd->ArgCount == Cmd->ArgMax) {
155 unsigned NewMax = Cmd->ArgMax + 10;
156 char** NewArgs = xmalloc (NewMax * sizeof (char*));
157 memcpy (NewArgs, Cmd->Args, Cmd->ArgMax * sizeof (char*));
160 Cmd->ArgMax = NewMax;
163 /* Add a copy of the new argument, allow a NULL pointer */
165 Cmd->Args [Cmd->ArgCount++] = xstrdup (Arg);
167 Cmd->Args [Cmd->ArgCount++] = 0;
173 static void CmdAddArg2 (CmdDesc* Cmd, const char* Arg1, const char* Arg2)
174 /* Add a new argument pair to the command */
176 CmdAddArg (Cmd, Arg1);
177 CmdAddArg (Cmd, Arg2);
182 static void CmdDelArgs (CmdDesc* Cmd, unsigned LastValid)
183 /* Remove all arguments with an index greater than LastValid */
185 while (Cmd->ArgCount > LastValid) {
187 xfree (Cmd->Args [Cmd->ArgCount]);
188 Cmd->Args [Cmd->ArgCount] = 0;
194 static void CmdAddFile (CmdDesc* Cmd, const char* File)
195 /* Add a new file to the command */
197 /* Expand the file vector if needed */
198 if (Cmd->FileCount == Cmd->FileMax) {
199 unsigned NewMax = Cmd->FileMax + 10;
200 char** NewFiles = xmalloc (NewMax * sizeof (char*));
201 memcpy (NewFiles, Cmd->Files, Cmd->FileMax * sizeof (char*));
203 Cmd->Files = NewFiles;
204 Cmd->FileMax = NewMax;
207 /* If the file name is not NULL (which is legal and is used to terminate
208 * the file list), check if the file name does already exist in the file
209 * list and print a warning if so. Regardless of the search result, add
214 for (I = 0; I < Cmd->FileCount; ++I) {
215 if (strcmp (Cmd->Files[I], File) == 0) {
217 Warning ("Duplicate file in argument list: `%s'", File);
218 /* No need to search further */
224 Cmd->Files [Cmd->FileCount++] = xstrdup (File);
226 /* Add a NULL pointer */
227 Cmd->Files [Cmd->FileCount++] = 0;
233 static void CmdInit (CmdDesc* Cmd, const char* Path)
234 /* Initialize the command using the given path to the executable */
236 /* Remember the command */
237 Cmd->Name = xstrdup (Path);
239 /* Use the command name as first argument */
240 CmdAddArg (Cmd, Path);
245 static void CmdSetOutput (CmdDesc* Cmd, const char* File)
246 /* Set the output file in a command desc */
248 CmdAddArg2 (Cmd, "-o", File);
253 static void CmdSetTarget (CmdDesc* Cmd, target_t Target)
254 /* Set the output file in a command desc */
256 CmdAddArg2 (Cmd, "-t", TargetNames[Target]);
261 static void CmdPrint (CmdDesc* Cmd, FILE* F)
262 /* Output the command line encoded in the command desc */
265 for (I = 0; I < Cmd->ArgCount && Cmd->Args[I] != 0; ++I) {
266 fprintf (F, "%s ", Cmd->Args[I]);
272 /*****************************************************************************/
273 /* Target handling */
274 /*****************************************************************************/
278 static void SetTargetFiles (void)
279 /* Set the target system files */
281 /* Determine the names of the default startup and library file */
282 if (Target != TGT_NONE) {
284 /* Get a pointer to the system name and its length */
285 const char* TargetName = TargetNames [Target];
286 unsigned TargetNameLen = strlen (TargetName);
288 /* Set the startup file */
289 TargetCRT0 = xmalloc (TargetNameLen + 2 + 1);
290 strcpy (TargetCRT0, TargetName);
291 strcat (TargetCRT0, ".o");
293 /* Set the library file */
294 TargetLib = xmalloc (TargetNameLen + 4 + 1);
295 strcpy (TargetLib, TargetName);
296 strcat (TargetLib, ".lib");
303 /*****************************************************************************/
305 /*****************************************************************************/
309 static void ExecProgram (CmdDesc* Cmd)
310 /* Execute a subprocess with the given name/parameters. Exit on errors. */
314 /* If in debug mode, output the command line we will execute */
316 printf ("Executing: ");
317 CmdPrint (Cmd, stdout);
321 /* Call the program */
322 Status = spawnvp (P_WAIT, Cmd->Name, Cmd->Args);
324 /* Check the result code */
326 /* Error executing the program */
327 Error ("Cannot execute `%s': %s", Cmd->Name, strerror (errno));
328 } else if (Status != 0) {
329 /* Called program had an error */
336 static void Link (void)
337 /* Link the resulting executable */
341 /* If we have a linker config file given, add it to the command line.
342 * Otherwise pass the target to the linker if we have one.
346 Error ("Cannot use -C and --module together");
348 CmdAddArg2 (&LD65, "-C", LinkerConfig);
350 CmdSetTarget (&LD65, TGT_MODULE);
351 } else if (Target != TGT_NONE) {
352 CmdSetTarget (&LD65, Target);
355 /* Determine which target libraries are needed */
358 /* Since linking is always the final step, if we have an output file name
359 * given, set it here. If we don't have an explicit output name given,
360 * try to build one from the name of the first input file.
364 CmdSetOutput (&LD65, OutputName);
366 } else if (FirstInput && FindExt (FirstInput)) { /* Only if ext present! */
368 const char* Extension = Module? MODULE_EXT : "";
369 char* Output = MakeFilename (FirstInput, Extension);
370 CmdSetOutput (&LD65, Output);
375 /* If we have a startup file and if we are not linking a module, add its
376 * name as a parameter
378 if (TargetCRT0 && !Module) {
379 CmdAddArg (&LD65, TargetCRT0);
382 /* Add all object files as parameters */
383 for (I = 0; I < LD65.FileCount; ++I) {
384 CmdAddArg (&LD65, LD65.Files [I]);
387 /* Add the system runtime library */
389 CmdAddArg (&LD65, TargetLib);
392 /* Terminate the argument list with a NULL pointer */
393 CmdAddArg (&LD65, 0);
395 /* Call the linker */
401 static void Assemble (const char* File)
402 /* Assemble the given file */
404 /* Remember the current assembler argument count */
405 unsigned ArgCount = CA65.ArgCount;
407 /* Set the target system */
408 CmdSetTarget (&CA65, Target);
410 /* If we won't link, this is the final step. In this case, set the
413 if (DontLink && OutputName) {
414 CmdSetOutput (&CA65, OutputName);
416 /* The object file name will be the name of the source file
417 * with .s replaced by ".o". Add this file to the list of
420 char* ObjName = MakeFilename (File, ".o");
421 CmdAddFile (&LD65, ObjName);
425 /* Add the file as argument for the assembler */
426 CmdAddArg (&CA65, File);
428 /* Add a NULL pointer to terminate the argument list */
429 CmdAddArg (&CA65, 0);
431 /* Run the assembler */
434 /* Remove the excess arguments */
435 CmdDelArgs (&CA65, ArgCount);
440 static void Compile (const char* File)
441 /* Compile the given file */
445 /* Remember the current compiler argument count */
446 unsigned ArgCount = CC65.ArgCount;
448 /* Set the target system */
449 CmdSetTarget (&CC65, Target);
451 /* If we won't link, this is the final step. In this case, set the
454 if (DontAssemble && OutputName) {
455 CmdSetOutput (&CC65, OutputName);
457 /* The assembler file name will be the name of the source file
458 * with .c replaced by ".s".
460 AsmName = MakeFilename (File, ".s");
463 /* Add the file as argument for the compiler */
464 CmdAddArg (&CC65, File);
466 /* Add a NULL pointer to terminate the argument list */
467 CmdAddArg (&CC65, 0);
469 /* Run the compiler */
472 /* Remove the excess arguments */
473 CmdDelArgs (&CC65, ArgCount);
475 /* If this is not the final step, assemble the generated file, then
480 if (remove (AsmName) < 0) {
481 Warning ("Cannot remove temporary file `%s': %s",
482 AsmName, strerror (errno));
490 static void CompileRes (const char* File)
491 /* Compile the given geos resource file */
495 /* Remember the current assembler argument count */
496 unsigned ArgCount = GRC.ArgCount;
498 /* The assembler file name will be the name of the source file
499 * with .grc replaced by ".s".
501 AsmName = MakeFilename (File, ".s");
503 /* Add the file as argument for the resource compiler */
504 CmdAddArg (&GRC, File);
506 /* Add a NULL pointer to terminate the argument list */
509 /* Run the compiler */
512 /* Remove the excess arguments */
513 CmdDelArgs (&GRC, ArgCount);
515 /* If this is not the final step, assemble the generated file, then
520 if (remove (AsmName) < 0) {
521 Warning ("Cannot remove temporary file `%s': %s",
522 AsmName, strerror (errno));
526 /* Free the assembler file name which was allocated from the heap */
532 static void ConvertO65 (const char* File)
533 /* Convert an o65 object file into an assembler file */
537 /* Remember the current converter argument count */
538 unsigned ArgCount = CO65.ArgCount;
540 /* If we won't link, this is the final step. In this case, set the
543 if (DontAssemble && OutputName) {
544 CmdSetOutput (&CO65, OutputName);
546 /* The assembler file name will be the name of the source file
547 * with .c replaced by ".s".
549 AsmName = MakeFilename (File, ".s");
552 /* Add the file as argument for the object file converter */
553 CmdAddArg (&CO65, File);
555 /* Add a NULL pointer to terminate the argument list */
556 CmdAddArg (&CO65, 0);
558 /* Run the converter */
561 /* Remove the excess arguments */
562 CmdDelArgs (&CO65, ArgCount);
564 /* If this is not the final step, assemble the generated file, then
569 if (remove (AsmName) < 0) {
570 Warning ("Cannot remove temporary file `%s': %s",
571 AsmName, strerror (errno));
575 /* Free the assembler file name which was allocated from the heap */
581 /*****************************************************************************/
583 /*****************************************************************************/
587 static void Usage (void)
588 /* Print usage information and exit */
591 "Usage: %s [options] file [...]\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 " -o name\t\tName the output file\n"
600 " -r\t\t\tEnable register variables\n"
601 " -t sys\t\tSet the target system\n"
602 " -v\t\t\tVerbose mode\n"
603 " -vm\t\t\tVerbose map file\n"
604 " -A\t\t\tStrict ANSI mode\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"
621 " --add-source\t\tInclude source as comment\n"
622 " --ansi\t\tStrict ANSI mode\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 " --cpu type\t\tSet cpu type\n"
632 " --create-dep\t\tCreate a make dependency file\n"
633 " --data-label name\tDefine and export a DATA segment label\n"
634 " --data-name seg\tSet the name of the DATA segment\n"
635 " --debug\t\tDebug mode\n"
636 " --debug-info\t\tAdd debug info\n"
637 " --feature name\tSet an emulation feature\n"
638 " --help\t\tHelp (this text)\n"
639 " --include-dir dir\tSet a compiler include directory path\n"
640 " --lib file\t\tLink this library\n"
641 " --lib-path path\tSpecify a library search path\n"
642 " --listing\t\tCreate an assembler listing\n"
643 " --mapfile name\tCreate a map file\n"
644 " --module\t\tLink as a module\n"
645 " --module-id id\tSpecify a module id for the linker\n"
646 " --o65-model model\tOverride the o65 model\n"
647 " --obj file\t\tLink this object file\n"
648 " --obj-path path\tSpecify an object file search path\n"
649 " --register-space b\tSet space available for register variables\n"
650 " --register-vars\tEnable register variables\n"
651 " --rodata-name seg\tSet the name of the RODATA segment\n"
652 " --signed-chars\tDefault characters are signed\n"
653 " --start-addr addr\tSet the default start address\n"
654 " --static-locals\tMake local variables static\n"
655 " --target sys\t\tSet the target system\n"
656 " --version\t\tPrint the version number\n"
657 " --verbose\t\tVerbose mode\n"
658 " --zeropage-label name\tDefine and export a ZEROPAGE segment label\n"
659 " --zeropage-name seg\tSet the name of the ZEROPAGE segment\n",
665 static void OptAddSource (const char* Opt attribute ((unused)),
666 const char* Arg attribute ((unused)))
667 /* Strict source code as comments to the generated asm code */
669 CmdAddArg (&CC65, "-T");
674 static void OptAnsi (const char* Opt attribute ((unused)),
675 const char* Arg attribute ((unused)))
676 /* Strict ANSI mode (compiler) */
678 CmdAddArg (&CC65, "-A");
683 static void OptAsmIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
684 /* Include directory (assembler) */
686 CmdAddArg2 (&CA65, "-I", Arg);
691 static void OptBssLabel (const char* Opt attribute ((unused)), const char* Arg)
692 /* Handle the --bss-label option */
694 CmdAddArg2 (&CO65, "--bss-label", Arg);
699 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
700 /* Handle the --bss-name option */
702 CmdAddArg2 (&CC65, "--bss-name", Arg);
703 CmdAddArg2 (&CO65, "--bss-name", Arg);
708 static void OptCfgPath (const char* Opt attribute ((unused)), const char* Arg)
709 /* Config file search path (linker) */
711 CmdAddArg2 (&LD65, "--cfg-path", Arg);
716 static void OptCheckStack (const char* Opt attribute ((unused)),
717 const char* Arg attribute ((unused)))
718 /* Handle the --check-stack option */
720 CmdAddArg (&CC65, "--check-stack");
725 static void OptCodeLabel (const char* Opt attribute ((unused)), const char* Arg)
726 /* Handle the --code-label option */
728 CmdAddArg2 (&CO65, "--code-label", Arg);
733 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
734 /* Handle the --code-name option */
736 CmdAddArg2 (&CC65, "--code-name", Arg);
737 CmdAddArg2 (&CO65, "--code-name", Arg);
742 static void OptCodeSize (const char* Opt attribute ((unused)), const char* Arg)
743 /* Handle the --codesize option */
745 CmdAddArg2 (&CC65, "--codesize", Arg);
750 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
751 /* Handle the --cpu option */
753 /* Add the cpu type to the assembler and compiler */
754 CmdAddArg2 (&CA65, "--cpu", Arg);
755 CmdAddArg2 (&CC65, "--cpu", Arg);
760 static void OptCreateDep (const char* Opt attribute ((unused)),
761 const char* Arg attribute ((unused)))
762 /* Handle the --create-dep option */
764 CmdAddArg (&CC65, "--create-dep");
769 static void OptDataLabel (const char* Opt attribute ((unused)), const char* Arg)
770 /* Handle the --data-label option */
772 CmdAddArg2 (&CO65, "--data-label", Arg);
777 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
778 /* Handle the --data-name option */
780 CmdAddArg2 (&CC65, "--data-name", Arg);
781 CmdAddArg2 (&CO65, "--data-name", Arg);
786 static void OptDebug (const char* Opt attribute ((unused)),
787 const char* Arg attribute ((unused)))
788 /* Debug mode (compiler and cl65 utility) */
790 CmdAddArg (&CC65, "-d");
791 CmdAddArg (&CO65, "-d");
797 static void OptDebugInfo (const char* Opt attribute ((unused)),
798 const char* Arg attribute ((unused)))
799 /* Debug Info - add to compiler and assembler */
801 CmdAddArg (&CC65, "-g");
802 CmdAddArg (&CA65, "-g");
803 CmdAddArg (&CO65, "-g");
808 static void OptFeature (const char* Opt attribute ((unused)), const char* Arg)
809 /* Emulation features for the assembler */
811 CmdAddArg2 (&CA65, "--feature", Arg);
816 static void OptHelp (const char* Opt attribute ((unused)),
817 const char* Arg attribute ((unused)))
818 /* Print help - cl65 */
826 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
827 /* Include directory (compiler) */
829 CmdAddArg2 (&CC65, "-I", Arg);
834 static void OptLib (const char* Opt attribute ((unused)), const char* Arg)
835 /* Library file follows (linker) */
837 CmdAddArg2 (&LD65, "--lib", Arg);
842 static void OptLibPath (const char* Opt attribute ((unused)), const char* Arg)
843 /* Library search path (linker) */
845 CmdAddArg2 (&LD65, "--lib-path", Arg);
850 static void OptListing (const char* Opt attribute ((unused)),
851 const char* Arg attribute ((unused)))
852 /* Create an assembler listing */
854 CmdAddArg (&CA65, "-l");
859 static void OptMapFile (const char* Opt attribute ((unused)), const char* Arg)
860 /* Create a map file */
862 /* Create a map file (linker) */
863 CmdAddArg2 (&LD65, "-m", Arg);
868 static void OptModule (const char* Opt attribute ((unused)),
869 const char* Arg attribute ((unused)))
870 /* Link as a module */
877 static void OptModuleId (const char* Opt attribute ((unused)), const char* Arg)
878 /* Specify a module if for the linker */
880 /* Pass it straight to the linker */
881 CmdAddArg2 (&LD65, "--module-id", Arg);
886 static void OptO65Model (const char* Opt attribute ((unused)), const char* Arg)
887 /* Handle the --o65-model option */
889 CmdAddArg2 (&CO65, "-m", Arg);
894 static void OptObj (const char* Opt attribute ((unused)), const char* Arg)
895 /* Object file follows (linker) */
897 CmdAddArg2 (&LD65, "--obj", Arg);
902 static void OptObjPath (const char* Opt attribute ((unused)), const char* Arg)
903 /* Object file search path (linker) */
905 CmdAddArg2 (&LD65, "--obj-path", Arg);
910 static void OptRegisterSpace (const char* Opt attribute ((unused)), const char* Arg)
911 /* Handle the --register-space option */
913 CmdAddArg2 (&CC65, "--register-space", Arg);
918 static void OptRegisterVars (const char* Opt attribute ((unused)),
919 const char* Arg attribute ((unused)))
920 /* Handle the --register-vars option */
922 CmdAddArg (&CC65, "-r");
927 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
928 /* Handle the --rodata-name option */
930 CmdAddArg2 (&CC65, "--rodata-name", Arg);
935 static void OptSignedChars (const char* Opt attribute ((unused)),
936 const char* Arg attribute ((unused)))
937 /* Make default characters signed */
939 CmdAddArg (&CC65, "-j");
944 static void OptStartAddr (const char* Opt attribute ((unused)), const char* Arg)
945 /* Set the default start address */
947 CmdAddArg2 (&LD65, "-S", Arg);
952 static void OptStaticLocals (const char* Opt attribute ((unused)),
953 const char* Arg attribute ((unused)))
954 /* Place local variables in static storage */
956 CmdAddArg (&CC65, "-Cl");
961 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
962 /* Set the target system */
964 Target = FindTarget (Arg);
965 if (Target == TGT_UNKNOWN) {
966 Error ("No such target system: `%s'", Arg);
967 } else if (Target == TGT_MODULE) {
968 Error ("Cannot use `module' as target, use --module instead");
974 static void OptVerbose (const char* Opt attribute ((unused)),
975 const char* Arg attribute ((unused)))
976 /* Verbose mode (compiler, assembler, linker) */
978 CmdAddArg (&CC65, "-v");
979 CmdAddArg (&CA65, "-v");
980 CmdAddArg (&CO65, "-v");
981 CmdAddArg (&LD65, "-v");
986 static void OptVersion (const char* Opt attribute ((unused)),
987 const char* Arg attribute ((unused)))
988 /* Print version number */
991 "cl65 V%u.%u.%u - (C) Copyright 1998-2003 Ullrich von Bassewitz\n",
992 VER_MAJOR, VER_MINOR, VER_PATCH);
997 static void OptZeropageLabel (const char* Opt attribute ((unused)), const char* Arg)
998 /* Handle the --zeropage-label option */
1000 CmdAddArg2 (&CO65, "--zeropage-label", Arg);
1005 static void OptZeropageName (const char* Opt attribute ((unused)), const char* Arg)
1006 /* Handle the --zeropage-name option */
1008 CmdAddArg2 (&CO65, "--zeropage-name", Arg);
1013 int main (int argc, char* argv [])
1014 /* Utility main program */
1016 /* Program long options */
1017 static const LongOpt OptTab[] = {
1018 { "--add-source", 0, OptAddSource },
1019 { "--ansi", 0, OptAnsi },
1020 { "--asm-include-dir", 1, OptAsmIncludeDir },
1021 { "--bss-label", 1, OptBssLabel },
1022 { "--bss-name", 1, OptBssName },
1023 { "--cfg-path", 1, OptCfgPath },
1024 { "--check-stack", 0, OptCheckStack },
1025 { "--code-label", 1, OptCodeLabel },
1026 { "--code-name", 1, OptCodeName },
1027 { "--codesize", 1, OptCodeSize },
1028 { "--cpu", 1, OptCPU },
1029 { "--create-dep", 0, OptCreateDep },
1030 { "--data-label", 1, OptDataLabel },
1031 { "--data-name", 1, OptDataName },
1032 { "--debug", 0, OptDebug },
1033 { "--debug-info", 0, OptDebugInfo },
1034 { "--feature", 1, OptFeature },
1035 { "--help", 0, OptHelp },
1036 { "--include-dir", 1, OptIncludeDir },
1037 { "--lib", 1, OptLib },
1038 { "--lib-path", 1, OptLibPath },
1039 { "--listing", 0, OptListing },
1040 { "--mapfile", 1, OptMapFile },
1041 { "--module", 0, OptModule },
1042 { "--module-id", 1, OptModuleId },
1043 { "--o65-model", 1, OptO65Model },
1044 { "--obj", 1, OptObj },
1045 { "--obj-path", 1, OptObjPath },
1046 { "--register-space", 1, OptRegisterSpace },
1047 { "--register-vars", 0, OptRegisterVars },
1048 { "--rodata-name", 1, OptRodataName },
1049 { "--signed-chars", 0, OptSignedChars },
1050 { "--start-addr", 1, OptStartAddr },
1051 { "--static-locals", 0, OptStaticLocals },
1052 { "--target", 1, OptTarget },
1053 { "--verbose", 0, OptVerbose },
1054 { "--version", 0, OptVersion },
1055 { "--zeropage-label", 1, OptZeropageLabel },
1056 { "--zeropage-name", 1, OptZeropageName },
1061 /* Initialize the cmdline module */
1062 InitCmdLine (&argc, &argv, "cl65");
1064 /* Initialize the command descriptors */
1065 CmdInit (&CC65, "cc65");
1066 CmdInit (&CA65, "ca65");
1067 CmdInit (&CO65, "co65");
1068 CmdInit (&LD65, "ld65");
1069 CmdInit (&GRC, "grc");
1071 /* Our default target is the C64 instead of "none" */
1074 /* Check the parameters */
1076 while (I < ArgCount) {
1078 /* Get the argument */
1079 const char* Arg = ArgVec[I];
1081 /* Check for an option */
1082 if (Arg [0] == '-') {
1087 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
1091 /* Strict ANSI mode (compiler) */
1096 if (Arg[2] == 'l' && Arg[3] == '\0') {
1097 /* Make local variables static */
1098 OptStaticLocals (Arg, 0);
1100 /* Specify linker config file */
1101 LinkerConfig = GetArg (&I, 2);
1106 /* Define a preprocessor symbol (compiler) */
1107 CmdAddArg2 (&CC65, "-D", GetArg (&I, 2));
1111 /* Include directory (compiler) */
1112 OptIncludeDir (Arg, GetArg (&I, 2));
1116 if (Arg[2] == 'n' && Arg[3] == '\0') {
1117 /* VICE label file (linker) */
1118 CmdAddArg2 (&LD65, "-Ln", GetArg (&I, 3));
1120 /* Library search path (linker) */
1121 OptLibPath (Arg, GetArg (&I, 2));
1126 /* Optimize code (compiler, also covers -Oi and others) */
1127 CmdAddArg (&CC65, Arg);
1131 /* Dont assemble and link the created files */
1132 DontLink = DontAssemble = 1;
1136 /* Include source as comment (compiler) */
1137 OptAddSource (Arg, 0);
1141 /* Print version number */
1142 OptVersion (Arg, 0);
1146 /* Suppress warnings - compiler and assembler */
1147 CmdAddArg (&CC65, "-W");
1148 CmdAddArg2 (&CA65, "-W", "0");
1152 /* Don't link the resulting files */
1157 /* Debug mode (compiler) */
1162 /* Debugging - add to compiler and assembler */
1163 OptDebugInfo (Arg, 0);
1168 /* Print help - cl65 */
1173 /* Default characters are signed */
1174 OptSignedChars (Arg, 0);
1178 /* Create an assembler listing */
1179 OptListing (Arg, 0);
1183 /* Create a map file (linker) */
1184 OptMapFile (Arg, GetArg (&I, 2));
1188 /* Name the output file */
1189 OutputName = GetArg (&I, 2);
1193 /* Enable register variables */
1194 OptRegisterVars (Arg, 0);
1198 /* Set target system - compiler, assembler and linker */
1199 OptTarget (Arg, GetArg (&I, 2));
1203 if (Arg [2] == 'm') {
1204 /* Verbose map file (linker) */
1205 CmdAddArg (&LD65, "-vm");
1207 /* Verbose mode (compiler, assembler, linker) */
1208 OptVerbose (Arg, 0);
1213 UnknownOption (Arg);
1217 /* Remember the first file name */
1218 if (FirstInput == 0) {
1222 /* Determine the file type by the extension */
1223 switch (GetFileType (Arg)) {
1226 /* Compile the file */
1231 /* Assemble the file */
1232 if (!DontAssemble) {
1239 /* Add to the linker files */
1240 CmdAddFile (&LD65, Arg);
1244 /* Add to the resource compiler files */
1249 /* Add the the object file converter files */
1254 Error ("Don't know what to do with `%s'", Arg);
1264 /* Check if we had any input files */
1265 if (FirstInput == 0) {
1266 Warning ("No input files");
1269 /* Link the given files if requested and if we have any */
1270 if (DontLink == 0 && LD65.FileCount > 0) {
1274 /* Return an apropriate exit code */
1275 return EXIT_SUCCESS;