wip II
[sdk] / compiler / ecc / ecc.ec
index 91ded3c..09401fc 100644 (file)
@@ -247,6 +247,10 @@ class CompilerApp : Application
       Platform targetPlatform = GetRuntimePlatform();
       int targetBits = GetHostBits();
 
+#ifdef _DEBUG
+      // buildingBootStrap = true;
+#endif
+
       SetSymbolsDir("");
 
       /*for(c = 0; c<this.argc; c++)
@@ -274,6 +278,17 @@ class CompilerApp : Application
          argc++;
       }*/
 
+#ifdef _DEBUG
+      printf("\nArguments given:\n");
+      for(c=1; c<argc; c++)
+         printf(" %s", argv[c]);
+      printf("\n\n");
+      for(c=1; c<argc; c++)
+         PrintLn("Arg", c, ": ", argv[c]);
+      printf("\n");
+      //getch();
+#endif
+
       for(c = 1; c<argc; c++)
       {
          char * arg = argv[c];
@@ -281,42 +296,31 @@ class CompilerApp : Application
          {
             if(!strcmp(arg + 1, "m32") || !strcmp(arg + 1, "m64"))
             {
-               int argLen = strlen(arg);
-               int newLen = cppOptionsLen + 1 + argLen;
+               int newLen = cppOptionsLen + 1 + strlen(arg);
                cppOptions = renew cppOptions char[newLen + 1];
                cppOptions[cppOptionsLen] = ' ';
                strcpy(cppOptions + cppOptionsLen + 1, arg);
                cppOptionsLen = newLen;
                targetBits = !strcmp(arg + 1, "m32") ? 32 : 64;
             }
-            else if(arg[1] == 'D')
-            {
-               int argLen = strlen(arg);
-               int newLen = cppOptionsLen + 1 + argLen;
-               cppOptions = renew cppOptions char[newLen + 1];
-               cppOptions[cppOptionsLen] = ' ';
-               strcpy(cppOptions + cppOptionsLen + 1, arg); 
-               cppOptionsLen = newLen;
-               if(!strcmp(arg, "-DBUILDING_ECERE_COM"))
-                  SetBuildingEcereCom(true);
-               else if(!strcmp(arg, "-DECERE_COM_MODULE"))
-                  SetBuildingEcereComModule(true);
-               else if(!strcmp(arg, "-DECERE_BOOTSTRAP"))
-                  buildingBootStrap = true;
-            }
-            else if(arg[1] == 'I')
+            else if(arg[1] == 'D' || arg[1] == 'I')
             {
-               int argLen = strlen(arg);
-               int newLen = cppOptionsLen + argLen + 3;
-               cppOptions = renew cppOptions char[newLen + 1];
-               cppOptions[cppOptionsLen] = ' ';
-               cppOptions[cppOptionsLen+1] = '-';
-               cppOptions[cppOptionsLen+2] = 'I';
-               cppOptions[cppOptionsLen+3] = '"';
-               strcpy(cppOptions + cppOptionsLen + 4, arg+2); 
-               cppOptions[newLen-1] = '\"';
-               cppOptions[newLen] = '\0';
-               cppOptionsLen = newLen;
+               char * buf;
+               int size = cppOptionsLen + 1 + strlen(arg) * 2 + 1;
+               cppOptions = renew cppOptions char[size];
+               buf = cppOptions + cppOptionsLen;
+               *buf++ = ' ';
+               PassArg(buf, arg);
+               cppOptionsLen = cppOptionsLen + 1 + strlen(buf);
+               if(arg[1] == 'D')
+               {
+                  if(!strcmp(arg, "-DBUILDING_ECERE_COM"))
+                     SetBuildingEcereCom(true);
+                  else if(!strcmp(arg, "-DECERE_COM_MODULE"))
+                     SetBuildingEcereComModule(true);
+                  else if(!strcmp(arg, "-DECERE_BOOTSTRAP"))
+                     buildingBootStrap = true;
+               }
             }
             else if(!strcmp(arg+1, "t"))
             {
@@ -356,19 +360,16 @@ class CompilerApp : Application
             {
                if(c + 1 < argc)
                {
-                  int argLen = strlen(arg);
-                  int arg1Len = strlen(argv[c+1]);
-                  int newLen = cppOptionsLen + argLen + arg1Len + 4;
-                  cppOptions = renew cppOptions char[newLen + 1];
-                  cppOptions[cppOptionsLen] = ' ';
-                  strcpy(cppOptions + cppOptionsLen + 1, arg); 
-                  cppOptions[cppOptionsLen+argLen+1] = ' ';
-                  cppOptions[cppOptionsLen+argLen+2] = '"';
-                  arg = argv[++c];
-                  strcpy(cppOptions + cppOptionsLen + argLen + 3, arg); 
-                  cppOptions[newLen-1] = '\"';
-                  cppOptions[newLen] = '\0';
-                  cppOptionsLen = newLen;
+                  char * buf;
+                  char * arg1 = argv[++c];
+                  int size = cppOptionsLen + 1 + strlen(arg) * 2 + strlen(arg1) * 2 + 1;
+                  cppOptions = renew cppOptions char[size];
+                  buf = cppOptions + cppOptionsLen;
+                  *buf++ = ' ';
+                  buf = PassArg(buf, arg);
+                  *buf++ = ' ';
+                  buf = PassArg(buf, arg1);
+                  cppOptionsLen = buf - cppOptions;
                }
                else
                   valid = false;
@@ -426,7 +427,9 @@ class CompilerApp : Application
       }
 
       if(!valid)
+      {
          printf($"Syntax:\n   ecc [-t <target platform>] [-cpp <c preprocessor>] [-o <output>] [-symbols <outputdir>] [-I<includedir>]* [-isystem <sysincludedir>]* [-D<definition>]* -c <input>\n");
+      }
       else
       {
          DualPipe cppOutput;
@@ -473,10 +476,15 @@ class CompilerApp : Application
 
          snprintf(command, sizeof(command), "%s%s -x c -E %s\"%s\"", cppCommand, cppOptions ? cppOptions : "", buildingBootStrap ? "" : "-include stdint.h -include sys/types.h ", GetSourceFile());
          command[sizeof(command)-1] = 0;
+#ifdef _DEBUG
+         PrintLn("ECC Executing:");
+         PrintLn(command);
+#endif
          if((cppOutput = DualPipeOpen({ output = true }, command)))
          {
             char impFile[MAX_LOCATION];
             ImportedModule module;
+            char sourceFileName[MAX_FILENAME];
             char mainModuleName[MAX_FILENAME];
             int exitCode;
             OldList * ast;
@@ -487,7 +495,8 @@ class CompilerApp : Application
             imports.Add((mainModule = ModuleImport { }));
             SetMainModule(mainModule);
 
-            GetLastDirectory(GetSourceFile(), mainModuleName);
+            GetLastDirectory(GetSourceFile(), sourceFileName);
+            strcpy(mainModuleName, sourceFileName);
 
 #if 0
             // TEMP: UNTIL WE CAN HAVE PER SOURCE FILE PREPROCESSOR DEFINITIONS...
@@ -527,7 +536,7 @@ class CompilerApp : Application
             fileInput.Seek(0, start);
 
    #ifdef _DEBUG
-            // SetYydebug(true);
+             // SetYydebug(true);
    #endif
 
             // Predeclare all classes
@@ -536,7 +545,7 @@ class CompilerApp : Application
                char symLocation[MAX_LOCATION];
                ImportedModule module, next;
 
-               GetLastDirectory(GetSourceFile(), symFile);
+               strcpy(symFile, sourceFileName);
                ChangeExtension(symFile, "sym", symFile);
 
                strcpy(symLocation, GetSymbolsDir());
@@ -605,12 +614,8 @@ class CompilerApp : Application
                ProcessInstanceDeclarations();
 
                strcpy(impFile, GetSymbolsDir());
-               {
-                  char fileName[MAX_FILENAME];
-                  GetLastDirectory(GetSourceFile(), fileName);
-                  PathCat(impFile, fileName);
-                  ChangeExtension(impFile, "imp", impFile);
-               }
+               PathCat(impFile, sourceFileName);
+               ChangeExtension(impFile, "imp", impFile);
                if(imports.first)
                   OutputImports(impFile);
                // For now use precomp to generate sym file only...
@@ -620,6 +625,7 @@ class CompilerApp : Application
                   File output = FileOpen(GetOutputFile(), write);
                   if(output)
                   {
+                     output.Printf("/* Code generated from eC source file: %s */\n", sourceFileName);
                      output.Printf("#if defined(__GNUC__)\n");
                         output.Printf("typedef long long int64;\n");
                         output.Printf("typedef unsigned long long uint64;\n");
@@ -719,6 +725,7 @@ class CompilerApp : Application
       OutputIntlStrings();
 
 #if defined(_DEBUG) && defined(__WIN32__)
+      PrintLn("Done.");
       if(exitCode || GetNumWarnings())
          getch();
 #endif