1 /*****************************************************************************/
5 /* Main module for the cl65 compile and link utility */
9 /* (C) 1999-2009, Ullrich von Bassewitz */
10 /* Roemerstrasse 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 OptForceImport (const char* Opt attribute ((unused)), const char* Arg)
881 /* Emulation features for the assembler */
883 CmdAddArg2 (&LD65, "--force-import", Arg);
888 static void OptForgetIncPaths (const char* Opt attribute ((unused)),
889 const char* Arg attribute ((unused)))
890 /* Forget all currently defined include paths */
892 CmdAddArg (&CC65, "--forget-inc-paths");
897 static void OptHelp (const char* Opt attribute ((unused)),
898 const char* Arg attribute ((unused)))
899 /* Print help - cl65 */
907 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
908 /* Include directory (compiler) */
910 CmdAddArg2 (&CC65, "-I", Arg);
915 static void OptLdArgs (const char* Opt attribute ((unused)), const char* Arg)
916 /* Pass arguments to the linker */
918 CmdAddArgList (&LD65, Arg);
923 static void OptLib (const char* Opt attribute ((unused)), const char* Arg)
924 /* Library file follows (linker) */
926 CmdAddArg2 (&LD65, "--lib", Arg);
931 static void OptLibPath (const char* Opt attribute ((unused)), const char* Arg)
932 /* Library search path (linker) */
934 CmdAddArg2 (&LD65, "--lib-path", Arg);
939 static void OptListBytes (const char* Opt attribute ((unused)), const char* Arg)
940 /* Set the maximum number of bytes per asm listing line */
942 CmdAddArg2 (&CA65, "--list-bytes", Arg);
947 static void OptListing (const char* Opt attribute ((unused)),
948 const char* Arg attribute ((unused)))
949 /* Create an assembler listing */
951 CmdAddArg (&CA65, "-l");
956 static void OptListTargets (const char* Opt attribute ((unused)),
957 const char* Arg attribute ((unused)))
958 /* List all targets */
962 /* List the targets */
963 for (I = TGT_NONE; I < TGT_COUNT; ++I) {
964 printf ("%s\n", TargetNames[I]);
973 static void OptMapFile (const char* Opt attribute ((unused)), const char* Arg)
974 /* Create a map file */
976 /* Create a map file (linker) */
977 CmdAddArg2 (&LD65, "-m", Arg);
982 static void OptMemoryModel (const char* Opt attribute ((unused)), const char* Arg)
983 /* Set the memory model */
985 mmodel_t MemoryModel = FindMemoryModel (Arg);
986 if (MemoryModel == MMODEL_UNKNOWN) {
987 Error ("Unknown memory model: %s", Arg);
988 } else if (MemoryModel == MMODEL_HUGE) {
989 Error ("Unsupported memory model: %s", Arg);
991 CmdAddArg2 (&CA65, "-mm", Arg);
992 CmdAddArg2 (&CC65, "-mm", Arg);
998 static void OptModule (const char* Opt attribute ((unused)),
999 const char* Arg attribute ((unused)))
1000 /* Link as a module */
1007 static void OptModuleId (const char* Opt attribute ((unused)), const char* Arg)
1008 /* Specify a module if for the linker */
1010 /* Pass it straight to the linker */
1011 CmdAddArg2 (&LD65, "--module-id", Arg);
1016 static void OptO65Model (const char* Opt attribute ((unused)), const char* Arg)
1017 /* Handle the --o65-model option */
1019 CmdAddArg2 (&CO65, "-m", Arg);
1024 static void OptObj (const char* Opt attribute ((unused)), const char* Arg)
1025 /* Object file follows (linker) */
1027 CmdAddArg2 (&LD65, "--obj", Arg);
1032 static void OptObjPath (const char* Opt attribute ((unused)), const char* Arg)
1033 /* Object file search path (linker) */
1035 CmdAddArg2 (&LD65, "--obj-path", Arg);
1040 static void OptRegisterSpace (const char* Opt attribute ((unused)), const char* Arg)
1041 /* Handle the --register-space option */
1043 CmdAddArg2 (&CC65, "--register-space", Arg);
1048 static void OptRegisterVars (const char* Opt attribute ((unused)),
1049 const char* Arg attribute ((unused)))
1050 /* Handle the --register-vars option */
1052 CmdAddArg (&CC65, "-r");
1057 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
1058 /* Handle the --rodata-name option */
1060 CmdAddArg2 (&CC65, "--rodata-name", Arg);
1065 static void OptSignedChars (const char* Opt attribute ((unused)),
1066 const char* Arg attribute ((unused)))
1067 /* Make default characters signed */
1069 CmdAddArg (&CC65, "-j");
1074 static void OptStandard (const char* Opt attribute ((unused)), const char* Arg)
1075 /* Set the language standard */
1077 CmdAddArg2 (&CC65, "--standard", Arg);
1082 static void OptStartAddr (const char* Opt attribute ((unused)), const char* Arg)
1083 /* Set the default start address */
1085 CmdAddArg2 (&LD65, "-S", Arg);
1090 static void OptStaticLocals (const char* Opt attribute ((unused)),
1091 const char* Arg attribute ((unused)))
1092 /* Place local variables in static storage */
1094 CmdAddArg (&CC65, "-Cl");
1099 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
1100 /* Set the target system */
1102 Target = FindTarget (Arg);
1103 if (Target == TGT_UNKNOWN) {
1104 Error ("No such target system: `%s'", Arg);
1105 } else if (Target == TGT_MODULE) {
1106 Error ("Cannot use `module' as target, use --module instead");
1112 static void OptVerbose (const char* Opt attribute ((unused)),
1113 const char* Arg attribute ((unused)))
1114 /* Verbose mode (compiler, assembler, linker) */
1116 CmdAddArg (&CC65, "-v");
1117 CmdAddArg (&CA65, "-v");
1118 CmdAddArg (&CO65, "-v");
1119 CmdAddArg (&LD65, "-v");
1124 static void OptVersion (const char* Opt attribute ((unused)),
1125 const char* Arg attribute ((unused)))
1126 /* Print version number */
1129 "cl65 V%u.%u.%u - (C) Copyright 1998-2005 Ullrich von Bassewitz\n",
1130 VER_MAJOR, VER_MINOR, VER_PATCH);
1135 static void OptZeropageLabel (const char* Opt attribute ((unused)), const char* Arg)
1136 /* Handle the --zeropage-label option */
1138 CmdAddArg2 (&CO65, "--zeropage-label", Arg);
1143 static void OptZeropageName (const char* Opt attribute ((unused)), const char* Arg)
1144 /* Handle the --zeropage-name option */
1146 CmdAddArg2 (&CO65, "--zeropage-name", Arg);
1151 int main (int argc, char* argv [])
1152 /* Utility main program */
1154 /* Program long options */
1155 static const LongOpt OptTab[] = {
1156 { "--add-source", 0, OptAddSource },
1157 { "--asm-args", 1, OptAsmArgs },
1158 { "--asm-define", 1, OptAsmDefine },
1159 { "--asm-include-dir", 1, OptAsmIncludeDir },
1160 { "--bss-label", 1, OptBssLabel },
1161 { "--bss-name", 1, OptBssName },
1162 { "--cfg-path", 1, OptCfgPath },
1163 { "--check-stack", 0, OptCheckStack },
1164 { "--code-label", 1, OptCodeLabel },
1165 { "--code-name", 1, OptCodeName },
1166 { "--codesize", 1, OptCodeSize },
1167 { "--config", 1, OptConfig },
1168 { "--cpu", 1, OptCPU },
1169 { "--create-dep", 0, OptCreateDep },
1170 { "--data-label", 1, OptDataLabel },
1171 { "--data-name", 1, OptDataName },
1172 { "--debug", 0, OptDebug },
1173 { "--debug-info", 0, OptDebugInfo },
1174 { "--feature", 1, OptFeature },
1175 { "--force-import", 1, OptForceImport },
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;