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 " -u sym\t\tForce an import of symbol `sym'\n"
640 " -v\t\t\tVerbose mode\n"
641 " -vm\t\t\tVerbose map file\n"
642 " -C name\t\tUse linker config file\n"
643 " -Cl\t\t\tMake local variables static\n"
644 " -D sym[=defn]\t\tDefine a preprocessor symbol\n"
645 " -I dir\t\tSet a compiler include directory path\n"
646 " -L path\t\tSpecify a library search path\n"
647 " -Ln name\t\tCreate a VICE label file\n"
648 " -O\t\t\tOptimize code\n"
649 " -Oi\t\t\tOptimize code, inline functions\n"
650 " -Or\t\t\tOptimize code, honour the register keyword\n"
651 " -Os\t\t\tOptimize code, inline known C funtions\n"
652 " -S\t\t\tCompile but don't assemble and link\n"
653 " -T\t\t\tInclude source as comment\n"
654 " -V\t\t\tPrint the version number\n"
655 " -W\t\t\tSuppress warnings\n"
656 " -Wa options\t\tPass options to the assembler\n"
657 " -Wl options\t\tPass options to the linker\n"
660 " --add-source\t\tInclude source as comment\n"
661 " --asm-args options\tPass options to the assembler\n"
662 " --asm-define sym[=v]\tDefine an assembler symbol\n"
663 " --asm-include-dir dir\tSet an assembler include directory\n"
664 " --bss-label name\tDefine and export a BSS segment label\n"
665 " --bss-name seg\tSet the name of the BSS segment\n"
666 " --cfg-path path\tSpecify a config file search path\n"
667 " --check-stack\t\tGenerate stack overflow checks\n"
668 " --code-label name\tDefine and export a CODE segment label\n"
669 " --code-name seg\tSet the name of the CODE segment\n"
670 " --codesize x\t\tAccept larger code by factor x\n"
671 " --config name\t\tUse linker config file\n"
672 " --cpu type\t\tSet cpu type\n"
673 " --create-dep\t\tCreate a make dependency file\n"
674 " --data-label name\tDefine and export a DATA segment label\n"
675 " --data-name seg\tSet the name of the DATA segment\n"
676 " --debug\t\tDebug mode\n"
677 " --debug-info\t\tAdd debug info\n"
678 " --feature name\tSet an emulation feature\n"
679 " --force-import sym\tForce an import of symbol `sym'\n"
680 " --forget-inc-paths\tForget include search paths (compiler)\n"
681 " --help\t\tHelp (this text)\n"
682 " --include-dir dir\tSet a compiler include directory path\n"
683 " --ld-args options\tPass options to the linker\n"
684 " --lib file\t\tLink this library\n"
685 " --lib-path path\tSpecify a library search path\n"
686 " --list-targets\tList all available targets\n"
687 " --listing\t\tCreate an assembler listing\n"
688 " --list-bytes n\tNumber of bytes per assembler listing line\n"
689 " --mapfile name\tCreate a map file\n"
690 " --memory-model model\tSet the memory model\n"
691 " --module\t\tLink as a module\n"
692 " --module-id id\tSpecify a module id for the linker\n"
693 " --o65-model model\tOverride the o65 model\n"
694 " --obj file\t\tLink this object file\n"
695 " --obj-path path\tSpecify an object file search path\n"
696 " --register-space b\tSet space available for register variables\n"
697 " --register-vars\tEnable register variables\n"
698 " --rodata-name seg\tSet the name of the RODATA segment\n"
699 " --signed-chars\tDefault characters are signed\n"
700 " --standard std\tLanguage standard (c89, c99, cc65)\n"
701 " --start-addr addr\tSet the default start address\n"
702 " --static-locals\tMake local variables static\n"
703 " --target sys\t\tSet the target system\n"
704 " --version\t\tPrint the version number\n"
705 " --verbose\t\tVerbose mode\n"
706 " --zeropage-label name\tDefine and export a ZEROPAGE segment label\n"
707 " --zeropage-name seg\tSet the name of the ZEROPAGE segment\n",
713 static void OptAddSource (const char* Opt attribute ((unused)),
714 const char* Arg attribute ((unused)))
715 /* Strict source code as comments to the generated asm code */
717 CmdAddArg (&CC65, "-T");
722 static void OptAsmArgs (const char* Opt attribute ((unused)), const char* Arg)
723 /* Pass arguments to the assembler */
725 CmdAddArgList (&CA65, Arg);
730 static void OptAsmDefine (const char* Opt attribute ((unused)), const char* Arg)
731 /* Define an assembler symbol (assembler) */
733 CmdAddArg2 (&CA65, "-D", Arg);
738 static void OptAsmIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
739 /* Include directory (assembler) */
741 CmdAddArg2 (&CA65, "-I", Arg);
746 static void OptBssLabel (const char* Opt attribute ((unused)), const char* Arg)
747 /* Handle the --bss-label option */
749 CmdAddArg2 (&CO65, "--bss-label", Arg);
754 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
755 /* Handle the --bss-name option */
757 CmdAddArg2 (&CC65, "--bss-name", Arg);
758 CmdAddArg2 (&CO65, "--bss-name", Arg);
763 static void OptCfgPath (const char* Opt attribute ((unused)), const char* Arg)
764 /* Config file search path (linker) */
766 CmdAddArg2 (&LD65, "--cfg-path", Arg);
771 static void OptCheckStack (const char* Opt attribute ((unused)),
772 const char* Arg attribute ((unused)))
773 /* Handle the --check-stack option */
775 CmdAddArg (&CC65, "--check-stack");
780 static void OptCodeLabel (const char* Opt attribute ((unused)), const char* Arg)
781 /* Handle the --code-label option */
783 CmdAddArg2 (&CO65, "--code-label", Arg);
788 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
789 /* Handle the --code-name option */
791 CmdAddArg2 (&CC65, "--code-name", Arg);
792 CmdAddArg2 (&CO65, "--code-name", Arg);
797 static void OptCodeSize (const char* Opt attribute ((unused)), const char* Arg)
798 /* Handle the --codesize option */
800 CmdAddArg2 (&CC65, "--codesize", Arg);
805 static void OptConfig (const char* Opt attribute ((unused)), const char* Arg)
806 /* Config file (linker) */
809 Error ("Cannot specify -C/--config twice");
816 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
817 /* Handle the --cpu option */
819 /* Add the cpu type to the assembler and compiler */
820 CmdAddArg2 (&CA65, "--cpu", Arg);
821 CmdAddArg2 (&CC65, "--cpu", Arg);
826 static void OptCreateDep (const char* Opt attribute ((unused)),
827 const char* Arg attribute ((unused)))
828 /* Handle the --create-dep option */
830 CmdAddArg (&CC65, "--create-dep");
835 static void OptDataLabel (const char* Opt attribute ((unused)), const char* Arg)
836 /* Handle the --data-label option */
838 CmdAddArg2 (&CO65, "--data-label", Arg);
843 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
844 /* Handle the --data-name option */
846 CmdAddArg2 (&CC65, "--data-name", Arg);
847 CmdAddArg2 (&CO65, "--data-name", Arg);
852 static void OptDebug (const char* Opt attribute ((unused)),
853 const char* Arg attribute ((unused)))
854 /* Debug mode (compiler and cl65 utility) */
856 CmdAddArg (&CC65, "-d");
857 CmdAddArg (&CO65, "-d");
863 static void OptDebugInfo (const char* Opt attribute ((unused)),
864 const char* Arg attribute ((unused)))
865 /* Debug Info - add to compiler and assembler */
867 CmdAddArg (&CC65, "-g");
868 CmdAddArg (&CA65, "-g");
869 CmdAddArg (&CO65, "-g");
874 static void OptFeature (const char* Opt attribute ((unused)), const char* Arg)
875 /* Emulation features for the assembler */
877 CmdAddArg2 (&CA65, "--feature", Arg);
882 static void OptForceImport (const char* Opt attribute ((unused)), const char* Arg)
883 /* Emulation features for the assembler */
885 CmdAddArg2 (&LD65, "-u", Arg);
890 static void OptForgetIncPaths (const char* Opt attribute ((unused)),
891 const char* Arg attribute ((unused)))
892 /* Forget all currently defined include paths */
894 CmdAddArg (&CC65, "--forget-inc-paths");
899 static void OptHelp (const char* Opt attribute ((unused)),
900 const char* Arg attribute ((unused)))
901 /* Print help - cl65 */
909 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
910 /* Include directory (compiler) */
912 CmdAddArg2 (&CC65, "-I", Arg);
917 static void OptLdArgs (const char* Opt attribute ((unused)), const char* Arg)
918 /* Pass arguments to the linker */
920 CmdAddArgList (&LD65, Arg);
925 static void OptLib (const char* Opt attribute ((unused)), const char* Arg)
926 /* Library file follows (linker) */
928 CmdAddArg2 (&LD65, "--lib", Arg);
933 static void OptLibPath (const char* Opt attribute ((unused)), const char* Arg)
934 /* Library search path (linker) */
936 CmdAddArg2 (&LD65, "--lib-path", Arg);
941 static void OptListBytes (const char* Opt attribute ((unused)), const char* Arg)
942 /* Set the maximum number of bytes per asm listing line */
944 CmdAddArg2 (&CA65, "--list-bytes", Arg);
949 static void OptListing (const char* Opt attribute ((unused)),
950 const char* Arg attribute ((unused)))
951 /* Create an assembler listing */
953 CmdAddArg (&CA65, "-l");
958 static void OptListTargets (const char* Opt attribute ((unused)),
959 const char* Arg attribute ((unused)))
960 /* List all targets */
964 /* List the targets */
965 for (I = TGT_NONE; I < TGT_COUNT; ++I) {
966 printf ("%s\n", TargetNames[I]);
975 static void OptMapFile (const char* Opt attribute ((unused)), const char* Arg)
976 /* Create a map file */
978 /* Create a map file (linker) */
979 CmdAddArg2 (&LD65, "-m", Arg);
984 static void OptMemoryModel (const char* Opt attribute ((unused)), const char* Arg)
985 /* Set the memory model */
987 mmodel_t MemoryModel = FindMemoryModel (Arg);
988 if (MemoryModel == MMODEL_UNKNOWN) {
989 Error ("Unknown memory model: %s", Arg);
990 } else if (MemoryModel == MMODEL_HUGE) {
991 Error ("Unsupported memory model: %s", Arg);
993 CmdAddArg2 (&CA65, "-mm", Arg);
994 CmdAddArg2 (&CC65, "-mm", Arg);
1000 static void OptModule (const char* Opt attribute ((unused)),
1001 const char* Arg attribute ((unused)))
1002 /* Link as a module */
1009 static void OptModuleId (const char* Opt attribute ((unused)), const char* Arg)
1010 /* Specify a module if for the linker */
1012 /* Pass it straight to the linker */
1013 CmdAddArg2 (&LD65, "--module-id", Arg);
1018 static void OptO65Model (const char* Opt attribute ((unused)), const char* Arg)
1019 /* Handle the --o65-model option */
1021 CmdAddArg2 (&CO65, "-m", Arg);
1026 static void OptObj (const char* Opt attribute ((unused)), const char* Arg)
1027 /* Object file follows (linker) */
1029 CmdAddArg2 (&LD65, "--obj", Arg);
1034 static void OptObjPath (const char* Opt attribute ((unused)), const char* Arg)
1035 /* Object file search path (linker) */
1037 CmdAddArg2 (&LD65, "--obj-path", Arg);
1042 static void OptRegisterSpace (const char* Opt attribute ((unused)), const char* Arg)
1043 /* Handle the --register-space option */
1045 CmdAddArg2 (&CC65, "--register-space", Arg);
1050 static void OptRegisterVars (const char* Opt attribute ((unused)),
1051 const char* Arg attribute ((unused)))
1052 /* Handle the --register-vars option */
1054 CmdAddArg (&CC65, "-r");
1059 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
1060 /* Handle the --rodata-name option */
1062 CmdAddArg2 (&CC65, "--rodata-name", Arg);
1067 static void OptSignedChars (const char* Opt attribute ((unused)),
1068 const char* Arg attribute ((unused)))
1069 /* Make default characters signed */
1071 CmdAddArg (&CC65, "-j");
1076 static void OptStandard (const char* Opt attribute ((unused)), const char* Arg)
1077 /* Set the language standard */
1079 CmdAddArg2 (&CC65, "--standard", Arg);
1084 static void OptStartAddr (const char* Opt attribute ((unused)), const char* Arg)
1085 /* Set the default start address */
1087 CmdAddArg2 (&LD65, "-S", Arg);
1092 static void OptStaticLocals (const char* Opt attribute ((unused)),
1093 const char* Arg attribute ((unused)))
1094 /* Place local variables in static storage */
1096 CmdAddArg (&CC65, "-Cl");
1101 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
1102 /* Set the target system */
1104 Target = FindTarget (Arg);
1105 if (Target == TGT_UNKNOWN) {
1106 Error ("No such target system: `%s'", Arg);
1107 } else if (Target == TGT_MODULE) {
1108 Error ("Cannot use `module' as target, use --module instead");
1114 static void OptVerbose (const char* Opt attribute ((unused)),
1115 const char* Arg attribute ((unused)))
1116 /* Verbose mode (compiler, assembler, linker) */
1118 CmdAddArg (&CC65, "-v");
1119 CmdAddArg (&CA65, "-v");
1120 CmdAddArg (&CO65, "-v");
1121 CmdAddArg (&LD65, "-v");
1126 static void OptVersion (const char* Opt attribute ((unused)),
1127 const char* Arg attribute ((unused)))
1128 /* Print version number */
1131 "cl65 V%s - (C) Copyright 1998-2009 Ullrich von Bassewitz\n",
1132 GetVersionAsString ());
1137 static void OptZeropageLabel (const char* Opt attribute ((unused)), const char* Arg)
1138 /* Handle the --zeropage-label option */
1140 CmdAddArg2 (&CO65, "--zeropage-label", Arg);
1145 static void OptZeropageName (const char* Opt attribute ((unused)), const char* Arg)
1146 /* Handle the --zeropage-name option */
1148 CmdAddArg2 (&CO65, "--zeropage-name", Arg);
1153 int main (int argc, char* argv [])
1154 /* Utility main program */
1156 /* Program long options */
1157 static const LongOpt OptTab[] = {
1158 { "--add-source", 0, OptAddSource },
1159 { "--asm-args", 1, OptAsmArgs },
1160 { "--asm-define", 1, OptAsmDefine },
1161 { "--asm-include-dir", 1, OptAsmIncludeDir },
1162 { "--bss-label", 1, OptBssLabel },
1163 { "--bss-name", 1, OptBssName },
1164 { "--cfg-path", 1, OptCfgPath },
1165 { "--check-stack", 0, OptCheckStack },
1166 { "--code-label", 1, OptCodeLabel },
1167 { "--code-name", 1, OptCodeName },
1168 { "--codesize", 1, OptCodeSize },
1169 { "--config", 1, OptConfig },
1170 { "--cpu", 1, OptCPU },
1171 { "--create-dep", 0, OptCreateDep },
1172 { "--data-label", 1, OptDataLabel },
1173 { "--data-name", 1, OptDataName },
1174 { "--debug", 0, OptDebug },
1175 { "--debug-info", 0, OptDebugInfo },
1176 { "--feature", 1, OptFeature },
1177 { "--force-import", 1, OptForceImport },
1178 { "--forget-inc-paths", 0, OptForgetIncPaths },
1179 { "--help", 0, OptHelp },
1180 { "--include-dir", 1, OptIncludeDir },
1181 { "--ld-args", 1, OptLdArgs },
1182 { "--lib", 1, OptLib },
1183 { "--lib-path", 1, OptLibPath },
1184 { "--list-targets", 0, OptListTargets },
1185 { "--listing", 0, OptListing },
1186 { "--list-bytes", 1, OptListBytes },
1187 { "--mapfile", 1, OptMapFile },
1188 { "--memory-model", 1, OptMemoryModel },
1189 { "--module", 0, OptModule },
1190 { "--module-id", 1, OptModuleId },
1191 { "--o65-model", 1, OptO65Model },
1192 { "--obj", 1, OptObj },
1193 { "--obj-path", 1, OptObjPath },
1194 { "--register-space", 1, OptRegisterSpace },
1195 { "--register-vars", 0, OptRegisterVars },
1196 { "--rodata-name", 1, OptRodataName },
1197 { "--signed-chars", 0, OptSignedChars },
1198 { "--standard", 1, OptStandard },
1199 { "--start-addr", 1, OptStartAddr },
1200 { "--static-locals", 0, OptStaticLocals },
1201 { "--target", 1, OptTarget },
1202 { "--verbose", 0, OptVerbose },
1203 { "--version", 0, OptVersion },
1204 { "--zeropage-label", 1, OptZeropageLabel },
1205 { "--zeropage-name", 1, OptZeropageName },
1210 /* Initialize the cmdline module */
1211 InitCmdLine (&argc, &argv, "cl65");
1213 /* Initialize the command descriptors */
1214 CmdInit (&CC65, "cc65");
1215 CmdInit (&CA65, "ca65");
1216 CmdInit (&CO65, "co65");
1217 CmdInit (&LD65, "ld65");
1218 CmdInit (&GRC, "grc");
1220 /* Our default target is the C64 instead of "none" */
1223 /* Check the parameters */
1225 while (I < ArgCount) {
1227 /* Get the argument */
1228 const char* Arg = ArgVec[I];
1230 /* Check for an option */
1231 if (Arg [0] == '-') {
1236 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
1240 if (Arg[2] == 'l' && Arg[3] == '\0') {
1241 /* Make local variables static */
1242 OptStaticLocals (Arg, 0);
1244 /* Specify linker config file */
1245 OptConfig (Arg, GetArg (&I, 2));
1250 /* Define a preprocessor symbol (compiler) */
1251 CmdAddArg2 (&CC65, "-D", GetArg (&I, 2));
1255 /* Include directory (compiler) */
1256 OptIncludeDir (Arg, GetArg (&I, 2));
1260 if (Arg[2] == 'n' && Arg[3] == '\0') {
1261 /* VICE label file (linker) */
1262 CmdAddArg2 (&LD65, "-Ln", GetArg (&I, 3));
1264 /* Library search path (linker) */
1265 OptLibPath (Arg, GetArg (&I, 2));
1270 /* Optimize code (compiler, also covers -Oi and others) */
1271 CmdAddArg (&CC65, Arg);
1275 /* Dont assemble and link the created files */
1276 DontLink = DontAssemble = 1;
1280 /* Include source as comment (compiler) */
1281 OptAddSource (Arg, 0);
1285 /* Print version number */
1286 OptVersion (Arg, 0);
1293 OptAsmArgs (Arg, GetArg (&I, 3));
1297 OptLdArgs (Arg, GetArg (&I, 3));
1301 /* Suppress warnings - compiler and assembler */
1302 CmdAddArg (&CC65, "-W");
1303 CmdAddArg2 (&CA65, "-W", "0");
1307 UnknownOption (Arg);
1313 /* Don't link the resulting files */
1318 /* Debug mode (compiler) */
1323 /* Debugging - add to compiler and assembler */
1324 OptDebugInfo (Arg, 0);
1329 /* Print help - cl65 */
1334 /* Default characters are signed */
1335 OptSignedChars (Arg, 0);
1339 /* Create an assembler listing */
1340 OptListing (Arg, 0);
1344 /* Create a map file (linker) */
1345 OptMapFile (Arg, GetArg (&I, 2));
1349 /* Name the output file */
1350 OutputName = GetArg (&I, 2);
1354 /* Enable register variables */
1355 OptRegisterVars (Arg, 0);
1359 /* Set target system - compiler, assembler and linker */
1360 OptTarget (Arg, GetArg (&I, 2));
1364 /* Force an import (linker) */
1365 OptForceImport (Arg, GetArg (&I, 2));
1369 if (Arg [2] == 'm') {
1370 /* Verbose map file (linker) */
1371 CmdAddArg (&LD65, "-vm");
1373 /* Verbose mode (compiler, assembler, linker) */
1374 OptVerbose (Arg, 0);
1379 UnknownOption (Arg);
1383 /* Remember the first file name */
1384 if (FirstInput == 0) {
1388 /* Determine the file type by the extension */
1389 switch (GetFileType (Arg)) {
1392 /* Compile the file */
1397 /* Assemble the file */
1398 if (!DontAssemble) {
1405 /* Add to the linker files */
1406 CmdAddFile (&LD65, Arg);
1410 /* Add to the resource compiler files */
1415 /* Add the the object file converter files */
1420 Error ("Don't know what to do with `%s'", Arg);
1430 /* Check if we had any input files */
1431 if (FirstInput == 0) {
1432 Warning ("No input files");
1435 /* Link the given files if requested and if we have any */
1436 if (DontLink == 0 && LD65.FileCount > 0) {
1440 /* Return an apropriate exit code */
1441 return EXIT_SUCCESS;