1 /*****************************************************************************/
5 /* Main module for the cl65 compile and link utility */
9 /* (C) 1999-2004 Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
36 /* Check out if we have a spawn() function on the system, or if we must use
39 #if defined(__WATCOMC__) || defined(_MSC_VER) || defined(__MINGW32__) || defined(__DJGPP__)
73 /*****************************************************************************/
75 /*****************************************************************************/
79 /* Struct that describes a command */
80 typedef struct CmdDesc CmdDesc;
82 char* Name; /* The command name */
84 unsigned ArgCount; /* Count of arguments */
85 unsigned ArgMax; /* Maximum count of arguments */
86 char** Args; /* The arguments */
88 unsigned FileCount; /* Count of files to translate */
89 unsigned FileMax; /* Maximum count of files */
90 char** Files; /* The files */
93 /* Command descriptors for the different programs */
94 static CmdDesc CC65 = { 0, 0, 0, 0, 0, 0, 0 };
95 static CmdDesc CA65 = { 0, 0, 0, 0, 0, 0, 0 };
96 static CmdDesc CO65 = { 0, 0, 0, 0, 0, 0, 0 };
97 static CmdDesc LD65 = { 0, 0, 0, 0, 0, 0, 0 };
98 static CmdDesc GRC = { 0, 0, 0, 0, 0, 0, 0 };
100 /* Variables controlling the steps we're doing */
101 static int DontLink = 0;
102 static int DontAssemble = 0;
104 /* The name of the output file, NULL if none given */
105 static const char* OutputName = 0;
107 /* The name of the linker configuration file if given */
108 static const char* LinkerConfig = 0;
110 /* The name of the first input file. This will be used to construct the
111 * executable file name if no explicit name is given.
113 static const char* FirstInput = 0;
115 /* Remember if we should link a module */
116 static int Module = 0;
118 /* Extension used for a module */
119 #define MODULE_EXT ".o65"
121 /* Name of the crt0 object file and the runtime library */
122 static char* TargetCRT0 = 0;
123 static char* TargetLib = 0;
127 /*****************************************************************************/
128 /* Include the system specific spawn function */
129 /*****************************************************************************/
133 #if defined(NEED_SPAWN)
134 # if defined(SPAWN_UNIX)
135 # include "spawn-unix.inc"
136 # elif defined(SPAWN_AMIGA)
137 # include "spawn-amiga.inc"
139 # error "Don't know which spawn module to include!"
145 /*****************************************************************************/
146 /* Command structure handling */
147 /*****************************************************************************/
151 static void CmdAddArg (CmdDesc* Cmd, const char* Arg)
152 /* Add a new argument to the command */
154 /* Expand the argument vector if needed */
155 if (Cmd->ArgCount == Cmd->ArgMax) {
156 unsigned NewMax = Cmd->ArgMax + 10;
157 char** NewArgs = xmalloc (NewMax * sizeof (char*));
158 memcpy (NewArgs, Cmd->Args, Cmd->ArgMax * sizeof (char*));
161 Cmd->ArgMax = NewMax;
164 /* Add a copy of the new argument, allow a NULL pointer */
166 Cmd->Args [Cmd->ArgCount++] = xstrdup (Arg);
168 Cmd->Args [Cmd->ArgCount++] = 0;
174 static void CmdAddArg2 (CmdDesc* Cmd, const char* Arg1, const char* Arg2)
175 /* Add a new argument pair to the command */
177 CmdAddArg (Cmd, Arg1);
178 CmdAddArg (Cmd, Arg2);
183 static void CmdDelArgs (CmdDesc* Cmd, unsigned LastValid)
184 /* Remove all arguments with an index greater than LastValid */
186 while (Cmd->ArgCount > LastValid) {
188 xfree (Cmd->Args [Cmd->ArgCount]);
189 Cmd->Args [Cmd->ArgCount] = 0;
195 static void CmdAddFile (CmdDesc* Cmd, const char* File)
196 /* Add a new file to the command */
198 /* Expand the file vector if needed */
199 if (Cmd->FileCount == Cmd->FileMax) {
200 unsigned NewMax = Cmd->FileMax + 10;
201 char** NewFiles = xmalloc (NewMax * sizeof (char*));
202 memcpy (NewFiles, Cmd->Files, Cmd->FileMax * sizeof (char*));
204 Cmd->Files = NewFiles;
205 Cmd->FileMax = NewMax;
208 /* If the file name is not NULL (which is legal and is used to terminate
209 * the file list), check if the file name does already exist in the file
210 * list and print a warning if so. Regardless of the search result, add
215 for (I = 0; I < Cmd->FileCount; ++I) {
216 if (strcmp (Cmd->Files[I], File) == 0) {
218 Warning ("Duplicate file in argument list: `%s'", File);
219 /* No need to search further */
225 Cmd->Files [Cmd->FileCount++] = xstrdup (File);
227 /* Add a NULL pointer */
228 Cmd->Files [Cmd->FileCount++] = 0;
234 static void CmdInit (CmdDesc* Cmd, const char* Path)
235 /* Initialize the command using the given path to the executable */
237 /* Remember the command */
238 Cmd->Name = xstrdup (Path);
240 /* Use the command name as first argument */
241 CmdAddArg (Cmd, Path);
246 static void CmdSetOutput (CmdDesc* Cmd, const char* File)
247 /* Set the output file in a command desc */
249 CmdAddArg2 (Cmd, "-o", File);
254 static void CmdSetTarget (CmdDesc* Cmd, target_t Target)
255 /* Set the output file in a command desc */
257 CmdAddArg2 (Cmd, "-t", TargetNames[Target]);
262 static void CmdPrint (CmdDesc* Cmd, FILE* F)
263 /* Output the command line encoded in the command desc */
266 for (I = 0; I < Cmd->ArgCount && Cmd->Args[I] != 0; ++I) {
267 fprintf (F, "%s ", Cmd->Args[I]);
273 /*****************************************************************************/
274 /* Target handling */
275 /*****************************************************************************/
279 static void SetTargetFiles (void)
280 /* Set the target system files */
282 /* Determine the names of the default startup and library file */
283 if (Target != TGT_NONE) {
285 /* Get a pointer to the system name and its length */
286 const char* TargetName = TargetNames [Target];
287 unsigned TargetNameLen = strlen (TargetName);
289 /* Set the startup file */
290 TargetCRT0 = xmalloc (TargetNameLen + 2 + 1);
291 memcpy (TargetCRT0, TargetName, TargetNameLen);
292 strcpy (TargetCRT0 + TargetNameLen, ".o");
294 /* Set the library file */
295 TargetLib = xmalloc (TargetNameLen + 4 + 1);
296 memcpy (TargetLib, TargetName, TargetNameLen);
297 strcpy (TargetLib + TargetNameLen, ".lib");
304 /*****************************************************************************/
306 /*****************************************************************************/
310 static void ExecProgram (CmdDesc* Cmd)
311 /* Execute a subprocess with the given name/parameters. Exit on errors. */
315 /* If in debug mode, output the command line we will execute */
317 printf ("Executing: ");
318 CmdPrint (Cmd, stdout);
322 /* Call the program */
323 Status = spawnvp (P_WAIT, Cmd->Name, Cmd->Args);
325 /* Check the result code */
327 /* Error executing the program */
328 Error ("Cannot execute `%s': %s", Cmd->Name, strerror (errno));
329 } else if (Status != 0) {
330 /* Called program had an error */
337 static void Link (void)
338 /* Link the resulting executable */
342 /* If we have a linker config file given, add it to the command line.
343 * Otherwise pass the target to the linker if we have one.
347 Error ("Cannot use -C and --module together");
349 CmdAddArg2 (&LD65, "-C", LinkerConfig);
351 CmdSetTarget (&LD65, TGT_MODULE);
353 CmdSetTarget (&LD65, Target);
356 /* Determine which target libraries are needed */
359 /* Since linking is always the final step, if we have an output file name
360 * given, set it here. If we don't have an explicit output name given,
361 * try to build one from the name of the first input file.
365 CmdSetOutput (&LD65, OutputName);
367 } else if (FirstInput && FindExt (FirstInput)) { /* Only if ext present! */
369 const char* Extension = Module? MODULE_EXT : "";
370 char* Output = MakeFilename (FirstInput, Extension);
371 CmdSetOutput (&LD65, Output);
376 /* If we have a startup file and if we are not linking a module, add its
377 * name as a parameter
379 if (TargetCRT0 && !Module) {
380 CmdAddArg (&LD65, TargetCRT0);
383 /* Add all object files as parameters */
384 for (I = 0; I < LD65.FileCount; ++I) {
385 CmdAddArg (&LD65, LD65.Files [I]);
388 /* Add the system runtime library */
390 CmdAddArg (&LD65, TargetLib);
393 /* Terminate the argument list with a NULL pointer */
394 CmdAddArg (&LD65, 0);
396 /* Call the linker */
402 static void Assemble (const char* File)
403 /* Assemble the given file */
405 /* Remember the current assembler argument count */
406 unsigned ArgCount = CA65.ArgCount;
408 /* Set the target system */
409 CmdSetTarget (&CA65, Target);
411 /* If we won't link, this is the final step. In this case, set the
414 if (DontLink && OutputName) {
415 CmdSetOutput (&CA65, OutputName);
417 /* The object file name will be the name of the source file
418 * with .s replaced by ".o". Add this file to the list of
421 char* ObjName = MakeFilename (File, ".o");
422 CmdAddFile (&LD65, ObjName);
426 /* Add the file as argument for the assembler */
427 CmdAddArg (&CA65, File);
429 /* Add a NULL pointer to terminate the argument list */
430 CmdAddArg (&CA65, 0);
432 /* Run the assembler */
435 /* Remove the excess arguments */
436 CmdDelArgs (&CA65, ArgCount);
441 static void Compile (const char* File)
442 /* Compile the given file */
446 /* Remember the current compiler argument count */
447 unsigned ArgCount = CC65.ArgCount;
449 /* Set the target system */
450 CmdSetTarget (&CC65, Target);
452 /* If we won't link, this is the final step. In this case, set the
455 if (DontAssemble && OutputName) {
456 CmdSetOutput (&CC65, OutputName);
458 /* The assembler file name will be the name of the source file
459 * with .c replaced by ".s".
461 AsmName = MakeFilename (File, ".s");
464 /* Add the file as argument for the compiler */
465 CmdAddArg (&CC65, File);
467 /* Add a NULL pointer to terminate the argument list */
468 CmdAddArg (&CC65, 0);
470 /* Run the compiler */
473 /* Remove the excess arguments */
474 CmdDelArgs (&CC65, ArgCount);
476 /* If this is not the final step, assemble the generated file, then
481 if (remove (AsmName) < 0) {
482 Warning ("Cannot remove temporary file `%s': %s",
483 AsmName, strerror (errno));
491 static void CompileRes (const char* File)
492 /* Compile the given geos resource file */
496 /* Remember the current assembler argument count */
497 unsigned ArgCount = GRC.ArgCount;
499 /* The assembler file name will be the name of the source file
500 * with .grc replaced by ".s".
502 AsmName = MakeFilename (File, ".s");
504 /* Add the file as argument for the resource compiler */
505 CmdAddArg (&GRC, File);
507 /* Add a NULL pointer to terminate the argument list */
510 /* Run the compiler */
513 /* Remove the excess arguments */
514 CmdDelArgs (&GRC, ArgCount);
516 /* If this is not the final step, assemble the generated file, then
521 if (remove (AsmName) < 0) {
522 Warning ("Cannot remove temporary file `%s': %s",
523 AsmName, strerror (errno));
527 /* Free the assembler file name which was allocated from the heap */
533 static void ConvertO65 (const char* File)
534 /* Convert an o65 object file into an assembler file */
538 /* Remember the current converter argument count */
539 unsigned ArgCount = CO65.ArgCount;
541 /* If we won't link, this is the final step. In this case, set the
544 if (DontAssemble && OutputName) {
545 CmdSetOutput (&CO65, OutputName);
547 /* The assembler file name will be the name of the source file
548 * with .c replaced by ".s".
550 AsmName = MakeFilename (File, ".s");
553 /* Add the file as argument for the object file converter */
554 CmdAddArg (&CO65, File);
556 /* Add a NULL pointer to terminate the argument list */
557 CmdAddArg (&CO65, 0);
559 /* Run the converter */
562 /* Remove the excess arguments */
563 CmdDelArgs (&CO65, ArgCount);
565 /* If this is not the final step, assemble the generated file, then
570 if (remove (AsmName) < 0) {
571 Warning ("Cannot remove temporary file `%s': %s",
572 AsmName, strerror (errno));
576 /* Free the assembler file name which was allocated from the heap */
582 /*****************************************************************************/
584 /*****************************************************************************/
588 static void Usage (void)
589 /* Print usage information and exit */
592 "Usage: %s [options] file [...]\n"
594 " -c\t\t\tCompile and assemble but don't link\n"
595 " -d\t\t\tDebug mode\n"
596 " -g\t\t\tAdd debug info\n"
597 " -h\t\t\tHelp (this text)\n"
598 " -l\t\t\tCreate an assembler listing\n"
599 " -m name\t\tCreate a map file\n"
600 " -mm model\t\tSet the memory model\n"
601 " -o name\t\tName the output file\n"
602 " -r\t\t\tEnable register variables\n"
603 " -t sys\t\tSet the target system\n"
604 " -v\t\t\tVerbose mode\n"
605 " -vm\t\t\tVerbose map file\n"
606 " -A\t\t\tStrict ANSI mode\n"
607 " -C name\t\tUse linker config file\n"
608 " -Cl\t\t\tMake local variables static\n"
609 " -D sym[=defn]\t\tDefine a preprocessor symbol\n"
610 " -I dir\t\tSet a compiler include directory path\n"
611 " -L path\t\tSpecify a library search path\n"
612 " -Ln name\t\tCreate a VICE label file\n"
613 " -O\t\t\tOptimize code\n"
614 " -Oi\t\t\tOptimize code, inline functions\n"
615 " -Or\t\t\tOptimize code, honour the register keyword\n"
616 " -Os\t\t\tOptimize code, inline known C funtions\n"
617 " -S\t\t\tCompile but don't assemble and link\n"
618 " -T\t\t\tInclude source as comment\n"
619 " -V\t\t\tPrint the version number\n"
620 " -W\t\t\tSuppress warnings\n"
623 " --add-source\t\tInclude source as comment\n"
624 " --ansi\t\tStrict ANSI mode\n"
625 " --asm-define sym[=v]\tDefine an assembler symbol\n"
626 " --asm-include-dir dir\tSet an assembler include directory\n"
627 " --bss-label name\tDefine and export a BSS segment label\n"
628 " --bss-name seg\tSet the name of the BSS segment\n"
629 " --cfg-path path\tSpecify a config file search path\n"
630 " --check-stack\t\tGenerate stack overflow checks\n"
631 " --code-label name\tDefine and export a CODE segment label\n"
632 " --code-name seg\tSet the name of the CODE segment\n"
633 " --codesize x\t\tAccept larger code by factor x\n"
634 " --config name\t\tUse linker config file\n"
635 " --cpu type\t\tSet cpu type\n"
636 " --create-dep\t\tCreate a make dependency file\n"
637 " --data-label name\tDefine and export a DATA segment label\n"
638 " --data-name seg\tSet the name of the DATA segment\n"
639 " --debug\t\tDebug mode\n"
640 " --debug-info\t\tAdd debug info\n"
641 " --feature name\tSet an emulation feature\n"
642 " --forget-inc-paths\tForget include search paths (compiler)\n"
643 " --help\t\tHelp (this text)\n"
644 " --include-dir dir\tSet a compiler include directory path\n"
645 " --lib file\t\tLink this library\n"
646 " --lib-path path\tSpecify a library search path\n"
647 " --list-targets\tList all available targets\n"
648 " --listing\t\tCreate an assembler listing\n"
649 " --list-bytes n\tNumber of bytes per assembler listing line\n"
650 " --mapfile name\tCreate a map file\n"
651 " --memory-model model\tSet the memory model\n"
652 " --module\t\tLink as a module\n"
653 " --module-id id\tSpecify a module id for the linker\n"
654 " --o65-model model\tOverride the o65 model\n"
655 " --obj file\t\tLink this object file\n"
656 " --obj-path path\tSpecify an object file search path\n"
657 " --register-space b\tSet space available for register variables\n"
658 " --register-vars\tEnable register variables\n"
659 " --rodata-name seg\tSet the name of the RODATA segment\n"
660 " --signed-chars\tDefault characters are signed\n"
661 " --start-addr addr\tSet the default start address\n"
662 " --static-locals\tMake local variables static\n"
663 " --target sys\t\tSet the target system\n"
664 " --version\t\tPrint the version number\n"
665 " --verbose\t\tVerbose mode\n"
666 " --zeropage-label name\tDefine and export a ZEROPAGE segment label\n"
667 " --zeropage-name seg\tSet the name of the ZEROPAGE segment\n",
673 static void OptAddSource (const char* Opt attribute ((unused)),
674 const char* Arg attribute ((unused)))
675 /* Strict source code as comments to the generated asm code */
677 CmdAddArg (&CC65, "-T");
682 static void OptAnsi (const char* Opt attribute ((unused)),
683 const char* Arg attribute ((unused)))
684 /* Strict ANSI mode (compiler) */
686 CmdAddArg (&CC65, "-A");
691 static void OptAsmDefine (const char* Opt attribute ((unused)), const char* Arg)
692 /* Define an assembler symbol (assembler) */
694 CmdAddArg2 (&CA65, "-D", Arg);
699 static void OptAsmIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
700 /* Include directory (assembler) */
702 CmdAddArg2 (&CA65, "-I", Arg);
707 static void OptBssLabel (const char* Opt attribute ((unused)), const char* Arg)
708 /* Handle the --bss-label option */
710 CmdAddArg2 (&CO65, "--bss-label", Arg);
715 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
716 /* Handle the --bss-name option */
718 CmdAddArg2 (&CC65, "--bss-name", Arg);
719 CmdAddArg2 (&CO65, "--bss-name", Arg);
724 static void OptCfgPath (const char* Opt attribute ((unused)), const char* Arg)
725 /* Config file search path (linker) */
727 CmdAddArg2 (&LD65, "--cfg-path", Arg);
732 static void OptCheckStack (const char* Opt attribute ((unused)),
733 const char* Arg attribute ((unused)))
734 /* Handle the --check-stack option */
736 CmdAddArg (&CC65, "--check-stack");
741 static void OptCodeLabel (const char* Opt attribute ((unused)), const char* Arg)
742 /* Handle the --code-label option */
744 CmdAddArg2 (&CO65, "--code-label", Arg);
749 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
750 /* Handle the --code-name option */
752 CmdAddArg2 (&CC65, "--code-name", Arg);
753 CmdAddArg2 (&CO65, "--code-name", Arg);
758 static void OptCodeSize (const char* Opt attribute ((unused)), const char* Arg)
759 /* Handle the --codesize option */
761 CmdAddArg2 (&CC65, "--codesize", Arg);
766 static void OptConfig (const char* Opt attribute ((unused)), const char* Arg)
767 /* Config file (linker) */
770 Error ("Cannot specify -C/--config twice");
777 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
778 /* Handle the --cpu option */
780 /* Add the cpu type to the assembler and compiler */
781 CmdAddArg2 (&CA65, "--cpu", Arg);
782 CmdAddArg2 (&CC65, "--cpu", Arg);
787 static void OptCreateDep (const char* Opt attribute ((unused)),
788 const char* Arg attribute ((unused)))
789 /* Handle the --create-dep option */
791 CmdAddArg (&CC65, "--create-dep");
796 static void OptDataLabel (const char* Opt attribute ((unused)), const char* Arg)
797 /* Handle the --data-label option */
799 CmdAddArg2 (&CO65, "--data-label", Arg);
804 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
805 /* Handle the --data-name option */
807 CmdAddArg2 (&CC65, "--data-name", Arg);
808 CmdAddArg2 (&CO65, "--data-name", Arg);
813 static void OptDebug (const char* Opt attribute ((unused)),
814 const char* Arg attribute ((unused)))
815 /* Debug mode (compiler and cl65 utility) */
817 CmdAddArg (&CC65, "-d");
818 CmdAddArg (&CO65, "-d");
824 static void OptDebugInfo (const char* Opt attribute ((unused)),
825 const char* Arg attribute ((unused)))
826 /* Debug Info - add to compiler and assembler */
828 CmdAddArg (&CC65, "-g");
829 CmdAddArg (&CA65, "-g");
830 CmdAddArg (&CO65, "-g");
835 static void OptFeature (const char* Opt attribute ((unused)), const char* Arg)
836 /* Emulation features for the assembler */
838 CmdAddArg2 (&CA65, "--feature", Arg);
843 static void OptForgetIncPaths (const char* Opt attribute ((unused)), const char* Arg)
844 /* Forget all currently defined include paths */
846 CmdAddArg2 (&CC65, "--forget-inc-paths", Arg);
851 static void OptHelp (const char* Opt attribute ((unused)),
852 const char* Arg attribute ((unused)))
853 /* Print help - cl65 */
861 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
862 /* Include directory (compiler) */
864 CmdAddArg2 (&CC65, "-I", Arg);
869 static void OptLib (const char* Opt attribute ((unused)), const char* Arg)
870 /* Library file follows (linker) */
872 CmdAddArg2 (&LD65, "--lib", Arg);
877 static void OptLibPath (const char* Opt attribute ((unused)), const char* Arg)
878 /* Library search path (linker) */
880 CmdAddArg2 (&LD65, "--lib-path", Arg);
885 static void OptListBytes (const char* Opt attribute ((unused)), const char* Arg)
886 /* Set the maximum number of bytes per asm listing line */
888 CmdAddArg2 (&CA65, "--list-bytes", Arg);
893 static void OptListing (const char* Opt attribute ((unused)),
894 const char* Arg attribute ((unused)))
895 /* Create an assembler listing */
897 CmdAddArg (&CA65, "-l");
902 static void OptListTargets (const char* Opt attribute ((unused)),
903 const char* Arg attribute ((unused)))
904 /* List all targets */
908 /* List the targets */
909 for (I = TGT_NONE; I < TGT_COUNT; ++I) {
910 printf ("%s\n", TargetNames[I]);
919 static void OptMapFile (const char* Opt attribute ((unused)), const char* Arg)
920 /* Create a map file */
922 /* Create a map file (linker) */
923 CmdAddArg2 (&LD65, "-m", Arg);
928 static void OptMemoryModel (const char* Opt attribute ((unused)), const char* Arg)
929 /* Set the memory model */
931 mmodel_t MemoryModel = FindMemoryModel (Arg);
932 if (MemoryModel == MMODEL_UNKNOWN) {
933 Error ("Unknown memory model: %s", Arg);
934 } else if (MemoryModel == MMODEL_HUGE) {
935 Error ("Unsupported memory model: %s", Arg);
937 CmdAddArg2 (&CA65, "-mm", Arg);
938 CmdAddArg2 (&CC65, "-mm", Arg);
944 static void OptModule (const char* Opt attribute ((unused)),
945 const char* Arg attribute ((unused)))
946 /* Link as a module */
953 static void OptModuleId (const char* Opt attribute ((unused)), const char* Arg)
954 /* Specify a module if for the linker */
956 /* Pass it straight to the linker */
957 CmdAddArg2 (&LD65, "--module-id", Arg);
962 static void OptO65Model (const char* Opt attribute ((unused)), const char* Arg)
963 /* Handle the --o65-model option */
965 CmdAddArg2 (&CO65, "-m", Arg);
970 static void OptObj (const char* Opt attribute ((unused)), const char* Arg)
971 /* Object file follows (linker) */
973 CmdAddArg2 (&LD65, "--obj", Arg);
978 static void OptObjPath (const char* Opt attribute ((unused)), const char* Arg)
979 /* Object file search path (linker) */
981 CmdAddArg2 (&LD65, "--obj-path", Arg);
986 static void OptRegisterSpace (const char* Opt attribute ((unused)), const char* Arg)
987 /* Handle the --register-space option */
989 CmdAddArg2 (&CC65, "--register-space", Arg);
994 static void OptRegisterVars (const char* Opt attribute ((unused)),
995 const char* Arg attribute ((unused)))
996 /* Handle the --register-vars option */
998 CmdAddArg (&CC65, "-r");
1003 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
1004 /* Handle the --rodata-name option */
1006 CmdAddArg2 (&CC65, "--rodata-name", Arg);
1011 static void OptSignedChars (const char* Opt attribute ((unused)),
1012 const char* Arg attribute ((unused)))
1013 /* Make default characters signed */
1015 CmdAddArg (&CC65, "-j");
1020 static void OptStartAddr (const char* Opt attribute ((unused)), const char* Arg)
1021 /* Set the default start address */
1023 CmdAddArg2 (&LD65, "-S", Arg);
1028 static void OptStaticLocals (const char* Opt attribute ((unused)),
1029 const char* Arg attribute ((unused)))
1030 /* Place local variables in static storage */
1032 CmdAddArg (&CC65, "-Cl");
1037 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
1038 /* Set the target system */
1040 Target = FindTarget (Arg);
1041 if (Target == TGT_UNKNOWN) {
1042 Error ("No such target system: `%s'", Arg);
1043 } else if (Target == TGT_MODULE) {
1044 Error ("Cannot use `module' as target, use --module instead");
1050 static void OptVerbose (const char* Opt attribute ((unused)),
1051 const char* Arg attribute ((unused)))
1052 /* Verbose mode (compiler, assembler, linker) */
1054 CmdAddArg (&CC65, "-v");
1055 CmdAddArg (&CA65, "-v");
1056 CmdAddArg (&CO65, "-v");
1057 CmdAddArg (&LD65, "-v");
1062 static void OptVersion (const char* Opt attribute ((unused)),
1063 const char* Arg attribute ((unused)))
1064 /* Print version number */
1067 "cl65 V%u.%u.%u - (C) Copyright 1998-2003 Ullrich von Bassewitz\n",
1068 VER_MAJOR, VER_MINOR, VER_PATCH);
1073 static void OptZeropageLabel (const char* Opt attribute ((unused)), const char* Arg)
1074 /* Handle the --zeropage-label option */
1076 CmdAddArg2 (&CO65, "--zeropage-label", Arg);
1081 static void OptZeropageName (const char* Opt attribute ((unused)), const char* Arg)
1082 /* Handle the --zeropage-name option */
1084 CmdAddArg2 (&CO65, "--zeropage-name", Arg);
1089 int main (int argc, char* argv [])
1090 /* Utility main program */
1092 /* Program long options */
1093 static const LongOpt OptTab[] = {
1094 { "--add-source", 0, OptAddSource },
1095 { "--ansi", 0, OptAnsi },
1096 { "--asm-define", 1, OptAsmDefine },
1097 { "--asm-include-dir", 1, OptAsmIncludeDir },
1098 { "--bss-label", 1, OptBssLabel },
1099 { "--bss-name", 1, OptBssName },
1100 { "--cfg-path", 1, OptCfgPath },
1101 { "--check-stack", 0, OptCheckStack },
1102 { "--code-label", 1, OptCodeLabel },
1103 { "--code-name", 1, OptCodeName },
1104 { "--codesize", 1, OptCodeSize },
1105 { "--config", 1, OptConfig },
1106 { "--cpu", 1, OptCPU },
1107 { "--create-dep", 0, OptCreateDep },
1108 { "--data-label", 1, OptDataLabel },
1109 { "--data-name", 1, OptDataName },
1110 { "--debug", 0, OptDebug },
1111 { "--debug-info", 0, OptDebugInfo },
1112 { "--feature", 1, OptFeature },
1113 { "--forget-inc-paths", 0, OptForgetIncPaths },
1114 { "--help", 0, OptHelp },
1115 { "--include-dir", 1, OptIncludeDir },
1116 { "--lib", 1, OptLib },
1117 { "--lib-path", 1, OptLibPath },
1118 { "--list-targets", 0, OptListTargets },
1119 { "--listing", 0, OptListing },
1120 { "--list-bytes", 1, OptListBytes },
1121 { "--mapfile", 1, OptMapFile },
1122 { "--memory-model", 1, OptMemoryModel },
1123 { "--module", 0, OptModule },
1124 { "--module-id", 1, OptModuleId },
1125 { "--o65-model", 1, OptO65Model },
1126 { "--obj", 1, OptObj },
1127 { "--obj-path", 1, OptObjPath },
1128 { "--register-space", 1, OptRegisterSpace },
1129 { "--register-vars", 0, OptRegisterVars },
1130 { "--rodata-name", 1, OptRodataName },
1131 { "--signed-chars", 0, OptSignedChars },
1132 { "--start-addr", 1, OptStartAddr },
1133 { "--static-locals", 0, OptStaticLocals },
1134 { "--target", 1, OptTarget },
1135 { "--verbose", 0, OptVerbose },
1136 { "--version", 0, OptVersion },
1137 { "--zeropage-label", 1, OptZeropageLabel },
1138 { "--zeropage-name", 1, OptZeropageName },
1143 /* Initialize the cmdline module */
1144 InitCmdLine (&argc, &argv, "cl65");
1146 /* Initialize the command descriptors */
1147 CmdInit (&CC65, "cc65");
1148 CmdInit (&CA65, "ca65");
1149 CmdInit (&CO65, "co65");
1150 CmdInit (&LD65, "ld65");
1151 CmdInit (&GRC, "grc");
1153 /* Our default target is the C64 instead of "none" */
1156 /* Check the parameters */
1158 while (I < ArgCount) {
1160 /* Get the argument */
1161 const char* Arg = ArgVec[I];
1163 /* Check for an option */
1164 if (Arg [0] == '-') {
1169 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
1173 /* Strict ANSI mode (compiler) */
1178 if (Arg[2] == 'l' && Arg[3] == '\0') {
1179 /* Make local variables static */
1180 OptStaticLocals (Arg, 0);
1182 /* Specify linker config file */
1183 OptConfig (Arg, GetArg (&I, 2));
1188 /* Define a preprocessor symbol (compiler) */
1189 CmdAddArg2 (&CC65, "-D", GetArg (&I, 2));
1193 /* Include directory (compiler) */
1194 OptIncludeDir (Arg, GetArg (&I, 2));
1198 if (Arg[2] == 'n' && Arg[3] == '\0') {
1199 /* VICE label file (linker) */
1200 CmdAddArg2 (&LD65, "-Ln", GetArg (&I, 3));
1202 /* Library search path (linker) */
1203 OptLibPath (Arg, GetArg (&I, 2));
1208 /* Optimize code (compiler, also covers -Oi and others) */
1209 CmdAddArg (&CC65, Arg);
1213 /* Dont assemble and link the created files */
1214 DontLink = DontAssemble = 1;
1218 /* Include source as comment (compiler) */
1219 OptAddSource (Arg, 0);
1223 /* Print version number */
1224 OptVersion (Arg, 0);
1228 /* Suppress warnings - compiler and assembler */
1229 CmdAddArg (&CC65, "-W");
1230 CmdAddArg2 (&CA65, "-W", "0");
1234 /* Don't link the resulting files */
1239 /* Debug mode (compiler) */
1244 /* Debugging - add to compiler and assembler */
1245 OptDebugInfo (Arg, 0);
1250 /* Print help - cl65 */
1255 /* Default characters are signed */
1256 OptSignedChars (Arg, 0);
1260 /* Create an assembler listing */
1261 OptListing (Arg, 0);
1265 /* Create a map file (linker) */
1266 OptMapFile (Arg, GetArg (&I, 2));
1270 /* Name the output file */
1271 OutputName = GetArg (&I, 2);
1275 /* Enable register variables */
1276 OptRegisterVars (Arg, 0);
1280 /* Set target system - compiler, assembler and linker */
1281 OptTarget (Arg, GetArg (&I, 2));
1285 if (Arg [2] == 'm') {
1286 /* Verbose map file (linker) */
1287 CmdAddArg (&LD65, "-vm");
1289 /* Verbose mode (compiler, assembler, linker) */
1290 OptVerbose (Arg, 0);
1295 UnknownOption (Arg);
1299 /* Remember the first file name */
1300 if (FirstInput == 0) {
1304 /* Determine the file type by the extension */
1305 switch (GetFileType (Arg)) {
1308 /* Compile the file */
1313 /* Assemble the file */
1314 if (!DontAssemble) {
1321 /* Add to the linker files */
1322 CmdAddFile (&LD65, Arg);
1326 /* Add to the resource compiler files */
1331 /* Add the the object file converter files */
1336 Error ("Don't know what to do with `%s'", Arg);
1346 /* Check if we had any input files */
1347 if (FirstInput == 0) {
1348 Warning ("No input files");
1351 /* Link the given files if requested and if we have any */
1352 if (DontLink == 0 && LD65.FileCount > 0) {
1356 /* Return an apropriate exit code */
1357 return EXIT_SUCCESS;