1 /*****************************************************************************/
5 /* Main module for the cl65 compile-and-link utility */
9 /* (C) 1999-2012, 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
39 #if defined(__WATCOMC__) || defined(_MSC_VER) || defined(__MINGW32__) || defined(__DJGPP__)
73 /*****************************************************************************/
75 /*****************************************************************************/
79 /* Struct that describes a command */
80 typedef struct CmdDesc CmdDesc;
82 char* Name; /* The command name */
84 unsigned ArgCount; /* Count of arguments */
85 unsigned ArgMax; /* Maximum count of arguments */
86 char** Args; /* The arguments */
88 unsigned FileCount; /* Count of files to translate */
89 unsigned FileMax; /* Maximum count of files */
90 char** Files; /* The files */
93 /* Command descriptors for the different programs */
94 static CmdDesc CC65 = { 0, 0, 0, 0, 0, 0, 0 };
95 static CmdDesc CA65 = { 0, 0, 0, 0, 0, 0, 0 };
96 static CmdDesc CO65 = { 0, 0, 0, 0, 0, 0, 0 };
97 static CmdDesc LD65 = { 0, 0, 0, 0, 0, 0, 0 };
98 static CmdDesc GRC = { 0, 0, 0, 0, 0, 0, 0 };
100 /* Variables controlling the steps we're doing */
101 static int DoLink = 1;
102 static int DoAssemble = 1;
104 /* The name of the output file, NULL if none given */
105 static const char* OutputName = 0;
107 /* The name of the linker configuration file if given */
108 static const char* LinkerConfig = 0;
110 /* The name of the first input file. This will be used to construct the
111 * executable file name if no explicit name is given.
113 static const char* FirstInput = 0;
115 /* The names of the files for dependency generation */
116 static const char* DepName = 0;
117 static const char* FullDepName = 0;
119 /* Remember if we should link a module */
120 static int Module = 0;
122 /* Extension used for a module */
123 #define MODULE_EXT ".o65"
125 /* Name of the target specific runtime library */
126 static char* TargetLib = 0;
130 /*****************************************************************************/
131 /* Include the system specific spawn function */
132 /*****************************************************************************/
136 #if defined(NEED_SPAWN)
137 # if defined(SPAWN_UNIX)
138 # include "spawn-unix.inc"
139 # elif defined(SPAWN_AMIGA)
140 # include "spawn-amiga.inc"
142 # error "Don't know which spawn module to include!"
148 /*****************************************************************************/
149 /* Command structure handling */
150 /*****************************************************************************/
154 static void CmdExpand (CmdDesc* Cmd)
155 /* Expand the argument vector */
157 unsigned NewMax = Cmd->ArgMax + 10;
158 char** NewArgs = xmalloc (NewMax * sizeof (char*));
159 memcpy (NewArgs, Cmd->Args, Cmd->ArgMax * sizeof (char*));
162 Cmd->ArgMax = NewMax;
167 static void CmdAddArg (CmdDesc* Cmd, const char* Arg)
168 /* Add a new argument to the command */
170 /* Expand the argument vector if needed */
171 if (Cmd->ArgCount >= Cmd->ArgMax) {
175 /* Add a copy of the new argument, allow a NULL pointer */
177 Cmd->Args[Cmd->ArgCount++] = xstrdup (Arg);
179 Cmd->Args[Cmd->ArgCount++] = 0;
185 static void CmdAddArg2 (CmdDesc* Cmd, const char* Arg1, const char* Arg2)
186 /* Add a new argument pair to the command */
188 CmdAddArg (Cmd, Arg1);
189 CmdAddArg (Cmd, Arg2);
194 static void CmdAddArgList (CmdDesc* Cmd, const char* ArgList)
195 /* Add a list of arguments separated by commas */
197 const char* Arg = ArgList;
201 if (*P == '\0' || *P == ',') {
203 /* End of argument, add it */
204 unsigned Len = P - Arg;
206 /* Expand the argument vector if needed */
207 if (Cmd->ArgCount >= Cmd->ArgMax) {
211 /* Add the new argument */
212 Cmd->Args[Cmd->ArgCount] = memcpy (xmalloc (Len + 1), Arg, Len);
213 Cmd->Args[Cmd->ArgCount][Len] = '\0';
216 /* If the argument was terminated by a comma, skip it, otherwise
220 /* Start over at next char */
226 /* Skip other chars */
235 static void CmdDelArgs (CmdDesc* Cmd, unsigned LastValid)
236 /* Remove all arguments with an index greater than LastValid */
238 while (Cmd->ArgCount > LastValid) {
240 xfree (Cmd->Args [Cmd->ArgCount]);
241 Cmd->Args [Cmd->ArgCount] = 0;
247 static void CmdAddFile (CmdDesc* Cmd, const char* File)
248 /* Add a new file to the command */
250 /* Expand the file vector if needed */
251 if (Cmd->FileCount == Cmd->FileMax) {
252 unsigned NewMax = Cmd->FileMax + 10;
253 char** NewFiles = xmalloc (NewMax * sizeof (char*));
254 memcpy (NewFiles, Cmd->Files, Cmd->FileMax * sizeof (char*));
256 Cmd->Files = NewFiles;
257 Cmd->FileMax = NewMax;
260 /* If the file name is not NULL (which is legal and is used to terminate
261 * the file list), check if the file name does already exist in the file
262 * list and print a warning if so. Regardless of the search result, add
267 for (I = 0; I < Cmd->FileCount; ++I) {
268 if (strcmp (Cmd->Files[I], File) == 0) {
270 Warning ("Duplicate file in argument list: `%s'", File);
271 /* No need to search further */
277 Cmd->Files [Cmd->FileCount++] = xstrdup (File);
279 /* Add a NULL pointer */
280 Cmd->Files [Cmd->FileCount++] = 0;
286 static void CmdInit (CmdDesc* Cmd, const char* Path)
287 /* Initialize the command using the given path to the executable */
289 /* Remember the command */
290 Cmd->Name = xstrdup (Path);
292 /* Use the command name as first argument */
293 CmdAddArg (Cmd, Path);
298 static void CmdSetOutput (CmdDesc* Cmd, const char* File)
299 /* Set the output file in a command desc */
301 CmdAddArg2 (Cmd, "-o", File);
306 static void CmdSetTarget (CmdDesc* Cmd, target_t Target)
307 /* Set the output file in a command desc */
309 CmdAddArg2 (Cmd, "-t", GetTargetName (Target));
314 static void CmdPrint (CmdDesc* Cmd, FILE* F)
315 /* Output the command line encoded in the command desc */
318 for (I = 0; I < Cmd->ArgCount && Cmd->Args[I] != 0; ++I) {
319 fprintf (F, "%s ", Cmd->Args[I]);
325 /*****************************************************************************/
326 /* Target handling */
327 /*****************************************************************************/
331 static void SetTargetFiles (void)
332 /* Set the target system files */
334 /* Determine the names of the target specific library file */
335 if (Target != TGT_NONE) {
337 /* Get a pointer to the system name and its length */
338 const char* TargetName = GetTargetName (Target);
339 unsigned TargetNameLen = strlen (TargetName);
341 /* Set the library file */
342 TargetLib = xmalloc (TargetNameLen + 4 + 1);
343 memcpy (TargetLib, TargetName, TargetNameLen);
344 strcpy (TargetLib + TargetNameLen, ".lib");
351 /*****************************************************************************/
353 /*****************************************************************************/
357 static void ExecProgram (CmdDesc* Cmd)
358 /* Execute a subprocess with the given name/parameters. Exit on errors. */
362 /* If in debug mode, output the command line we will execute */
364 printf ("Executing: ");
365 CmdPrint (Cmd, stdout);
369 /* Call the program */
370 Status = spawnvp (P_WAIT, Cmd->Name, Cmd->Args);
372 /* Check the result code */
374 /* Error executing the program */
375 Error ("Cannot execute `%s': %s", Cmd->Name, strerror (errno));
376 } else if (Status != 0) {
377 /* Called program had an error */
384 static void Link (void)
385 /* Link the resulting executable */
389 /* Since linking is always the final step, if we have an output file name
390 * given, set it here. If we don't have an explicit output name given,
391 * try to build one from the name of the first input file.
395 CmdSetOutput (&LD65, OutputName);
397 } else if (FirstInput && FindExt (FirstInput)) { /* Only if ext present! */
399 const char* Extension = Module? MODULE_EXT : "";
400 char* Output = MakeFilename (FirstInput, Extension);
401 CmdSetOutput (&LD65, Output);
406 /* If we have a linker config file given, add it to the command line.
407 * Otherwise pass the target to the linker if we have one.
411 Error ("Cannot use -C and --module together");
413 CmdAddArg2 (&LD65, "-C", LinkerConfig);
415 CmdSetTarget (&LD65, TGT_MODULE);
417 CmdSetTarget (&LD65, Target);
420 /* Determine which target libraries are needed */
423 /* Add all object files as parameters */
424 for (I = 0; I < LD65.FileCount; ++I) {
425 CmdAddArg (&LD65, LD65.Files [I]);
428 /* Add the system runtime library */
430 CmdAddArg (&LD65, TargetLib);
433 /* Terminate the argument list with a NULL pointer */
434 CmdAddArg (&LD65, 0);
436 /* Call the linker */
442 static void AssembleFile (const char* File, unsigned ArgCount)
443 /* Common routine to assemble a file. Will be called by Assemble() and
444 * AssembleIntermediate(). Adds options common for both routines and
445 * assembles the file. Will remove excess arguments after assembly.
448 /* Set the target system */
449 CmdSetTarget (&CA65, Target);
451 /* Check if this is the last processing step */
453 /* We're linking later. Add the output file of the assembly
454 * the the file list of the linker. The name of the output
455 * file is that of the input file with ".s" replaced by ".o".
457 char* ObjName = MakeFilename (File, ".o");
458 CmdAddFile (&LD65, ObjName);
461 /* This is the final step. If an output name is given, set it */
463 CmdSetOutput (&CA65, OutputName);
467 /* Add the file as argument for the assembler */
468 CmdAddArg (&CA65, File);
470 /* Add a NULL pointer to terminate the argument list */
471 CmdAddArg (&CA65, 0);
473 /* Run the assembler */
476 /* Remove the excess arguments */
477 CmdDelArgs (&CA65, ArgCount);
482 static void AssembleIntermediate (const char* SourceFile)
483 /* Assemble an intermediate file which was generated by a previous processing
484 * step with SourceFile as input. The -dep options won't be added and
485 * the intermediate assembler file is removed after assembly.
488 /* Generate the name of the assembler output file from the source file
489 * name. It's the same name with the extension replaced by ".s"
491 char* AsmName = MakeFilename (SourceFile, ".s");
493 /* Assemble the intermediate assembler file */
494 AssembleFile (AsmName, CA65.ArgCount);
496 /* Remove the input file */
497 if (remove (AsmName) < 0) {
498 Warning ("Cannot remove temporary file `%s': %s",
499 AsmName, strerror (errno));
502 /* Free the assembler file name which was allocated from the heap */
508 static void Assemble (const char* File)
509 /* Assemble the given file */
511 /* Remember the current assembler argument count */
512 unsigned ArgCount = CA65.ArgCount;
514 /* We aren't assembling an intermediate file, but one requested by the
515 * user. So add a few options here if they were given on the command
518 if (DepName && *DepName) {
519 CmdAddArg2 (&CA65, "--create-dep", DepName);
521 if (FullDepName && *FullDepName) {
522 CmdAddArg2 (&CA65, "--create-full-dep", FullDepName);
525 /* Use the common routine */
526 AssembleFile (File, ArgCount);
531 static void Compile (const char* File)
532 /* Compile the given file */
534 /* Remember the current compiler argument count */
535 unsigned ArgCount = CC65.ArgCount;
537 /* Set the target system */
538 CmdSetTarget (&CC65, Target);
540 /* Check if this is the final step */
542 /* We will assemble this file later. If a dependency file is to be
543 * generated, set the dependency target to be the final object file,
544 * not the intermediate assembler file. But beware: There may be an
545 * output name specified for the assembler.
547 if (DepName || FullDepName) {
548 /* Was an output name for the assembler specified? */
549 if (!DoLink && OutputName) {
550 /* Use this name as the dependency target */
551 CmdAddArg2 (&CC65, "--dep-target", OutputName);
553 /* Use the object file name as the dependency target */
554 char* ObjName = MakeFilename (File, ".o");
555 CmdAddArg2 (&CC65, "--dep-target", ObjName);
560 /* If we won't assemble, this is the final step. In this case, set
561 * the output name if it was given.
564 CmdSetOutput (&CC65, OutputName);
568 /* Add the file as argument for the compiler */
569 CmdAddArg (&CC65, File);
571 /* Add a NULL pointer to terminate the argument list */
572 CmdAddArg (&CC65, 0);
574 /* Run the compiler */
577 /* Remove the excess arguments */
578 CmdDelArgs (&CC65, ArgCount);
580 /* If this is not the final step, assemble the generated file, then
584 /* Assemble the intermediate file and remove it */
585 AssembleIntermediate (File);
591 static void CompileRes (const char* File)
592 /* Compile the given geos resource file */
594 /* Remember the current assembler argument count */
595 unsigned ArgCount = GRC.ArgCount;
597 /* Resource files need an geos-apple or geos-cbm target */
598 if (Target != TGT_GEOS_APPLE && Target != TGT_GEOS_CBM) {
599 Error ("Resource files need a geos-apple or geos-cbm, target");
601 CmdSetTarget (&GRC, Target);
603 /* Add the file as argument for the resource compiler */
604 CmdAddArg (&GRC, File);
606 /* Add a NULL pointer to terminate the argument list */
609 /* Run the compiler */
612 /* Remove the excess arguments */
613 CmdDelArgs (&GRC, ArgCount);
615 /* If this is not the final step, assemble the generated file, then
619 /* Assemble the intermediate file and remove it */
620 AssembleIntermediate (File);
626 static void ConvertO65 (const char* File)
627 /* Convert an o65 object file into an assembler file */
629 /* Remember the current converter argument count */
630 unsigned ArgCount = CO65.ArgCount;
632 /* If we won't assemble, this is the final step. In this case, set the
635 if (!DoAssemble && OutputName) {
636 CmdSetOutput (&CO65, OutputName);
639 /* Add the file as argument for the object file converter */
640 CmdAddArg (&CO65, File);
642 /* Add a NULL pointer to terminate the argument list */
643 CmdAddArg (&CO65, 0);
645 /* Run the converter */
648 /* Remove the excess arguments */
649 CmdDelArgs (&CO65, ArgCount);
651 /* If this is not the final step, assemble the generated file, then
655 /* Assemble the intermediate file and remove it */
656 AssembleIntermediate (File);
662 /*****************************************************************************/
664 /*****************************************************************************/
668 static void Usage (void)
669 /* Print usage information and exit */
671 printf ("Usage: %s [options] file [...]\n"
673 " -c\t\t\t\tCompile and assemble, but don't link\n"
674 " -d\t\t\t\tDebug mode\n"
675 " -g\t\t\t\tAdd debug info\n"
676 " -h\t\t\t\tHelp (this text)\n"
677 " -l name\t\t\tCreate an assembler listing file\n"
678 " -m name\t\t\tCreate a map file\n"
679 " -mm model\t\t\tSet the memory model\n"
680 " -o name\t\t\tName the output file\n"
681 " -r\t\t\t\tEnable register variables\n"
682 " -t sys\t\t\tSet the target system\n"
683 " -u sym\t\t\tForce an import of symbol `sym'\n"
684 " -v\t\t\t\tVerbose mode\n"
685 " -vm\t\t\t\tVerbose map file\n"
686 " -C name\t\t\tUse linker config file\n"
687 " -Cl\t\t\t\tMake local variables static\n"
688 " -D sym[=defn]\t\t\tDefine a preprocessor symbol\n"
689 " -I dir\t\t\tSet a compiler include directory path\n"
690 " -L path\t\t\tSpecify a library search path\n"
691 " -Ln name\t\t\tCreate a VICE label file\n"
692 " -O\t\t\t\tOptimize code\n"
693 " -Oi\t\t\t\tOptimize code, inline runtime functions\n"
694 " -Or\t\t\t\tOptimize code, honour the register keyword\n"
695 " -Os\t\t\t\tOptimize code, inline known C functions\n"
696 " -S\t\t\t\tCompile, but don't assemble and link\n"
697 " -T\t\t\t\tInclude source as comment\n"
698 " -V\t\t\t\tPrint the version number\n"
699 " -W name[,...]\t\t\tSuppress compiler warnings\n"
700 " -Wa options\t\t\tPass options to the assembler\n"
701 " -Wl options\t\t\tPass options to the linker\n"
704 " --add-source\t\t\tInclude source as comment\n"
705 " --asm-args options\t\tPass options to the assembler\n"
706 " --asm-define sym[=v]\t\tDefine an assembler symbol\n"
707 " --asm-include-dir dir\t\tSet an assembler include directory\n"
708 " --bin-include-dir dir\t\tSet an assembler binary include directory\n"
709 " --bss-label name\t\tDefine and export a BSS segment label\n"
710 " --bss-name seg\t\tSet the name of the BSS segment\n"
711 " --cfg-path path\t\tSpecify a config file search path\n"
712 " --check-stack\t\t\tGenerate stack overflow checks\n"
713 " --code-label name\t\tDefine and export a CODE segment label\n"
714 " --code-name seg\t\tSet the name of the CODE segment\n"
715 " --codesize x\t\t\tAccept larger code by factor x\n"
716 " --config name\t\t\tUse linker config file\n"
717 " --cpu type\t\t\tSet CPU type\n"
718 " --create-dep name\t\tCreate a make dependency file\n"
719 " --create-full-dep name\tCreate a full make dependency file\n"
720 " --data-label name\t\tDefine and export a DATA segment label\n"
721 " --data-name seg\t\tSet the name of the DATA segment\n"
722 " --debug\t\t\tDebug mode\n"
723 " --debug-info\t\t\tAdd debug info\n"
724 " --feature name\t\tSet an emulation feature\n"
725 " --force-import sym\t\tForce an import of symbol `sym'\n"
726 " --forget-inc-paths\t\tForget include search paths (compiler)\n"
727 " --help\t\t\tHelp (this text)\n"
728 " --include-dir dir\t\tSet a compiler include directory path\n"
729 " --ld-args options\t\tPass options to the linker\n"
730 " --lib file\t\t\tLink this library\n"
731 " --lib-path path\t\tSpecify a library search path\n"
732 " --list-targets\t\tList all available targets\n"
733 " --listing name\t\tCreate an assembler listing file\n"
734 " --list-bytes n\t\tNumber of bytes per assembler listing line\n"
735 " --mapfile name\t\tCreate a map file\n"
736 " --memory-model model\t\tSet the memory model\n"
737 " --module\t\t\tLink as a module\n"
738 " --module-id id\t\tSpecify a module ID for the linker\n"
739 " --o65-model model\t\tOverride the o65 model\n"
740 " --obj file\t\t\tLink this object file\n"
741 " --obj-path path\t\tSpecify an object file search path\n"
742 " --register-space b\t\tSet space available for register variables\n"
743 " --register-vars\t\tEnable register variables\n"
744 " --rodata-name seg\t\tSet the name of the RODATA segment\n"
745 " --signed-chars\t\tDefault characters are signed\n"
746 " --standard std\t\tLanguage standard (c89, c99, cc65)\n"
747 " --start-addr addr\t\tSet the default start address\n"
748 " --static-locals\t\tMake local variables static\n"
749 " --target sys\t\t\tSet the target system\n"
750 " --version\t\t\tPrint the version number\n"
751 " --verbose\t\t\tVerbose mode\n"
752 " --zeropage-label name\t\tDefine and export a ZEROPAGE segment label\n"
753 " --zeropage-name seg\t\tSet the name of the ZEROPAGE segment\n",
759 static void OptAddSource (const char* Opt attribute ((unused)),
760 const char* Arg attribute ((unused)))
761 /* Strict source code as comments to the generated asm code */
763 CmdAddArg (&CC65, "-T");
768 static void OptAsmArgs (const char* Opt attribute ((unused)), const char* Arg)
769 /* Pass arguments to the assembler */
771 CmdAddArgList (&CA65, Arg);
776 static void OptAsmDefine (const char* Opt attribute ((unused)), const char* Arg)
777 /* Define an assembler symbol (assembler) */
779 CmdAddArg2 (&CA65, "-D", Arg);
784 static void OptAsmIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
785 /* Include directory (assembler) */
787 CmdAddArg2 (&CA65, "-I", Arg);
792 static void OptBinIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
793 /* Binary include directory (assembler) */
795 CmdAddArg2 (&CA65, "--bin-include-dir", Arg);
800 static void OptBssLabel (const char* Opt attribute ((unused)), const char* Arg)
801 /* Handle the --bss-label option */
803 CmdAddArg2 (&CO65, "--bss-label", Arg);
808 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
809 /* Handle the --bss-name option */
811 CmdAddArg2 (&CC65, "--bss-name", Arg);
812 CmdAddArg2 (&CO65, "--bss-name", Arg);
817 static void OptCfgPath (const char* Opt attribute ((unused)), const char* Arg)
818 /* Config file search path (linker) */
820 CmdAddArg2 (&LD65, "--cfg-path", Arg);
825 static void OptCheckStack (const char* Opt attribute ((unused)),
826 const char* Arg attribute ((unused)))
827 /* Handle the --check-stack option */
829 CmdAddArg (&CC65, "--check-stack");
834 static void OptCodeLabel (const char* Opt attribute ((unused)), const char* Arg)
835 /* Handle the --code-label option */
837 CmdAddArg2 (&CO65, "--code-label", Arg);
842 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
843 /* Handle the --code-name option */
845 CmdAddArg2 (&CC65, "--code-name", Arg);
846 CmdAddArg2 (&CO65, "--code-name", Arg);
851 static void OptCodeSize (const char* Opt attribute ((unused)), const char* Arg)
852 /* Handle the --codesize option */
854 CmdAddArg2 (&CC65, "--codesize", Arg);
859 static void OptConfig (const char* Opt attribute ((unused)), const char* Arg)
860 /* Config file (linker) */
863 Error ("Cannot specify -C/--config twice");
870 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
871 /* Handle the --cpu option */
873 /* Add the cpu type to the assembler and compiler */
874 CmdAddArg2 (&CA65, "--cpu", Arg);
875 CmdAddArg2 (&CC65, "--cpu", Arg);
880 static void OptCreateDep (const char* Opt attribute ((unused)), const char* Arg)
881 /* Handle the --create-dep option */
883 /* Add the file name to the compiler */
884 CmdAddArg2 (&CC65, "--create-dep", Arg);
886 /* Remember the file name for the assembler */
892 static void OptCreateFullDep (const char* Opt attribute ((unused)), const char* Arg)
893 /* Handle the --create-full-dep option */
895 /* Add the file name to the compiler */
896 CmdAddArg2 (&CC65, "--create-full-dep", Arg);
898 /* Remember the file name for the assembler */
904 static void OptDataLabel (const char* Opt attribute ((unused)), const char* Arg)
905 /* Handle the --data-label option */
907 CmdAddArg2 (&CO65, "--data-label", Arg);
912 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
913 /* Handle the --data-name option */
915 CmdAddArg2 (&CC65, "--data-name", Arg);
916 CmdAddArg2 (&CO65, "--data-name", Arg);
921 static void OptDebug (const char* Opt attribute ((unused)),
922 const char* Arg attribute ((unused)))
923 /* Debug mode (compiler and cl65 utility) */
925 CmdAddArg (&CC65, "-d");
926 CmdAddArg (&CO65, "-d");
932 static void OptDebugInfo (const char* Opt attribute ((unused)),
933 const char* Arg attribute ((unused)))
934 /* Debug Info - add to compiler and assembler */
936 CmdAddArg (&CC65, "-g");
937 CmdAddArg (&CA65, "-g");
938 CmdAddArg (&CO65, "-g");
943 static void OptFeature (const char* Opt attribute ((unused)), const char* Arg)
944 /* Emulation features for the assembler */
946 CmdAddArg2 (&CA65, "--feature", Arg);
951 static void OptForceImport (const char* Opt attribute ((unused)), const char* Arg)
952 /* Emulation features for the assembler */
954 CmdAddArg2 (&LD65, "-u", Arg);
959 static void OptForgetIncPaths (const char* Opt attribute ((unused)),
960 const char* Arg attribute ((unused)))
961 /* Forget all currently defined include paths */
963 CmdAddArg (&CC65, "--forget-inc-paths");
968 static void OptHelp (const char* Opt attribute ((unused)),
969 const char* Arg attribute ((unused)))
970 /* Print help - cl65 */
978 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
979 /* Include directory (compiler) */
981 CmdAddArg2 (&CC65, "-I", Arg);
986 static void OptLdArgs (const char* Opt attribute ((unused)), const char* Arg)
987 /* Pass arguments to the linker */
989 CmdAddArgList (&LD65, Arg);
994 static void OptLib (const char* Opt attribute ((unused)), const char* Arg)
995 /* Library file follows (linker) */
997 CmdAddArg2 (&LD65, "--lib", Arg);
1002 static void OptLibPath (const char* Opt attribute ((unused)), const char* Arg)
1003 /* Library search path (linker) */
1005 CmdAddArg2 (&LD65, "--lib-path", Arg);
1010 static void OptListBytes (const char* Opt attribute ((unused)), const char* Arg)
1011 /* Set the maximum number of bytes per asm listing line */
1013 CmdAddArg2 (&CA65, "--list-bytes", Arg);
1018 static void OptListing (const char* Opt attribute ((unused)), const char* Arg)
1019 /* Create an assembler listing */
1021 CmdAddArg2 (&CA65, "-l", Arg);
1026 static void OptListTargets (const char* Opt attribute ((unused)),
1027 const char* Arg attribute ((unused)))
1028 /* List all targets */
1032 /* List the targets */
1033 for (T = TGT_NONE; T < TGT_COUNT; ++T) {
1034 printf ("%s\n", GetTargetName (T));
1038 exit (EXIT_SUCCESS);
1043 static void OptMapFile (const char* Opt attribute ((unused)), const char* Arg)
1044 /* Create a map file */
1046 /* Create a map file (linker) */
1047 CmdAddArg2 (&LD65, "-m", Arg);
1052 static void OptMemoryModel (const char* Opt attribute ((unused)), const char* Arg)
1053 /* Set the memory model */
1055 mmodel_t MemoryModel = FindMemoryModel (Arg);
1056 if (MemoryModel == MMODEL_UNKNOWN) {
1057 Error ("Unknown memory model: %s", Arg);
1058 } else if (MemoryModel == MMODEL_HUGE) {
1059 Error ("Unsupported memory model: %s", Arg);
1061 CmdAddArg2 (&CA65, "-mm", Arg);
1062 CmdAddArg2 (&CC65, "-mm", Arg);
1068 static void OptModule (const char* Opt attribute ((unused)),
1069 const char* Arg attribute ((unused)))
1070 /* Link as a module */
1077 static void OptModuleId (const char* Opt attribute ((unused)), const char* Arg)
1078 /* Specify a module if for the linker */
1080 /* Pass it straight to the linker */
1081 CmdAddArg2 (&LD65, "--module-id", Arg);
1086 static void OptO65Model (const char* Opt attribute ((unused)), const char* Arg)
1087 /* Handle the --o65-model option */
1089 CmdAddArg2 (&CO65, "-m", Arg);
1094 static void OptObj (const char* Opt attribute ((unused)), const char* Arg)
1095 /* Object file follows (linker) */
1097 CmdAddArg2 (&LD65, "--obj", Arg);
1102 static void OptObjPath (const char* Opt attribute ((unused)), const char* Arg)
1103 /* Object file search path (linker) */
1105 CmdAddArg2 (&LD65, "--obj-path", Arg);
1110 static void OptRegisterSpace (const char* Opt attribute ((unused)), const char* Arg)
1111 /* Handle the --register-space option */
1113 CmdAddArg2 (&CC65, "--register-space", Arg);
1118 static void OptRegisterVars (const char* Opt attribute ((unused)),
1119 const char* Arg attribute ((unused)))
1120 /* Handle the --register-vars option */
1122 CmdAddArg (&CC65, "-r");
1127 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
1128 /* Handle the --rodata-name option */
1130 CmdAddArg2 (&CC65, "--rodata-name", Arg);
1135 static void OptSignedChars (const char* Opt attribute ((unused)),
1136 const char* Arg attribute ((unused)))
1137 /* Make default characters signed */
1139 CmdAddArg (&CC65, "-j");
1144 static void OptStandard (const char* Opt attribute ((unused)), const char* Arg)
1145 /* Set the language standard */
1147 CmdAddArg2 (&CC65, "--standard", Arg);
1152 static void OptStartAddr (const char* Opt attribute ((unused)), const char* Arg)
1153 /* Set the default start address */
1155 CmdAddArg2 (&LD65, "-S", Arg);
1160 static void OptStaticLocals (const char* Opt attribute ((unused)),
1161 const char* Arg attribute ((unused)))
1162 /* Place local variables in static storage */
1164 CmdAddArg (&CC65, "-Cl");
1169 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
1170 /* Set the target system */
1172 Target = FindTarget (Arg);
1173 if (Target == TGT_UNKNOWN) {
1174 Error ("No such target system: `%s'", Arg);
1175 } else if (Target == TGT_MODULE) {
1176 Error ("Cannot use `module' as target, use --module instead");
1182 static void OptVerbose (const char* Opt attribute ((unused)),
1183 const char* Arg attribute ((unused)))
1184 /* Verbose mode (compiler, assembler, linker) */
1186 CmdAddArg (&CC65, "-v");
1187 CmdAddArg (&CA65, "-v");
1188 CmdAddArg (&CO65, "-v");
1189 CmdAddArg (&LD65, "-v");
1194 static void OptVersion (const char* Opt attribute ((unused)),
1195 const char* Arg attribute ((unused)))
1196 /* Print version number */
1199 "cl65 V%s - (C) Copyright 1998-2011 Ullrich von Bassewitz\n",
1200 GetVersionAsString ());
1205 static void OptZeropageLabel (const char* Opt attribute ((unused)), const char* Arg)
1206 /* Handle the --zeropage-label option */
1208 CmdAddArg2 (&CO65, "--zeropage-label", Arg);
1213 static void OptZeropageName (const char* Opt attribute ((unused)), const char* Arg)
1214 /* Handle the --zeropage-name option */
1216 CmdAddArg2 (&CO65, "--zeropage-name", Arg);
1221 int main (int argc, char* argv [])
1222 /* Utility main program */
1224 /* Program long options */
1225 static const LongOpt OptTab[] = {
1226 { "--add-source", 0, OptAddSource },
1227 { "--asm-args", 1, OptAsmArgs },
1228 { "--asm-define", 1, OptAsmDefine },
1229 { "--asm-include-dir", 1, OptAsmIncludeDir },
1230 { "--bin-include-dir", 1, OptBinIncludeDir },
1231 { "--bss-label", 1, OptBssLabel },
1232 { "--bss-name", 1, OptBssName },
1233 { "--cfg-path", 1, OptCfgPath },
1234 { "--check-stack", 0, OptCheckStack },
1235 { "--code-label", 1, OptCodeLabel },
1236 { "--code-name", 1, OptCodeName },
1237 { "--codesize", 1, OptCodeSize },
1238 { "--config", 1, OptConfig },
1239 { "--cpu", 1, OptCPU },
1240 { "--create-dep", 1, OptCreateDep },
1241 { "--create-full-dep", 1, OptCreateFullDep },
1242 { "--data-label", 1, OptDataLabel },
1243 { "--data-name", 1, OptDataName },
1244 { "--debug", 0, OptDebug },
1245 { "--debug-info", 0, OptDebugInfo },
1246 { "--feature", 1, OptFeature },
1247 { "--force-import", 1, OptForceImport },
1248 { "--forget-inc-paths", 0, OptForgetIncPaths },
1249 { "--help", 0, OptHelp },
1250 { "--include-dir", 1, OptIncludeDir },
1251 { "--ld-args", 1, OptLdArgs },
1252 { "--lib", 1, OptLib },
1253 { "--lib-path", 1, OptLibPath },
1254 { "--list-targets", 0, OptListTargets },
1255 { "--listing", 1, OptListing },
1256 { "--list-bytes", 1, OptListBytes },
1257 { "--mapfile", 1, OptMapFile },
1258 { "--memory-model", 1, OptMemoryModel },
1259 { "--module", 0, OptModule },
1260 { "--module-id", 1, OptModuleId },
1261 { "--o65-model", 1, OptO65Model },
1262 { "--obj", 1, OptObj },
1263 { "--obj-path", 1, OptObjPath },
1264 { "--register-space", 1, OptRegisterSpace },
1265 { "--register-vars", 0, OptRegisterVars },
1266 { "--rodata-name", 1, OptRodataName },
1267 { "--signed-chars", 0, OptSignedChars },
1268 { "--standard", 1, OptStandard },
1269 { "--start-addr", 1, OptStartAddr },
1270 { "--static-locals", 0, OptStaticLocals },
1271 { "--target", 1, OptTarget },
1272 { "--verbose", 0, OptVerbose },
1273 { "--version", 0, OptVersion },
1274 { "--zeropage-label", 1, OptZeropageLabel },
1275 { "--zeropage-name", 1, OptZeropageName },
1280 /* Initialize the cmdline module */
1281 InitCmdLine (&argc, &argv, "cl65");
1283 /* Initialize the command descriptors */
1284 CmdInit (&CC65, "cc65");
1285 CmdInit (&CA65, "ca65");
1286 CmdInit (&CO65, "co65");
1287 CmdInit (&LD65, "ld65");
1288 CmdInit (&GRC, "grc65");
1290 /* Our default target is the C64 instead of "none" */
1293 /* Check the parameters */
1295 while (I < ArgCount) {
1297 /* Get the argument */
1298 const char* Arg = ArgVec[I];
1300 /* Check for an option */
1301 if (Arg [0] == '-') {
1306 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
1310 if (Arg[2] == 'l' && Arg[3] == '\0') {
1311 /* Make local variables static */
1312 OptStaticLocals (Arg, 0);
1314 /* Specify linker config file */
1315 OptConfig (Arg, GetArg (&I, 2));
1320 /* Define a preprocessor symbol (compiler) */
1321 CmdAddArg2 (&CC65, "-D", GetArg (&I, 2));
1325 /* Include directory (compiler) */
1326 OptIncludeDir (Arg, GetArg (&I, 2));
1330 if (Arg[2] == 'n' && Arg[3] == '\0') {
1331 /* VICE label file (linker) */
1332 CmdAddArg2 (&LD65, "-Ln", GetArg (&I, 3));
1334 /* Library search path (linker) */
1335 OptLibPath (Arg, GetArg (&I, 2));
1340 /* Optimize code (compiler, also covers -Oi and others) */
1341 CmdAddArg (&CC65, Arg);
1345 /* Dont assemble and link the created files */
1351 /* Include source as comment (compiler) */
1352 OptAddSource (Arg, 0);
1356 /* Print version number */
1357 OptVersion (Arg, 0);
1361 if (Arg[2] == 'a' && Arg[3] == '\0') {
1362 /* -Wa: Pass options to assembler */
1363 OptAsmArgs (Arg, GetArg (&I, 3));
1364 } else if (Arg[2] == 'l' && Arg[3] == '\0') {
1365 /* -Wl: Pass options to linker */
1366 OptLdArgs (Arg, GetArg (&I, 3));
1368 /* Anything else: Suppress warnings (compiler) */
1369 CmdAddArg2 (&CC65, "-W", GetArg (&I, 2));
1374 /* Don't link the resulting files */
1379 /* Debug mode (compiler) */
1384 /* Debugging - add to compiler and assembler */
1385 OptDebugInfo (Arg, 0);
1390 /* Print help - cl65 */
1395 /* Default characters are signed */
1396 OptSignedChars (Arg, 0);
1400 /* Create an assembler listing */
1401 OptListing (Arg, GetArg (&I, 2));
1405 /* Create a map file (linker) */
1406 OptMapFile (Arg, GetArg (&I, 2));
1410 /* Name the output file */
1411 OutputName = GetArg (&I, 2);
1415 /* Enable register variables */
1416 OptRegisterVars (Arg, 0);
1420 /* Set target system - compiler, assembler and linker */
1421 OptTarget (Arg, GetArg (&I, 2));
1425 /* Force an import (linker) */
1426 OptForceImport (Arg, GetArg (&I, 2));
1430 if (Arg [2] == 'm') {
1431 /* Verbose map file (linker) */
1432 CmdAddArg (&LD65, "-vm");
1434 /* Verbose mode (compiler, assembler, linker) */
1435 OptVerbose (Arg, 0);
1440 UnknownOption (Arg);
1444 /* Remember the first file name */
1445 if (FirstInput == 0) {
1449 /* Determine the file type by the extension */
1450 switch (GetFileType (Arg)) {
1453 /* Compile the file */
1458 /* Assemble the file */
1466 /* Add to the linker files */
1467 CmdAddFile (&LD65, Arg);
1471 /* Add to the resource compiler files */
1476 /* Add the the object file converter files */
1481 Error ("Don't know what to do with `%s'", Arg);
1491 /* Check if we had any input files */
1492 if (FirstInput == 0) {
1493 Warning ("No input files");
1496 /* Link the given files if requested and if we have any */
1497 if (DoLink && LD65.FileCount > 0) {
1501 /* Return an apropriate exit code */
1502 return EXIT_SUCCESS;