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_UNIX)
140 # include "spawn-unix.inc"
141 # elif defined(SPAWN_AMIGA)
142 # include "spawn-amiga.inc"
144 # error "Don't know which spawn module to include!"
150 /*****************************************************************************/
151 /* Command structure handling */
152 /*****************************************************************************/
156 static void CmdExpand (CmdDesc* Cmd)
157 /* Expand the argument vector */
159 unsigned NewMax = Cmd->ArgMax + 10;
160 char** NewArgs = xmalloc (NewMax * sizeof (char*));
161 memcpy (NewArgs, Cmd->Args, Cmd->ArgMax * sizeof (char*));
164 Cmd->ArgMax = NewMax;
169 static void CmdAddArg (CmdDesc* Cmd, const char* Arg)
170 /* Add a new argument to the command */
172 /* Expand the argument vector if needed */
173 if (Cmd->ArgCount >= Cmd->ArgMax) {
177 /* Add a copy of the new argument, allow a NULL pointer */
179 Cmd->Args[Cmd->ArgCount++] = xstrdup (Arg);
181 Cmd->Args[Cmd->ArgCount++] = 0;
187 static void CmdAddArg2 (CmdDesc* Cmd, const char* Arg1, const char* Arg2)
188 /* Add a new argument pair to the command */
190 CmdAddArg (Cmd, Arg1);
191 CmdAddArg (Cmd, Arg2);
196 static void CmdAddArgList (CmdDesc* Cmd, const char* ArgList)
197 /* Add a list of arguments separated by commas */
199 const char* Arg = ArgList;
203 if (*P == '\0' || *P == ',') {
205 /* End of argument, add it */
206 unsigned Len = P - Arg;
208 /* Expand the argument vector if needed */
209 if (Cmd->ArgCount >= Cmd->ArgMax) {
213 /* Add the new argument */
214 Cmd->Args[Cmd->ArgCount] = memcpy (xmalloc (Len + 1), Arg, Len);
215 Cmd->Args[Cmd->ArgCount][Len] = '\0';
218 /* If the argument was terminated by a comma, skip it, otherwise
222 /* Start over at next char */
228 /* Skip other chars */
237 static void CmdDelArgs (CmdDesc* Cmd, unsigned LastValid)
238 /* Remove all arguments with an index greater than LastValid */
240 while (Cmd->ArgCount > LastValid) {
242 xfree (Cmd->Args [Cmd->ArgCount]);
243 Cmd->Args [Cmd->ArgCount] = 0;
249 static void CmdAddFile (CmdDesc* Cmd, const char* File)
250 /* Add a new file to the command */
252 /* Expand the file vector if needed */
253 if (Cmd->FileCount == Cmd->FileMax) {
254 unsigned NewMax = Cmd->FileMax + 10;
255 char** NewFiles = xmalloc (NewMax * sizeof (char*));
256 memcpy (NewFiles, Cmd->Files, Cmd->FileMax * sizeof (char*));
258 Cmd->Files = NewFiles;
259 Cmd->FileMax = NewMax;
262 /* If the file name is not NULL (which is legal and is used to terminate
263 * the file list), check if the file name does already exist in the file
264 * list and print a warning if so. Regardless of the search result, add
269 for (I = 0; I < Cmd->FileCount; ++I) {
270 if (strcmp (Cmd->Files[I], File) == 0) {
272 Warning ("Duplicate file in argument list: `%s'", File);
273 /* No need to search further */
279 Cmd->Files [Cmd->FileCount++] = xstrdup (File);
281 /* Add a NULL pointer */
282 Cmd->Files [Cmd->FileCount++] = 0;
288 static void CmdInit (CmdDesc* Cmd, const char* Path)
289 /* Initialize the command using the given path to the executable */
291 /* Remember the command */
292 Cmd->Name = xstrdup (Path);
294 /* Use the command name as first argument */
295 CmdAddArg (Cmd, Path);
300 static void CmdSetOutput (CmdDesc* Cmd, const char* File)
301 /* Set the output file in a command desc */
303 CmdAddArg2 (Cmd, "-o", File);
308 static void CmdSetTarget (CmdDesc* Cmd, target_t Target)
309 /* Set the output file in a command desc */
311 CmdAddArg2 (Cmd, "-t", GetTargetName (Target));
316 static void CmdPrint (CmdDesc* Cmd, FILE* F)
317 /* Output the command line encoded in the command desc */
320 for (I = 0; I < Cmd->ArgCount && Cmd->Args[I] != 0; ++I) {
321 fprintf (F, "%s ", Cmd->Args[I]);
327 /*****************************************************************************/
328 /* Target handling */
329 /*****************************************************************************/
333 static void SetTargetFiles (void)
334 /* Set the target system files */
336 /* Determine the names of the target specific library file */
337 if (Target != TGT_NONE) {
339 /* Get a pointer to the system name and its length */
340 const char* TargetName = GetTargetName (Target);
341 unsigned TargetNameLen = strlen (TargetName);
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 /* Since linking is always the final step, if we have an output file name
392 * given, set it here. If we don't have an explicit output name given,
393 * try to build one from the name of the first input file.
397 CmdSetOutput (&LD65, OutputName);
399 } else if (FirstInput && FindExt (FirstInput)) { /* Only if ext present! */
401 const char* Extension = Module? MODULE_EXT : "";
402 char* Output = MakeFilename (FirstInput, Extension);
403 CmdSetOutput (&LD65, Output);
408 /* If we have a linker config file given, add it to the command line.
409 * Otherwise pass the target to the linker if we have one.
413 Error ("Cannot use -C and --module together");
415 CmdAddArg2 (&LD65, "-C", LinkerConfig);
417 CmdSetTarget (&LD65, TGT_MODULE);
419 CmdSetTarget (&LD65, Target);
422 /* Determine which target libraries are needed */
425 /* Add all object files as parameters */
426 for (I = 0; I < LD65.FileCount; ++I) {
427 CmdAddArg (&LD65, LD65.Files [I]);
430 /* Add the system runtime library */
432 CmdAddArg (&LD65, TargetLib);
435 /* Terminate the argument list with a NULL pointer */
436 CmdAddArg (&LD65, 0);
438 /* Call the linker */
444 static void AssembleFile (const char* File, unsigned ArgCount)
445 /* Common routine to assemble a file. Will be called by Assemble() and
446 * AssembleIntermediate(). Adds options common for both routines and
447 * assembles the file. Will remove excess arguments after assembly.
450 /* Set the target system */
451 CmdSetTarget (&CA65, Target);
453 /* Check if this is the last processing step */
455 /* We're linking later. Add the output file of the assembly
456 * the the file list of the linker. The name of the output
457 * file is that of the input file with ".s" replaced by ".o".
459 char* ObjName = MakeFilename (File, ".o");
460 CmdAddFile (&LD65, ObjName);
463 /* This is the final step. If an output name is given, set it */
465 CmdSetOutput (&CA65, OutputName);
469 /* Add the file as argument for the assembler */
470 CmdAddArg (&CA65, File);
472 /* Add a NULL pointer to terminate the argument list */
473 CmdAddArg (&CA65, 0);
475 /* Run the assembler */
478 /* Remove the excess arguments */
479 CmdDelArgs (&CA65, ArgCount);
484 static void AssembleIntermediate (const char* SourceFile)
485 /* Assemble an intermediate file which was generated by a previous processing
486 * step with SourceFile as input. The -dep options won't be added and
487 * the intermediate assembler file is removed after assembly.
490 /* Generate the name of the assembler output file from the source file
491 * name. It's the same name with the extension replaced by ".s"
493 char* AsmName = MakeFilename (SourceFile, ".s");
495 /* Assemble the intermediate assembler file */
496 AssembleFile (AsmName, CA65.ArgCount);
498 /* Remove the input file */
499 if (remove (AsmName) < 0) {
500 Warning ("Cannot remove temporary file `%s': %s",
501 AsmName, strerror (errno));
504 /* Free the assembler file name which was allocated from the heap */
510 static void Assemble (const char* File)
511 /* Assemble the given file */
513 /* Remember the current assembler argument count */
514 unsigned ArgCount = CA65.ArgCount;
516 /* We aren't assembling an intermediate file, but one requested by the
517 * user. So add a few options here if they were given on the command
520 if (DepName && *DepName) {
521 CmdAddArg2 (&CA65, "--create-dep", DepName);
523 if (FullDepName && *FullDepName) {
524 CmdAddArg2 (&CA65, "--create-full-dep", FullDepName);
527 /* Use the common routine */
528 AssembleFile (File, ArgCount);
533 static void Compile (const char* File)
534 /* Compile the given file */
536 /* Remember the current compiler argument count */
537 unsigned ArgCount = CC65.ArgCount;
539 /* Set the target system */
540 CmdSetTarget (&CC65, Target);
542 /* Check if this is the final step */
544 /* We will assemble this file later. If a dependency file is to be
545 * generated, set the dependency target to be the final object file,
546 * not the intermediate assembler file. But beware: There may be an
547 * output name specified for the assembler.
549 if (DepName || FullDepName) {
550 /* Was an output name for the assembler specified? */
551 if (!DoLink && OutputName) {
552 /* Use this name as the dependency target */
553 CmdAddArg2 (&CC65, "--dep-target", OutputName);
555 /* Use the object file name as the dependency target */
556 char* ObjName = MakeFilename (File, ".o");
557 CmdAddArg2 (&CC65, "--dep-target", ObjName);
562 /* If we won't assemble, this is the final step. In this case, set
563 * the output name if it was given.
566 CmdSetOutput (&CC65, OutputName);
570 /* Add the file as argument for the compiler */
571 CmdAddArg (&CC65, File);
573 /* Add a NULL pointer to terminate the argument list */
574 CmdAddArg (&CC65, 0);
576 /* Run the compiler */
579 /* Remove the excess arguments */
580 CmdDelArgs (&CC65, ArgCount);
582 /* If this is not the final step, assemble the generated file, then
586 /* Assemble the intermediate file and remove it */
587 AssembleIntermediate (File);
593 static void CompileRes (const char* File)
594 /* Compile the given geos resource file */
596 /* Remember the current assembler argument count */
597 unsigned ArgCount = GRC.ArgCount;
599 /* Resource files need an geos-apple or geos-cbm target but this
600 * is checked within grc65.
602 CmdSetTarget (&GRC, Target);
604 /* Add the file as argument for the resource compiler */
605 CmdAddArg (&GRC, File);
607 /* Add a NULL pointer to terminate the argument list */
610 /* Run the compiler */
613 /* Remove the excess arguments */
614 CmdDelArgs (&GRC, ArgCount);
616 /* If this is not the final step, assemble the generated file, then
620 /* Assemble the intermediate file and remove it */
621 AssembleIntermediate (File);
627 static void ConvertO65 (const char* File)
628 /* Convert an o65 object file into an assembler file */
630 /* Remember the current converter argument count */
631 unsigned ArgCount = CO65.ArgCount;
633 /* If we won't assemble, this is the final step. In this case, set the
636 if (!DoAssemble && OutputName) {
637 CmdSetOutput (&CO65, OutputName);
640 /* Add the file as argument for the object file converter */
641 CmdAddArg (&CO65, File);
643 /* Add a NULL pointer to terminate the argument list */
644 CmdAddArg (&CO65, 0);
646 /* Run the converter */
649 /* Remove the excess arguments */
650 CmdDelArgs (&CO65, ArgCount);
652 /* If this is not the final step, assemble the generated file, then
656 /* Assemble the intermediate file and remove it */
657 AssembleIntermediate (File);
663 /*****************************************************************************/
665 /*****************************************************************************/
669 static void Usage (void)
670 /* Print usage information and exit */
672 printf ("Usage: %s [options] file [...]\n"
674 " -c\t\t\t\tCompile and assemble, but don't link\n"
675 " -d\t\t\t\tDebug mode\n"
676 " -g\t\t\t\tAdd debug info\n"
677 " -h\t\t\t\tHelp (this text)\n"
678 " -l name\t\t\tCreate an assembler listing file\n"
679 " -m name\t\t\tCreate a map file\n"
680 " -mm model\t\t\tSet the memory model\n"
681 " -o name\t\t\tName the output file\n"
682 " -r\t\t\t\tEnable register variables\n"
683 " -t sys\t\t\tSet the target system\n"
684 " -u sym\t\t\tForce an import of symbol `sym'\n"
685 " -v\t\t\t\tVerbose mode\n"
686 " -vm\t\t\t\tVerbose map file\n"
687 " -C name\t\t\tUse linker config file\n"
688 " -Cl\t\t\t\tMake local variables static\n"
689 " -D sym[=defn]\t\t\tDefine a preprocessor symbol\n"
690 " -I dir\t\t\tSet a compiler include directory path\n"
691 " -L path\t\t\tSpecify a library search path\n"
692 " -Ln name\t\t\tCreate a VICE label file\n"
693 " -O\t\t\t\tOptimize code\n"
694 " -Oi\t\t\t\tOptimize code, inline runtime functions\n"
695 " -Or\t\t\t\tOptimize code, honour the register keyword\n"
696 " -Os\t\t\t\tOptimize code, inline known C functions\n"
697 " -S\t\t\t\tCompile, but don't assemble and link\n"
698 " -T\t\t\t\tInclude source as comment\n"
699 " -V\t\t\t\tPrint the version number\n"
700 " -W name[,...]\t\t\tSuppress compiler warnings\n"
701 " -Wa options\t\t\tPass options to the assembler\n"
702 " -Wc options\t\t\tPass options to the compiler\n"
703 " -Wl options\t\t\tPass options to the linker\n"
706 " --add-source\t\t\tInclude source as comment\n"
707 " --asm-args options\t\tPass options to the assembler\n"
708 " --asm-define sym[=v]\t\tDefine an assembler symbol\n"
709 " --asm-include-dir dir\t\tSet an assembler include directory\n"
710 " --bin-include-dir dir\t\tSet an assembler binary include directory\n"
711 " --bss-label name\t\tDefine and export a BSS segment label\n"
712 " --bss-name seg\t\tSet the name of the BSS segment\n"
713 " --cc-args options\t\tPass options to the compiler\n"
714 " --cfg-path path\t\tSpecify a config file search path\n"
715 " --check-stack\t\t\tGenerate stack overflow checks\n"
716 " --code-label name\t\tDefine and export a CODE segment label\n"
717 " --code-name seg\t\tSet the name of the CODE segment\n"
718 " --codesize x\t\t\tAccept larger code by factor x\n"
719 " --config name\t\t\tUse linker config file\n"
720 " --cpu type\t\t\tSet CPU type\n"
721 " --create-dep name\t\tCreate a make dependency file\n"
722 " --create-full-dep name\tCreate a full make dependency file\n"
723 " --data-label name\t\tDefine and export a DATA segment label\n"
724 " --data-name seg\t\tSet the name of the DATA segment\n"
725 " --debug\t\t\tDebug mode\n"
726 " --debug-info\t\t\tAdd debug info\n"
727 " --feature name\t\tSet an emulation feature\n"
728 " --force-import sym\t\tForce an import of symbol `sym'\n"
729 " --forget-inc-paths\t\tForget include search paths (compiler)\n"
730 " --help\t\t\tHelp (this text)\n"
731 " --include-dir dir\t\tSet a compiler include directory path\n"
732 " --ld-args options\t\tPass options to the linker\n"
733 " --lib file\t\t\tLink this library\n"
734 " --lib-path path\t\tSpecify a library search path\n"
735 " --list-targets\t\tList all available targets\n"
736 " --listing name\t\tCreate an assembler listing file\n"
737 " --list-bytes n\t\tNumber of bytes per assembler listing line\n"
738 " --mapfile name\t\tCreate a map file\n"
739 " --memory-model model\t\tSet the memory model\n"
740 " --module\t\t\tLink as a module\n"
741 " --module-id id\t\tSpecify a module ID for the linker\n"
742 " --o65-model model\t\tOverride the o65 model\n"
743 " --obj file\t\t\tLink this object file\n"
744 " --obj-path path\t\tSpecify an object file search path\n"
745 " --register-space b\t\tSet space available for register variables\n"
746 " --register-vars\t\tEnable register variables\n"
747 " --rodata-name seg\t\tSet the name of the RODATA segment\n"
748 " --signed-chars\t\tDefault characters are signed\n"
749 " --standard std\t\tLanguage standard (c89, c99, cc65)\n"
750 " --start-addr addr\t\tSet the default start address\n"
751 " --static-locals\t\tMake local variables static\n"
752 " --target sys\t\t\tSet the target system\n"
753 " --version\t\t\tPrint the version number\n"
754 " --verbose\t\t\tVerbose mode\n"
755 " --zeropage-label name\t\tDefine and export a ZEROPAGE segment label\n"
756 " --zeropage-name seg\t\tSet the name of the ZEROPAGE segment\n",
762 static void OptAddSource (const char* Opt attribute ((unused)),
763 const char* Arg attribute ((unused)))
764 /* Strict source code as comments to the generated asm code */
766 CmdAddArg (&CC65, "-T");
771 static void OptAsmArgs (const char* Opt attribute ((unused)), const char* Arg)
772 /* Pass arguments to the assembler */
774 CmdAddArgList (&CA65, Arg);
779 static void OptAsmDefine (const char* Opt attribute ((unused)), const char* Arg)
780 /* Define an assembler symbol (assembler) */
782 CmdAddArg2 (&CA65, "-D", Arg);
787 static void OptAsmIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
788 /* Include directory (assembler) */
790 CmdAddArg2 (&CA65, "-I", Arg);
795 static void OptBinIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
796 /* Binary include directory (assembler) */
798 CmdAddArg2 (&CA65, "--bin-include-dir", Arg);
803 static void OptBssLabel (const char* Opt attribute ((unused)), const char* Arg)
804 /* Handle the --bss-label option */
806 CmdAddArg2 (&CO65, "--bss-label", Arg);
811 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
812 /* Handle the --bss-name option */
814 CmdAddArg2 (&CC65, "--bss-name", Arg);
815 CmdAddArg2 (&CO65, "--bss-name", Arg);
820 static void OptCCArgs (const char* Opt attribute ((unused)), const char* Arg)
821 /* Pass arguments to the compiler */
823 CmdAddArgList (&CC65, Arg);
828 static void OptCfgPath (const char* Opt attribute ((unused)), const char* Arg)
829 /* Config file search path (linker) */
831 CmdAddArg2 (&LD65, "--cfg-path", Arg);
836 static void OptCheckStack (const char* Opt attribute ((unused)),
837 const char* Arg attribute ((unused)))
838 /* Handle the --check-stack option */
840 CmdAddArg (&CC65, "--check-stack");
845 static void OptCodeLabel (const char* Opt attribute ((unused)), const char* Arg)
846 /* Handle the --code-label option */
848 CmdAddArg2 (&CO65, "--code-label", Arg);
853 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
854 /* Handle the --code-name option */
856 CmdAddArg2 (&CC65, "--code-name", Arg);
857 CmdAddArg2 (&CO65, "--code-name", Arg);
862 static void OptCodeSize (const char* Opt attribute ((unused)), const char* Arg)
863 /* Handle the --codesize option */
865 CmdAddArg2 (&CC65, "--codesize", Arg);
870 static void OptConfig (const char* Opt attribute ((unused)), const char* Arg)
871 /* Config file (linker) */
874 Error ("Cannot specify -C/--config twice");
881 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
882 /* Handle the --cpu option */
884 /* Add the cpu type to the assembler and compiler */
885 CmdAddArg2 (&CA65, "--cpu", Arg);
886 CmdAddArg2 (&CC65, "--cpu", Arg);
891 static void OptCreateDep (const char* Opt attribute ((unused)), const char* Arg)
892 /* Handle the --create-dep option */
894 /* Add the file name to the compiler */
895 CmdAddArg2 (&CC65, "--create-dep", Arg);
897 /* Remember the file name for the assembler */
903 static void OptCreateFullDep (const char* Opt attribute ((unused)), const char* Arg)
904 /* Handle the --create-full-dep option */
906 /* Add the file name to the compiler */
907 CmdAddArg2 (&CC65, "--create-full-dep", Arg);
909 /* Remember the file name for the assembler */
915 static void OptDataLabel (const char* Opt attribute ((unused)), const char* Arg)
916 /* Handle the --data-label option */
918 CmdAddArg2 (&CO65, "--data-label", Arg);
923 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
924 /* Handle the --data-name option */
926 CmdAddArg2 (&CC65, "--data-name", Arg);
927 CmdAddArg2 (&CO65, "--data-name", Arg);
932 static void OptDebug (const char* Opt attribute ((unused)),
933 const char* Arg attribute ((unused)))
934 /* Debug mode (compiler and cl65 utility) */
936 CmdAddArg (&CC65, "-d");
937 CmdAddArg (&CO65, "-d");
943 static void OptDebugInfo (const char* Opt attribute ((unused)),
944 const char* Arg attribute ((unused)))
945 /* Debug Info - add to compiler and assembler */
947 CmdAddArg (&CC65, "-g");
948 CmdAddArg (&CA65, "-g");
949 CmdAddArg (&CO65, "-g");
954 static void OptFeature (const char* Opt attribute ((unused)), const char* Arg)
955 /* Emulation features for the assembler */
957 CmdAddArg2 (&CA65, "--feature", Arg);
962 static void OptForceImport (const char* Opt attribute ((unused)), const char* Arg)
963 /* Emulation features for the assembler */
965 CmdAddArg2 (&LD65, "-u", Arg);
970 static void OptForgetIncPaths (const char* Opt attribute ((unused)),
971 const char* Arg attribute ((unused)))
972 /* Forget all currently defined include paths */
974 CmdAddArg (&CC65, "--forget-inc-paths");
979 static void OptHelp (const char* Opt attribute ((unused)),
980 const char* Arg attribute ((unused)))
981 /* Print help - cl65 */
989 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
990 /* Include directory (compiler) */
992 CmdAddArg2 (&CC65, "-I", Arg);
997 static void OptLdArgs (const char* Opt attribute ((unused)), const char* Arg)
998 /* Pass arguments to the linker */
1000 CmdAddArgList (&LD65, Arg);
1005 static void OptLib (const char* Opt attribute ((unused)), const char* Arg)
1006 /* Library file follows (linker) */
1008 CmdAddArg2 (&LD65, "--lib", Arg);
1013 static void OptLibPath (const char* Opt attribute ((unused)), const char* Arg)
1014 /* Library search path (linker) */
1016 CmdAddArg2 (&LD65, "--lib-path", Arg);
1021 static void OptListBytes (const char* Opt attribute ((unused)), const char* Arg)
1022 /* Set the maximum number of bytes per asm listing line */
1024 CmdAddArg2 (&CA65, "--list-bytes", Arg);
1029 static void OptListing (const char* Opt attribute ((unused)), const char* Arg)
1030 /* Create an assembler listing */
1032 CmdAddArg2 (&CA65, "-l", Arg);
1037 static void OptListTargets (const char* Opt attribute ((unused)),
1038 const char* Arg attribute ((unused)))
1039 /* List all targets */
1043 /* List the targets */
1044 for (T = TGT_NONE; T < TGT_COUNT; ++T) {
1045 printf ("%s\n", GetTargetName (T));
1049 exit (EXIT_SUCCESS);
1054 static void OptMapFile (const char* Opt attribute ((unused)), const char* Arg)
1055 /* Create a map file */
1057 /* Create a map file (linker) */
1058 CmdAddArg2 (&LD65, "-m", Arg);
1063 static void OptMemoryModel (const char* Opt attribute ((unused)), const char* Arg)
1064 /* Set the memory model */
1066 mmodel_t MemoryModel = FindMemoryModel (Arg);
1067 if (MemoryModel == MMODEL_UNKNOWN) {
1068 Error ("Unknown memory model: %s", Arg);
1069 } else if (MemoryModel == MMODEL_HUGE) {
1070 Error ("Unsupported memory model: %s", Arg);
1072 CmdAddArg2 (&CA65, "-mm", Arg);
1073 CmdAddArg2 (&CC65, "-mm", Arg);
1079 static void OptModule (const char* Opt attribute ((unused)),
1080 const char* Arg attribute ((unused)))
1081 /* Link as a module */
1088 static void OptModuleId (const char* Opt attribute ((unused)), const char* Arg)
1089 /* Specify a module if for the linker */
1091 /* Pass it straight to the linker */
1092 CmdAddArg2 (&LD65, "--module-id", Arg);
1097 static void OptO65Model (const char* Opt attribute ((unused)), const char* Arg)
1098 /* Handle the --o65-model option */
1100 CmdAddArg2 (&CO65, "-m", Arg);
1105 static void OptObj (const char* Opt attribute ((unused)), const char* Arg)
1106 /* Object file follows (linker) */
1108 CmdAddArg2 (&LD65, "--obj", Arg);
1113 static void OptObjPath (const char* Opt attribute ((unused)), const char* Arg)
1114 /* Object file search path (linker) */
1116 CmdAddArg2 (&LD65, "--obj-path", Arg);
1121 static void OptRegisterSpace (const char* Opt attribute ((unused)), const char* Arg)
1122 /* Handle the --register-space option */
1124 CmdAddArg2 (&CC65, "--register-space", Arg);
1129 static void OptRegisterVars (const char* Opt attribute ((unused)),
1130 const char* Arg attribute ((unused)))
1131 /* Handle the --register-vars option */
1133 CmdAddArg (&CC65, "-r");
1138 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
1139 /* Handle the --rodata-name option */
1141 CmdAddArg2 (&CC65, "--rodata-name", Arg);
1146 static void OptSignedChars (const char* Opt attribute ((unused)),
1147 const char* Arg attribute ((unused)))
1148 /* Make default characters signed */
1150 CmdAddArg (&CC65, "-j");
1155 static void OptStandard (const char* Opt attribute ((unused)), const char* Arg)
1156 /* Set the language standard */
1158 CmdAddArg2 (&CC65, "--standard", Arg);
1163 static void OptStartAddr (const char* Opt attribute ((unused)), const char* Arg)
1164 /* Set the default start address */
1166 CmdAddArg2 (&LD65, "-S", Arg);
1171 static void OptStaticLocals (const char* Opt attribute ((unused)),
1172 const char* Arg attribute ((unused)))
1173 /* Place local variables in static storage */
1175 CmdAddArg (&CC65, "-Cl");
1180 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
1181 /* Set the target system */
1183 Target = FindTarget (Arg);
1184 if (Target == TGT_UNKNOWN) {
1185 Error ("No such target system: `%s'", Arg);
1186 } else if (Target == TGT_MODULE) {
1187 Error ("Cannot use `module' as target, use --module instead");
1193 static void OptVerbose (const char* Opt attribute ((unused)),
1194 const char* Arg attribute ((unused)))
1195 /* Verbose mode (compiler, assembler, linker) */
1197 CmdAddArg (&CC65, "-v");
1198 CmdAddArg (&CA65, "-v");
1199 CmdAddArg (&CO65, "-v");
1200 CmdAddArg (&LD65, "-v");
1205 static void OptVersion (const char* Opt attribute ((unused)),
1206 const char* Arg attribute ((unused)))
1207 /* Print version number */
1210 "cl65 V%s - (C) Copyright 1998-2011 Ullrich von Bassewitz\n",
1211 GetVersionAsString ());
1216 static void OptZeropageLabel (const char* Opt attribute ((unused)), const char* Arg)
1217 /* Handle the --zeropage-label option */
1219 CmdAddArg2 (&CO65, "--zeropage-label", Arg);
1224 static void OptZeropageName (const char* Opt attribute ((unused)), const char* Arg)
1225 /* Handle the --zeropage-name option */
1227 CmdAddArg2 (&CO65, "--zeropage-name", Arg);
1232 int main (int argc, char* argv [])
1233 /* Utility main program */
1235 /* Program long options */
1236 static const LongOpt OptTab[] = {
1237 { "--add-source", 0, OptAddSource },
1238 { "--asm-args", 1, OptAsmArgs },
1239 { "--asm-define", 1, OptAsmDefine },
1240 { "--asm-include-dir", 1, OptAsmIncludeDir },
1241 { "--bin-include-dir", 1, OptBinIncludeDir },
1242 { "--bss-label", 1, OptBssLabel },
1243 { "--bss-name", 1, OptBssName },
1244 { "--cc-args", 1, OptCCArgs },
1245 { "--cfg-path", 1, OptCfgPath },
1246 { "--check-stack", 0, OptCheckStack },
1247 { "--code-label", 1, OptCodeLabel },
1248 { "--code-name", 1, OptCodeName },
1249 { "--codesize", 1, OptCodeSize },
1250 { "--config", 1, OptConfig },
1251 { "--cpu", 1, OptCPU },
1252 { "--create-dep", 1, OptCreateDep },
1253 { "--create-full-dep", 1, OptCreateFullDep },
1254 { "--data-label", 1, OptDataLabel },
1255 { "--data-name", 1, OptDataName },
1256 { "--debug", 0, OptDebug },
1257 { "--debug-info", 0, OptDebugInfo },
1258 { "--feature", 1, OptFeature },
1259 { "--force-import", 1, OptForceImport },
1260 { "--forget-inc-paths", 0, OptForgetIncPaths },
1261 { "--help", 0, OptHelp },
1262 { "--include-dir", 1, OptIncludeDir },
1263 { "--ld-args", 1, OptLdArgs },
1264 { "--lib", 1, OptLib },
1265 { "--lib-path", 1, OptLibPath },
1266 { "--list-targets", 0, OptListTargets },
1267 { "--listing", 1, OptListing },
1268 { "--list-bytes", 1, OptListBytes },
1269 { "--mapfile", 1, OptMapFile },
1270 { "--memory-model", 1, OptMemoryModel },
1271 { "--module", 0, OptModule },
1272 { "--module-id", 1, OptModuleId },
1273 { "--o65-model", 1, OptO65Model },
1274 { "--obj", 1, OptObj },
1275 { "--obj-path", 1, OptObjPath },
1276 { "--register-space", 1, OptRegisterSpace },
1277 { "--register-vars", 0, OptRegisterVars },
1278 { "--rodata-name", 1, OptRodataName },
1279 { "--signed-chars", 0, OptSignedChars },
1280 { "--standard", 1, OptStandard },
1281 { "--start-addr", 1, OptStartAddr },
1282 { "--static-locals", 0, OptStaticLocals },
1283 { "--target", 1, OptTarget },
1284 { "--verbose", 0, OptVerbose },
1285 { "--version", 0, OptVersion },
1286 { "--zeropage-label", 1, OptZeropageLabel },
1287 { "--zeropage-name", 1, OptZeropageName },
1292 /* Initialize the cmdline module */
1293 InitCmdLine (&argc, &argv, "cl65");
1295 /* Initialize the command descriptors */
1296 CmdInit (&CC65, "cc65");
1297 CmdInit (&CA65, "ca65");
1298 CmdInit (&CO65, "co65");
1299 CmdInit (&LD65, "ld65");
1300 CmdInit (&GRC, "grc65");
1302 /* Our default target is the C64 instead of "none" */
1305 /* Check the parameters */
1307 while (I < ArgCount) {
1309 /* Get the argument */
1310 const char* Arg = ArgVec[I];
1312 /* Check for an option */
1313 if (Arg [0] == '-') {
1318 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
1322 if (Arg[2] == 'l' && Arg[3] == '\0') {
1323 /* Make local variables static */
1324 OptStaticLocals (Arg, 0);
1326 /* Specify linker config file */
1327 OptConfig (Arg, GetArg (&I, 2));
1332 /* Define a preprocessor symbol (compiler) */
1333 CmdAddArg2 (&CC65, "-D", GetArg (&I, 2));
1337 /* Include directory (compiler) */
1338 OptIncludeDir (Arg, GetArg (&I, 2));
1342 if (Arg[2] == 'n' && Arg[3] == '\0') {
1343 /* VICE label file (linker) */
1344 CmdAddArg2 (&LD65, "-Ln", GetArg (&I, 3));
1346 /* Library search path (linker) */
1347 OptLibPath (Arg, GetArg (&I, 2));
1352 /* Optimize code (compiler, also covers -Oi and others) */
1353 CmdAddArg (&CC65, Arg);
1357 /* Dont assemble and link the created files */
1363 /* Include source as comment (compiler) */
1364 OptAddSource (Arg, 0);
1368 /* Print version number */
1369 OptVersion (Arg, 0);
1373 if (Arg[2] == 'a' && Arg[3] == '\0') {
1374 /* -Wa: Pass options to assembler */
1375 OptAsmArgs (Arg, GetArg (&I, 3));
1376 } else if (Arg[2] == 'c' && Arg[3] == '\0') {
1377 /* -Wc: Pass options to compiler */
1378 OptCCArgs (Arg, GetArg (&I, 3));
1379 } else if (Arg[2] == 'l' && Arg[3] == '\0') {
1380 /* -Wl: Pass options to linker */
1381 OptLdArgs (Arg, GetArg (&I, 3));
1383 /* Anything else: Suppress warnings (compiler) */
1384 CmdAddArg2 (&CC65, "-W", GetArg (&I, 2));
1389 /* Don't link the resulting files */
1394 /* Debug mode (compiler) */
1399 /* Debugging - add to compiler and assembler */
1400 OptDebugInfo (Arg, 0);
1405 /* Print help - cl65 */
1410 /* Default characters are signed */
1411 OptSignedChars (Arg, 0);
1415 /* Create an assembler listing */
1416 OptListing (Arg, GetArg (&I, 2));
1420 /* Create a map file (linker) */
1421 OptMapFile (Arg, GetArg (&I, 2));
1425 /* Name the output file */
1426 OutputName = GetArg (&I, 2);
1430 /* Enable register variables */
1431 OptRegisterVars (Arg, 0);
1435 /* Set target system - compiler, assembler and linker */
1436 OptTarget (Arg, GetArg (&I, 2));
1440 /* Force an import (linker) */
1441 OptForceImport (Arg, GetArg (&I, 2));
1445 if (Arg [2] == 'm') {
1446 /* Verbose map file (linker) */
1447 CmdAddArg (&LD65, "-vm");
1449 /* Verbose mode (compiler, assembler, linker) */
1450 OptVerbose (Arg, 0);
1455 UnknownOption (Arg);
1459 /* Remember the first file name */
1460 if (FirstInput == 0) {
1464 /* Determine the file type by the extension */
1465 switch (GetFileType (Arg)) {
1468 /* Compile the file */
1473 /* Assemble the file */
1481 /* Add to the linker files */
1482 CmdAddFile (&LD65, Arg);
1486 /* Add to the resource compiler files */
1491 /* Add the the object file converter files */
1496 Error ("Don't know what to do with `%s'", Arg);
1506 /* Check if we had any input files */
1507 if (FirstInput == 0) {
1508 Warning ("No input files");
1511 /* Link the given files if requested and if we have any */
1512 if (DoLink && LD65.FileCount > 0) {
1516 /* Return an apropriate exit code */
1517 return EXIT_SUCCESS;