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)
183 DisableAssembling ();
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 /* Get a pointer to the system name and its length */
384 const char* TargetName = GetTargetName (Target);
385 unsigned TargetNameLen = strlen (TargetName);
387 /* Set the library file */
388 TargetLib = xmalloc (TargetNameLen + 4 + 1);
389 memcpy (TargetLib, TargetName, TargetNameLen);
390 strcpy (TargetLib + TargetNameLen, ".lib");
395 /*****************************************************************************/
397 /*****************************************************************************/
401 static void ExecProgram (CmdDesc* Cmd)
402 /* Execute a subprocess with the given name/parameters. Exit on errors. */
406 /* If in debug mode, output the command line we will execute */
408 printf ("Executing: ");
409 CmdPrint (Cmd, stdout);
413 /* Call the program */
414 Status = spawnvp (P_WAIT, Cmd->Name, SPAWN_ARGV_CONST_CAST Cmd->Args);
416 /* Check the result code */
418 /* Error executing the program */
419 Error ("Cannot execute `%s': %s", Cmd->Name, strerror (errno));
420 } else if (Status != 0) {
421 /* Called program had an error */
428 static void Link (void)
429 /* Link the resulting executable */
433 /* Since linking is always the final step, if we have an output file name
434 ** given, set it here. If we don't have an explicit output name given,
435 ** try to build one from the name of the first input file.
439 CmdSetOutput (&LD65, OutputName);
441 } else if (FirstInput && FindExt (FirstInput)) { /* Only if ext present! */
443 const char* Extension = Module? MODULE_EXT : "";
444 char* Output = MakeFilename (FirstInput, Extension);
445 CmdSetOutput (&LD65, Output);
450 /* If we have a linker config file given, add it to the command line.
451 ** Otherwise pass the target to the linker if we have one.
455 Error ("Cannot use -C and --module together");
457 CmdAddArg2 (&LD65, "-C", LinkerConfig);
459 CmdSetTarget (&LD65, TGT_MODULE);
461 CmdSetTarget (&LD65, Target);
464 /* Determine which target libraries are needed */
467 /* Add all object files as parameters */
468 for (I = 0; I < LD65.FileCount; ++I) {
469 CmdAddArg (&LD65, LD65.Files [I]);
472 /* Add the system runtime library */
474 CmdAddArg (&LD65, TargetLib);
477 /* Terminate the argument list with a NULL pointer */
478 CmdAddArg (&LD65, 0);
480 /* Call the linker */
486 static void AssembleFile (const char* File, unsigned ArgCount)
487 /* Common routine to assemble a file. Will be called by Assemble() and
488 ** AssembleIntermediate(). Adds options common for both routines and
489 ** assembles the file. Will remove excess arguments after assembly.
492 /* Set the target system */
493 CmdSetTarget (&CA65, Target);
495 /* Check if this is the last processing step */
497 /* We're linking later. Add the output file of the assembly
498 ** the the file list of the linker. The name of the output
499 ** file is that of the input file with ".s" replaced by ".o".
501 char* ObjName = MakeFilename (File, ".o");
502 CmdAddFile (&LD65, ObjName);
505 /* This is the final step. If an output name is given, set it */
507 CmdSetOutput (&CA65, OutputName);
511 /* Add the file as argument for the assembler */
512 CmdAddArg (&CA65, File);
514 /* Add a NULL pointer to terminate the argument list */
515 CmdAddArg (&CA65, 0);
517 /* Run the assembler */
520 /* Remove the excess arguments */
521 CmdDelArgs (&CA65, ArgCount);
526 static void AssembleIntermediate (const char* SourceFile)
527 /* Assemble an intermediate file which was generated by a previous processing
528 ** step with SourceFile as input. The -dep options won't be added and
529 ** the intermediate assembler file is removed after assembly.
532 /* Generate the name of the assembler output file from the source file
533 ** name. It's the same name with the extension replaced by ".s"
535 char* AsmName = MakeFilename (SourceFile, ".s");
537 /* Assemble the intermediate assembler file */
538 AssembleFile (AsmName, CA65.ArgCount);
540 /* Remove the input file */
541 if (remove (AsmName) < 0) {
542 Warning ("Cannot remove temporary file `%s': %s",
543 AsmName, strerror (errno));
546 /* Free the assembler file name which was allocated from the heap */
552 static void Assemble (const char* File)
553 /* Assemble the given file */
555 /* Remember the current assembler argument count */
556 unsigned ArgCount = CA65.ArgCount;
558 /* We aren't assembling an intermediate file, but one requested by the
559 ** user. So add a few options here if they were given on the command
562 if (DepName && *DepName) {
563 CmdAddArg2 (&CA65, "--create-dep", DepName);
565 if (FullDepName && *FullDepName) {
566 CmdAddArg2 (&CA65, "--create-full-dep", FullDepName);
569 /* Use the common routine */
570 AssembleFile (File, ArgCount);
575 static void Compile (const char* File)
576 /* Compile the given file */
578 /* Remember the current compiler argument count */
579 unsigned ArgCount = CC65.ArgCount;
581 /* Set the target system */
582 CmdSetTarget (&CC65, Target);
584 /* Check if this is the final step */
586 /* We will assemble this file later. If a dependency file is to be
587 ** generated, set the dependency target to be the final object file,
588 ** not the intermediate assembler file. But beware: There may be an
589 ** output name specified for the assembler.
591 if (DepName || FullDepName) {
592 /* Was an output name for the assembler specified? */
593 if (!DoLink && OutputName) {
594 /* Use this name as the dependency target */
595 CmdAddArg2 (&CC65, "--dep-target", OutputName);
597 /* Use the object file name as the dependency target */
598 char* ObjName = MakeFilename (File, ".o");
599 CmdAddArg2 (&CC65, "--dep-target", ObjName);
604 /* If we won't assemble, this is the final step. In this case, set
605 ** the output name if it was given.
608 CmdSetOutput (&CC65, OutputName);
612 /* Add the file as argument for the compiler */
613 CmdAddArg (&CC65, File);
615 /* Add a NULL pointer to terminate the argument list */
616 CmdAddArg (&CC65, 0);
618 /* Run the compiler */
621 /* Remove the excess arguments */
622 CmdDelArgs (&CC65, ArgCount);
624 /* If this is not the final step, assemble the generated file, then
628 /* Assemble the intermediate file and remove it */
629 AssembleIntermediate (File);
635 static void CompileRes (const char* File)
636 /* Compile the given geos resource file */
638 /* Remember the current assembler argument count */
639 unsigned ArgCount = GRC.ArgCount;
641 /* Resource files need an geos-apple or geos-cbm target but this
642 ** is checked within grc65.
644 CmdSetTarget (&GRC, Target);
646 /* Add the file as argument for the resource compiler */
647 CmdAddArg (&GRC, File);
649 /* Add a NULL pointer to terminate the argument list */
652 /* Run the compiler */
655 /* Remove the excess arguments */
656 CmdDelArgs (&GRC, 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 static void ConvertO65 (const char* File)
670 /* Convert an o65 object file into an assembler file */
672 /* Remember the current converter argument count */
673 unsigned ArgCount = CO65.ArgCount;
675 /* If we won't assemble, this is the final step. In this case, set the
678 if (!DoAssemble && OutputName) {
679 CmdSetOutput (&CO65, OutputName);
682 /* Add the file as argument for the object file converter */
683 CmdAddArg (&CO65, File);
685 /* Add a NULL pointer to terminate the argument list */
686 CmdAddArg (&CO65, 0);
688 /* Run the converter */
691 /* Remove the excess arguments */
692 CmdDelArgs (&CO65, ArgCount);
694 /* If this is not the final step, assemble the generated file, then
698 /* Assemble the intermediate file and remove it */
699 AssembleIntermediate (File);
705 /*****************************************************************************/
707 /*****************************************************************************/
711 static void Usage (void)
712 /* Print usage information and exit */
714 printf ("Usage: %s [options] file [...]\n"
716 " -c\t\t\t\tCompile and assemble, but don't link\n"
717 " -d\t\t\t\tDebug mode\n"
718 " -g\t\t\t\tAdd debug info\n"
719 " -h\t\t\t\tHelp (this text)\n"
720 " -l name\t\t\tCreate an assembler listing file\n"
721 " -m name\t\t\tCreate a map file\n"
722 " -mm model\t\t\tSet the memory model\n"
723 " -o name\t\t\tName the output file\n"
724 " -r\t\t\t\tEnable register variables\n"
725 " -t sys\t\t\tSet the target system\n"
726 " -u sym\t\t\tForce an import of symbol `sym'\n"
727 " -v\t\t\t\tVerbose mode\n"
728 " -vm\t\t\t\tVerbose map file\n"
729 " -C name\t\t\tUse linker config file\n"
730 " -Cl\t\t\t\tMake local variables static\n"
731 " -D sym[=defn]\t\t\tDefine a preprocessor symbol\n"
732 " -E\t\t\t\tStop after the preprocessing stage\n"
733 " -I dir\t\t\tSet a compiler include directory path\n"
734 " -L path\t\t\tSpecify a library search path\n"
735 " -Ln name\t\t\tCreate a VICE label file\n"
736 " -O\t\t\t\tOptimize code\n"
737 " -Oi\t\t\t\tOptimize code, inline runtime functions\n"
738 " -Or\t\t\t\tOptimize code, honour the register keyword\n"
739 " -Os\t\t\t\tOptimize code, inline known C functions\n"
740 " -S\t\t\t\tCompile, but don't assemble and link\n"
741 " -T\t\t\t\tInclude source as comment\n"
742 " -V\t\t\t\tPrint the version number\n"
743 " -W name[,...]\t\t\tSuppress compiler warnings\n"
744 " -Wa options\t\t\tPass options to the assembler\n"
745 " -Wc options\t\t\tPass options to the compiler\n"
746 " -Wl options\t\t\tPass options to the linker\n"
749 " --add-source\t\t\tInclude source as comment\n"
750 " --all-cdecl\t\t\tMake functions default to __cdecl__\n"
751 " --asm-args options\t\tPass options to the assembler\n"
752 " --asm-define sym[=v]\t\tDefine an assembler symbol\n"
753 " --asm-include-dir dir\t\tSet an assembler include directory\n"
754 " --bin-include-dir dir\t\tSet an assembler binary include directory\n"
755 " --bss-label name\t\tDefine and export a BSS segment label\n"
756 " --bss-name seg\t\tSet the name of the BSS segment\n"
757 " --cc-args options\t\tPass options to the compiler\n"
758 " --cfg-path path\t\tSpecify a config file search path\n"
759 " --check-stack\t\t\tGenerate stack overflow checks\n"
760 " --code-label name\t\tDefine and export a CODE segment label\n"
761 " --code-name seg\t\tSet the name of the CODE segment\n"
762 " --codesize x\t\t\tAccept larger code by factor x\n"
763 " --config name\t\t\tUse linker config file\n"
764 " --cpu type\t\t\tSet CPU type\n"
765 " --create-dep name\t\tCreate a make dependency file\n"
766 " --create-full-dep name\tCreate a full make dependency file\n"
767 " --data-label name\t\tDefine and export a DATA segment label\n"
768 " --data-name seg\t\tSet the name of the DATA segment\n"
769 " --debug\t\t\tDebug mode\n"
770 " --debug-info\t\t\tAdd debug info\n"
771 " --feature name\t\tSet an emulation feature\n"
772 " --force-import sym\t\tForce an import of symbol `sym'\n"
773 " --help\t\t\tHelp (this text)\n"
774 " --include-dir dir\t\tSet a compiler include directory path\n"
775 " --ld-args options\t\tPass options to the linker\n"
776 " --lib file\t\t\tLink this library\n"
777 " --lib-path path\t\tSpecify a library search path\n"
778 " --list-targets\t\tList all available targets\n"
779 " --listing name\t\tCreate an assembler listing file\n"
780 " --list-bytes n\t\tNumber of bytes per assembler listing line\n"
781 " --mapfile name\t\tCreate a map file\n"
782 " --memory-model model\t\tSet the memory model\n"
783 " --module\t\t\tLink as a module\n"
784 " --module-id id\t\tSpecify a module ID for the linker\n"
785 " --o65-model model\t\tOverride the o65 model\n"
786 " --obj file\t\t\tLink this object file\n"
787 " --obj-path path\t\tSpecify an object file search path\n"
788 " --print-target-path\t\tPrint the target file path\n"
789 " --register-space b\t\tSet space available for register variables\n"
790 " --register-vars\t\tEnable register variables\n"
791 " --rodata-name seg\t\tSet the name of the RODATA segment\n"
792 " --signed-chars\t\tDefault characters are signed\n"
793 " --standard std\t\tLanguage standard (c89, c99, cc65)\n"
794 " --start-addr addr\t\tSet the default start address\n"
795 " --static-locals\t\tMake local variables static\n"
796 " --target sys\t\t\tSet the target system\n"
797 " --version\t\t\tPrint the version number\n"
798 " --verbose\t\t\tVerbose mode\n"
799 " --zeropage-label name\t\tDefine and export a ZEROPAGE segment label\n"
800 " --zeropage-name seg\t\tSet the name of the ZEROPAGE segment\n",
806 static void OptAddSource (const char* Opt attribute ((unused)),
807 const char* Arg attribute ((unused)))
808 /* Strict source code as comments to the generated asm code */
810 CmdAddArg (&CC65, "-T");
814 static void OptAllCDecl (const char* Opt attribute ((unused)),
815 const char* Arg attribute ((unused)))
816 /* Make functions default to __cdecl__ */
818 CmdAddArg (&CC65, "--all-cdecl");
823 static void OptAsmArgs (const char* Opt attribute ((unused)), const char* Arg)
824 /* Pass arguments to the assembler */
826 CmdAddArgList (&CA65, Arg);
831 static void OptAsmDefine (const char* Opt attribute ((unused)), const char* Arg)
832 /* Define an assembler symbol (assembler) */
834 CmdAddArg2 (&CA65, "-D", Arg);
839 static void OptAsmIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
840 /* Include directory (assembler) */
842 CmdAddArg2 (&CA65, "-I", Arg);
847 static void OptBinIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
848 /* Binary include directory (assembler) */
850 CmdAddArg2 (&CA65, "--bin-include-dir", Arg);
855 static void OptBssLabel (const char* Opt attribute ((unused)), const char* Arg)
856 /* Handle the --bss-label option */
858 CmdAddArg2 (&CO65, "--bss-label", Arg);
863 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
864 /* Handle the --bss-name option */
866 CmdAddArg2 (&CC65, "--bss-name", Arg);
867 CmdAddArg2 (&CO65, "--bss-name", Arg);
872 static void OptCCArgs (const char* Opt attribute ((unused)), const char* Arg)
873 /* Pass arguments to the compiler */
875 CmdAddArgList (&CC65, Arg);
880 static void OptCfgPath (const char* Opt attribute ((unused)), const char* Arg)
881 /* Config file search path (linker) */
883 CmdAddArg2 (&LD65, "--cfg-path", Arg);
888 static void OptCheckStack (const char* Opt attribute ((unused)),
889 const char* Arg attribute ((unused)))
890 /* Handle the --check-stack option */
892 CmdAddArg (&CC65, "--check-stack");
897 static void OptCodeLabel (const char* Opt attribute ((unused)), const char* Arg)
898 /* Handle the --code-label option */
900 CmdAddArg2 (&CO65, "--code-label", Arg);
905 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
906 /* Handle the --code-name option */
908 CmdAddArg2 (&CC65, "--code-name", Arg);
909 CmdAddArg2 (&CO65, "--code-name", Arg);
914 static void OptCodeSize (const char* Opt attribute ((unused)), const char* Arg)
915 /* Handle the --codesize option */
917 CmdAddArg2 (&CC65, "--codesize", Arg);
922 static void OptConfig (const char* Opt attribute ((unused)), const char* Arg)
923 /* Config file (linker) */
926 Error ("Cannot specify -C/--config twice");
933 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
934 /* Handle the --cpu option */
936 /* Add the cpu type to the assembler and compiler */
937 CmdAddArg2 (&CA65, "--cpu", Arg);
938 CmdAddArg2 (&CC65, "--cpu", Arg);
943 static void OptCreateDep (const char* Opt attribute ((unused)), const char* Arg)
944 /* Handle the --create-dep option */
946 /* Add the file name to the compiler */
947 CmdAddArg2 (&CC65, "--create-dep", Arg);
949 /* Remember the file name for the assembler */
955 static void OptCreateFullDep (const char* Opt attribute ((unused)), const char* Arg)
956 /* Handle the --create-full-dep option */
958 /* Add the file name to the compiler */
959 CmdAddArg2 (&CC65, "--create-full-dep", Arg);
961 /* Remember the file name for the assembler */
967 static void OptDataLabel (const char* Opt attribute ((unused)), const char* Arg)
968 /* Handle the --data-label option */
970 CmdAddArg2 (&CO65, "--data-label", Arg);
975 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
976 /* Handle the --data-name option */
978 CmdAddArg2 (&CC65, "--data-name", Arg);
979 CmdAddArg2 (&CO65, "--data-name", Arg);
984 static void OptDebug (const char* Opt attribute ((unused)),
985 const char* Arg attribute ((unused)))
986 /* Debug mode (compiler and cl65 utility) */
988 CmdAddArg (&CC65, "-d");
989 CmdAddArg (&CO65, "-d");
995 static void OptDebugInfo (const char* Opt attribute ((unused)),
996 const char* Arg attribute ((unused)))
997 /* Debug Info - add to compiler and assembler */
999 CmdAddArg (&CC65, "-g");
1000 CmdAddArg (&CA65, "-g");
1001 CmdAddArg (&CO65, "-g");
1006 static void OptFeature (const char* Opt attribute ((unused)), const char* Arg)
1007 /* Emulation features for the assembler */
1009 CmdAddArg2 (&CA65, "--feature", Arg);
1014 static void OptForceImport (const char* Opt attribute ((unused)), const char* Arg)
1015 /* Emulation features for the assembler */
1017 CmdAddArg2 (&LD65, "-u", Arg);
1022 static void OptHelp (const char* Opt attribute ((unused)),
1023 const char* Arg attribute ((unused)))
1024 /* Print help - cl65 */
1027 exit (EXIT_SUCCESS);
1032 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
1033 /* Include directory (compiler) */
1035 CmdAddArg2 (&CC65, "-I", Arg);
1040 static void OptLdArgs (const char* Opt attribute ((unused)), const char* Arg)
1041 /* Pass arguments to the linker */
1043 CmdAddArgList (&LD65, Arg);
1048 static void OptLib (const char* Opt attribute ((unused)), const char* Arg)
1049 /* Library file follows (linker) */
1051 CmdAddArg2 (&LD65, "--lib", Arg);
1056 static void OptLibPath (const char* Opt attribute ((unused)), const char* Arg)
1057 /* Library search path (linker) */
1059 CmdAddArg2 (&LD65, "--lib-path", Arg);
1064 static void OptListBytes (const char* Opt attribute ((unused)), const char* Arg)
1065 /* Set the maximum number of bytes per asm listing line */
1067 CmdAddArg2 (&CA65, "--list-bytes", Arg);
1072 static void OptListing (const char* Opt attribute ((unused)), const char* Arg)
1073 /* Create an assembler listing */
1075 CmdAddArg2 (&CA65, "-l", Arg);
1080 static void OptListTargets (const char* Opt attribute ((unused)),
1081 const char* Arg attribute ((unused)))
1082 /* List all targets */
1086 /* List the targets */
1087 for (T = TGT_NONE; T < TGT_COUNT; ++T) {
1088 printf ("%s\n", GetTargetName (T));
1092 exit (EXIT_SUCCESS);
1097 static void OptMapFile (const char* Opt attribute ((unused)), const char* Arg)
1098 /* Create a map file */
1100 /* Create a map file (linker) */
1101 CmdAddArg2 (&LD65, "-m", Arg);
1106 static void OptMemoryModel (const char* Opt attribute ((unused)), const char* Arg)
1107 /* Set the memory model */
1109 mmodel_t MemoryModel = FindMemoryModel (Arg);
1110 if (MemoryModel == MMODEL_UNKNOWN) {
1111 Error ("Unknown memory model: %s", Arg);
1112 } else if (MemoryModel == MMODEL_HUGE) {
1113 Error ("Unsupported memory model: %s", Arg);
1115 CmdAddArg2 (&CA65, "-mm", Arg);
1116 CmdAddArg2 (&CC65, "-mm", Arg);
1122 static void OptModule (const char* Opt attribute ((unused)),
1123 const char* Arg attribute ((unused)))
1124 /* Link as a module */
1131 static void OptModuleId (const char* Opt attribute ((unused)), const char* Arg)
1132 /* Specify a module if for the linker */
1134 /* Pass it straight to the linker */
1135 CmdAddArg2 (&LD65, "--module-id", Arg);
1140 static void OptO65Model (const char* Opt attribute ((unused)), const char* Arg)
1141 /* Handle the --o65-model option */
1143 CmdAddArg2 (&CO65, "-m", Arg);
1148 static void OptObj (const char* Opt attribute ((unused)), const char* Arg)
1149 /* Object file follows (linker) */
1151 CmdAddArg2 (&LD65, "--obj", Arg);
1156 static void OptObjPath (const char* Opt attribute ((unused)), const char* Arg)
1157 /* Object file search path (linker) */
1159 CmdAddArg2 (&LD65, "--obj-path", Arg);
1164 static void OptPrintTargetPath (const char* Opt attribute ((unused)),
1165 const char* Arg attribute ((unused)))
1166 /* Print the target file path */
1168 SearchPaths* TargetPath = NewSearchPath ();
1169 AddSubSearchPathFromEnv (TargetPath, "CC65_HOME", "target");
1170 #if defined(CL65_TGT) && !defined(_WIN32)
1171 AddSearchPath (TargetPath, STRINGIZE (CL65_TGT));
1173 AddSubSearchPathFromWinBin (TargetPath, "target");
1175 printf ("%s\n", GetSearchPath (TargetPath, 0));
1176 exit (EXIT_SUCCESS);
1181 static void OptRegisterSpace (const char* Opt attribute ((unused)), const char* Arg)
1182 /* Handle the --register-space option */
1184 CmdAddArg2 (&CC65, "--register-space", Arg);
1189 static void OptRegisterVars (const char* Opt attribute ((unused)),
1190 const char* Arg attribute ((unused)))
1191 /* Handle the --register-vars option */
1193 CmdAddArg (&CC65, "-r");
1198 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
1199 /* Handle the --rodata-name option */
1201 CmdAddArg2 (&CC65, "--rodata-name", Arg);
1206 static void OptSignedChars (const char* Opt attribute ((unused)),
1207 const char* Arg attribute ((unused)))
1208 /* Make default characters signed */
1210 CmdAddArg (&CC65, "-j");
1215 static void OptStandard (const char* Opt attribute ((unused)), const char* Arg)
1216 /* Set the language standard */
1218 CmdAddArg2 (&CC65, "--standard", Arg);
1223 static void OptStartAddr (const char* Opt attribute ((unused)), const char* Arg)
1224 /* Set the default start address */
1226 CmdAddArg2 (&LD65, "-S", Arg);
1231 static void OptStaticLocals (const char* Opt attribute ((unused)),
1232 const char* Arg attribute ((unused)))
1233 /* Place local variables in static storage */
1235 CmdAddArg (&CC65, "-Cl");
1240 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
1241 /* Set the target system */
1243 Target = FindTarget (Arg);
1244 if (Target == TGT_UNKNOWN) {
1245 Error ("No such target system: `%s'", Arg);
1246 } else if (Target == TGT_MODULE) {
1247 Error ("Cannot use `module' as target, use --module instead");
1253 static void OptVerbose (const char* Opt attribute ((unused)),
1254 const char* Arg attribute ((unused)))
1255 /* Verbose mode (compiler, assembler, linker) */
1257 CmdAddArg (&CC65, "-v");
1258 CmdAddArg (&CA65, "-v");
1259 CmdAddArg (&CO65, "-v");
1260 CmdAddArg (&LD65, "-v");
1265 static void OptVersion (const char* Opt attribute ((unused)),
1266 const char* Arg attribute ((unused)))
1267 /* Print version number */
1269 fprintf (stderr, "%s V%s\n", ProgName, GetVersionAsString ());
1275 static void OptZeropageLabel (const char* Opt attribute ((unused)), const char* Arg)
1276 /* Handle the --zeropage-label option */
1278 CmdAddArg2 (&CO65, "--zeropage-label", Arg);
1283 static void OptZeropageName (const char* Opt attribute ((unused)), const char* Arg)
1284 /* Handle the --zeropage-name option */
1286 CmdAddArg2 (&CO65, "--zeropage-name", Arg);
1291 int main (int argc, char* argv [])
1292 /* Utility main program */
1294 /* Program long options */
1295 static const LongOpt OptTab[] = {
1296 { "--add-source", 0, OptAddSource },
1297 { "--all-cdecl", 0, OptAllCDecl },
1298 { "--asm-args", 1, OptAsmArgs },
1299 { "--asm-define", 1, OptAsmDefine },
1300 { "--asm-include-dir", 1, OptAsmIncludeDir },
1301 { "--bin-include-dir", 1, OptBinIncludeDir },
1302 { "--bss-label", 1, OptBssLabel },
1303 { "--bss-name", 1, OptBssName },
1304 { "--cc-args", 1, OptCCArgs },
1305 { "--cfg-path", 1, OptCfgPath },
1306 { "--check-stack", 0, OptCheckStack },
1307 { "--code-label", 1, OptCodeLabel },
1308 { "--code-name", 1, OptCodeName },
1309 { "--codesize", 1, OptCodeSize },
1310 { "--config", 1, OptConfig },
1311 { "--cpu", 1, OptCPU },
1312 { "--create-dep", 1, OptCreateDep },
1313 { "--create-full-dep", 1, OptCreateFullDep },
1314 { "--data-label", 1, OptDataLabel },
1315 { "--data-name", 1, OptDataName },
1316 { "--debug", 0, OptDebug },
1317 { "--debug-info", 0, OptDebugInfo },
1318 { "--feature", 1, OptFeature },
1319 { "--force-import", 1, OptForceImport },
1320 { "--help", 0, OptHelp },
1321 { "--include-dir", 1, OptIncludeDir },
1322 { "--ld-args", 1, OptLdArgs },
1323 { "--lib", 1, OptLib },
1324 { "--lib-path", 1, OptLibPath },
1325 { "--list-targets", 0, OptListTargets },
1326 { "--listing", 1, OptListing },
1327 { "--list-bytes", 1, OptListBytes },
1328 { "--mapfile", 1, OptMapFile },
1329 { "--memory-model", 1, OptMemoryModel },
1330 { "--module", 0, OptModule },
1331 { "--module-id", 1, OptModuleId },
1332 { "--o65-model", 1, OptO65Model },
1333 { "--obj", 1, OptObj },
1334 { "--obj-path", 1, OptObjPath },
1335 { "--print-target-path", 0, OptPrintTargetPath},
1336 { "--register-space", 1, OptRegisterSpace },
1337 { "--register-vars", 0, OptRegisterVars },
1338 { "--rodata-name", 1, OptRodataName },
1339 { "--signed-chars", 0, OptSignedChars },
1340 { "--standard", 1, OptStandard },
1341 { "--start-addr", 1, OptStartAddr },
1342 { "--static-locals", 0, OptStaticLocals },
1343 { "--target", 1, OptTarget },
1344 { "--verbose", 0, OptVerbose },
1345 { "--version", 0, OptVersion },
1346 { "--zeropage-label", 1, OptZeropageLabel },
1347 { "--zeropage-name", 1, OptZeropageName },
1353 /* Initialize the cmdline module */
1354 InitCmdLine (&argc, &argv, "cl65");
1356 /* Initialize the command descriptors */
1358 CmdPath = xstrdup ("");
1361 CmdPath = xstrdup (argv[0]);
1362 Ptr = strrchr (CmdPath, '/');
1364 Ptr = strrchr (CmdPath, '\\');
1372 CmdInit (&CC65, CmdPath, "cc65");
1373 CmdInit (&CA65, CmdPath, "ca65");
1374 CmdInit (&CO65, CmdPath, "co65");
1375 CmdInit (&LD65, CmdPath, "ld65");
1376 CmdInit (&GRC, CmdPath, "grc65");
1379 /* Our default target is the C64 instead of "none" */
1382 /* Check the parameters */
1384 while (I < ArgCount) {
1386 /* Get the argument */
1387 const char* Arg = ArgVec[I];
1389 /* Check for an option */
1390 if (Arg [0] == '-') {
1395 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
1399 if (Arg[2] == 'l' && Arg[3] == '\0') {
1400 /* Make local variables static */
1401 OptStaticLocals (Arg, 0);
1403 /* Specify linker config file */
1404 OptConfig (Arg, GetArg (&I, 2));
1409 /* Define a preprocessor symbol (compiler) */
1410 CmdAddArg2 (&CC65, "-D", GetArg (&I, 2));
1414 /* Include directory (compiler) */
1415 OptIncludeDir (Arg, GetArg (&I, 2));
1419 if (Arg[2] == 'n' && Arg[3] == '\0') {
1420 /* VICE label file (linker) */
1421 CmdAddArg2 (&LD65, "-Ln", GetArg (&I, 3));
1423 /* Library search path (linker) */
1424 OptLibPath (Arg, GetArg (&I, 2));
1429 /* Optimize code (compiler, also covers -Oi and others) */
1430 CmdAddArg (&CC65, Arg);
1434 /* Dont assemble and link the created files */
1435 DisableAssemblingAndLinking ();
1439 /* Include source as comment (compiler) */
1440 OptAddSource (Arg, 0);
1444 /* Print version number */
1445 OptVersion (Arg, 0);
1449 /* Forward -E to compiler */
1450 CmdAddArg (&CC65, Arg);
1451 DisableAssemblingAndLinking ();
1455 /* avoid && with'\0' in if clauses */
1456 if (Arg[3] == '\0') {
1459 /* -Wa: Pass options to assembler */
1460 OptAsmArgs (Arg, GetArg (&I, 3));
1463 /* -Wc: Pass options to compiler
1464 ** Remember -Wc sub arguments in cc65 arg struct
1466 OptCCArgs (Arg, GetArg (&I, 3));
1469 /* -Wl: Pass options to linker */
1470 OptLdArgs (Arg, GetArg (&I, 3));
1473 UnknownOption (Arg);
1477 /* Anything else: Suppress warnings (compiler) */
1478 CmdAddArg2 (&CC65, "-W", GetArg (&I, 2));
1483 /* Don't link the resulting files */
1488 /* Debug mode (compiler) */
1493 /* Debugging - add to compiler and assembler */
1494 OptDebugInfo (Arg, 0);
1499 /* Print help - cl65 */
1504 /* Default characters are signed */
1505 OptSignedChars (Arg, 0);
1509 /* Create an assembler listing */
1510 OptListing (Arg, GetArg (&I, 2));
1514 /* Create a map file (linker) */
1515 OptMapFile (Arg, GetArg (&I, 2));
1519 /* Name the output file */
1520 OutputName = GetArg (&I, 2);
1524 /* Enable register variables */
1525 OptRegisterVars (Arg, 0);
1529 /* Set target system - compiler, assembler and linker */
1530 OptTarget (Arg, GetArg (&I, 2));
1534 /* Force an import (linker) */
1535 OptForceImport (Arg, GetArg (&I, 2));
1539 if (Arg [2] == 'm') {
1540 /* Verbose map file (linker) */
1541 CmdAddArg (&LD65, "-vm");
1543 /* Verbose mode (compiler, assembler, linker) */
1544 OptVerbose (Arg, 0);
1549 UnknownOption (Arg);
1553 /* Remember the first file name */
1554 if (FirstInput == 0) {
1558 /* Determine the file type by the extension */
1559 switch (GetFileType (Arg)) {
1562 /* Compile the file */
1567 /* Assemble the file */
1575 /* Add to the linker files */
1576 CmdAddFile (&LD65, Arg);
1580 /* Add to the resource compiler files */
1585 /* Add the the object file converter files */
1590 Error ("Don't know what to do with `%s'", Arg);
1600 /* Check if we had any input files */
1601 if (FirstInput == 0) {
1602 Warning ("No input files");
1605 /* Link the given files if requested and if we have any */
1606 if (DoLink && LD65.FileCount > 0) {
1610 /* Return an apropriate exit code */
1611 return EXIT_SUCCESS;