]> git.sur5r.net Git - cc65/blobdiff - src/cl65/main.c
The assembler options -l and --listing will now take the name of the listing
[cc65] / src / cl65 / main.c
index ca1f5634250eb5c7d3ea8d4a1b3712cfe9b407ce..cf22adb196502c620ce3cea301bc7448d4b6ac1b 100644 (file)
@@ -6,7 +6,7 @@
 /*                                                                          */
 /*                                                                          */
 /*                                                                          */
-/* (C) 1999-2010, Ullrich von Bassewitz                                      */
+/* (C) 1999-2011, Ullrich von Bassewitz                                      */
 /*                Roemerstrasse 52                                           */
 /*                D-70794 Filderstadt                                        */
 /* EMail:         uz@cc65.org                                                */
@@ -98,8 +98,8 @@ static CmdDesc LD65 = { 0, 0, 0, 0, 0, 0, 0 };
 static CmdDesc GRC  = { 0, 0, 0, 0, 0, 0, 0 };
 
 /* Variables controlling the steps we're doing */
-static int DontLink    = 0;
-static int DontAssemble = 0;
+static int DoLink       = 1;
+static int DoAssemble   = 1;
 
 /* The name of the output file, NULL if none given */
 static const char* OutputName = 0;
@@ -386,23 +386,6 @@ static void Link (void)
 {
     unsigned I;
 
-    /* If we have a linker config file given, add it to the command line.
-     * Otherwise pass the target to the linker if we have one.
-     */
-    if (LinkerConfig) {
-        if (Module) {
-            Error ("Cannot use -C and --module together");
-        }
-               CmdAddArg2 (&LD65, "-C", LinkerConfig);
-    } else if (Module) {
-        CmdSetTarget (&LD65, TGT_MODULE);
-    } else {
-       CmdSetTarget (&LD65, Target);
-    }
-
-    /* Determine which target libraries are needed */
-    SetTargetFiles ();
-
     /* Since linking is always the final step, if we have an output file name
      * given, set it here. If we don't have an explicit output name given,
      * try to build one from the name of the first input file.
@@ -420,6 +403,23 @@ static void Link (void)
 
     }
 
+    /* If we have a linker config file given, add it to the command line.
+     * Otherwise pass the target to the linker if we have one.
+     */
+    if (LinkerConfig) {
+        if (Module) {
+            Error ("Cannot use -C and --module together");
+        }
+               CmdAddArg2 (&LD65, "-C", LinkerConfig);
+    } else if (Module) {
+        CmdSetTarget (&LD65, TGT_MODULE);
+    } else {
+       CmdSetTarget (&LD65, Target);
+    }
+
+    /* Determine which target libraries are needed */
+    SetTargetFiles ();
+
     /* Add all object files as parameters */
     for (I = 0; I < LD65.FileCount; ++I) {
        CmdAddArg (&LD65, LD65.Files [I]);
@@ -448,19 +448,20 @@ static void AssembleFile (const char* File, unsigned ArgCount)
     /* Set the target system */
     CmdSetTarget (&CA65, Target);
 
-    /* If we won't link, this is the final step. In this case, set the
-     * output name.
-     */
-    if (DontLink && OutputName) {
-       CmdSetOutput (&CA65, OutputName);
-    } else {
-       /* The object file name will be the name of the source file
-        * with .s replaced by ".o". Add this file to the list of
-        * linker files.
-        */
+    /* Check if this is the last processing step */
+    if (DoLink) {
+        /* We're linking later. Add the output file of the assembly
+         * the the file list of the linker. The name of the output
+         * file is that of the input file with ".s" replaced by ".o".
+         */
        char* ObjName = MakeFilename (File, ".o");
        CmdAddFile (&LD65, ObjName);
        xfree (ObjName);
+    } else {
+        /* This is the final step. If an output name is given, set it */
+        if (OutputName) {
+            CmdSetOutput (&CA65, OutputName);
+        }
     }
 
     /* Add the file as argument for the assembler */
@@ -478,19 +479,28 @@ static void AssembleFile (const char* File, unsigned ArgCount)
 
 
 
-static void AssembleIntermediate (const char* File)
-/* Assemble an intermediate file. The -dep options won't be added and
- * the file is removed after assembly.
+static void AssembleIntermediate (const char* SourceFile)
+/* Assemble an intermediate file which was generated by a previous processing
+ * step with SourceFile as input. The -dep options won't be added and
+ * the intermediate assembler file is removed after assembly.
  */
 {
-    /* Use common routine */
-    AssembleFile (File, CA65.ArgCount);
+    /* Generate the name of the assembler output file from the source file
+     * name. It's the same name with the extension replaced by ".s"
+     */
+    char* AsmName = MakeFilename (SourceFile, ".s");
 
-    /* Remove the generated file */
-    if (remove (File) < 0) {
+    /* Assemble the intermediate assembler file */
+    AssembleFile (AsmName, CA65.ArgCount);
+
+    /* Remove the input file */
+    if (remove (AsmName) < 0) {
         Warning ("Cannot remove temporary file `%s': %s",
-                 File, strerror (errno));
+                 AsmName, strerror (errno));
     }
+
+    /* Free the assembler file name which was allocated from the heap */
+    xfree (AsmName);
 }
 
 
@@ -521,24 +531,38 @@ static void Assemble (const char* File)
 static void Compile (const char* File)
 /* Compile the given file */
 {
-    char* AsmName = 0;
-
     /* Remember the current compiler argument count */
     unsigned ArgCount = CC65.ArgCount;
 
     /* Set the target system */
     CmdSetTarget (&CC65, Target);
 
-    /* If we won't assemble, this is the final step. In this case, set the
-     * output name.
-     */
-    if (DontAssemble && OutputName) {
-       CmdSetOutput (&CC65, OutputName);
+    /* Check if this is the final step */
+    if (DoAssemble) {
+        /* We will assemble this file later. If a dependency file is to be
+         * generated, set the dependency target to be the final object file,
+         * not the intermediate assembler file. But beware: There may be an
+         * output name specified for the assembler.
+         */
+        if (DepName || FullDepName) {
+            /* Was an output name for the assembler specified? */
+            if (!DoLink && OutputName) {
+                /* Use this name as the dependency target */
+                CmdAddArg2 (&CC65, "--dep-target", OutputName);
+            } else {
+                /* Use the object file name as the dependency target */
+                char* ObjName = MakeFilename (File, ".o");
+                CmdAddArg2 (&CC65, "--dep-target", ObjName);
+                xfree (ObjName);
+            }
+        }
     } else {
-       /* The assembler file name will be the name of the source file
-        * with .c replaced by ".s".
-        */
-       AsmName = MakeFilename (File, ".s");
+        /* If we won't assemble, this is the final step. In this case, set
+         * the output name if it was given.
+         */
+        if (OutputName) {
+            CmdSetOutput (&CC65, OutputName);
+        }
     }
 
     /* Add the file as argument for the compiler */
@@ -556,12 +580,10 @@ static void Compile (const char* File)
     /* If this is not the final step, assemble the generated file, then
      * remove it
      */
-    if (!DontAssemble) {
-       AssembleIntermediate (AsmName);
+    if (DoAssemble) {
+        /* Assemble the intermediate file and remove it */
+       AssembleIntermediate (File);
     }
-
-    /* Free the assembler file name which was allocated from the heap */
-    xfree (AsmName);
 }
 
 
@@ -569,16 +591,9 @@ static void Compile (const char* File)
 static void CompileRes (const char* File)
 /* Compile the given geos resource file */
 {
-    char* AsmName = 0;
-
     /* Remember the current assembler argument count */
     unsigned ArgCount = GRC.ArgCount;
 
-    /* The assembler file name will be the name of the source file
-     * with .grc replaced by ".s".
-     */
-    AsmName = MakeFilename (File, ".s");
-
     /* Add the file as argument for the resource compiler */
     CmdAddArg (&GRC, File);
 
@@ -594,12 +609,10 @@ static void CompileRes (const char* File)
     /* If this is not the final step, assemble the generated file, then
      * remove it
      */
-    if (!DontAssemble) {
-       AssembleIntermediate (AsmName);
+    if (DoAssemble) {
+        /* Assemble the intermediate file and remove it */
+       AssembleIntermediate (File);
     }
-
-    /* Free the assembler file name which was allocated from the heap */
-    xfree (AsmName);
 }
 
 
@@ -607,21 +620,14 @@ static void CompileRes (const char* File)
 static void ConvertO65 (const char* File)
 /* Convert an o65 object file into an assembler file */
 {
-    char* AsmName = 0;
-
     /* Remember the current converter argument count */
     unsigned ArgCount = CO65.ArgCount;
 
     /* If we won't assemble, this is the final step. In this case, set the
      * output name.
      */
-    if (DontAssemble && OutputName) {
-       CmdSetOutput (&CO65, OutputName);
-    } else {
-       /* The assembler file name will be the name of the source file
-        * with .c replaced by ".s".
-        */
-       AsmName = MakeFilename (File, ".s");
+    if (!DoAssemble && OutputName) {
+               CmdSetOutput (&CO65, OutputName);
     }
 
     /* Add the file as argument for the object file converter */
@@ -639,12 +645,10 @@ static void ConvertO65 (const char* File)
     /* If this is not the final step, assemble the generated file, then
      * remove it
      */
-    if (!DontAssemble) {
-               AssembleIntermediate (AsmName);
+    if (DoAssemble) {
+        /* Assemble the intermediate file and remove it */
+       AssembleIntermediate (File);
     }
-
-    /* Free the assembler file name which was allocated from the heap */
-    xfree (AsmName);
 }
 
 
@@ -664,7 +668,7 @@ static void Usage (void)
             "  -d\t\t\t\tDebug mode\n"
             "  -g\t\t\t\tAdd debug info\n"
             "  -h\t\t\t\tHelp (this text)\n"
-            "  -l\t\t\t\tCreate an assembler listing\n"
+            "  -l name\t\t\tCreate an assembler listing file\n"
             "  -m name\t\t\tCreate a map file\n"
             "  -mm model\t\t\tSet the memory model\n"
             "  -o name\t\t\tName the output file\n"
@@ -695,6 +699,7 @@ static void Usage (void)
             "  --asm-args options\t\tPass options to the assembler\n"
             "  --asm-define sym[=v]\t\tDefine an assembler symbol\n"
             "  --asm-include-dir dir\t\tSet an assembler include directory\n"
+            "  --bin-include-dir dir\t\tSet an assembker binary include directory\n"
             "  --bss-label name\t\tDefine and export a BSS segment label\n"
             "  --bss-name seg\t\tSet the name of the BSS segment\n"
             "  --cfg-path path\t\tSpecify a config file search path\n"
@@ -719,7 +724,7 @@ static void Usage (void)
             "  --lib file\t\t\tLink this library\n"
             "  --lib-path path\t\tSpecify a library search path\n"
             "  --list-targets\t\tList all available targets\n"
-            "  --listing\t\t\tCreate an assembler listing\n"
+            "  --listing name\t\tCreate an assembler listing file\n"
             "  --list-bytes n\t\tNumber of bytes per assembler listing line\n"
             "  --mapfile name\t\tCreate a map file\n"
             "  --memory-model model\t\tSet the memory model\n"
@@ -761,7 +766,7 @@ static void OptAsmArgs (const char* Opt attribute ((unused)), const char* Arg)
 }
 
 
-
+                                                            
 static void OptAsmDefine (const char* Opt attribute ((unused)), const char* Arg)
 /* Define an assembler symbol (assembler) */
 {
@@ -778,6 +783,14 @@ static void OptAsmIncludeDir (const char* Opt attribute ((unused)), const char*
 
 
 
+static void OptBinIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
+/* Binary include directory (assembler) */
+{
+    CmdAddArg2 (&CA65, "--bin-include-dir", Arg);
+}
+
+
+
 static void OptBssLabel (const char* Opt attribute ((unused)), const char* Arg)
 /* Handle the --bss-label option */
 {
@@ -996,17 +1009,16 @@ static void OptListBytes (const char* Opt attribute ((unused)), const char* Arg)
 
 
 
-static void OptListing (const char* Opt attribute ((unused)),
-                       const char* Arg attribute ((unused)))
+static void OptListing (const char* Opt attribute ((unused)), const char* Arg)
 /* Create an assembler listing */
 {
-    CmdAddArg (&CA65, "-l");
+    CmdAddArg2 (&CA65, "-l", Arg);
 }
 
 
 
 static void OptListTargets (const char* Opt attribute ((unused)),
-                           const char* Arg attribute ((unused)))
+                           const char* Arg attribute ((unused)))
 /* List all targets */
 {
     unsigned I;
@@ -1178,7 +1190,7 @@ static void OptVersion (const char* Opt attribute ((unused)),
 /* Print version number */
 {
     fprintf (stderr,
-            "cl65 V%s - (C) Copyright 1998-2009 Ullrich von Bassewitz\n",
+            "cl65 V%s - (C) Copyright 1998-2011 Ullrich von Bassewitz\n",
             GetVersionAsString ());
 }
 
@@ -1209,6 +1221,7 @@ int main (int argc, char* argv [])
         { "--asm-args",         1,      OptAsmArgs              },
                { "--asm-define",       1,      OptAsmDefine            },
        { "--asm-include-dir",  1,      OptAsmIncludeDir        },
+        { "--bin-include-dir",  1,      OptBinIncludeDir        },
                { "--bss-label",        1,      OptBssLabel             },
        { "--bss-name",         1,      OptBssName              },
                { "--cfg-path",         1,      OptCfgPath              },
@@ -1233,7 +1246,7 @@ int main (int argc, char* argv [])
                { "--lib",              1,      OptLib                  },
                { "--lib-path",         1,      OptLibPath              },
        { "--list-targets",     0,      OptListTargets          },
-       { "--listing",          0,      OptListing              },
+       { "--listing",          1,      OptListing              },
         { "--list-bytes",       1,      OptListBytes            },
        { "--mapfile",          1,      OptMapFile              },
         { "--memory-model",     1,      OptMemoryModel          },
@@ -1324,7 +1337,8 @@ int main (int argc, char* argv [])
 
                case 'S':
                    /* Dont assemble and link the created files */
-                   DontLink = DontAssemble = 1;
+                    DoAssemble = 0;
+                   DoLink     = 0;
                    break;
 
                case 'T':
@@ -1362,7 +1376,7 @@ int main (int argc, char* argv [])
 
                case 'c':
                    /* Don't link the resulting files */
-                   DontLink = 1;
+                   DoLink = 0;
                    break;
 
                case 'd':
@@ -1388,7 +1402,7 @@ int main (int argc, char* argv [])
 
                case 'l':
                    /* Create an assembler listing */
-                   OptListing (Arg, 0);
+                   OptListing (Arg, GetArg (&I, 2));
                    break;
 
                case 'm':
@@ -1446,7 +1460,7 @@ int main (int argc, char* argv [])
 
                case FILETYPE_ASM:
                    /* Assemble the file */
-                   if (!DontAssemble) {
+                   if (DoAssemble) {
                        Assemble (Arg);
                    }
                    break;
@@ -1484,7 +1498,7 @@ int main (int argc, char* argv [])
     }
 
     /* Link the given files if requested and if we have any */
-    if (DontLink == 0 && LD65.FileCount > 0) {
+    if (DoLink && LD65.FileCount > 0) {
        Link ();
     }