1 /*****************************************************************************/
5 /* Main module for the cl65 compile-and-link utility */
9 /* (C) 1999-2012, 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__)
45 # pragma warning(disable : 4996)
75 /*****************************************************************************/
77 /*****************************************************************************/
81 /* Struct that describes a command */
82 typedef struct CmdDesc CmdDesc;
84 char* Name; /* The command name */
86 unsigned ArgCount; /* Count of arguments */
87 unsigned ArgMax; /* Maximum count of arguments */
88 char** Args; /* The arguments */
90 unsigned FileCount; /* Count of files to translate */
91 unsigned FileMax; /* Maximum count of files */
92 char** Files; /* The files */
95 /* Command descriptors for the different programs */
96 static CmdDesc CC65 = { 0, 0, 0, 0, 0, 0, 0 };
97 static CmdDesc CA65 = { 0, 0, 0, 0, 0, 0, 0 };
98 static CmdDesc CO65 = { 0, 0, 0, 0, 0, 0, 0 };
99 static CmdDesc LD65 = { 0, 0, 0, 0, 0, 0, 0 };
100 static CmdDesc GRC = { 0, 0, 0, 0, 0, 0, 0 };
102 /* Variables controlling the steps we're doing */
103 static int DoLink = 1;
104 static int DoAssemble = 1;
106 /* The name of the output file, NULL if none given */
107 static const char* OutputName = 0;
109 /* The name of the linker configuration file if given */
110 static const char* LinkerConfig = 0;
112 /* The name of the first input file. This will be used to construct the
113 * executable file name if no explicit name is given.
115 static const char* FirstInput = 0;
117 /* The names of the files for dependency generation */
118 static const char* DepName = 0;
119 static const char* FullDepName = 0;
121 /* Remember if we should link a module */
122 static int Module = 0;
124 /* Extension used for a module */
125 #define MODULE_EXT ".o65"
127 /* Name of the target specific runtime library */
128 static char* TargetLib = 0;
132 /*****************************************************************************/
133 /* Include the system specific spawn function */
134 /*****************************************************************************/
138 #if defined(NEED_SPAWN)
139 # if defined(SPAWN_AMIGA)
140 # include "spawn-amiga.inc"
142 # include "spawn-unix.inc"
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, const char* Name)
287 /* Initialize the command using the given path and name of the executable */
291 FullName = (char*) xmalloc (strlen (Path) + strlen (Name) + 1);
292 strcpy (FullName, Path);
293 strcat (FullName, Name);
295 /* Remember the command */
296 Cmd->Name = xstrdup (FullName);
298 /* Use the command name as first argument */
299 CmdAddArg (Cmd, FullName);
306 static void CmdSetOutput (CmdDesc* Cmd, const char* File)
307 /* Set the output file in a command desc */
309 CmdAddArg2 (Cmd, "-o", File);
314 static void CmdSetTarget (CmdDesc* Cmd, target_t Target)
315 /* Set the output file in a command desc */
317 CmdAddArg2 (Cmd, "-t", GetTargetName (Target));
322 static void CmdPrint (CmdDesc* Cmd, FILE* F)
323 /* Output the command line encoded in the command desc */
326 for (I = 0; I < Cmd->ArgCount && Cmd->Args[I] != 0; ++I) {
327 fprintf (F, "%s ", Cmd->Args[I]);
333 /*****************************************************************************/
334 /* Target handling */
335 /*****************************************************************************/
339 static void SetTargetFiles (void)
340 /* Set the target system files */
342 /* Determine the names of the target specific library file */
343 if (Target != TGT_NONE) {
345 /* Get a pointer to the system name and its length */
346 const char* TargetName = GetTargetName (Target);
347 unsigned TargetNameLen = strlen (TargetName);
349 /* Set the library file */
350 TargetLib = xmalloc (TargetNameLen + 4 + 1);
351 memcpy (TargetLib, TargetName, TargetNameLen);
352 strcpy (TargetLib + TargetNameLen, ".lib");
359 /*****************************************************************************/
361 /*****************************************************************************/
365 static void ExecProgram (CmdDesc* Cmd)
366 /* Execute a subprocess with the given name/parameters. Exit on errors. */
370 /* If in debug mode, output the command line we will execute */
372 printf ("Executing: ");
373 CmdPrint (Cmd, stdout);
377 /* Call the program */
378 Status = spawnvp (P_WAIT, Cmd->Name, Cmd->Args);
380 /* Check the result code */
382 /* Error executing the program */
383 Error ("Cannot execute `%s': %s", Cmd->Name, strerror (errno));
384 } else if (Status != 0) {
385 /* Called program had an error */
392 static void Link (void)
393 /* Link the resulting executable */
397 /* Since linking is always the final step, if we have an output file name
398 * given, set it here. If we don't have an explicit output name given,
399 * try to build one from the name of the first input file.
403 CmdSetOutput (&LD65, OutputName);
405 } else if (FirstInput && FindExt (FirstInput)) { /* Only if ext present! */
407 const char* Extension = Module? MODULE_EXT : "";
408 char* Output = MakeFilename (FirstInput, Extension);
409 CmdSetOutput (&LD65, Output);
414 /* If we have a linker config file given, add it to the command line.
415 * Otherwise pass the target to the linker if we have one.
419 Error ("Cannot use -C and --module together");
421 CmdAddArg2 (&LD65, "-C", LinkerConfig);
423 CmdSetTarget (&LD65, TGT_MODULE);
425 CmdSetTarget (&LD65, Target);
428 /* Determine which target libraries are needed */
431 /* Add all object files as parameters */
432 for (I = 0; I < LD65.FileCount; ++I) {
433 CmdAddArg (&LD65, LD65.Files [I]);
436 /* Add the system runtime library */
438 CmdAddArg (&LD65, TargetLib);
441 /* Terminate the argument list with a NULL pointer */
442 CmdAddArg (&LD65, 0);
444 /* Call the linker */
450 static void AssembleFile (const char* File, unsigned ArgCount)
451 /* Common routine to assemble a file. Will be called by Assemble() and
452 * AssembleIntermediate(). Adds options common for both routines and
453 * assembles the file. Will remove excess arguments after assembly.
456 /* Set the target system */
457 CmdSetTarget (&CA65, Target);
459 /* Check if this is the last processing step */
461 /* We're linking later. Add the output file of the assembly
462 * the the file list of the linker. The name of the output
463 * file is that of the input file with ".s" replaced by ".o".
465 char* ObjName = MakeFilename (File, ".o");
466 CmdAddFile (&LD65, ObjName);
469 /* This is the final step. If an output name is given, set it */
471 CmdSetOutput (&CA65, OutputName);
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 AssembleIntermediate (const char* SourceFile)
491 /* Assemble an intermediate file which was generated by a previous processing
492 * step with SourceFile as input. The -dep options won't be added and
493 * the intermediate assembler file is removed after assembly.
496 /* Generate the name of the assembler output file from the source file
497 * name. It's the same name with the extension replaced by ".s"
499 char* AsmName = MakeFilename (SourceFile, ".s");
501 /* Assemble the intermediate assembler file */
502 AssembleFile (AsmName, CA65.ArgCount);
504 /* Remove the input file */
505 if (remove (AsmName) < 0) {
506 Warning ("Cannot remove temporary file `%s': %s",
507 AsmName, strerror (errno));
510 /* Free the assembler file name which was allocated from the heap */
516 static void Assemble (const char* File)
517 /* Assemble the given file */
519 /* Remember the current assembler argument count */
520 unsigned ArgCount = CA65.ArgCount;
522 /* We aren't assembling an intermediate file, but one requested by the
523 * user. So add a few options here if they were given on the command
526 if (DepName && *DepName) {
527 CmdAddArg2 (&CA65, "--create-dep", DepName);
529 if (FullDepName && *FullDepName) {
530 CmdAddArg2 (&CA65, "--create-full-dep", FullDepName);
533 /* Use the common routine */
534 AssembleFile (File, ArgCount);
539 static void Compile (const char* File)
540 /* Compile the given file */
542 /* Remember the current compiler argument count */
543 unsigned ArgCount = CC65.ArgCount;
545 /* Set the target system */
546 CmdSetTarget (&CC65, Target);
548 /* Check if this is the final step */
550 /* We will assemble this file later. If a dependency file is to be
551 * generated, set the dependency target to be the final object file,
552 * not the intermediate assembler file. But beware: There may be an
553 * output name specified for the assembler.
555 if (DepName || FullDepName) {
556 /* Was an output name for the assembler specified? */
557 if (!DoLink && OutputName) {
558 /* Use this name as the dependency target */
559 CmdAddArg2 (&CC65, "--dep-target", OutputName);
561 /* Use the object file name as the dependency target */
562 char* ObjName = MakeFilename (File, ".o");
563 CmdAddArg2 (&CC65, "--dep-target", ObjName);
568 /* If we won't assemble, this is the final step. In this case, set
569 * the output name if it was given.
572 CmdSetOutput (&CC65, OutputName);
576 /* Add the file as argument for the compiler */
577 CmdAddArg (&CC65, File);
579 /* Add a NULL pointer to terminate the argument list */
580 CmdAddArg (&CC65, 0);
582 /* Run the compiler */
585 /* Remove the excess arguments */
586 CmdDelArgs (&CC65, ArgCount);
588 /* If this is not the final step, assemble the generated file, then
592 /* Assemble the intermediate file and remove it */
593 AssembleIntermediate (File);
599 static void CompileRes (const char* File)
600 /* Compile the given geos resource file */
602 /* Remember the current assembler argument count */
603 unsigned ArgCount = GRC.ArgCount;
605 /* Resource files need an geos-apple or geos-cbm target but this
606 * is checked within grc65.
608 CmdSetTarget (&GRC, Target);
610 /* Add the file as argument for the resource compiler */
611 CmdAddArg (&GRC, File);
613 /* Add a NULL pointer to terminate the argument list */
616 /* Run the compiler */
619 /* Remove the excess arguments */
620 CmdDelArgs (&GRC, ArgCount);
622 /* If this is not the final step, assemble the generated file, then
626 /* Assemble the intermediate file and remove it */
627 AssembleIntermediate (File);
633 static void ConvertO65 (const char* File)
634 /* Convert an o65 object file into an assembler file */
636 /* Remember the current converter argument count */
637 unsigned ArgCount = CO65.ArgCount;
639 /* If we won't assemble, this is the final step. In this case, set the
642 if (!DoAssemble && OutputName) {
643 CmdSetOutput (&CO65, OutputName);
646 /* Add the file as argument for the object file converter */
647 CmdAddArg (&CO65, File);
649 /* Add a NULL pointer to terminate the argument list */
650 CmdAddArg (&CO65, 0);
652 /* Run the converter */
655 /* Remove the excess arguments */
656 CmdDelArgs (&CO65, ArgCount);
658 /* If this is not the final step, assemble the generated file, then
662 /* Assemble the intermediate file and remove it */
663 AssembleIntermediate (File);
669 /*****************************************************************************/
671 /*****************************************************************************/
675 static void Usage (void)
676 /* Print usage information and exit */
678 printf ("Usage: %s [options] file [...]\n"
680 " -c\t\t\t\tCompile and assemble, but don't link\n"
681 " -d\t\t\t\tDebug mode\n"
682 " -g\t\t\t\tAdd debug info\n"
683 " -h\t\t\t\tHelp (this text)\n"
684 " -l name\t\t\tCreate an assembler listing file\n"
685 " -m name\t\t\tCreate a map file\n"
686 " -mm model\t\t\tSet the memory model\n"
687 " -o name\t\t\tName the output file\n"
688 " -r\t\t\t\tEnable register variables\n"
689 " -t sys\t\t\tSet the target system\n"
690 " -u sym\t\t\tForce an import of symbol `sym'\n"
691 " -v\t\t\t\tVerbose mode\n"
692 " -vm\t\t\t\tVerbose map file\n"
693 " -C name\t\t\tUse linker config file\n"
694 " -Cl\t\t\t\tMake local variables static\n"
695 " -D sym[=defn]\t\t\tDefine a preprocessor symbol\n"
696 " -I dir\t\t\tSet a compiler include directory path\n"
697 " -L path\t\t\tSpecify a library search path\n"
698 " -Ln name\t\t\tCreate a VICE label file\n"
699 " -O\t\t\t\tOptimize code\n"
700 " -Oi\t\t\t\tOptimize code, inline runtime functions\n"
701 " -Or\t\t\t\tOptimize code, honour the register keyword\n"
702 " -Os\t\t\t\tOptimize code, inline known C functions\n"
703 " -S\t\t\t\tCompile, but don't assemble and link\n"
704 " -T\t\t\t\tInclude source as comment\n"
705 " -V\t\t\t\tPrint the version number\n"
706 " -W name[,...]\t\t\tSuppress compiler warnings\n"
707 " -Wa options\t\t\tPass options to the assembler\n"
708 " -Wc options\t\t\tPass options to the compiler\n"
709 " -Wl options\t\t\tPass options to the linker\n"
712 " --add-source\t\t\tInclude source as comment\n"
713 " --asm-args options\t\tPass options to the assembler\n"
714 " --asm-define sym[=v]\t\tDefine an assembler symbol\n"
715 " --asm-include-dir dir\t\tSet an assembler include directory\n"
716 " --bin-include-dir dir\t\tSet an assembler binary include directory\n"
717 " --bss-label name\t\tDefine and export a BSS segment label\n"
718 " --bss-name seg\t\tSet the name of the BSS segment\n"
719 " --cc-args options\t\tPass options to the compiler\n"
720 " --cfg-path path\t\tSpecify a config file search path\n"
721 " --check-stack\t\t\tGenerate stack overflow checks\n"
722 " --code-label name\t\tDefine and export a CODE segment label\n"
723 " --code-name seg\t\tSet the name of the CODE segment\n"
724 " --codesize x\t\t\tAccept larger code by factor x\n"
725 " --config name\t\t\tUse linker config file\n"
726 " --cpu type\t\t\tSet CPU type\n"
727 " --create-dep name\t\tCreate a make dependency file\n"
728 " --create-full-dep name\tCreate a full make dependency file\n"
729 " --data-label name\t\tDefine and export a DATA segment label\n"
730 " --data-name seg\t\tSet the name of the DATA segment\n"
731 " --debug\t\t\tDebug mode\n"
732 " --debug-info\t\t\tAdd debug info\n"
733 " --feature name\t\tSet an emulation feature\n"
734 " --force-import sym\t\tForce an import of symbol `sym'\n"
735 " --forget-inc-paths\t\tForget include search paths (compiler)\n"
736 " --help\t\t\tHelp (this text)\n"
737 " --include-dir dir\t\tSet a compiler include directory path\n"
738 " --ld-args options\t\tPass options to the linker\n"
739 " --lib file\t\t\tLink this library\n"
740 " --lib-path path\t\tSpecify a library search path\n"
741 " --list-targets\t\tList all available targets\n"
742 " --listing name\t\tCreate an assembler listing file\n"
743 " --list-bytes n\t\tNumber of bytes per assembler listing line\n"
744 " --mapfile name\t\tCreate a map file\n"
745 " --memory-model model\t\tSet the memory model\n"
746 " --module\t\t\tLink as a module\n"
747 " --module-id id\t\tSpecify a module ID for the linker\n"
748 " --o65-model model\t\tOverride the o65 model\n"
749 " --obj file\t\t\tLink this object file\n"
750 " --obj-path path\t\tSpecify an object file search path\n"
751 " --register-space b\t\tSet space available for register variables\n"
752 " --register-vars\t\tEnable register variables\n"
753 " --rodata-name seg\t\tSet the name of the RODATA segment\n"
754 " --signed-chars\t\tDefault characters are signed\n"
755 " --standard std\t\tLanguage standard (c89, c99, cc65)\n"
756 " --start-addr addr\t\tSet the default start address\n"
757 " --static-locals\t\tMake local variables static\n"
758 " --target sys\t\t\tSet the target system\n"
759 " --version\t\t\tPrint the version number\n"
760 " --verbose\t\t\tVerbose mode\n"
761 " --zeropage-label name\t\tDefine and export a ZEROPAGE segment label\n"
762 " --zeropage-name seg\t\tSet the name of the ZEROPAGE segment\n",
768 static void OptAddSource (const char* Opt attribute ((unused)),
769 const char* Arg attribute ((unused)))
770 /* Strict source code as comments to the generated asm code */
772 CmdAddArg (&CC65, "-T");
777 static void OptAsmArgs (const char* Opt attribute ((unused)), const char* Arg)
778 /* Pass arguments to the assembler */
780 CmdAddArgList (&CA65, Arg);
785 static void OptAsmDefine (const char* Opt attribute ((unused)), const char* Arg)
786 /* Define an assembler symbol (assembler) */
788 CmdAddArg2 (&CA65, "-D", Arg);
793 static void OptAsmIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
794 /* Include directory (assembler) */
796 CmdAddArg2 (&CA65, "-I", Arg);
801 static void OptBinIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
802 /* Binary include directory (assembler) */
804 CmdAddArg2 (&CA65, "--bin-include-dir", Arg);
809 static void OptBssLabel (const char* Opt attribute ((unused)), const char* Arg)
810 /* Handle the --bss-label option */
812 CmdAddArg2 (&CO65, "--bss-label", Arg);
817 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
818 /* Handle the --bss-name option */
820 CmdAddArg2 (&CC65, "--bss-name", Arg);
821 CmdAddArg2 (&CO65, "--bss-name", Arg);
826 static void OptCCArgs (const char* Opt attribute ((unused)), const char* Arg)
827 /* Pass arguments to the compiler */
829 CmdAddArgList (&CC65, Arg);
834 static void OptCfgPath (const char* Opt attribute ((unused)), const char* Arg)
835 /* Config file search path (linker) */
837 CmdAddArg2 (&LD65, "--cfg-path", Arg);
842 static void OptCheckStack (const char* Opt attribute ((unused)),
843 const char* Arg attribute ((unused)))
844 /* Handle the --check-stack option */
846 CmdAddArg (&CC65, "--check-stack");
851 static void OptCodeLabel (const char* Opt attribute ((unused)), const char* Arg)
852 /* Handle the --code-label option */
854 CmdAddArg2 (&CO65, "--code-label", Arg);
859 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
860 /* Handle the --code-name option */
862 CmdAddArg2 (&CC65, "--code-name", Arg);
863 CmdAddArg2 (&CO65, "--code-name", Arg);
868 static void OptCodeSize (const char* Opt attribute ((unused)), const char* Arg)
869 /* Handle the --codesize option */
871 CmdAddArg2 (&CC65, "--codesize", Arg);
876 static void OptConfig (const char* Opt attribute ((unused)), const char* Arg)
877 /* Config file (linker) */
880 Error ("Cannot specify -C/--config twice");
887 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
888 /* Handle the --cpu option */
890 /* Add the cpu type to the assembler and compiler */
891 CmdAddArg2 (&CA65, "--cpu", Arg);
892 CmdAddArg2 (&CC65, "--cpu", Arg);
897 static void OptCreateDep (const char* Opt attribute ((unused)), const char* Arg)
898 /* Handle the --create-dep option */
900 /* Add the file name to the compiler */
901 CmdAddArg2 (&CC65, "--create-dep", Arg);
903 /* Remember the file name for the assembler */
909 static void OptCreateFullDep (const char* Opt attribute ((unused)), const char* Arg)
910 /* Handle the --create-full-dep option */
912 /* Add the file name to the compiler */
913 CmdAddArg2 (&CC65, "--create-full-dep", Arg);
915 /* Remember the file name for the assembler */
921 static void OptDataLabel (const char* Opt attribute ((unused)), const char* Arg)
922 /* Handle the --data-label option */
924 CmdAddArg2 (&CO65, "--data-label", Arg);
929 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
930 /* Handle the --data-name option */
932 CmdAddArg2 (&CC65, "--data-name", Arg);
933 CmdAddArg2 (&CO65, "--data-name", Arg);
938 static void OptDebug (const char* Opt attribute ((unused)),
939 const char* Arg attribute ((unused)))
940 /* Debug mode (compiler and cl65 utility) */
942 CmdAddArg (&CC65, "-d");
943 CmdAddArg (&CO65, "-d");
949 static void OptDebugInfo (const char* Opt attribute ((unused)),
950 const char* Arg attribute ((unused)))
951 /* Debug Info - add to compiler and assembler */
953 CmdAddArg (&CC65, "-g");
954 CmdAddArg (&CA65, "-g");
955 CmdAddArg (&CO65, "-g");
960 static void OptFeature (const char* Opt attribute ((unused)), const char* Arg)
961 /* Emulation features for the assembler */
963 CmdAddArg2 (&CA65, "--feature", Arg);
968 static void OptForceImport (const char* Opt attribute ((unused)), const char* Arg)
969 /* Emulation features for the assembler */
971 CmdAddArg2 (&LD65, "-u", Arg);
976 static void OptForgetIncPaths (const char* Opt attribute ((unused)),
977 const char* Arg attribute ((unused)))
978 /* Forget all currently defined include paths */
980 CmdAddArg (&CC65, "--forget-inc-paths");
985 static void OptHelp (const char* Opt attribute ((unused)),
986 const char* Arg attribute ((unused)))
987 /* Print help - cl65 */
995 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
996 /* Include directory (compiler) */
998 CmdAddArg2 (&CC65, "-I", Arg);
1003 static void OptLdArgs (const char* Opt attribute ((unused)), const char* Arg)
1004 /* Pass arguments to the linker */
1006 CmdAddArgList (&LD65, Arg);
1011 static void OptLib (const char* Opt attribute ((unused)), const char* Arg)
1012 /* Library file follows (linker) */
1014 CmdAddArg2 (&LD65, "--lib", Arg);
1019 static void OptLibPath (const char* Opt attribute ((unused)), const char* Arg)
1020 /* Library search path (linker) */
1022 CmdAddArg2 (&LD65, "--lib-path", Arg);
1027 static void OptListBytes (const char* Opt attribute ((unused)), const char* Arg)
1028 /* Set the maximum number of bytes per asm listing line */
1030 CmdAddArg2 (&CA65, "--list-bytes", Arg);
1035 static void OptListing (const char* Opt attribute ((unused)), const char* Arg)
1036 /* Create an assembler listing */
1038 CmdAddArg2 (&CA65, "-l", Arg);
1043 static void OptListTargets (const char* Opt attribute ((unused)),
1044 const char* Arg attribute ((unused)))
1045 /* List all targets */
1049 /* List the targets */
1050 for (T = TGT_NONE; T < TGT_COUNT; ++T) {
1051 printf ("%s\n", GetTargetName (T));
1055 exit (EXIT_SUCCESS);
1060 static void OptMapFile (const char* Opt attribute ((unused)), const char* Arg)
1061 /* Create a map file */
1063 /* Create a map file (linker) */
1064 CmdAddArg2 (&LD65, "-m", Arg);
1069 static void OptMemoryModel (const char* Opt attribute ((unused)), const char* Arg)
1070 /* Set the memory model */
1072 mmodel_t MemoryModel = FindMemoryModel (Arg);
1073 if (MemoryModel == MMODEL_UNKNOWN) {
1074 Error ("Unknown memory model: %s", Arg);
1075 } else if (MemoryModel == MMODEL_HUGE) {
1076 Error ("Unsupported memory model: %s", Arg);
1078 CmdAddArg2 (&CA65, "-mm", Arg);
1079 CmdAddArg2 (&CC65, "-mm", Arg);
1085 static void OptModule (const char* Opt attribute ((unused)),
1086 const char* Arg attribute ((unused)))
1087 /* Link as a module */
1094 static void OptModuleId (const char* Opt attribute ((unused)), const char* Arg)
1095 /* Specify a module if for the linker */
1097 /* Pass it straight to the linker */
1098 CmdAddArg2 (&LD65, "--module-id", Arg);
1103 static void OptO65Model (const char* Opt attribute ((unused)), const char* Arg)
1104 /* Handle the --o65-model option */
1106 CmdAddArg2 (&CO65, "-m", Arg);
1111 static void OptObj (const char* Opt attribute ((unused)), const char* Arg)
1112 /* Object file follows (linker) */
1114 CmdAddArg2 (&LD65, "--obj", Arg);
1119 static void OptObjPath (const char* Opt attribute ((unused)), const char* Arg)
1120 /* Object file search path (linker) */
1122 CmdAddArg2 (&LD65, "--obj-path", Arg);
1127 static void OptRegisterSpace (const char* Opt attribute ((unused)), const char* Arg)
1128 /* Handle the --register-space option */
1130 CmdAddArg2 (&CC65, "--register-space", Arg);
1135 static void OptRegisterVars (const char* Opt attribute ((unused)),
1136 const char* Arg attribute ((unused)))
1137 /* Handle the --register-vars option */
1139 CmdAddArg (&CC65, "-r");
1144 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
1145 /* Handle the --rodata-name option */
1147 CmdAddArg2 (&CC65, "--rodata-name", Arg);
1152 static void OptSignedChars (const char* Opt attribute ((unused)),
1153 const char* Arg attribute ((unused)))
1154 /* Make default characters signed */
1156 CmdAddArg (&CC65, "-j");
1161 static void OptStandard (const char* Opt attribute ((unused)), const char* Arg)
1162 /* Set the language standard */
1164 CmdAddArg2 (&CC65, "--standard", Arg);
1169 static void OptStartAddr (const char* Opt attribute ((unused)), const char* Arg)
1170 /* Set the default start address */
1172 CmdAddArg2 (&LD65, "-S", Arg);
1177 static void OptStaticLocals (const char* Opt attribute ((unused)),
1178 const char* Arg attribute ((unused)))
1179 /* Place local variables in static storage */
1181 CmdAddArg (&CC65, "-Cl");
1186 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
1187 /* Set the target system */
1189 Target = FindTarget (Arg);
1190 if (Target == TGT_UNKNOWN) {
1191 Error ("No such target system: `%s'", Arg);
1192 } else if (Target == TGT_MODULE) {
1193 Error ("Cannot use `module' as target, use --module instead");
1199 static void OptVerbose (const char* Opt attribute ((unused)),
1200 const char* Arg attribute ((unused)))
1201 /* Verbose mode (compiler, assembler, linker) */
1203 CmdAddArg (&CC65, "-v");
1204 CmdAddArg (&CA65, "-v");
1205 CmdAddArg (&CO65, "-v");
1206 CmdAddArg (&LD65, "-v");
1211 static void OptVersion (const char* Opt attribute ((unused)),
1212 const char* Arg attribute ((unused)))
1213 /* Print version number */
1215 fprintf (stderr, "cl65 V%s\n", GetVersionAsString ());
1220 static void OptZeropageLabel (const char* Opt attribute ((unused)), const char* Arg)
1221 /* Handle the --zeropage-label option */
1223 CmdAddArg2 (&CO65, "--zeropage-label", Arg);
1228 static void OptZeropageName (const char* Opt attribute ((unused)), const char* Arg)
1229 /* Handle the --zeropage-name option */
1231 CmdAddArg2 (&CO65, "--zeropage-name", Arg);
1236 int main (int argc, char* argv [])
1237 /* Utility main program */
1239 /* Program long options */
1240 static const LongOpt OptTab[] = {
1241 { "--add-source", 0, OptAddSource },
1242 { "--asm-args", 1, OptAsmArgs },
1243 { "--asm-define", 1, OptAsmDefine },
1244 { "--asm-include-dir", 1, OptAsmIncludeDir },
1245 { "--bin-include-dir", 1, OptBinIncludeDir },
1246 { "--bss-label", 1, OptBssLabel },
1247 { "--bss-name", 1, OptBssName },
1248 { "--cc-args", 1, OptCCArgs },
1249 { "--cfg-path", 1, OptCfgPath },
1250 { "--check-stack", 0, OptCheckStack },
1251 { "--code-label", 1, OptCodeLabel },
1252 { "--code-name", 1, OptCodeName },
1253 { "--codesize", 1, OptCodeSize },
1254 { "--config", 1, OptConfig },
1255 { "--cpu", 1, OptCPU },
1256 { "--create-dep", 1, OptCreateDep },
1257 { "--create-full-dep", 1, OptCreateFullDep },
1258 { "--data-label", 1, OptDataLabel },
1259 { "--data-name", 1, OptDataName },
1260 { "--debug", 0, OptDebug },
1261 { "--debug-info", 0, OptDebugInfo },
1262 { "--feature", 1, OptFeature },
1263 { "--force-import", 1, OptForceImport },
1264 { "--forget-inc-paths", 0, OptForgetIncPaths },
1265 { "--help", 0, OptHelp },
1266 { "--include-dir", 1, OptIncludeDir },
1267 { "--ld-args", 1, OptLdArgs },
1268 { "--lib", 1, OptLib },
1269 { "--lib-path", 1, OptLibPath },
1270 { "--list-targets", 0, OptListTargets },
1271 { "--listing", 1, OptListing },
1272 { "--list-bytes", 1, OptListBytes },
1273 { "--mapfile", 1, OptMapFile },
1274 { "--memory-model", 1, OptMemoryModel },
1275 { "--module", 0, OptModule },
1276 { "--module-id", 1, OptModuleId },
1277 { "--o65-model", 1, OptO65Model },
1278 { "--obj", 1, OptObj },
1279 { "--obj-path", 1, OptObjPath },
1280 { "--register-space", 1, OptRegisterSpace },
1281 { "--register-vars", 0, OptRegisterVars },
1282 { "--rodata-name", 1, OptRodataName },
1283 { "--signed-chars", 0, OptSignedChars },
1284 { "--standard", 1, OptStandard },
1285 { "--start-addr", 1, OptStartAddr },
1286 { "--static-locals", 0, OptStaticLocals },
1287 { "--target", 1, OptTarget },
1288 { "--verbose", 0, OptVerbose },
1289 { "--version", 0, OptVersion },
1290 { "--zeropage-label", 1, OptZeropageLabel },
1291 { "--zeropage-name", 1, OptZeropageName },
1297 /* Initialize the cmdline module */
1298 InitCmdLine (&argc, &argv, "cl65");
1300 /* Initialize the command descriptors */
1302 CmdPath = xstrdup ("");
1305 CmdPath = xstrdup (argv[0]);
1306 Ptr = strrchr (CmdPath, '/');
1308 Ptr = strrchr (CmdPath, '\\');
1316 CmdInit (&CC65, CmdPath, "cc65");
1317 CmdInit (&CA65, CmdPath, "ca65");
1318 CmdInit (&CO65, CmdPath, "co65");
1319 CmdInit (&LD65, CmdPath, "ld65");
1320 CmdInit (&GRC, CmdPath, "grc65");
1323 /* Our default target is the C64 instead of "none" */
1326 /* Check the parameters */
1328 while (I < ArgCount) {
1330 /* Get the argument */
1331 const char* Arg = ArgVec[I];
1333 /* Check for an option */
1334 if (Arg [0] == '-') {
1339 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
1343 if (Arg[2] == 'l' && Arg[3] == '\0') {
1344 /* Make local variables static */
1345 OptStaticLocals (Arg, 0);
1347 /* Specify linker config file */
1348 OptConfig (Arg, GetArg (&I, 2));
1353 /* Define a preprocessor symbol (compiler) */
1354 CmdAddArg2 (&CC65, "-D", GetArg (&I, 2));
1358 /* Include directory (compiler) */
1359 OptIncludeDir (Arg, GetArg (&I, 2));
1363 if (Arg[2] == 'n' && Arg[3] == '\0') {
1364 /* VICE label file (linker) */
1365 CmdAddArg2 (&LD65, "-Ln", GetArg (&I, 3));
1367 /* Library search path (linker) */
1368 OptLibPath (Arg, GetArg (&I, 2));
1373 /* Optimize code (compiler, also covers -Oi and others) */
1374 CmdAddArg (&CC65, Arg);
1378 /* Dont assemble and link the created files */
1384 /* Include source as comment (compiler) */
1385 OptAddSource (Arg, 0);
1389 /* Print version number */
1390 OptVersion (Arg, 0);
1394 if (Arg[2] == 'a' && Arg[3] == '\0') {
1395 /* -Wa: Pass options to assembler */
1396 OptAsmArgs (Arg, GetArg (&I, 3));
1397 } else if (Arg[2] == 'c' && Arg[3] == '\0') {
1398 /* -Wc: Pass options to compiler */
1399 OptCCArgs (Arg, GetArg (&I, 3));
1400 } else if (Arg[2] == 'l' && Arg[3] == '\0') {
1401 /* -Wl: Pass options to linker */
1402 OptLdArgs (Arg, GetArg (&I, 3));
1404 /* Anything else: Suppress warnings (compiler) */
1405 CmdAddArg2 (&CC65, "-W", GetArg (&I, 2));
1410 /* Don't link the resulting files */
1415 /* Debug mode (compiler) */
1420 /* Debugging - add to compiler and assembler */
1421 OptDebugInfo (Arg, 0);
1426 /* Print help - cl65 */
1431 /* Default characters are signed */
1432 OptSignedChars (Arg, 0);
1436 /* Create an assembler listing */
1437 OptListing (Arg, GetArg (&I, 2));
1441 /* Create a map file (linker) */
1442 OptMapFile (Arg, GetArg (&I, 2));
1446 /* Name the output file */
1447 OutputName = GetArg (&I, 2);
1451 /* Enable register variables */
1452 OptRegisterVars (Arg, 0);
1456 /* Set target system - compiler, assembler and linker */
1457 OptTarget (Arg, GetArg (&I, 2));
1461 /* Force an import (linker) */
1462 OptForceImport (Arg, GetArg (&I, 2));
1466 if (Arg [2] == 'm') {
1467 /* Verbose map file (linker) */
1468 CmdAddArg (&LD65, "-vm");
1470 /* Verbose mode (compiler, assembler, linker) */
1471 OptVerbose (Arg, 0);
1476 UnknownOption (Arg);
1480 /* Remember the first file name */
1481 if (FirstInput == 0) {
1485 /* Determine the file type by the extension */
1486 switch (GetFileType (Arg)) {
1489 /* Compile the file */
1494 /* Assemble the file */
1502 /* Add to the linker files */
1503 CmdAddFile (&LD65, Arg);
1507 /* Add to the resource compiler files */
1512 /* Add the the object file converter files */
1517 Error ("Don't know what to do with `%s'", Arg);
1527 /* Check if we had any input files */
1528 if (FirstInput == 0) {
1529 Warning ("No input files");
1532 /* Link the given files if requested and if we have any */
1533 if (DoLink && LD65.FileCount > 0) {
1537 /* Return an apropriate exit code */
1538 return EXIT_SUCCESS;