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 target specific runtime library */
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 CmdExpand (CmdDesc* Cmd)
151 /* Expand the argument vector */
153 unsigned NewMax = Cmd->ArgMax + 10;
154 char** NewArgs = xmalloc (NewMax * sizeof (char*));
155 memcpy (NewArgs, Cmd->Args, Cmd->ArgMax * sizeof (char*));
158 Cmd->ArgMax = NewMax;
163 static void CmdAddArg (CmdDesc* Cmd, const char* Arg)
164 /* Add a new argument to the command */
166 /* Expand the argument vector if needed */
167 if (Cmd->ArgCount >= Cmd->ArgMax) {
171 /* Add a copy of the new argument, allow a NULL pointer */
173 Cmd->Args[Cmd->ArgCount++] = xstrdup (Arg);
175 Cmd->Args[Cmd->ArgCount++] = 0;
181 static void CmdAddArg2 (CmdDesc* Cmd, const char* Arg1, const char* Arg2)
182 /* Add a new argument pair to the command */
184 CmdAddArg (Cmd, Arg1);
185 CmdAddArg (Cmd, Arg2);
190 static void CmdAddArgList (CmdDesc* Cmd, const char* ArgList)
191 /* Add a list of arguments separated by commas */
193 const char* Arg = ArgList;
197 if (*P == '\0' || *P == ',') {
199 /* End of argument, add it */
200 unsigned Len = P - Arg;
202 /* Expand the argument vector if needed */
203 if (Cmd->ArgCount >= Cmd->ArgMax) {
207 /* Add the new argument */
208 Cmd->Args[Cmd->ArgCount] = memcpy (xmalloc (Len + 1), Arg, Len);
209 Cmd->Args[Cmd->ArgCount][Len] = '\0';
212 /* If the argument was terminated by a comma, skip it, otherwise
216 /* Start over at next char */
222 /* Skip other chars */
231 static void CmdDelArgs (CmdDesc* Cmd, unsigned LastValid)
232 /* Remove all arguments with an index greater than LastValid */
234 while (Cmd->ArgCount > LastValid) {
236 xfree (Cmd->Args [Cmd->ArgCount]);
237 Cmd->Args [Cmd->ArgCount] = 0;
243 static void CmdAddFile (CmdDesc* Cmd, const char* File)
244 /* Add a new file to the command */
246 /* Expand the file vector if needed */
247 if (Cmd->FileCount == Cmd->FileMax) {
248 unsigned NewMax = Cmd->FileMax + 10;
249 char** NewFiles = xmalloc (NewMax * sizeof (char*));
250 memcpy (NewFiles, Cmd->Files, Cmd->FileMax * sizeof (char*));
252 Cmd->Files = NewFiles;
253 Cmd->FileMax = NewMax;
256 /* If the file name is not NULL (which is legal and is used to terminate
257 * the file list), check if the file name does already exist in the file
258 * list and print a warning if so. Regardless of the search result, add
263 for (I = 0; I < Cmd->FileCount; ++I) {
264 if (strcmp (Cmd->Files[I], File) == 0) {
266 Warning ("Duplicate file in argument list: `%s'", File);
267 /* No need to search further */
273 Cmd->Files [Cmd->FileCount++] = xstrdup (File);
275 /* Add a NULL pointer */
276 Cmd->Files [Cmd->FileCount++] = 0;
282 static void CmdInit (CmdDesc* Cmd, const char* Path)
283 /* Initialize the command using the given path to the executable */
285 /* Remember the command */
286 Cmd->Name = xstrdup (Path);
288 /* Use the command name as first argument */
289 CmdAddArg (Cmd, Path);
294 static void CmdSetOutput (CmdDesc* Cmd, const char* File)
295 /* Set the output file in a command desc */
297 CmdAddArg2 (Cmd, "-o", File);
302 static void CmdSetTarget (CmdDesc* Cmd, target_t Target)
303 /* Set the output file in a command desc */
305 CmdAddArg2 (Cmd, "-t", TargetNames[Target]);
310 static void CmdPrint (CmdDesc* Cmd, FILE* F)
311 /* Output the command line encoded in the command desc */
314 for (I = 0; I < Cmd->ArgCount && Cmd->Args[I] != 0; ++I) {
315 fprintf (F, "%s ", Cmd->Args[I]);
321 /*****************************************************************************/
322 /* Target handling */
323 /*****************************************************************************/
327 static void SetTargetFiles (void)
328 /* Set the target system files */
330 /* Determine the names of the target specific library file */
331 if (Target != TGT_NONE) {
333 /* Get a pointer to the system name and its length */
334 const char* TargetName = TargetNames [Target];
335 unsigned TargetNameLen = strlen (TargetName);
337 /* Set the library file */
338 TargetLib = xmalloc (TargetNameLen + 4 + 1);
339 memcpy (TargetLib, TargetName, TargetNameLen);
340 strcpy (TargetLib + TargetNameLen, ".lib");
347 /*****************************************************************************/
349 /*****************************************************************************/
353 static void ExecProgram (CmdDesc* Cmd)
354 /* Execute a subprocess with the given name/parameters. Exit on errors. */
358 /* If in debug mode, output the command line we will execute */
360 printf ("Executing: ");
361 CmdPrint (Cmd, stdout);
365 /* Call the program */
366 Status = spawnvp (P_WAIT, Cmd->Name, Cmd->Args);
368 /* Check the result code */
370 /* Error executing the program */
371 Error ("Cannot execute `%s': %s", Cmd->Name, strerror (errno));
372 } else if (Status != 0) {
373 /* Called program had an error */
380 static void Link (void)
381 /* Link the resulting executable */
385 /* If we have a linker config file given, add it to the command line.
386 * Otherwise pass the target to the linker if we have one.
390 Error ("Cannot use -C and --module together");
392 CmdAddArg2 (&LD65, "-C", LinkerConfig);
394 CmdSetTarget (&LD65, TGT_MODULE);
396 CmdSetTarget (&LD65, Target);
399 /* Determine which target libraries are needed */
402 /* Since linking is always the final step, if we have an output file name
403 * given, set it here. If we don't have an explicit output name given,
404 * try to build one from the name of the first input file.
408 CmdSetOutput (&LD65, OutputName);
410 } else if (FirstInput && FindExt (FirstInput)) { /* Only if ext present! */
412 const char* Extension = Module? MODULE_EXT : "";
413 char* Output = MakeFilename (FirstInput, Extension);
414 CmdSetOutput (&LD65, Output);
419 /* Add all object files as parameters */
420 for (I = 0; I < LD65.FileCount; ++I) {
421 CmdAddArg (&LD65, LD65.Files [I]);
424 /* Add the system runtime library */
426 CmdAddArg (&LD65, TargetLib);
429 /* Terminate the argument list with a NULL pointer */
430 CmdAddArg (&LD65, 0);
432 /* Call the linker */
438 static void Assemble (const char* File)
439 /* Assemble the given file */
441 /* Remember the current assembler argument count */
442 unsigned ArgCount = CA65.ArgCount;
444 /* Set the target system */
445 CmdSetTarget (&CA65, Target);
447 /* If we won't link, this is the final step. In this case, set the
450 if (DontLink && OutputName) {
451 CmdSetOutput (&CA65, OutputName);
453 /* The object file name will be the name of the source file
454 * with .s replaced by ".o". Add this file to the list of
457 char* ObjName = MakeFilename (File, ".o");
458 CmdAddFile (&LD65, ObjName);
462 /* Add the file as argument for the assembler */
463 CmdAddArg (&CA65, File);
465 /* Add a NULL pointer to terminate the argument list */
466 CmdAddArg (&CA65, 0);
468 /* Run the assembler */
471 /* Remove the excess arguments */
472 CmdDelArgs (&CA65, ArgCount);
477 static void Compile (const char* File)
478 /* Compile the given file */
482 /* Remember the current compiler argument count */
483 unsigned ArgCount = CC65.ArgCount;
485 /* Set the target system */
486 CmdSetTarget (&CC65, Target);
488 /* If we won't link, this is the final step. In this case, set the
491 if (DontAssemble && OutputName) {
492 CmdSetOutput (&CC65, OutputName);
494 /* The assembler file name will be the name of the source file
495 * with .c replaced by ".s".
497 AsmName = MakeFilename (File, ".s");
500 /* Add the file as argument for the compiler */
501 CmdAddArg (&CC65, File);
503 /* Add a NULL pointer to terminate the argument list */
504 CmdAddArg (&CC65, 0);
506 /* Run the compiler */
509 /* Remove the excess arguments */
510 CmdDelArgs (&CC65, ArgCount);
512 /* If this is not the final step, assemble the generated file, then
517 if (remove (AsmName) < 0) {
518 Warning ("Cannot remove temporary file `%s': %s",
519 AsmName, strerror (errno));
527 static void CompileRes (const char* File)
528 /* Compile the given geos resource file */
532 /* Remember the current assembler argument count */
533 unsigned ArgCount = GRC.ArgCount;
535 /* The assembler file name will be the name of the source file
536 * with .grc replaced by ".s".
538 AsmName = MakeFilename (File, ".s");
540 /* Add the file as argument for the resource compiler */
541 CmdAddArg (&GRC, File);
543 /* Add a NULL pointer to terminate the argument list */
546 /* Run the compiler */
549 /* Remove the excess arguments */
550 CmdDelArgs (&GRC, ArgCount);
552 /* If this is not the final step, assemble the generated file, then
557 if (remove (AsmName) < 0) {
558 Warning ("Cannot remove temporary file `%s': %s",
559 AsmName, strerror (errno));
563 /* Free the assembler file name which was allocated from the heap */
569 static void ConvertO65 (const char* File)
570 /* Convert an o65 object file into an assembler file */
574 /* Remember the current converter argument count */
575 unsigned ArgCount = CO65.ArgCount;
577 /* If we won't link, this is the final step. In this case, set the
580 if (DontAssemble && OutputName) {
581 CmdSetOutput (&CO65, OutputName);
583 /* The assembler file name will be the name of the source file
584 * with .c replaced by ".s".
586 AsmName = MakeFilename (File, ".s");
589 /* Add the file as argument for the object file converter */
590 CmdAddArg (&CO65, File);
592 /* Add a NULL pointer to terminate the argument list */
593 CmdAddArg (&CO65, 0);
595 /* Run the converter */
598 /* Remove the excess arguments */
599 CmdDelArgs (&CO65, ArgCount);
601 /* If this is not the final step, assemble the generated file, then
606 if (remove (AsmName) < 0) {
607 Warning ("Cannot remove temporary file `%s': %s",
608 AsmName, strerror (errno));
612 /* Free the assembler file name which was allocated from the heap */
618 /*****************************************************************************/
620 /*****************************************************************************/
624 static void Usage (void)
625 /* Print usage information and exit */
627 printf ("Usage: %s [options] file [...]\n"
629 " -c\t\t\tCompile and assemble but don't link\n"
630 " -d\t\t\tDebug mode\n"
631 " -g\t\t\tAdd debug info\n"
632 " -h\t\t\tHelp (this text)\n"
633 " -l\t\t\tCreate an assembler listing\n"
634 " -m name\t\tCreate a map file\n"
635 " -mm model\t\tSet the memory model\n"
636 " -o name\t\tName the output file\n"
637 " -r\t\t\tEnable register variables\n"
638 " -t sys\t\tSet the target system\n"
639 " -v\t\t\tVerbose mode\n"
640 " -vm\t\t\tVerbose map file\n"
641 " -C name\t\tUse linker config file\n"
642 " -Cl\t\t\tMake local variables static\n"
643 " -D sym[=defn]\t\tDefine a preprocessor symbol\n"
644 " -I dir\t\tSet a compiler include directory path\n"
645 " -L path\t\tSpecify a library search path\n"
646 " -Ln name\t\tCreate a VICE label file\n"
647 " -O\t\t\tOptimize code\n"
648 " -Oi\t\t\tOptimize code, inline functions\n"
649 " -Or\t\t\tOptimize code, honour the register keyword\n"
650 " -Os\t\t\tOptimize code, inline known C funtions\n"
651 " -S\t\t\tCompile but don't assemble and link\n"
652 " -T\t\t\tInclude source as comment\n"
653 " -V\t\t\tPrint the version number\n"
654 " -W\t\t\tSuppress warnings\n"
655 " -Wa options\t\tPass options to the assembler\n"
656 " -Wl options\t\tPass options to the linker\n"
659 " --add-source\t\tInclude source as comment\n"
660 " --asm-args options\tPass options to the assembler\n"
661 " --asm-define sym[=v]\tDefine an assembler symbol\n"
662 " --asm-include-dir dir\tSet an assembler include directory\n"
663 " --bss-label name\tDefine and export a BSS segment label\n"
664 " --bss-name seg\tSet the name of the BSS segment\n"
665 " --cfg-path path\tSpecify a config file search path\n"
666 " --check-stack\t\tGenerate stack overflow checks\n"
667 " --code-label name\tDefine and export a CODE segment label\n"
668 " --code-name seg\tSet the name of the CODE segment\n"
669 " --codesize x\t\tAccept larger code by factor x\n"
670 " --config name\t\tUse linker config file\n"
671 " --cpu type\t\tSet cpu type\n"
672 " --create-dep\t\tCreate a make dependency file\n"
673 " --data-label name\tDefine and export a DATA segment label\n"
674 " --data-name seg\tSet the name of the DATA segment\n"
675 " --debug\t\tDebug mode\n"
676 " --debug-info\t\tAdd debug info\n"
677 " --feature name\tSet an emulation feature\n"
678 " --forget-inc-paths\tForget include search paths (compiler)\n"
679 " --help\t\tHelp (this text)\n"
680 " --include-dir dir\tSet a compiler include directory path\n"
681 " --ld-args options\tPass options to the linker\n"
682 " --lib file\t\tLink this library\n"
683 " --lib-path path\tSpecify a library search path\n"
684 " --list-targets\tList all available targets\n"
685 " --listing\t\tCreate an assembler listing\n"
686 " --list-bytes n\tNumber of bytes per assembler listing line\n"
687 " --mapfile name\tCreate a map file\n"
688 " --memory-model model\tSet the memory model\n"
689 " --module\t\tLink as a module\n"
690 " --module-id id\tSpecify a module id for the linker\n"
691 " --o65-model model\tOverride the o65 model\n"
692 " --obj file\t\tLink this object file\n"
693 " --obj-path path\tSpecify an object file search path\n"
694 " --register-space b\tSet space available for register variables\n"
695 " --register-vars\tEnable register variables\n"
696 " --rodata-name seg\tSet the name of the RODATA segment\n"
697 " --signed-chars\tDefault characters are signed\n"
698 " --standard std\tLanguage standard (c89, c99, cc65)\n"
699 " --start-addr addr\tSet the default start address\n"
700 " --static-locals\tMake local variables static\n"
701 " --target sys\t\tSet the target system\n"
702 " --version\t\tPrint the version number\n"
703 " --verbose\t\tVerbose mode\n"
704 " --zeropage-label name\tDefine and export a ZEROPAGE segment label\n"
705 " --zeropage-name seg\tSet the name of the ZEROPAGE segment\n",
711 static void OptAddSource (const char* Opt attribute ((unused)),
712 const char* Arg attribute ((unused)))
713 /* Strict source code as comments to the generated asm code */
715 CmdAddArg (&CC65, "-T");
720 static void OptAsmArgs (const char* Opt attribute ((unused)), const char* Arg)
721 /* Pass arguments to the assembler */
723 CmdAddArgList (&CA65, Arg);
728 static void OptAsmDefine (const char* Opt attribute ((unused)), const char* Arg)
729 /* Define an assembler symbol (assembler) */
731 CmdAddArg2 (&CA65, "-D", Arg);
736 static void OptAsmIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
737 /* Include directory (assembler) */
739 CmdAddArg2 (&CA65, "-I", Arg);
744 static void OptBssLabel (const char* Opt attribute ((unused)), const char* Arg)
745 /* Handle the --bss-label option */
747 CmdAddArg2 (&CO65, "--bss-label", Arg);
752 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
753 /* Handle the --bss-name option */
755 CmdAddArg2 (&CC65, "--bss-name", Arg);
756 CmdAddArg2 (&CO65, "--bss-name", Arg);
761 static void OptCfgPath (const char* Opt attribute ((unused)), const char* Arg)
762 /* Config file search path (linker) */
764 CmdAddArg2 (&LD65, "--cfg-path", Arg);
769 static void OptCheckStack (const char* Opt attribute ((unused)),
770 const char* Arg attribute ((unused)))
771 /* Handle the --check-stack option */
773 CmdAddArg (&CC65, "--check-stack");
778 static void OptCodeLabel (const char* Opt attribute ((unused)), const char* Arg)
779 /* Handle the --code-label option */
781 CmdAddArg2 (&CO65, "--code-label", Arg);
786 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
787 /* Handle the --code-name option */
789 CmdAddArg2 (&CC65, "--code-name", Arg);
790 CmdAddArg2 (&CO65, "--code-name", Arg);
795 static void OptCodeSize (const char* Opt attribute ((unused)), const char* Arg)
796 /* Handle the --codesize option */
798 CmdAddArg2 (&CC65, "--codesize", Arg);
803 static void OptConfig (const char* Opt attribute ((unused)), const char* Arg)
804 /* Config file (linker) */
807 Error ("Cannot specify -C/--config twice");
814 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
815 /* Handle the --cpu option */
817 /* Add the cpu type to the assembler and compiler */
818 CmdAddArg2 (&CA65, "--cpu", Arg);
819 CmdAddArg2 (&CC65, "--cpu", Arg);
824 static void OptCreateDep (const char* Opt attribute ((unused)),
825 const char* Arg attribute ((unused)))
826 /* Handle the --create-dep option */
828 CmdAddArg (&CC65, "--create-dep");
833 static void OptDataLabel (const char* Opt attribute ((unused)), const char* Arg)
834 /* Handle the --data-label option */
836 CmdAddArg2 (&CO65, "--data-label", Arg);
841 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
842 /* Handle the --data-name option */
844 CmdAddArg2 (&CC65, "--data-name", Arg);
845 CmdAddArg2 (&CO65, "--data-name", Arg);
850 static void OptDebug (const char* Opt attribute ((unused)),
851 const char* Arg attribute ((unused)))
852 /* Debug mode (compiler and cl65 utility) */
854 CmdAddArg (&CC65, "-d");
855 CmdAddArg (&CO65, "-d");
861 static void OptDebugInfo (const char* Opt attribute ((unused)),
862 const char* Arg attribute ((unused)))
863 /* Debug Info - add to compiler and assembler */
865 CmdAddArg (&CC65, "-g");
866 CmdAddArg (&CA65, "-g");
867 CmdAddArg (&CO65, "-g");
872 static void OptFeature (const char* Opt attribute ((unused)), const char* Arg)
873 /* Emulation features for the assembler */
875 CmdAddArg2 (&CA65, "--feature", Arg);
880 static void OptForgetIncPaths (const char* Opt attribute ((unused)),
881 const char* Arg attribute ((unused)))
882 /* Forget all currently defined include paths */
884 CmdAddArg (&CC65, "--forget-inc-paths");
889 static void OptHelp (const char* Opt attribute ((unused)),
890 const char* Arg attribute ((unused)))
891 /* Print help - cl65 */
899 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
900 /* Include directory (compiler) */
902 CmdAddArg2 (&CC65, "-I", Arg);
907 static void OptLdArgs (const char* Opt attribute ((unused)), const char* Arg)
908 /* Pass arguments to the linker */
910 CmdAddArgList (&LD65, Arg);
915 static void OptLib (const char* Opt attribute ((unused)), const char* Arg)
916 /* Library file follows (linker) */
918 CmdAddArg2 (&LD65, "--lib", Arg);
923 static void OptLibPath (const char* Opt attribute ((unused)), const char* Arg)
924 /* Library search path (linker) */
926 CmdAddArg2 (&LD65, "--lib-path", Arg);
931 static void OptListBytes (const char* Opt attribute ((unused)), const char* Arg)
932 /* Set the maximum number of bytes per asm listing line */
934 CmdAddArg2 (&CA65, "--list-bytes", Arg);
939 static void OptListing (const char* Opt attribute ((unused)),
940 const char* Arg attribute ((unused)))
941 /* Create an assembler listing */
943 CmdAddArg (&CA65, "-l");
948 static void OptListTargets (const char* Opt attribute ((unused)),
949 const char* Arg attribute ((unused)))
950 /* List all targets */
954 /* List the targets */
955 for (I = TGT_NONE; I < TGT_COUNT; ++I) {
956 printf ("%s\n", TargetNames[I]);
965 static void OptMapFile (const char* Opt attribute ((unused)), const char* Arg)
966 /* Create a map file */
968 /* Create a map file (linker) */
969 CmdAddArg2 (&LD65, "-m", Arg);
974 static void OptMemoryModel (const char* Opt attribute ((unused)), const char* Arg)
975 /* Set the memory model */
977 mmodel_t MemoryModel = FindMemoryModel (Arg);
978 if (MemoryModel == MMODEL_UNKNOWN) {
979 Error ("Unknown memory model: %s", Arg);
980 } else if (MemoryModel == MMODEL_HUGE) {
981 Error ("Unsupported memory model: %s", Arg);
983 CmdAddArg2 (&CA65, "-mm", Arg);
984 CmdAddArg2 (&CC65, "-mm", Arg);
990 static void OptModule (const char* Opt attribute ((unused)),
991 const char* Arg attribute ((unused)))
992 /* Link as a module */
999 static void OptModuleId (const char* Opt attribute ((unused)), const char* Arg)
1000 /* Specify a module if for the linker */
1002 /* Pass it straight to the linker */
1003 CmdAddArg2 (&LD65, "--module-id", Arg);
1008 static void OptO65Model (const char* Opt attribute ((unused)), const char* Arg)
1009 /* Handle the --o65-model option */
1011 CmdAddArg2 (&CO65, "-m", Arg);
1016 static void OptObj (const char* Opt attribute ((unused)), const char* Arg)
1017 /* Object file follows (linker) */
1019 CmdAddArg2 (&LD65, "--obj", Arg);
1024 static void OptObjPath (const char* Opt attribute ((unused)), const char* Arg)
1025 /* Object file search path (linker) */
1027 CmdAddArg2 (&LD65, "--obj-path", Arg);
1032 static void OptRegisterSpace (const char* Opt attribute ((unused)), const char* Arg)
1033 /* Handle the --register-space option */
1035 CmdAddArg2 (&CC65, "--register-space", Arg);
1040 static void OptRegisterVars (const char* Opt attribute ((unused)),
1041 const char* Arg attribute ((unused)))
1042 /* Handle the --register-vars option */
1044 CmdAddArg (&CC65, "-r");
1049 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
1050 /* Handle the --rodata-name option */
1052 CmdAddArg2 (&CC65, "--rodata-name", Arg);
1057 static void OptSignedChars (const char* Opt attribute ((unused)),
1058 const char* Arg attribute ((unused)))
1059 /* Make default characters signed */
1061 CmdAddArg (&CC65, "-j");
1066 static void OptStandard (const char* Opt attribute ((unused)), const char* Arg)
1067 /* Set the language standard */
1069 CmdAddArg2 (&CC65, "--standard", Arg);
1074 static void OptStartAddr (const char* Opt attribute ((unused)), const char* Arg)
1075 /* Set the default start address */
1077 CmdAddArg2 (&LD65, "-S", Arg);
1082 static void OptStaticLocals (const char* Opt attribute ((unused)),
1083 const char* Arg attribute ((unused)))
1084 /* Place local variables in static storage */
1086 CmdAddArg (&CC65, "-Cl");
1091 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
1092 /* Set the target system */
1094 Target = FindTarget (Arg);
1095 if (Target == TGT_UNKNOWN) {
1096 Error ("No such target system: `%s'", Arg);
1097 } else if (Target == TGT_MODULE) {
1098 Error ("Cannot use `module' as target, use --module instead");
1104 static void OptVerbose (const char* Opt attribute ((unused)),
1105 const char* Arg attribute ((unused)))
1106 /* Verbose mode (compiler, assembler, linker) */
1108 CmdAddArg (&CC65, "-v");
1109 CmdAddArg (&CA65, "-v");
1110 CmdAddArg (&CO65, "-v");
1111 CmdAddArg (&LD65, "-v");
1116 static void OptVersion (const char* Opt attribute ((unused)),
1117 const char* Arg attribute ((unused)))
1118 /* Print version number */
1121 "cl65 V%u.%u.%u - (C) Copyright 1998-2005 Ullrich von Bassewitz\n",
1122 VER_MAJOR, VER_MINOR, VER_PATCH);
1127 static void OptZeropageLabel (const char* Opt attribute ((unused)), const char* Arg)
1128 /* Handle the --zeropage-label option */
1130 CmdAddArg2 (&CO65, "--zeropage-label", Arg);
1135 static void OptZeropageName (const char* Opt attribute ((unused)), const char* Arg)
1136 /* Handle the --zeropage-name option */
1138 CmdAddArg2 (&CO65, "--zeropage-name", Arg);
1143 int main (int argc, char* argv [])
1144 /* Utility main program */
1146 /* Program long options */
1147 static const LongOpt OptTab[] = {
1148 { "--add-source", 0, OptAddSource },
1149 { "--asm-args", 1, OptAsmArgs },
1150 { "--asm-define", 1, OptAsmDefine },
1151 { "--asm-include-dir", 1, OptAsmIncludeDir },
1152 { "--bss-label", 1, OptBssLabel },
1153 { "--bss-name", 1, OptBssName },
1154 { "--cfg-path", 1, OptCfgPath },
1155 { "--check-stack", 0, OptCheckStack },
1156 { "--code-label", 1, OptCodeLabel },
1157 { "--code-name", 1, OptCodeName },
1158 { "--codesize", 1, OptCodeSize },
1159 { "--config", 1, OptConfig },
1160 { "--cpu", 1, OptCPU },
1161 { "--create-dep", 0, OptCreateDep },
1162 { "--data-label", 1, OptDataLabel },
1163 { "--data-name", 1, OptDataName },
1164 { "--debug", 0, OptDebug },
1165 { "--debug-info", 0, OptDebugInfo },
1166 { "--feature", 1, OptFeature },
1167 { "--forget-inc-paths", 0, OptForgetIncPaths },
1168 { "--help", 0, OptHelp },
1169 { "--include-dir", 1, OptIncludeDir },
1170 { "--ld-args", 1, OptLdArgs },
1171 { "--lib", 1, OptLib },
1172 { "--lib-path", 1, OptLibPath },
1173 { "--list-targets", 0, OptListTargets },
1174 { "--listing", 0, OptListing },
1175 { "--list-bytes", 1, OptListBytes },
1176 { "--mapfile", 1, OptMapFile },
1177 { "--memory-model", 1, OptMemoryModel },
1178 { "--module", 0, OptModule },
1179 { "--module-id", 1, OptModuleId },
1180 { "--o65-model", 1, OptO65Model },
1181 { "--obj", 1, OptObj },
1182 { "--obj-path", 1, OptObjPath },
1183 { "--register-space", 1, OptRegisterSpace },
1184 { "--register-vars", 0, OptRegisterVars },
1185 { "--rodata-name", 1, OptRodataName },
1186 { "--signed-chars", 0, OptSignedChars },
1187 { "--standard", 1, OptStandard },
1188 { "--start-addr", 1, OptStartAddr },
1189 { "--static-locals", 0, OptStaticLocals },
1190 { "--target", 1, OptTarget },
1191 { "--verbose", 0, OptVerbose },
1192 { "--version", 0, OptVersion },
1193 { "--zeropage-label", 1, OptZeropageLabel },
1194 { "--zeropage-name", 1, OptZeropageName },
1199 /* Initialize the cmdline module */
1200 InitCmdLine (&argc, &argv, "cl65");
1202 /* Initialize the command descriptors */
1203 CmdInit (&CC65, "cc65");
1204 CmdInit (&CA65, "ca65");
1205 CmdInit (&CO65, "co65");
1206 CmdInit (&LD65, "ld65");
1207 CmdInit (&GRC, "grc");
1209 /* Our default target is the C64 instead of "none" */
1212 /* Check the parameters */
1214 while (I < ArgCount) {
1216 /* Get the argument */
1217 const char* Arg = ArgVec[I];
1219 /* Check for an option */
1220 if (Arg [0] == '-') {
1225 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
1229 if (Arg[2] == 'l' && Arg[3] == '\0') {
1230 /* Make local variables static */
1231 OptStaticLocals (Arg, 0);
1233 /* Specify linker config file */
1234 OptConfig (Arg, GetArg (&I, 2));
1239 /* Define a preprocessor symbol (compiler) */
1240 CmdAddArg2 (&CC65, "-D", GetArg (&I, 2));
1244 /* Include directory (compiler) */
1245 OptIncludeDir (Arg, GetArg (&I, 2));
1249 if (Arg[2] == 'n' && Arg[3] == '\0') {
1250 /* VICE label file (linker) */
1251 CmdAddArg2 (&LD65, "-Ln", GetArg (&I, 3));
1253 /* Library search path (linker) */
1254 OptLibPath (Arg, GetArg (&I, 2));
1259 /* Optimize code (compiler, also covers -Oi and others) */
1260 CmdAddArg (&CC65, Arg);
1264 /* Dont assemble and link the created files */
1265 DontLink = DontAssemble = 1;
1269 /* Include source as comment (compiler) */
1270 OptAddSource (Arg, 0);
1274 /* Print version number */
1275 OptVersion (Arg, 0);
1282 OptAsmArgs (Arg, GetArg (&I, 3));
1286 OptLdArgs (Arg, GetArg (&I, 3));
1290 /* Suppress warnings - compiler and assembler */
1291 CmdAddArg (&CC65, "-W");
1292 CmdAddArg2 (&CA65, "-W", "0");
1296 UnknownOption (Arg);
1302 /* Don't link the resulting files */
1307 /* Debug mode (compiler) */
1312 /* Debugging - add to compiler and assembler */
1313 OptDebugInfo (Arg, 0);
1318 /* Print help - cl65 */
1323 /* Default characters are signed */
1324 OptSignedChars (Arg, 0);
1328 /* Create an assembler listing */
1329 OptListing (Arg, 0);
1333 /* Create a map file (linker) */
1334 OptMapFile (Arg, GetArg (&I, 2));
1338 /* Name the output file */
1339 OutputName = GetArg (&I, 2);
1343 /* Enable register variables */
1344 OptRegisterVars (Arg, 0);
1348 /* Set target system - compiler, assembler and linker */
1349 OptTarget (Arg, GetArg (&I, 2));
1353 if (Arg [2] == 'm') {
1354 /* Verbose map file (linker) */
1355 CmdAddArg (&LD65, "-vm");
1357 /* Verbose mode (compiler, assembler, linker) */
1358 OptVerbose (Arg, 0);
1363 UnknownOption (Arg);
1367 /* Remember the first file name */
1368 if (FirstInput == 0) {
1372 /* Determine the file type by the extension */
1373 switch (GetFileType (Arg)) {
1376 /* Compile the file */
1381 /* Assemble the file */
1382 if (!DontAssemble) {
1389 /* Add to the linker files */
1390 CmdAddFile (&LD65, Arg);
1394 /* Add to the resource compiler files */
1399 /* Add the the object file converter files */
1404 Error ("Don't know what to do with `%s'", Arg);
1414 /* Check if we had any input files */
1415 if (FirstInput == 0) {
1416 Warning ("No input files");
1419 /* Link the given files if requested and if we have any */
1420 if (DontLink == 0 && LD65.FileCount > 0) {
1424 /* Return an apropriate exit code */
1425 return EXIT_SUCCESS;