1 /*****************************************************************************/
5 /* Main module for the cl65 compile-and-link utility */
9 /* (C) 1999-2013, 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
45 /* GCC strictly follows http://c-faq.com/ansi/constmismatch.html and issues an
46 ** 'incompatible pointer type' warning - that can't be suppressed via #pragma.
47 ** The spawnvp() prototype of MinGW (http://www.mingw.org/) differs from the
48 ** one of MinGW-w64 (http://mingw-w64.sourceforge.net/) regarding constness.
49 ** So there's no alternative to actually distinguish these environments :-(
51 #define SPAWN_ARGV_CONST_CAST
52 #if defined(__MINGW32__)
54 # if !defined(__MINGW64_VERSION_MAJOR)
55 # undef SPAWN_ARGV_CONST_CAST
56 # define SPAWN_ARGV_CONST_CAST (const char* const *)
66 #if defined(HAVE_SPAWN)
76 #include "searchpath.h"
88 /*****************************************************************************/
90 /*****************************************************************************/
94 /* Struct that describes a command */
95 typedef struct CmdDesc CmdDesc;
97 char* Name; /* The command name */
99 unsigned ArgCount; /* Count of arguments */
100 unsigned ArgMax; /* Maximum count of arguments */
101 char** Args; /* The arguments */
103 unsigned FileCount; /* Count of files to translate */
104 unsigned FileMax; /* Maximum count of files */
105 char** Files; /* The files */
108 /* Command descriptors for the different programs */
109 static CmdDesc CC65 = { 0, 0, 0, 0, 0, 0, 0 };
110 static CmdDesc CA65 = { 0, 0, 0, 0, 0, 0, 0 };
111 static CmdDesc CO65 = { 0, 0, 0, 0, 0, 0, 0 };
112 static CmdDesc LD65 = { 0, 0, 0, 0, 0, 0, 0 };
113 static CmdDesc GRC = { 0, 0, 0, 0, 0, 0, 0 };
115 /* Variables controlling the steps we're doing */
116 static int DoLink = 1;
117 static int DoAssemble = 1;
119 /* The name of the output file, NULL if none given */
120 static const char* OutputName = 0;
122 /* The name of the linker configuration file if given */
123 static const char* LinkerConfig = 0;
125 /* The name of the first input file. This will be used to construct the
126 ** executable file name if no explicit name is given.
128 static const char* FirstInput = 0;
130 /* The names of the files for dependency generation */
131 static const char* DepName = 0;
132 static const char* FullDepName = 0;
134 /* Remember if we should link a module */
135 static int Module = 0;
137 /* Extension used for a module */
138 #define MODULE_EXT ".o65"
140 /* Name of the target specific runtime library */
141 static char* TargetLib = 0;
145 /*****************************************************************************/
146 /* Include the system specific spawn function */
147 /*****************************************************************************/
151 #if defined(NEED_SPAWN)
153 # include "spawn-amiga.inc"
155 # include "spawn-unix.inc"
161 /*****************************************************************************/
162 /* Credential functions */
163 /*****************************************************************************/
167 static void DisableAssembling (void)
174 static void DisableLinking (void)
181 static void DisableAssemblingAndLinking (void)
189 /*****************************************************************************/
190 /* Command structure handling */
191 /*****************************************************************************/
195 static void CmdExpand (CmdDesc* Cmd)
196 /* Expand the argument vector */
198 unsigned NewMax = Cmd->ArgMax + 10;
199 char** NewArgs = xmalloc (NewMax * sizeof (char*));
200 memcpy (NewArgs, Cmd->Args, Cmd->ArgMax * sizeof (char*));
203 Cmd->ArgMax = NewMax;
208 static void CmdAddArg (CmdDesc* Cmd, const char* Arg)
209 /* Add a new argument to the command */
211 /* Expand the argument vector if needed */
212 if (Cmd->ArgCount >= Cmd->ArgMax) {
216 /* Add a copy of the new argument, allow a NULL pointer */
218 Cmd->Args[Cmd->ArgCount++] = xstrdup (Arg);
220 Cmd->Args[Cmd->ArgCount++] = 0;
226 static void CmdAddArg2 (CmdDesc* Cmd, const char* Arg1, const char* Arg2)
227 /* Add a new argument pair to the command */
229 CmdAddArg (Cmd, Arg1);
230 CmdAddArg (Cmd, Arg2);
235 static void CmdAddArgList (CmdDesc* Cmd, const char* ArgList)
236 /* Add a list of arguments separated by commas */
238 const char* Arg = ArgList;
242 if (*P == '\0' || *P == ',') {
244 /* End of argument, add it */
245 unsigned Len = P - Arg;
247 /* Expand the argument vector if needed */
248 if (Cmd->ArgCount >= Cmd->ArgMax) {
252 /* Add the new argument */
253 Cmd->Args[Cmd->ArgCount] = memcpy (xmalloc (Len + 1), Arg, Len);
254 Cmd->Args[Cmd->ArgCount][Len] = '\0';
257 /* If the argument was terminated by a comma, skip it, otherwise
261 /* Start over at next char */
267 /* Skip other chars */
276 static void CmdDelArgs (CmdDesc* Cmd, unsigned LastValid)
277 /* Remove all arguments with an index greater than LastValid */
279 while (Cmd->ArgCount > LastValid) {
281 xfree (Cmd->Args [Cmd->ArgCount]);
282 Cmd->Args [Cmd->ArgCount] = 0;
288 static void CmdAddFile (CmdDesc* Cmd, const char* File)
289 /* Add a new file to the command */
291 /* Expand the file vector if needed */
292 if (Cmd->FileCount == Cmd->FileMax) {
293 unsigned NewMax = Cmd->FileMax + 10;
294 char** NewFiles = xmalloc (NewMax * sizeof (char*));
295 memcpy (NewFiles, Cmd->Files, Cmd->FileMax * sizeof (char*));
297 Cmd->Files = NewFiles;
298 Cmd->FileMax = NewMax;
301 /* If the file name is not NULL (which is legal and is used to terminate
302 ** the file list), check if the file name does already exist in the file
303 ** list and print a warning if so. Regardless of the search result, add
308 for (I = 0; I < Cmd->FileCount; ++I) {
309 if (strcmp (Cmd->Files[I], File) == 0) {
311 Warning ("Duplicate file in argument list: `%s'", File);
312 /* No need to search further */
318 Cmd->Files [Cmd->FileCount++] = xstrdup (File);
320 /* Add a NULL pointer */
321 Cmd->Files [Cmd->FileCount++] = 0;
327 static void CmdInit (CmdDesc* Cmd, const char* Path, const char* Name)
328 /* Initialize the command using the given path and name of the executable */
332 FullName = (char*) xmalloc (strlen (Path) + strlen (Name) + 1);
333 strcpy (FullName, Path);
334 strcat (FullName, Name);
336 /* Remember the command */
337 Cmd->Name = xstrdup (FullName);
339 /* Use the command name as first argument */
340 CmdAddArg (Cmd, FullName);
347 static void CmdSetOutput (CmdDesc* Cmd, const char* File)
348 /* Set the output file in a command desc */
350 CmdAddArg2 (Cmd, "-o", File);
355 static void CmdSetTarget (CmdDesc* Cmd, target_t Target)
356 /* Set the output file in a command desc */
358 CmdAddArg2 (Cmd, "-t", GetTargetName (Target));
363 static void CmdPrint (CmdDesc* Cmd, FILE* F)
364 /* Output the command line encoded in the command desc */
367 for (I = 0; I < Cmd->ArgCount && Cmd->Args[I] != 0; ++I) {
368 fprintf (F, "%s ", Cmd->Args[I]);
374 /*****************************************************************************/
375 /* Target handling */
376 /*****************************************************************************/
380 static void SetTargetFiles (void)
381 /* Set the target system files */
383 /* Determine the names of the target specific library file */
384 if (Target != TGT_NONE) {
386 /* Get a pointer to the system name and its length */
387 const char* TargetName = GetTargetName (Target);
388 unsigned TargetNameLen = strlen (TargetName);
390 /* Set the library file */
391 TargetLib = xmalloc (TargetNameLen + 4 + 1);
392 memcpy (TargetLib, TargetName, TargetNameLen);
393 strcpy (TargetLib + TargetNameLen, ".lib");
400 /*****************************************************************************/
402 /*****************************************************************************/
406 static void ExecProgram (CmdDesc* Cmd)
407 /* Execute a subprocess with the given name/parameters. Exit on errors. */
411 /* If in debug mode, output the command line we will execute */
413 printf ("Executing: ");
414 CmdPrint (Cmd, stdout);
418 /* Call the program */
419 Status = spawnvp (P_WAIT, Cmd->Name, SPAWN_ARGV_CONST_CAST Cmd->Args);
421 /* Check the result code */
423 /* Error executing the program */
424 Error ("Cannot execute `%s': %s", Cmd->Name, strerror (errno));
425 } else if (Status != 0) {
426 /* Called program had an error */
433 static void Link (void)
434 /* Link the resulting executable */
438 /* Since linking is always the final step, if we have an output file name
439 ** given, set it here. If we don't have an explicit output name given,
440 ** try to build one from the name of the first input file.
444 CmdSetOutput (&LD65, OutputName);
446 } else if (FirstInput && FindExt (FirstInput)) { /* Only if ext present! */
448 const char* Extension = Module? MODULE_EXT : "";
449 char* Output = MakeFilename (FirstInput, Extension);
450 CmdSetOutput (&LD65, Output);
455 /* If we have a linker config file given, add it to the command line.
456 ** Otherwise pass the target to the linker if we have one.
460 Error ("Cannot use -C and --module together");
462 CmdAddArg2 (&LD65, "-C", LinkerConfig);
464 CmdSetTarget (&LD65, TGT_MODULE);
466 CmdSetTarget (&LD65, Target);
469 /* Determine which target libraries are needed */
472 /* Add all object files as parameters */
473 for (I = 0; I < LD65.FileCount; ++I) {
474 CmdAddArg (&LD65, LD65.Files [I]);
477 /* Add the system runtime library */
479 CmdAddArg (&LD65, TargetLib);
482 /* Terminate the argument list with a NULL pointer */
483 CmdAddArg (&LD65, 0);
485 /* Call the linker */
491 static void AssembleFile (const char* File, unsigned ArgCount)
492 /* Common routine to assemble a file. Will be called by Assemble() and
493 ** AssembleIntermediate(). Adds options common for both routines and
494 ** assembles the file. Will remove excess arguments after assembly.
497 /* Set the target system */
498 CmdSetTarget (&CA65, Target);
500 /* Check if this is the last processing step */
502 /* We're linking later. Add the output file of the assembly
503 ** the the file list of the linker. The name of the output
504 ** file is that of the input file with ".s" replaced by ".o".
506 char* ObjName = MakeFilename (File, ".o");
507 CmdAddFile (&LD65, ObjName);
510 /* This is the final step. If an output name is given, set it */
512 CmdSetOutput (&CA65, OutputName);
516 /* Add the file as argument for the assembler */
517 CmdAddArg (&CA65, File);
519 /* Add a NULL pointer to terminate the argument list */
520 CmdAddArg (&CA65, 0);
522 /* Run the assembler */
525 /* Remove the excess arguments */
526 CmdDelArgs (&CA65, ArgCount);
531 static void AssembleIntermediate (const char* SourceFile)
532 /* Assemble an intermediate file which was generated by a previous processing
533 ** step with SourceFile as input. The -dep options won't be added and
534 ** the intermediate assembler file is removed after assembly.
537 /* Generate the name of the assembler output file from the source file
538 ** name. It's the same name with the extension replaced by ".s"
540 char* AsmName = MakeFilename (SourceFile, ".s");
542 /* Assemble the intermediate assembler file */
543 AssembleFile (AsmName, CA65.ArgCount);
545 /* Remove the input file */
546 if (remove (AsmName) < 0) {
547 Warning ("Cannot remove temporary file `%s': %s",
548 AsmName, strerror (errno));
551 /* Free the assembler file name which was allocated from the heap */
557 static void Assemble (const char* File)
558 /* Assemble the given file */
560 /* Remember the current assembler argument count */
561 unsigned ArgCount = CA65.ArgCount;
563 /* We aren't assembling an intermediate file, but one requested by the
564 ** user. So add a few options here if they were given on the command
567 if (DepName && *DepName) {
568 CmdAddArg2 (&CA65, "--create-dep", DepName);
570 if (FullDepName && *FullDepName) {
571 CmdAddArg2 (&CA65, "--create-full-dep", FullDepName);
574 /* Use the common routine */
575 AssembleFile (File, ArgCount);
580 static void Compile (const char* File)
581 /* Compile the given file */
583 /* Remember the current compiler argument count */
584 unsigned ArgCount = CC65.ArgCount;
586 /* Set the target system */
587 CmdSetTarget (&CC65, Target);
589 /* Check if this is the final step */
591 /* We will assemble this file later. If a dependency file is to be
592 ** generated, set the dependency target to be the final object file,
593 ** not the intermediate assembler file. But beware: There may be an
594 ** output name specified for the assembler.
596 if (DepName || FullDepName) {
597 /* Was an output name for the assembler specified? */
598 if (!DoLink && OutputName) {
599 /* Use this name as the dependency target */
600 CmdAddArg2 (&CC65, "--dep-target", OutputName);
602 /* Use the object file name as the dependency target */
603 char* ObjName = MakeFilename (File, ".o");
604 CmdAddArg2 (&CC65, "--dep-target", ObjName);
609 /* If we won't assemble, this is the final step. In this case, set
610 ** the output name if it was given.
613 CmdSetOutput (&CC65, OutputName);
617 /* Add the file as argument for the compiler */
618 CmdAddArg (&CC65, File);
620 /* Add a NULL pointer to terminate the argument list */
621 CmdAddArg (&CC65, 0);
623 /* Run the compiler */
626 /* Remove the excess arguments */
627 CmdDelArgs (&CC65, ArgCount);
629 /* If this is not the final step, assemble the generated file, then
633 /* Assemble the intermediate file and remove it */
634 AssembleIntermediate (File);
640 static void CompileRes (const char* File)
641 /* Compile the given geos resource file */
643 /* Remember the current assembler argument count */
644 unsigned ArgCount = GRC.ArgCount;
646 /* Resource files need an geos-apple or geos-cbm target but this
647 ** is checked within grc65.
649 CmdSetTarget (&GRC, Target);
651 /* Add the file as argument for the resource compiler */
652 CmdAddArg (&GRC, File);
654 /* Add a NULL pointer to terminate the argument list */
657 /* Run the compiler */
660 /* Remove the excess arguments */
661 CmdDelArgs (&GRC, ArgCount);
663 /* If this is not the final step, assemble the generated file, then
667 /* Assemble the intermediate file and remove it */
668 AssembleIntermediate (File);
674 static void ConvertO65 (const char* File)
675 /* Convert an o65 object file into an assembler file */
677 /* Remember the current converter argument count */
678 unsigned ArgCount = CO65.ArgCount;
680 /* If we won't assemble, this is the final step. In this case, set the
683 if (!DoAssemble && OutputName) {
684 CmdSetOutput (&CO65, OutputName);
687 /* Add the file as argument for the object file converter */
688 CmdAddArg (&CO65, File);
690 /* Add a NULL pointer to terminate the argument list */
691 CmdAddArg (&CO65, 0);
693 /* Run the converter */
696 /* Remove the excess arguments */
697 CmdDelArgs (&CO65, ArgCount);
699 /* If this is not the final step, assemble the generated file, then
703 /* Assemble the intermediate file and remove it */
704 AssembleIntermediate (File);
710 /*****************************************************************************/
712 /*****************************************************************************/
716 static void Usage (void)
717 /* Print usage information and exit */
719 printf ("Usage: %s [options] file [...]\n"
721 " -c\t\t\t\tCompile and assemble, but don't link\n"
722 " -d\t\t\t\tDebug mode\n"
723 " -g\t\t\t\tAdd debug info\n"
724 " -h\t\t\t\tHelp (this text)\n"
725 " -l name\t\t\tCreate an assembler listing file\n"
726 " -m name\t\t\tCreate a map file\n"
727 " -mm model\t\t\tSet the memory model\n"
728 " -o name\t\t\tName the output file\n"
729 " -r\t\t\t\tEnable register variables\n"
730 " -t sys\t\t\tSet the target system\n"
731 " -u sym\t\t\tForce an import of symbol `sym'\n"
732 " -v\t\t\t\tVerbose mode\n"
733 " -vm\t\t\t\tVerbose map file\n"
734 " -C name\t\t\tUse linker config file\n"
735 " -Cl\t\t\t\tMake local variables static\n"
736 " -D sym[=defn]\t\t\tDefine a preprocessor symbol\n"
737 " -E\t\t\t\tStop after the preprocessing stage\n"
738 " -I dir\t\t\tSet a compiler include directory path\n"
739 " -L path\t\t\tSpecify a library search path\n"
740 " -Ln name\t\t\tCreate a VICE label file\n"
741 " -O\t\t\t\tOptimize code\n"
742 " -Oi\t\t\t\tOptimize code, inline runtime functions\n"
743 " -Or\t\t\t\tOptimize code, honour the register keyword\n"
744 " -Os\t\t\t\tOptimize code, inline known C functions\n"
745 " -S\t\t\t\tCompile, but don't assemble and link\n"
746 " -T\t\t\t\tInclude source as comment\n"
747 " -V\t\t\t\tPrint the version number\n"
748 " -W name[,...]\t\t\tSuppress compiler warnings\n"
749 " -Wa options\t\t\tPass options to the assembler\n"
750 " -Wc options\t\t\tPass options to the compiler\n"
751 " -Wl options\t\t\tPass options to the linker\n"
754 " --add-source\t\t\tInclude source as comment\n"
755 " --asm-args options\t\tPass options to the assembler\n"
756 " --asm-define sym[=v]\t\tDefine an assembler symbol\n"
757 " --asm-include-dir dir\t\tSet an assembler include directory\n"
758 " --bin-include-dir dir\t\tSet an assembler binary include directory\n"
759 " --bss-label name\t\tDefine and export a BSS segment label\n"
760 " --bss-name seg\t\tSet the name of the BSS segment\n"
761 " --cc-args options\t\tPass options to the compiler\n"
762 " --cfg-path path\t\tSpecify a config file search path\n"
763 " --check-stack\t\t\tGenerate stack overflow checks\n"
764 " --code-label name\t\tDefine and export a CODE segment label\n"
765 " --code-name seg\t\tSet the name of the CODE segment\n"
766 " --codesize x\t\t\tAccept larger code by factor x\n"
767 " --config name\t\t\tUse linker config file\n"
768 " --cpu type\t\t\tSet CPU type\n"
769 " --create-dep name\t\tCreate a make dependency file\n"
770 " --create-full-dep name\tCreate a full make dependency file\n"
771 " --data-label name\t\tDefine and export a DATA segment label\n"
772 " --data-name seg\t\tSet the name of the DATA segment\n"
773 " --debug\t\t\tDebug mode\n"
774 " --debug-info\t\t\tAdd debug info\n"
775 " --feature name\t\tSet an emulation feature\n"
776 " --force-import sym\t\tForce an import of symbol `sym'\n"
777 " --help\t\t\tHelp (this text)\n"
778 " --include-dir dir\t\tSet a compiler include directory path\n"
779 " --ld-args options\t\tPass options to the linker\n"
780 " --lib file\t\t\tLink this library\n"
781 " --lib-path path\t\tSpecify a library search path\n"
782 " --list-targets\t\tList all available targets\n"
783 " --listing name\t\tCreate an assembler listing file\n"
784 " --list-bytes n\t\tNumber of bytes per assembler listing line\n"
785 " --mapfile name\t\tCreate a map file\n"
786 " --memory-model model\t\tSet the memory model\n"
787 " --module\t\t\tLink as a module\n"
788 " --module-id id\t\tSpecify a module ID for the linker\n"
789 " --o65-model model\t\tOverride the o65 model\n"
790 " --obj file\t\t\tLink this object file\n"
791 " --obj-path path\t\tSpecify an object file search path\n"
792 " --print-target-path\t\tPrint the target file path\n"
793 " --register-space b\t\tSet space available for register variables\n"
794 " --register-vars\t\tEnable register variables\n"
795 " --rodata-name seg\t\tSet the name of the RODATA segment\n"
796 " --signed-chars\t\tDefault characters are signed\n"
797 " --standard std\t\tLanguage standard (c89, c99, cc65)\n"
798 " --start-addr addr\t\tSet the default start address\n"
799 " --static-locals\t\tMake local variables static\n"
800 " --target sys\t\t\tSet the target system\n"
801 " --version\t\t\tPrint the version number\n"
802 " --verbose\t\t\tVerbose mode\n"
803 " --zeropage-label name\t\tDefine and export a ZEROPAGE segment label\n"
804 " --zeropage-name seg\t\tSet the name of the ZEROPAGE segment\n",
810 static void OptAddSource (const char* Opt attribute ((unused)),
811 const char* Arg attribute ((unused)))
812 /* Strict source code as comments to the generated asm code */
814 CmdAddArg (&CC65, "-T");
819 static void OptAsmArgs (const char* Opt attribute ((unused)), const char* Arg)
820 /* Pass arguments to the assembler */
822 CmdAddArgList (&CA65, Arg);
827 static void OptAsmDefine (const char* Opt attribute ((unused)), const char* Arg)
828 /* Define an assembler symbol (assembler) */
830 CmdAddArg2 (&CA65, "-D", Arg);
835 static void OptAsmIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
836 /* Include directory (assembler) */
838 CmdAddArg2 (&CA65, "-I", Arg);
843 static void OptBinIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
844 /* Binary include directory (assembler) */
846 CmdAddArg2 (&CA65, "--bin-include-dir", Arg);
851 static void OptBssLabel (const char* Opt attribute ((unused)), const char* Arg)
852 /* Handle the --bss-label option */
854 CmdAddArg2 (&CO65, "--bss-label", Arg);
859 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
860 /* Handle the --bss-name option */
862 CmdAddArg2 (&CC65, "--bss-name", Arg);
863 CmdAddArg2 (&CO65, "--bss-name", Arg);
868 static void OptCCArgs (const char* Opt attribute ((unused)), const char* Arg)
869 /* Pass arguments to the compiler */
871 CmdAddArgList (&CC65, Arg);
876 static void OptCfgPath (const char* Opt attribute ((unused)), const char* Arg)
877 /* Config file search path (linker) */
879 CmdAddArg2 (&LD65, "--cfg-path", Arg);
884 static void OptCheckStack (const char* Opt attribute ((unused)),
885 const char* Arg attribute ((unused)))
886 /* Handle the --check-stack option */
888 CmdAddArg (&CC65, "--check-stack");
893 static void OptCodeLabel (const char* Opt attribute ((unused)), const char* Arg)
894 /* Handle the --code-label option */
896 CmdAddArg2 (&CO65, "--code-label", Arg);
901 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
902 /* Handle the --code-name option */
904 CmdAddArg2 (&CC65, "--code-name", Arg);
905 CmdAddArg2 (&CO65, "--code-name", Arg);
910 static void OptCodeSize (const char* Opt attribute ((unused)), const char* Arg)
911 /* Handle the --codesize option */
913 CmdAddArg2 (&CC65, "--codesize", Arg);
918 static void OptConfig (const char* Opt attribute ((unused)), const char* Arg)
919 /* Config file (linker) */
922 Error ("Cannot specify -C/--config twice");
929 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
930 /* Handle the --cpu option */
932 /* Add the cpu type to the assembler and compiler */
933 CmdAddArg2 (&CA65, "--cpu", Arg);
934 CmdAddArg2 (&CC65, "--cpu", Arg);
939 static void OptCreateDep (const char* Opt attribute ((unused)), const char* Arg)
940 /* Handle the --create-dep option */
942 /* Add the file name to the compiler */
943 CmdAddArg2 (&CC65, "--create-dep", Arg);
945 /* Remember the file name for the assembler */
951 static void OptCreateFullDep (const char* Opt attribute ((unused)), const char* Arg)
952 /* Handle the --create-full-dep option */
954 /* Add the file name to the compiler */
955 CmdAddArg2 (&CC65, "--create-full-dep", Arg);
957 /* Remember the file name for the assembler */
963 static void OptDataLabel (const char* Opt attribute ((unused)), const char* Arg)
964 /* Handle the --data-label option */
966 CmdAddArg2 (&CO65, "--data-label", Arg);
971 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
972 /* Handle the --data-name option */
974 CmdAddArg2 (&CC65, "--data-name", Arg);
975 CmdAddArg2 (&CO65, "--data-name", Arg);
980 static void OptDebug (const char* Opt attribute ((unused)),
981 const char* Arg attribute ((unused)))
982 /* Debug mode (compiler and cl65 utility) */
984 CmdAddArg (&CC65, "-d");
985 CmdAddArg (&CO65, "-d");
991 static void OptDebugInfo (const char* Opt attribute ((unused)),
992 const char* Arg attribute ((unused)))
993 /* Debug Info - add to compiler and assembler */
995 CmdAddArg (&CC65, "-g");
996 CmdAddArg (&CA65, "-g");
997 CmdAddArg (&CO65, "-g");
1002 static void OptFeature (const char* Opt attribute ((unused)), const char* Arg)
1003 /* Emulation features for the assembler */
1005 CmdAddArg2 (&CA65, "--feature", Arg);
1010 static void OptForceImport (const char* Opt attribute ((unused)), const char* Arg)
1011 /* Emulation features for the assembler */
1013 CmdAddArg2 (&LD65, "-u", Arg);
1018 static void OptHelp (const char* Opt attribute ((unused)),
1019 const char* Arg attribute ((unused)))
1020 /* Print help - cl65 */
1023 exit (EXIT_SUCCESS);
1028 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
1029 /* Include directory (compiler) */
1031 CmdAddArg2 (&CC65, "-I", Arg);
1036 static void OptLdArgs (const char* Opt attribute ((unused)), const char* Arg)
1037 /* Pass arguments to the linker */
1039 CmdAddArgList (&LD65, Arg);
1044 static void OptLib (const char* Opt attribute ((unused)), const char* Arg)
1045 /* Library file follows (linker) */
1047 CmdAddArg2 (&LD65, "--lib", Arg);
1052 static void OptLibPath (const char* Opt attribute ((unused)), const char* Arg)
1053 /* Library search path (linker) */
1055 CmdAddArg2 (&LD65, "--lib-path", Arg);
1060 static void OptListBytes (const char* Opt attribute ((unused)), const char* Arg)
1061 /* Set the maximum number of bytes per asm listing line */
1063 CmdAddArg2 (&CA65, "--list-bytes", Arg);
1068 static void OptListing (const char* Opt attribute ((unused)), const char* Arg)
1069 /* Create an assembler listing */
1071 CmdAddArg2 (&CA65, "-l", Arg);
1076 static void OptListTargets (const char* Opt attribute ((unused)),
1077 const char* Arg attribute ((unused)))
1078 /* List all targets */
1082 /* List the targets */
1083 for (T = TGT_NONE; T < TGT_COUNT; ++T) {
1084 printf ("%s\n", GetTargetName (T));
1088 exit (EXIT_SUCCESS);
1093 static void OptMapFile (const char* Opt attribute ((unused)), const char* Arg)
1094 /* Create a map file */
1096 /* Create a map file (linker) */
1097 CmdAddArg2 (&LD65, "-m", Arg);
1102 static void OptMemoryModel (const char* Opt attribute ((unused)), const char* Arg)
1103 /* Set the memory model */
1105 mmodel_t MemoryModel = FindMemoryModel (Arg);
1106 if (MemoryModel == MMODEL_UNKNOWN) {
1107 Error ("Unknown memory model: %s", Arg);
1108 } else if (MemoryModel == MMODEL_HUGE) {
1109 Error ("Unsupported memory model: %s", Arg);
1111 CmdAddArg2 (&CA65, "-mm", Arg);
1112 CmdAddArg2 (&CC65, "-mm", Arg);
1118 static void OptModule (const char* Opt attribute ((unused)),
1119 const char* Arg attribute ((unused)))
1120 /* Link as a module */
1127 static void OptModuleId (const char* Opt attribute ((unused)), const char* Arg)
1128 /* Specify a module if for the linker */
1130 /* Pass it straight to the linker */
1131 CmdAddArg2 (&LD65, "--module-id", Arg);
1136 static void OptO65Model (const char* Opt attribute ((unused)), const char* Arg)
1137 /* Handle the --o65-model option */
1139 CmdAddArg2 (&CO65, "-m", Arg);
1144 static void OptObj (const char* Opt attribute ((unused)), const char* Arg)
1145 /* Object file follows (linker) */
1147 CmdAddArg2 (&LD65, "--obj", Arg);
1152 static void OptObjPath (const char* Opt attribute ((unused)), const char* Arg)
1153 /* Object file search path (linker) */
1155 CmdAddArg2 (&LD65, "--obj-path", Arg);
1160 static void OptPrintTargetPath (const char* Opt attribute ((unused)),
1161 const char* Arg attribute ((unused)))
1162 /* Print the target file path */
1164 SearchPaths* TargetPath = NewSearchPath ();
1165 AddSubSearchPathFromEnv (TargetPath, "CC65_HOME", "target");
1166 #if defined(CL65_TGT) && !defined(_WIN32)
1167 AddSearchPath (TargetPath, STRINGIZE (CL65_TGT));
1169 AddSubSearchPathFromWinBin (TargetPath, "target");
1171 printf ("%s\n", GetSearchPath (TargetPath, 0));
1172 exit (EXIT_SUCCESS);
1177 static void OptRegisterSpace (const char* Opt attribute ((unused)), const char* Arg)
1178 /* Handle the --register-space option */
1180 CmdAddArg2 (&CC65, "--register-space", Arg);
1185 static void OptRegisterVars (const char* Opt attribute ((unused)),
1186 const char* Arg attribute ((unused)))
1187 /* Handle the --register-vars option */
1189 CmdAddArg (&CC65, "-r");
1194 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
1195 /* Handle the --rodata-name option */
1197 CmdAddArg2 (&CC65, "--rodata-name", Arg);
1202 static void OptSignedChars (const char* Opt attribute ((unused)),
1203 const char* Arg attribute ((unused)))
1204 /* Make default characters signed */
1206 CmdAddArg (&CC65, "-j");
1211 static void OptStandard (const char* Opt attribute ((unused)), const char* Arg)
1212 /* Set the language standard */
1214 CmdAddArg2 (&CC65, "--standard", Arg);
1219 static void OptStartAddr (const char* Opt attribute ((unused)), const char* Arg)
1220 /* Set the default start address */
1222 CmdAddArg2 (&LD65, "-S", Arg);
1227 static void OptStaticLocals (const char* Opt attribute ((unused)),
1228 const char* Arg attribute ((unused)))
1229 /* Place local variables in static storage */
1231 CmdAddArg (&CC65, "-Cl");
1236 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
1237 /* Set the target system */
1239 Target = FindTarget (Arg);
1240 if (Target == TGT_UNKNOWN) {
1241 Error ("No such target system: `%s'", Arg);
1242 } else if (Target == TGT_MODULE) {
1243 Error ("Cannot use `module' as target, use --module instead");
1249 static void OptVerbose (const char* Opt attribute ((unused)),
1250 const char* Arg attribute ((unused)))
1251 /* Verbose mode (compiler, assembler, linker) */
1253 CmdAddArg (&CC65, "-v");
1254 CmdAddArg (&CA65, "-v");
1255 CmdAddArg (&CO65, "-v");
1256 CmdAddArg (&LD65, "-v");
1261 static void OptVersion (const char* Opt attribute ((unused)),
1262 const char* Arg attribute ((unused)))
1263 /* Print version number */
1265 fprintf (stderr, "%s V%s\n", ProgName, GetVersionAsString ());
1271 static void OptZeropageLabel (const char* Opt attribute ((unused)), const char* Arg)
1272 /* Handle the --zeropage-label option */
1274 CmdAddArg2 (&CO65, "--zeropage-label", Arg);
1279 static void OptZeropageName (const char* Opt attribute ((unused)), const char* Arg)
1280 /* Handle the --zeropage-name option */
1282 CmdAddArg2 (&CO65, "--zeropage-name", Arg);
1287 int main (int argc, char* argv [])
1288 /* Utility main program */
1290 /* Program long options */
1291 static const LongOpt OptTab[] = {
1292 { "--add-source", 0, OptAddSource },
1293 { "--asm-args", 1, OptAsmArgs },
1294 { "--asm-define", 1, OptAsmDefine },
1295 { "--asm-include-dir", 1, OptAsmIncludeDir },
1296 { "--bin-include-dir", 1, OptBinIncludeDir },
1297 { "--bss-label", 1, OptBssLabel },
1298 { "--bss-name", 1, OptBssName },
1299 { "--cc-args", 1, OptCCArgs },
1300 { "--cfg-path", 1, OptCfgPath },
1301 { "--check-stack", 0, OptCheckStack },
1302 { "--code-label", 1, OptCodeLabel },
1303 { "--code-name", 1, OptCodeName },
1304 { "--codesize", 1, OptCodeSize },
1305 { "--config", 1, OptConfig },
1306 { "--cpu", 1, OptCPU },
1307 { "--create-dep", 1, OptCreateDep },
1308 { "--create-full-dep", 1, OptCreateFullDep },
1309 { "--data-label", 1, OptDataLabel },
1310 { "--data-name", 1, OptDataName },
1311 { "--debug", 0, OptDebug },
1312 { "--debug-info", 0, OptDebugInfo },
1313 { "--feature", 1, OptFeature },
1314 { "--force-import", 1, OptForceImport },
1315 { "--help", 0, OptHelp },
1316 { "--include-dir", 1, OptIncludeDir },
1317 { "--ld-args", 1, OptLdArgs },
1318 { "--lib", 1, OptLib },
1319 { "--lib-path", 1, OptLibPath },
1320 { "--list-targets", 0, OptListTargets },
1321 { "--listing", 1, OptListing },
1322 { "--list-bytes", 1, OptListBytes },
1323 { "--mapfile", 1, OptMapFile },
1324 { "--memory-model", 1, OptMemoryModel },
1325 { "--module", 0, OptModule },
1326 { "--module-id", 1, OptModuleId },
1327 { "--o65-model", 1, OptO65Model },
1328 { "--obj", 1, OptObj },
1329 { "--obj-path", 1, OptObjPath },
1330 { "--print-target-path", 0, OptPrintTargetPath},
1331 { "--register-space", 1, OptRegisterSpace },
1332 { "--register-vars", 0, OptRegisterVars },
1333 { "--rodata-name", 1, OptRodataName },
1334 { "--signed-chars", 0, OptSignedChars },
1335 { "--standard", 1, OptStandard },
1336 { "--start-addr", 1, OptStartAddr },
1337 { "--static-locals", 0, OptStaticLocals },
1338 { "--target", 1, OptTarget },
1339 { "--verbose", 0, OptVerbose },
1340 { "--version", 0, OptVersion },
1341 { "--zeropage-label", 1, OptZeropageLabel },
1342 { "--zeropage-name", 1, OptZeropageName },
1348 /* Initialize the cmdline module */
1349 InitCmdLine (&argc, &argv, "cl65");
1351 /* Initialize the command descriptors */
1353 CmdPath = xstrdup ("");
1356 CmdPath = xstrdup (argv[0]);
1357 Ptr = strrchr (CmdPath, '/');
1359 Ptr = strrchr (CmdPath, '\\');
1367 CmdInit (&CC65, CmdPath, "cc65");
1368 CmdInit (&CA65, CmdPath, "ca65");
1369 CmdInit (&CO65, CmdPath, "co65");
1370 CmdInit (&LD65, CmdPath, "ld65");
1371 CmdInit (&GRC, CmdPath, "grc65");
1374 /* Our default target is the C64 instead of "none" */
1377 /* Check the parameters */
1379 while (I < ArgCount) {
1381 /* Get the argument */
1382 const char* Arg = ArgVec[I];
1384 /* Check for an option */
1385 if (Arg [0] == '-') {
1390 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
1394 if (Arg[2] == 'l' && Arg[3] == '\0') {
1395 /* Make local variables static */
1396 OptStaticLocals (Arg, 0);
1398 /* Specify linker config file */
1399 OptConfig (Arg, GetArg (&I, 2));
1404 /* Define a preprocessor symbol (compiler) */
1405 CmdAddArg2 (&CC65, "-D", GetArg (&I, 2));
1409 /* Include directory (compiler) */
1410 OptIncludeDir (Arg, GetArg (&I, 2));
1414 if (Arg[2] == 'n' && Arg[3] == '\0') {
1415 /* VICE label file (linker) */
1416 CmdAddArg2 (&LD65, "-Ln", GetArg (&I, 3));
1418 /* Library search path (linker) */
1419 OptLibPath (Arg, GetArg (&I, 2));
1424 /* Optimize code (compiler, also covers -Oi and others) */
1425 CmdAddArg (&CC65, Arg);
1429 /* Dont assemble and link the created files */
1430 DisableAssemblingAndLinking();
1434 /* Include source as comment (compiler) */
1435 OptAddSource (Arg, 0);
1439 /* Print version number */
1440 OptVersion (Arg, 0);
1444 /* Forward -E to compiler */
1445 CmdAddArg (&CC65, Arg);
1446 DisableAssemblingAndLinking();
1450 /* avoid && with'\0' in if clauses */
1451 if (Arg[3] == '\0') {
1454 /* -Wa: Pass options to assembler */
1455 OptAsmArgs (Arg, GetArg (&I, 3));
1458 /* -Wc: Pass options to compiler
1459 ** Remember -Wc sub arguments in cc65 arg struct
1461 OptCCArgs (Arg, GetArg (&I, 3));
1464 /* -Wl: Pass options to linker */
1465 OptLdArgs (Arg, GetArg (&I, 3));
1471 /* Anything else: Suppress warnings (compiler) */
1472 CmdAddArg2 (&CC65, "-W", GetArg (&I, 2));
1477 /* Don't link the resulting files */
1482 /* Debug mode (compiler) */
1487 /* Debugging - add to compiler and assembler */
1488 OptDebugInfo (Arg, 0);
1493 /* Print help - cl65 */
1498 /* Default characters are signed */
1499 OptSignedChars (Arg, 0);
1503 /* Create an assembler listing */
1504 OptListing (Arg, GetArg (&I, 2));
1508 /* Create a map file (linker) */
1509 OptMapFile (Arg, GetArg (&I, 2));
1513 /* Name the output file */
1514 OutputName = GetArg (&I, 2);
1518 /* Enable register variables */
1519 OptRegisterVars (Arg, 0);
1523 /* Set target system - compiler, assembler and linker */
1524 OptTarget (Arg, GetArg (&I, 2));
1528 /* Force an import (linker) */
1529 OptForceImport (Arg, GetArg (&I, 2));
1533 if (Arg [2] == 'm') {
1534 /* Verbose map file (linker) */
1535 CmdAddArg (&LD65, "-vm");
1537 /* Verbose mode (compiler, assembler, linker) */
1538 OptVerbose (Arg, 0);
1543 UnknownOption (Arg);
1547 /* Remember the first file name */
1548 if (FirstInput == 0) {
1552 /* Determine the file type by the extension */
1553 switch (GetFileType (Arg)) {
1556 /* Compile the file */
1561 /* Assemble the file */
1569 /* Add to the linker files */
1570 CmdAddFile (&LD65, Arg);
1574 /* Add to the resource compiler files */
1579 /* Add the the object file converter files */
1584 Error ("Don't know what to do with `%s'", Arg);
1594 /* Check if we had any input files */
1595 if (FirstInput == 0) {
1596 Warning ("No input files");
1599 /* Link the given files if requested and if we have any */
1600 if (DoLink && LD65.FileCount > 0) {
1604 /* Return an apropriate exit code */
1605 return EXIT_SUCCESS;