1 /*****************************************************************************/
5 /* Main module for the cl65 compile and link utility */
9 /* (C) 1999-2010, 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 /* The names of the files for dependency generation */
116 static const char* DepName = 0;
117 static const char* FullDepName = 0;
119 /* Remember if we should link a module */
120 static int Module = 0;
122 /* Extension used for a module */
123 #define MODULE_EXT ".o65"
125 /* Name of the target specific runtime library */
126 static char* TargetLib = 0;
130 /*****************************************************************************/
131 /* Include the system specific spawn function */
132 /*****************************************************************************/
136 #if defined(NEED_SPAWN)
137 # if defined(SPAWN_UNIX)
138 # include "spawn-unix.inc"
139 # elif defined(SPAWN_AMIGA)
140 # include "spawn-amiga.inc"
142 # error "Don't know which spawn module to include!"
148 /*****************************************************************************/
149 /* Command structure handling */
150 /*****************************************************************************/
154 static void CmdExpand (CmdDesc* Cmd)
155 /* Expand the argument vector */
157 unsigned NewMax = Cmd->ArgMax + 10;
158 char** NewArgs = xmalloc (NewMax * sizeof (char*));
159 memcpy (NewArgs, Cmd->Args, Cmd->ArgMax * sizeof (char*));
162 Cmd->ArgMax = NewMax;
167 static void CmdAddArg (CmdDesc* Cmd, const char* Arg)
168 /* Add a new argument to the command */
170 /* Expand the argument vector if needed */
171 if (Cmd->ArgCount >= Cmd->ArgMax) {
175 /* Add a copy of the new argument, allow a NULL pointer */
177 Cmd->Args[Cmd->ArgCount++] = xstrdup (Arg);
179 Cmd->Args[Cmd->ArgCount++] = 0;
185 static void CmdAddArg2 (CmdDesc* Cmd, const char* Arg1, const char* Arg2)
186 /* Add a new argument pair to the command */
188 CmdAddArg (Cmd, Arg1);
189 CmdAddArg (Cmd, Arg2);
194 static void CmdAddArgList (CmdDesc* Cmd, const char* ArgList)
195 /* Add a list of arguments separated by commas */
197 const char* Arg = ArgList;
201 if (*P == '\0' || *P == ',') {
203 /* End of argument, add it */
204 unsigned Len = P - Arg;
206 /* Expand the argument vector if needed */
207 if (Cmd->ArgCount >= Cmd->ArgMax) {
211 /* Add the new argument */
212 Cmd->Args[Cmd->ArgCount] = memcpy (xmalloc (Len + 1), Arg, Len);
213 Cmd->Args[Cmd->ArgCount][Len] = '\0';
216 /* If the argument was terminated by a comma, skip it, otherwise
220 /* Start over at next char */
226 /* Skip other chars */
235 static void CmdDelArgs (CmdDesc* Cmd, unsigned LastValid)
236 /* Remove all arguments with an index greater than LastValid */
238 while (Cmd->ArgCount > LastValid) {
240 xfree (Cmd->Args [Cmd->ArgCount]);
241 Cmd->Args [Cmd->ArgCount] = 0;
247 static void CmdAddFile (CmdDesc* Cmd, const char* File)
248 /* Add a new file to the command */
250 /* Expand the file vector if needed */
251 if (Cmd->FileCount == Cmd->FileMax) {
252 unsigned NewMax = Cmd->FileMax + 10;
253 char** NewFiles = xmalloc (NewMax * sizeof (char*));
254 memcpy (NewFiles, Cmd->Files, Cmd->FileMax * sizeof (char*));
256 Cmd->Files = NewFiles;
257 Cmd->FileMax = NewMax;
260 /* If the file name is not NULL (which is legal and is used to terminate
261 * the file list), check if the file name does already exist in the file
262 * list and print a warning if so. Regardless of the search result, add
267 for (I = 0; I < Cmd->FileCount; ++I) {
268 if (strcmp (Cmd->Files[I], File) == 0) {
270 Warning ("Duplicate file in argument list: `%s'", File);
271 /* No need to search further */
277 Cmd->Files [Cmd->FileCount++] = xstrdup (File);
279 /* Add a NULL pointer */
280 Cmd->Files [Cmd->FileCount++] = 0;
286 static void CmdInit (CmdDesc* Cmd, const char* Path)
287 /* Initialize the command using the given path to the executable */
289 /* Remember the command */
290 Cmd->Name = xstrdup (Path);
292 /* Use the command name as first argument */
293 CmdAddArg (Cmd, Path);
298 static void CmdSetOutput (CmdDesc* Cmd, const char* File)
299 /* Set the output file in a command desc */
301 CmdAddArg2 (Cmd, "-o", File);
306 static void CmdSetTarget (CmdDesc* Cmd, target_t Target)
307 /* Set the output file in a command desc */
309 CmdAddArg2 (Cmd, "-t", TargetNames[Target]);
314 static void CmdPrint (CmdDesc* Cmd, FILE* F)
315 /* Output the command line encoded in the command desc */
318 for (I = 0; I < Cmd->ArgCount && Cmd->Args[I] != 0; ++I) {
319 fprintf (F, "%s ", Cmd->Args[I]);
325 /*****************************************************************************/
326 /* Target handling */
327 /*****************************************************************************/
331 static void SetTargetFiles (void)
332 /* Set the target system files */
334 /* Determine the names of the target specific library file */
335 if (Target != TGT_NONE) {
337 /* Get a pointer to the system name and its length */
338 const char* TargetName = TargetNames [Target];
339 unsigned TargetNameLen = strlen (TargetName);
341 /* Set the library file */
342 TargetLib = xmalloc (TargetNameLen + 4 + 1);
343 memcpy (TargetLib, TargetName, TargetNameLen);
344 strcpy (TargetLib + TargetNameLen, ".lib");
351 /*****************************************************************************/
353 /*****************************************************************************/
357 static void ExecProgram (CmdDesc* Cmd)
358 /* Execute a subprocess with the given name/parameters. Exit on errors. */
362 /* If in debug mode, output the command line we will execute */
364 printf ("Executing: ");
365 CmdPrint (Cmd, stdout);
369 /* Call the program */
370 Status = spawnvp (P_WAIT, Cmd->Name, Cmd->Args);
372 /* Check the result code */
374 /* Error executing the program */
375 Error ("Cannot execute `%s': %s", Cmd->Name, strerror (errno));
376 } else if (Status != 0) {
377 /* Called program had an error */
384 static void Link (void)
385 /* Link the resulting executable */
389 /* If we have a linker config file given, add it to the command line.
390 * Otherwise pass the target to the linker if we have one.
394 Error ("Cannot use -C and --module together");
396 CmdAddArg2 (&LD65, "-C", LinkerConfig);
398 CmdSetTarget (&LD65, TGT_MODULE);
400 CmdSetTarget (&LD65, Target);
403 /* Determine which target libraries are needed */
406 /* Since linking is always the final step, if we have an output file name
407 * given, set it here. If we don't have an explicit output name given,
408 * try to build one from the name of the first input file.
412 CmdSetOutput (&LD65, OutputName);
414 } else if (FirstInput && FindExt (FirstInput)) { /* Only if ext present! */
416 const char* Extension = Module? MODULE_EXT : "";
417 char* Output = MakeFilename (FirstInput, Extension);
418 CmdSetOutput (&LD65, Output);
423 /* Add all object files as parameters */
424 for (I = 0; I < LD65.FileCount; ++I) {
425 CmdAddArg (&LD65, LD65.Files [I]);
428 /* Add the system runtime library */
430 CmdAddArg (&LD65, TargetLib);
433 /* Terminate the argument list with a NULL pointer */
434 CmdAddArg (&LD65, 0);
436 /* Call the linker */
442 static void AssembleFile (const char* File, unsigned ArgCount)
443 /* Common routine to assemble a file. Will be called by Assemble() and
444 * AssembleIntermediate(). Adds options common for both routines and
445 * assembles the file. Will remove excess arguments after assembly.
448 /* Set the target system */
449 CmdSetTarget (&CA65, Target);
451 /* If we won't link, this is the final step. In this case, set the
454 if (DontLink && OutputName) {
455 CmdSetOutput (&CA65, OutputName);
457 /* The object file name will be the name of the source file
458 * with .s replaced by ".o". Add this file to the list of
461 char* ObjName = MakeFilename (File, ".o");
462 CmdAddFile (&LD65, ObjName);
466 /* Add the file as argument for the assembler */
467 CmdAddArg (&CA65, File);
469 /* Add a NULL pointer to terminate the argument list */
470 CmdAddArg (&CA65, 0);
472 /* Run the assembler */
475 /* Remove the excess arguments */
476 CmdDelArgs (&CA65, ArgCount);
481 static void AssembleIntermediate (const char* File)
482 /* Assemble an intermediate file. The -dep options won't be added and
483 * the file is removed after assembly.
486 /* Use common routine */
487 AssembleFile (File, CA65.ArgCount);
489 /* Remove the generated file */
490 if (remove (File) < 0) {
491 Warning ("Cannot remove temporary file `%s': %s",
492 File, strerror (errno));
498 static void Assemble (const char* File)
499 /* Assemble the given file */
501 /* Remember the current assembler argument count */
502 unsigned ArgCount = CA65.ArgCount;
504 /* We aren't assembling an intermediate file, but one requested by the
505 * user. So add a few options here if they were given on the command
508 if (DepName && *DepName) {
509 CmdAddArg2 (&CA65, "--create-dep", DepName);
511 if (FullDepName && *FullDepName) {
512 CmdAddArg2 (&CA65, "--create-full-dep", FullDepName);
515 /* Use the common routine */
516 AssembleFile (File, ArgCount);
521 static void Compile (const char* File)
522 /* Compile the given file */
526 /* Remember the current compiler argument count */
527 unsigned ArgCount = CC65.ArgCount;
529 /* Set the target system */
530 CmdSetTarget (&CC65, Target);
532 /* If we won't assemble, this is the final step. In this case, set the
535 if (DontAssemble && OutputName) {
536 CmdSetOutput (&CC65, OutputName);
538 /* The assembler file name will be the name of the source file
539 * with .c replaced by ".s".
541 AsmName = MakeFilename (File, ".s");
544 /* Add the file as argument for the compiler */
545 CmdAddArg (&CC65, File);
547 /* Add a NULL pointer to terminate the argument list */
548 CmdAddArg (&CC65, 0);
550 /* Run the compiler */
553 /* Remove the excess arguments */
554 CmdDelArgs (&CC65, ArgCount);
556 /* If this is not the final step, assemble the generated file, then
560 AssembleIntermediate (AsmName);
563 /* Free the assembler file name which was allocated from the heap */
569 static void CompileRes (const char* File)
570 /* Compile the given geos resource file */
574 /* Remember the current assembler argument count */
575 unsigned ArgCount = GRC.ArgCount;
577 /* The assembler file name will be the name of the source file
578 * with .grc replaced by ".s".
580 AsmName = MakeFilename (File, ".s");
582 /* Add the file as argument for the resource compiler */
583 CmdAddArg (&GRC, File);
585 /* Add a NULL pointer to terminate the argument list */
588 /* Run the compiler */
591 /* Remove the excess arguments */
592 CmdDelArgs (&GRC, ArgCount);
594 /* If this is not the final step, assemble the generated file, then
598 AssembleIntermediate (AsmName);
601 /* Free the assembler file name which was allocated from the heap */
607 static void ConvertO65 (const char* File)
608 /* Convert an o65 object file into an assembler file */
612 /* Remember the current converter argument count */
613 unsigned ArgCount = CO65.ArgCount;
615 /* If we won't assemble, this is the final step. In this case, set the
618 if (DontAssemble && OutputName) {
619 CmdSetOutput (&CO65, OutputName);
621 /* The assembler file name will be the name of the source file
622 * with .c replaced by ".s".
624 AsmName = MakeFilename (File, ".s");
627 /* Add the file as argument for the object file converter */
628 CmdAddArg (&CO65, File);
630 /* Add a NULL pointer to terminate the argument list */
631 CmdAddArg (&CO65, 0);
633 /* Run the converter */
636 /* Remove the excess arguments */
637 CmdDelArgs (&CO65, ArgCount);
639 /* If this is not the final step, assemble the generated file, then
643 AssembleIntermediate (AsmName);
646 /* Free the assembler file name which was allocated from the heap */
652 /*****************************************************************************/
654 /*****************************************************************************/
658 static void Usage (void)
659 /* Print usage information and exit */
661 printf ("Usage: %s [options] file [...]\n"
663 " -c\t\t\t\tCompile and assemble but don't link\n"
664 " -d\t\t\t\tDebug mode\n"
665 " -g\t\t\t\tAdd debug info\n"
666 " -h\t\t\t\tHelp (this text)\n"
667 " -l\t\t\t\tCreate an assembler listing\n"
668 " -m name\t\t\tCreate a map file\n"
669 " -mm model\t\t\tSet the memory model\n"
670 " -o name\t\t\tName the output file\n"
671 " -r\t\t\t\tEnable register variables\n"
672 " -t sys\t\t\tSet the target system\n"
673 " -u sym\t\t\tForce an import of symbol `sym'\n"
674 " -v\t\t\t\tVerbose mode\n"
675 " -vm\t\t\t\tVerbose map file\n"
676 " -C name\t\t\tUse linker config file\n"
677 " -Cl\t\t\t\tMake local variables static\n"
678 " -D sym[=defn]\t\t\tDefine a preprocessor symbol\n"
679 " -I dir\t\t\tSet a compiler include directory path\n"
680 " -L path\t\t\tSpecify a library search path\n"
681 " -Ln name\t\t\tCreate a VICE label file\n"
682 " -O\t\t\t\tOptimize code\n"
683 " -Oi\t\t\t\tOptimize code, inline functions\n"
684 " -Or\t\t\t\tOptimize code, honour the register keyword\n"
685 " -Os\t\t\t\tOptimize code, inline known C funtions\n"
686 " -S\t\t\t\tCompile but don't assemble and link\n"
687 " -T\t\t\t\tInclude source as comment\n"
688 " -V\t\t\t\tPrint the version number\n"
689 " -W\t\t\t\tSuppress warnings\n"
690 " -Wa options\t\t\tPass options to the assembler\n"
691 " -Wl options\t\t\tPass options to the linker\n"
694 " --add-source\t\t\tInclude source as comment\n"
695 " --asm-args options\t\tPass options to the assembler\n"
696 " --asm-define sym[=v]\t\tDefine an assembler symbol\n"
697 " --asm-include-dir dir\t\tSet an assembler include directory\n"
698 " --bss-label name\t\tDefine and export a BSS segment label\n"
699 " --bss-name seg\t\tSet the name of the BSS segment\n"
700 " --cfg-path path\t\tSpecify a config file search path\n"
701 " --check-stack\t\t\tGenerate stack overflow checks\n"
702 " --code-label name\t\tDefine and export a CODE segment label\n"
703 " --code-name seg\t\tSet the name of the CODE segment\n"
704 " --codesize x\t\t\tAccept larger code by factor x\n"
705 " --config name\t\t\tUse linker config file\n"
706 " --cpu type\t\t\tSet cpu type\n"
707 " --create-dep name\t\tCreate a make dependency file\n"
708 " --create-full-dep name\tCreate a full make dependency file\n"
709 " --data-label name\t\tDefine and export a DATA segment label\n"
710 " --data-name seg\t\tSet the name of the DATA segment\n"
711 " --debug\t\t\tDebug mode\n"
712 " --debug-info\t\t\tAdd debug info\n"
713 " --feature name\t\tSet an emulation feature\n"
714 " --force-import sym\t\tForce an import of symbol `sym'\n"
715 " --forget-inc-paths\t\tForget include search paths (compiler)\n"
716 " --help\t\t\tHelp (this text)\n"
717 " --include-dir dir\t\tSet a compiler include directory path\n"
718 " --ld-args options\t\tPass options to the linker\n"
719 " --lib file\t\t\tLink this library\n"
720 " --lib-path path\t\tSpecify a library search path\n"
721 " --list-targets\t\tList all available targets\n"
722 " --listing\t\t\tCreate an assembler listing\n"
723 " --list-bytes n\t\tNumber of bytes per assembler listing line\n"
724 " --mapfile name\t\tCreate a map file\n"
725 " --memory-model model\t\tSet the memory model\n"
726 " --module\t\t\tLink as a module\n"
727 " --module-id id\t\tSpecify a module id for the linker\n"
728 " --o65-model model\t\tOverride the o65 model\n"
729 " --obj file\t\t\tLink this object file\n"
730 " --obj-path path\t\tSpecify an object file search path\n"
731 " --register-space b\t\tSet space available for register variables\n"
732 " --register-vars\t\tEnable register variables\n"
733 " --rodata-name seg\t\tSet the name of the RODATA segment\n"
734 " --signed-chars\t\tDefault characters are signed\n"
735 " --standard std\t\tLanguage standard (c89, c99, cc65)\n"
736 " --start-addr addr\t\tSet the default start address\n"
737 " --static-locals\t\tMake local variables static\n"
738 " --target sys\t\t\tSet the target system\n"
739 " --version\t\t\tPrint the version number\n"
740 " --verbose\t\t\tVerbose mode\n"
741 " --zeropage-label name\t\tDefine and export a ZEROPAGE segment label\n"
742 " --zeropage-name seg\t\tSet the name of the ZEROPAGE segment\n",
748 static void OptAddSource (const char* Opt attribute ((unused)),
749 const char* Arg attribute ((unused)))
750 /* Strict source code as comments to the generated asm code */
752 CmdAddArg (&CC65, "-T");
757 static void OptAsmArgs (const char* Opt attribute ((unused)), const char* Arg)
758 /* Pass arguments to the assembler */
760 CmdAddArgList (&CA65, Arg);
765 static void OptAsmDefine (const char* Opt attribute ((unused)), const char* Arg)
766 /* Define an assembler symbol (assembler) */
768 CmdAddArg2 (&CA65, "-D", Arg);
773 static void OptAsmIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
774 /* Include directory (assembler) */
776 CmdAddArg2 (&CA65, "-I", Arg);
781 static void OptBssLabel (const char* Opt attribute ((unused)), const char* Arg)
782 /* Handle the --bss-label option */
784 CmdAddArg2 (&CO65, "--bss-label", Arg);
789 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
790 /* Handle the --bss-name option */
792 CmdAddArg2 (&CC65, "--bss-name", Arg);
793 CmdAddArg2 (&CO65, "--bss-name", Arg);
798 static void OptCfgPath (const char* Opt attribute ((unused)), const char* Arg)
799 /* Config file search path (linker) */
801 CmdAddArg2 (&LD65, "--cfg-path", Arg);
806 static void OptCheckStack (const char* Opt attribute ((unused)),
807 const char* Arg attribute ((unused)))
808 /* Handle the --check-stack option */
810 CmdAddArg (&CC65, "--check-stack");
815 static void OptCodeLabel (const char* Opt attribute ((unused)), const char* Arg)
816 /* Handle the --code-label option */
818 CmdAddArg2 (&CO65, "--code-label", Arg);
823 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
824 /* Handle the --code-name option */
826 CmdAddArg2 (&CC65, "--code-name", Arg);
827 CmdAddArg2 (&CO65, "--code-name", Arg);
832 static void OptCodeSize (const char* Opt attribute ((unused)), const char* Arg)
833 /* Handle the --codesize option */
835 CmdAddArg2 (&CC65, "--codesize", Arg);
840 static void OptConfig (const char* Opt attribute ((unused)), const char* Arg)
841 /* Config file (linker) */
844 Error ("Cannot specify -C/--config twice");
851 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
852 /* Handle the --cpu option */
854 /* Add the cpu type to the assembler and compiler */
855 CmdAddArg2 (&CA65, "--cpu", Arg);
856 CmdAddArg2 (&CC65, "--cpu", Arg);
861 static void OptCreateDep (const char* Opt attribute ((unused)), const char* Arg)
862 /* Handle the --create-dep option */
864 /* Add the file name to the compiler */
865 CmdAddArg2 (&CC65, "--create-dep", Arg);
867 /* Remember the file name for the assembler */
873 static void OptCreateFullDep (const char* Opt attribute ((unused)), const char* Arg)
874 /* Handle the --create-full-dep option */
876 /* Add the file name to the compiler */
877 CmdAddArg2 (&CC65, "--create-full-dep", Arg);
879 /* Remember the file name for the assembler */
885 static void OptDataLabel (const char* Opt attribute ((unused)), const char* Arg)
886 /* Handle the --data-label option */
888 CmdAddArg2 (&CO65, "--data-label", Arg);
893 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
894 /* Handle the --data-name option */
896 CmdAddArg2 (&CC65, "--data-name", Arg);
897 CmdAddArg2 (&CO65, "--data-name", Arg);
902 static void OptDebug (const char* Opt attribute ((unused)),
903 const char* Arg attribute ((unused)))
904 /* Debug mode (compiler and cl65 utility) */
906 CmdAddArg (&CC65, "-d");
907 CmdAddArg (&CO65, "-d");
913 static void OptDebugInfo (const char* Opt attribute ((unused)),
914 const char* Arg attribute ((unused)))
915 /* Debug Info - add to compiler and assembler */
917 CmdAddArg (&CC65, "-g");
918 CmdAddArg (&CA65, "-g");
919 CmdAddArg (&CO65, "-g");
924 static void OptFeature (const char* Opt attribute ((unused)), const char* Arg)
925 /* Emulation features for the assembler */
927 CmdAddArg2 (&CA65, "--feature", Arg);
932 static void OptForceImport (const char* Opt attribute ((unused)), const char* Arg)
933 /* Emulation features for the assembler */
935 CmdAddArg2 (&LD65, "-u", Arg);
940 static void OptForgetIncPaths (const char* Opt attribute ((unused)),
941 const char* Arg attribute ((unused)))
942 /* Forget all currently defined include paths */
944 CmdAddArg (&CC65, "--forget-inc-paths");
949 static void OptHelp (const char* Opt attribute ((unused)),
950 const char* Arg attribute ((unused)))
951 /* Print help - cl65 */
959 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
960 /* Include directory (compiler) */
962 CmdAddArg2 (&CC65, "-I", Arg);
967 static void OptLdArgs (const char* Opt attribute ((unused)), const char* Arg)
968 /* Pass arguments to the linker */
970 CmdAddArgList (&LD65, Arg);
975 static void OptLib (const char* Opt attribute ((unused)), const char* Arg)
976 /* Library file follows (linker) */
978 CmdAddArg2 (&LD65, "--lib", Arg);
983 static void OptLibPath (const char* Opt attribute ((unused)), const char* Arg)
984 /* Library search path (linker) */
986 CmdAddArg2 (&LD65, "--lib-path", Arg);
991 static void OptListBytes (const char* Opt attribute ((unused)), const char* Arg)
992 /* Set the maximum number of bytes per asm listing line */
994 CmdAddArg2 (&CA65, "--list-bytes", Arg);
999 static void OptListing (const char* Opt attribute ((unused)),
1000 const char* Arg attribute ((unused)))
1001 /* Create an assembler listing */
1003 CmdAddArg (&CA65, "-l");
1008 static void OptListTargets (const char* Opt attribute ((unused)),
1009 const char* Arg attribute ((unused)))
1010 /* List all targets */
1014 /* List the targets */
1015 for (I = TGT_NONE; I < TGT_COUNT; ++I) {
1016 printf ("%s\n", TargetNames[I]);
1020 exit (EXIT_SUCCESS);
1025 static void OptMapFile (const char* Opt attribute ((unused)), const char* Arg)
1026 /* Create a map file */
1028 /* Create a map file (linker) */
1029 CmdAddArg2 (&LD65, "-m", Arg);
1034 static void OptMemoryModel (const char* Opt attribute ((unused)), const char* Arg)
1035 /* Set the memory model */
1037 mmodel_t MemoryModel = FindMemoryModel (Arg);
1038 if (MemoryModel == MMODEL_UNKNOWN) {
1039 Error ("Unknown memory model: %s", Arg);
1040 } else if (MemoryModel == MMODEL_HUGE) {
1041 Error ("Unsupported memory model: %s", Arg);
1043 CmdAddArg2 (&CA65, "-mm", Arg);
1044 CmdAddArg2 (&CC65, "-mm", Arg);
1050 static void OptModule (const char* Opt attribute ((unused)),
1051 const char* Arg attribute ((unused)))
1052 /* Link as a module */
1059 static void OptModuleId (const char* Opt attribute ((unused)), const char* Arg)
1060 /* Specify a module if for the linker */
1062 /* Pass it straight to the linker */
1063 CmdAddArg2 (&LD65, "--module-id", Arg);
1068 static void OptO65Model (const char* Opt attribute ((unused)), const char* Arg)
1069 /* Handle the --o65-model option */
1071 CmdAddArg2 (&CO65, "-m", Arg);
1076 static void OptObj (const char* Opt attribute ((unused)), const char* Arg)
1077 /* Object file follows (linker) */
1079 CmdAddArg2 (&LD65, "--obj", Arg);
1084 static void OptObjPath (const char* Opt attribute ((unused)), const char* Arg)
1085 /* Object file search path (linker) */
1087 CmdAddArg2 (&LD65, "--obj-path", Arg);
1092 static void OptRegisterSpace (const char* Opt attribute ((unused)), const char* Arg)
1093 /* Handle the --register-space option */
1095 CmdAddArg2 (&CC65, "--register-space", Arg);
1100 static void OptRegisterVars (const char* Opt attribute ((unused)),
1101 const char* Arg attribute ((unused)))
1102 /* Handle the --register-vars option */
1104 CmdAddArg (&CC65, "-r");
1109 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
1110 /* Handle the --rodata-name option */
1112 CmdAddArg2 (&CC65, "--rodata-name", Arg);
1117 static void OptSignedChars (const char* Opt attribute ((unused)),
1118 const char* Arg attribute ((unused)))
1119 /* Make default characters signed */
1121 CmdAddArg (&CC65, "-j");
1126 static void OptStandard (const char* Opt attribute ((unused)), const char* Arg)
1127 /* Set the language standard */
1129 CmdAddArg2 (&CC65, "--standard", Arg);
1134 static void OptStartAddr (const char* Opt attribute ((unused)), const char* Arg)
1135 /* Set the default start address */
1137 CmdAddArg2 (&LD65, "-S", Arg);
1142 static void OptStaticLocals (const char* Opt attribute ((unused)),
1143 const char* Arg attribute ((unused)))
1144 /* Place local variables in static storage */
1146 CmdAddArg (&CC65, "-Cl");
1151 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
1152 /* Set the target system */
1154 Target = FindTarget (Arg);
1155 if (Target == TGT_UNKNOWN) {
1156 Error ("No such target system: `%s'", Arg);
1157 } else if (Target == TGT_MODULE) {
1158 Error ("Cannot use `module' as target, use --module instead");
1164 static void OptVerbose (const char* Opt attribute ((unused)),
1165 const char* Arg attribute ((unused)))
1166 /* Verbose mode (compiler, assembler, linker) */
1168 CmdAddArg (&CC65, "-v");
1169 CmdAddArg (&CA65, "-v");
1170 CmdAddArg (&CO65, "-v");
1171 CmdAddArg (&LD65, "-v");
1176 static void OptVersion (const char* Opt attribute ((unused)),
1177 const char* Arg attribute ((unused)))
1178 /* Print version number */
1181 "cl65 V%s - (C) Copyright 1998-2009 Ullrich von Bassewitz\n",
1182 GetVersionAsString ());
1187 static void OptZeropageLabel (const char* Opt attribute ((unused)), const char* Arg)
1188 /* Handle the --zeropage-label option */
1190 CmdAddArg2 (&CO65, "--zeropage-label", Arg);
1195 static void OptZeropageName (const char* Opt attribute ((unused)), const char* Arg)
1196 /* Handle the --zeropage-name option */
1198 CmdAddArg2 (&CO65, "--zeropage-name", Arg);
1203 int main (int argc, char* argv [])
1204 /* Utility main program */
1206 /* Program long options */
1207 static const LongOpt OptTab[] = {
1208 { "--add-source", 0, OptAddSource },
1209 { "--asm-args", 1, OptAsmArgs },
1210 { "--asm-define", 1, OptAsmDefine },
1211 { "--asm-include-dir", 1, OptAsmIncludeDir },
1212 { "--bss-label", 1, OptBssLabel },
1213 { "--bss-name", 1, OptBssName },
1214 { "--cfg-path", 1, OptCfgPath },
1215 { "--check-stack", 0, OptCheckStack },
1216 { "--code-label", 1, OptCodeLabel },
1217 { "--code-name", 1, OptCodeName },
1218 { "--codesize", 1, OptCodeSize },
1219 { "--config", 1, OptConfig },
1220 { "--cpu", 1, OptCPU },
1221 { "--create-dep", 1, OptCreateDep },
1222 { "--create-full-dep", 1, OptCreateFullDep },
1223 { "--data-label", 1, OptDataLabel },
1224 { "--data-name", 1, OptDataName },
1225 { "--debug", 0, OptDebug },
1226 { "--debug-info", 0, OptDebugInfo },
1227 { "--feature", 1, OptFeature },
1228 { "--force-import", 1, OptForceImport },
1229 { "--forget-inc-paths", 0, OptForgetIncPaths },
1230 { "--help", 0, OptHelp },
1231 { "--include-dir", 1, OptIncludeDir },
1232 { "--ld-args", 1, OptLdArgs },
1233 { "--lib", 1, OptLib },
1234 { "--lib-path", 1, OptLibPath },
1235 { "--list-targets", 0, OptListTargets },
1236 { "--listing", 0, OptListing },
1237 { "--list-bytes", 1, OptListBytes },
1238 { "--mapfile", 1, OptMapFile },
1239 { "--memory-model", 1, OptMemoryModel },
1240 { "--module", 0, OptModule },
1241 { "--module-id", 1, OptModuleId },
1242 { "--o65-model", 1, OptO65Model },
1243 { "--obj", 1, OptObj },
1244 { "--obj-path", 1, OptObjPath },
1245 { "--register-space", 1, OptRegisterSpace },
1246 { "--register-vars", 0, OptRegisterVars },
1247 { "--rodata-name", 1, OptRodataName },
1248 { "--signed-chars", 0, OptSignedChars },
1249 { "--standard", 1, OptStandard },
1250 { "--start-addr", 1, OptStartAddr },
1251 { "--static-locals", 0, OptStaticLocals },
1252 { "--target", 1, OptTarget },
1253 { "--verbose", 0, OptVerbose },
1254 { "--version", 0, OptVersion },
1255 { "--zeropage-label", 1, OptZeropageLabel },
1256 { "--zeropage-name", 1, OptZeropageName },
1261 /* Initialize the cmdline module */
1262 InitCmdLine (&argc, &argv, "cl65");
1264 /* Initialize the command descriptors */
1265 CmdInit (&CC65, "cc65");
1266 CmdInit (&CA65, "ca65");
1267 CmdInit (&CO65, "co65");
1268 CmdInit (&LD65, "ld65");
1269 CmdInit (&GRC, "grc");
1271 /* Our default target is the C64 instead of "none" */
1274 /* Check the parameters */
1276 while (I < ArgCount) {
1278 /* Get the argument */
1279 const char* Arg = ArgVec[I];
1281 /* Check for an option */
1282 if (Arg [0] == '-') {
1287 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
1291 if (Arg[2] == 'l' && Arg[3] == '\0') {
1292 /* Make local variables static */
1293 OptStaticLocals (Arg, 0);
1295 /* Specify linker config file */
1296 OptConfig (Arg, GetArg (&I, 2));
1301 /* Define a preprocessor symbol (compiler) */
1302 CmdAddArg2 (&CC65, "-D", GetArg (&I, 2));
1306 /* Include directory (compiler) */
1307 OptIncludeDir (Arg, GetArg (&I, 2));
1311 if (Arg[2] == 'n' && Arg[3] == '\0') {
1312 /* VICE label file (linker) */
1313 CmdAddArg2 (&LD65, "-Ln", GetArg (&I, 3));
1315 /* Library search path (linker) */
1316 OptLibPath (Arg, GetArg (&I, 2));
1321 /* Optimize code (compiler, also covers -Oi and others) */
1322 CmdAddArg (&CC65, Arg);
1326 /* Dont assemble and link the created files */
1327 DontLink = DontAssemble = 1;
1331 /* Include source as comment (compiler) */
1332 OptAddSource (Arg, 0);
1336 /* Print version number */
1337 OptVersion (Arg, 0);
1344 OptAsmArgs (Arg, GetArg (&I, 3));
1348 OptLdArgs (Arg, GetArg (&I, 3));
1352 /* Suppress warnings - compiler and assembler */
1353 CmdAddArg (&CC65, "-W");
1354 CmdAddArg2 (&CA65, "-W", "0");
1358 UnknownOption (Arg);
1364 /* Don't link the resulting files */
1369 /* Debug mode (compiler) */
1374 /* Debugging - add to compiler and assembler */
1375 OptDebugInfo (Arg, 0);
1380 /* Print help - cl65 */
1385 /* Default characters are signed */
1386 OptSignedChars (Arg, 0);
1390 /* Create an assembler listing */
1391 OptListing (Arg, 0);
1395 /* Create a map file (linker) */
1396 OptMapFile (Arg, GetArg (&I, 2));
1400 /* Name the output file */
1401 OutputName = GetArg (&I, 2);
1405 /* Enable register variables */
1406 OptRegisterVars (Arg, 0);
1410 /* Set target system - compiler, assembler and linker */
1411 OptTarget (Arg, GetArg (&I, 2));
1415 /* Force an import (linker) */
1416 OptForceImport (Arg, GetArg (&I, 2));
1420 if (Arg [2] == 'm') {
1421 /* Verbose map file (linker) */
1422 CmdAddArg (&LD65, "-vm");
1424 /* Verbose mode (compiler, assembler, linker) */
1425 OptVerbose (Arg, 0);
1430 UnknownOption (Arg);
1434 /* Remember the first file name */
1435 if (FirstInput == 0) {
1439 /* Determine the file type by the extension */
1440 switch (GetFileType (Arg)) {
1443 /* Compile the file */
1448 /* Assemble the file */
1449 if (!DontAssemble) {
1456 /* Add to the linker files */
1457 CmdAddFile (&LD65, Arg);
1461 /* Add to the resource compiler files */
1466 /* Add the the object file converter files */
1471 Error ("Don't know what to do with `%s'", Arg);
1481 /* Check if we had any input files */
1482 if (FirstInput == 0) {
1483 Warning ("No input files");
1486 /* Link the given files if requested and if we have any */
1487 if (DontLink == 0 && LD65.FileCount > 0) {
1491 /* Return an apropriate exit code */
1492 return EXIT_SUCCESS;