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 /* Command structure handling */
163 /*****************************************************************************/
167 static void CmdExpand (CmdDesc* Cmd)
168 /* Expand the argument vector */
170 unsigned NewMax = Cmd->ArgMax + 10;
171 char** NewArgs = xmalloc (NewMax * sizeof (char*));
172 memcpy (NewArgs, Cmd->Args, Cmd->ArgMax * sizeof (char*));
175 Cmd->ArgMax = NewMax;
180 static void CmdAddArg (CmdDesc* Cmd, const char* Arg)
181 /* Add a new argument to the command */
183 /* Expand the argument vector if needed */
184 if (Cmd->ArgCount >= Cmd->ArgMax) {
188 /* Add a copy of the new argument, allow a NULL pointer */
190 Cmd->Args[Cmd->ArgCount++] = xstrdup (Arg);
192 Cmd->Args[Cmd->ArgCount++] = 0;
198 static void CmdAddArg2 (CmdDesc* Cmd, const char* Arg1, const char* Arg2)
199 /* Add a new argument pair to the command */
201 CmdAddArg (Cmd, Arg1);
202 CmdAddArg (Cmd, Arg2);
207 static void CmdAddArgList (CmdDesc* Cmd, const char* ArgList)
208 /* Add a list of arguments separated by commas */
210 const char* Arg = ArgList;
214 if (*P == '\0' || *P == ',') {
216 /* End of argument, add it */
217 unsigned Len = P - Arg;
219 /* Expand the argument vector if needed */
220 if (Cmd->ArgCount >= Cmd->ArgMax) {
224 /* Add the new argument */
225 Cmd->Args[Cmd->ArgCount] = memcpy (xmalloc (Len + 1), Arg, Len);
226 Cmd->Args[Cmd->ArgCount][Len] = '\0';
229 /* If the argument was terminated by a comma, skip it, otherwise
233 /* Start over at next char */
239 /* Skip other chars */
248 static void CmdDelArgs (CmdDesc* Cmd, unsigned LastValid)
249 /* Remove all arguments with an index greater than LastValid */
251 while (Cmd->ArgCount > LastValid) {
253 xfree (Cmd->Args [Cmd->ArgCount]);
254 Cmd->Args [Cmd->ArgCount] = 0;
260 static void CmdAddFile (CmdDesc* Cmd, const char* File)
261 /* Add a new file to the command */
263 /* Expand the file vector if needed */
264 if (Cmd->FileCount == Cmd->FileMax) {
265 unsigned NewMax = Cmd->FileMax + 10;
266 char** NewFiles = xmalloc (NewMax * sizeof (char*));
267 memcpy (NewFiles, Cmd->Files, Cmd->FileMax * sizeof (char*));
269 Cmd->Files = NewFiles;
270 Cmd->FileMax = NewMax;
273 /* If the file name is not NULL (which is legal and is used to terminate
274 ** the file list), check if the file name does already exist in the file
275 ** list and print a warning if so. Regardless of the search result, add
280 for (I = 0; I < Cmd->FileCount; ++I) {
281 if (strcmp (Cmd->Files[I], File) == 0) {
283 Warning ("Duplicate file in argument list: `%s'", File);
284 /* No need to search further */
290 Cmd->Files [Cmd->FileCount++] = xstrdup (File);
292 /* Add a NULL pointer */
293 Cmd->Files [Cmd->FileCount++] = 0;
299 static void CmdInit (CmdDesc* Cmd, const char* Path, const char* Name)
300 /* Initialize the command using the given path and name of the executable */
304 FullName = (char*) xmalloc (strlen (Path) + strlen (Name) + 1);
305 strcpy (FullName, Path);
306 strcat (FullName, Name);
308 /* Remember the command */
309 Cmd->Name = xstrdup (FullName);
311 /* Use the command name as first argument */
312 CmdAddArg (Cmd, FullName);
319 static void CmdSetOutput (CmdDesc* Cmd, const char* File)
320 /* Set the output file in a command desc */
322 CmdAddArg2 (Cmd, "-o", File);
327 static void CmdSetTarget (CmdDesc* Cmd, target_t Target)
328 /* Set the output file in a command desc */
330 CmdAddArg2 (Cmd, "-t", GetTargetName (Target));
335 static void CmdPrint (CmdDesc* Cmd, FILE* F)
336 /* Output the command line encoded in the command desc */
339 for (I = 0; I < Cmd->ArgCount && Cmd->Args[I] != 0; ++I) {
340 fprintf (F, "%s ", Cmd->Args[I]);
346 /*****************************************************************************/
347 /* Target handling */
348 /*****************************************************************************/
352 static void SetTargetFiles (void)
353 /* Set the target system files */
355 /* Determine the names of the target specific library file */
356 if (Target != TGT_NONE) {
358 /* Get a pointer to the system name and its length */
359 const char* TargetName = GetTargetName (Target);
360 unsigned TargetNameLen = strlen (TargetName);
362 /* Set the library file */
363 TargetLib = xmalloc (TargetNameLen + 4 + 1);
364 memcpy (TargetLib, TargetName, TargetNameLen);
365 strcpy (TargetLib + TargetNameLen, ".lib");
372 /*****************************************************************************/
374 /*****************************************************************************/
378 static void ExecProgram (CmdDesc* Cmd)
379 /* Execute a subprocess with the given name/parameters. Exit on errors. */
383 /* If in debug mode, output the command line we will execute */
385 printf ("Executing: ");
386 CmdPrint (Cmd, stdout);
390 /* Call the program */
391 Status = spawnvp (P_WAIT, Cmd->Name, SPAWN_ARGV_CONST_CAST Cmd->Args);
393 /* Check the result code */
395 /* Error executing the program */
396 Error ("Cannot execute `%s': %s", Cmd->Name, strerror (errno));
397 } else if (Status != 0) {
398 /* Called program had an error */
405 static void Link (void)
406 /* Link the resulting executable */
410 /* Since linking is always the final step, if we have an output file name
411 ** given, set it here. If we don't have an explicit output name given,
412 ** try to build one from the name of the first input file.
416 CmdSetOutput (&LD65, OutputName);
418 } else if (FirstInput && FindExt (FirstInput)) { /* Only if ext present! */
420 const char* Extension = Module? MODULE_EXT : "";
421 char* Output = MakeFilename (FirstInput, Extension);
422 CmdSetOutput (&LD65, Output);
427 /* If we have a linker config file given, add it to the command line.
428 ** Otherwise pass the target to the linker if we have one.
432 Error ("Cannot use -C and --module together");
434 CmdAddArg2 (&LD65, "-C", LinkerConfig);
436 CmdSetTarget (&LD65, TGT_MODULE);
438 CmdSetTarget (&LD65, Target);
441 /* Determine which target libraries are needed */
444 /* Add all object files as parameters */
445 for (I = 0; I < LD65.FileCount; ++I) {
446 CmdAddArg (&LD65, LD65.Files [I]);
449 /* Add the system runtime library */
451 CmdAddArg (&LD65, TargetLib);
454 /* Terminate the argument list with a NULL pointer */
455 CmdAddArg (&LD65, 0);
457 /* Call the linker */
463 static void AssembleFile (const char* File, unsigned ArgCount)
464 /* Common routine to assemble a file. Will be called by Assemble() and
465 ** AssembleIntermediate(). Adds options common for both routines and
466 ** assembles the file. Will remove excess arguments after assembly.
469 /* Set the target system */
470 CmdSetTarget (&CA65, Target);
472 /* Check if this is the last processing step */
474 /* We're linking later. Add the output file of the assembly
475 ** the the file list of the linker. The name of the output
476 ** file is that of the input file with ".s" replaced by ".o".
478 char* ObjName = MakeFilename (File, ".o");
479 CmdAddFile (&LD65, ObjName);
482 /* This is the final step. If an output name is given, set it */
484 CmdSetOutput (&CA65, OutputName);
488 /* Add the file as argument for the assembler */
489 CmdAddArg (&CA65, File);
491 /* Add a NULL pointer to terminate the argument list */
492 CmdAddArg (&CA65, 0);
494 /* Run the assembler */
497 /* Remove the excess arguments */
498 CmdDelArgs (&CA65, ArgCount);
503 static void AssembleIntermediate (const char* SourceFile)
504 /* Assemble an intermediate file which was generated by a previous processing
505 ** step with SourceFile as input. The -dep options won't be added and
506 ** the intermediate assembler file is removed after assembly.
509 /* Generate the name of the assembler output file from the source file
510 ** name. It's the same name with the extension replaced by ".s"
512 char* AsmName = MakeFilename (SourceFile, ".s");
514 /* Assemble the intermediate assembler file */
515 AssembleFile (AsmName, CA65.ArgCount);
517 /* Remove the input file */
518 if (remove (AsmName) < 0) {
519 Warning ("Cannot remove temporary file `%s': %s",
520 AsmName, strerror (errno));
523 /* Free the assembler file name which was allocated from the heap */
529 static void Assemble (const char* File)
530 /* Assemble the given file */
532 /* Remember the current assembler argument count */
533 unsigned ArgCount = CA65.ArgCount;
535 /* We aren't assembling an intermediate file, but one requested by the
536 ** user. So add a few options here if they were given on the command
539 if (DepName && *DepName) {
540 CmdAddArg2 (&CA65, "--create-dep", DepName);
542 if (FullDepName && *FullDepName) {
543 CmdAddArg2 (&CA65, "--create-full-dep", FullDepName);
546 /* Use the common routine */
547 AssembleFile (File, ArgCount);
552 static void Compile (const char* File)
553 /* Compile the given file */
555 /* Remember the current compiler argument count */
556 unsigned ArgCount = CC65.ArgCount;
558 /* Set the target system */
559 CmdSetTarget (&CC65, Target);
561 /* Check if this is the final step */
563 /* We will assemble this file later. If a dependency file is to be
564 ** generated, set the dependency target to be the final object file,
565 ** not the intermediate assembler file. But beware: There may be an
566 ** output name specified for the assembler.
568 if (DepName || FullDepName) {
569 /* Was an output name for the assembler specified? */
570 if (!DoLink && OutputName) {
571 /* Use this name as the dependency target */
572 CmdAddArg2 (&CC65, "--dep-target", OutputName);
574 /* Use the object file name as the dependency target */
575 char* ObjName = MakeFilename (File, ".o");
576 CmdAddArg2 (&CC65, "--dep-target", ObjName);
581 /* If we won't assemble, this is the final step. In this case, set
582 ** the output name if it was given.
585 CmdSetOutput (&CC65, OutputName);
589 /* Add the file as argument for the compiler */
590 CmdAddArg (&CC65, File);
592 /* Add a NULL pointer to terminate the argument list */
593 CmdAddArg (&CC65, 0);
595 /* Run the compiler */
598 /* Remove the excess arguments */
599 CmdDelArgs (&CC65, ArgCount);
601 /* If this is not the final step, assemble the generated file, then
605 /* Assemble the intermediate file and remove it */
606 AssembleIntermediate (File);
612 static void CompileRes (const char* File)
613 /* Compile the given geos resource file */
615 /* Remember the current assembler argument count */
616 unsigned ArgCount = GRC.ArgCount;
618 /* Resource files need an geos-apple or geos-cbm target but this
619 ** is checked within grc65.
621 CmdSetTarget (&GRC, Target);
623 /* Add the file as argument for the resource compiler */
624 CmdAddArg (&GRC, File);
626 /* Add a NULL pointer to terminate the argument list */
629 /* Run the compiler */
632 /* Remove the excess arguments */
633 CmdDelArgs (&GRC, ArgCount);
635 /* If this is not the final step, assemble the generated file, then
639 /* Assemble the intermediate file and remove it */
640 AssembleIntermediate (File);
646 static void ConvertO65 (const char* File)
647 /* Convert an o65 object file into an assembler file */
649 /* Remember the current converter argument count */
650 unsigned ArgCount = CO65.ArgCount;
652 /* If we won't assemble, this is the final step. In this case, set the
655 if (!DoAssemble && OutputName) {
656 CmdSetOutput (&CO65, OutputName);
659 /* Add the file as argument for the object file converter */
660 CmdAddArg (&CO65, File);
662 /* Add a NULL pointer to terminate the argument list */
663 CmdAddArg (&CO65, 0);
665 /* Run the converter */
668 /* Remove the excess arguments */
669 CmdDelArgs (&CO65, ArgCount);
671 /* If this is not the final step, assemble the generated file, then
675 /* Assemble the intermediate file and remove it */
676 AssembleIntermediate (File);
682 /*****************************************************************************/
684 /*****************************************************************************/
688 static void Usage (void)
689 /* Print usage information and exit */
691 printf ("Usage: %s [options] file [...]\n"
693 " -c\t\t\t\tCompile and assemble, but don't link\n"
694 " -d\t\t\t\tDebug mode\n"
695 " -g\t\t\t\tAdd debug info\n"
696 " -h\t\t\t\tHelp (this text)\n"
697 " -l name\t\t\tCreate an assembler listing file\n"
698 " -m name\t\t\tCreate a map file\n"
699 " -mm model\t\t\tSet the memory model\n"
700 " -o name\t\t\tName the output file\n"
701 " -r\t\t\t\tEnable register variables\n"
702 " -t sys\t\t\tSet the target system\n"
703 " -u sym\t\t\tForce an import of symbol `sym'\n"
704 " -v\t\t\t\tVerbose mode\n"
705 " -vm\t\t\t\tVerbose map file\n"
706 " -C name\t\t\tUse linker config file\n"
707 " -Cl\t\t\t\tMake local variables static\n"
708 " -D sym[=defn]\t\t\tDefine a preprocessor symbol\n"
709 " -E Stop after the preprocessing stage\n"
710 " -I dir\t\t\tSet a compiler include directory path\n"
711 " -L path\t\t\tSpecify a library search path\n"
712 " -Ln name\t\t\tCreate a VICE label file\n"
713 " -O\t\t\t\tOptimize code\n"
714 " -Oi\t\t\t\tOptimize code, inline runtime functions\n"
715 " -Or\t\t\t\tOptimize code, honour the register keyword\n"
716 " -Os\t\t\t\tOptimize code, inline known C functions\n"
717 " -S\t\t\t\tCompile, but don't assemble and link\n"
718 " -T\t\t\t\tInclude source as comment\n"
719 " -V\t\t\t\tPrint the version number\n"
720 " -W name[,...]\t\t\tSuppress compiler warnings\n"
721 " -Wa options\t\t\tPass options to the assembler\n"
722 " -Wc options\t\t\tPass options to the compiler\n"
723 " -Wl options\t\t\tPass options to the linker\n"
726 " --add-source\t\t\tInclude source as comment\n"
727 " --asm-args options\t\tPass options to the assembler\n"
728 " --asm-define sym[=v]\t\tDefine an assembler symbol\n"
729 " --asm-include-dir dir\t\tSet an assembler include directory\n"
730 " --bin-include-dir dir\t\tSet an assembler binary include directory\n"
731 " --bss-label name\t\tDefine and export a BSS segment label\n"
732 " --bss-name seg\t\tSet the name of the BSS segment\n"
733 " --cc-args options\t\tPass options to the compiler\n"
734 " --cfg-path path\t\tSpecify a config file search path\n"
735 " --check-stack\t\t\tGenerate stack overflow checks\n"
736 " --code-label name\t\tDefine and export a CODE segment label\n"
737 " --code-name seg\t\tSet the name of the CODE segment\n"
738 " --codesize x\t\t\tAccept larger code by factor x\n"
739 " --config name\t\t\tUse linker config file\n"
740 " --cpu type\t\t\tSet CPU type\n"
741 " --create-dep name\t\tCreate a make dependency file\n"
742 " --create-full-dep name\tCreate a full make dependency file\n"
743 " --data-label name\t\tDefine and export a DATA segment label\n"
744 " --data-name seg\t\tSet the name of the DATA segment\n"
745 " --debug\t\t\tDebug mode\n"
746 " --debug-info\t\t\tAdd debug info\n"
747 " --feature name\t\tSet an emulation feature\n"
748 " --force-import sym\t\tForce an import of symbol `sym'\n"
749 " --help\t\t\tHelp (this text)\n"
750 " --include-dir dir\t\tSet a compiler include directory path\n"
751 " --ld-args options\t\tPass options to the linker\n"
752 " --lib file\t\t\tLink this library\n"
753 " --lib-path path\t\tSpecify a library search path\n"
754 " --list-targets\t\tList all available targets\n"
755 " --listing name\t\tCreate an assembler listing file\n"
756 " --list-bytes n\t\tNumber of bytes per assembler listing line\n"
757 " --mapfile name\t\tCreate a map file\n"
758 " --memory-model model\t\tSet the memory model\n"
759 " --module\t\t\tLink as a module\n"
760 " --module-id id\t\tSpecify a module ID for the linker\n"
761 " --o65-model model\t\tOverride the o65 model\n"
762 " --obj file\t\t\tLink this object file\n"
763 " --obj-path path\t\tSpecify an object file search path\n"
764 " --print-target-path\t\tPrint the target file path\n"
765 " --register-space b\t\tSet space available for register variables\n"
766 " --register-vars\t\tEnable register variables\n"
767 " --rodata-name seg\t\tSet the name of the RODATA segment\n"
768 " --signed-chars\t\tDefault characters are signed\n"
769 " --standard std\t\tLanguage standard (c89, c99, cc65)\n"
770 " --start-addr addr\t\tSet the default start address\n"
771 " --static-locals\t\tMake local variables static\n"
772 " --target sys\t\t\tSet the target system\n"
773 " --version\t\t\tPrint the version number\n"
774 " --verbose\t\t\tVerbose mode\n"
775 " --zeropage-label name\t\tDefine and export a ZEROPAGE segment label\n"
776 " --zeropage-name seg\t\tSet the name of the ZEROPAGE segment\n",
782 static void OptAddSource (const char* Opt attribute ((unused)),
783 const char* Arg attribute ((unused)))
784 /* Strict source code as comments to the generated asm code */
786 CmdAddArg (&CC65, "-T");
791 static void OptAsmArgs (const char* Opt attribute ((unused)), const char* Arg)
792 /* Pass arguments to the assembler */
794 CmdAddArgList (&CA65, Arg);
799 static void OptAsmDefine (const char* Opt attribute ((unused)), const char* Arg)
800 /* Define an assembler symbol (assembler) */
802 CmdAddArg2 (&CA65, "-D", Arg);
807 static void OptAsmIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
808 /* Include directory (assembler) */
810 CmdAddArg2 (&CA65, "-I", Arg);
815 static void OptBinIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
816 /* Binary include directory (assembler) */
818 CmdAddArg2 (&CA65, "--bin-include-dir", Arg);
823 static void OptBssLabel (const char* Opt attribute ((unused)), const char* Arg)
824 /* Handle the --bss-label option */
826 CmdAddArg2 (&CO65, "--bss-label", Arg);
831 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
832 /* Handle the --bss-name option */
834 CmdAddArg2 (&CC65, "--bss-name", Arg);
835 CmdAddArg2 (&CO65, "--bss-name", Arg);
840 static void OptCCArgs (const char* Opt attribute ((unused)), const char* Arg)
841 /* Pass arguments to the compiler */
843 CmdAddArgList (&CC65, Arg);
848 static void OptCfgPath (const char* Opt attribute ((unused)), const char* Arg)
849 /* Config file search path (linker) */
851 CmdAddArg2 (&LD65, "--cfg-path", Arg);
856 static void OptCheckStack (const char* Opt attribute ((unused)),
857 const char* Arg attribute ((unused)))
858 /* Handle the --check-stack option */
860 CmdAddArg (&CC65, "--check-stack");
865 static void OptCodeLabel (const char* Opt attribute ((unused)), const char* Arg)
866 /* Handle the --code-label option */
868 CmdAddArg2 (&CO65, "--code-label", Arg);
873 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
874 /* Handle the --code-name option */
876 CmdAddArg2 (&CC65, "--code-name", Arg);
877 CmdAddArg2 (&CO65, "--code-name", Arg);
882 static void OptCodeSize (const char* Opt attribute ((unused)), const char* Arg)
883 /* Handle the --codesize option */
885 CmdAddArg2 (&CC65, "--codesize", Arg);
890 static void OptConfig (const char* Opt attribute ((unused)), const char* Arg)
891 /* Config file (linker) */
894 Error ("Cannot specify -C/--config twice");
901 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
902 /* Handle the --cpu option */
904 /* Add the cpu type to the assembler and compiler */
905 CmdAddArg2 (&CA65, "--cpu", Arg);
906 CmdAddArg2 (&CC65, "--cpu", Arg);
911 static void OptCreateDep (const char* Opt attribute ((unused)), const char* Arg)
912 /* Handle the --create-dep option */
914 /* Add the file name to the compiler */
915 CmdAddArg2 (&CC65, "--create-dep", Arg);
917 /* Remember the file name for the assembler */
923 static void OptCreateFullDep (const char* Opt attribute ((unused)), const char* Arg)
924 /* Handle the --create-full-dep option */
926 /* Add the file name to the compiler */
927 CmdAddArg2 (&CC65, "--create-full-dep", Arg);
929 /* Remember the file name for the assembler */
935 static void OptDataLabel (const char* Opt attribute ((unused)), const char* Arg)
936 /* Handle the --data-label option */
938 CmdAddArg2 (&CO65, "--data-label", Arg);
943 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
944 /* Handle the --data-name option */
946 CmdAddArg2 (&CC65, "--data-name", Arg);
947 CmdAddArg2 (&CO65, "--data-name", Arg);
952 static void OptDebug (const char* Opt attribute ((unused)),
953 const char* Arg attribute ((unused)))
954 /* Debug mode (compiler and cl65 utility) */
956 CmdAddArg (&CC65, "-d");
957 CmdAddArg (&CO65, "-d");
963 static void OptDebugInfo (const char* Opt attribute ((unused)),
964 const char* Arg attribute ((unused)))
965 /* Debug Info - add to compiler and assembler */
967 CmdAddArg (&CC65, "-g");
968 CmdAddArg (&CA65, "-g");
969 CmdAddArg (&CO65, "-g");
974 static void OptFeature (const char* Opt attribute ((unused)), const char* Arg)
975 /* Emulation features for the assembler */
977 CmdAddArg2 (&CA65, "--feature", Arg);
982 static void OptForceImport (const char* Opt attribute ((unused)), const char* Arg)
983 /* Emulation features for the assembler */
985 CmdAddArg2 (&LD65, "-u", Arg);
990 static void OptHelp (const char* Opt attribute ((unused)),
991 const char* Arg attribute ((unused)))
992 /* Print help - cl65 */
1000 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
1001 /* Include directory (compiler) */
1003 CmdAddArg2 (&CC65, "-I", Arg);
1008 static void OptLdArgs (const char* Opt attribute ((unused)), const char* Arg)
1009 /* Pass arguments to the linker */
1011 CmdAddArgList (&LD65, Arg);
1016 static void OptLib (const char* Opt attribute ((unused)), const char* Arg)
1017 /* Library file follows (linker) */
1019 CmdAddArg2 (&LD65, "--lib", Arg);
1024 static void OptLibPath (const char* Opt attribute ((unused)), const char* Arg)
1025 /* Library search path (linker) */
1027 CmdAddArg2 (&LD65, "--lib-path", Arg);
1032 static void OptListBytes (const char* Opt attribute ((unused)), const char* Arg)
1033 /* Set the maximum number of bytes per asm listing line */
1035 CmdAddArg2 (&CA65, "--list-bytes", Arg);
1040 static void OptListing (const char* Opt attribute ((unused)), const char* Arg)
1041 /* Create an assembler listing */
1043 CmdAddArg2 (&CA65, "-l", Arg);
1048 static void OptListTargets (const char* Opt attribute ((unused)),
1049 const char* Arg attribute ((unused)))
1050 /* List all targets */
1054 /* List the targets */
1055 for (T = TGT_NONE; T < TGT_COUNT; ++T) {
1056 printf ("%s\n", GetTargetName (T));
1060 exit (EXIT_SUCCESS);
1065 static void OptMapFile (const char* Opt attribute ((unused)), const char* Arg)
1066 /* Create a map file */
1068 /* Create a map file (linker) */
1069 CmdAddArg2 (&LD65, "-m", Arg);
1074 static void OptMemoryModel (const char* Opt attribute ((unused)), const char* Arg)
1075 /* Set the memory model */
1077 mmodel_t MemoryModel = FindMemoryModel (Arg);
1078 if (MemoryModel == MMODEL_UNKNOWN) {
1079 Error ("Unknown memory model: %s", Arg);
1080 } else if (MemoryModel == MMODEL_HUGE) {
1081 Error ("Unsupported memory model: %s", Arg);
1083 CmdAddArg2 (&CA65, "-mm", Arg);
1084 CmdAddArg2 (&CC65, "-mm", Arg);
1090 static void OptModule (const char* Opt attribute ((unused)),
1091 const char* Arg attribute ((unused)))
1092 /* Link as a module */
1099 static void OptModuleId (const char* Opt attribute ((unused)), const char* Arg)
1100 /* Specify a module if for the linker */
1102 /* Pass it straight to the linker */
1103 CmdAddArg2 (&LD65, "--module-id", Arg);
1108 static void OptO65Model (const char* Opt attribute ((unused)), const char* Arg)
1109 /* Handle the --o65-model option */
1111 CmdAddArg2 (&CO65, "-m", Arg);
1116 static void OptObj (const char* Opt attribute ((unused)), const char* Arg)
1117 /* Object file follows (linker) */
1119 CmdAddArg2 (&LD65, "--obj", Arg);
1124 static void OptObjPath (const char* Opt attribute ((unused)), const char* Arg)
1125 /* Object file search path (linker) */
1127 CmdAddArg2 (&LD65, "--obj-path", Arg);
1132 static void OptPrintTargetPath (const char* Opt attribute ((unused)),
1133 const char* Arg attribute ((unused)))
1134 /* Print the target file path */
1136 SearchPaths* TargetPath = NewSearchPath ();
1137 AddSubSearchPathFromEnv (TargetPath, "CC65_HOME", "target");
1138 #if defined(CL65_TGT) && !defined(_WIN32)
1139 AddSearchPath (TargetPath, STRINGIZE (CL65_TGT));
1141 AddSubSearchPathFromWinBin (TargetPath, "target");
1143 printf ("%s\n", GetSearchPath (TargetPath, 0));
1144 exit (EXIT_SUCCESS);
1149 static void OptRegisterSpace (const char* Opt attribute ((unused)), const char* Arg)
1150 /* Handle the --register-space option */
1152 CmdAddArg2 (&CC65, "--register-space", Arg);
1157 static void OptRegisterVars (const char* Opt attribute ((unused)),
1158 const char* Arg attribute ((unused)))
1159 /* Handle the --register-vars option */
1161 CmdAddArg (&CC65, "-r");
1166 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
1167 /* Handle the --rodata-name option */
1169 CmdAddArg2 (&CC65, "--rodata-name", Arg);
1174 static void OptSignedChars (const char* Opt attribute ((unused)),
1175 const char* Arg attribute ((unused)))
1176 /* Make default characters signed */
1178 CmdAddArg (&CC65, "-j");
1183 static void OptStandard (const char* Opt attribute ((unused)), const char* Arg)
1184 /* Set the language standard */
1186 CmdAddArg2 (&CC65, "--standard", Arg);
1191 static void OptStartAddr (const char* Opt attribute ((unused)), const char* Arg)
1192 /* Set the default start address */
1194 CmdAddArg2 (&LD65, "-S", Arg);
1199 static void OptStaticLocals (const char* Opt attribute ((unused)),
1200 const char* Arg attribute ((unused)))
1201 /* Place local variables in static storage */
1203 CmdAddArg (&CC65, "-Cl");
1208 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
1209 /* Set the target system */
1211 Target = FindTarget (Arg);
1212 if (Target == TGT_UNKNOWN) {
1213 Error ("No such target system: `%s'", Arg);
1214 } else if (Target == TGT_MODULE) {
1215 Error ("Cannot use `module' as target, use --module instead");
1221 static void OptVerbose (const char* Opt attribute ((unused)),
1222 const char* Arg attribute ((unused)))
1223 /* Verbose mode (compiler, assembler, linker) */
1225 CmdAddArg (&CC65, "-v");
1226 CmdAddArg (&CA65, "-v");
1227 CmdAddArg (&CO65, "-v");
1228 CmdAddArg (&LD65, "-v");
1233 static void OptVersion (const char* Opt attribute ((unused)),
1234 const char* Arg attribute ((unused)))
1235 /* Print version number */
1237 fprintf (stderr, "%s V%s\n", ProgName, GetVersionAsString ());
1243 static void OptZeropageLabel (const char* Opt attribute ((unused)), const char* Arg)
1244 /* Handle the --zeropage-label option */
1246 CmdAddArg2 (&CO65, "--zeropage-label", Arg);
1251 static void OptZeropageName (const char* Opt attribute ((unused)), const char* Arg)
1252 /* Handle the --zeropage-name option */
1254 CmdAddArg2 (&CO65, "--zeropage-name", Arg);
1259 int main (int argc, char* argv [])
1260 /* Utility main program */
1262 /* Program long options */
1263 static const LongOpt OptTab[] = {
1264 { "--add-source", 0, OptAddSource },
1265 { "--asm-args", 1, OptAsmArgs },
1266 { "--asm-define", 1, OptAsmDefine },
1267 { "--asm-include-dir", 1, OptAsmIncludeDir },
1268 { "--bin-include-dir", 1, OptBinIncludeDir },
1269 { "--bss-label", 1, OptBssLabel },
1270 { "--bss-name", 1, OptBssName },
1271 { "--cc-args", 1, OptCCArgs },
1272 { "--cfg-path", 1, OptCfgPath },
1273 { "--check-stack", 0, OptCheckStack },
1274 { "--code-label", 1, OptCodeLabel },
1275 { "--code-name", 1, OptCodeName },
1276 { "--codesize", 1, OptCodeSize },
1277 { "--config", 1, OptConfig },
1278 { "--cpu", 1, OptCPU },
1279 { "--create-dep", 1, OptCreateDep },
1280 { "--create-full-dep", 1, OptCreateFullDep },
1281 { "--data-label", 1, OptDataLabel },
1282 { "--data-name", 1, OptDataName },
1283 { "--debug", 0, OptDebug },
1284 { "--debug-info", 0, OptDebugInfo },
1285 { "--feature", 1, OptFeature },
1286 { "--force-import", 1, OptForceImport },
1287 { "--help", 0, OptHelp },
1288 { "--include-dir", 1, OptIncludeDir },
1289 { "--ld-args", 1, OptLdArgs },
1290 { "--lib", 1, OptLib },
1291 { "--lib-path", 1, OptLibPath },
1292 { "--list-targets", 0, OptListTargets },
1293 { "--listing", 1, OptListing },
1294 { "--list-bytes", 1, OptListBytes },
1295 { "--mapfile", 1, OptMapFile },
1296 { "--memory-model", 1, OptMemoryModel },
1297 { "--module", 0, OptModule },
1298 { "--module-id", 1, OptModuleId },
1299 { "--o65-model", 1, OptO65Model },
1300 { "--obj", 1, OptObj },
1301 { "--obj-path", 1, OptObjPath },
1302 { "--print-target-path", 0, OptPrintTargetPath},
1303 { "--register-space", 1, OptRegisterSpace },
1304 { "--register-vars", 0, OptRegisterVars },
1305 { "--rodata-name", 1, OptRodataName },
1306 { "--signed-chars", 0, OptSignedChars },
1307 { "--standard", 1, OptStandard },
1308 { "--start-addr", 1, OptStartAddr },
1309 { "--static-locals", 0, OptStaticLocals },
1310 { "--target", 1, OptTarget },
1311 { "--verbose", 0, OptVerbose },
1312 { "--version", 0, OptVersion },
1313 { "--zeropage-label", 1, OptZeropageLabel },
1314 { "--zeropage-name", 1, OptZeropageName },
1320 /* Initialize the cmdline module */
1321 InitCmdLine (&argc, &argv, "cl65");
1323 /* Initialize the command descriptors */
1325 CmdPath = xstrdup ("");
1328 CmdPath = xstrdup (argv[0]);
1329 Ptr = strrchr (CmdPath, '/');
1331 Ptr = strrchr (CmdPath, '\\');
1339 CmdInit (&CC65, CmdPath, "cc65");
1340 CmdInit (&CA65, CmdPath, "ca65");
1341 CmdInit (&CO65, CmdPath, "co65");
1342 CmdInit (&LD65, CmdPath, "ld65");
1343 CmdInit (&GRC, CmdPath, "grc65");
1346 /* Our default target is the C64 instead of "none" */
1349 /* Check the parameters */
1351 while (I < ArgCount) {
1353 /* Get the argument */
1354 const char* Arg = ArgVec[I];
1356 /* Check for an option */
1357 if (Arg [0] == '-') {
1362 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
1366 if (Arg[2] == 'l' && Arg[3] == '\0') {
1367 /* Make local variables static */
1368 OptStaticLocals (Arg, 0);
1370 /* Specify linker config file */
1371 OptConfig (Arg, GetArg (&I, 2));
1376 /* Define a preprocessor symbol (compiler) */
1377 CmdAddArg2 (&CC65, "-D", GetArg (&I, 2));
1381 /* Include directory (compiler) */
1382 OptIncludeDir (Arg, GetArg (&I, 2));
1386 if (Arg[2] == 'n' && Arg[3] == '\0') {
1387 /* VICE label file (linker) */
1388 CmdAddArg2 (&LD65, "-Ln", GetArg (&I, 3));
1390 /* Library search path (linker) */
1391 OptLibPath (Arg, GetArg (&I, 2));
1396 /* Optimize code (compiler, also covers -Oi and others) */
1397 CmdAddArg (&CC65, Arg);
1401 /* Dont assemble and link the created files */
1407 /* Include source as comment (compiler) */
1408 OptAddSource (Arg, 0);
1412 /* Print version number */
1413 OptVersion (Arg, 0);
1417 /*Forward -E to compiler*/
1418 CmdAddArg (&CC65, Arg);
1423 if (Arg[2] == 'a' && Arg[3] == '\0') {
1424 /* -Wa: Pass options to assembler */
1425 OptAsmArgs (Arg, GetArg (&I, 3));
1427 else if (Arg[2] == 'c' && Arg[3] == '\0') {
1428 /* -Wc: Pass options to compiler */
1430 /* Get argument succeeding -Wc switch */
1431 const char* WcSubArgs = GetArg (&I, 3);
1433 /* Remember -Wc sub arguments in cc65 arg struct */
1434 OptCCArgs (Arg, WcSubArgs);
1435 /* Check for isolated -E switch given after -Wc*/
1436 if (!strcmp("-E", WcSubArgs)){
1437 /*If both -Wc and -E given, then prevent assembling
1442 /* Check for -E beeing part of comma separated arg
1443 list given after -Wc*/
1444 if ( (NULL!=strstr(WcSubArgs, "-E,")) ||
1445 (NULL!=strstr(WcSubArgs, ",-E"))){
1450 } else if (Arg[2] == 'l' && Arg[3] == '\0') {
1451 /* -Wl: Pass options to linker */
1452 OptLdArgs (Arg, GetArg (&I, 3));
1454 /* Anything else: Suppress warnings (compiler) */
1455 CmdAddArg2 (&CC65, "-W", GetArg (&I, 2));
1460 /* Don't link the resulting files */
1465 /* Debug mode (compiler) */
1470 /* Debugging - add to compiler and assembler */
1471 OptDebugInfo (Arg, 0);
1476 /* Print help - cl65 */
1481 /* Default characters are signed */
1482 OptSignedChars (Arg, 0);
1486 /* Create an assembler listing */
1487 OptListing (Arg, GetArg (&I, 2));
1491 /* Create a map file (linker) */
1492 OptMapFile (Arg, GetArg (&I, 2));
1496 /* Name the output file */
1497 OutputName = GetArg (&I, 2);
1501 /* Enable register variables */
1502 OptRegisterVars (Arg, 0);
1506 /* Set target system - compiler, assembler and linker */
1507 OptTarget (Arg, GetArg (&I, 2));
1511 /* Force an import (linker) */
1512 OptForceImport (Arg, GetArg (&I, 2));
1516 if (Arg [2] == 'm') {
1517 /* Verbose map file (linker) */
1518 CmdAddArg (&LD65, "-vm");
1520 /* Verbose mode (compiler, assembler, linker) */
1521 OptVerbose (Arg, 0);
1526 UnknownOption (Arg);
1530 /* Remember the first file name */
1531 if (FirstInput == 0) {
1535 /* Determine the file type by the extension */
1536 switch (GetFileType (Arg)) {
1539 /* Compile the file */
1544 /* Assemble the file */
1552 /* Add to the linker files */
1553 CmdAddFile (&LD65, Arg);
1557 /* Add to the resource compiler files */
1562 /* Add the the object file converter files */
1567 Error ("Don't know what to do with `%s'", Arg);
1577 /* Check if we had any input files */
1578 if (FirstInput == 0) {
1579 Warning ("No input files");
1582 /* Link the given files if requested and if we have any */
1583 if (DoLink && LD65.FileCount > 0) {
1587 /* Return an apropriate exit code */
1588 return EXIT_SUCCESS;