DS800C400 fun, improved ROM interface and tinibios
[fw/sdcc] / src / z80 / main.c
index c1b96e5e730a8b1b8570710b955cf3e69b5eb2a5..2ff5b561c18e6f56090a3bdee189bb213aae4202 100644 (file)
    what you give them.   Help stamp out software-hoarding!
 -------------------------------------------------------------------------*/
 
+#include <sys/stat.h>
 #include "z80.h"
 #include "MySystem.h"
 #include "BuildCmd.h"
 #include "SDCCutil.h"
+#include "dbuf.h"
 
 static char _z80_defaultRules[] =
 {
@@ -175,19 +177,18 @@ _process_pragma (const char *sz)
 
 static const char *_gbz80_rgbasmCmd[] =
 {
-  "rgbasm", "-o$1.o", "$1.asm", NULL
+  "rgbasm", "-o\"$1.o\"", "\"$1.asm\"", NULL
 };
 
 static const char *_gbz80_rgblinkCmd[] =
 {
-  "xlink", "-tg", "-n$1.sym", "-m$1.map", "-zFF", "$1.lnk", NULL
+  "xlink", "-tg", "-n\"$1.sym\"", "-m\"$1.map\"", "-zFF", "\"$1.lnk\"", NULL
 };
 
 static void
 _gbz80_rgblink (void)
 {
   FILE *lnkfile;
-  int i;
 
   /* first we need to create the <filename>.lnk file */
   sprintf (scratchFileName, "%s.lnk", dstFileName);
@@ -201,14 +202,11 @@ _gbz80_rgblink (void)
 
   fprintf (lnkfile, "%s.o\n", dstFileName);
 
-  for (i = 0; i < nrelFiles; i++)
-    fprintf (lnkfile, "%s\n", relFiles[i]);
+  fputStrSet(lnkfile, relFilesSet);
 
   fprintf (lnkfile, "\n[Libraries]\n");
   /* additional libraries if any */
-  for (i = 0; i < nlibFiles; i++)
-    fprintf (lnkfile, "%s\n", libFiles[i]);
-
+  fputStrSet(lnkfile, libFilesSet);
 
   fprintf (lnkfile, "\n[Output]\n" "%s.gb", dstFileName);
 
@@ -285,10 +283,48 @@ _parseOptions (int *pargc, char **argv, int *i)
 static void
 _setValues(void)
 {
+  const char *s;
+
   if (options.nostdlib == FALSE)
     {
-      setMainValue ("z80libspec", "-k{libdir}{sep}{port} -l{port}.lib");
-      setMainValue ("z80crt0", "{libdir}{sep}{port}{sep}crt0{objext}");
+      const char *s;
+      char path[PATH_MAX];
+      struct dbuf_s dbuf;
+
+      dbuf_init(&dbuf, PATH_MAX);
+
+      for (s = setFirstItem(libDirsSet); s != NULL; s = setNextItem(libDirsSet))
+        {
+          buildCmdLine2(path, sizeof path, "-k\"%s" DIR_SEPARATOR_STRING "{port}\" ", s);
+          dbuf_append(&dbuf, path, strlen(path));
+        }
+      buildCmdLine2(path, sizeof path, "-l\"{port}.lib\"", s);
+      dbuf_append(&dbuf, path, strlen(path));
+
+      setMainValue ("z80libspec", dbuf_c_str(&dbuf));
+      dbuf_destroy(&dbuf);
+
+      for (s = setFirstItem(libDirsSet); s != NULL; s = setNextItem(libDirsSet))
+        {
+          struct stat stat_buf;
+
+          buildCmdLine2(path, sizeof path, "%s" DIR_SEPARATOR_STRING "{port}" DIR_SEPARATOR_STRING "crt0{objext}", s);
+          if (stat(path, &stat_buf) == 0)
+            break;
+        }
+
+      if (s == NULL)
+        setMainValue ("z80crt0", "\"crt0{objext}\"");
+      else
+        {
+          char *buf;
+          size_t len = strlen(path) + 3;
+
+          buf = Safe_alloc(len);
+          SNPRINTF(buf, len, "\"%s\"", path);
+          setMainValue("z80crt0", buf);
+          Safe_free(buf);
+        } 
     }
   else
     {
@@ -296,8 +332,10 @@ _setValues(void)
       setMainValue ("z80crt0", "");
     }
 
-  setMainValue ("z80extralibfiles", joinn (libFiles, nlibFiles));
-  setMainValue ("z80extralibpaths", joinn (libPaths, nlibPaths));
+  setMainValue ("z80extralibfiles", (s = joinStrSet(libFilesSet)));
+  Safe_free((void *)s);
+  setMainValue ("z80extralibpaths", (s = joinStrSet(libPathsSet)));
+  Safe_free((void *)s);
 
   if (IS_GB)
     {
@@ -313,7 +351,8 @@ _setValues(void)
   setMainValue ("stdobjdstfilename" , "{dstfilename}{objext}");
   setMainValue ("stdlinkdstfilename", "{dstfilename}{z80outext}");
 
-  setMainValue ("z80extraobj", joinn (relFiles, nrelFiles));
+  setMainValue ("z80extraobj", (s = joinStrSet(relFilesSet)));
+  Safe_free((void *)s);
 
   sprintf (buffer, "-b_CODE=0x%04X -b_DATA=0x%04X", options.code_loc, options.data_loc);
   setMainValue ("z80bases", buffer);
@@ -333,7 +372,6 @@ _finaliseOptions (void)
 static void
 _setDefaultOptions (void)
 {
-  options.genericPtr = 1;      /* default on */
   options.nopeep = 0;
   options.stackAuto = 1;
   options.mainreturn = 1;
@@ -402,7 +440,7 @@ _getRegName (struct regs *reg)
     {
       return reg->name;
     }
-  //  assert (0);
+  /*  assert (0); */
   return "err";
 }
 
@@ -441,16 +479,16 @@ _hasNativeMulFor (iCode *ic, sym_link *left, sym_link *right)
 }
 
 #define LINKCMD \
-    "{bindir}{sep}link-{port} -n -c -- {z80bases} -m -j" \
+    "link-{port} -n -c -- {z80bases} -m -j" \
     " {z80libspec}" \
     " {z80extralibfiles} {z80extralibpaths}" \
-    " {z80outputtypeflag} {linkdstfilename}" \
+    " {z80outputtypeflag} \"{linkdstfilename}\"" \
     " {z80crt0}" \
-    " {dstfilename}{objext}" \
+    " \"{dstfilename}{objext}\"" \
     " {z80extraobj}"
 
 #define ASMCMD \
-    "{bindir}{sep}as-{port} -plosgff {objdstfilename} {dstfilename}{asmext}"
+    "as-{port} -plosgff \"{objdstfilename}\" \"{dstfilename}{asmext}\""
 
 /* Globals */
 PORT z80_port =
@@ -498,12 +536,13 @@ PORT z80_port =
     "OVERLAY",
     "GSFINAL",
     "HOME",
-    NULL, // xidata
-    NULL, // xinit
+    NULL, /* xidata */
+    NULL, /* xinit */
     NULL,
     NULL,
     1
   },
+  { NULL, NULL },
   {
     -1, 0, 0, 4, 0, 2
   },
@@ -592,12 +631,13 @@ PORT gbz80_port =
     "OVERLAY",
     "GSFINAL",
     "HOME",
-    NULL, // xidata
-    NULL, // xinit
+    NULL, /* xidata */
+    NULL, /* xinit */
     NULL,
     NULL,
     1
   },
+  { NULL, NULL },
   {
     -1, 0, 0, 2, 0, 4
   },