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 " -I dir\t\t\tSet a compiler include directory path\n"
710 " -L path\t\t\tSpecify a library search path\n"
711 " -Ln name\t\t\tCreate a VICE label file\n"
712 " -O\t\t\t\tOptimize code\n"
713 " -Oi\t\t\t\tOptimize code, inline runtime functions\n"
714 " -Or\t\t\t\tOptimize code, honour the register keyword\n"
715 " -Os\t\t\t\tOptimize code, inline known C functions\n"
716 " -S\t\t\t\tCompile, but don't assemble and link\n"
717 " -T\t\t\t\tInclude source as comment\n"
718 " -V\t\t\t\tPrint the version number\n"
719 " -W name[,...]\t\t\tSuppress compiler warnings\n"
720 " -Wa options\t\t\tPass options to the assembler\n"
721 " -Wc options\t\t\tPass options to the compiler\n"
722 " -Wl options\t\t\tPass options to the linker\n"
725 " --add-source\t\t\tInclude source as comment\n"
726 " --asm-args options\t\tPass options to the assembler\n"
727 " --asm-define sym[=v]\t\tDefine an assembler symbol\n"
728 " --asm-include-dir dir\t\tSet an assembler include directory\n"
729 " --bin-include-dir dir\t\tSet an assembler binary include directory\n"
730 " --bss-label name\t\tDefine and export a BSS segment label\n"
731 " --bss-name seg\t\tSet the name of the BSS segment\n"
732 " --cc-args options\t\tPass options to the compiler\n"
733 " --cfg-path path\t\tSpecify a config file search path\n"
734 " --check-stack\t\t\tGenerate stack overflow checks\n"
735 " --code-label name\t\tDefine and export a CODE segment label\n"
736 " --code-name seg\t\tSet the name of the CODE segment\n"
737 " --codesize x\t\t\tAccept larger code by factor x\n"
738 " --config name\t\t\tUse linker config file\n"
739 " --cpu type\t\t\tSet CPU type\n"
740 " --create-dep name\t\tCreate a make dependency file\n"
741 " --create-full-dep name\tCreate a full make dependency file\n"
742 " --data-label name\t\tDefine and export a DATA segment label\n"
743 " --data-name seg\t\tSet the name of the DATA segment\n"
744 " --debug\t\t\tDebug mode\n"
745 " --debug-info\t\t\tAdd debug info\n"
746 " --feature name\t\tSet an emulation feature\n"
747 " --force-import sym\t\tForce an import of symbol `sym'\n"
748 " --help\t\t\tHelp (this text)\n"
749 " --include-dir dir\t\tSet a compiler include directory path\n"
750 " --ld-args options\t\tPass options to the linker\n"
751 " --lib file\t\t\tLink this library\n"
752 " --lib-path path\t\tSpecify a library search path\n"
753 " --list-targets\t\tList all available targets\n"
754 " --listing name\t\tCreate an assembler listing file\n"
755 " --list-bytes n\t\tNumber of bytes per assembler listing line\n"
756 " --mapfile name\t\tCreate a map file\n"
757 " --memory-model model\t\tSet the memory model\n"
758 " --module\t\t\tLink as a module\n"
759 " --module-id id\t\tSpecify a module ID for the linker\n"
760 " --o65-model model\t\tOverride the o65 model\n"
761 " --obj file\t\t\tLink this object file\n"
762 " --obj-path path\t\tSpecify an object file search path\n"
763 " --print-target-path\t\tPrint the target file path\n"
764 " --register-space b\t\tSet space available for register variables\n"
765 " --register-vars\t\tEnable register variables\n"
766 " --rodata-name seg\t\tSet the name of the RODATA segment\n"
767 " --signed-chars\t\tDefault characters are signed\n"
768 " --standard std\t\tLanguage standard (c89, c99, cc65)\n"
769 " --start-addr addr\t\tSet the default start address\n"
770 " --static-locals\t\tMake local variables static\n"
771 " --target sys\t\t\tSet the target system\n"
772 " --version\t\t\tPrint the version number\n"
773 " --verbose\t\t\tVerbose mode\n"
774 " --zeropage-label name\t\tDefine and export a ZEROPAGE segment label\n"
775 " --zeropage-name seg\t\tSet the name of the ZEROPAGE segment\n",
781 static void OptAddSource (const char* Opt attribute ((unused)),
782 const char* Arg attribute ((unused)))
783 /* Strict source code as comments to the generated asm code */
785 CmdAddArg (&CC65, "-T");
790 static void OptAsmArgs (const char* Opt attribute ((unused)), const char* Arg)
791 /* Pass arguments to the assembler */
793 CmdAddArgList (&CA65, Arg);
798 static void OptAsmDefine (const char* Opt attribute ((unused)), const char* Arg)
799 /* Define an assembler symbol (assembler) */
801 CmdAddArg2 (&CA65, "-D", Arg);
806 static void OptAsmIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
807 /* Include directory (assembler) */
809 CmdAddArg2 (&CA65, "-I", Arg);
814 static void OptBinIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
815 /* Binary include directory (assembler) */
817 CmdAddArg2 (&CA65, "--bin-include-dir", Arg);
822 static void OptBssLabel (const char* Opt attribute ((unused)), const char* Arg)
823 /* Handle the --bss-label option */
825 CmdAddArg2 (&CO65, "--bss-label", Arg);
830 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
831 /* Handle the --bss-name option */
833 CmdAddArg2 (&CC65, "--bss-name", Arg);
834 CmdAddArg2 (&CO65, "--bss-name", Arg);
839 static void OptCCArgs (const char* Opt attribute ((unused)), const char* Arg)
840 /* Pass arguments to the compiler */
842 CmdAddArgList (&CC65, Arg);
847 static void OptCfgPath (const char* Opt attribute ((unused)), const char* Arg)
848 /* Config file search path (linker) */
850 CmdAddArg2 (&LD65, "--cfg-path", Arg);
855 static void OptCheckStack (const char* Opt attribute ((unused)),
856 const char* Arg attribute ((unused)))
857 /* Handle the --check-stack option */
859 CmdAddArg (&CC65, "--check-stack");
864 static void OptCodeLabel (const char* Opt attribute ((unused)), const char* Arg)
865 /* Handle the --code-label option */
867 CmdAddArg2 (&CO65, "--code-label", Arg);
872 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
873 /* Handle the --code-name option */
875 CmdAddArg2 (&CC65, "--code-name", Arg);
876 CmdAddArg2 (&CO65, "--code-name", Arg);
881 static void OptCodeSize (const char* Opt attribute ((unused)), const char* Arg)
882 /* Handle the --codesize option */
884 CmdAddArg2 (&CC65, "--codesize", Arg);
889 static void OptConfig (const char* Opt attribute ((unused)), const char* Arg)
890 /* Config file (linker) */
893 Error ("Cannot specify -C/--config twice");
900 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
901 /* Handle the --cpu option */
903 /* Add the cpu type to the assembler and compiler */
904 CmdAddArg2 (&CA65, "--cpu", Arg);
905 CmdAddArg2 (&CC65, "--cpu", Arg);
910 static void OptCreateDep (const char* Opt attribute ((unused)), const char* Arg)
911 /* Handle the --create-dep option */
913 /* Add the file name to the compiler */
914 CmdAddArg2 (&CC65, "--create-dep", Arg);
916 /* Remember the file name for the assembler */
922 static void OptCreateFullDep (const char* Opt attribute ((unused)), const char* Arg)
923 /* Handle the --create-full-dep option */
925 /* Add the file name to the compiler */
926 CmdAddArg2 (&CC65, "--create-full-dep", Arg);
928 /* Remember the file name for the assembler */
934 static void OptDataLabel (const char* Opt attribute ((unused)), const char* Arg)
935 /* Handle the --data-label option */
937 CmdAddArg2 (&CO65, "--data-label", Arg);
942 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
943 /* Handle the --data-name option */
945 CmdAddArg2 (&CC65, "--data-name", Arg);
946 CmdAddArg2 (&CO65, "--data-name", Arg);
951 static void OptDebug (const char* Opt attribute ((unused)),
952 const char* Arg attribute ((unused)))
953 /* Debug mode (compiler and cl65 utility) */
955 CmdAddArg (&CC65, "-d");
956 CmdAddArg (&CO65, "-d");
962 static void OptDebugInfo (const char* Opt attribute ((unused)),
963 const char* Arg attribute ((unused)))
964 /* Debug Info - add to compiler and assembler */
966 CmdAddArg (&CC65, "-g");
967 CmdAddArg (&CA65, "-g");
968 CmdAddArg (&CO65, "-g");
973 static void OptFeature (const char* Opt attribute ((unused)), const char* Arg)
974 /* Emulation features for the assembler */
976 CmdAddArg2 (&CA65, "--feature", Arg);
981 static void OptForceImport (const char* Opt attribute ((unused)), const char* Arg)
982 /* Emulation features for the assembler */
984 CmdAddArg2 (&LD65, "-u", Arg);
989 static void OptHelp (const char* Opt attribute ((unused)),
990 const char* Arg attribute ((unused)))
991 /* Print help - cl65 */
999 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
1000 /* Include directory (compiler) */
1002 CmdAddArg2 (&CC65, "-I", Arg);
1007 static void OptLdArgs (const char* Opt attribute ((unused)), const char* Arg)
1008 /* Pass arguments to the linker */
1010 CmdAddArgList (&LD65, Arg);
1015 static void OptLib (const char* Opt attribute ((unused)), const char* Arg)
1016 /* Library file follows (linker) */
1018 CmdAddArg2 (&LD65, "--lib", Arg);
1023 static void OptLibPath (const char* Opt attribute ((unused)), const char* Arg)
1024 /* Library search path (linker) */
1026 CmdAddArg2 (&LD65, "--lib-path", Arg);
1031 static void OptListBytes (const char* Opt attribute ((unused)), const char* Arg)
1032 /* Set the maximum number of bytes per asm listing line */
1034 CmdAddArg2 (&CA65, "--list-bytes", Arg);
1039 static void OptListing (const char* Opt attribute ((unused)), const char* Arg)
1040 /* Create an assembler listing */
1042 CmdAddArg2 (&CA65, "-l", Arg);
1047 static void OptListTargets (const char* Opt attribute ((unused)),
1048 const char* Arg attribute ((unused)))
1049 /* List all targets */
1053 /* List the targets */
1054 for (T = TGT_NONE; T < TGT_COUNT; ++T) {
1055 printf ("%s\n", GetTargetName (T));
1059 exit (EXIT_SUCCESS);
1064 static void OptMapFile (const char* Opt attribute ((unused)), const char* Arg)
1065 /* Create a map file */
1067 /* Create a map file (linker) */
1068 CmdAddArg2 (&LD65, "-m", Arg);
1073 static void OptMemoryModel (const char* Opt attribute ((unused)), const char* Arg)
1074 /* Set the memory model */
1076 mmodel_t MemoryModel = FindMemoryModel (Arg);
1077 if (MemoryModel == MMODEL_UNKNOWN) {
1078 Error ("Unknown memory model: %s", Arg);
1079 } else if (MemoryModel == MMODEL_HUGE) {
1080 Error ("Unsupported memory model: %s", Arg);
1082 CmdAddArg2 (&CA65, "-mm", Arg);
1083 CmdAddArg2 (&CC65, "-mm", Arg);
1089 static void OptModule (const char* Opt attribute ((unused)),
1090 const char* Arg attribute ((unused)))
1091 /* Link as a module */
1098 static void OptModuleId (const char* Opt attribute ((unused)), const char* Arg)
1099 /* Specify a module if for the linker */
1101 /* Pass it straight to the linker */
1102 CmdAddArg2 (&LD65, "--module-id", Arg);
1107 static void OptO65Model (const char* Opt attribute ((unused)), const char* Arg)
1108 /* Handle the --o65-model option */
1110 CmdAddArg2 (&CO65, "-m", Arg);
1115 static void OptObj (const char* Opt attribute ((unused)), const char* Arg)
1116 /* Object file follows (linker) */
1118 CmdAddArg2 (&LD65, "--obj", Arg);
1123 static void OptObjPath (const char* Opt attribute ((unused)), const char* Arg)
1124 /* Object file search path (linker) */
1126 CmdAddArg2 (&LD65, "--obj-path", Arg);
1131 static void OptPrintTargetPath (const char* Opt attribute ((unused)),
1132 const char* Arg attribute ((unused)))
1133 /* Print the target file path */
1135 SearchPaths* TargetPath = NewSearchPath ();
1136 AddSubSearchPathFromEnv (TargetPath, "CC65_HOME", "target");
1137 #if defined(CL65_TGT) && !defined(_WIN32)
1138 AddSearchPath (TargetPath, STRINGIZE (CL65_TGT));
1140 AddSubSearchPathFromWinBin (TargetPath, "target");
1142 printf ("%s\n", GetSearchPath (TargetPath, 0));
1143 exit (EXIT_SUCCESS);
1148 static void OptRegisterSpace (const char* Opt attribute ((unused)), const char* Arg)
1149 /* Handle the --register-space option */
1151 CmdAddArg2 (&CC65, "--register-space", Arg);
1156 static void OptRegisterVars (const char* Opt attribute ((unused)),
1157 const char* Arg attribute ((unused)))
1158 /* Handle the --register-vars option */
1160 CmdAddArg (&CC65, "-r");
1165 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
1166 /* Handle the --rodata-name option */
1168 CmdAddArg2 (&CC65, "--rodata-name", Arg);
1173 static void OptSignedChars (const char* Opt attribute ((unused)),
1174 const char* Arg attribute ((unused)))
1175 /* Make default characters signed */
1177 CmdAddArg (&CC65, "-j");
1182 static void OptStandard (const char* Opt attribute ((unused)), const char* Arg)
1183 /* Set the language standard */
1185 CmdAddArg2 (&CC65, "--standard", Arg);
1190 static void OptStartAddr (const char* Opt attribute ((unused)), const char* Arg)
1191 /* Set the default start address */
1193 CmdAddArg2 (&LD65, "-S", Arg);
1198 static void OptStaticLocals (const char* Opt attribute ((unused)),
1199 const char* Arg attribute ((unused)))
1200 /* Place local variables in static storage */
1202 CmdAddArg (&CC65, "-Cl");
1207 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
1208 /* Set the target system */
1210 Target = FindTarget (Arg);
1211 if (Target == TGT_UNKNOWN) {
1212 Error ("No such target system: `%s'", Arg);
1213 } else if (Target == TGT_MODULE) {
1214 Error ("Cannot use `module' as target, use --module instead");
1220 static void OptVerbose (const char* Opt attribute ((unused)),
1221 const char* Arg attribute ((unused)))
1222 /* Verbose mode (compiler, assembler, linker) */
1224 CmdAddArg (&CC65, "-v");
1225 CmdAddArg (&CA65, "-v");
1226 CmdAddArg (&CO65, "-v");
1227 CmdAddArg (&LD65, "-v");
1232 static void OptVersion (const char* Opt attribute ((unused)),
1233 const char* Arg attribute ((unused)))
1234 /* Print version number */
1236 fprintf (stderr, "%s V%s\n", ProgName, GetVersionAsString ());
1242 static void OptZeropageLabel (const char* Opt attribute ((unused)), const char* Arg)
1243 /* Handle the --zeropage-label option */
1245 CmdAddArg2 (&CO65, "--zeropage-label", Arg);
1250 static void OptZeropageName (const char* Opt attribute ((unused)), const char* Arg)
1251 /* Handle the --zeropage-name option */
1253 CmdAddArg2 (&CO65, "--zeropage-name", Arg);
1258 int main (int argc, char* argv [])
1259 /* Utility main program */
1261 /* Program long options */
1262 static const LongOpt OptTab[] = {
1263 { "--add-source", 0, OptAddSource },
1264 { "--asm-args", 1, OptAsmArgs },
1265 { "--asm-define", 1, OptAsmDefine },
1266 { "--asm-include-dir", 1, OptAsmIncludeDir },
1267 { "--bin-include-dir", 1, OptBinIncludeDir },
1268 { "--bss-label", 1, OptBssLabel },
1269 { "--bss-name", 1, OptBssName },
1270 { "--cc-args", 1, OptCCArgs },
1271 { "--cfg-path", 1, OptCfgPath },
1272 { "--check-stack", 0, OptCheckStack },
1273 { "--code-label", 1, OptCodeLabel },
1274 { "--code-name", 1, OptCodeName },
1275 { "--codesize", 1, OptCodeSize },
1276 { "--config", 1, OptConfig },
1277 { "--cpu", 1, OptCPU },
1278 { "--create-dep", 1, OptCreateDep },
1279 { "--create-full-dep", 1, OptCreateFullDep },
1280 { "--data-label", 1, OptDataLabel },
1281 { "--data-name", 1, OptDataName },
1282 { "--debug", 0, OptDebug },
1283 { "--debug-info", 0, OptDebugInfo },
1284 { "--feature", 1, OptFeature },
1285 { "--force-import", 1, OptForceImport },
1286 { "--help", 0, OptHelp },
1287 { "--include-dir", 1, OptIncludeDir },
1288 { "--ld-args", 1, OptLdArgs },
1289 { "--lib", 1, OptLib },
1290 { "--lib-path", 1, OptLibPath },
1291 { "--list-targets", 0, OptListTargets },
1292 { "--listing", 1, OptListing },
1293 { "--list-bytes", 1, OptListBytes },
1294 { "--mapfile", 1, OptMapFile },
1295 { "--memory-model", 1, OptMemoryModel },
1296 { "--module", 0, OptModule },
1297 { "--module-id", 1, OptModuleId },
1298 { "--o65-model", 1, OptO65Model },
1299 { "--obj", 1, OptObj },
1300 { "--obj-path", 1, OptObjPath },
1301 { "--print-target-path", 0, OptPrintTargetPath},
1302 { "--register-space", 1, OptRegisterSpace },
1303 { "--register-vars", 0, OptRegisterVars },
1304 { "--rodata-name", 1, OptRodataName },
1305 { "--signed-chars", 0, OptSignedChars },
1306 { "--standard", 1, OptStandard },
1307 { "--start-addr", 1, OptStartAddr },
1308 { "--static-locals", 0, OptStaticLocals },
1309 { "--target", 1, OptTarget },
1310 { "--verbose", 0, OptVerbose },
1311 { "--version", 0, OptVersion },
1312 { "--zeropage-label", 1, OptZeropageLabel },
1313 { "--zeropage-name", 1, OptZeropageName },
1319 /* Initialize the cmdline module */
1320 InitCmdLine (&argc, &argv, "cl65");
1322 /* Initialize the command descriptors */
1324 CmdPath = xstrdup ("");
1327 CmdPath = xstrdup (argv[0]);
1328 Ptr = strrchr (CmdPath, '/');
1330 Ptr = strrchr (CmdPath, '\\');
1338 CmdInit (&CC65, CmdPath, "cc65");
1339 CmdInit (&CA65, CmdPath, "ca65");
1340 CmdInit (&CO65, CmdPath, "co65");
1341 CmdInit (&LD65, CmdPath, "ld65");
1342 CmdInit (&GRC, CmdPath, "grc65");
1345 /* Our default target is the C64 instead of "none" */
1348 /* Check the parameters */
1350 while (I < ArgCount) {
1352 /* Get the argument */
1353 const char* Arg = ArgVec[I];
1355 /* Check for an option */
1356 if (Arg [0] == '-') {
1361 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
1365 if (Arg[2] == 'l' && Arg[3] == '\0') {
1366 /* Make local variables static */
1367 OptStaticLocals (Arg, 0);
1369 /* Specify linker config file */
1370 OptConfig (Arg, GetArg (&I, 2));
1375 /* Define a preprocessor symbol (compiler) */
1376 CmdAddArg2 (&CC65, "-D", GetArg (&I, 2));
1380 /* Include directory (compiler) */
1381 OptIncludeDir (Arg, GetArg (&I, 2));
1385 if (Arg[2] == 'n' && Arg[3] == '\0') {
1386 /* VICE label file (linker) */
1387 CmdAddArg2 (&LD65, "-Ln", GetArg (&I, 3));
1389 /* Library search path (linker) */
1390 OptLibPath (Arg, GetArg (&I, 2));
1395 /* Optimize code (compiler, also covers -Oi and others) */
1396 CmdAddArg (&CC65, Arg);
1400 /* Dont assemble and link the created files */
1406 /* Include source as comment (compiler) */
1407 OptAddSource (Arg, 0);
1411 /* Print version number */
1412 OptVersion (Arg, 0);
1416 if (Arg[2] == 'a' && Arg[3] == '\0') {
1417 /* -Wa: Pass options to assembler */
1418 OptAsmArgs (Arg, GetArg (&I, 3));
1419 } else if (Arg[2] == 'c' && Arg[3] == '\0') {
1420 /* -Wc: Pass options to compiler */
1421 OptCCArgs (Arg, GetArg (&I, 3));
1422 } else if (Arg[2] == 'l' && Arg[3] == '\0') {
1423 /* -Wl: Pass options to linker */
1424 OptLdArgs (Arg, GetArg (&I, 3));
1426 /* Anything else: Suppress warnings (compiler) */
1427 CmdAddArg2 (&CC65, "-W", GetArg (&I, 2));
1432 /* Don't link the resulting files */
1437 /* Debug mode (compiler) */
1442 /* Debugging - add to compiler and assembler */
1443 OptDebugInfo (Arg, 0);
1448 /* Print help - cl65 */
1453 /* Default characters are signed */
1454 OptSignedChars (Arg, 0);
1458 /* Create an assembler listing */
1459 OptListing (Arg, GetArg (&I, 2));
1463 /* Create a map file (linker) */
1464 OptMapFile (Arg, GetArg (&I, 2));
1468 /* Name the output file */
1469 OutputName = GetArg (&I, 2);
1473 /* Enable register variables */
1474 OptRegisterVars (Arg, 0);
1478 /* Set target system - compiler, assembler and linker */
1479 OptTarget (Arg, GetArg (&I, 2));
1483 /* Force an import (linker) */
1484 OptForceImport (Arg, GetArg (&I, 2));
1488 if (Arg [2] == 'm') {
1489 /* Verbose map file (linker) */
1490 CmdAddArg (&LD65, "-vm");
1492 /* Verbose mode (compiler, assembler, linker) */
1493 OptVerbose (Arg, 0);
1498 UnknownOption (Arg);
1502 /* Remember the first file name */
1503 if (FirstInput == 0) {
1507 /* Determine the file type by the extension */
1508 switch (GetFileType (Arg)) {
1511 /* Compile the file */
1516 /* Assemble the file */
1524 /* Add to the linker files */
1525 CmdAddFile (&LD65, Arg);
1529 /* Add to the resource compiler files */
1534 /* Add the the object file converter files */
1539 Error ("Don't know what to do with `%s'", Arg);
1549 /* Check if we had any input files */
1550 if (FirstInput == 0) {
1551 Warning ("No input files");
1554 /* Link the given files if requested and if we have any */
1555 if (DoLink && LD65.FileCount > 0) {
1559 /* Return an apropriate exit code */
1560 return EXIT_SUCCESS;