1 /*****************************************************************************/
5 /* Main module for the cl65 compile and link utility */
9 /* (C) 1999-2005, 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__)
73 /*****************************************************************************/
75 /*****************************************************************************/
79 /* Struct that describes a command */
80 typedef struct CmdDesc CmdDesc;
82 char* Name; /* The command name */
84 unsigned ArgCount; /* Count of arguments */
85 unsigned ArgMax; /* Maximum count of arguments */
86 char** Args; /* The arguments */
88 unsigned FileCount; /* Count of files to translate */
89 unsigned FileMax; /* Maximum count of files */
90 char** Files; /* The files */
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 };
100 /* Variables controlling the steps we're doing */
101 static int DontLink = 0;
102 static int DontAssemble = 0;
104 /* The name of the output file, NULL if none given */
105 static const char* OutputName = 0;
107 /* The name of the linker configuration file if given */
108 static const char* LinkerConfig = 0;
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.
113 static const char* FirstInput = 0;
115 /* Remember if we should link a module */
116 static int Module = 0;
118 /* Extension used for a module */
119 #define MODULE_EXT ".o65"
121 /* Name of the crt0 object file and the runtime library */
122 static char* TargetCRT0 = 0;
123 static char* TargetLib = 0;
127 /*****************************************************************************/
128 /* Include the system specific spawn function */
129 /*****************************************************************************/
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"
139 # error "Don't know which spawn module to include!"
145 /*****************************************************************************/
146 /* Command structure handling */
147 /*****************************************************************************/
151 static void CmdExpand (CmdDesc* Cmd)
152 /* Expand the argument vector */
154 unsigned NewMax = Cmd->ArgMax + 10;
155 char** NewArgs = xmalloc (NewMax * sizeof (char*));
156 memcpy (NewArgs, Cmd->Args, Cmd->ArgMax * sizeof (char*));
159 Cmd->ArgMax = NewMax;
164 static void CmdAddArg (CmdDesc* Cmd, const char* Arg)
165 /* Add a new argument to the command */
167 /* Expand the argument vector if needed */
168 if (Cmd->ArgCount >= Cmd->ArgMax) {
172 /* Add a copy of the new argument, allow a NULL pointer */
174 Cmd->Args[Cmd->ArgCount++] = xstrdup (Arg);
176 Cmd->Args[Cmd->ArgCount++] = 0;
182 static void CmdAddArg2 (CmdDesc* Cmd, const char* Arg1, const char* Arg2)
183 /* Add a new argument pair to the command */
185 CmdAddArg (Cmd, Arg1);
186 CmdAddArg (Cmd, Arg2);
191 static void CmdAddArgList (CmdDesc* Cmd, const char* ArgList)
192 /* Add a list of arguments separated by commas */
194 const char* Arg = ArgList;
198 if (*P == '\0' || *P == ',') {
200 /* End of argument, add it */
201 unsigned Len = P - Arg;
203 /* Expand the argument vector if needed */
204 if (Cmd->ArgCount >= Cmd->ArgMax) {
208 /* Add the new argument */
209 Cmd->Args[Cmd->ArgCount] = memcpy (xmalloc (Len + 1), Arg, Len);
210 Cmd->Args[Cmd->ArgCount][Len] = '\0';
213 /* If the argument was terminated by a comma, skip it, otherwise
217 /* Start over at next char */
223 /* Skip other chars */
232 static void CmdDelArgs (CmdDesc* Cmd, unsigned LastValid)
233 /* Remove all arguments with an index greater than LastValid */
235 while (Cmd->ArgCount > LastValid) {
237 xfree (Cmd->Args [Cmd->ArgCount]);
238 Cmd->Args [Cmd->ArgCount] = 0;
244 static void CmdAddFile (CmdDesc* Cmd, const char* File)
245 /* Add a new file to the command */
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*));
253 Cmd->Files = NewFiles;
254 Cmd->FileMax = NewMax;
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
264 for (I = 0; I < Cmd->FileCount; ++I) {
265 if (strcmp (Cmd->Files[I], File) == 0) {
267 Warning ("Duplicate file in argument list: `%s'", File);
268 /* No need to search further */
274 Cmd->Files [Cmd->FileCount++] = xstrdup (File);
276 /* Add a NULL pointer */
277 Cmd->Files [Cmd->FileCount++] = 0;
283 static void CmdInit (CmdDesc* Cmd, const char* Path)
284 /* Initialize the command using the given path to the executable */
286 /* Remember the command */
287 Cmd->Name = xstrdup (Path);
289 /* Use the command name as first argument */
290 CmdAddArg (Cmd, Path);
295 static void CmdSetOutput (CmdDesc* Cmd, const char* File)
296 /* Set the output file in a command desc */
298 CmdAddArg2 (Cmd, "-o", File);
303 static void CmdSetTarget (CmdDesc* Cmd, target_t Target)
304 /* Set the output file in a command desc */
306 CmdAddArg2 (Cmd, "-t", TargetNames[Target]);
311 static void CmdPrint (CmdDesc* Cmd, FILE* F)
312 /* Output the command line encoded in the command desc */
315 for (I = 0; I < Cmd->ArgCount && Cmd->Args[I] != 0; ++I) {
316 fprintf (F, "%s ", Cmd->Args[I]);
322 /*****************************************************************************/
323 /* Target handling */
324 /*****************************************************************************/
328 static void SetTargetFiles (void)
329 /* Set the target system files */
331 /* Determine the names of the default startup and library file */
332 if (Target != TGT_NONE) {
334 /* Get a pointer to the system name and its length */
335 const char* TargetName = TargetNames [Target];
336 unsigned TargetNameLen = strlen (TargetName);
338 /* Set the startup file */
339 TargetCRT0 = xmalloc (TargetNameLen + 2 + 1);
340 memcpy (TargetCRT0, TargetName, TargetNameLen);
341 strcpy (TargetCRT0 + TargetNameLen, ".o");
343 /* Set the library file */
344 TargetLib = xmalloc (TargetNameLen + 4 + 1);
345 memcpy (TargetLib, TargetName, TargetNameLen);
346 strcpy (TargetLib + TargetNameLen, ".lib");
353 /*****************************************************************************/
355 /*****************************************************************************/
359 static void ExecProgram (CmdDesc* Cmd)
360 /* Execute a subprocess with the given name/parameters. Exit on errors. */
364 /* If in debug mode, output the command line we will execute */
366 printf ("Executing: ");
367 CmdPrint (Cmd, stdout);
371 /* Call the program */
372 Status = spawnvp (P_WAIT, Cmd->Name, Cmd->Args);
374 /* Check the result code */
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 */
386 static void Link (void)
387 /* Link the resulting executable */
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.
396 Error ("Cannot use -C and --module together");
398 CmdAddArg2 (&LD65, "-C", LinkerConfig);
400 CmdSetTarget (&LD65, TGT_MODULE);
402 CmdSetTarget (&LD65, Target);
405 /* Determine which target libraries are needed */
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.
414 CmdSetOutput (&LD65, OutputName);
416 } else if (FirstInput && FindExt (FirstInput)) { /* Only if ext present! */
418 const char* Extension = Module? MODULE_EXT : "";
419 char* Output = MakeFilename (FirstInput, Extension);
420 CmdSetOutput (&LD65, Output);
425 /* If we have a startup file and if we are not linking a module, add its
426 * name as a parameter
428 if (TargetCRT0 && !Module) {
429 CmdAddArg (&LD65, TargetCRT0);
432 /* Add all object files as parameters */
433 for (I = 0; I < LD65.FileCount; ++I) {
434 CmdAddArg (&LD65, LD65.Files [I]);
437 /* Add the system runtime library */
439 CmdAddArg (&LD65, TargetLib);
442 /* Terminate the argument list with a NULL pointer */
443 CmdAddArg (&LD65, 0);
445 /* Call the linker */
451 static void Assemble (const char* File)
452 /* Assemble the given file */
454 /* Remember the current assembler argument count */
455 unsigned ArgCount = CA65.ArgCount;
457 /* Set the target system */
458 CmdSetTarget (&CA65, Target);
460 /* If we won't link, this is the final step. In this case, set the
463 if (DontLink && OutputName) {
464 CmdSetOutput (&CA65, OutputName);
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
470 char* ObjName = MakeFilename (File, ".o");
471 CmdAddFile (&LD65, ObjName);
475 /* Add the file as argument for the assembler */
476 CmdAddArg (&CA65, File);
478 /* Add a NULL pointer to terminate the argument list */
479 CmdAddArg (&CA65, 0);
481 /* Run the assembler */
484 /* Remove the excess arguments */
485 CmdDelArgs (&CA65, ArgCount);
490 static void Compile (const char* File)
491 /* Compile the given file */
495 /* Remember the current compiler argument count */
496 unsigned ArgCount = CC65.ArgCount;
498 /* Set the target system */
499 CmdSetTarget (&CC65, Target);
501 /* If we won't link, this is the final step. In this case, set the
504 if (DontAssemble && OutputName) {
505 CmdSetOutput (&CC65, OutputName);
507 /* The assembler file name will be the name of the source file
508 * with .c replaced by ".s".
510 AsmName = MakeFilename (File, ".s");
513 /* Add the file as argument for the compiler */
514 CmdAddArg (&CC65, File);
516 /* Add a NULL pointer to terminate the argument list */
517 CmdAddArg (&CC65, 0);
519 /* Run the compiler */
522 /* Remove the excess arguments */
523 CmdDelArgs (&CC65, ArgCount);
525 /* If this is not the final step, assemble the generated file, then
530 if (remove (AsmName) < 0) {
531 Warning ("Cannot remove temporary file `%s': %s",
532 AsmName, strerror (errno));
540 static void CompileRes (const char* File)
541 /* Compile the given geos resource file */
545 /* Remember the current assembler argument count */
546 unsigned ArgCount = GRC.ArgCount;
548 /* The assembler file name will be the name of the source file
549 * with .grc replaced by ".s".
551 AsmName = MakeFilename (File, ".s");
553 /* Add the file as argument for the resource compiler */
554 CmdAddArg (&GRC, File);
556 /* Add a NULL pointer to terminate the argument list */
559 /* Run the compiler */
562 /* Remove the excess arguments */
563 CmdDelArgs (&GRC, ArgCount);
565 /* If this is not the final step, assemble the generated file, then
570 if (remove (AsmName) < 0) {
571 Warning ("Cannot remove temporary file `%s': %s",
572 AsmName, strerror (errno));
576 /* Free the assembler file name which was allocated from the heap */
582 static void ConvertO65 (const char* File)
583 /* Convert an o65 object file into an assembler file */
587 /* Remember the current converter argument count */
588 unsigned ArgCount = CO65.ArgCount;
590 /* If we won't link, this is the final step. In this case, set the
593 if (DontAssemble && OutputName) {
594 CmdSetOutput (&CO65, OutputName);
596 /* The assembler file name will be the name of the source file
597 * with .c replaced by ".s".
599 AsmName = MakeFilename (File, ".s");
602 /* Add the file as argument for the object file converter */
603 CmdAddArg (&CO65, File);
605 /* Add a NULL pointer to terminate the argument list */
606 CmdAddArg (&CO65, 0);
608 /* Run the converter */
611 /* Remove the excess arguments */
612 CmdDelArgs (&CO65, ArgCount);
614 /* If this is not the final step, assemble the generated file, then
619 if (remove (AsmName) < 0) {
620 Warning ("Cannot remove temporary file `%s': %s",
621 AsmName, strerror (errno));
625 /* Free the assembler file name which was allocated from the heap */
631 /*****************************************************************************/
633 /*****************************************************************************/
637 static void Usage (void)
638 /* Print usage information and exit */
640 printf ("Usage: %s [options] file [...]\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"
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",
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 */
724 CmdAddArg (&CC65, "-T");
729 static void OptAsmArgs (const char* Opt attribute ((unused)), const char* Arg)
730 /* Pass arguments to the assembler */
732 CmdAddArgList (&CA65, Arg);
737 static void OptAsmDefine (const char* Opt attribute ((unused)), const char* Arg)
738 /* Define an assembler symbol (assembler) */
740 CmdAddArg2 (&CA65, "-D", Arg);
745 static void OptAsmIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
746 /* Include directory (assembler) */
748 CmdAddArg2 (&CA65, "-I", Arg);
753 static void OptBssLabel (const char* Opt attribute ((unused)), const char* Arg)
754 /* Handle the --bss-label option */
756 CmdAddArg2 (&CO65, "--bss-label", Arg);
761 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
762 /* Handle the --bss-name option */
764 CmdAddArg2 (&CC65, "--bss-name", Arg);
765 CmdAddArg2 (&CO65, "--bss-name", Arg);
770 static void OptCfgPath (const char* Opt attribute ((unused)), const char* Arg)
771 /* Config file search path (linker) */
773 CmdAddArg2 (&LD65, "--cfg-path", Arg);
778 static void OptCheckStack (const char* Opt attribute ((unused)),
779 const char* Arg attribute ((unused)))
780 /* Handle the --check-stack option */
782 CmdAddArg (&CC65, "--check-stack");
787 static void OptCodeLabel (const char* Opt attribute ((unused)), const char* Arg)
788 /* Handle the --code-label option */
790 CmdAddArg2 (&CO65, "--code-label", Arg);
795 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
796 /* Handle the --code-name option */
798 CmdAddArg2 (&CC65, "--code-name", Arg);
799 CmdAddArg2 (&CO65, "--code-name", Arg);
804 static void OptCodeSize (const char* Opt attribute ((unused)), const char* Arg)
805 /* Handle the --codesize option */
807 CmdAddArg2 (&CC65, "--codesize", Arg);
812 static void OptConfig (const char* Opt attribute ((unused)), const char* Arg)
813 /* Config file (linker) */
816 Error ("Cannot specify -C/--config twice");
823 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
824 /* Handle the --cpu option */
826 /* Add the cpu type to the assembler and compiler */
827 CmdAddArg2 (&CA65, "--cpu", Arg);
828 CmdAddArg2 (&CC65, "--cpu", Arg);
833 static void OptCreateDep (const char* Opt attribute ((unused)),
834 const char* Arg attribute ((unused)))
835 /* Handle the --create-dep option */
837 CmdAddArg (&CC65, "--create-dep");
842 static void OptDataLabel (const char* Opt attribute ((unused)), const char* Arg)
843 /* Handle the --data-label option */
845 CmdAddArg2 (&CO65, "--data-label", Arg);
850 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
851 /* Handle the --data-name option */
853 CmdAddArg2 (&CC65, "--data-name", Arg);
854 CmdAddArg2 (&CO65, "--data-name", Arg);
859 static void OptDebug (const char* Opt attribute ((unused)),
860 const char* Arg attribute ((unused)))
861 /* Debug mode (compiler and cl65 utility) */
863 CmdAddArg (&CC65, "-d");
864 CmdAddArg (&CO65, "-d");
870 static void OptDebugInfo (const char* Opt attribute ((unused)),
871 const char* Arg attribute ((unused)))
872 /* Debug Info - add to compiler and assembler */
874 CmdAddArg (&CC65, "-g");
875 CmdAddArg (&CA65, "-g");
876 CmdAddArg (&CO65, "-g");
881 static void OptFeature (const char* Opt attribute ((unused)), const char* Arg)
882 /* Emulation features for the assembler */
884 CmdAddArg2 (&CA65, "--feature", Arg);
889 static void OptForgetIncPaths (const char* Opt attribute ((unused)),
890 const char* Arg attribute ((unused)))
891 /* Forget all currently defined include paths */
893 CmdAddArg (&CC65, "--forget-inc-paths");
898 static void OptHelp (const char* Opt attribute ((unused)),
899 const char* Arg attribute ((unused)))
900 /* Print help - cl65 */
908 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
909 /* Include directory (compiler) */
911 CmdAddArg2 (&CC65, "-I", Arg);
916 static void OptLdArgs (const char* Opt attribute ((unused)), const char* Arg)
917 /* Pass arguments to the linker */
919 CmdAddArgList (&LD65, Arg);
924 static void OptLib (const char* Opt attribute ((unused)), const char* Arg)
925 /* Library file follows (linker) */
927 CmdAddArg2 (&LD65, "--lib", Arg);
932 static void OptLibPath (const char* Opt attribute ((unused)), const char* Arg)
933 /* Library search path (linker) */
935 CmdAddArg2 (&LD65, "--lib-path", Arg);
940 static void OptListBytes (const char* Opt attribute ((unused)), const char* Arg)
941 /* Set the maximum number of bytes per asm listing line */
943 CmdAddArg2 (&CA65, "--list-bytes", Arg);
948 static void OptListing (const char* Opt attribute ((unused)),
949 const char* Arg attribute ((unused)))
950 /* Create an assembler listing */
952 CmdAddArg (&CA65, "-l");
957 static void OptListTargets (const char* Opt attribute ((unused)),
958 const char* Arg attribute ((unused)))
959 /* List all targets */
963 /* List the targets */
964 for (I = TGT_NONE; I < TGT_COUNT; ++I) {
965 printf ("%s\n", TargetNames[I]);
974 static void OptMapFile (const char* Opt attribute ((unused)), const char* Arg)
975 /* Create a map file */
977 /* Create a map file (linker) */
978 CmdAddArg2 (&LD65, "-m", Arg);
983 static void OptMemoryModel (const char* Opt attribute ((unused)), const char* Arg)
984 /* Set the memory model */
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);
992 CmdAddArg2 (&CA65, "-mm", Arg);
993 CmdAddArg2 (&CC65, "-mm", Arg);
999 static void OptModule (const char* Opt attribute ((unused)),
1000 const char* Arg attribute ((unused)))
1001 /* Link as a module */
1008 static void OptModuleId (const char* Opt attribute ((unused)), const char* Arg)
1009 /* Specify a module if for the linker */
1011 /* Pass it straight to the linker */
1012 CmdAddArg2 (&LD65, "--module-id", Arg);
1017 static void OptO65Model (const char* Opt attribute ((unused)), const char* Arg)
1018 /* Handle the --o65-model option */
1020 CmdAddArg2 (&CO65, "-m", Arg);
1025 static void OptObj (const char* Opt attribute ((unused)), const char* Arg)
1026 /* Object file follows (linker) */
1028 CmdAddArg2 (&LD65, "--obj", Arg);
1033 static void OptObjPath (const char* Opt attribute ((unused)), const char* Arg)
1034 /* Object file search path (linker) */
1036 CmdAddArg2 (&LD65, "--obj-path", Arg);
1041 static void OptRegisterSpace (const char* Opt attribute ((unused)), const char* Arg)
1042 /* Handle the --register-space option */
1044 CmdAddArg2 (&CC65, "--register-space", Arg);
1049 static void OptRegisterVars (const char* Opt attribute ((unused)),
1050 const char* Arg attribute ((unused)))
1051 /* Handle the --register-vars option */
1053 CmdAddArg (&CC65, "-r");
1058 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
1059 /* Handle the --rodata-name option */
1061 CmdAddArg2 (&CC65, "--rodata-name", Arg);
1066 static void OptSignedChars (const char* Opt attribute ((unused)),
1067 const char* Arg attribute ((unused)))
1068 /* Make default characters signed */
1070 CmdAddArg (&CC65, "-j");
1075 static void OptStandard (const char* Opt attribute ((unused)), const char* Arg)
1076 /* Set the language standard */
1078 CmdAddArg2 (&CC65, "--standard", Arg);
1083 static void OptStartAddr (const char* Opt attribute ((unused)), const char* Arg)
1084 /* Set the default start address */
1086 CmdAddArg2 (&LD65, "-S", Arg);
1091 static void OptStaticLocals (const char* Opt attribute ((unused)),
1092 const char* Arg attribute ((unused)))
1093 /* Place local variables in static storage */
1095 CmdAddArg (&CC65, "-Cl");
1100 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
1101 /* Set the target system */
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");
1113 static void OptVerbose (const char* Opt attribute ((unused)),
1114 const char* Arg attribute ((unused)))
1115 /* Verbose mode (compiler, assembler, linker) */
1117 CmdAddArg (&CC65, "-v");
1118 CmdAddArg (&CA65, "-v");
1119 CmdAddArg (&CO65, "-v");
1120 CmdAddArg (&LD65, "-v");
1125 static void OptVersion (const char* Opt attribute ((unused)),
1126 const char* Arg attribute ((unused)))
1127 /* Print version number */
1130 "cl65 V%u.%u.%u - (C) Copyright 1998-2003 Ullrich von Bassewitz\n",
1131 VER_MAJOR, VER_MINOR, VER_PATCH);
1136 static void OptZeropageLabel (const char* Opt attribute ((unused)), const char* Arg)
1137 /* Handle the --zeropage-label option */
1139 CmdAddArg2 (&CO65, "--zeropage-label", Arg);
1144 static void OptZeropageName (const char* Opt attribute ((unused)), const char* Arg)
1145 /* Handle the --zeropage-name option */
1147 CmdAddArg2 (&CO65, "--zeropage-name", Arg);
1152 int main (int argc, char* argv [])
1153 /* Utility main program */
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 },
1208 /* Initialize the cmdline module */
1209 InitCmdLine (&argc, &argv, "cl65");
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");
1218 /* Our default target is the C64 instead of "none" */
1221 /* Check the parameters */
1223 while (I < ArgCount) {
1225 /* Get the argument */
1226 const char* Arg = ArgVec[I];
1228 /* Check for an option */
1229 if (Arg [0] == '-') {
1234 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
1238 if (Arg[2] == 'l' && Arg[3] == '\0') {
1239 /* Make local variables static */
1240 OptStaticLocals (Arg, 0);
1242 /* Specify linker config file */
1243 OptConfig (Arg, GetArg (&I, 2));
1248 /* Define a preprocessor symbol (compiler) */
1249 CmdAddArg2 (&CC65, "-D", GetArg (&I, 2));
1253 /* Include directory (compiler) */
1254 OptIncludeDir (Arg, GetArg (&I, 2));
1258 if (Arg[2] == 'n' && Arg[3] == '\0') {
1259 /* VICE label file (linker) */
1260 CmdAddArg2 (&LD65, "-Ln", GetArg (&I, 3));
1262 /* Library search path (linker) */
1263 OptLibPath (Arg, GetArg (&I, 2));
1268 /* Optimize code (compiler, also covers -Oi and others) */
1269 CmdAddArg (&CC65, Arg);
1273 /* Dont assemble and link the created files */
1274 DontLink = DontAssemble = 1;
1278 /* Include source as comment (compiler) */
1279 OptAddSource (Arg, 0);
1283 /* Print version number */
1284 OptVersion (Arg, 0);
1291 OptAsmArgs (Arg, GetArg (&I, 3));
1295 OptLdArgs (Arg, GetArg (&I, 3));
1299 /* Suppress warnings - compiler and assembler */
1300 CmdAddArg (&CC65, "-W");
1301 CmdAddArg2 (&CA65, "-W", "0");
1305 UnknownOption (Arg);
1311 /* Don't link the resulting files */
1316 /* Debug mode (compiler) */
1321 /* Debugging - add to compiler and assembler */
1322 OptDebugInfo (Arg, 0);
1327 /* Print help - cl65 */
1332 /* Default characters are signed */
1333 OptSignedChars (Arg, 0);
1337 /* Create an assembler listing */
1338 OptListing (Arg, 0);
1342 /* Create a map file (linker) */
1343 OptMapFile (Arg, GetArg (&I, 2));
1347 /* Name the output file */
1348 OutputName = GetArg (&I, 2);
1352 /* Enable register variables */
1353 OptRegisterVars (Arg, 0);
1357 /* Set target system - compiler, assembler and linker */
1358 OptTarget (Arg, GetArg (&I, 2));
1362 if (Arg [2] == 'm') {
1363 /* Verbose map file (linker) */
1364 CmdAddArg (&LD65, "-vm");
1366 /* Verbose mode (compiler, assembler, linker) */
1367 OptVerbose (Arg, 0);
1372 UnknownOption (Arg);
1376 /* Remember the first file name */
1377 if (FirstInput == 0) {
1381 /* Determine the file type by the extension */
1382 switch (GetFileType (Arg)) {
1385 /* Compile the file */
1390 /* Assemble the file */
1391 if (!DontAssemble) {
1398 /* Add to the linker files */
1399 CmdAddFile (&LD65, Arg);
1403 /* Add to the resource compiler files */
1408 /* Add the the object file converter files */
1413 Error ("Don't know what to do with `%s'", Arg);
1423 /* Check if we had any input files */
1424 if (FirstInput == 0) {
1425 Warning ("No input files");
1428 /* Link the given files if requested and if we have any */
1429 if (DontLink == 0 && LD65.FileCount > 0) {
1433 /* Return an apropriate exit code */
1434 return EXIT_SUCCESS;