1 /*****************************************************************************/
5 /* Main module for the cl65 compile and link utility */
9 /* (C) 1999-2003 Ullrich von Bassewitz */
10 /* Römerstrasse 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__)
72 /*****************************************************************************/
74 /*****************************************************************************/
78 /* Struct that describes a command */
79 typedef struct CmdDesc CmdDesc;
81 char* Name; /* The command name */
83 unsigned ArgCount; /* Count of arguments */
84 unsigned ArgMax; /* Maximum count of arguments */
85 char** Args; /* The arguments */
87 unsigned FileCount; /* Count of files to translate */
88 unsigned FileMax; /* Maximum count of files */
89 char** Files; /* The files */
92 /* Command descriptors for the different programs */
93 static CmdDesc CC65 = { 0, 0, 0, 0, 0, 0, 0 };
94 static CmdDesc CA65 = { 0, 0, 0, 0, 0, 0, 0 };
95 static CmdDesc CO65 = { 0, 0, 0, 0, 0, 0, 0 };
96 static CmdDesc LD65 = { 0, 0, 0, 0, 0, 0, 0 };
97 static CmdDesc GRC = { 0, 0, 0, 0, 0, 0, 0 };
99 /* Variables controlling the steps we're doing */
100 static int DontLink = 0;
101 static int DontAssemble = 0;
103 /* The name of the output file, NULL if none given */
104 static const char* OutputName = 0;
106 /* The name of the linker configuration file if given */
107 static const char* LinkerConfig = 0;
109 /* The name of the first input file. This will be used to construct the
110 * executable file name if no explicit name is given.
112 static const char* FirstInput = 0;
114 /* Remember if we should link a module */
115 static int Module = 0;
117 /* Extension used for a module */
118 #define MODULE_EXT ".o65"
120 /* Name of the crt0 object file and the runtime library */
121 static char* TargetCRT0 = 0;
122 static char* TargetLib = 0;
126 /*****************************************************************************/
127 /* Include the system specific spawn function */
128 /*****************************************************************************/
132 #if defined(NEED_SPAWN)
133 # if defined(SPAWN_UNIX)
134 # include "spawn-unix.inc"
135 # elif defined(SPAWN_AMIGA)
136 # include "spawn-amiga.inc"
138 # error "Don't know which spawn module to include!"
144 /*****************************************************************************/
145 /* Command structure handling */
146 /*****************************************************************************/
150 static void CmdAddArg (CmdDesc* Cmd, const char* Arg)
151 /* Add a new argument to the command */
153 /* Expand the argument vector if needed */
154 if (Cmd->ArgCount == Cmd->ArgMax) {
155 unsigned NewMax = Cmd->ArgMax + 10;
156 char** NewArgs = xmalloc (NewMax * sizeof (char*));
157 memcpy (NewArgs, Cmd->Args, Cmd->ArgMax * sizeof (char*));
160 Cmd->ArgMax = NewMax;
163 /* Add a copy of the new argument, allow a NULL pointer */
165 Cmd->Args [Cmd->ArgCount++] = xstrdup (Arg);
167 Cmd->Args [Cmd->ArgCount++] = 0;
173 static void CmdAddArg2 (CmdDesc* Cmd, const char* Arg1, const char* Arg2)
174 /* Add a new argument pair to the command */
176 CmdAddArg (Cmd, Arg1);
177 CmdAddArg (Cmd, Arg2);
182 static void CmdDelArgs (CmdDesc* Cmd, unsigned LastValid)
183 /* Remove all arguments with an index greater than LastValid */
185 while (Cmd->ArgCount > LastValid) {
187 xfree (Cmd->Args [Cmd->ArgCount]);
188 Cmd->Args [Cmd->ArgCount] = 0;
194 static void CmdAddFile (CmdDesc* Cmd, const char* File)
195 /* Add a new file to the command */
197 /* Expand the file vector if needed */
198 if (Cmd->FileCount == Cmd->FileMax) {
199 unsigned NewMax = Cmd->FileMax + 10;
200 char** NewFiles = xmalloc (NewMax * sizeof (char*));
201 memcpy (NewFiles, Cmd->Files, Cmd->FileMax * sizeof (char*));
203 Cmd->Files = NewFiles;
204 Cmd->FileMax = NewMax;
207 /* If the file name is not NULL (which is legal and is used to terminate
208 * the file list), check if the file name does already exist in the file
209 * list and print a warning if so. Regardless of the search result, add
214 for (I = 0; I < Cmd->FileCount; ++I) {
215 if (strcmp (Cmd->Files[I], File) == 0) {
217 Warning ("Duplicate file in argument list: `%s'", File);
218 /* No need to search further */
224 Cmd->Files [Cmd->FileCount++] = xstrdup (File);
226 /* Add a NULL pointer */
227 Cmd->Files [Cmd->FileCount++] = 0;
233 static void CmdInit (CmdDesc* Cmd, const char* Path)
234 /* Initialize the command using the given path to the executable */
236 /* Remember the command */
237 Cmd->Name = xstrdup (Path);
239 /* Use the command name as first argument */
240 CmdAddArg (Cmd, Path);
245 static void CmdSetOutput (CmdDesc* Cmd, const char* File)
246 /* Set the output file in a command desc */
248 CmdAddArg2 (Cmd, "-o", File);
253 static void CmdSetTarget (CmdDesc* Cmd, target_t Target)
254 /* Set the output file in a command desc */
256 CmdAddArg2 (Cmd, "-t", TargetNames[Target]);
261 static void CmdPrint (CmdDesc* Cmd, FILE* F)
262 /* Output the command line encoded in the command desc */
265 for (I = 0; I < Cmd->ArgCount && Cmd->Args[I] != 0; ++I) {
266 fprintf (F, "%s ", Cmd->Args[I]);
272 /*****************************************************************************/
273 /* Target handling */
274 /*****************************************************************************/
278 static void SetTargetFiles (void)
279 /* Set the target system files */
281 /* Determine the names of the default startup and library file */
282 if (Target != TGT_NONE) {
284 /* Get a pointer to the system name and its length */
285 const char* TargetName = TargetNames [Target];
286 unsigned TargetNameLen = strlen (TargetName);
288 /* Set the startup file */
289 TargetCRT0 = xmalloc (TargetNameLen + 2 + 1);
290 strcpy (TargetCRT0, TargetName);
291 strcat (TargetCRT0, ".o");
293 /* Set the library file */
294 TargetLib = xmalloc (TargetNameLen + 4 + 1);
295 strcpy (TargetLib, TargetName);
296 strcat (TargetLib, ".lib");
303 /*****************************************************************************/
305 /*****************************************************************************/
309 static void ExecProgram (CmdDesc* Cmd)
310 /* Execute a subprocess with the given name/parameters. Exit on errors. */
314 /* If in debug mode, output the command line we will execute */
316 printf ("Executing: ");
317 CmdPrint (Cmd, stdout);
321 /* Call the program */
322 Status = spawnvp (P_WAIT, Cmd->Name, Cmd->Args);
324 /* Check the result code */
326 /* Error executing the program */
327 Error ("Cannot execute `%s': %s", Cmd->Name, strerror (errno));
328 } else if (Status != 0) {
329 /* Called program had an error */
336 static void Link (void)
337 /* Link the resulting executable */
341 /* If we have a linker config file given, add it to the command line.
342 * Otherwise pass the target to the linker if we have one.
346 Error ("Cannot use -C and --module together");
348 CmdAddArg2 (&LD65, "-C", LinkerConfig);
350 CmdSetTarget (&LD65, TGT_MODULE);
351 } else if (Target != TGT_NONE) {
352 CmdSetTarget (&LD65, Target);
355 /* Determine which target libraries are needed */
358 /* Since linking is always the final step, if we have an output file name
359 * given, set it here. If we don't have an explicit output name given,
360 * try to build one from the name of the first input file.
364 CmdSetOutput (&LD65, OutputName);
366 } else if (FirstInput && FindExt (FirstInput)) { /* Only if ext present! */
368 const char* Extension = Module? MODULE_EXT : "";
369 char* Output = MakeFilename (FirstInput, Extension);
370 CmdSetOutput (&LD65, Output);
375 /* If we have a startup file and if we are not linking a module, add its
376 * name as a parameter
378 if (TargetCRT0 && !Module) {
379 CmdAddArg (&LD65, TargetCRT0);
382 /* Add all object files as parameters */
383 for (I = 0; I < LD65.FileCount; ++I) {
384 CmdAddArg (&LD65, LD65.Files [I]);
387 /* Add the system runtime library */
389 CmdAddArg (&LD65, TargetLib);
392 /* Terminate the argument list with a NULL pointer */
393 CmdAddArg (&LD65, 0);
395 /* Call the linker */
401 static void Assemble (const char* File)
402 /* Assemble the given file */
404 /* Remember the current assembler argument count */
405 unsigned ArgCount = CA65.ArgCount;
407 /* Set the target system */
408 CmdSetTarget (&CA65, Target);
410 /* If we won't link, this is the final step. In this case, set the
413 if (DontLink && OutputName) {
414 CmdSetOutput (&CA65, OutputName);
416 /* The object file name will be the name of the source file
417 * with .s replaced by ".o". Add this file to the list of
420 char* ObjName = MakeFilename (File, ".o");
421 CmdAddFile (&LD65, ObjName);
425 /* Add the file as argument for the assembler */
426 CmdAddArg (&CA65, File);
428 /* Add a NULL pointer to terminate the argument list */
429 CmdAddArg (&CA65, 0);
431 /* Run the assembler */
434 /* Remove the excess arguments */
435 CmdDelArgs (&CA65, ArgCount);
440 static void Compile (const char* File)
441 /* Compile the given file */
445 /* Remember the current compiler argument count */
446 unsigned ArgCount = CC65.ArgCount;
448 /* Set the target system */
449 CmdSetTarget (&CC65, Target);
451 /* If we won't link, this is the final step. In this case, set the
454 if (DontAssemble && OutputName) {
455 CmdSetOutput (&CC65, OutputName);
457 /* The assembler file name will be the name of the source file
458 * with .c replaced by ".s".
460 AsmName = MakeFilename (File, ".s");
463 /* Add the file as argument for the compiler */
464 CmdAddArg (&CC65, File);
466 /* Add a NULL pointer to terminate the argument list */
467 CmdAddArg (&CC65, 0);
469 /* Run the compiler */
472 /* Remove the excess arguments */
473 CmdDelArgs (&CC65, ArgCount);
475 /* If this is not the final step, assemble the generated file, then
480 if (remove (AsmName) < 0) {
481 Warning ("Cannot remove temporary file `%s': %s",
482 AsmName, strerror (errno));
490 static void CompileRes (const char* File)
491 /* Compile the given geos resource file */
495 /* Remember the current assembler argument count */
496 unsigned ArgCount = GRC.ArgCount;
498 /* The assembler file name will be the name of the source file
499 * with .grc replaced by ".s".
501 AsmName = MakeFilename (File, ".s");
503 /* Add the file as argument for the resource compiler */
504 CmdAddArg (&GRC, File);
506 /* Add a NULL pointer to terminate the argument list */
509 /* Run the compiler */
512 /* Remove the excess arguments */
513 CmdDelArgs (&GRC, ArgCount);
515 /* If this is not the final step, assemble the generated file, then
520 if (remove (AsmName) < 0) {
521 Warning ("Cannot remove temporary file `%s': %s",
522 AsmName, strerror (errno));
526 /* Free the assembler file name which was allocated from the heap */
532 static void ConvertO65 (const char* File)
533 /* Convert an o65 object file into an assembler file */
537 /* Remember the current converter argument count */
538 unsigned ArgCount = CO65.ArgCount;
540 /* If we won't link, this is the final step. In this case, set the
543 if (DontAssemble && OutputName) {
544 CmdSetOutput (&CO65, OutputName);
546 /* The assembler file name will be the name of the source file
547 * with .c replaced by ".s".
549 AsmName = MakeFilename (File, ".s");
552 /* Add the file as argument for the object file converter */
553 CmdAddArg (&CO65, File);
555 /* Add a NULL pointer to terminate the argument list */
556 CmdAddArg (&CO65, 0);
558 /* Run the converter */
561 /* Remove the excess arguments */
562 CmdDelArgs (&CO65, ArgCount);
564 /* If this is not the final step, assemble the generated file, then
569 if (remove (AsmName) < 0) {
570 Warning ("Cannot remove temporary file `%s': %s",
571 AsmName, strerror (errno));
575 /* Free the assembler file name which was allocated from the heap */
581 /*****************************************************************************/
583 /*****************************************************************************/
587 static void Usage (void)
588 /* Print usage information and exit */
591 "Usage: %s [options] file [...]\n"
593 " -c\t\t\tCompile and assemble but don't link\n"
594 " -d\t\t\tDebug mode\n"
595 " -g\t\t\tAdd debug info\n"
596 " -h\t\t\tHelp (this text)\n"
597 " -l\t\t\tCreate an assembler listing\n"
598 " -m name\t\tCreate a map file\n"
599 " -o name\t\tName the output file\n"
600 " -r\t\t\tEnable register variables\n"
601 " -t sys\t\tSet the target system\n"
602 " -v\t\t\tVerbose mode\n"
603 " -vm\t\t\tVerbose map file\n"
604 " -A\t\t\tStrict ANSI mode\n"
605 " -C name\t\tUse linker config file\n"
606 " -Cl\t\t\tMake local variables static\n"
607 " -D sym[=defn]\t\tDefine a preprocessor symbol\n"
608 " -I dir\t\tSet a compiler include directory path\n"
609 " -L path\t\tSpecify a library search path\n"
610 " -Ln name\t\tCreate a VICE label file\n"
611 " -O\t\t\tOptimize code\n"
612 " -Oi\t\t\tOptimize code, inline functions\n"
613 " -Or\t\t\tOptimize code, honour the register keyword\n"
614 " -Os\t\t\tOptimize code, inline known C funtions\n"
615 " -S\t\t\tCompile but don't assemble and link\n"
616 " -T\t\t\tInclude source as comment\n"
617 " -V\t\t\tPrint the version number\n"
618 " -W\t\t\tSuppress warnings\n"
621 " --add-source\t\tInclude source as comment\n"
622 " --ansi\t\tStrict ANSI mode\n"
623 " --asm-define sym[=v]\tDefine an assembler symbol\n"
624 " --asm-include-dir dir\tSet an assembler include directory\n"
625 " --bss-label name\tDefine and export a BSS segment label\n"
626 " --bss-name seg\tSet the name of the BSS segment\n"
627 " --cfg-path path\tSpecify a config file search path\n"
628 " --check-stack\t\tGenerate stack overflow checks\n"
629 " --code-label name\tDefine and export a CODE segment label\n"
630 " --code-name seg\tSet the name of the CODE segment\n"
631 " --codesize x\t\tAccept larger code by factor x\n"
632 " --config name\t\tUse linker config file\n"
633 " --cpu type\t\tSet cpu type\n"
634 " --create-dep\t\tCreate a make dependency file\n"
635 " --data-label name\tDefine and export a DATA segment label\n"
636 " --data-name seg\tSet the name of the DATA segment\n"
637 " --debug\t\tDebug mode\n"
638 " --debug-info\t\tAdd debug info\n"
639 " --feature name\tSet an emulation feature\n"
640 " --forget-inc-paths\tForget include search paths (compiler)\n"
641 " --help\t\tHelp (this text)\n"
642 " --include-dir dir\tSet a compiler include directory path\n"
643 " --lib file\t\tLink this library\n"
644 " --lib-path path\tSpecify a library search path\n"
645 " --listing\t\tCreate an assembler listing\n"
646 " --mapfile name\tCreate a map file\n"
647 " --module\t\tLink as a module\n"
648 " --module-id id\tSpecify a module id for the linker\n"
649 " --o65-model model\tOverride the o65 model\n"
650 " --obj file\t\tLink this object file\n"
651 " --obj-path path\tSpecify an object file search path\n"
652 " --register-space b\tSet space available for register variables\n"
653 " --register-vars\tEnable register variables\n"
654 " --rodata-name seg\tSet the name of the RODATA segment\n"
655 " --signed-chars\tDefault characters are signed\n"
656 " --start-addr addr\tSet the default start address\n"
657 " --static-locals\tMake local variables static\n"
658 " --target sys\t\tSet the target system\n"
659 " --version\t\tPrint the version number\n"
660 " --verbose\t\tVerbose mode\n"
661 " --zeropage-label name\tDefine and export a ZEROPAGE segment label\n"
662 " --zeropage-name seg\tSet the name of the ZEROPAGE segment\n",
668 static void OptAddSource (const char* Opt attribute ((unused)),
669 const char* Arg attribute ((unused)))
670 /* Strict source code as comments to the generated asm code */
672 CmdAddArg (&CC65, "-T");
677 static void OptAnsi (const char* Opt attribute ((unused)),
678 const char* Arg attribute ((unused)))
679 /* Strict ANSI mode (compiler) */
681 CmdAddArg (&CC65, "-A");
686 static void OptAsmDefine (const char* Opt attribute ((unused)), const char* Arg)
687 /* Define an assembler symbol (assembler) */
689 CmdAddArg2 (&CA65, "-D", Arg);
694 static void OptAsmIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
695 /* Include directory (assembler) */
697 CmdAddArg2 (&CA65, "-I", Arg);
702 static void OptBssLabel (const char* Opt attribute ((unused)), const char* Arg)
703 /* Handle the --bss-label option */
705 CmdAddArg2 (&CO65, "--bss-label", Arg);
710 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
711 /* Handle the --bss-name option */
713 CmdAddArg2 (&CC65, "--bss-name", Arg);
714 CmdAddArg2 (&CO65, "--bss-name", Arg);
719 static void OptCfgPath (const char* Opt attribute ((unused)), const char* Arg)
720 /* Config file search path (linker) */
722 CmdAddArg2 (&LD65, "--cfg-path", Arg);
727 static void OptCheckStack (const char* Opt attribute ((unused)),
728 const char* Arg attribute ((unused)))
729 /* Handle the --check-stack option */
731 CmdAddArg (&CC65, "--check-stack");
736 static void OptCodeLabel (const char* Opt attribute ((unused)), const char* Arg)
737 /* Handle the --code-label option */
739 CmdAddArg2 (&CO65, "--code-label", Arg);
744 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
745 /* Handle the --code-name option */
747 CmdAddArg2 (&CC65, "--code-name", Arg);
748 CmdAddArg2 (&CO65, "--code-name", Arg);
753 static void OptCodeSize (const char* Opt attribute ((unused)), const char* Arg)
754 /* Handle the --codesize option */
756 CmdAddArg2 (&CC65, "--codesize", Arg);
761 static void OptConfig (const char* Opt attribute ((unused)), const char* Arg)
762 /* Config file (linker) */
765 Error ("Cannot specify -C/--config twice");
772 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
773 /* Handle the --cpu option */
775 /* Add the cpu type to the assembler and compiler */
776 CmdAddArg2 (&CA65, "--cpu", Arg);
777 CmdAddArg2 (&CC65, "--cpu", Arg);
782 static void OptCreateDep (const char* Opt attribute ((unused)),
783 const char* Arg attribute ((unused)))
784 /* Handle the --create-dep option */
786 CmdAddArg (&CC65, "--create-dep");
791 static void OptDataLabel (const char* Opt attribute ((unused)), const char* Arg)
792 /* Handle the --data-label option */
794 CmdAddArg2 (&CO65, "--data-label", Arg);
799 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
800 /* Handle the --data-name option */
802 CmdAddArg2 (&CC65, "--data-name", Arg);
803 CmdAddArg2 (&CO65, "--data-name", Arg);
808 static void OptDebug (const char* Opt attribute ((unused)),
809 const char* Arg attribute ((unused)))
810 /* Debug mode (compiler and cl65 utility) */
812 CmdAddArg (&CC65, "-d");
813 CmdAddArg (&CO65, "-d");
819 static void OptDebugInfo (const char* Opt attribute ((unused)),
820 const char* Arg attribute ((unused)))
821 /* Debug Info - add to compiler and assembler */
823 CmdAddArg (&CC65, "-g");
824 CmdAddArg (&CA65, "-g");
825 CmdAddArg (&CO65, "-g");
830 static void OptFeature (const char* Opt attribute ((unused)), const char* Arg)
831 /* Emulation features for the assembler */
833 CmdAddArg2 (&CA65, "--feature", Arg);
838 static void OptForgetIncPaths (const char* Opt attribute ((unused)), const char* Arg)
839 /* Forget all currently defined include paths */
841 CmdAddArg2 (&CC65, "--forget-inc-paths", Arg);
846 static void OptHelp (const char* Opt attribute ((unused)),
847 const char* Arg attribute ((unused)))
848 /* Print help - cl65 */
856 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
857 /* Include directory (compiler) */
859 CmdAddArg2 (&CC65, "-I", Arg);
864 static void OptLib (const char* Opt attribute ((unused)), const char* Arg)
865 /* Library file follows (linker) */
867 CmdAddArg2 (&LD65, "--lib", Arg);
872 static void OptLibPath (const char* Opt attribute ((unused)), const char* Arg)
873 /* Library search path (linker) */
875 CmdAddArg2 (&LD65, "--lib-path", Arg);
880 static void OptListing (const char* Opt attribute ((unused)),
881 const char* Arg attribute ((unused)))
882 /* Create an assembler listing */
884 CmdAddArg (&CA65, "-l");
889 static void OptMapFile (const char* Opt attribute ((unused)), const char* Arg)
890 /* Create a map file */
892 /* Create a map file (linker) */
893 CmdAddArg2 (&LD65, "-m", Arg);
898 static void OptModule (const char* Opt attribute ((unused)),
899 const char* Arg attribute ((unused)))
900 /* Link as a module */
907 static void OptModuleId (const char* Opt attribute ((unused)), const char* Arg)
908 /* Specify a module if for the linker */
910 /* Pass it straight to the linker */
911 CmdAddArg2 (&LD65, "--module-id", Arg);
916 static void OptO65Model (const char* Opt attribute ((unused)), const char* Arg)
917 /* Handle the --o65-model option */
919 CmdAddArg2 (&CO65, "-m", Arg);
924 static void OptObj (const char* Opt attribute ((unused)), const char* Arg)
925 /* Object file follows (linker) */
927 CmdAddArg2 (&LD65, "--obj", Arg);
932 static void OptObjPath (const char* Opt attribute ((unused)), const char* Arg)
933 /* Object file search path (linker) */
935 CmdAddArg2 (&LD65, "--obj-path", Arg);
940 static void OptRegisterSpace (const char* Opt attribute ((unused)), const char* Arg)
941 /* Handle the --register-space option */
943 CmdAddArg2 (&CC65, "--register-space", Arg);
948 static void OptRegisterVars (const char* Opt attribute ((unused)),
949 const char* Arg attribute ((unused)))
950 /* Handle the --register-vars option */
952 CmdAddArg (&CC65, "-r");
957 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
958 /* Handle the --rodata-name option */
960 CmdAddArg2 (&CC65, "--rodata-name", Arg);
965 static void OptSignedChars (const char* Opt attribute ((unused)),
966 const char* Arg attribute ((unused)))
967 /* Make default characters signed */
969 CmdAddArg (&CC65, "-j");
974 static void OptStartAddr (const char* Opt attribute ((unused)), const char* Arg)
975 /* Set the default start address */
977 CmdAddArg2 (&LD65, "-S", Arg);
982 static void OptStaticLocals (const char* Opt attribute ((unused)),
983 const char* Arg attribute ((unused)))
984 /* Place local variables in static storage */
986 CmdAddArg (&CC65, "-Cl");
991 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
992 /* Set the target system */
994 Target = FindTarget (Arg);
995 if (Target == TGT_UNKNOWN) {
996 Error ("No such target system: `%s'", Arg);
997 } else if (Target == TGT_MODULE) {
998 Error ("Cannot use `module' as target, use --module instead");
1004 static void OptVerbose (const char* Opt attribute ((unused)),
1005 const char* Arg attribute ((unused)))
1006 /* Verbose mode (compiler, assembler, linker) */
1008 CmdAddArg (&CC65, "-v");
1009 CmdAddArg (&CA65, "-v");
1010 CmdAddArg (&CO65, "-v");
1011 CmdAddArg (&LD65, "-v");
1016 static void OptVersion (const char* Opt attribute ((unused)),
1017 const char* Arg attribute ((unused)))
1018 /* Print version number */
1021 "cl65 V%u.%u.%u - (C) Copyright 1998-2003 Ullrich von Bassewitz\n",
1022 VER_MAJOR, VER_MINOR, VER_PATCH);
1027 static void OptZeropageLabel (const char* Opt attribute ((unused)), const char* Arg)
1028 /* Handle the --zeropage-label option */
1030 CmdAddArg2 (&CO65, "--zeropage-label", Arg);
1035 static void OptZeropageName (const char* Opt attribute ((unused)), const char* Arg)
1036 /* Handle the --zeropage-name option */
1038 CmdAddArg2 (&CO65, "--zeropage-name", Arg);
1043 int main (int argc, char* argv [])
1044 /* Utility main program */
1046 /* Program long options */
1047 static const LongOpt OptTab[] = {
1048 { "--add-source", 0, OptAddSource },
1049 { "--ansi", 0, OptAnsi },
1050 { "--asm-define", 1, OptAsmDefine },
1051 { "--asm-include-dir", 1, OptAsmIncludeDir },
1052 { "--bss-label", 1, OptBssLabel },
1053 { "--bss-name", 1, OptBssName },
1054 { "--cfg-path", 1, OptCfgPath },
1055 { "--check-stack", 0, OptCheckStack },
1056 { "--code-label", 1, OptCodeLabel },
1057 { "--code-name", 1, OptCodeName },
1058 { "--codesize", 1, OptCodeSize },
1059 { "--config", 1, OptConfig },
1060 { "--cpu", 1, OptCPU },
1061 { "--create-dep", 0, OptCreateDep },
1062 { "--data-label", 1, OptDataLabel },
1063 { "--data-name", 1, OptDataName },
1064 { "--debug", 0, OptDebug },
1065 { "--debug-info", 0, OptDebugInfo },
1066 { "--feature", 1, OptFeature },
1067 { "--forget-inc-paths", 0, OptForgetIncPaths },
1068 { "--help", 0, OptHelp },
1069 { "--include-dir", 1, OptIncludeDir },
1070 { "--lib", 1, OptLib },
1071 { "--lib-path", 1, OptLibPath },
1072 { "--listing", 0, OptListing },
1073 { "--mapfile", 1, OptMapFile },
1074 { "--module", 0, OptModule },
1075 { "--module-id", 1, OptModuleId },
1076 { "--o65-model", 1, OptO65Model },
1077 { "--obj", 1, OptObj },
1078 { "--obj-path", 1, OptObjPath },
1079 { "--register-space", 1, OptRegisterSpace },
1080 { "--register-vars", 0, OptRegisterVars },
1081 { "--rodata-name", 1, OptRodataName },
1082 { "--signed-chars", 0, OptSignedChars },
1083 { "--start-addr", 1, OptStartAddr },
1084 { "--static-locals", 0, OptStaticLocals },
1085 { "--target", 1, OptTarget },
1086 { "--verbose", 0, OptVerbose },
1087 { "--version", 0, OptVersion },
1088 { "--zeropage-label", 1, OptZeropageLabel },
1089 { "--zeropage-name", 1, OptZeropageName },
1094 /* Initialize the cmdline module */
1095 InitCmdLine (&argc, &argv, "cl65");
1097 /* Initialize the command descriptors */
1098 CmdInit (&CC65, "cc65");
1099 CmdInit (&CA65, "ca65");
1100 CmdInit (&CO65, "co65");
1101 CmdInit (&LD65, "ld65");
1102 CmdInit (&GRC, "grc");
1104 /* Our default target is the C64 instead of "none" */
1107 /* Check the parameters */
1109 while (I < ArgCount) {
1111 /* Get the argument */
1112 const char* Arg = ArgVec[I];
1114 /* Check for an option */
1115 if (Arg [0] == '-') {
1120 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
1124 /* Strict ANSI mode (compiler) */
1129 if (Arg[2] == 'l' && Arg[3] == '\0') {
1130 /* Make local variables static */
1131 OptStaticLocals (Arg, 0);
1133 /* Specify linker config file */
1134 OptConfig (Arg, GetArg (&I, 2));
1139 /* Define a preprocessor symbol (compiler) */
1140 CmdAddArg2 (&CC65, "-D", GetArg (&I, 2));
1144 /* Include directory (compiler) */
1145 OptIncludeDir (Arg, GetArg (&I, 2));
1149 if (Arg[2] == 'n' && Arg[3] == '\0') {
1150 /* VICE label file (linker) */
1151 CmdAddArg2 (&LD65, "-Ln", GetArg (&I, 3));
1153 /* Library search path (linker) */
1154 OptLibPath (Arg, GetArg (&I, 2));
1159 /* Optimize code (compiler, also covers -Oi and others) */
1160 CmdAddArg (&CC65, Arg);
1164 /* Dont assemble and link the created files */
1165 DontLink = DontAssemble = 1;
1169 /* Include source as comment (compiler) */
1170 OptAddSource (Arg, 0);
1174 /* Print version number */
1175 OptVersion (Arg, 0);
1179 /* Suppress warnings - compiler and assembler */
1180 CmdAddArg (&CC65, "-W");
1181 CmdAddArg2 (&CA65, "-W", "0");
1185 /* Don't link the resulting files */
1190 /* Debug mode (compiler) */
1195 /* Debugging - add to compiler and assembler */
1196 OptDebugInfo (Arg, 0);
1201 /* Print help - cl65 */
1206 /* Default characters are signed */
1207 OptSignedChars (Arg, 0);
1211 /* Create an assembler listing */
1212 OptListing (Arg, 0);
1216 /* Create a map file (linker) */
1217 OptMapFile (Arg, GetArg (&I, 2));
1221 /* Name the output file */
1222 OutputName = GetArg (&I, 2);
1226 /* Enable register variables */
1227 OptRegisterVars (Arg, 0);
1231 /* Set target system - compiler, assembler and linker */
1232 OptTarget (Arg, GetArg (&I, 2));
1236 if (Arg [2] == 'm') {
1237 /* Verbose map file (linker) */
1238 CmdAddArg (&LD65, "-vm");
1240 /* Verbose mode (compiler, assembler, linker) */
1241 OptVerbose (Arg, 0);
1246 UnknownOption (Arg);
1250 /* Remember the first file name */
1251 if (FirstInput == 0) {
1255 /* Determine the file type by the extension */
1256 switch (GetFileType (Arg)) {
1259 /* Compile the file */
1264 /* Assemble the file */
1265 if (!DontAssemble) {
1272 /* Add to the linker files */
1273 CmdAddFile (&LD65, Arg);
1277 /* Add to the resource compiler files */
1282 /* Add the the object file converter files */
1287 Error ("Don't know what to do with `%s'", Arg);
1297 /* Check if we had any input files */
1298 if (FirstInput == 0) {
1299 Warning ("No input files");
1302 /* Link the given files if requested and if we have any */
1303 if (DontLink == 0 && LD65.FileCount > 0) {
1307 /* Return an apropriate exit code */
1308 return EXIT_SUCCESS;