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)
87 /*****************************************************************************/
89 /*****************************************************************************/
93 /* Struct that describes a command */
94 typedef struct CmdDesc CmdDesc;
96 char* Name; /* The command name */
98 unsigned ArgCount; /* Count of arguments */
99 unsigned ArgMax; /* Maximum count of arguments */
100 char** Args; /* The arguments */
102 unsigned FileCount; /* Count of files to translate */
103 unsigned FileMax; /* Maximum count of files */
104 char** Files; /* The files */
107 /* Command descriptors for the different programs */
108 static CmdDesc CC65 = { 0, 0, 0, 0, 0, 0, 0 };
109 static CmdDesc CA65 = { 0, 0, 0, 0, 0, 0, 0 };
110 static CmdDesc CO65 = { 0, 0, 0, 0, 0, 0, 0 };
111 static CmdDesc LD65 = { 0, 0, 0, 0, 0, 0, 0 };
112 static CmdDesc GRC = { 0, 0, 0, 0, 0, 0, 0 };
114 /* Variables controlling the steps we're doing */
115 static int DoLink = 1;
116 static int DoAssemble = 1;
118 /* The name of the output file, NULL if none given */
119 static const char* OutputName = 0;
121 /* The name of the linker configuration file if given */
122 static const char* LinkerConfig = 0;
124 /* The name of the first input file. This will be used to construct the
125 ** executable file name if no explicit name is given.
127 static const char* FirstInput = 0;
129 /* The names of the files for dependency generation */
130 static const char* DepName = 0;
131 static const char* FullDepName = 0;
133 /* Remember if we should link a module */
134 static int Module = 0;
136 /* Extension used for a module */
137 #define MODULE_EXT ".o65"
139 /* Name of the target specific runtime library */
140 static char* TargetLib = 0;
144 /*****************************************************************************/
145 /* Include the system specific spawn function */
146 /*****************************************************************************/
150 #if defined(NEED_SPAWN)
151 # if defined(SPAWN_AMIGA)
152 # include "spawn-amiga.inc"
154 # include "spawn-unix.inc"
160 /*****************************************************************************/
161 /* Command structure handling */
162 /*****************************************************************************/
166 static void CmdExpand (CmdDesc* Cmd)
167 /* Expand the argument vector */
169 unsigned NewMax = Cmd->ArgMax + 10;
170 char** NewArgs = xmalloc (NewMax * sizeof (char*));
171 memcpy (NewArgs, Cmd->Args, Cmd->ArgMax * sizeof (char*));
174 Cmd->ArgMax = NewMax;
179 static void CmdAddArg (CmdDesc* Cmd, const char* Arg)
180 /* Add a new argument to the command */
182 /* Expand the argument vector if needed */
183 if (Cmd->ArgCount >= Cmd->ArgMax) {
187 /* Add a copy of the new argument, allow a NULL pointer */
189 Cmd->Args[Cmd->ArgCount++] = xstrdup (Arg);
191 Cmd->Args[Cmd->ArgCount++] = 0;
197 static void CmdAddArg2 (CmdDesc* Cmd, const char* Arg1, const char* Arg2)
198 /* Add a new argument pair to the command */
200 CmdAddArg (Cmd, Arg1);
201 CmdAddArg (Cmd, Arg2);
206 static void CmdAddArgList (CmdDesc* Cmd, const char* ArgList)
207 /* Add a list of arguments separated by commas */
209 const char* Arg = ArgList;
213 if (*P == '\0' || *P == ',') {
215 /* End of argument, add it */
216 unsigned Len = P - Arg;
218 /* Expand the argument vector if needed */
219 if (Cmd->ArgCount >= Cmd->ArgMax) {
223 /* Add the new argument */
224 Cmd->Args[Cmd->ArgCount] = memcpy (xmalloc (Len + 1), Arg, Len);
225 Cmd->Args[Cmd->ArgCount][Len] = '\0';
228 /* If the argument was terminated by a comma, skip it, otherwise
232 /* Start over at next char */
238 /* Skip other chars */
247 static void CmdDelArgs (CmdDesc* Cmd, unsigned LastValid)
248 /* Remove all arguments with an index greater than LastValid */
250 while (Cmd->ArgCount > LastValid) {
252 xfree (Cmd->Args [Cmd->ArgCount]);
253 Cmd->Args [Cmd->ArgCount] = 0;
259 static void CmdAddFile (CmdDesc* Cmd, const char* File)
260 /* Add a new file to the command */
262 /* Expand the file vector if needed */
263 if (Cmd->FileCount == Cmd->FileMax) {
264 unsigned NewMax = Cmd->FileMax + 10;
265 char** NewFiles = xmalloc (NewMax * sizeof (char*));
266 memcpy (NewFiles, Cmd->Files, Cmd->FileMax * sizeof (char*));
268 Cmd->Files = NewFiles;
269 Cmd->FileMax = NewMax;
272 /* If the file name is not NULL (which is legal and is used to terminate
273 ** the file list), check if the file name does already exist in the file
274 ** list and print a warning if so. Regardless of the search result, add
279 for (I = 0; I < Cmd->FileCount; ++I) {
280 if (strcmp (Cmd->Files[I], File) == 0) {
282 Warning ("Duplicate file in argument list: `%s'", File);
283 /* No need to search further */
289 Cmd->Files [Cmd->FileCount++] = xstrdup (File);
291 /* Add a NULL pointer */
292 Cmd->Files [Cmd->FileCount++] = 0;
298 static void CmdInit (CmdDesc* Cmd, const char* Path, const char* Name)
299 /* Initialize the command using the given path and name of the executable */
303 FullName = (char*) xmalloc (strlen (Path) + strlen (Name) + 1);
304 strcpy (FullName, Path);
305 strcat (FullName, Name);
307 /* Remember the command */
308 Cmd->Name = xstrdup (FullName);
310 /* Use the command name as first argument */
311 CmdAddArg (Cmd, FullName);
318 static void CmdSetOutput (CmdDesc* Cmd, const char* File)
319 /* Set the output file in a command desc */
321 CmdAddArg2 (Cmd, "-o", File);
326 static void CmdSetTarget (CmdDesc* Cmd, target_t Target)
327 /* Set the output file in a command desc */
329 CmdAddArg2 (Cmd, "-t", GetTargetName (Target));
334 static void CmdPrint (CmdDesc* Cmd, FILE* F)
335 /* Output the command line encoded in the command desc */
338 for (I = 0; I < Cmd->ArgCount && Cmd->Args[I] != 0; ++I) {
339 fprintf (F, "%s ", Cmd->Args[I]);
345 /*****************************************************************************/
346 /* Target handling */
347 /*****************************************************************************/
351 static void SetTargetFiles (void)
352 /* Set the target system files */
354 /* Determine the names of the target specific library file */
355 if (Target != TGT_NONE) {
357 /* Get a pointer to the system name and its length */
358 const char* TargetName = GetTargetName (Target);
359 unsigned TargetNameLen = strlen (TargetName);
361 /* Set the library file */
362 TargetLib = xmalloc (TargetNameLen + 4 + 1);
363 memcpy (TargetLib, TargetName, TargetNameLen);
364 strcpy (TargetLib + TargetNameLen, ".lib");
371 /*****************************************************************************/
373 /*****************************************************************************/
377 static void ExecProgram (CmdDesc* Cmd)
378 /* Execute a subprocess with the given name/parameters. Exit on errors. */
382 /* If in debug mode, output the command line we will execute */
384 printf ("Executing: ");
385 CmdPrint (Cmd, stdout);
389 /* Call the program */
390 Status = spawnvp (P_WAIT, Cmd->Name, SPAWN_ARGV_CONST_CAST Cmd->Args);
392 /* Check the result code */
394 /* Error executing the program */
395 Error ("Cannot execute `%s': %s", Cmd->Name, strerror (errno));
396 } else if (Status != 0) {
397 /* Called program had an error */
404 static void Link (void)
405 /* Link the resulting executable */
409 /* Since linking is always the final step, if we have an output file name
410 ** given, set it here. If we don't have an explicit output name given,
411 ** try to build one from the name of the first input file.
415 CmdSetOutput (&LD65, OutputName);
417 } else if (FirstInput && FindExt (FirstInput)) { /* Only if ext present! */
419 const char* Extension = Module? MODULE_EXT : "";
420 char* Output = MakeFilename (FirstInput, Extension);
421 CmdSetOutput (&LD65, Output);
426 /* If we have a linker config file given, add it to the command line.
427 ** Otherwise pass the target to the linker if we have one.
431 Error ("Cannot use -C and --module together");
433 CmdAddArg2 (&LD65, "-C", LinkerConfig);
435 CmdSetTarget (&LD65, TGT_MODULE);
437 CmdSetTarget (&LD65, Target);
440 /* Determine which target libraries are needed */
443 /* Add all object files as parameters */
444 for (I = 0; I < LD65.FileCount; ++I) {
445 CmdAddArg (&LD65, LD65.Files [I]);
448 /* Add the system runtime library */
450 CmdAddArg (&LD65, TargetLib);
453 /* Terminate the argument list with a NULL pointer */
454 CmdAddArg (&LD65, 0);
456 /* Call the linker */
462 static void AssembleFile (const char* File, unsigned ArgCount)
463 /* Common routine to assemble a file. Will be called by Assemble() and
464 ** AssembleIntermediate(). Adds options common for both routines and
465 ** assembles the file. Will remove excess arguments after assembly.
468 /* Set the target system */
469 CmdSetTarget (&CA65, Target);
471 /* Check if this is the last processing step */
473 /* We're linking later. Add the output file of the assembly
474 ** the the file list of the linker. The name of the output
475 ** file is that of the input file with ".s" replaced by ".o".
477 char* ObjName = MakeFilename (File, ".o");
478 CmdAddFile (&LD65, ObjName);
481 /* This is the final step. If an output name is given, set it */
483 CmdSetOutput (&CA65, OutputName);
487 /* Add the file as argument for the assembler */
488 CmdAddArg (&CA65, File);
490 /* Add a NULL pointer to terminate the argument list */
491 CmdAddArg (&CA65, 0);
493 /* Run the assembler */
496 /* Remove the excess arguments */
497 CmdDelArgs (&CA65, ArgCount);
502 static void AssembleIntermediate (const char* SourceFile)
503 /* Assemble an intermediate file which was generated by a previous processing
504 ** step with SourceFile as input. The -dep options won't be added and
505 ** the intermediate assembler file is removed after assembly.
508 /* Generate the name of the assembler output file from the source file
509 ** name. It's the same name with the extension replaced by ".s"
511 char* AsmName = MakeFilename (SourceFile, ".s");
513 /* Assemble the intermediate assembler file */
514 AssembleFile (AsmName, CA65.ArgCount);
516 /* Remove the input file */
517 if (remove (AsmName) < 0) {
518 Warning ("Cannot remove temporary file `%s': %s",
519 AsmName, strerror (errno));
522 /* Free the assembler file name which was allocated from the heap */
528 static void Assemble (const char* File)
529 /* Assemble the given file */
531 /* Remember the current assembler argument count */
532 unsigned ArgCount = CA65.ArgCount;
534 /* We aren't assembling an intermediate file, but one requested by the
535 ** user. So add a few options here if they were given on the command
538 if (DepName && *DepName) {
539 CmdAddArg2 (&CA65, "--create-dep", DepName);
541 if (FullDepName && *FullDepName) {
542 CmdAddArg2 (&CA65, "--create-full-dep", FullDepName);
545 /* Use the common routine */
546 AssembleFile (File, ArgCount);
551 static void Compile (const char* File)
552 /* Compile the given file */
554 /* Remember the current compiler argument count */
555 unsigned ArgCount = CC65.ArgCount;
557 /* Set the target system */
558 CmdSetTarget (&CC65, Target);
560 /* Check if this is the final step */
562 /* We will assemble this file later. If a dependency file is to be
563 ** generated, set the dependency target to be the final object file,
564 ** not the intermediate assembler file. But beware: There may be an
565 ** output name specified for the assembler.
567 if (DepName || FullDepName) {
568 /* Was an output name for the assembler specified? */
569 if (!DoLink && OutputName) {
570 /* Use this name as the dependency target */
571 CmdAddArg2 (&CC65, "--dep-target", OutputName);
573 /* Use the object file name as the dependency target */
574 char* ObjName = MakeFilename (File, ".o");
575 CmdAddArg2 (&CC65, "--dep-target", ObjName);
580 /* If we won't assemble, this is the final step. In this case, set
581 ** the output name if it was given.
584 CmdSetOutput (&CC65, OutputName);
588 /* Add the file as argument for the compiler */
589 CmdAddArg (&CC65, File);
591 /* Add a NULL pointer to terminate the argument list */
592 CmdAddArg (&CC65, 0);
594 /* Run the compiler */
597 /* Remove the excess arguments */
598 CmdDelArgs (&CC65, ArgCount);
600 /* If this is not the final step, assemble the generated file, then
604 /* Assemble the intermediate file and remove it */
605 AssembleIntermediate (File);
611 static void CompileRes (const char* File)
612 /* Compile the given geos resource file */
614 /* Remember the current assembler argument count */
615 unsigned ArgCount = GRC.ArgCount;
617 /* Resource files need an geos-apple or geos-cbm target but this
618 ** is checked within grc65.
620 CmdSetTarget (&GRC, Target);
622 /* Add the file as argument for the resource compiler */
623 CmdAddArg (&GRC, File);
625 /* Add a NULL pointer to terminate the argument list */
628 /* Run the compiler */
631 /* Remove the excess arguments */
632 CmdDelArgs (&GRC, ArgCount);
634 /* If this is not the final step, assemble the generated file, then
638 /* Assemble the intermediate file and remove it */
639 AssembleIntermediate (File);
645 static void ConvertO65 (const char* File)
646 /* Convert an o65 object file into an assembler file */
648 /* Remember the current converter argument count */
649 unsigned ArgCount = CO65.ArgCount;
651 /* If we won't assemble, this is the final step. In this case, set the
654 if (!DoAssemble && OutputName) {
655 CmdSetOutput (&CO65, OutputName);
658 /* Add the file as argument for the object file converter */
659 CmdAddArg (&CO65, File);
661 /* Add a NULL pointer to terminate the argument list */
662 CmdAddArg (&CO65, 0);
664 /* Run the converter */
667 /* Remove the excess arguments */
668 CmdDelArgs (&CO65, ArgCount);
670 /* If this is not the final step, assemble the generated file, then
674 /* Assemble the intermediate file and remove it */
675 AssembleIntermediate (File);
681 /*****************************************************************************/
683 /*****************************************************************************/
687 static void Usage (void)
688 /* Print usage information and exit */
690 printf ("Usage: %s [options] file [...]\n"
692 " -c\t\t\t\tCompile and assemble, but don't link\n"
693 " -d\t\t\t\tDebug mode\n"
694 " -g\t\t\t\tAdd debug info\n"
695 " -h\t\t\t\tHelp (this text)\n"
696 " -l name\t\t\tCreate an assembler listing file\n"
697 " -m name\t\t\tCreate a map file\n"
698 " -mm model\t\t\tSet the memory model\n"
699 " -o name\t\t\tName the output file\n"
700 " -r\t\t\t\tEnable register variables\n"
701 " -t sys\t\t\tSet the target system\n"
702 " -u sym\t\t\tForce an import of symbol `sym'\n"
703 " -v\t\t\t\tVerbose mode\n"
704 " -vm\t\t\t\tVerbose map file\n"
705 " -C name\t\t\tUse linker config file\n"
706 " -Cl\t\t\t\tMake local variables static\n"
707 " -D sym[=defn]\t\t\tDefine a preprocessor symbol\n"
708 " -I dir\t\t\tSet a compiler include directory path\n"
709 " -L path\t\t\tSpecify a library search path\n"
710 " -Ln name\t\t\tCreate a VICE label file\n"
711 " -O\t\t\t\tOptimize code\n"
712 " -Oi\t\t\t\tOptimize code, inline runtime functions\n"
713 " -Or\t\t\t\tOptimize code, honour the register keyword\n"
714 " -Os\t\t\t\tOptimize code, inline known C functions\n"
715 " -S\t\t\t\tCompile, but don't assemble and link\n"
716 " -T\t\t\t\tInclude source as comment\n"
717 " -V\t\t\t\tPrint the version number\n"
718 " -W name[,...]\t\t\tSuppress compiler warnings\n"
719 " -Wa options\t\t\tPass options to the assembler\n"
720 " -Wc options\t\t\tPass options to the compiler\n"
721 " -Wl options\t\t\tPass options to the linker\n"
724 " --add-source\t\t\tInclude source as comment\n"
725 " --asm-args options\t\tPass options to the assembler\n"
726 " --asm-define sym[=v]\t\tDefine an assembler symbol\n"
727 " --asm-include-dir dir\t\tSet an assembler include directory\n"
728 " --bin-include-dir dir\t\tSet an assembler binary include directory\n"
729 " --bss-label name\t\tDefine and export a BSS segment label\n"
730 " --bss-name seg\t\tSet the name of the BSS segment\n"
731 " --cc-args options\t\tPass options to the compiler\n"
732 " --cfg-path path\t\tSpecify a config file search path\n"
733 " --check-stack\t\t\tGenerate stack overflow checks\n"
734 " --code-label name\t\tDefine and export a CODE segment label\n"
735 " --code-name seg\t\tSet the name of the CODE segment\n"
736 " --codesize x\t\t\tAccept larger code by factor x\n"
737 " --config name\t\t\tUse linker config file\n"
738 " --cpu type\t\t\tSet CPU type\n"
739 " --create-dep name\t\tCreate a make dependency file\n"
740 " --create-full-dep name\tCreate a full make dependency file\n"
741 " --data-label name\t\tDefine and export a DATA segment label\n"
742 " --data-name seg\t\tSet the name of the DATA segment\n"
743 " --debug\t\t\tDebug mode\n"
744 " --debug-info\t\t\tAdd debug info\n"
745 " --feature name\t\tSet an emulation feature\n"
746 " --force-import sym\t\tForce an import of symbol `sym'\n"
747 " --help\t\t\tHelp (this text)\n"
748 " --include-dir dir\t\tSet a compiler include directory path\n"
749 " --ld-args options\t\tPass options to the linker\n"
750 " --lib file\t\t\tLink this library\n"
751 " --lib-path path\t\tSpecify a library search path\n"
752 " --list-targets\t\tList all available targets\n"
753 " --listing name\t\tCreate an assembler listing file\n"
754 " --list-bytes n\t\tNumber of bytes per assembler listing line\n"
755 " --mapfile name\t\tCreate a map file\n"
756 " --memory-model model\t\tSet the memory model\n"
757 " --module\t\t\tLink as a module\n"
758 " --module-id id\t\tSpecify a module ID for the linker\n"
759 " --o65-model model\t\tOverride the o65 model\n"
760 " --obj file\t\t\tLink this object file\n"
761 " --obj-path path\t\tSpecify an object file search path\n"
762 " --register-space b\t\tSet space available for register variables\n"
763 " --register-vars\t\tEnable register variables\n"
764 " --rodata-name seg\t\tSet the name of the RODATA segment\n"
765 " --signed-chars\t\tDefault characters are signed\n"
766 " --standard std\t\tLanguage standard (c89, c99, cc65)\n"
767 " --start-addr addr\t\tSet the default start address\n"
768 " --static-locals\t\tMake local variables static\n"
769 " --target sys\t\t\tSet the target system\n"
770 " --version\t\t\tPrint the version number\n"
771 " --verbose\t\t\tVerbose mode\n"
772 " --zeropage-label name\t\tDefine and export a ZEROPAGE segment label\n"
773 " --zeropage-name seg\t\tSet the name of the ZEROPAGE segment\n",
779 static void OptAddSource (const char* Opt attribute ((unused)),
780 const char* Arg attribute ((unused)))
781 /* Strict source code as comments to the generated asm code */
783 CmdAddArg (&CC65, "-T");
788 static void OptAsmArgs (const char* Opt attribute ((unused)), const char* Arg)
789 /* Pass arguments to the assembler */
791 CmdAddArgList (&CA65, Arg);
796 static void OptAsmDefine (const char* Opt attribute ((unused)), const char* Arg)
797 /* Define an assembler symbol (assembler) */
799 CmdAddArg2 (&CA65, "-D", Arg);
804 static void OptAsmIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
805 /* Include directory (assembler) */
807 CmdAddArg2 (&CA65, "-I", Arg);
812 static void OptBinIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
813 /* Binary include directory (assembler) */
815 CmdAddArg2 (&CA65, "--bin-include-dir", Arg);
820 static void OptBssLabel (const char* Opt attribute ((unused)), const char* Arg)
821 /* Handle the --bss-label option */
823 CmdAddArg2 (&CO65, "--bss-label", Arg);
828 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
829 /* Handle the --bss-name option */
831 CmdAddArg2 (&CC65, "--bss-name", Arg);
832 CmdAddArg2 (&CO65, "--bss-name", Arg);
837 static void OptCCArgs (const char* Opt attribute ((unused)), const char* Arg)
838 /* Pass arguments to the compiler */
840 CmdAddArgList (&CC65, Arg);
845 static void OptCfgPath (const char* Opt attribute ((unused)), const char* Arg)
846 /* Config file search path (linker) */
848 CmdAddArg2 (&LD65, "--cfg-path", Arg);
853 static void OptCheckStack (const char* Opt attribute ((unused)),
854 const char* Arg attribute ((unused)))
855 /* Handle the --check-stack option */
857 CmdAddArg (&CC65, "--check-stack");
862 static void OptCodeLabel (const char* Opt attribute ((unused)), const char* Arg)
863 /* Handle the --code-label option */
865 CmdAddArg2 (&CO65, "--code-label", Arg);
870 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
871 /* Handle the --code-name option */
873 CmdAddArg2 (&CC65, "--code-name", Arg);
874 CmdAddArg2 (&CO65, "--code-name", Arg);
879 static void OptCodeSize (const char* Opt attribute ((unused)), const char* Arg)
880 /* Handle the --codesize option */
882 CmdAddArg2 (&CC65, "--codesize", Arg);
887 static void OptConfig (const char* Opt attribute ((unused)), const char* Arg)
888 /* Config file (linker) */
891 Error ("Cannot specify -C/--config twice");
898 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
899 /* Handle the --cpu option */
901 /* Add the cpu type to the assembler and compiler */
902 CmdAddArg2 (&CA65, "--cpu", Arg);
903 CmdAddArg2 (&CC65, "--cpu", Arg);
908 static void OptCreateDep (const char* Opt attribute ((unused)), const char* Arg)
909 /* Handle the --create-dep option */
911 /* Add the file name to the compiler */
912 CmdAddArg2 (&CC65, "--create-dep", Arg);
914 /* Remember the file name for the assembler */
920 static void OptCreateFullDep (const char* Opt attribute ((unused)), const char* Arg)
921 /* Handle the --create-full-dep option */
923 /* Add the file name to the compiler */
924 CmdAddArg2 (&CC65, "--create-full-dep", Arg);
926 /* Remember the file name for the assembler */
932 static void OptDataLabel (const char* Opt attribute ((unused)), const char* Arg)
933 /* Handle the --data-label option */
935 CmdAddArg2 (&CO65, "--data-label", Arg);
940 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
941 /* Handle the --data-name option */
943 CmdAddArg2 (&CC65, "--data-name", Arg);
944 CmdAddArg2 (&CO65, "--data-name", Arg);
949 static void OptDebug (const char* Opt attribute ((unused)),
950 const char* Arg attribute ((unused)))
951 /* Debug mode (compiler and cl65 utility) */
953 CmdAddArg (&CC65, "-d");
954 CmdAddArg (&CO65, "-d");
960 static void OptDebugInfo (const char* Opt attribute ((unused)),
961 const char* Arg attribute ((unused)))
962 /* Debug Info - add to compiler and assembler */
964 CmdAddArg (&CC65, "-g");
965 CmdAddArg (&CA65, "-g");
966 CmdAddArg (&CO65, "-g");
971 static void OptFeature (const char* Opt attribute ((unused)), const char* Arg)
972 /* Emulation features for the assembler */
974 CmdAddArg2 (&CA65, "--feature", Arg);
979 static void OptForceImport (const char* Opt attribute ((unused)), const char* Arg)
980 /* Emulation features for the assembler */
982 CmdAddArg2 (&LD65, "-u", Arg);
987 static void OptHelp (const char* Opt attribute ((unused)),
988 const char* Arg attribute ((unused)))
989 /* Print help - cl65 */
997 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
998 /* Include directory (compiler) */
1000 CmdAddArg2 (&CC65, "-I", Arg);
1005 static void OptLdArgs (const char* Opt attribute ((unused)), const char* Arg)
1006 /* Pass arguments to the linker */
1008 CmdAddArgList (&LD65, Arg);
1013 static void OptLib (const char* Opt attribute ((unused)), const char* Arg)
1014 /* Library file follows (linker) */
1016 CmdAddArg2 (&LD65, "--lib", Arg);
1021 static void OptLibPath (const char* Opt attribute ((unused)), const char* Arg)
1022 /* Library search path (linker) */
1024 CmdAddArg2 (&LD65, "--lib-path", Arg);
1029 static void OptListBytes (const char* Opt attribute ((unused)), const char* Arg)
1030 /* Set the maximum number of bytes per asm listing line */
1032 CmdAddArg2 (&CA65, "--list-bytes", Arg);
1037 static void OptListing (const char* Opt attribute ((unused)), const char* Arg)
1038 /* Create an assembler listing */
1040 CmdAddArg2 (&CA65, "-l", Arg);
1045 static void OptListTargets (const char* Opt attribute ((unused)),
1046 const char* Arg attribute ((unused)))
1047 /* List all targets */
1051 /* List the targets */
1052 for (T = TGT_NONE; T < TGT_COUNT; ++T) {
1053 printf ("%s\n", GetTargetName (T));
1057 exit (EXIT_SUCCESS);
1062 static void OptMapFile (const char* Opt attribute ((unused)), const char* Arg)
1063 /* Create a map file */
1065 /* Create a map file (linker) */
1066 CmdAddArg2 (&LD65, "-m", Arg);
1071 static void OptMemoryModel (const char* Opt attribute ((unused)), const char* Arg)
1072 /* Set the memory model */
1074 mmodel_t MemoryModel = FindMemoryModel (Arg);
1075 if (MemoryModel == MMODEL_UNKNOWN) {
1076 Error ("Unknown memory model: %s", Arg);
1077 } else if (MemoryModel == MMODEL_HUGE) {
1078 Error ("Unsupported memory model: %s", Arg);
1080 CmdAddArg2 (&CA65, "-mm", Arg);
1081 CmdAddArg2 (&CC65, "-mm", Arg);
1087 static void OptModule (const char* Opt attribute ((unused)),
1088 const char* Arg attribute ((unused)))
1089 /* Link as a module */
1096 static void OptModuleId (const char* Opt attribute ((unused)), const char* Arg)
1097 /* Specify a module if for the linker */
1099 /* Pass it straight to the linker */
1100 CmdAddArg2 (&LD65, "--module-id", Arg);
1105 static void OptO65Model (const char* Opt attribute ((unused)), const char* Arg)
1106 /* Handle the --o65-model option */
1108 CmdAddArg2 (&CO65, "-m", Arg);
1113 static void OptObj (const char* Opt attribute ((unused)), const char* Arg)
1114 /* Object file follows (linker) */
1116 CmdAddArg2 (&LD65, "--obj", Arg);
1121 static void OptObjPath (const char* Opt attribute ((unused)), const char* Arg)
1122 /* Object file search path (linker) */
1124 CmdAddArg2 (&LD65, "--obj-path", Arg);
1129 static void OptRegisterSpace (const char* Opt attribute ((unused)), const char* Arg)
1130 /* Handle the --register-space option */
1132 CmdAddArg2 (&CC65, "--register-space", Arg);
1137 static void OptRegisterVars (const char* Opt attribute ((unused)),
1138 const char* Arg attribute ((unused)))
1139 /* Handle the --register-vars option */
1141 CmdAddArg (&CC65, "-r");
1146 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
1147 /* Handle the --rodata-name option */
1149 CmdAddArg2 (&CC65, "--rodata-name", Arg);
1154 static void OptSignedChars (const char* Opt attribute ((unused)),
1155 const char* Arg attribute ((unused)))
1156 /* Make default characters signed */
1158 CmdAddArg (&CC65, "-j");
1163 static void OptStandard (const char* Opt attribute ((unused)), const char* Arg)
1164 /* Set the language standard */
1166 CmdAddArg2 (&CC65, "--standard", Arg);
1171 static void OptStartAddr (const char* Opt attribute ((unused)), const char* Arg)
1172 /* Set the default start address */
1174 CmdAddArg2 (&LD65, "-S", Arg);
1179 static void OptStaticLocals (const char* Opt attribute ((unused)),
1180 const char* Arg attribute ((unused)))
1181 /* Place local variables in static storage */
1183 CmdAddArg (&CC65, "-Cl");
1188 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
1189 /* Set the target system */
1191 Target = FindTarget (Arg);
1192 if (Target == TGT_UNKNOWN) {
1193 Error ("No such target system: `%s'", Arg);
1194 } else if (Target == TGT_MODULE) {
1195 Error ("Cannot use `module' as target, use --module instead");
1201 static void OptVerbose (const char* Opt attribute ((unused)),
1202 const char* Arg attribute ((unused)))
1203 /* Verbose mode (compiler, assembler, linker) */
1205 CmdAddArg (&CC65, "-v");
1206 CmdAddArg (&CA65, "-v");
1207 CmdAddArg (&CO65, "-v");
1208 CmdAddArg (&LD65, "-v");
1213 static void OptVersion (const char* Opt attribute ((unused)),
1214 const char* Arg attribute ((unused)))
1215 /* Print version number */
1217 fprintf (stderr, "cl65 V%s\n", GetVersionAsString ());
1222 static void OptZeropageLabel (const char* Opt attribute ((unused)), const char* Arg)
1223 /* Handle the --zeropage-label option */
1225 CmdAddArg2 (&CO65, "--zeropage-label", Arg);
1230 static void OptZeropageName (const char* Opt attribute ((unused)), const char* Arg)
1231 /* Handle the --zeropage-name option */
1233 CmdAddArg2 (&CO65, "--zeropage-name", Arg);
1238 int main (int argc, char* argv [])
1239 /* Utility main program */
1241 /* Program long options */
1242 static const LongOpt OptTab[] = {
1243 { "--add-source", 0, OptAddSource },
1244 { "--asm-args", 1, OptAsmArgs },
1245 { "--asm-define", 1, OptAsmDefine },
1246 { "--asm-include-dir", 1, OptAsmIncludeDir },
1247 { "--bin-include-dir", 1, OptBinIncludeDir },
1248 { "--bss-label", 1, OptBssLabel },
1249 { "--bss-name", 1, OptBssName },
1250 { "--cc-args", 1, OptCCArgs },
1251 { "--cfg-path", 1, OptCfgPath },
1252 { "--check-stack", 0, OptCheckStack },
1253 { "--code-label", 1, OptCodeLabel },
1254 { "--code-name", 1, OptCodeName },
1255 { "--codesize", 1, OptCodeSize },
1256 { "--config", 1, OptConfig },
1257 { "--cpu", 1, OptCPU },
1258 { "--create-dep", 1, OptCreateDep },
1259 { "--create-full-dep", 1, OptCreateFullDep },
1260 { "--data-label", 1, OptDataLabel },
1261 { "--data-name", 1, OptDataName },
1262 { "--debug", 0, OptDebug },
1263 { "--debug-info", 0, OptDebugInfo },
1264 { "--feature", 1, OptFeature },
1265 { "--force-import", 1, OptForceImport },
1266 { "--help", 0, OptHelp },
1267 { "--include-dir", 1, OptIncludeDir },
1268 { "--ld-args", 1, OptLdArgs },
1269 { "--lib", 1, OptLib },
1270 { "--lib-path", 1, OptLibPath },
1271 { "--list-targets", 0, OptListTargets },
1272 { "--listing", 1, OptListing },
1273 { "--list-bytes", 1, OptListBytes },
1274 { "--mapfile", 1, OptMapFile },
1275 { "--memory-model", 1, OptMemoryModel },
1276 { "--module", 0, OptModule },
1277 { "--module-id", 1, OptModuleId },
1278 { "--o65-model", 1, OptO65Model },
1279 { "--obj", 1, OptObj },
1280 { "--obj-path", 1, OptObjPath },
1281 { "--register-space", 1, OptRegisterSpace },
1282 { "--register-vars", 0, OptRegisterVars },
1283 { "--rodata-name", 1, OptRodataName },
1284 { "--signed-chars", 0, OptSignedChars },
1285 { "--standard", 1, OptStandard },
1286 { "--start-addr", 1, OptStartAddr },
1287 { "--static-locals", 0, OptStaticLocals },
1288 { "--target", 1, OptTarget },
1289 { "--verbose", 0, OptVerbose },
1290 { "--version", 0, OptVersion },
1291 { "--zeropage-label", 1, OptZeropageLabel },
1292 { "--zeropage-name", 1, OptZeropageName },
1298 /* Initialize the cmdline module */
1299 InitCmdLine (&argc, &argv, "cl65");
1301 /* Initialize the command descriptors */
1303 CmdPath = xstrdup ("");
1306 CmdPath = xstrdup (argv[0]);
1307 Ptr = strrchr (CmdPath, '/');
1309 Ptr = strrchr (CmdPath, '\\');
1317 CmdInit (&CC65, CmdPath, "cc65");
1318 CmdInit (&CA65, CmdPath, "ca65");
1319 CmdInit (&CO65, CmdPath, "co65");
1320 CmdInit (&LD65, CmdPath, "ld65");
1321 CmdInit (&GRC, CmdPath, "grc65");
1324 /* Our default target is the C64 instead of "none" */
1327 /* Check the parameters */
1329 while (I < ArgCount) {
1331 /* Get the argument */
1332 const char* Arg = ArgVec[I];
1334 /* Check for an option */
1335 if (Arg [0] == '-') {
1340 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
1344 if (Arg[2] == 'l' && Arg[3] == '\0') {
1345 /* Make local variables static */
1346 OptStaticLocals (Arg, 0);
1348 /* Specify linker config file */
1349 OptConfig (Arg, GetArg (&I, 2));
1354 /* Define a preprocessor symbol (compiler) */
1355 CmdAddArg2 (&CC65, "-D", GetArg (&I, 2));
1359 /* Include directory (compiler) */
1360 OptIncludeDir (Arg, GetArg (&I, 2));
1364 if (Arg[2] == 'n' && Arg[3] == '\0') {
1365 /* VICE label file (linker) */
1366 CmdAddArg2 (&LD65, "-Ln", GetArg (&I, 3));
1368 /* Library search path (linker) */
1369 OptLibPath (Arg, GetArg (&I, 2));
1374 /* Optimize code (compiler, also covers -Oi and others) */
1375 CmdAddArg (&CC65, Arg);
1379 /* Dont assemble and link the created files */
1385 /* Include source as comment (compiler) */
1386 OptAddSource (Arg, 0);
1390 /* Print version number */
1391 OptVersion (Arg, 0);
1395 if (Arg[2] == 'a' && Arg[3] == '\0') {
1396 /* -Wa: Pass options to assembler */
1397 OptAsmArgs (Arg, GetArg (&I, 3));
1398 } else if (Arg[2] == 'c' && Arg[3] == '\0') {
1399 /* -Wc: Pass options to compiler */
1400 OptCCArgs (Arg, GetArg (&I, 3));
1401 } else if (Arg[2] == 'l' && Arg[3] == '\0') {
1402 /* -Wl: Pass options to linker */
1403 OptLdArgs (Arg, GetArg (&I, 3));
1405 /* Anything else: Suppress warnings (compiler) */
1406 CmdAddArg2 (&CC65, "-W", GetArg (&I, 2));
1411 /* Don't link the resulting files */
1416 /* Debug mode (compiler) */
1421 /* Debugging - add to compiler and assembler */
1422 OptDebugInfo (Arg, 0);
1427 /* Print help - cl65 */
1432 /* Default characters are signed */
1433 OptSignedChars (Arg, 0);
1437 /* Create an assembler listing */
1438 OptListing (Arg, GetArg (&I, 2));
1442 /* Create a map file (linker) */
1443 OptMapFile (Arg, GetArg (&I, 2));
1447 /* Name the output file */
1448 OutputName = GetArg (&I, 2);
1452 /* Enable register variables */
1453 OptRegisterVars (Arg, 0);
1457 /* Set target system - compiler, assembler and linker */
1458 OptTarget (Arg, GetArg (&I, 2));
1462 /* Force an import (linker) */
1463 OptForceImport (Arg, GetArg (&I, 2));
1467 if (Arg [2] == 'm') {
1468 /* Verbose map file (linker) */
1469 CmdAddArg (&LD65, "-vm");
1471 /* Verbose mode (compiler, assembler, linker) */
1472 OptVerbose (Arg, 0);
1477 UnknownOption (Arg);
1481 /* Remember the first file name */
1482 if (FirstInput == 0) {
1486 /* Determine the file type by the extension */
1487 switch (GetFileType (Arg)) {
1490 /* Compile the file */
1495 /* Assemble the file */
1503 /* Add to the linker files */
1504 CmdAddFile (&LD65, Arg);
1508 /* Add to the resource compiler files */
1513 /* Add the the object file converter files */
1518 Error ("Don't know what to do with `%s'", Arg);
1528 /* Check if we had any input files */
1529 if (FirstInput == 0) {
1530 Warning ("No input files");
1533 /* Link the given files if requested and if we have any */
1534 if (DoLink && LD65.FileCount > 0) {
1538 /* Return an apropriate exit code */
1539 return EXIT_SUCCESS;