1 /*****************************************************************************/
5 /* Main module for the cl65 compile and link utility */
9 /* (C) 1999-2005, Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
36 /* Check out if we have a spawn() function on the system, or if we must use
39 #if defined(__WATCOMC__) || defined(_MSC_VER) || defined(__MINGW32__) || defined(__DJGPP__)
73 /*****************************************************************************/
75 /*****************************************************************************/
79 /* Struct that describes a command */
80 typedef struct CmdDesc CmdDesc;
82 char* Name; /* The command name */
84 unsigned ArgCount; /* Count of arguments */
85 unsigned ArgMax; /* Maximum count of arguments */
86 char** Args; /* The arguments */
88 unsigned FileCount; /* Count of files to translate */
89 unsigned FileMax; /* Maximum count of files */
90 char** Files; /* The files */
93 /* Command descriptors for the different programs */
94 static CmdDesc CC65 = { 0, 0, 0, 0, 0, 0, 0 };
95 static CmdDesc CA65 = { 0, 0, 0, 0, 0, 0, 0 };
96 static CmdDesc CO65 = { 0, 0, 0, 0, 0, 0, 0 };
97 static CmdDesc LD65 = { 0, 0, 0, 0, 0, 0, 0 };
98 static CmdDesc GRC = { 0, 0, 0, 0, 0, 0, 0 };
100 /* Variables controlling the steps we're doing */
101 static int DontLink = 0;
102 static int DontAssemble = 0;
104 /* The name of the output file, NULL if none given */
105 static const char* OutputName = 0;
107 /* The name of the linker configuration file if given */
108 static const char* LinkerConfig = 0;
110 /* The name of the first input file. This will be used to construct the
111 * executable file name if no explicit name is given.
113 static const char* FirstInput = 0;
115 /* Remember if we should link a module */
116 static int Module = 0;
118 /* Extension used for a module */
119 #define MODULE_EXT ".o65"
121 /* Name of the crt0 object file and the runtime library */
122 static char* TargetCRT0 = 0;
123 static char* TargetLib = 0;
127 /*****************************************************************************/
128 /* Include the system specific spawn function */
129 /*****************************************************************************/
133 #if defined(NEED_SPAWN)
134 # if defined(SPAWN_UNIX)
135 # include "spawn-unix.inc"
136 # elif defined(SPAWN_AMIGA)
137 # include "spawn-amiga.inc"
139 # error "Don't know which spawn module to include!"
145 /*****************************************************************************/
146 /* Command structure handling */
147 /*****************************************************************************/
151 static void CmdExpand (CmdDesc* Cmd)
152 /* Expand the argument vector */
154 unsigned NewMax = Cmd->ArgMax + 10;
155 char** NewArgs = xmalloc (NewMax * sizeof (char*));
156 memcpy (NewArgs, Cmd->Args, Cmd->ArgMax * sizeof (char*));
159 Cmd->ArgMax = NewMax;
164 static void CmdAddArg (CmdDesc* Cmd, const char* Arg)
165 /* Add a new argument to the command */
167 /* Expand the argument vector if needed */
168 if (Cmd->ArgCount >= Cmd->ArgMax) {
172 /* Add a copy of the new argument, allow a NULL pointer */
174 Cmd->Args[Cmd->ArgCount++] = xstrdup (Arg);
176 Cmd->Args[Cmd->ArgCount++] = 0;
182 static void CmdAddArg2 (CmdDesc* Cmd, const char* Arg1, const char* Arg2)
183 /* Add a new argument pair to the command */
185 CmdAddArg (Cmd, Arg1);
186 CmdAddArg (Cmd, Arg2);
191 static void CmdAddArgList (CmdDesc* Cmd, const char* ArgList)
192 /* Add a list of arguments separated by commas */
194 const char* Arg = ArgList;
198 if (*P == '\0' || *P == ',') {
200 /* End of argument, add it */
201 unsigned Len = P - Arg;
203 /* Expand the argument vector if needed */
204 if (Cmd->ArgCount >= Cmd->ArgMax) {
208 /* Add the new argument */
209 Cmd->Args[Cmd->ArgCount] = memcpy (xmalloc (Len + 1), Arg, Len);
210 Cmd->Args[Cmd->ArgCount][Len] = '\0';
213 /* If the argument was terminated by a comma, skip it, otherwise
217 /* Start over at next char */
223 /* Skip other chars */
232 static void CmdDelArgs (CmdDesc* Cmd, unsigned LastValid)
233 /* Remove all arguments with an index greater than LastValid */
235 while (Cmd->ArgCount > LastValid) {
237 xfree (Cmd->Args [Cmd->ArgCount]);
238 Cmd->Args [Cmd->ArgCount] = 0;
244 static void CmdAddFile (CmdDesc* Cmd, const char* File)
245 /* Add a new file to the command */
247 /* Expand the file vector if needed */
248 if (Cmd->FileCount == Cmd->FileMax) {
249 unsigned NewMax = Cmd->FileMax + 10;
250 char** NewFiles = xmalloc (NewMax * sizeof (char*));
251 memcpy (NewFiles, Cmd->Files, Cmd->FileMax * sizeof (char*));
253 Cmd->Files = NewFiles;
254 Cmd->FileMax = NewMax;
257 /* If the file name is not NULL (which is legal and is used to terminate
258 * the file list), check if the file name does already exist in the file
259 * list and print a warning if so. Regardless of the search result, add
264 for (I = 0; I < Cmd->FileCount; ++I) {
265 if (strcmp (Cmd->Files[I], File) == 0) {
267 Warning ("Duplicate file in argument list: `%s'", File);
268 /* No need to search further */
274 Cmd->Files [Cmd->FileCount++] = xstrdup (File);
276 /* Add a NULL pointer */
277 Cmd->Files [Cmd->FileCount++] = 0;
283 static void CmdInit (CmdDesc* Cmd, const char* Path)
284 /* Initialize the command using the given path to the executable */
286 /* Remember the command */
287 Cmd->Name = xstrdup (Path);
289 /* Use the command name as first argument */
290 CmdAddArg (Cmd, Path);
295 static void CmdSetOutput (CmdDesc* Cmd, const char* File)
296 /* Set the output file in a command desc */
298 CmdAddArg2 (Cmd, "-o", File);
303 static void CmdSetTarget (CmdDesc* Cmd, target_t Target)
304 /* Set the output file in a command desc */
306 CmdAddArg2 (Cmd, "-t", TargetNames[Target]);
311 static void CmdPrint (CmdDesc* Cmd, FILE* F)
312 /* Output the command line encoded in the command desc */
315 for (I = 0; I < Cmd->ArgCount && Cmd->Args[I] != 0; ++I) {
316 fprintf (F, "%s ", Cmd->Args[I]);
322 /*****************************************************************************/
323 /* Target handling */
324 /*****************************************************************************/
328 static void SetTargetFiles (void)
329 /* Set the target system files */
331 /* Determine the names of the default startup and library file */
332 if (Target != TGT_NONE) {
334 /* Get a pointer to the system name and its length */
335 const char* TargetName = TargetNames [Target];
336 unsigned TargetNameLen = strlen (TargetName);
338 /* Set the startup file */
339 TargetCRT0 = xmalloc (TargetNameLen + 2 + 1);
340 memcpy (TargetCRT0, TargetName, TargetNameLen);
341 strcpy (TargetCRT0 + TargetNameLen, ".o");
343 /* Set the library file */
344 TargetLib = xmalloc (TargetNameLen + 4 + 1);
345 memcpy (TargetLib, TargetName, TargetNameLen);
346 strcpy (TargetLib + TargetNameLen, ".lib");
353 /*****************************************************************************/
355 /*****************************************************************************/
359 static void ExecProgram (CmdDesc* Cmd)
360 /* Execute a subprocess with the given name/parameters. Exit on errors. */
364 /* If in debug mode, output the command line we will execute */
366 printf ("Executing: ");
367 CmdPrint (Cmd, stdout);
371 /* Call the program */
372 Status = spawnvp (P_WAIT, Cmd->Name, Cmd->Args);
374 /* Check the result code */
376 /* Error executing the program */
377 Error ("Cannot execute `%s': %s", Cmd->Name, strerror (errno));
378 } else if (Status != 0) {
379 /* Called program had an error */
386 static void Link (void)
387 /* Link the resulting executable */
391 /* If we have a linker config file given, add it to the command line.
392 * Otherwise pass the target to the linker if we have one.
396 Error ("Cannot use -C and --module together");
398 CmdAddArg2 (&LD65, "-C", LinkerConfig);
400 CmdSetTarget (&LD65, TGT_MODULE);
402 CmdSetTarget (&LD65, Target);
405 /* Determine which target libraries are needed */
408 /* Since linking is always the final step, if we have an output file name
409 * given, set it here. If we don't have an explicit output name given,
410 * try to build one from the name of the first input file.
414 CmdSetOutput (&LD65, OutputName);
416 } else if (FirstInput && FindExt (FirstInput)) { /* Only if ext present! */
418 const char* Extension = Module? MODULE_EXT : "";
419 char* Output = MakeFilename (FirstInput, Extension);
420 CmdSetOutput (&LD65, Output);
425 /* If we have a startup file and if we are not linking a module, add its
426 * name as a parameter
428 if (TargetCRT0 && !Module) {
429 CmdAddArg (&LD65, TargetCRT0);
432 /* Add all object files as parameters */
433 for (I = 0; I < LD65.FileCount; ++I) {
434 CmdAddArg (&LD65, LD65.Files [I]);
437 /* Add the system runtime library */
439 CmdAddArg (&LD65, TargetLib);
442 /* Terminate the argument list with a NULL pointer */
443 CmdAddArg (&LD65, 0);
445 /* Call the linker */
451 static void Assemble (const char* File)
452 /* Assemble the given file */
454 /* Remember the current assembler argument count */
455 unsigned ArgCount = CA65.ArgCount;
457 /* Set the target system */
458 CmdSetTarget (&CA65, Target);
460 /* If we won't link, this is the final step. In this case, set the
463 if (DontLink && OutputName) {
464 CmdSetOutput (&CA65, OutputName);
466 /* The object file name will be the name of the source file
467 * with .s replaced by ".o". Add this file to the list of
470 char* ObjName = MakeFilename (File, ".o");
471 CmdAddFile (&LD65, ObjName);
475 /* Add the file as argument for the assembler */
476 CmdAddArg (&CA65, File);
478 /* Add a NULL pointer to terminate the argument list */
479 CmdAddArg (&CA65, 0);
481 /* Run the assembler */
484 /* Remove the excess arguments */
485 CmdDelArgs (&CA65, ArgCount);
490 static void Compile (const char* File)
491 /* Compile the given file */
495 /* Remember the current compiler argument count */
496 unsigned ArgCount = CC65.ArgCount;
498 /* Set the target system */
499 CmdSetTarget (&CC65, Target);
501 /* If we won't link, this is the final step. In this case, set the
504 if (DontAssemble && OutputName) {
505 CmdSetOutput (&CC65, OutputName);
507 /* The assembler file name will be the name of the source file
508 * with .c replaced by ".s".
510 AsmName = MakeFilename (File, ".s");
513 /* Add the file as argument for the compiler */
514 CmdAddArg (&CC65, File);
516 /* Add a NULL pointer to terminate the argument list */
517 CmdAddArg (&CC65, 0);
519 /* Run the compiler */
522 /* Remove the excess arguments */
523 CmdDelArgs (&CC65, ArgCount);
525 /* If this is not the final step, assemble the generated file, then
530 if (remove (AsmName) < 0) {
531 Warning ("Cannot remove temporary file `%s': %s",
532 AsmName, strerror (errno));
540 static void CompileRes (const char* File)
541 /* Compile the given geos resource file */
545 /* Remember the current assembler argument count */
546 unsigned ArgCount = GRC.ArgCount;
548 /* The assembler file name will be the name of the source file
549 * with .grc replaced by ".s".
551 AsmName = MakeFilename (File, ".s");
553 /* Add the file as argument for the resource compiler */
554 CmdAddArg (&GRC, File);
556 /* Add a NULL pointer to terminate the argument list */
559 /* Run the compiler */
562 /* Remove the excess arguments */
563 CmdDelArgs (&GRC, ArgCount);
565 /* If this is not the final step, assemble the generated file, then
570 if (remove (AsmName) < 0) {
571 Warning ("Cannot remove temporary file `%s': %s",
572 AsmName, strerror (errno));
576 /* Free the assembler file name which was allocated from the heap */
582 static void ConvertO65 (const char* File)
583 /* Convert an o65 object file into an assembler file */
587 /* Remember the current converter argument count */
588 unsigned ArgCount = CO65.ArgCount;
590 /* If we won't link, this is the final step. In this case, set the
593 if (DontAssemble && OutputName) {
594 CmdSetOutput (&CO65, OutputName);
596 /* The assembler file name will be the name of the source file
597 * with .c replaced by ".s".
599 AsmName = MakeFilename (File, ".s");
602 /* Add the file as argument for the object file converter */
603 CmdAddArg (&CO65, File);
605 /* Add a NULL pointer to terminate the argument list */
606 CmdAddArg (&CO65, 0);
608 /* Run the converter */
611 /* Remove the excess arguments */
612 CmdDelArgs (&CO65, ArgCount);
614 /* If this is not the final step, assemble the generated file, then
619 if (remove (AsmName) < 0) {
620 Warning ("Cannot remove temporary file `%s': %s",
621 AsmName, strerror (errno));
625 /* Free the assembler file name which was allocated from the heap */
631 /*****************************************************************************/
633 /*****************************************************************************/
637 static void Usage (void)
638 /* Print usage information and exit */
640 printf ("Usage: %s [options] file [...]\n"
642 " -c\t\t\tCompile and assemble but don't link\n"
643 " -d\t\t\tDebug mode\n"
644 " -g\t\t\tAdd debug info\n"
645 " -h\t\t\tHelp (this text)\n"
646 " -l\t\t\tCreate an assembler listing\n"
647 " -m name\t\tCreate a map file\n"
648 " -mm model\t\tSet the memory model\n"
649 " -o name\t\tName the output file\n"
650 " -r\t\t\tEnable register variables\n"
651 " -t sys\t\tSet the target system\n"
652 " -v\t\t\tVerbose mode\n"
653 " -vm\t\t\tVerbose map file\n"
654 " -C name\t\tUse linker config file\n"
655 " -Cl\t\t\tMake local variables static\n"
656 " -D sym[=defn]\t\tDefine a preprocessor symbol\n"
657 " -I dir\t\tSet a compiler include directory path\n"
658 " -L path\t\tSpecify a library search path\n"
659 " -Ln name\t\tCreate a VICE label file\n"
660 " -O\t\t\tOptimize code\n"
661 " -Oi\t\t\tOptimize code, inline functions\n"
662 " -Or\t\t\tOptimize code, honour the register keyword\n"
663 " -Os\t\t\tOptimize code, inline known C funtions\n"
664 " -S\t\t\tCompile but don't assemble and link\n"
665 " -T\t\t\tInclude source as comment\n"
666 " -V\t\t\tPrint the version number\n"
667 " -W\t\t\tSuppress warnings\n"
668 " -Wa options\t\tPass options to the assembler\n"
669 " -Wl options\t\tPass options to the linker\n"
672 " --add-source\t\tInclude source as comment\n"
673 " --asm-args options\tPass options to the assembler\n"
674 " --asm-define sym[=v]\tDefine an assembler symbol\n"
675 " --asm-include-dir dir\tSet an assembler include directory\n"
676 " --bss-label name\tDefine and export a BSS segment label\n"
677 " --bss-name seg\tSet the name of the BSS segment\n"
678 " --cfg-path path\tSpecify a config file search path\n"
679 " --check-stack\t\tGenerate stack overflow checks\n"
680 " --code-label name\tDefine and export a CODE segment label\n"
681 " --code-name seg\tSet the name of the CODE segment\n"
682 " --codesize x\t\tAccept larger code by factor x\n"
683 " --config name\t\tUse linker config file\n"
684 " --cpu type\t\tSet cpu type\n"
685 " --create-dep\t\tCreate a make dependency file\n"
686 " --data-label name\tDefine and export a DATA segment label\n"
687 " --data-name seg\tSet the name of the DATA segment\n"
688 " --debug\t\tDebug mode\n"
689 " --debug-info\t\tAdd debug info\n"
690 " --feature name\tSet an emulation feature\n"
691 " --forget-inc-paths\tForget include search paths (compiler)\n"
692 " --help\t\tHelp (this text)\n"
693 " --include-dir dir\tSet a compiler include directory path\n"
694 " --ld-args options\tPass options to the linker\n"
695 " --lib file\t\tLink this library\n"
696 " --lib-path path\tSpecify a library search path\n"
697 " --list-targets\tList all available targets\n"
698 " --listing\t\tCreate an assembler listing\n"
699 " --list-bytes n\tNumber of bytes per assembler listing line\n"
700 " --mapfile name\tCreate a map file\n"
701 " --memory-model model\tSet the memory model\n"
702 " --module\t\tLink as a module\n"
703 " --module-id id\tSpecify a module id for the linker\n"
704 " --o65-model model\tOverride the o65 model\n"
705 " --obj file\t\tLink this object file\n"
706 " --obj-path path\tSpecify an object file search path\n"
707 " --register-space b\tSet space available for register variables\n"
708 " --register-vars\tEnable register variables\n"
709 " --rodata-name seg\tSet the name of the RODATA segment\n"
710 " --signed-chars\tDefault characters are signed\n"
711 " --standard std\tLanguage standard (c89, c99, cc65)\n"
712 " --start-addr addr\tSet the default start address\n"
713 " --static-locals\tMake local variables static\n"
714 " --target sys\t\tSet the target system\n"
715 " --version\t\tPrint the version number\n"
716 " --verbose\t\tVerbose mode\n"
717 " --zeropage-label name\tDefine and export a ZEROPAGE segment label\n"
718 " --zeropage-name seg\tSet the name of the ZEROPAGE segment\n",
724 static void OptAddSource (const char* Opt attribute ((unused)),
725 const char* Arg attribute ((unused)))
726 /* Strict source code as comments to the generated asm code */
728 CmdAddArg (&CC65, "-T");
733 static void OptAsmArgs (const char* Opt attribute ((unused)), const char* Arg)
734 /* Pass arguments to the assembler */
736 CmdAddArgList (&CA65, Arg);
741 static void OptAsmDefine (const char* Opt attribute ((unused)), const char* Arg)
742 /* Define an assembler symbol (assembler) */
744 CmdAddArg2 (&CA65, "-D", Arg);
749 static void OptAsmIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
750 /* Include directory (assembler) */
752 CmdAddArg2 (&CA65, "-I", Arg);
757 static void OptBssLabel (const char* Opt attribute ((unused)), const char* Arg)
758 /* Handle the --bss-label option */
760 CmdAddArg2 (&CO65, "--bss-label", Arg);
765 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
766 /* Handle the --bss-name option */
768 CmdAddArg2 (&CC65, "--bss-name", Arg);
769 CmdAddArg2 (&CO65, "--bss-name", Arg);
774 static void OptCfgPath (const char* Opt attribute ((unused)), const char* Arg)
775 /* Config file search path (linker) */
777 CmdAddArg2 (&LD65, "--cfg-path", Arg);
782 static void OptCheckStack (const char* Opt attribute ((unused)),
783 const char* Arg attribute ((unused)))
784 /* Handle the --check-stack option */
786 CmdAddArg (&CC65, "--check-stack");
791 static void OptCodeLabel (const char* Opt attribute ((unused)), const char* Arg)
792 /* Handle the --code-label option */
794 CmdAddArg2 (&CO65, "--code-label", Arg);
799 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
800 /* Handle the --code-name option */
802 CmdAddArg2 (&CC65, "--code-name", Arg);
803 CmdAddArg2 (&CO65, "--code-name", Arg);
808 static void OptCodeSize (const char* Opt attribute ((unused)), const char* Arg)
809 /* Handle the --codesize option */
811 CmdAddArg2 (&CC65, "--codesize", Arg);
816 static void OptConfig (const char* Opt attribute ((unused)), const char* Arg)
817 /* Config file (linker) */
820 Error ("Cannot specify -C/--config twice");
827 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
828 /* Handle the --cpu option */
830 /* Add the cpu type to the assembler and compiler */
831 CmdAddArg2 (&CA65, "--cpu", Arg);
832 CmdAddArg2 (&CC65, "--cpu", Arg);
837 static void OptCreateDep (const char* Opt attribute ((unused)),
838 const char* Arg attribute ((unused)))
839 /* Handle the --create-dep option */
841 CmdAddArg (&CC65, "--create-dep");
846 static void OptDataLabel (const char* Opt attribute ((unused)), const char* Arg)
847 /* Handle the --data-label option */
849 CmdAddArg2 (&CO65, "--data-label", Arg);
854 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
855 /* Handle the --data-name option */
857 CmdAddArg2 (&CC65, "--data-name", Arg);
858 CmdAddArg2 (&CO65, "--data-name", Arg);
863 static void OptDebug (const char* Opt attribute ((unused)),
864 const char* Arg attribute ((unused)))
865 /* Debug mode (compiler and cl65 utility) */
867 CmdAddArg (&CC65, "-d");
868 CmdAddArg (&CO65, "-d");
874 static void OptDebugInfo (const char* Opt attribute ((unused)),
875 const char* Arg attribute ((unused)))
876 /* Debug Info - add to compiler and assembler */
878 CmdAddArg (&CC65, "-g");
879 CmdAddArg (&CA65, "-g");
880 CmdAddArg (&CO65, "-g");
885 static void OptFeature (const char* Opt attribute ((unused)), const char* Arg)
886 /* Emulation features for the assembler */
888 CmdAddArg2 (&CA65, "--feature", Arg);
893 static void OptForgetIncPaths (const char* Opt attribute ((unused)),
894 const char* Arg attribute ((unused)))
895 /* Forget all currently defined include paths */
897 CmdAddArg (&CC65, "--forget-inc-paths");
902 static void OptHelp (const char* Opt attribute ((unused)),
903 const char* Arg attribute ((unused)))
904 /* Print help - cl65 */
912 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
913 /* Include directory (compiler) */
915 CmdAddArg2 (&CC65, "-I", Arg);
920 static void OptLdArgs (const char* Opt attribute ((unused)), const char* Arg)
921 /* Pass arguments to the linker */
923 CmdAddArgList (&LD65, Arg);
928 static void OptLib (const char* Opt attribute ((unused)), const char* Arg)
929 /* Library file follows (linker) */
931 CmdAddArg2 (&LD65, "--lib", Arg);
936 static void OptLibPath (const char* Opt attribute ((unused)), const char* Arg)
937 /* Library search path (linker) */
939 CmdAddArg2 (&LD65, "--lib-path", Arg);
944 static void OptListBytes (const char* Opt attribute ((unused)), const char* Arg)
945 /* Set the maximum number of bytes per asm listing line */
947 CmdAddArg2 (&CA65, "--list-bytes", Arg);
952 static void OptListing (const char* Opt attribute ((unused)),
953 const char* Arg attribute ((unused)))
954 /* Create an assembler listing */
956 CmdAddArg (&CA65, "-l");
961 static void OptListTargets (const char* Opt attribute ((unused)),
962 const char* Arg attribute ((unused)))
963 /* List all targets */
967 /* List the targets */
968 for (I = TGT_NONE; I < TGT_COUNT; ++I) {
969 printf ("%s\n", TargetNames[I]);
978 static void OptMapFile (const char* Opt attribute ((unused)), const char* Arg)
979 /* Create a map file */
981 /* Create a map file (linker) */
982 CmdAddArg2 (&LD65, "-m", Arg);
987 static void OptMemoryModel (const char* Opt attribute ((unused)), const char* Arg)
988 /* Set the memory model */
990 mmodel_t MemoryModel = FindMemoryModel (Arg);
991 if (MemoryModel == MMODEL_UNKNOWN) {
992 Error ("Unknown memory model: %s", Arg);
993 } else if (MemoryModel == MMODEL_HUGE) {
994 Error ("Unsupported memory model: %s", Arg);
996 CmdAddArg2 (&CA65, "-mm", Arg);
997 CmdAddArg2 (&CC65, "-mm", Arg);
1003 static void OptModule (const char* Opt attribute ((unused)),
1004 const char* Arg attribute ((unused)))
1005 /* Link as a module */
1012 static void OptModuleId (const char* Opt attribute ((unused)), const char* Arg)
1013 /* Specify a module if for the linker */
1015 /* Pass it straight to the linker */
1016 CmdAddArg2 (&LD65, "--module-id", Arg);
1021 static void OptO65Model (const char* Opt attribute ((unused)), const char* Arg)
1022 /* Handle the --o65-model option */
1024 CmdAddArg2 (&CO65, "-m", Arg);
1029 static void OptObj (const char* Opt attribute ((unused)), const char* Arg)
1030 /* Object file follows (linker) */
1032 CmdAddArg2 (&LD65, "--obj", Arg);
1037 static void OptObjPath (const char* Opt attribute ((unused)), const char* Arg)
1038 /* Object file search path (linker) */
1040 CmdAddArg2 (&LD65, "--obj-path", Arg);
1045 static void OptRegisterSpace (const char* Opt attribute ((unused)), const char* Arg)
1046 /* Handle the --register-space option */
1048 CmdAddArg2 (&CC65, "--register-space", Arg);
1053 static void OptRegisterVars (const char* Opt attribute ((unused)),
1054 const char* Arg attribute ((unused)))
1055 /* Handle the --register-vars option */
1057 CmdAddArg (&CC65, "-r");
1062 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
1063 /* Handle the --rodata-name option */
1065 CmdAddArg2 (&CC65, "--rodata-name", Arg);
1070 static void OptSignedChars (const char* Opt attribute ((unused)),
1071 const char* Arg attribute ((unused)))
1072 /* Make default characters signed */
1074 CmdAddArg (&CC65, "-j");
1079 static void OptStandard (const char* Opt attribute ((unused)), const char* Arg)
1080 /* Set the language standard */
1082 CmdAddArg2 (&CC65, "--standard", Arg);
1087 static void OptStartAddr (const char* Opt attribute ((unused)), const char* Arg)
1088 /* Set the default start address */
1090 CmdAddArg2 (&LD65, "-S", Arg);
1095 static void OptStaticLocals (const char* Opt attribute ((unused)),
1096 const char* Arg attribute ((unused)))
1097 /* Place local variables in static storage */
1099 CmdAddArg (&CC65, "-Cl");
1104 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
1105 /* Set the target system */
1107 Target = FindTarget (Arg);
1108 if (Target == TGT_UNKNOWN) {
1109 Error ("No such target system: `%s'", Arg);
1110 } else if (Target == TGT_MODULE) {
1111 Error ("Cannot use `module' as target, use --module instead");
1117 static void OptVerbose (const char* Opt attribute ((unused)),
1118 const char* Arg attribute ((unused)))
1119 /* Verbose mode (compiler, assembler, linker) */
1121 CmdAddArg (&CC65, "-v");
1122 CmdAddArg (&CA65, "-v");
1123 CmdAddArg (&CO65, "-v");
1124 CmdAddArg (&LD65, "-v");
1129 static void OptVersion (const char* Opt attribute ((unused)),
1130 const char* Arg attribute ((unused)))
1131 /* Print version number */
1134 "cl65 V%u.%u.%u - (C) Copyright 1998-2005 Ullrich von Bassewitz\n",
1135 VER_MAJOR, VER_MINOR, VER_PATCH);
1140 static void OptZeropageLabel (const char* Opt attribute ((unused)), const char* Arg)
1141 /* Handle the --zeropage-label option */
1143 CmdAddArg2 (&CO65, "--zeropage-label", Arg);
1148 static void OptZeropageName (const char* Opt attribute ((unused)), const char* Arg)
1149 /* Handle the --zeropage-name option */
1151 CmdAddArg2 (&CO65, "--zeropage-name", Arg);
1156 int main (int argc, char* argv [])
1157 /* Utility main program */
1159 /* Program long options */
1160 static const LongOpt OptTab[] = {
1161 { "--add-source", 0, OptAddSource },
1162 { "--asm-args", 1, OptAsmArgs },
1163 { "--asm-define", 1, OptAsmDefine },
1164 { "--asm-include-dir", 1, OptAsmIncludeDir },
1165 { "--bss-label", 1, OptBssLabel },
1166 { "--bss-name", 1, OptBssName },
1167 { "--cfg-path", 1, OptCfgPath },
1168 { "--check-stack", 0, OptCheckStack },
1169 { "--code-label", 1, OptCodeLabel },
1170 { "--code-name", 1, OptCodeName },
1171 { "--codesize", 1, OptCodeSize },
1172 { "--config", 1, OptConfig },
1173 { "--cpu", 1, OptCPU },
1174 { "--create-dep", 0, OptCreateDep },
1175 { "--data-label", 1, OptDataLabel },
1176 { "--data-name", 1, OptDataName },
1177 { "--debug", 0, OptDebug },
1178 { "--debug-info", 0, OptDebugInfo },
1179 { "--feature", 1, OptFeature },
1180 { "--forget-inc-paths", 0, OptForgetIncPaths },
1181 { "--help", 0, OptHelp },
1182 { "--include-dir", 1, OptIncludeDir },
1183 { "--ld-args", 1, OptLdArgs },
1184 { "--lib", 1, OptLib },
1185 { "--lib-path", 1, OptLibPath },
1186 { "--list-targets", 0, OptListTargets },
1187 { "--listing", 0, OptListing },
1188 { "--list-bytes", 1, OptListBytes },
1189 { "--mapfile", 1, OptMapFile },
1190 { "--memory-model", 1, OptMemoryModel },
1191 { "--module", 0, OptModule },
1192 { "--module-id", 1, OptModuleId },
1193 { "--o65-model", 1, OptO65Model },
1194 { "--obj", 1, OptObj },
1195 { "--obj-path", 1, OptObjPath },
1196 { "--register-space", 1, OptRegisterSpace },
1197 { "--register-vars", 0, OptRegisterVars },
1198 { "--rodata-name", 1, OptRodataName },
1199 { "--signed-chars", 0, OptSignedChars },
1200 { "--standard", 1, OptStandard },
1201 { "--start-addr", 1, OptStartAddr },
1202 { "--static-locals", 0, OptStaticLocals },
1203 { "--target", 1, OptTarget },
1204 { "--verbose", 0, OptVerbose },
1205 { "--version", 0, OptVersion },
1206 { "--zeropage-label", 1, OptZeropageLabel },
1207 { "--zeropage-name", 1, OptZeropageName },
1212 /* Initialize the cmdline module */
1213 InitCmdLine (&argc, &argv, "cl65");
1215 /* Initialize the command descriptors */
1216 CmdInit (&CC65, "cc65");
1217 CmdInit (&CA65, "ca65");
1218 CmdInit (&CO65, "co65");
1219 CmdInit (&LD65, "ld65");
1220 CmdInit (&GRC, "grc");
1222 /* Our default target is the C64 instead of "none" */
1225 /* Check the parameters */
1227 while (I < ArgCount) {
1229 /* Get the argument */
1230 const char* Arg = ArgVec[I];
1232 /* Check for an option */
1233 if (Arg [0] == '-') {
1238 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
1242 if (Arg[2] == 'l' && Arg[3] == '\0') {
1243 /* Make local variables static */
1244 OptStaticLocals (Arg, 0);
1246 /* Specify linker config file */
1247 OptConfig (Arg, GetArg (&I, 2));
1252 /* Define a preprocessor symbol (compiler) */
1253 CmdAddArg2 (&CC65, "-D", GetArg (&I, 2));
1257 /* Include directory (compiler) */
1258 OptIncludeDir (Arg, GetArg (&I, 2));
1262 if (Arg[2] == 'n' && Arg[3] == '\0') {
1263 /* VICE label file (linker) */
1264 CmdAddArg2 (&LD65, "-Ln", GetArg (&I, 3));
1266 /* Library search path (linker) */
1267 OptLibPath (Arg, GetArg (&I, 2));
1272 /* Optimize code (compiler, also covers -Oi and others) */
1273 CmdAddArg (&CC65, Arg);
1277 /* Dont assemble and link the created files */
1278 DontLink = DontAssemble = 1;
1282 /* Include source as comment (compiler) */
1283 OptAddSource (Arg, 0);
1287 /* Print version number */
1288 OptVersion (Arg, 0);
1295 OptAsmArgs (Arg, GetArg (&I, 3));
1299 OptLdArgs (Arg, GetArg (&I, 3));
1303 /* Suppress warnings - compiler and assembler */
1304 CmdAddArg (&CC65, "-W");
1305 CmdAddArg2 (&CA65, "-W", "0");
1309 UnknownOption (Arg);
1315 /* Don't link the resulting files */
1320 /* Debug mode (compiler) */
1325 /* Debugging - add to compiler and assembler */
1326 OptDebugInfo (Arg, 0);
1331 /* Print help - cl65 */
1336 /* Default characters are signed */
1337 OptSignedChars (Arg, 0);
1341 /* Create an assembler listing */
1342 OptListing (Arg, 0);
1346 /* Create a map file (linker) */
1347 OptMapFile (Arg, GetArg (&I, 2));
1351 /* Name the output file */
1352 OutputName = GetArg (&I, 2);
1356 /* Enable register variables */
1357 OptRegisterVars (Arg, 0);
1361 /* Set target system - compiler, assembler and linker */
1362 OptTarget (Arg, GetArg (&I, 2));
1366 if (Arg [2] == 'm') {
1367 /* Verbose map file (linker) */
1368 CmdAddArg (&LD65, "-vm");
1370 /* Verbose mode (compiler, assembler, linker) */
1371 OptVerbose (Arg, 0);
1376 UnknownOption (Arg);
1380 /* Remember the first file name */
1381 if (FirstInput == 0) {
1385 /* Determine the file type by the extension */
1386 switch (GetFileType (Arg)) {
1389 /* Compile the file */
1394 /* Assemble the file */
1395 if (!DontAssemble) {
1402 /* Add to the linker files */
1403 CmdAddFile (&LD65, Arg);
1407 /* Add to the resource compiler files */
1412 /* Add the the object file converter files */
1417 Error ("Don't know what to do with `%s'", Arg);
1427 /* Check if we had any input files */
1428 if (FirstInput == 0) {
1429 Warning ("No input files");
1432 /* Link the given files if requested and if we have any */
1433 if (DontLink == 0 && LD65.FileCount > 0) {
1437 /* Return an apropriate exit code */
1438 return EXIT_SUCCESS;