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"
159 /*****************************************************************************/
160 /* Credential functions */
161 /*****************************************************************************/
162 void DisableAssembling(void){
166 void DisableLinking(void){
170 void DisableAssemblingAndLinking(void){
175 /*****************************************************************************/
176 /* Command structure handling */
177 /*****************************************************************************/
181 static void CmdExpand (CmdDesc* Cmd)
182 /* Expand the argument vector */
184 unsigned NewMax = Cmd->ArgMax + 10;
185 char** NewArgs = xmalloc (NewMax * sizeof (char*));
186 memcpy (NewArgs, Cmd->Args, Cmd->ArgMax * sizeof (char*));
189 Cmd->ArgMax = NewMax;
194 static void CmdAddArg (CmdDesc* Cmd, const char* Arg)
195 /* Add a new argument to the command */
197 /* Expand the argument vector if needed */
198 if (Cmd->ArgCount >= Cmd->ArgMax) {
202 /* Add a copy of the new argument, allow a NULL pointer */
204 Cmd->Args[Cmd->ArgCount++] = xstrdup (Arg);
206 Cmd->Args[Cmd->ArgCount++] = 0;
212 static void CmdAddArg2 (CmdDesc* Cmd, const char* Arg1, const char* Arg2)
213 /* Add a new argument pair to the command */
215 CmdAddArg (Cmd, Arg1);
216 CmdAddArg (Cmd, Arg2);
221 static void CmdAddArgList (CmdDesc* Cmd, const char* ArgList)
222 /* Add a list of arguments separated by commas */
224 const char* Arg = ArgList;
228 if (*P == '\0' || *P == ',') {
230 /* End of argument, add it */
231 unsigned Len = P - Arg;
233 /* Expand the argument vector if needed */
234 if (Cmd->ArgCount >= Cmd->ArgMax) {
238 /* Add the new argument */
239 Cmd->Args[Cmd->ArgCount] = memcpy (xmalloc (Len + 1), Arg, Len);
240 Cmd->Args[Cmd->ArgCount][Len] = '\0';
243 /* If the argument was terminated by a comma, skip it, otherwise
247 /* Start over at next char */
253 /* Skip other chars */
262 static void CmdDelArgs (CmdDesc* Cmd, unsigned LastValid)
263 /* Remove all arguments with an index greater than LastValid */
265 while (Cmd->ArgCount > LastValid) {
267 xfree (Cmd->Args [Cmd->ArgCount]);
268 Cmd->Args [Cmd->ArgCount] = 0;
274 static void CmdAddFile (CmdDesc* Cmd, const char* File)
275 /* Add a new file to the command */
277 /* Expand the file vector if needed */
278 if (Cmd->FileCount == Cmd->FileMax) {
279 unsigned NewMax = Cmd->FileMax + 10;
280 char** NewFiles = xmalloc (NewMax * sizeof (char*));
281 memcpy (NewFiles, Cmd->Files, Cmd->FileMax * sizeof (char*));
283 Cmd->Files = NewFiles;
284 Cmd->FileMax = NewMax;
287 /* If the file name is not NULL (which is legal and is used to terminate
288 ** the file list), check if the file name does already exist in the file
289 ** list and print a warning if so. Regardless of the search result, add
294 for (I = 0; I < Cmd->FileCount; ++I) {
295 if (strcmp (Cmd->Files[I], File) == 0) {
297 Warning ("Duplicate file in argument list: `%s'", File);
298 /* No need to search further */
304 Cmd->Files [Cmd->FileCount++] = xstrdup (File);
306 /* Add a NULL pointer */
307 Cmd->Files [Cmd->FileCount++] = 0;
313 static void CmdInit (CmdDesc* Cmd, const char* Path, const char* Name)
314 /* Initialize the command using the given path and name of the executable */
318 FullName = (char*) xmalloc (strlen (Path) + strlen (Name) + 1);
319 strcpy (FullName, Path);
320 strcat (FullName, Name);
322 /* Remember the command */
323 Cmd->Name = xstrdup (FullName);
325 /* Use the command name as first argument */
326 CmdAddArg (Cmd, FullName);
333 static void CmdSetOutput (CmdDesc* Cmd, const char* File)
334 /* Set the output file in a command desc */
336 CmdAddArg2 (Cmd, "-o", File);
341 static void CmdSetTarget (CmdDesc* Cmd, target_t Target)
342 /* Set the output file in a command desc */
344 CmdAddArg2 (Cmd, "-t", GetTargetName (Target));
349 static void CmdPrint (CmdDesc* Cmd, FILE* F)
350 /* Output the command line encoded in the command desc */
353 for (I = 0; I < Cmd->ArgCount && Cmd->Args[I] != 0; ++I) {
354 fprintf (F, "%s ", Cmd->Args[I]);
360 /*****************************************************************************/
361 /* Target handling */
362 /*****************************************************************************/
366 static void SetTargetFiles (void)
367 /* Set the target system files */
369 /* Determine the names of the target specific library file */
370 if (Target != TGT_NONE) {
372 /* Get a pointer to the system name and its length */
373 const char* TargetName = GetTargetName (Target);
374 unsigned TargetNameLen = strlen (TargetName);
376 /* Set the library file */
377 TargetLib = xmalloc (TargetNameLen + 4 + 1);
378 memcpy (TargetLib, TargetName, TargetNameLen);
379 strcpy (TargetLib + TargetNameLen, ".lib");
386 /*****************************************************************************/
388 /*****************************************************************************/
392 static void ExecProgram (CmdDesc* Cmd)
393 /* Execute a subprocess with the given name/parameters. Exit on errors. */
397 /* If in debug mode, output the command line we will execute */
399 printf ("Executing: ");
400 CmdPrint (Cmd, stdout);
404 /* Call the program */
405 Status = spawnvp (P_WAIT, Cmd->Name, SPAWN_ARGV_CONST_CAST Cmd->Args);
407 /* Check the result code */
409 /* Error executing the program */
410 Error ("Cannot execute `%s': %s", Cmd->Name, strerror (errno));
411 } else if (Status != 0) {
412 /* Called program had an error */
419 static void Link (void)
420 /* Link the resulting executable */
424 /* Since linking is always the final step, if we have an output file name
425 ** given, set it here. If we don't have an explicit output name given,
426 ** try to build one from the name of the first input file.
430 CmdSetOutput (&LD65, OutputName);
432 } else if (FirstInput && FindExt (FirstInput)) { /* Only if ext present! */
434 const char* Extension = Module? MODULE_EXT : "";
435 char* Output = MakeFilename (FirstInput, Extension);
436 CmdSetOutput (&LD65, Output);
441 /* If we have a linker config file given, add it to the command line.
442 ** Otherwise pass the target to the linker if we have one.
446 Error ("Cannot use -C and --module together");
448 CmdAddArg2 (&LD65, "-C", LinkerConfig);
450 CmdSetTarget (&LD65, TGT_MODULE);
452 CmdSetTarget (&LD65, Target);
455 /* Determine which target libraries are needed */
458 /* Add all object files as parameters */
459 for (I = 0; I < LD65.FileCount; ++I) {
460 CmdAddArg (&LD65, LD65.Files [I]);
463 /* Add the system runtime library */
465 CmdAddArg (&LD65, TargetLib);
468 /* Terminate the argument list with a NULL pointer */
469 CmdAddArg (&LD65, 0);
471 /* Call the linker */
477 static void AssembleFile (const char* File, unsigned ArgCount)
478 /* Common routine to assemble a file. Will be called by Assemble() and
479 ** AssembleIntermediate(). Adds options common for both routines and
480 ** assembles the file. Will remove excess arguments after assembly.
483 /* Set the target system */
484 CmdSetTarget (&CA65, Target);
486 /* Check if this is the last processing step */
488 /* We're linking later. Add the output file of the assembly
489 ** the the file list of the linker. The name of the output
490 ** file is that of the input file with ".s" replaced by ".o".
492 char* ObjName = MakeFilename (File, ".o");
493 CmdAddFile (&LD65, ObjName);
496 /* This is the final step. If an output name is given, set it */
498 CmdSetOutput (&CA65, OutputName);
502 /* Add the file as argument for the assembler */
503 CmdAddArg (&CA65, File);
505 /* Add a NULL pointer to terminate the argument list */
506 CmdAddArg (&CA65, 0);
508 /* Run the assembler */
511 /* Remove the excess arguments */
512 CmdDelArgs (&CA65, ArgCount);
517 static void AssembleIntermediate (const char* SourceFile)
518 /* Assemble an intermediate file which was generated by a previous processing
519 ** step with SourceFile as input. The -dep options won't be added and
520 ** the intermediate assembler file is removed after assembly.
523 /* Generate the name of the assembler output file from the source file
524 ** name. It's the same name with the extension replaced by ".s"
526 char* AsmName = MakeFilename (SourceFile, ".s");
528 /* Assemble the intermediate assembler file */
529 AssembleFile (AsmName, CA65.ArgCount);
531 /* Remove the input file */
532 if (remove (AsmName) < 0) {
533 Warning ("Cannot remove temporary file `%s': %s",
534 AsmName, strerror (errno));
537 /* Free the assembler file name which was allocated from the heap */
543 static void Assemble (const char* File)
544 /* Assemble the given file */
546 /* Remember the current assembler argument count */
547 unsigned ArgCount = CA65.ArgCount;
549 /* We aren't assembling an intermediate file, but one requested by the
550 ** user. So add a few options here if they were given on the command
553 if (DepName && *DepName) {
554 CmdAddArg2 (&CA65, "--create-dep", DepName);
556 if (FullDepName && *FullDepName) {
557 CmdAddArg2 (&CA65, "--create-full-dep", FullDepName);
560 /* Use the common routine */
561 AssembleFile (File, ArgCount);
566 static void Compile (const char* File)
567 /* Compile the given file */
569 /* Remember the current compiler argument count */
570 unsigned ArgCount = CC65.ArgCount;
572 /* Set the target system */
573 CmdSetTarget (&CC65, Target);
575 /* Check if this is the final step */
577 /* We will assemble this file later. If a dependency file is to be
578 ** generated, set the dependency target to be the final object file,
579 ** not the intermediate assembler file. But beware: There may be an
580 ** output name specified for the assembler.
582 if (DepName || FullDepName) {
583 /* Was an output name for the assembler specified? */
584 if (!DoLink && OutputName) {
585 /* Use this name as the dependency target */
586 CmdAddArg2 (&CC65, "--dep-target", OutputName);
588 /* Use the object file name as the dependency target */
589 char* ObjName = MakeFilename (File, ".o");
590 CmdAddArg2 (&CC65, "--dep-target", ObjName);
595 /* If we won't assemble, this is the final step. In this case, set
596 ** the output name if it was given.
599 CmdSetOutput (&CC65, OutputName);
603 /* Add the file as argument for the compiler */
604 CmdAddArg (&CC65, File);
606 /* Add a NULL pointer to terminate the argument list */
607 CmdAddArg (&CC65, 0);
609 /* Run the compiler */
612 /* Remove the excess arguments */
613 CmdDelArgs (&CC65, ArgCount);
615 /* If this is not the final step, assemble the generated file, then
619 /* Assemble the intermediate file and remove it */
620 AssembleIntermediate (File);
626 static void CompileRes (const char* File)
627 /* Compile the given geos resource file */
629 /* Remember the current assembler argument count */
630 unsigned ArgCount = GRC.ArgCount;
632 /* Resource files need an geos-apple or geos-cbm target but this
633 ** is checked within grc65.
635 CmdSetTarget (&GRC, Target);
637 /* Add the file as argument for the resource compiler */
638 CmdAddArg (&GRC, File);
640 /* Add a NULL pointer to terminate the argument list */
643 /* Run the compiler */
646 /* Remove the excess arguments */
647 CmdDelArgs (&GRC, ArgCount);
649 /* If this is not the final step, assemble the generated file, then
653 /* Assemble the intermediate file and remove it */
654 AssembleIntermediate (File);
660 static void ConvertO65 (const char* File)
661 /* Convert an o65 object file into an assembler file */
663 /* Remember the current converter argument count */
664 unsigned ArgCount = CO65.ArgCount;
666 /* If we won't assemble, this is the final step. In this case, set the
669 if (!DoAssemble && OutputName) {
670 CmdSetOutput (&CO65, OutputName);
673 /* Add the file as argument for the object file converter */
674 CmdAddArg (&CO65, File);
676 /* Add a NULL pointer to terminate the argument list */
677 CmdAddArg (&CO65, 0);
679 /* Run the converter */
682 /* Remove the excess arguments */
683 CmdDelArgs (&CO65, ArgCount);
685 /* If this is not the final step, assemble the generated file, then
689 /* Assemble the intermediate file and remove it */
690 AssembleIntermediate (File);
696 /*****************************************************************************/
698 /*****************************************************************************/
702 static void Usage (void)
703 /* Print usage information and exit */
705 printf ("Usage: %s [options] file [...]\n"
707 " -c\t\t\t\tCompile and assemble, but don't link\n"
708 " -d\t\t\t\tDebug mode\n"
709 " -g\t\t\t\tAdd debug info\n"
710 " -h\t\t\t\tHelp (this text)\n"
711 " -l name\t\t\tCreate an assembler listing file\n"
712 " -m name\t\t\tCreate a map file\n"
713 " -mm model\t\t\tSet the memory model\n"
714 " -o name\t\t\tName the output file\n"
715 " -r\t\t\t\tEnable register variables\n"
716 " -t sys\t\t\tSet the target system\n"
717 " -u sym\t\t\tForce an import of symbol `sym'\n"
718 " -v\t\t\t\tVerbose mode\n"
719 " -vm\t\t\t\tVerbose map file\n"
720 " -C name\t\t\tUse linker config file\n"
721 " -Cl\t\t\t\tMake local variables static\n"
722 " -D sym[=defn]\t\t\tDefine a preprocessor symbol\n"
723 " -E Stop after the preprocessing stage\n"
724 " -I dir\t\t\tSet a compiler include directory path\n"
725 " -L path\t\t\tSpecify a library search path\n"
726 " -Ln name\t\t\tCreate a VICE label file\n"
727 " -O\t\t\t\tOptimize code\n"
728 " -Oi\t\t\t\tOptimize code, inline runtime functions\n"
729 " -Or\t\t\t\tOptimize code, honour the register keyword\n"
730 " -Os\t\t\t\tOptimize code, inline known C functions\n"
731 " -S\t\t\t\tCompile, but don't assemble and link\n"
732 " -T\t\t\t\tInclude source as comment\n"
733 " -V\t\t\t\tPrint the version number\n"
734 " -W name[,...]\t\t\tSuppress compiler warnings\n"
735 " -Wa options\t\t\tPass options to the assembler\n"
736 " -Wc options\t\t\tPass options to the compiler\n"
737 " -Wl options\t\t\tPass options to the linker\n"
740 " --add-source\t\t\tInclude source as comment\n"
741 " --asm-args options\t\tPass options to the assembler\n"
742 " --asm-define sym[=v]\t\tDefine an assembler symbol\n"
743 " --asm-include-dir dir\t\tSet an assembler include directory\n"
744 " --bin-include-dir dir\t\tSet an assembler binary include directory\n"
745 " --bss-label name\t\tDefine and export a BSS segment label\n"
746 " --bss-name seg\t\tSet the name of the BSS segment\n"
747 " --cc-args options\t\tPass options to the compiler\n"
748 " --cfg-path path\t\tSpecify a config file search path\n"
749 " --check-stack\t\t\tGenerate stack overflow checks\n"
750 " --code-label name\t\tDefine and export a CODE segment label\n"
751 " --code-name seg\t\tSet the name of the CODE segment\n"
752 " --codesize x\t\t\tAccept larger code by factor x\n"
753 " --config name\t\t\tUse linker config file\n"
754 " --cpu type\t\t\tSet CPU type\n"
755 " --create-dep name\t\tCreate a make dependency file\n"
756 " --create-full-dep name\tCreate a full make dependency file\n"
757 " --data-label name\t\tDefine and export a DATA segment label\n"
758 " --data-name seg\t\tSet the name of the DATA segment\n"
759 " --debug\t\t\tDebug mode\n"
760 " --debug-info\t\t\tAdd debug info\n"
761 " --feature name\t\tSet an emulation feature\n"
762 " --force-import sym\t\tForce an import of symbol `sym'\n"
763 " --help\t\t\tHelp (this text)\n"
764 " --include-dir dir\t\tSet a compiler include directory path\n"
765 " --ld-args options\t\tPass options to the linker\n"
766 " --lib file\t\t\tLink this library\n"
767 " --lib-path path\t\tSpecify a library search path\n"
768 " --list-targets\t\tList all available targets\n"
769 " --listing name\t\tCreate an assembler listing file\n"
770 " --list-bytes n\t\tNumber of bytes per assembler listing line\n"
771 " --mapfile name\t\tCreate a map file\n"
772 " --memory-model model\t\tSet the memory model\n"
773 " --module\t\t\tLink as a module\n"
774 " --module-id id\t\tSpecify a module ID for the linker\n"
775 " --o65-model model\t\tOverride the o65 model\n"
776 " --obj file\t\t\tLink this object file\n"
777 " --obj-path path\t\tSpecify an object file search path\n"
778 " --print-target-path\t\tPrint the target file path\n"
779 " --register-space b\t\tSet space available for register variables\n"
780 " --register-vars\t\tEnable register variables\n"
781 " --rodata-name seg\t\tSet the name of the RODATA segment\n"
782 " --signed-chars\t\tDefault characters are signed\n"
783 " --standard std\t\tLanguage standard (c89, c99, cc65)\n"
784 " --start-addr addr\t\tSet the default start address\n"
785 " --static-locals\t\tMake local variables static\n"
786 " --target sys\t\t\tSet the target system\n"
787 " --version\t\t\tPrint the version number\n"
788 " --verbose\t\t\tVerbose mode\n"
789 " --zeropage-label name\t\tDefine and export a ZEROPAGE segment label\n"
790 " --zeropage-name seg\t\tSet the name of the ZEROPAGE segment\n",
796 static void OptAddSource (const char* Opt attribute ((unused)),
797 const char* Arg attribute ((unused)))
798 /* Strict source code as comments to the generated asm code */
800 CmdAddArg (&CC65, "-T");
805 static void OptAsmArgs (const char* Opt attribute ((unused)), const char* Arg)
806 /* Pass arguments to the assembler */
808 CmdAddArgList (&CA65, Arg);
813 static void OptAsmDefine (const char* Opt attribute ((unused)), const char* Arg)
814 /* Define an assembler symbol (assembler) */
816 CmdAddArg2 (&CA65, "-D", Arg);
821 static void OptAsmIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
822 /* Include directory (assembler) */
824 CmdAddArg2 (&CA65, "-I", Arg);
829 static void OptBinIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
830 /* Binary include directory (assembler) */
832 CmdAddArg2 (&CA65, "--bin-include-dir", Arg);
837 static void OptBssLabel (const char* Opt attribute ((unused)), const char* Arg)
838 /* Handle the --bss-label option */
840 CmdAddArg2 (&CO65, "--bss-label", Arg);
845 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
846 /* Handle the --bss-name option */
848 CmdAddArg2 (&CC65, "--bss-name", Arg);
849 CmdAddArg2 (&CO65, "--bss-name", Arg);
854 static void OptCCArgs (const char* Opt attribute ((unused)), const char* Arg)
855 /* Pass arguments to the compiler */
857 CmdAddArgList (&CC65, Arg);
862 static void OptCfgPath (const char* Opt attribute ((unused)), const char* Arg)
863 /* Config file search path (linker) */
865 CmdAddArg2 (&LD65, "--cfg-path", Arg);
870 static void OptCheckStack (const char* Opt attribute ((unused)),
871 const char* Arg attribute ((unused)))
872 /* Handle the --check-stack option */
874 CmdAddArg (&CC65, "--check-stack");
879 static void OptCodeLabel (const char* Opt attribute ((unused)), const char* Arg)
880 /* Handle the --code-label option */
882 CmdAddArg2 (&CO65, "--code-label", Arg);
887 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
888 /* Handle the --code-name option */
890 CmdAddArg2 (&CC65, "--code-name", Arg);
891 CmdAddArg2 (&CO65, "--code-name", Arg);
896 static void OptCodeSize (const char* Opt attribute ((unused)), const char* Arg)
897 /* Handle the --codesize option */
899 CmdAddArg2 (&CC65, "--codesize", Arg);
904 static void OptConfig (const char* Opt attribute ((unused)), const char* Arg)
905 /* Config file (linker) */
908 Error ("Cannot specify -C/--config twice");
915 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
916 /* Handle the --cpu option */
918 /* Add the cpu type to the assembler and compiler */
919 CmdAddArg2 (&CA65, "--cpu", Arg);
920 CmdAddArg2 (&CC65, "--cpu", Arg);
925 static void OptCreateDep (const char* Opt attribute ((unused)), const char* Arg)
926 /* Handle the --create-dep option */
928 /* Add the file name to the compiler */
929 CmdAddArg2 (&CC65, "--create-dep", Arg);
931 /* Remember the file name for the assembler */
937 static void OptCreateFullDep (const char* Opt attribute ((unused)), const char* Arg)
938 /* Handle the --create-full-dep option */
940 /* Add the file name to the compiler */
941 CmdAddArg2 (&CC65, "--create-full-dep", Arg);
943 /* Remember the file name for the assembler */
949 static void OptDataLabel (const char* Opt attribute ((unused)), const char* Arg)
950 /* Handle the --data-label option */
952 CmdAddArg2 (&CO65, "--data-label", Arg);
957 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
958 /* Handle the --data-name option */
960 CmdAddArg2 (&CC65, "--data-name", Arg);
961 CmdAddArg2 (&CO65, "--data-name", Arg);
966 static void OptDebug (const char* Opt attribute ((unused)),
967 const char* Arg attribute ((unused)))
968 /* Debug mode (compiler and cl65 utility) */
970 CmdAddArg (&CC65, "-d");
971 CmdAddArg (&CO65, "-d");
977 static void OptDebugInfo (const char* Opt attribute ((unused)),
978 const char* Arg attribute ((unused)))
979 /* Debug Info - add to compiler and assembler */
981 CmdAddArg (&CC65, "-g");
982 CmdAddArg (&CA65, "-g");
983 CmdAddArg (&CO65, "-g");
988 static void OptFeature (const char* Opt attribute ((unused)), const char* Arg)
989 /* Emulation features for the assembler */
991 CmdAddArg2 (&CA65, "--feature", Arg);
996 static void OptForceImport (const char* Opt attribute ((unused)), const char* Arg)
997 /* Emulation features for the assembler */
999 CmdAddArg2 (&LD65, "-u", Arg);
1004 static void OptHelp (const char* Opt attribute ((unused)),
1005 const char* Arg attribute ((unused)))
1006 /* Print help - cl65 */
1009 exit (EXIT_SUCCESS);
1014 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
1015 /* Include directory (compiler) */
1017 CmdAddArg2 (&CC65, "-I", Arg);
1022 static void OptLdArgs (const char* Opt attribute ((unused)), const char* Arg)
1023 /* Pass arguments to the linker */
1025 CmdAddArgList (&LD65, Arg);
1030 static void OptLib (const char* Opt attribute ((unused)), const char* Arg)
1031 /* Library file follows (linker) */
1033 CmdAddArg2 (&LD65, "--lib", Arg);
1038 static void OptLibPath (const char* Opt attribute ((unused)), const char* Arg)
1039 /* Library search path (linker) */
1041 CmdAddArg2 (&LD65, "--lib-path", Arg);
1046 static void OptListBytes (const char* Opt attribute ((unused)), const char* Arg)
1047 /* Set the maximum number of bytes per asm listing line */
1049 CmdAddArg2 (&CA65, "--list-bytes", Arg);
1054 static void OptListing (const char* Opt attribute ((unused)), const char* Arg)
1055 /* Create an assembler listing */
1057 CmdAddArg2 (&CA65, "-l", Arg);
1062 static void OptListTargets (const char* Opt attribute ((unused)),
1063 const char* Arg attribute ((unused)))
1064 /* List all targets */
1068 /* List the targets */
1069 for (T = TGT_NONE; T < TGT_COUNT; ++T) {
1070 printf ("%s\n", GetTargetName (T));
1074 exit (EXIT_SUCCESS);
1079 static void OptMapFile (const char* Opt attribute ((unused)), const char* Arg)
1080 /* Create a map file */
1082 /* Create a map file (linker) */
1083 CmdAddArg2 (&LD65, "-m", Arg);
1088 static void OptMemoryModel (const char* Opt attribute ((unused)), const char* Arg)
1089 /* Set the memory model */
1091 mmodel_t MemoryModel = FindMemoryModel (Arg);
1092 if (MemoryModel == MMODEL_UNKNOWN) {
1093 Error ("Unknown memory model: %s", Arg);
1094 } else if (MemoryModel == MMODEL_HUGE) {
1095 Error ("Unsupported memory model: %s", Arg);
1097 CmdAddArg2 (&CA65, "-mm", Arg);
1098 CmdAddArg2 (&CC65, "-mm", Arg);
1104 static void OptModule (const char* Opt attribute ((unused)),
1105 const char* Arg attribute ((unused)))
1106 /* Link as a module */
1113 static void OptModuleId (const char* Opt attribute ((unused)), const char* Arg)
1114 /* Specify a module if for the linker */
1116 /* Pass it straight to the linker */
1117 CmdAddArg2 (&LD65, "--module-id", Arg);
1122 static void OptO65Model (const char* Opt attribute ((unused)), const char* Arg)
1123 /* Handle the --o65-model option */
1125 CmdAddArg2 (&CO65, "-m", Arg);
1130 static void OptObj (const char* Opt attribute ((unused)), const char* Arg)
1131 /* Object file follows (linker) */
1133 CmdAddArg2 (&LD65, "--obj", Arg);
1138 static void OptObjPath (const char* Opt attribute ((unused)), const char* Arg)
1139 /* Object file search path (linker) */
1141 CmdAddArg2 (&LD65, "--obj-path", Arg);
1146 static void OptPrintTargetPath (const char* Opt attribute ((unused)),
1147 const char* Arg attribute ((unused)))
1148 /* Print the target file path */
1150 SearchPaths* TargetPath = NewSearchPath ();
1151 AddSubSearchPathFromEnv (TargetPath, "CC65_HOME", "target");
1152 #if defined(CL65_TGT) && !defined(_WIN32)
1153 AddSearchPath (TargetPath, STRINGIZE (CL65_TGT));
1155 AddSubSearchPathFromWinBin (TargetPath, "target");
1157 printf ("%s\n", GetSearchPath (TargetPath, 0));
1158 exit (EXIT_SUCCESS);
1163 static void OptRegisterSpace (const char* Opt attribute ((unused)), const char* Arg)
1164 /* Handle the --register-space option */
1166 CmdAddArg2 (&CC65, "--register-space", Arg);
1171 static void OptRegisterVars (const char* Opt attribute ((unused)),
1172 const char* Arg attribute ((unused)))
1173 /* Handle the --register-vars option */
1175 CmdAddArg (&CC65, "-r");
1180 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
1181 /* Handle the --rodata-name option */
1183 CmdAddArg2 (&CC65, "--rodata-name", Arg);
1188 static void OptSignedChars (const char* Opt attribute ((unused)),
1189 const char* Arg attribute ((unused)))
1190 /* Make default characters signed */
1192 CmdAddArg (&CC65, "-j");
1197 static void OptStandard (const char* Opt attribute ((unused)), const char* Arg)
1198 /* Set the language standard */
1200 CmdAddArg2 (&CC65, "--standard", Arg);
1205 static void OptStartAddr (const char* Opt attribute ((unused)), const char* Arg)
1206 /* Set the default start address */
1208 CmdAddArg2 (&LD65, "-S", Arg);
1213 static void OptStaticLocals (const char* Opt attribute ((unused)),
1214 const char* Arg attribute ((unused)))
1215 /* Place local variables in static storage */
1217 CmdAddArg (&CC65, "-Cl");
1222 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
1223 /* Set the target system */
1225 Target = FindTarget (Arg);
1226 if (Target == TGT_UNKNOWN) {
1227 Error ("No such target system: `%s'", Arg);
1228 } else if (Target == TGT_MODULE) {
1229 Error ("Cannot use `module' as target, use --module instead");
1235 static void OptVerbose (const char* Opt attribute ((unused)),
1236 const char* Arg attribute ((unused)))
1237 /* Verbose mode (compiler, assembler, linker) */
1239 CmdAddArg (&CC65, "-v");
1240 CmdAddArg (&CA65, "-v");
1241 CmdAddArg (&CO65, "-v");
1242 CmdAddArg (&LD65, "-v");
1247 static void OptVersion (const char* Opt attribute ((unused)),
1248 const char* Arg attribute ((unused)))
1249 /* Print version number */
1251 fprintf (stderr, "%s V%s\n", ProgName, GetVersionAsString ());
1257 static void OptZeropageLabel (const char* Opt attribute ((unused)), const char* Arg)
1258 /* Handle the --zeropage-label option */
1260 CmdAddArg2 (&CO65, "--zeropage-label", Arg);
1265 static void OptZeropageName (const char* Opt attribute ((unused)), const char* Arg)
1266 /* Handle the --zeropage-name option */
1268 CmdAddArg2 (&CO65, "--zeropage-name", Arg);
1273 int main (int argc, char* argv [])
1274 /* Utility main program */
1276 /* Program long options */
1277 static const LongOpt OptTab[] = {
1278 { "--add-source", 0, OptAddSource },
1279 { "--asm-args", 1, OptAsmArgs },
1280 { "--asm-define", 1, OptAsmDefine },
1281 { "--asm-include-dir", 1, OptAsmIncludeDir },
1282 { "--bin-include-dir", 1, OptBinIncludeDir },
1283 { "--bss-label", 1, OptBssLabel },
1284 { "--bss-name", 1, OptBssName },
1285 { "--cc-args", 1, OptCCArgs },
1286 { "--cfg-path", 1, OptCfgPath },
1287 { "--check-stack", 0, OptCheckStack },
1288 { "--code-label", 1, OptCodeLabel },
1289 { "--code-name", 1, OptCodeName },
1290 { "--codesize", 1, OptCodeSize },
1291 { "--config", 1, OptConfig },
1292 { "--cpu", 1, OptCPU },
1293 { "--create-dep", 1, OptCreateDep },
1294 { "--create-full-dep", 1, OptCreateFullDep },
1295 { "--data-label", 1, OptDataLabel },
1296 { "--data-name", 1, OptDataName },
1297 { "--debug", 0, OptDebug },
1298 { "--debug-info", 0, OptDebugInfo },
1299 { "--feature", 1, OptFeature },
1300 { "--force-import", 1, OptForceImport },
1301 { "--help", 0, OptHelp },
1302 { "--include-dir", 1, OptIncludeDir },
1303 { "--ld-args", 1, OptLdArgs },
1304 { "--lib", 1, OptLib },
1305 { "--lib-path", 1, OptLibPath },
1306 { "--list-targets", 0, OptListTargets },
1307 { "--listing", 1, OptListing },
1308 { "--list-bytes", 1, OptListBytes },
1309 { "--mapfile", 1, OptMapFile },
1310 { "--memory-model", 1, OptMemoryModel },
1311 { "--module", 0, OptModule },
1312 { "--module-id", 1, OptModuleId },
1313 { "--o65-model", 1, OptO65Model },
1314 { "--obj", 1, OptObj },
1315 { "--obj-path", 1, OptObjPath },
1316 { "--print-target-path", 0, OptPrintTargetPath},
1317 { "--register-space", 1, OptRegisterSpace },
1318 { "--register-vars", 0, OptRegisterVars },
1319 { "--rodata-name", 1, OptRodataName },
1320 { "--signed-chars", 0, OptSignedChars },
1321 { "--standard", 1, OptStandard },
1322 { "--start-addr", 1, OptStartAddr },
1323 { "--static-locals", 0, OptStaticLocals },
1324 { "--target", 1, OptTarget },
1325 { "--verbose", 0, OptVerbose },
1326 { "--version", 0, OptVersion },
1327 { "--zeropage-label", 1, OptZeropageLabel },
1328 { "--zeropage-name", 1, OptZeropageName },
1334 /* Initialize the cmdline module */
1335 InitCmdLine (&argc, &argv, "cl65");
1337 /* Initialize the command descriptors */
1339 CmdPath = xstrdup ("");
1342 CmdPath = xstrdup (argv[0]);
1343 Ptr = strrchr (CmdPath, '/');
1345 Ptr = strrchr (CmdPath, '\\');
1353 CmdInit (&CC65, CmdPath, "cc65");
1354 CmdInit (&CA65, CmdPath, "ca65");
1355 CmdInit (&CO65, CmdPath, "co65");
1356 CmdInit (&LD65, CmdPath, "ld65");
1357 CmdInit (&GRC, CmdPath, "grc65");
1360 /* Our default target is the C64 instead of "none" */
1363 /* Check the parameters */
1365 while (I < ArgCount) {
1367 /* Get the argument */
1368 const char* Arg = ArgVec[I];
1370 /* Check for an option */
1371 if (Arg [0] == '-') {
1376 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
1380 if (Arg[2] == 'l' && Arg[3] == '\0') {
1381 /* Make local variables static */
1382 OptStaticLocals (Arg, 0);
1384 /* Specify linker config file */
1385 OptConfig (Arg, GetArg (&I, 2));
1390 /* Define a preprocessor symbol (compiler) */
1391 CmdAddArg2 (&CC65, "-D", GetArg (&I, 2));
1395 /* Include directory (compiler) */
1396 OptIncludeDir (Arg, GetArg (&I, 2));
1400 if (Arg[2] == 'n' && Arg[3] == '\0') {
1401 /* VICE label file (linker) */
1402 CmdAddArg2 (&LD65, "-Ln", GetArg (&I, 3));
1404 /* Library search path (linker) */
1405 OptLibPath (Arg, GetArg (&I, 2));
1410 /* Optimize code (compiler, also covers -Oi and others) */
1411 CmdAddArg (&CC65, Arg);
1415 /* Dont assemble and link the created files */
1416 DisableAssemblingAndLinking();
1420 /* Include source as comment (compiler) */
1421 OptAddSource (Arg, 0);
1425 /* Print version number */
1426 OptVersion (Arg, 0);
1430 /*Forward -E to compiler*/
1431 CmdAddArg (&CC65, Arg);
1432 DisableAssemblingAndLinking();
1435 if (Arg[2] == 'a' && Arg[3] == '\0') {
1436 /* -Wa: Pass options to assembler */
1437 OptAsmArgs (Arg, GetArg (&I, 3));
1438 } else if (Arg[2] == 'c' && Arg[3] == '\0') {
1439 /* -Wc: Pass options to compiler */
1440 /* Remember -Wc sub arguments in cc65 arg struct */
1441 OptCCArgs (Arg, GetArg (&I, 3));
1442 } else if (Arg[2] == 'l' && Arg[3] == '\0') {
1443 /* -Wl: Pass options to linker */
1444 OptLdArgs (Arg, GetArg (&I, 3));
1446 /* Anything else: Suppress warnings (compiler) */
1447 CmdAddArg2 (&CC65, "-W", GetArg (&I, 2));
1452 /* Don't link the resulting files */
1457 /* Debug mode (compiler) */
1462 /* Debugging - add to compiler and assembler */
1463 OptDebugInfo (Arg, 0);
1468 /* Print help - cl65 */
1473 /* Default characters are signed */
1474 OptSignedChars (Arg, 0);
1478 /* Create an assembler listing */
1479 OptListing (Arg, GetArg (&I, 2));
1483 /* Create a map file (linker) */
1484 OptMapFile (Arg, GetArg (&I, 2));
1488 /* Name the output file */
1489 OutputName = GetArg (&I, 2);
1493 /* Enable register variables */
1494 OptRegisterVars (Arg, 0);
1498 /* Set target system - compiler, assembler and linker */
1499 OptTarget (Arg, GetArg (&I, 2));
1503 /* Force an import (linker) */
1504 OptForceImport (Arg, GetArg (&I, 2));
1508 if (Arg [2] == 'm') {
1509 /* Verbose map file (linker) */
1510 CmdAddArg (&LD65, "-vm");
1512 /* Verbose mode (compiler, assembler, linker) */
1513 OptVerbose (Arg, 0);
1518 UnknownOption (Arg);
1522 /* Remember the first file name */
1523 if (FirstInput == 0) {
1527 /* Determine the file type by the extension */
1528 switch (GetFileType (Arg)) {
1531 /* Compile the file */
1536 /* Assemble the file */
1544 /* Add to the linker files */
1545 CmdAddFile (&LD65, Arg);
1549 /* Add to the resource compiler files */
1554 /* Add the the object file converter files */
1559 Error ("Don't know what to do with `%s'", Arg);
1569 /* Check if we had any input files */
1570 if (FirstInput == 0) {
1571 Warning ("No input files");
1574 /* Link the given files if requested and if we have any */
1575 if (DoLink && LD65.FileCount > 0) {
1579 /* Return an apropriate exit code */
1580 return EXIT_SUCCESS;