c81f06778b8ae156bc8a3fa9d59df03d6169e066
[sdk] / ide / src / project / ProjectNode.ec
1 #ifndef MAKEFILE_GENERATOR
2 import "ide"
3 #else
4 #ifdef ECERE_STATIC
5 import static "ecere"
6 #else
7 import "ecere"
8 #endif
9
10 import "Project"
11
12 static define app = ((GuiApplication)__thisModule);
13 #endif
14
15 #define OPTION(x) ((uint)(uintptr)(&((ProjectOptions)0).x))
16
17 static void OutputLog(const char * string)
18 {
19 #ifdef MAKEFILE_GENERATOR
20    printf("%s", string);
21 #else
22    ide.outputView.buildBox.Log(string);
23 #endif
24 }
25
26 bool eString_PathInsideOfMore(const char * path, const char * of, char * pathRest)
27 {
28    if(!path[0] || !of[0])
29       return false;  // What to do here? Ever used?
30    else
31    {
32       char ofPart[MAX_FILENAME], ofRest[MAX_LOCATION];
33       char pathPart[MAX_FILENAME]; //, pathRest[MAX_LOCATION];
34       strcpy(ofRest, of);
35       strcpy(pathRest, path);
36       for(; ofRest[0] && pathRest[0];)
37       {
38          SplitDirectory(ofRest, ofPart, ofRest);
39          SplitDirectory(pathRest, pathPart, pathRest);
40          if(fstrcmp(pathPart, ofPart))
41             return false;
42       }
43       if(!ofRest[0] && !pathRest[0])
44          return false;
45       else if(!pathRest[0])           // not inside of, it's the other way around
46          return false;
47       return true;
48    }
49 }
50
51 enum NodeTypes { project, file, folder, resources, folderOpen };
52 enum NodeIcons
53 {
54    genFile, ewsFile, epjFile, folder, openFolder, ecFile, ehFile,
55    sFile, cFile, hFile, cppFile, hppFile, textFile, webFile, pictureFile, soundFile,
56    archiveFile, packageFile, opticalMediaImageFile, mFile, mmFile;
57
58    NodeIcons ::SelectFileIcon(const char * filePath)
59    {
60       NodeIcons icon;
61       if(filePath && filePath[0])
62       {
63          char extension[MAX_EXTENSION];
64          GetExtension(filePath, extension);
65          if(strlen(extension))
66          {
67             if(!strcmpi(extension, WorkspaceExtension))
68                icon = ewsFile;
69             else if(!strcmpi(extension, ProjectExtension))
70                icon = epjFile;
71             else if(!strcmpi(extension, "ec"))
72                icon = ecFile;
73             else if(!strcmpi(extension, "eh"))
74                icon = ehFile;
75             else if(!strcmpi(extension, "cpp") || !strcmpi(extension, "cc") ||
76                   !strcmpi(extension, "cxx"))
77                icon = cppFile;
78             else if(!strcmpi(extension, "hpp") || !strcmpi(extension, "hh") ||
79                   !strcmpi(extension, "hxx"))
80                icon = hppFile;
81             else if(!strcmpi(extension, "s"))
82                icon = sFile;
83             else if(!strcmpi(extension, "c"))
84                icon = cFile;
85             else if(!strcmpi(extension, "h"))
86                icon = hFile;
87             else if(!strcmpi(extension, "m"))
88                icon = mFile;
89             else if(!strcmpi(extension, "mm"))
90                icon = mmFile;
91             else if(!strcmpi(extension, "txt") || !strcmpi(extension, "text") ||
92                   !strcmpi(extension, "nfo") || !strcmpi(extension, "info"))
93                icon = textFile;
94             else if(!strcmpi(extension, "htm") || !strcmpi(extension, "html") ||
95                   !strcmpi(extension, "css") || !strcmpi(extension, "php") ||
96                   !strcmpi(extension, "js"))
97                icon = webFile;
98             else if(!strcmpi(extension, "bmp") || !strcmpi(extension, "pcx") ||
99                   !strcmpi(extension, "jpg") || !strcmpi(extension, "jpeg") ||
100                   !strcmpi(extension, "gif") || !strcmpi(extension, "png") ||
101                   !strcmpi(extension, "ico"))
102                icon = pictureFile;
103             else if(!strcmpi(extension, "wav") || !strcmpi(extension, "mp3") ||
104                   !strcmpi(extension, "ogg") || !strcmpi(extension, "snd"))
105                icon = soundFile;
106             else if(!strcmpi(extension, "ear") || !strcmpi(extension, "7z") ||
107                   !strcmpi(extension, "rar") || !strcmpi(extension, "zip") ||
108                   !strcmpi(extension, "gz") || !strcmpi(extension, "bz2") ||
109                   !strcmpi(extension, "tar") || !strcmpi(extension, "arj") ||
110                   !strcmpi(extension, "lza") || !strcmpi(extension, "lzh") ||
111                   !strcmpi(extension, "cpio") || !strcmpi(extension, "z"))
112                icon = archiveFile;
113             else if(!strcmpi(extension, "cab") || !strcmpi(extension, "deb") ||
114                   !strcmpi(extension, "rpm"))
115                icon = packageFile;
116             else if(!strcmpi(extension, "iso") || !strcmpi(extension, "mds") ||
117                   !strcmpi(extension, "cue") || !strcmpi(extension, "bin") ||
118                   !strcmpi(extension, "ccd") || !strcmpi(extension, "bwt") ||
119                   !strcmpi(extension, "cdi") || !strcmpi(extension, "nrg"))
120                icon = opticalMediaImageFile;
121             else
122                icon = genFile;
123          }
124          else
125             icon = genFile;
126       }
127       else
128          icon = genFile; // tocheck: error icon?
129       return icon;
130    }
131
132    NodeIcons ::SelectNodeIcon(NodeTypes type)
133    {
134       switch(type)
135       {
136          case project:
137             return epjFile;
138          case file:
139             return genFile;
140          case folder:
141             return folder;
142          case resources:
143             return archiveFile;
144          case folderOpen:
145             return openFolder;
146       }
147       return genFile;
148    }
149 };
150
151 #define SELECTION_COLOR Color { 10, 36, 106 }
152
153 // On Windows & UNIX
154 #define SEPS    "/"
155 #define SEP     '/'
156
157 // this is so not working, why!
158 //struct TwoStrings
159 // return result was not even executed (did not step on while debugging)
160 class TwoStrings : struct
161 {
162    char * a;
163    char * b;
164
165    property bool
166    {
167       get
168       {
169          return a && a[0];
170       }
171    }
172
173    ~TwoStrings()
174    {
175       delete a;
176       delete b;
177    }
178 }
179
180 class DotMain : bool
181 {
182    //property char * { set { } }
183    DotMain ::FromFileName(const char * fileName)
184    {
185       DotMain dotMain = false;
186       if(fileName && fileName[0])
187       {
188          char ext[MAX_EXTENSION];
189          GetExtension(fileName, ext);
190          if(!strcmp(ext, "c") || !strcmp(ext, "ec"))
191          {
192             char stripExt[MAX_LOCATION];
193             strcpy(stripExt, fileName);
194             StripExtension(stripExt);
195             GetExtension(stripExt, ext);
196             if(!strcmp(ext, "main"))
197                dotMain = true;
198          }
199       }
200       return dotMain;
201    }
202 }
203
204 enum IntermediateFileType
205 {
206    none, ec, c, sym, imp, bowl, o;
207
208    //property char * { set { } }
209    IntermediateFileType ::FromExtension(char * extension)
210    {
211       IntermediateFileType type = none;
212       if(extension && extension[0])
213       {
214          if(!fstrcmp(extension, "ec"))
215             type = ec;
216          else if(!fstrcmp(extension, "c"))
217             type = c;
218          else if(!fstrcmp(extension, "sym"))
219             type = sym;
220          else if(!fstrcmp(extension, "imp"))
221             type = imp;
222          else if(!fstrcmp(extension, "bowl"))
223             type = bowl;
224          else if(!fstrcmp(extension, "o"))
225             type = o;
226       }
227       return type;
228    }
229 };
230
231 class ProjectNode : ListItem
232 {
233 public:
234    property String
235    {
236       set { return { fileName = value }; }
237       // TOCHECK: Is this isset necessary at all?
238       isset { return nodeType == file && !options && !configurations && !platforms && !files; }
239    };
240    property String folder
241    {
242       set
243       {
244          nodeType = folder;
245          if(strchr(value, '/'))
246          {
247             char p[MAX_LOCATION];
248             char n[MAX_FILENAME];
249             GetLastDirectory(value, n);
250             StripLastDirectory(value, p);
251             name = CopyString(n);
252             path = CopyString(p);
253          }
254          else
255             name = CopyString(value);
256       }
257       get
258       {
259          // TOCHECK: Non Reentrant
260          static char insidePath[MAX_LOCATION];
261
262          strcpy(insidePath, (parent.type == project) ? "" : parent.path);
263          PathCatSlash(insidePath, name);
264
265          if(!fstrcmp(path, insidePath))
266             return name;
267          else
268          {
269             strcpy(insidePath, path);
270             if(!insidePath[0]) strcpy(insidePath, ".");
271             PathCatSlash(insidePath, name);
272             return insidePath;
273          }
274       }
275       isset { return nodeType == folder; }
276    };
277    property const String fileName
278    {
279       set
280       {
281          nodeType = file;
282          if(strchr(value, '/'))
283          {
284             char p[MAX_LOCATION];
285             char n[MAX_FILENAME];
286             GetLastDirectory(value, n);
287             StripLastDirectory(value, p);
288             name = CopyValidateMakefilePath(n);
289             path = CopyValidateMakefilePath(p);
290          }
291          else
292             name = CopyValidateMakefilePath(value);
293       }
294       get
295       {
296          // TOCHECK: Non Reentrant
297          static char insidePath[MAX_LOCATION];
298
299          strcpy(insidePath, (parent.type == project) ? "" : parent.path);
300          if(!fstrcmp(path, insidePath))
301             return name;
302          else
303          {
304             strcpy(insidePath, path);
305             if(!insidePath[0]) strcpy(insidePath, ".");
306             PathCatSlash(insidePath, name);
307             return insidePath;
308          }
309       }
310       isset { return nodeType == file && (options || configurations || platforms); }
311    };
312
313    LinkList<ProjectNode> files;
314    property ProjectOptions options
315    {
316       get { return project ? project.options : options; }
317       set { if(project) { delete project.options; project.options = value; } else { delete options; options = value; } }
318       isset { ProjectOptions options = project ? project.options : this.options; return options && !options.isEmpty; }
319    }
320    property Array<PlatformOptions> platforms
321    {
322       get { return project ? project.platforms : platforms; }
323       set
324       {
325          if(project) { project.platforms = value; }
326          else
327          {
328             if(platforms) { platforms.Free(); delete platforms; }
329             if(value)
330             {
331                List<PlatformOptions> empty { };
332                Iterator<PlatformOptions> it { value };
333                platforms = value;
334                for(p : platforms; !p.options || p.options.isEmpty) empty.Add(p);
335                for(p : empty; it.Find(p)) platforms.Delete(it.pointer);
336                delete empty;
337             }
338          }
339       }
340       isset
341       {
342          Array<PlatformOptions> platforms = project ? project.platforms : this.platforms;
343          if(platforms)
344          {
345             for(p : platforms)
346             {
347                if(p.options && !p.options.isEmpty)
348                   return true;
349             }
350          }
351          return false;
352       }
353    }
354    property List<ProjectConfig> configurations
355    {
356       get { return project ? project.configurations : configurations; }
357       set
358       {
359          if(project)
360          {
361             if(project.configurations)
362             {
363                project.configurations.Free();
364                delete project.configurations;
365             }
366             project.configurations = value;
367          }
368          else
369          {
370             if(configurations) { configurations.Free(); delete configurations; }
371             if(value)
372             {
373                List<ProjectConfig> empty { };
374                Iterator<ProjectConfig> it { value };
375                configurations = value;
376                for(c : configurations)
377                {
378                   bool somethingSet = c.options && !c.options.isEmpty;
379                   // TODO: Implement isset keyword
380                   if(!somethingSet && c.platforms && c.platforms.count)
381                   {
382                      for(p : c.platforms)
383                      {
384                         if(p.options && !p.options.isEmpty)
385                         {
386                            somethingSet = true;
387                            break;
388                         }
389                      }
390                   }
391                   if(!somethingSet)
392                      empty.Add(c);
393                }
394                for(c : empty; it.Find(c)) configurations.Delete(it.pointer);
395                delete empty;
396             }
397          }
398       }
399       isset
400       {
401          if(!parent) return true;
402          if(configurations)
403          {
404             for(c : configurations)
405             {
406                bool somethingSet = c.options && !c.options.isEmpty;
407                if(!somethingSet && c.platforms && c.platforms.count)
408                {
409                   for(p : c.platforms)
410                   {
411                      if(p.options && !p.options.isEmpty)
412                      {
413                         somethingSet = true;
414                         break;
415                      }
416                   }
417                }
418                return somethingSet;
419             }
420          }
421          return false;
422       }
423    }
424
425 private:
426    ProjectOptions options;
427    Array<PlatformOptions> platforms;
428    List<ProjectConfig> configurations;
429    ProjectNodeType nodeType;
430    ProjectNode parent;
431    char * name;
432    char * info;
433
434    // This holds the absolute path of the .epj for the project topnode (without the filename)
435    // It holds a relative path to the topNode (project) for other nodes (folders and files)
436    // For folders, it includes the folder it refers to. If there is a name difference between the
437    // file system folder and the grouping folder of the project view, it maps to that folder.
438    char * path;
439
440    NodeTypes type;
441    NodeIcons icon;
442    int indent;
443    DataRow row;
444
445    bool modified;
446
447    // This is only set for Top Nodes
448    Project project;
449
450    void OpenRulesPlatformExclusionIfs(File f, int * ifCount, Array<Platform> platforms)
451    {
452       if(!platforms.Find(unknown))  // unknown is "Common"
453       {
454          // e.g. ifneq ($(or $(or $(OSX_TARGET),$(LINUX_TARGET)),$(WINDOWS_TARGET)),)
455          int i = 0;
456          f.Puts("ifneq (");
457          for(i = 0; platforms.count && i < platforms.count - 1; i++)
458             f.Puts("$(or ");
459          i = 0;
460          for(p : platforms)
461          {
462             if(i > 0)
463                f.Puts(",");
464             f.Puts("$(");
465             f.Puts(PlatformToMakefileTargetVariable(p));
466             f.Puts(")");
467             if(i > 0)
468                f.Puts(")");
469             i++;
470          }
471          f.Puts(",)\n");
472          (*ifCount)++;
473       }
474       else
475          *ifCount = 0;
476    }
477
478    ProjectConfig GetMatchingNodeConfig(ProjectConfig prjConfig)
479    {
480       ProjectConfig nodeConfig = null;
481       if(property::configurations && prjConfig)
482       {
483          const char * configName = prjConfig.name;
484          for(cfg : property::configurations)
485          {
486             if(!strcmpi(cfg.name, configName))
487             {
488                nodeConfig = cfg;
489                break;
490             }
491          }
492       }
493       return nodeConfig;
494    }
495
496    property ProjectNode root { get { ProjectNode n; for(n = this; n.parent; n = n.parent); return n; } }
497
498    property bool containsFile
499    {
500       get
501       {
502          bool result = false;
503          if(files)
504          {
505             for(child : files)
506             {
507                if(child.type == file ||
508                      ((child.type == folder || child.type == folderOpen) && child.containsFile))
509                {
510                   result = true;
511                   break;
512                }
513             }
514          }
515          else
516             result = false;
517          return result;
518       }
519    }
520
521    char * GetFullFilePath(char * buffer)
522    {
523       if(buffer)
524       {
525          strcpy(buffer, root.path);
526          PathCatSlash(buffer, path);
527          PathCatSlash(buffer, name);
528       }
529       return buffer;
530    }
531
532    char * GetObjectFileName(char * buffer, Map<String, NameCollisionInfo> namesInfo, IntermediateFileType type, bool dotMain, const char * objectFileExt)
533    {
534       if(buffer && (this.type == file || (this.type == project && dotMain == true)))
535       {
536          bool collision;
537          char extension[MAX_EXTENSION];
538          char moduleName[MAX_FILENAME];
539          const char * objFileExt = objectFileExt ? objectFileExt : objectDefaultFileExt;
540          NameCollisionInfo info;
541
542          GetExtension(name, extension);
543          ReplaceSpaces(moduleName, name);
544          StripExtension(moduleName);
545          info = namesInfo[moduleName];
546          collision = info ? info.IsExtensionColliding(extension) : false;
547
548          if(dotMain)
549          {
550             Project prj = property::project;
551             ReplaceSpaces(buffer, prj.moduleName);
552             StripExtension(buffer);
553             strcat(buffer, ".main.ec");
554          }
555          else
556             strcpy(buffer, name);
557          if(!strcmp(extension, "ec") || dotMain)
558          {
559             if(type == c)
560                ChangeExtension(buffer, "c", buffer);
561             else if(type == sym)
562                ChangeExtension(buffer, "sym", buffer);
563             else if(type == imp)
564                ChangeExtension(buffer, "imp", buffer);
565             else if(type == bowl)
566                ChangeExtension(buffer, "bowl", buffer);
567          }
568          if(type == o)
569          {
570             if(collision)
571             {
572                strcat(buffer, ".");
573                strcat(buffer, objFileExt);
574             }
575             else
576                ChangeExtension(buffer, objFileExt, buffer);
577          }
578       }
579       return buffer;
580    }
581
582    char * GetFileSysMatchingPath(char * buffer)
583    {
584       if(buffer)
585       {
586          ProjectNode n, root = this.root;
587          for(n = this; n && (n.type == folder || n.type == project); n = n.parent)
588          {
589             strcpy(buffer, root.path);
590             if(n != root)
591                PathCatSlash(buffer, n.path);
592             if(FileExists(buffer).isDirectory)
593                break;
594          }
595          if(!(n && (n.type == folder || n.type == project)))
596             buffer[0] = '\0';
597       }
598       return buffer;
599    }
600
601    void CollectPerFileAndDirOptions(ProjectConfig prjConfig, Array<String> perFilePreprocessorDefs, Array<DirPath> perFileIncludeDirs)
602    {
603       ProjectNode node = null;
604       ProjectConfig config = GetMatchingNodeConfig(prjConfig);
605       List<ProjectNode> nodeStack { };
606
607       for(node = this; node && node.parent; node = node.parent)
608          nodeStack.Add(node);
609
610       // Should we reverse this stack to give priority to the per-file includes? Does the following technique already reverse?
611
612       // TODO: Check how to fix duplication of following options when configuration is made per-config-per-file
613       while((node = nodeStack.lastIterator.data))
614       {
615          ProjectOptions nodeOptions = node.property::options;
616          if(nodeOptions && nodeOptions.preprocessorDefinitions)
617          {
618             for(def : nodeOptions.preprocessorDefinitions)
619                perFilePreprocessorDefs.Add(CopyString(def));
620          }
621          if(config && config.options && config.options.preprocessorDefinitions)
622          {
623             for(def : config.options.preprocessorDefinitions)
624                perFilePreprocessorDefs.Add(CopyString(def));
625          }
626          if(nodeOptions && nodeOptions.includeDirs)
627          {
628             for(dir : nodeOptions.includeDirs)
629                perFileIncludeDirs.Add(CopySystemPath(dir));
630          }
631          if(config && config.options && config.options.includeDirs)
632          {
633             for(dir : config.options.includeDirs)
634                perFileIncludeDirs.Add(CopySystemPath(dir));
635          }
636          nodeStack.lastIterator.Remove();
637       }
638       delete nodeStack;
639    }
640
641
642    property Project project
643    {
644       get
645       {
646          ProjectNode n = this;
647          while(n && n.type != project) n = n.parent;
648          return n ? (*&n.project) : null;
649       }
650    }
651
652    void RenameConfig(const char * oldName, const char * newName)
653    {
654       if(files)
655       {
656          for(f : files; (f.configurations || f.files)) { f.RenameConfig(oldName, newName); }
657       }
658       if(property::configurations)
659       {
660          for(c : property::configurations; !strcmp(c.name, oldName))
661          {
662             delete c.name;
663             c.name = CopyString(newName);
664          }
665       }
666    }
667
668    void DeleteConfig(ProjectConfig configToDelete)
669    {
670       if(files)
671       {
672          for(f : files; (f.configurations || f.files)) { f.DeleteConfig(configToDelete); }
673       }
674       if(property::configurations)
675       {
676          Iterator<ProjectConfig> c { property::configurations };
677          while(c.Next())
678          {
679             ProjectConfig config = c.data;
680             if(!strcmp(configToDelete.name, config.name))
681             {
682                c.Remove();
683                delete config;
684                break;
685             }
686          }
687          if(!property::configurations.count)
688             property::configurations = null;
689       }
690    }
691
692    ProjectNode Backup()
693    {
694       ProjectNode backupNode { };
695
696       if(files)
697       {
698          backupNode.files = { };
699          for(f : files) backupNode.files.Add(f.Backup());
700       }
701       if(property::options)
702          backupNode.options = property::options.Copy();
703
704       if(property::platforms)
705       {
706          backupNode.platforms = { };
707          for(p : property::platforms)
708             backupNode.platforms.Add(p.Copy());
709       }
710
711       if(property::configurations)
712       {
713          backupNode.configurations = { };
714          for(c : property::configurations)
715             backupNode.configurations.Add(c.Copy());
716       }
717       return backupNode;
718    }
719
720    void Revert(ProjectNode backupNode)
721    {
722       if(files)
723       {
724          Iterator<ProjectNode> it { backupNode.files };
725          for(f : files)
726          {
727             it.Next();
728             f.Revert(it.data);
729          }
730       }
731
732       property::options = backupNode.options ? backupNode.options.Copy() : null;
733       if(backupNode.platforms)
734       {
735          Array<PlatformOptions> platforms { };
736          property::platforms = platforms;
737
738          for(p : backupNode.platforms)
739             platforms.Add(p.Copy());
740       }
741       if(backupNode.configurations)
742       {
743          List<ProjectConfig> configurations { };
744          property::configurations = configurations;
745          for(c : backupNode.configurations)
746             configurations.Add(c.Copy());
747       }
748    }
749
750    void FixupNode(char * parentPath)
751    {
752       if(!parent)
753       {
754          type = project;
755       }
756       else if(nodeType == file)
757       {
758          type = file;
759          if(!path)
760          {
761             path = CopyString((parent.type == folder || parent.type == resources) ? parentPath : "");
762          }
763       }
764       else if(nodeType == folder)
765       {
766          type = folder;
767
768          if(!path)
769          {
770             char temp[MAX_LOCATION];
771             strcpy(temp, (parent.type == folder || parent.type == resources) ? parentPath : "");
772             PathCatSlash(temp, name);
773             path = CopyString(temp);
774          }
775       }
776
777       indent = parent ? parent.indent + 1 : 0;
778
779       if(type == file)
780          icon = NodeIcons::SelectFileIcon(name);
781       else
782          icon = NodeIcons::SelectNodeIcon(type);
783
784       if(files)
785       {
786          for(f : files)
787          {
788             f.parent = this;
789
790             if(type == project)
791                parentPath[0] = '\0';
792             else if(type == resources || type == folder)
793                strcpy(parentPath, path);
794
795             f.FixupNode(parentPath);
796          }
797       }
798    }
799
800    const char * OnGetString(char * tempString, void * fieldData, bool * needClass)
801    {
802       if(!needClass)
803       {
804          // TOCHECK: Called from JSON writer
805          if(nodeType == file && !property::options && !property::configurations && !property::platforms && name)
806          {
807             strcpy(tempString, "\"");
808             strcat(tempString, property::fileName);
809             strcat(tempString, "\"");
810             return tempString;
811          }
812          else
813             return null;
814       }
815       else
816          // TOCHECK: Called from ProjectView rendering
817          return name ? name : "";
818    }
819
820    ~ProjectNode()
821    {
822       if(files)
823       {
824          files.Free();
825          delete files;
826       }
827       if(!project)
828          delete options;
829
830       if(!project && platforms)
831       {
832          platforms.Free();
833          delete platforms;
834       };
835       if(!project && configurations)
836       {
837          configurations.Free();
838          delete configurations;
839       }
840
841       /////////////////////////////
842       delete path;
843       delete name;
844       delete info;
845    }
846
847    property bool isInResources
848    {
849       get
850       {
851          ProjectNode node;
852          for(node = this; node; node = node.parent)
853          {
854             if(node.type == resources)
855                return true;
856          }
857          return false;
858       }
859    }
860
861    TwoStrings GetPlatformSpecificFu(ProjectConfig prjConfig)
862    {
863       TwoStrings result { a = CopyString(""), b = CopyString("") };
864       // note: unknown platform is for common
865       Map<Platform, SetBool> exclusionInfo { };
866       MapNode<Platform, SetBool> mn;
867       char * exp;
868       const char * var;
869       int len;
870       SetBool common;
871
872       CollectExclusionInfo(exclusionInfo, prjConfig);
873       common = exclusionInfo[unknown];
874       {
875          Map<Platform, SetBool> cleaned { };
876          SetBool opposite = common == true ? false : true;
877          for(mn = exclusionInfo.root.minimum; mn; mn = mn.next)
878          {
879             if(mn.key == unknown || mn.value == opposite)
880               cleaned[mn.key] = mn.value;
881          }
882          delete exclusionInfo;
883          exclusionInfo = cleaned;
884       }
885
886       if(exclusionInfo.count > 1)
887       {
888          if(exclusionInfo.count > 2)
889          {
890             exp = result.a;
891             len = strlen(exp) + strlen("$(if $(or ");
892             exp = renew exp char[len+1];
893             strcat(exp, "$(if $(or ");
894             result.a = exp;
895
896             for(mn = exclusionInfo.root.minimum; mn; mn = mn.next)
897             {
898                if(mn.key != unknown)
899                {
900                   const char * comma = mn.next ? "," : "";
901
902                   var = PlatformToMakefileTargetVariable(mn.key);
903
904                   exp = result.a;
905                   len = strlen(exp) + strlen("$(") + strlen(var) + strlen(")") + strlen(comma);
906                   exp = renew exp char[len+1];
907                   strcat(exp, "$(");
908                   strcat(exp, var);
909                   strcat(exp, ")");
910                   strcat(exp, comma);
911                   result.a = exp;
912                }
913             }
914
915             exp = result.a;
916             len = strlen(exp) + strlen("),");
917             exp = renew exp char[len+1];
918          }
919          else
920          {
921             if(exclusionInfo.root.minimum.key != unknown)
922                var = PlatformToMakefileTargetVariable(exclusionInfo.root.minimum.key);
923             else
924                var = PlatformToMakefileTargetVariable(exclusionInfo.root.minimum.next.key);
925
926             exp = result.a;
927             len = strlen(exp) + strlen("$(if $(") + strlen(var) + strlen("),");
928             exp = renew exp char[len+1];
929             strcat(exp, "$(if $(");
930             strcat(exp, var);
931          }
932
933          strcat(exp, "),");
934          result.a = exp;
935
936          exp = common == true ? result.b : result.a;
937          len = strlen(exp) + strlen(",");
938          exp = renew exp char[len+1];
939          strcat(exp, ",");
940          if(common == true) result.b = exp; else result.a = exp;
941
942          exp = result.b;
943          len = strlen(exp) + strlen(")");
944          exp = renew exp char[len+1];
945          strcat(exp, ")");
946          result.b = exp;
947       }
948       delete exclusionInfo;
949
950       return result;
951    }
952
953    bool GetIsExcluded(ProjectConfig prjConfig)
954    {
955       bool result;
956       // note: unknown platform is for common
957       Map<Platform, SetBool> exclusionInfo { };
958       CollectExclusionInfo(exclusionInfo, prjConfig);
959       if(exclusionInfo.count == 0)
960          result = false;
961       else if(exclusionInfo.count == 1)
962          result = exclusionInfo.root.minimum.value == true;
963       else
964       {
965          SetBool check = exclusionInfo.root.minimum.value;
966          MapNode<Platform, SetBool> mn;
967          for(mn = exclusionInfo.root.minimum; mn; mn = mn.next)
968          {
969             if(check != mn.value)
970                break;
971          }
972          if(!mn) // all are same
973             result = check == true;
974          else
975             result = false;
976       }
977       delete exclusionInfo;
978       return result;
979    }
980
981    bool GetIsExcludedForCompiler(ProjectConfig prjConfig, CompilerConfig compiler)
982    {
983       bool result;
984       Map<Platform, SetBool> exclusionInfo { };
985       SetBool common, platform;
986       CollectExclusionInfo(exclusionInfo, prjConfig);
987       common = exclusionInfo[unknown];
988       platform = exclusionInfo[compiler.targetPlatform];
989       result = platform == true || (common == true && platform == unset);
990       delete exclusionInfo;
991       return result;
992    }
993
994    void CollectExclusionInfo(Map<Platform, SetBool> output, ProjectConfig prjConfig)
995    {
996       // note: unknown platform is for common
997       Platform platform;
998       ProjectConfig config = GetMatchingNodeConfig(prjConfig);
999       ProjectOptions options = property::options;
1000       Array<PlatformOptions> platforms = property::platforms;
1001
1002       if(parent)
1003          parent.CollectExclusionInfo(output, prjConfig);
1004       else
1005          output[unknown] = unset;
1006
1007       if(options && options.excludeFromBuild)
1008          output[unknown] = options.excludeFromBuild;
1009
1010       if(config && config.options && config.options.excludeFromBuild)
1011          output[unknown] = config.options.excludeFromBuild;
1012
1013       if(platforms)
1014       {
1015          for(p : platforms)
1016          {
1017             if(p.options.excludeFromBuild && (platform = p.name))
1018                output[platform] = p.options.excludeFromBuild;
1019          }
1020       }
1021       if(config && config.platforms)
1022       {
1023          for(p : config.platforms)
1024          {
1025             if(p.options.excludeFromBuild && (platform = p.name))
1026                output[platform] = p.options.excludeFromBuild;
1027          }
1028       }
1029    }
1030
1031    void EnsureVisible()
1032    {
1033       if(parent)
1034          parent.EnsureVisible();
1035       row.collapsed = false;
1036    }
1037
1038    void Delete()
1039    {
1040       if(parent)
1041          parent.files.Delete(this);
1042    }
1043
1044    ProjectNode Find(const char * name, bool includeResources)
1045    {
1046       ProjectNode result = null;
1047       if(files)
1048       {
1049          for(child : files)
1050          {
1051             if(includeResources || child.type != resources)
1052             {
1053                if(child.type != folder && child.name && !strcmpi(child.name, name))
1054                {
1055                   result = child;
1056                   break;
1057                }
1058                result = child.Find(name, includeResources);
1059                if(result)
1060                   break;
1061             }
1062          }
1063       }
1064       return result;
1065    }
1066
1067    ProjectNode FindWithPath(const char * name, bool includeResources)
1068    {
1069       ProjectNode result = null;
1070       if(files)
1071       {
1072          for(child : files)
1073          {
1074             if(includeResources || child.type != resources)
1075             {
1076                char path[MAX_LOCATION];
1077                strcpy(path, child.path);
1078                if(child.type != folder && child.name)
1079                {
1080                   PathCatSlash(path, child.name);
1081                   if(!strcmpi(path, name))
1082                   {
1083                      result = child;
1084                      break;
1085                   }
1086                }
1087                result = child.FindWithPath(name, includeResources);
1088                if(result)
1089                   break;
1090             }
1091          }
1092       }
1093       return result;
1094    }
1095
1096    ProjectNode FindByFullPath(const char * path, bool includeResources)
1097    {
1098       if(files)
1099       {
1100          char name[MAX_FILENAME];
1101          GetLastDirectory(path, name);
1102          return InternalFindByFullPath(path, includeResources, name);
1103       }
1104       return null;
1105    }
1106
1107    ProjectNode InternalFindByFullPath(const char * path, bool includeResources, const char * lastDirName)
1108    {
1109       ProjectNode result = null;
1110       if(files)
1111       {
1112          for(child : files)
1113          {
1114             if(includeResources || child.type != resources)
1115             {
1116                if(child.type != file)
1117                   result = child.InternalFindByFullPath(path, includeResources, lastDirName);
1118                else if(child.name && !fstrcmp(lastDirName, child.name))
1119                {
1120                   char p[MAX_LOCATION];
1121                   child.GetFullFilePath(p);
1122                   if(!fstrcmp(p, path))
1123                   {
1124                      result = child;
1125                      break;
1126                   }
1127                }
1128                if(result)
1129                   break;
1130             }
1131          }
1132       }
1133       return result;
1134    }
1135
1136    ProjectNode FindByObjectFileName(const char * fileName, IntermediateFileType type, bool dotMain, Map<String, NameCollisionInfo> namesInfo, const char * objectFileExt)
1137    {
1138       char p[MAX_LOCATION];
1139       ProjectNode result = null;
1140       if(dotMain == true && this.type == project)
1141       {
1142          GetObjectFileName(p, namesInfo, type, dotMain, objectFileExt);
1143          if(!fstrcmp(p, fileName))
1144             result = this;
1145       }
1146       else if(files)
1147       {
1148          for(child : files; child.type != resources)
1149          {
1150             if(child.type != file && (result = child.FindByObjectFileName(fileName, type, dotMain, namesInfo, objectFileExt)))
1151                break;
1152             else if(child.type == file && child.name)
1153             {
1154                child.GetObjectFileName(p, namesInfo, type, dotMain, objectFileExt);
1155                if(!fstrcmp(p, fileName))
1156                {
1157                   result = child;
1158                   break;
1159                }
1160             }
1161          }
1162       }
1163       return result;
1164    }
1165
1166    ProjectNode FindSpecial(const char * name, bool recursive, bool includeResources, bool includeFolders)
1167    {
1168       ProjectNode result = null;
1169       if(files)
1170       {
1171          for(child : files)
1172          {
1173             if(includeResources || child.type != resources)
1174             {
1175                if((includeFolders || child.type != folder) && child.name && !strcmpi(child.name, name))
1176                {
1177                   result = child;
1178                   break;
1179                }
1180                if(recursive)
1181                   result = child.FindSpecial(name, recursive, includeResources, includeFolders);
1182                if(result)
1183                   break;
1184             }
1185          }
1186       }
1187       return result;
1188    }
1189
1190    ProjectNode FindSameNameConflict(const char * name, bool includeResources,
1191       Map<Platform, SetBool> exclusionInfo, ProjectConfig prjConfig)
1192    {
1193       ProjectNode result = null;
1194       Map<Platform, SetBool> compareExclusion { };
1195       SetBool common, commonComp;
1196       SetBool actual, actualComp;
1197       if(files)
1198       {
1199          for(child : files)
1200          {
1201             if(includeResources || child.type != resources)
1202             {
1203                if(child.type != folder && child.name && !strcmpi(child.name, name))
1204                {
1205                   child.CollectExclusionInfo(compareExclusion, prjConfig);
1206                   common = exclusionInfo[unknown];
1207                   commonComp = compareExclusion[unknown];
1208                   if(exclusionInfo.count == 1 && compareExclusion.count == 1)
1209                   {
1210                      if(!(common == true || commonComp == true))
1211                      {
1212                         result = child;
1213                         break;
1214                      }
1215                   }
1216                   else
1217                   {
1218                      Platform platform;
1219                      for(platform = (Platform)1; platform < Platform::enumSize; platform++)
1220                      {
1221                         actual = common;
1222                         actualComp = commonComp;
1223                         if(exclusionInfo[platform] != unset)
1224                            actual = exclusionInfo[platform];
1225                         if(compareExclusion[platform] != unset)
1226                            actualComp = compareExclusion[platform];
1227                         if(!(actual == true || actualComp == true))
1228                         {
1229                            result = child;
1230                            break;
1231                         }
1232                      }
1233                      if(result) break;
1234                   }
1235                   compareExclusion.Free();
1236                   break;
1237                }
1238                result = child.FindSameNameConflict(name, includeResources, exclusionInfo, prjConfig);
1239                if(result) break;
1240             }
1241          }
1242          compareExclusion.Free();
1243       }
1244       delete compareExclusion;
1245       return result;
1246    }
1247
1248    ProjectNode Add(Project project, const char * filePath, ProjectNode after, NodeTypes type, NodeIcons icon, bool checkIfExists)
1249    {
1250       ProjectNode node = null;
1251       if(!project.topNode.FindByFullPath(filePath, true))
1252       {
1253          char temp[MAX_LOCATION];
1254          Map<Platform, SetBool> exclusionInfo { };
1255
1256          GetLastDirectory(filePath, temp);
1257          //if(!checkIfExists || !project.topNode.Find(temp, false))
1258
1259          // TOCHECK: Shouldn't this apply either for all configs or none?
1260          CollectExclusionInfo(exclusionInfo, project.config);
1261          if(!checkIfExists || type == folder || !project.topNode.FindSameNameConflict(temp, false, exclusionInfo, project.config))
1262          {
1263             // Do the check for folder in the same parent or resource files only here
1264             if(type == folder || !checkIfExists)
1265             {
1266                for(node : files)
1267                {
1268                   if(node.name && !strcmpi(node.name, temp))
1269                      return null;
1270                }
1271             }
1272
1273             node = ProjectNode { parent = this, indent = indent + 1, type = type, icon = icon, name = CopyString(temp) };
1274             if(type != file)
1275             {
1276                node.files = { };
1277                node.nodeType = folder;
1278             }
1279             if(type != folder)
1280             {
1281                if(filePath)
1282                {
1283                   StripLastDirectory(filePath, temp);
1284                   MakePathRelative(temp, project.topNode.path, temp);
1285                   node.path = CopyUnixPath(temp);
1286                }
1287                node.nodeType = file;
1288             }
1289             else
1290             {
1291                strcpy(temp, (type == NodeTypes::project) ? "" : path);
1292                PathCatSlash(temp, node.name);
1293                node.path = CopyString(temp);
1294             }
1295             files.Insert(after, node);
1296          }
1297          delete exclusionInfo;
1298       }
1299       return node;
1300    }
1301
1302 #ifndef MAKEFILE_GENERATOR
1303    void OnDisplay(Surface surface, int x, int y, int width, ProjectView projectView, Alignment alignment, DataDisplayFlags displayFlags)
1304    {
1305       char label[MAX_FILENAME];
1306       int xStart;
1307       int len;
1308       int w, h;
1309       Bitmap bmp;
1310       bool showConfig = true;
1311
1312       if(!projectView)
1313       {
1314          showConfig = false;
1315          projectView = ide.projectView;
1316       }
1317
1318       bmp = projectView.icons[icon].bitmap;
1319       xStart = x + (bmp ? (bmp.width + 5) : 0);
1320
1321       GetLastDirectory(name, label);
1322       if(!showConfig || projectView.drawingInProjectSettingsDialogHeader)
1323       {
1324          if(projectView.drawingInProjectSettingsDialogHeader || (type == project && info))
1325          {
1326             if(projectView.projectSettingsDialog && projectView.projectSettingsDialog.buildTab)
1327             {
1328                const char * addendum = projectView.projectSettingsDialog.buildTab.selectedConfigName;
1329                if(strlen(addendum))
1330                {
1331                   strcat(label, " (");
1332                   strcat(label, addendum);
1333                   strcat(label, ")");
1334                }
1335                addendum = projectView.projectSettingsDialog.buildTab.selectedPlatformName;
1336                if(strlen(addendum))
1337                {
1338                   strcat(label, " (");
1339                   strcat(label, addendum);
1340                   strcat(label, ")");
1341                }
1342             }
1343          }
1344       }
1345       else if(!projectView.drawingInProjectSettingsDialog)
1346       {
1347          if(modified)
1348             strcat(label, " *");
1349          if(type == project && info)
1350          {
1351             int len = strlen(info) + 4;
1352             char * more = new char[len];
1353             sprintf(more, " (%s)", info);
1354             strcat(label, more);
1355             delete more;
1356          }
1357       }
1358       len = strlen(label);
1359
1360       if(!bmp)
1361       {
1362          if(type == folder || type == folderOpen)
1363             surface.SetForeground(yellow);
1364          indent = 8;
1365       }
1366
1367       surface.TextOpacity(false);
1368       surface.TextExtent(label, len, &w, &h);
1369       h = Max(h, 16);
1370
1371       // Draw the current row stipple
1372       if(displayFlags.selected)
1373          //surface.Area(xStart - 1, y, xStart - 1, y + h - 1);
1374          //surface.Area(xStart + w - 1, y, xStart + w + 1, y + h - 1);
1375          surface.Area(xStart - 3, y, xStart + w + 1, y + h - 1);
1376
1377       surface.WriteTextDots(alignment, xStart, y + 2, width, label, len);
1378
1379       if(!app.textMode)
1380       {
1381          if(displayFlags.current)
1382          {
1383             if(displayFlags.active)
1384             {
1385                surface.LineStipple(0x5555);
1386                if(displayFlags.selected)
1387                   surface.SetForeground(projectView.fileList.stippleColor);
1388                else
1389                   surface.SetForeground(projectView.fileList.foreground);
1390             }
1391             else
1392             {
1393                surface.SetForeground(SELECTION_COLOR);
1394             }
1395             surface.Rectangle(xStart - 3, y, xStart + w + 1, y + h - 1);
1396             surface.LineStipple(0);
1397          }
1398
1399          if(bmp)
1400          {
1401             surface.SetForeground(white);
1402             surface.Blit(bmp, x /*+ indent * indent*/,y,0,0, bmp.width, bmp.height);
1403          }
1404       }
1405    }
1406 #endif
1407
1408    int OnCompare(ProjectNode b)
1409    {
1410       int result = 0;
1411       if(type == b.type /*|| type >= TYPE_DRIVE*/)
1412          result = strcmpi(name, b.name);
1413       else
1414       {
1415          if(type == folder && b.type == file) result = -1;
1416          else if(type == file && b.type == folder) result = 1;
1417       }
1418       return result;
1419    }
1420
1421    bool ContainsFilesWithExtension(const char * extension, ProjectConfig prjConfig)
1422    {
1423       if(type == file && name && name[0])
1424       {
1425          char ext[MAX_EXTENSION];
1426          GetExtension(name, ext);
1427          if(!fstrcmp(ext, extension))
1428             return true;
1429       }
1430       else if(files)
1431       {
1432          for(child : files)
1433          {
1434             if(child.type != resources && (child.type == folder || !prjConfig || !child.GetIsExcluded(prjConfig)))
1435             {
1436                if(child.ContainsFilesWithExtension(extension, prjConfig))
1437                   return true;
1438             }
1439          }
1440       }
1441       return false;
1442    }
1443
1444    void GenMakefileGetNameCollisionInfo(Map<String, NameCollisionInfo> namesInfo, ProjectConfig prjConfig)
1445    {
1446       if(type == file)
1447       {
1448          char extension[MAX_EXTENSION];
1449          GetExtension(name, extension);
1450          if(!strcmpi(extension, "ec") || !strcmpi(extension, "s") || !strcmpi(extension, "c") ||
1451                !strcmpi(extension, "rc") || !strcmpi(extension, "cpp") || !strcmpi(extension, "cc") ||
1452                !strcmpi(extension, "cxx") || !strcmpi(extension, "m") || !strcmpi(extension, "mm"))
1453          {
1454             char moduleName[MAX_FILENAME];
1455             NameCollisionInfo info;
1456             ReplaceSpaces(moduleName, name);
1457             StripExtension(moduleName);
1458             info = namesInfo[moduleName];
1459             if(!info)
1460                info = NameCollisionInfo { };
1461             info.count++; // += 1; unless this is for a bug?
1462             if(!strcmpi(extension, "ec"))
1463                info.ec = true;
1464             else if(!strcmpi(extension, "s"))
1465                info.s = true;
1466             else if(!strcmpi(extension, "c"))
1467                info.c = true;
1468             else if(!strcmpi(extension, "rc"))
1469                info.rc = true;
1470             else if(!strcmpi(extension, "cpp"))
1471                info.cpp = true;
1472             else if(!strcmpi(extension, "cc"))
1473                info.cc = true;
1474             else if(!strcmpi(extension, "cxx"))
1475                info.cxx = true;
1476             else if(!strcmpi(extension, "m"))
1477                info.m = true;
1478             else if(!strcmpi(extension, "mm"))
1479                info.mm = true;
1480             namesInfo[moduleName] = info;
1481          }
1482       }
1483       else if(files)
1484       {
1485          for(child : files)
1486          {
1487             if(child.type != resources && (child.type == folder || !child.GetIsExcluded(prjConfig)))
1488                child.GenMakefileGetNameCollisionInfo(namesInfo, prjConfig);
1489          }
1490       }
1491    }
1492
1493    int GenMakefilePrintNode(File f, Project project, GenMakefilePrintTypes printType,
1494       Map<String, NameCollisionInfo> namesInfo, Array<String> items,
1495       ProjectConfig prjConfig, bool * containsCXX)
1496    {
1497       int count = 0;
1498       if(type == file)
1499       {
1500          char s[2048];
1501          TwoStrings ts = GetPlatformSpecificFu(prjConfig);
1502          char moduleName[MAX_FILENAME];
1503          char extension[MAX_EXTENSION];
1504          GetExtension(name, extension);
1505          if(printType == resources)
1506          {
1507             bool useRes;
1508             char tempPath[MAX_LOCATION];
1509             char modulePath[MAX_LOCATION];
1510
1511             tempPath[0] = '\0';
1512             if(eString_PathInsideOfMore(path, project.resNode.path, tempPath))
1513             {
1514                useRes = true;
1515                PathCatSlash(tempPath, name);
1516             }
1517             else
1518             {
1519                useRes = false;
1520                strcpy(tempPath, path);
1521                PathCatSlash(tempPath, name);
1522             }
1523             EscapeForMake(modulePath, tempPath, false, true, false);
1524             sprintf(s, "%s%s%s%s", ts.a, useRes ? "$(RES)" : "", modulePath, ts.b);
1525             items.Add(CopyString(s));
1526          }
1527          else if(printType == sources)
1528          {
1529             if(!strcmpi(extension, "s") || !strcmpi(extension, "c") || !strcmpi(extension, "cpp") ||
1530                   !strcmpi(extension, "cc") || !strcmpi(extension, "cxx") ||
1531                   !strcmpi(extension, "m") || !strcmpi(extension, "mm"))
1532             {
1533                char modulePath[MAX_LOCATION];
1534                EscapeForMake(modulePath, path, false, true, false);
1535                EscapeForMake(moduleName, name, false, true, false);
1536                sprintf(s, "%s%s%s%s%s", ts.a, modulePath, path[0] ? SEPS : "", moduleName, ts.b);
1537                items.Add(CopyString(s));
1538             }
1539          }
1540          else if(printType == eCsources)
1541          {
1542             if(!strcmpi(extension, "ec"))
1543             {
1544                char modulePath[MAX_LOCATION];
1545                EscapeForMake(modulePath, path, true, true, false);
1546                EscapeForMake(moduleName, name, true, true, false);
1547                sprintf(s, "%s%s%s%s%s", ts.a, modulePath, path[0] ? SEPS : "", moduleName, ts.b);
1548                items.Add(CopyString(s));
1549                count++;
1550             }
1551          }
1552          else if(printType == rcSources)
1553          {
1554             if(!strcmpi(extension, "rc"))
1555             {
1556                char modulePath[MAX_LOCATION];
1557                EscapeForMake(modulePath, path, false, true, false);
1558                EscapeForMake(moduleName, name, false, true, false);
1559                sprintf(s, "%s%s%s%s%s", ts.a, modulePath, path[0] ? SEPS : "", moduleName, ts.b);
1560                items.Add(CopyString(s));
1561                count++;
1562             }
1563          }
1564          else if(!strcmpi(extension, "s") || !strcmpi(extension, "c") || !strcmpi(extension, "cpp") ||
1565                !strcmpi(extension, "cc") || !strcmpi(extension, "cxx") ||
1566                !strcmpi(extension, "m") || !strcmpi(extension, "mm"))
1567          {
1568             if(printType == objects)
1569             {
1570                bool collision;
1571                NameCollisionInfo info;
1572                count++;
1573                EscapeForMake(moduleName, name, false, true, false);
1574                StripExtension(moduleName);
1575                info = namesInfo[moduleName];
1576                collision = info ? info.IsExtensionColliding(extension) : false;
1577                sprintf(s, "%s$(OBJ)%s%s%s$(O)%s", ts.a, moduleName, collision ? "." : "", collision ? extension : "", ts.b);
1578                items.Add(CopyString(s));
1579                if(containsCXX && (!strcmpi(extension, "cpp") || !strcmpi(extension, "cc") || !strcmpi(extension, "cxx")))
1580                   *containsCXX = true;
1581             }
1582          }
1583          delete ts;
1584       }
1585       else if(files)
1586       {
1587          for(child : files)
1588          {
1589             if(child.type != resources && (child.type == folder || !child.GetIsExcluded(prjConfig)))
1590                count += child.GenMakefilePrintNode(f, project, printType, namesInfo, items, prjConfig, containsCXX);
1591          }
1592       }
1593       return count;
1594    }
1595
1596    void GenMakefilePrintSymbolRules(File f, Project project,
1597          ProjectConfig prjConfig, //Map<Platform, bool> parentExcludedPlatforms,
1598          Map<intptr, int> nodeCFlagsMapping, Map<intptr, int> nodeECFlagsMapping)
1599    {
1600       int ifCount = 0;
1601       Array<Platform> platforms = GetPlatformsArrayFromExclusionInfo(prjConfig);
1602       //ProjectNode child;
1603       //char objDir[MAX_LOCATION];
1604       //ReplaceSpaces(objDir, config.objDir.dir);
1605
1606       //eSystem_Log("Printing Symbol Rules\n");
1607       if(type == file)
1608       {
1609          char extension[MAX_EXTENSION];
1610          char modulePath[MAX_LOCATION];
1611          char moduleName[MAX_FILENAME];
1612
1613          GetExtension(name, extension);
1614          if(!strcmpi(extension, "ec"))
1615          {
1616             //DualPipe dep;
1617             //char command[2048];
1618
1619             ReplaceSpaces(moduleName, name);
1620             StripExtension(moduleName);
1621
1622             ReplaceSpaces(modulePath, path);
1623             if(modulePath[0]) strcat(modulePath, SEPS);
1624
1625             OpenRulesPlatformExclusionIfs(f, &ifCount, platforms);
1626 #if 0
1627             // *** Dependency command ***
1628             sprintf(command, "gcc -MT $(OBJ)%s.o -MM %s%s.%s", moduleName,
1629                modulePath, moduleName, extension);
1630
1631             // System Includes (from global settings)
1632             for(item : compiler.dirs[Includes])
1633             {
1634                strcat(command, " -isystem ");
1635                if(strchr(item, ' '))
1636                {
1637                   strcat(command, "\"");
1638                   strcat(command, item);
1639                   strcat(command, "\"");
1640                }
1641                else
1642                   strcat(command, item);
1643             }
1644
1645             for(item : project.includeDirs)
1646             {
1647                strcat(command, " -I");
1648                if(strchr(item, ' '))
1649                {
1650                   strcat(command, "\"");
1651                   strcat(command, item);
1652                   strcat(command, "\"");
1653                }
1654                else
1655                   strcat(command, item);
1656             }
1657             for(item : project.preprocessorDefs)
1658             {
1659                strcat(command, " -D");
1660                strcat(command, item);
1661             }
1662
1663             // Execute it
1664             if((dep = DualPipeOpen(PipeOpenMode { output = true, error = true/*, input = true*/ }, command)))
1665             {
1666                char line[1024];
1667                bool firstLine = true;
1668                bool result = true;
1669
1670                // To do some time: auto save external dependencies?
1671                while(!dep.Eof())
1672                {
1673                   if(dep.GetLine(line, sizeof(line)-1))
1674                   {
1675                      if(firstLine)
1676                      {
1677                         char * colon = strstr(line, ":");
1678                         if(strstr(line, "No such file") || strstr(line, ",") || (colon && strstr(colon+1, ":")))
1679                         {
1680                            result = false;
1681                            break;
1682                         }
1683                         firstLine = false;
1684                      }
1685                      f.Puts(line);
1686                      f.Puts("\n");
1687                   }
1688                   if(!result) break;
1689                }
1690                delete dep;
1691
1692                // If we failed to generate dependencies...
1693                if(!result)
1694                {
1695 #endif
1696                   f.Printf("$(OBJ)%s.sym: %s%s.%s\n",
1697                      moduleName, modulePath, moduleName, extension);
1698
1699                   f.Puts("\t$(ECP)");
1700
1701                   f.Puts(" $(CFLAGS)");
1702                   f.Puts(" $(CECFLAGS)"); // tocheck: what of this? should this stuff be per-file customized?
1703
1704                   GenMakePrintNodeFlagsVariable(this, nodeECFlagsMapping, "ECFLAGS", f);
1705                   GenMakePrintNodeFlagsVariable(this, nodeCFlagsMapping, "PRJ_CFLAGS", f);
1706
1707                   f.Printf(" -c $(call quote_path,%s%s.%s) -o $(call quote_path,$@)\n",
1708                      modulePath, moduleName, extension);
1709                   if(ifCount) f.Puts("endif\n");
1710                   f.Puts("\n");
1711 #if 0
1712                }
1713             }
1714 #endif
1715          }
1716       }
1717       if(files)
1718       {
1719          if(ContainsFilesWithExtension("ec", prjConfig))
1720          {
1721             for(child : files)
1722             {
1723                if(child.type != resources && (child.type == folder || !child.GetIsExcluded(prjConfig)))
1724                   child.GenMakefilePrintSymbolRules(f, project, prjConfig, /*excludedPlatforms,*/
1725                         nodeCFlagsMapping, nodeECFlagsMapping);
1726             }
1727          }
1728       }
1729       delete platforms;
1730    }
1731
1732    void GenMakefilePrintPrepecsRules(File f, Project project,
1733          ProjectConfig prjConfig, /*Map<Platform, bool> parentExcludedPlatforms,*/
1734          Map<intptr, int> nodeCFlagsMapping, Map<intptr, int> nodeECFlagsMapping)
1735    {
1736       int ifCount = 0;
1737       Array<Platform> platforms = GetPlatformsArrayFromExclusionInfo(prjConfig);
1738       //ProjectNode child;
1739       //char objDir[MAX_LOCATION];
1740       //ReplaceSpaces(objDir, config.objDir.dir);
1741
1742       //eSystem_Log("Printing Symbol Rules\n");
1743       if(type == file)
1744       {
1745          char extension[MAX_EXTENSION];
1746          char modulePath[MAX_LOCATION];
1747          char moduleName[MAX_FILENAME];
1748
1749          GetExtension(name, extension);
1750          if(!strcmpi(extension, "ec"))
1751          {
1752             ReplaceSpaces(moduleName, name);
1753             StripExtension(moduleName);
1754
1755             ReplaceSpaces(modulePath, path);
1756             if(modulePath[0]) strcat(modulePath, SEPS);
1757
1758             OpenRulesPlatformExclusionIfs(f, &ifCount, platforms);
1759             f.Printf("$(OBJ)%s$(EC): %s%s.%s\n",
1760                moduleName, modulePath, moduleName, extension);
1761             /*f.Printf("\t$(CPP) %s%s.%s %s$(S)\n\n",
1762                modulePath, moduleName, extension, moduleName);*/
1763
1764             f.Puts("\t$(CPP)");
1765
1766             f.Puts(" $(CFLAGS)");
1767             //f.Puts(" $(CECFLAGS)");
1768             //GenMakePrintNodeFlagsVariable(this, nodeECFlagsMapping, "ECFLAGS", f);
1769             GenMakePrintNodeFlagsVariable(this, nodeCFlagsMapping, "PRJ_CFLAGS", f);
1770
1771             f.Printf(" -x c -E %s%s.%s -o $(OBJ)%s$(EC)\n",
1772                modulePath, moduleName, extension, moduleName);
1773             if(ifCount) f.Puts("endif\n");
1774             f.Puts("\n");
1775          }
1776       }
1777       if(files)
1778       {
1779          if(ContainsFilesWithExtension("ec", prjConfig))
1780          {
1781             for(child : files)
1782             {
1783                if(child.type != resources && (child.type == folder || !child.GetIsExcluded(prjConfig)))
1784                   child.GenMakefilePrintPrepecsRules(f, project, prjConfig, /*excludedPlatforms,*/
1785                         nodeCFlagsMapping, nodeECFlagsMapping);
1786             }
1787          }
1788       }
1789       delete platforms;
1790    }
1791
1792    void GenMakefilePrintCObjectRules(File f, Project project,
1793       ProjectConfig prjConfig, /*Map<Platform, bool> parentExcludedPlatforms,*/
1794       Map<intptr, int> nodeCFlagsMapping, Map<intptr, int> nodeECFlagsMapping)
1795    {
1796       int ifCount = 0;
1797       Array<Platform> platforms = GetPlatformsArrayFromExclusionInfo(prjConfig);
1798       //ProjectNode child;
1799       //char objDir[MAX_LOCATION];
1800       //ReplaceSpaces(objDir, config.objDir.dir);
1801       //eSystem_Log("Printing C Object Rules\n");
1802       if(type == file)
1803       {
1804          char extension[MAX_EXTENSION];
1805          char modulePath[MAX_LOCATION];
1806          char moduleName[MAX_FILENAME];
1807
1808          GetExtension(name, extension);
1809          if(!strcmpi(extension, "ec"))
1810          {
1811             //DualPipe dep;
1812             //char command[2048];
1813
1814             ReplaceSpaces(moduleName, name);
1815             StripExtension(moduleName);
1816
1817             ReplaceSpaces(modulePath, path);
1818             if(modulePath[0]) strcat(modulePath, SEPS);
1819
1820             OpenRulesPlatformExclusionIfs(f, &ifCount, platforms);
1821 #if 0
1822             // *** Dependency command ***
1823             sprintf(command, "gcc -MT $(OBJ)%s.o -MM %s%s.%s",
1824                moduleName, modulePath, moduleName, extension);
1825
1826             // System Includes (from global settings)
1827             for(item : compiler.dirs[Includes])
1828             {
1829                strcat(command, " -isystem ");
1830                if(strchr(item, ' '))
1831                {
1832                   strcat(command, "\"");
1833                   strcat(command, item);
1834                   strcat(command, "\"");
1835                }
1836                else
1837                   strcat(command, item);
1838             }
1839
1840             for(item : config.includeDirs)
1841             {
1842                strcat(command, " -I");
1843                if(strchr(item, ' '))
1844                {
1845                   strcat(command, "\"");
1846                   strcat(command, item);
1847                   strcat(command, "\"");
1848                }
1849                else
1850                   strcat(command, item);
1851             }
1852             for(item : config.preprocessorDefs)
1853             {
1854                strcat(command, " -D");
1855                strcat(command, item);
1856             }
1857
1858             // Execute it
1859             if((dep = DualPipeOpen(PipeOpenMode { output = true, error = true/*, input = true*/ }, command)))
1860             {
1861                char line[1024];
1862                bool result = true;
1863                bool firstLine = true;
1864
1865                // To do some time: auto save external dependencies?
1866                while(!dep.Eof())
1867                {
1868                   if(dep.GetLine(line, sizeof(line)-1))
1869                   {
1870                      if(firstLine)
1871                      {
1872                         char * colon = strstr(line, ":");
1873                         if(strstr(line, "No such file") || strstr(line, ",") || (colon && strstr(colon+1, ":")))
1874                         {
1875                            result = false;
1876                            break;
1877                         }
1878                         firstLine = false;
1879                      }
1880                      f.Puts(line);
1881                      f.Puts("\n");
1882                   }
1883                   if(!result) break;
1884                }
1885                delete dep;
1886
1887                // If we failed to generate dependencies...
1888                if(!result)
1889                {
1890                   /* COMMENTED OUT FOR NOW
1891                   f.Printf("$(OBJ)%s.c: %s%s.%s $(Symbols)\n",
1892                      moduleName, modulePath, moduleName, extension);
1893                   */
1894 #endif
1895                   f.Printf("$(OBJ)%s.c: %s%s.%s $(OBJ)%s.sym | $(SYMBOLS)\n",
1896                      moduleName, modulePath, moduleName, extension, moduleName);
1897 #if 0
1898                }
1899             }
1900 #endif
1901          /*
1902             f.Printf("\t$(ECC) %s%s.%s $(OBJ)%s.c\n\n",
1903                modulePath, moduleName, extension, moduleName);
1904          */
1905
1906             f.Puts("\t$(ECC)");
1907
1908             f.Puts(" $(CFLAGS)");
1909             f.Puts(" $(CECFLAGS)"); // what of this? should this stuff be per-file customized?
1910             GenMakePrintNodeFlagsVariable(this, nodeECFlagsMapping, "ECFLAGS", f);
1911             GenMakePrintNodeFlagsVariable(this, nodeCFlagsMapping, "PRJ_CFLAGS", f);
1912             f.Puts(" $(FVISIBILITY)");
1913
1914             f.Printf(" -c $(call quote_path,%s%s.%s) -o $(call quote_path,$@) -symbols $(OBJ)\n",
1915                modulePath, moduleName, extension);
1916             if(ifCount) f.Puts("endif\n");
1917             f.Puts("\n");
1918          }
1919       }
1920       if(files)
1921       {
1922          if(ContainsFilesWithExtension("ec", prjConfig))
1923          {
1924             for(child : files)
1925             {
1926                if(child.type != resources && (child.type == folder || !child.GetIsExcluded(prjConfig)))
1927                   child.GenMakefilePrintCObjectRules(f, project, prjConfig, /*excludedPlatforms,*/
1928                         nodeCFlagsMapping, nodeECFlagsMapping);
1929             }
1930          }
1931       }
1932       delete platforms;
1933    }
1934
1935    void GenMakefilePrintObjectRules(File f, Project project,
1936       Map<String, NameCollisionInfo> namesInfo,
1937       ProjectConfig prjConfig,
1938       //Map<Platform, bool> parentExcludedPlatforms,
1939       Map<intptr, int> nodeCFlagsMapping, Map<intptr, int> nodeECFlagsMapping)
1940    {
1941       int ifCount = 0;
1942       Array<Platform> platforms = GetPlatformsArrayFromExclusionInfo(prjConfig);
1943       //ProjectNode child;
1944       //char objDir[MAX_LOCATION];
1945       //ReplaceSpaces(objDir, config.objDir.dir);
1946       //eSystem_Log("Printing Object Rules\n");
1947       if(type == file)
1948       {
1949          bool collision;
1950          char extension[MAX_EXTENSION];
1951          char modulePath[MAX_LOCATION];
1952          char moduleName[MAX_FILENAME];
1953
1954          GetExtension(name, extension);
1955          if(!strcmpi(extension, "s") || !strcmpi(extension, "c") || !strcmpi(extension, "rc") ||
1956                !strcmpi(extension, "cpp") || !strcmpi(extension, "cc") || !strcmpi(extension, "cxx") ||
1957                !strcmpi(extension, "m") || !strcmpi(extension, "mm") || !strcmpi(extension, "ec"))
1958          {
1959             //DualPipe dep;
1960             //char command[2048];
1961             NameCollisionInfo info;
1962
1963             ReplaceSpaces(moduleName, name);
1964             StripExtension(moduleName);
1965
1966             info = namesInfo[moduleName];
1967             collision = info ? info.IsExtensionColliding(extension) : false;
1968
1969             ReplaceSpaces(modulePath, path);
1970             if(modulePath[0]) strcat(modulePath, SEPS);
1971
1972             /*
1973 #if 0
1974             // *** Dependency command ***
1975             if(!strcmpi(extension, "ec"))
1976                sprintf(command, "%s -MT $(OBJ)%s.o -MM $(OBJ)%s.c", "$(CPP)", moduleName, moduleName);
1977             else
1978                sprintf(command, "%s -MT $(OBJ)%s.o -MM %s%s.%s", (!strcmpi(extension, "cc") || !strcmpi(extension, "cxx") || !strcmpi(extension, "cpp")) ? "$(CXX)" : "$(CC)",
1979                   moduleName, modulePath, moduleName, extension);
1980
1981             if(!strcmpi(extension, "ec"))
1982             {
1983                f.Printf("$(OBJ)%s.o: $(OBJ)%s.c\n", moduleName, moduleName);
1984             }
1985             else
1986             {
1987                // System Includes (from global settings)
1988                for(item : compiler.dirs[includes])
1989                {
1990                   strcat(command, " -isystem ");
1991                   if(strchr(item, ' '))
1992                   {
1993                      strcat(command, "\"");
1994                      strcat(command, item);
1995                      strcat(command, "\"");
1996                   }
1997                   else
1998                      strcat(command, item);
1999                }
2000
2001                for(item : config.includeDirs)
2002                {
2003                   strcat(command, " -I");
2004                   if(strchr(item, ' '))
2005                   {
2006                      strcat(command, "\"");
2007                      strcat(command, item);
2008                      strcat(command, "\"");
2009                   }
2010                   else
2011                      strcat(command, item);
2012                }
2013                for(item : config.preprocessorDefs)
2014                {
2015                   strcat(command, " -D");
2016                   strcat(command, item);
2017                }
2018
2019                // Execute it
2020                if((dep = DualPipeOpen(PipeOpenMode { output = true, error = true, input = false }, command)))
2021                {
2022                   char line[1024];
2023                   bool firstLine = true;
2024                   bool result = true;
2025
2026                   // To do some time: auto save external dependencies?
2027
2028                   while(!dep.Eof())
2029                   {
2030                      if(dep.GetLine(line, sizeof(line)-1))
2031                      {
2032                         if(firstLine)
2033                         {
2034                            char * colon = strstr(line, ":");
2035                            if(strstr(line, "No such file") || strstr(line, ",") || (colon && strstr(colon+1, ":")))
2036                            {
2037                               result = false;
2038                               break;
2039                            }
2040                            firstLine = false;
2041                         }
2042                         f.Puts(line);
2043                         f.Puts("\n");
2044                      }
2045                      if(!result) break;
2046                   }
2047                   delete dep;
2048
2049                   // If we failed to generate dependencies...
2050                   if(!result)
2051                   {
2052
2053                   }
2054                }
2055             }
2056 #endif
2057          */
2058             if(!strcmpi(extension, "rc"))
2059             {
2060                ifCount++;
2061                f.Puts("ifdef WINDOWS_TARGET\n\n");
2062             }
2063             else
2064                OpenRulesPlatformExclusionIfs(f, &ifCount, platforms);
2065
2066             if(!strcmpi(extension, "ec"))
2067                f.Printf("$(OBJ)%s$(O): $(OBJ)%s.c\n", moduleName, moduleName);
2068             else
2069                f.Printf("$(OBJ)%s%s%s$(O): %s%s.%s\n",
2070                      moduleName, collision ? "." : "", collision ? extension : "",
2071                      modulePath, moduleName, extension);
2072             if(!strcmpi(extension, "cc") || !strcmpi(extension, "cpp") || !strcmpi(extension, "cxx"))
2073                f.Printf("\t$(CXX)");
2074             else if(!strcmpi(extension, "rc"))
2075                f.Printf("\t$(WINDRES) $(WINDRES_FLAGS) $< \"$(call escspace,$(call quote_path,$@))\"\n");
2076             else
2077                f.Printf("\t$(CC)");
2078
2079             if(strcmpi(extension, "rc") != 0)
2080             {
2081                f.Puts(" $(CFLAGS)");
2082                GenMakePrintNodeFlagsVariable(this, nodeCFlagsMapping, "PRJ_CFLAGS", f);
2083
2084                if(!strcmpi(extension, "ec"))
2085                   f.Printf(" $(FVISIBILITY) -c $(call quote_path,$(OBJ)%s.c) -o $(call quote_path,$@)\n",
2086                         moduleName);
2087                else
2088                   f.Printf(" -c $(call quote_path,%s%s.%s) -o $(call quote_path,$@)\n",
2089                         modulePath, moduleName, !strcmpi(extension, "ec") ? "c" : extension);
2090             }
2091             if(ifCount) f.Puts("endif\n");
2092             f.Puts("\n");
2093          }
2094       }
2095       if(files)
2096       {
2097          bool needed = false;
2098          for(child : files)
2099          {
2100             if(child.type != resources && (child.type == folder || !child.GetIsExcluded(prjConfig)))
2101             {
2102                needed = true;
2103                break;
2104             }
2105          }
2106          if(needed)
2107          {
2108             for(child : files)
2109             {
2110                if(child.type != resources && (child.type == folder || !child.GetIsExcluded(prjConfig)))
2111                   child.GenMakefilePrintObjectRules(f, project, namesInfo, prjConfig, /*excludedPlatforms,*/
2112                         nodeCFlagsMapping, nodeECFlagsMapping);
2113             }
2114          }
2115       }
2116       delete platforms;
2117    }
2118
2119    void GenMakefileAddResources(File f, String resourcesPath, ProjectConfig prjConfig)
2120    {
2121       int count = 0;
2122       if(files)
2123       {
2124          int c;
2125          bool prev = false;
2126          //Iterator<ProjectNode> i { files };
2127          //Iterator<ProjectNode> prev { files };
2128          //for(child : files)
2129          //while(i.Next())
2130          for(c = 0; c < files.count; c++)
2131          {
2132             ProjectNode child = files[c];
2133             TwoStrings ts = child.GetPlatformSpecificFu(prjConfig);
2134             if(count > 0 && ts)
2135                prev = true;
2136             if(child.type == file && !child.GetIsExcluded(prjConfig) && !(count > 0 && ts))
2137             {
2138                bool useRes;
2139                char tempPath[MAX_LOCATION];
2140                char resPath[MAX_LOCATION];
2141
2142                // $(EAR) aw%s --- /*quiet ? "q" : */""
2143                if(count == 0)
2144                   f.Printf("\t%s$(EAR) aw$(EARFLAGS) $(TARGET)", ts.a);
2145
2146                tempPath[0] = '\0';
2147                if(eString_PathInsideOfMore(child.path, resourcesPath, tempPath))
2148                {
2149                   useRes = true;
2150                   PathCatSlash(tempPath, child.name);
2151                }
2152                else
2153                {
2154                   useRes = false;
2155                   strcpy(tempPath, child.path);
2156                   PathCatSlash(tempPath, child.name);
2157                }
2158                EscapeForMake(resPath, tempPath, false, true, false);
2159                f.Printf(" %s%s", useRes ? "$(RES)" : "", resPath);
2160                count++;
2161             }
2162             if(count == 10 || (count > 0 && (ts || !child.next)))
2163             {
2164                char path[MAX_LOCATION] = "", temp[MAX_LOCATION];
2165                ProjectNode parent;
2166
2167                for(parent = this; parent.type == folder; parent = parent.parent)
2168                {
2169                   strcpy(temp, path);
2170                   strcpy(path, parent.name);
2171                   if(temp[0])
2172                   {
2173                      strcat(path, "/");
2174                      strcat(path, temp);
2175                   }
2176                }
2177                f.Printf(" \"%s\"%s\n", path, ts.b);
2178                count = 0;
2179                if(prev)
2180                {
2181                   c--;
2182                   prev = false;
2183                }
2184             }
2185             delete ts;
2186          }
2187          for(child : files)
2188          {
2189             if(child.type == folder)
2190                child.GenMakefileAddResources(f, resourcesPath, prjConfig);
2191          }
2192       }
2193    }
2194
2195    void GenMakeCollectAssignNodeFlags(ProjectConfig prjConfig, bool prjWithEcFiles,
2196          Map<String, int> cflagsVariations, Map<intptr, int> nodeCFlagsMapping,
2197          Map<String, int> ecflagsVariations, Map<intptr, int> nodeECFlagsMapping,
2198          Map<Platform, ProjectOptions> parentByPlatformOptions)
2199    {
2200       Map<Platform, ProjectOptions> byPlatformOptions = parentByPlatformOptions;
2201       if(type == file || type == folder || type == project)
2202       {
2203          bool hasPerNodeOptions = type == project;
2204          if(!hasPerNodeOptions)
2205          {
2206             if(options && !options.isEmpty)
2207                hasPerNodeOptions = true;
2208             else if(configurations)
2209             {
2210                for(c : configurations)
2211                {
2212                   if(c.options && !c.options.isEmpty)
2213                   {
2214                      hasPerNodeOptions = true;
2215                      break;
2216                   }
2217                   if(c.platforms)
2218                   {
2219                      for(p : c.platforms)
2220                      {
2221                         if(p.options && !p.options.isEmpty)
2222                         {
2223                            hasPerNodeOptions = true;
2224                            break;
2225                         }
2226                      }
2227                      if(hasPerNodeOptions)
2228                         break;
2229                   }
2230                }
2231             }
2232             if(!hasPerNodeOptions && platforms)
2233             {
2234                for(p : platforms)
2235                {
2236                   if(p.options && !p.options.isEmpty)
2237                   {
2238                      hasPerNodeOptions = true;
2239                      break;
2240                   }
2241                }
2242             }
2243
2244          }
2245          if(hasPerNodeOptions)
2246          {
2247             bool isEqual = false, isGreater = false;
2248             ComplexComparison complexCmp;
2249             DynamicString s;
2250             Map<Platform, ProjectOptions> additionsByPlatformOptions { };
2251             ProjectOptions platformsCommonOptions;
2252             ProjectOptions byFileConfigPlatformProjectOptions;
2253
2254             DynamicString cflags { };
2255             DynamicString ecflags { };
2256
2257             Platform platform;
2258
2259             byPlatformOptions = { };
2260
2261             for(platform = (Platform)1; platform < Platform::enumSize; platform++)
2262             {
2263                byFileConfigPlatformProjectOptions =
2264                      BlendFileConfigPlatformProjectOptions(this, prjConfig, platform);
2265                byPlatformOptions[platform] = byFileConfigPlatformProjectOptions;
2266             }
2267
2268             CollectPlatformsCommonOptions(byPlatformOptions, &platformsCommonOptions);
2269
2270             byPlatformOptions[unknown] = platformsCommonOptions;
2271
2272             if(parentByPlatformOptions)
2273             {
2274                complexCmp = PlatformsOptionsGreaterEqual(byPlatformOptions,
2275                      parentByPlatformOptions, additionsByPlatformOptions);
2276                isGreater = complexCmp == greater;
2277                isEqual = complexCmp == equal;
2278             }
2279
2280             if(!isEqual)
2281             {
2282                for(platform = (Platform)1; platform < Platform::enumSize; platform++)
2283                {
2284                   byFileConfigPlatformProjectOptions = isGreater ? additionsByPlatformOptions[platform] : byPlatformOptions[platform];
2285                   s = { };
2286                   GenCFlagsFromProjectOptions(byFileConfigPlatformProjectOptions, prjWithEcFiles, false, isGreater, s);
2287                   if(s.count > 1)
2288                      cflags.concatf(" \\\n\t $(if $(%s),%s,)", PlatformToMakefileTargetVariable(platform), (String)s);
2289                   delete s;
2290                   s = { };
2291                   GenECFlagsFromProjectOptions(byFileConfigPlatformProjectOptions, prjWithEcFiles, s);
2292                   if(s.count > 1)
2293                      ecflags.concatf(" \\\n\t $(if $(%s),%s,)", PlatformToMakefileTargetVariable(platform), (String)s);
2294                   delete s;
2295                }
2296
2297                platformsCommonOptions = isGreater ? additionsByPlatformOptions[unknown] : byPlatformOptions[unknown];
2298                s = { };
2299                GenCFlagsFromProjectOptions(platformsCommonOptions, prjWithEcFiles, true, isGreater, s);
2300                if(s.count > 1)
2301                {
2302                   if(!isGreater) cflags.concat(" \\\n\t");
2303                   cflags.concat(s);
2304                }
2305                delete s;
2306                s = { };
2307                GenECFlagsFromProjectOptions(platformsCommonOptions, prjWithEcFiles, s);
2308                if(s.count > 1)
2309                {
2310                   ecflags.concat(" \\\n\t");
2311                   ecflags.concat(s);
2312                }
2313                delete s;
2314
2315                if(isGreater)
2316                {
2317                   cflags.concat(" \\\n\t");
2318                   DynStringPrintNodeFlagsVariable(parent, nodeCFlagsMapping, "PRJ_CFLAGS", cflags);
2319                }
2320             }
2321
2322             additionsByPlatformOptions.Free();
2323             delete additionsByPlatformOptions;
2324
2325             // output
2326             {
2327                if(isEqual)
2328                {
2329                   nodeCFlagsMapping[(intptr)this] = nodeCFlagsMapping[(intptr)parent];
2330                   nodeECFlagsMapping[(intptr)this] = nodeECFlagsMapping[(intptr)parent];
2331                }
2332                else
2333                {
2334                   String s;
2335                   int variationNum;
2336
2337                   variationNum = 1;
2338                   if((s = cflags) && s[0] && !(variationNum = cflagsVariations[s]))
2339                      cflagsVariations[s] = variationNum = cflagsVariations.count;
2340                   nodeCFlagsMapping[(intptr)this] = variationNum;
2341
2342                   variationNum = 1;
2343                   if((s = ecflags) && s[0] && !(variationNum = ecflagsVariations[s]))
2344                      ecflagsVariations[s] = variationNum = ecflagsVariations.count;
2345                   nodeECFlagsMapping[(intptr)this] = variationNum;
2346                }
2347             }
2348
2349             delete cflags;
2350             delete ecflags;
2351          }
2352          else
2353          {
2354             // output
2355             {
2356                nodeCFlagsMapping[(intptr)this] = nodeCFlagsMapping[(intptr)parent];
2357                nodeECFlagsMapping[(intptr)this] = nodeECFlagsMapping[(intptr)parent];
2358             }
2359          }
2360       }
2361       if(files)
2362       {
2363          for(child : files)
2364          {
2365             if(child.type != resources && (child.type == folder || !child.GetIsExcluded(prjConfig)))
2366                child.GenMakeCollectAssignNodeFlags(prjConfig, prjWithEcFiles,
2367                      cflagsVariations, nodeCFlagsMapping, ecflagsVariations, nodeECFlagsMapping,
2368                      byPlatformOptions);
2369          }
2370       }
2371
2372       if(byPlatformOptions != parentByPlatformOptions)
2373       {
2374          byPlatformOptions.Free();
2375          delete byPlatformOptions;
2376       }
2377    }
2378
2379    Array<Platform> GetPlatformsArrayFromExclusionInfo(ProjectConfig prjConfig)
2380    {
2381       Array<Platform> platforms { };
2382       Map<Platform, SetBool> exclusionInfo { };
2383       CollectExclusionInfo(exclusionInfo, prjConfig);
2384
2385       if(exclusionInfo[unknown] == true)
2386       {
2387          if(exclusionInfo.count > 1)
2388             for(p : exclusionInfo; p == false)
2389                platforms.Add(&p);
2390       }
2391       else
2392       {
2393          bool onlyOnknown = true;
2394          for(p : exclusionInfo)
2395             if(&p != unknown && p == true)
2396             {
2397                onlyOnknown = false;
2398                break;
2399             }
2400          if(onlyOnknown)
2401             platforms.Add(unknown);
2402          else
2403          {
2404             Platform p;
2405             for(p = unknown + 1; p < Platform::enumSize; p++)
2406                if(exclusionInfo[p] != true)
2407                   platforms.Add(p);
2408          }
2409       }
2410       delete exclusionInfo;
2411       return platforms;
2412    }
2413
2414    void GetTargets(ProjectConfig prjConfig, Map<String, NameCollisionInfo> namesInfo, char * objDir, const char * objectFileExt, DynamicString output)
2415    {
2416       char moduleName[MAX_FILENAME];
2417       if(type == file)
2418       {
2419          bool headerAltFailed = false;
2420          bool collision;
2421          char extension[MAX_EXTENSION];
2422          NameCollisionInfo info;
2423          Project prj = property::project;
2424          Map<String, const String> headerToSource { [ { "eh", "ec" }, { "h", "c" }, { "hh", "cc" }, { "hpp", "cpp" }, { "hxx", "cxx" } ] };
2425
2426          GetExtension(name, extension);
2427          strcpy(moduleName, name);
2428          StripExtension(moduleName);
2429          info = namesInfo[moduleName];
2430          collision = info ? info.IsExtensionColliding(extension) : false;
2431
2432          for(h2s : headerToSource)
2433          {
2434             if(!strcmpi(extension, &h2s))
2435             {
2436                char filePath[MAX_LOCATION];
2437                GetFullFilePath(filePath);
2438                OutputLog($"No compilation required for header file "); OutputLog(filePath); OutputLog("\n");
2439                ChangeExtension(moduleName, h2s, moduleName);
2440                if(prj.topNode.Find(moduleName, false))
2441                {
2442                   strcpy(extension, h2s);
2443                   collision = info ? info.IsExtensionColliding(extension) : false;
2444                   ChangeExtension(filePath, h2s, filePath);
2445                   OutputLog($"Compiling source file "); OutputLog(filePath); OutputLog($" instead\n");
2446                   StripExtension(moduleName);
2447                }
2448                else
2449                {
2450                   headerAltFailed = true;
2451                   OutputLog($"Unable to locate source file "); OutputLog(moduleName); OutputLog($" to compile instead of "); OutputLog(filePath); OutputLog($"\n");
2452                   StripExtension(moduleName);
2453                }
2454                break;
2455             }
2456          }
2457
2458          if(!headerAltFailed)
2459          {
2460             output.concat(" \"");
2461             output.concat(objDir); //.concat(" $(OBJ)");
2462             output.concat("/");
2463
2464             if(collision)
2465             {
2466                strcat(moduleName, ".");
2467                strcat(moduleName, extension);
2468             }
2469             strcat(moduleName, ".");
2470             strcat(moduleName, objectFileExt);
2471             output.concat(moduleName);
2472             output.concat("\"");
2473          }
2474       }
2475       else if(type == project && ContainsFilesWithExtension("ec", prjConfig))
2476       {
2477          Project prj = property::project;
2478
2479          ReplaceSpaces(moduleName, prj.moduleName);
2480          strcat(moduleName, ".main.ec");
2481          output.concat(" \"");
2482          output.concat(objDir);
2483          output.concat("/");
2484          output.concat(moduleName);
2485          output.concat("\"");
2486
2487          ChangeExtension(moduleName, "c", moduleName);
2488          output.concat(" \"");
2489          output.concat(objDir);
2490          output.concat("/");
2491          output.concat(moduleName);
2492          output.concat("\"");
2493
2494          ChangeExtension(moduleName, "o", moduleName);
2495          output.concat(" \"");
2496          output.concat(objDir);
2497          output.concat("/");
2498          output.concat(moduleName);
2499          output.concat("\"");
2500       }
2501       else if(files)
2502       {
2503          for(child : files)
2504          {
2505             if(child.type != resources && (child.type == folder || !child.GetIsExcluded(prjConfig)))
2506                child.GetTargets(prjConfig, namesInfo, objDir, objectFileExt, output);
2507          }
2508       }
2509    }
2510
2511    void DeleteIntermediateFiles(CompilerConfig compiler, ProjectConfig prjConfig, int bitDepth, Map<String, NameCollisionInfo> namesInfo, bool onlyCObject)
2512    {
2513       if(type == file)
2514       {
2515          bool collision;
2516          const char * objectFileExt = compiler ? compiler.objectFileExt : objectDefaultFileExt;
2517          char extension[MAX_EXTENSION];
2518          char fileName[MAX_FILENAME];
2519          char moduleName[MAX_FILENAME];
2520          NameCollisionInfo info;
2521          Project prj = property::project;
2522          DirExpression objDir = prj.GetObjDir(compiler, prjConfig, bitDepth);
2523
2524          GetExtension(name, extension);
2525          ReplaceSpaces(moduleName, name);
2526          StripExtension(moduleName);
2527          info = namesInfo[moduleName];
2528          collision = info ? info.IsExtensionColliding(extension) : false;
2529
2530          strcpy(fileName, prj.topNode.path);
2531          PathCatSlash(fileName, objDir.dir);
2532          PathCatSlash(fileName, name);
2533
2534          if(!onlyCObject && !strcmp(extension, "ec"))
2535          {
2536             ChangeExtension(fileName, "c", fileName);
2537             if(FileExists(fileName)) DeleteFile(fileName);
2538             ChangeExtension(fileName, "sym", fileName);
2539             if(FileExists(fileName)) DeleteFile(fileName);
2540             ChangeExtension(fileName, "imp", fileName);
2541             if(FileExists(fileName)) DeleteFile(fileName);
2542             ChangeExtension(fileName, "bowl", fileName);
2543             if(FileExists(fileName)) DeleteFile(fileName);
2544             ChangeExtension(fileName, "ec", fileName);
2545          }
2546
2547          if(collision)
2548          {
2549             strcat(fileName, ".");
2550             strcat(fileName, objectFileExt);
2551          }
2552          else
2553             ChangeExtension(fileName, objectFileExt, fileName);
2554          if(FileExists(fileName)) DeleteFile(fileName);
2555
2556          delete objDir;
2557       }
2558       else if(files)
2559       {
2560          for(child : files)
2561          {
2562             if(child.type != resources && (child.type == folder || !child.GetIsExcluded(prjConfig)))
2563                child.DeleteIntermediateFiles(compiler, prjConfig, bitDepth, namesInfo, onlyCObject);
2564          }
2565       }
2566    }
2567
2568    bool IsInNode(ProjectNode node)
2569    {
2570       bool result = false;
2571       ProjectNode n;
2572       for(n = this; n; n = n.parent)
2573       {
2574          if(n == node)
2575          {
2576             result = true;
2577             break;
2578          }
2579       }
2580       return result;
2581    }
2582 }
2583
2584 // the code in this function is closely matched to OptionsBox::Load
2585 // and accompanying derivations of OptionBox and their use of OptionSet,
2586 // OptionCheck, LoadOption and FinalizeLoading methods.
2587 // output changing modification should be mirrored in both implementations
2588 static ProjectOptions BlendFileConfigPlatformProjectOptions(ProjectNode node, ProjectConfig projectConfig, Platform platform)
2589 {
2590    ProjectOptions output { };
2591
2592    // legend: e Element
2593    //         o Option (of a ProjectOptions)
2594    //         n Node (ProjectNode)
2595    //         p Platform
2596    //         u Utility (GenericOptionTools)
2597
2598    int o;
2599    int priority = 0;
2600    int includeDirsOption = OPTION(includeDirs);
2601    ProjectNode n;
2602    const char * platformName = platform ? platform.OnGetString(0,0,0) : null;
2603
2604    // OPTION(ProjectOptions' last member) for size
2605    Array<bool> optionConfigXplatformSet   { size = OPTION(installCommands) };
2606    Array<bool> optionDone                 { size = OPTION(installCommands) };
2607    Array<Array<String>> optionTempStrings { size = OPTION(installCommands) };
2608
2609    GenericOptionTools<SetBool>              utilSetBool {
2610       bool OptionCheck(ProjectOptions options, int option) {
2611          return *(SetBool*)((byte *)options + option) == true;
2612       }
2613       void LoadOption(ProjectOptions options, int option, int priority, Array<Array<String>> optionTempStrings, ProjectOptions output) {
2614          if(options && (*(SetBool*)((byte *)options + option) == true))
2615             *(SetBool*)((byte *)output + option) = true;
2616       }
2617    };
2618    GenericOptionTools<String>               utilString {
2619       void LoadOption(ProjectOptions options, int option, int priority, Array<Array<String>> optionTempStrings, ProjectOptions output) {
2620          String * string = (String*)((byte *)output + option);
2621          if(*string) delete *string;
2622          if(options)
2623             *string = CopyString(*(String*)((byte *)options + option));
2624       }
2625    };
2626    StringArrayOptionTools                   utilStringArrays {
2627       mergeValues = true;
2628       caseSensitive = true;
2629       bool OptionCheck(ProjectOptions options, int option) {
2630          Array<String> strings = *(Array<String>*)((byte *)options + option);
2631          return strings && strings.count;
2632       }
2633       bool OptionSet(ProjectOptions options, int option) {
2634          Array<String> strings = *(Array<String>*)((byte *)options + option);
2635          if(mergeValues && !configReplaces)
2636             return strings && strings.count;
2637          else
2638             return strings != null;
2639       }
2640       void LoadOption(ProjectOptions options, int option, int priority, Array<Array<String>> optionTempStrings, ProjectOptions output) {
2641          if(mergeValues)
2642          {
2643             Array<String> strings = options ? *(Array<String>*)((byte *)options + option) : null;
2644             if(strings)
2645             {
2646                int order = 0;
2647                Array<String> tempStrings = optionTempStrings[option];
2648                if(!tempStrings)
2649                   optionTempStrings[option] = tempStrings = { };
2650                for(s : strings)
2651                {
2652                   bool found = false;
2653                   char priorityMark[10];
2654                   order++;
2655                   if(priority)
2656                      sprintf(priorityMark, "%06d\n", priority * 1000 + order);
2657                   for(i : tempStrings; !(caseSensitive ? strcmp : strcmpi)(i, s)) { found = true; break; }
2658                   if(!found) tempStrings.Add(priority ? PrintString(priorityMark, s) : CopyString(s));
2659                }
2660             }
2661          }
2662          else
2663          {
2664             Array<String> * newStrings = (Array<String>*)((byte *)options + option);
2665             Array<String> * strings = (Array<String>*)((byte *)output + option);
2666             if(*strings) { strings->Free(); delete *strings; }
2667             if(*newStrings && newStrings->count) { *strings = { }; strings->Copy((void*)*newStrings); }
2668          }
2669       }
2670       void FinalizeLoading(int option, Array<Array<String>> optionTempStrings, ProjectOptions output) {
2671          if(mergeValues)
2672          {
2673             Array<String> tempStrings = optionTempStrings[option];
2674             Array<String> * strings = (Array<String>*)((byte *)output + option);
2675             if(*strings) { strings->Free(); delete *strings; }
2676             if(tempStrings && tempStrings.count) { *strings = { }; strings->Copy((void*)tempStrings); }
2677             delete tempStrings;
2678          }
2679       }
2680    };
2681    GenericOptionTools<WarningsOption>       utilWarningsOption {
2682       bool OptionCheck(ProjectOptions options, int option) {
2683          WarningsOption value = *(WarningsOption*)((byte *)options + option);
2684          return value && value != none;
2685       }
2686       void LoadOption(ProjectOptions options, int option, int priority, Array<Array<String>> optionTempStrings, ProjectOptions output) {
2687          WarningsOption value = options ? *(WarningsOption*)((byte *)options + option) : (WarningsOption)0;
2688          *(WarningsOption*)((byte *)output + option) = value;
2689       }
2690    };
2691    GenericOptionTools<OptimizationStrategy> utilOptimizationStrategy {
2692       bool OptionCheck(ProjectOptions options, int option) {
2693          OptimizationStrategy value = *(OptimizationStrategy*)((byte *)options + option);
2694          return value && value != none;
2695       }
2696       void LoadOption(ProjectOptions options, int option, int priority, Array<Array<String>> optionTempStrings, ProjectOptions output) {
2697          OptimizationStrategy value = options ? *(OptimizationStrategy*)((byte *)options + option) : (OptimizationStrategy)0;
2698          *(OptimizationStrategy*)((byte *)output + option) = value;
2699       }
2700    };
2701
2702    Map<int, GenericOptionTools> ot { };
2703
2704    // The following are compiler options
2705
2706    ot[OPTION(debug)] =                   utilSetBool;
2707    ot[OPTION(memoryGuard)] =             utilSetBool;
2708    ot[OPTION(profile)] =                 utilSetBool;
2709    ot[OPTION(noLineNumbers)] =           utilSetBool;
2710    ot[OPTION(strictNameSpaces)] =        utilSetBool;
2711    ot[OPTION(fastMath)] =                utilSetBool;
2712
2713    ot[OPTION(defaultNameSpace)] =        utilString;
2714
2715    ot[OPTION(preprocessorDefinitions)] = utilStringArrays;
2716    ot[OPTION(includeDirs)] =             utilStringArrays;
2717
2718    ot[OPTION(warnings)] =                utilWarningsOption;
2719
2720    ot[OPTION(optimization)] =            utilOptimizationStrategy;
2721
2722    for(n = node; n; n = n.parent)
2723    {
2724       ProjectConfig nodeConfig = null;
2725       if(n.parent)
2726          priority = (priority / 10 + 1) * 10;
2727       else
2728          priority = 9990;
2729       if(projectConfig && n.configurations)
2730       {
2731          for(c : n.configurations; !strcmpi(c.name, projectConfig.name))
2732          {
2733             if(platform && c.platforms)
2734             {
2735                for(p : c.platforms; !strcmpi(p.name, platformName))
2736                {
2737                   for(uu : ot)
2738                   {
2739                      GenericOptionTools u = uu;
2740                      o = &uu;
2741                      if(!optionDone[o] && p.options && (u.mergeValues ? u.OptionCheck(p.options, o) : u.OptionSet(p.options, o)))
2742                      {
2743                         u.LoadOption(p.options, o, o == includeDirsOption ? priority : 0, optionTempStrings, output);
2744                         if(!u.mergeValues) { u.FinalizeLoading(o, optionTempStrings, output); optionDone[o] = true; }
2745                         optionConfigXplatformSet[o] = true;
2746                      }
2747                   }
2748                   break;
2749                }
2750             }
2751             nodeConfig = c;
2752             break;
2753          }
2754       }
2755       for(uu : ot)
2756       {
2757          GenericOptionTools u = uu;
2758          o = &uu;
2759          if(!optionDone[o])
2760          {
2761             if(platform && n.platforms && (!optionConfigXplatformSet[o] || !u.configReplaces))
2762             {
2763                for(p : n.platforms; !strcmpi(p.name, platformName))
2764                {
2765                   if(p.options && (u.mergeValues ? u.OptionCheck(p.options, o) : u.OptionSet(p.options, o)))
2766                   {
2767                      u.LoadOption(p.options, o, o == includeDirsOption ? priority + 1 : 0, optionTempStrings, output);
2768                      if(!u.mergeValues) { u.FinalizeLoading(o, optionTempStrings, output); optionDone[o] = true; }
2769                   }
2770                   break;
2771                }
2772             }
2773             if(!optionDone[o] && nodeConfig && nodeConfig.options &&
2774                   ((u.mergeValues && !u.configReplaces) ?
2775                         u.OptionCheck(nodeConfig.options, o) :
2776                         u.OptionSet(nodeConfig.options, o)))
2777             {
2778                u.LoadOption(nodeConfig.options, o, o == includeDirsOption ? priority : 0, optionTempStrings, output);
2779                if(!u.mergeValues || u.configReplaces) { u.FinalizeLoading(o, optionTempStrings, output); optionDone[o] = true; }
2780             }
2781             if(!optionDone[o])
2782             {
2783                if(n.options && (u.mergeValues ? u.OptionCheck(n.options, o) : u.OptionSet(n.options, o)))
2784                {
2785                   u.LoadOption(n.options, o, o == includeDirsOption ? priority + 1 : 0, optionTempStrings, output);
2786                   if(!u.mergeValues) { u.FinalizeLoading(o, optionTempStrings, output); optionDone[o] = true; }
2787                }
2788                else if(!n.parent)
2789                {
2790                   u.LoadOption(null, o, o == includeDirsOption ? priority + 1 : 0, optionTempStrings, output);
2791                   if(!u.mergeValues) { u.FinalizeLoading(o, optionTempStrings, output); optionDone[o] = true; }
2792                }
2793             }
2794          }
2795       }
2796    }
2797    for(uu : ot)
2798    {
2799       GenericOptionTools u = uu;
2800       o = &uu;
2801       if(!optionDone[o])
2802          u.FinalizeLoading(o, optionTempStrings, output);
2803    }
2804
2805    delete optionConfigXplatformSet;
2806    delete optionDone;
2807    delete optionTempStrings;
2808
2809    delete utilSetBool;
2810    delete utilString;
2811    delete utilStringArrays;
2812    delete utilWarningsOption;
2813    delete utilOptimizationStrategy;
2814
2815    delete ot;
2816
2817    return output;
2818 }
2819
2820 static void CollectPlatformsCommonOptions(Map<Platform, ProjectOptions> byPlatformOptions, ProjectOptions * platformsCommonOptions)
2821 {
2822    ProjectOptions first = null;
2823    ProjectOptions commonOptions;
2824
2825    Map<String, int> countIncludeDirs { };
2826    Map<String, int> countPreprocessorDefinitions { };
2827    Map<String, bool> commonIncludeDirs { };
2828    Map<String, bool> commonPreprocessorDefinitions { };
2829
2830    for(options : byPlatformOptions) { first = options; break; }
2831
2832    *platformsCommonOptions = commonOptions = first ? first.Copy() : { };
2833
2834    if(commonOptions.includeDirs)
2835       commonOptions.includeDirs.Free();
2836    if(commonOptions.preprocessorDefinitions)
2837       commonOptions.preprocessorDefinitions.Free();
2838
2839    for(options : byPlatformOptions)
2840    {
2841       if(options != first)
2842       {
2843          if(commonOptions.debug && options.debug != commonOptions.debug)
2844             commonOptions.debug = unset;
2845          if(commonOptions.memoryGuard && options.memoryGuard != commonOptions.memoryGuard)
2846             commonOptions.memoryGuard = unset;
2847          if(commonOptions.profile && options.profile != commonOptions.profile)
2848             commonOptions.profile = unset;
2849          if(commonOptions.noLineNumbers && options.noLineNumbers != commonOptions.noLineNumbers)
2850             commonOptions.noLineNumbers = unset;
2851          if(commonOptions.strictNameSpaces && options.strictNameSpaces != commonOptions.strictNameSpaces)
2852             commonOptions.strictNameSpaces = unset;
2853          if(commonOptions.fastMath && options.fastMath != commonOptions.fastMath)
2854             commonOptions.fastMath = unset;
2855
2856          if(commonOptions.warnings && options.warnings != commonOptions.warnings)
2857             commonOptions.warnings = unset;
2858          if(commonOptions.optimization && options.optimization != commonOptions.optimization)
2859             commonOptions.optimization = unset;
2860
2861          if(commonOptions.defaultNameSpace && strcmp(options.defaultNameSpace, commonOptions.defaultNameSpace))
2862             delete commonOptions.defaultNameSpace;
2863       }
2864
2865       CountSameNonEmptyOrNullStrings(options.includeDirs, countIncludeDirs);
2866       CountSameNonEmptyOrNullStrings(options.preprocessorDefinitions, countPreprocessorDefinitions);
2867    }
2868
2869    GetPlatformsCommonStrings(countIncludeDirs, byPlatformOptions.count,
2870          commonIncludeDirs, commonOptions.includeDirs);
2871    GetPlatformsCommonStrings(countPreprocessorDefinitions, byPlatformOptions.count,
2872          commonPreprocessorDefinitions, commonOptions.preprocessorDefinitions);
2873
2874    for(options : byPlatformOptions)
2875    {
2876       if(options.debug && options.debug == commonOptions.debug)
2877          options.debug = unset;
2878       if(options.memoryGuard && options.memoryGuard == commonOptions.memoryGuard)
2879          options.memoryGuard = unset;
2880       if(options.profile && options.profile == commonOptions.profile)
2881          options.profile = unset;
2882       if(options.noLineNumbers && options.noLineNumbers == commonOptions.noLineNumbers)
2883          options.noLineNumbers = unset;
2884       if(options.strictNameSpaces && options.strictNameSpaces == commonOptions.strictNameSpaces)
2885          options.strictNameSpaces = unset;
2886       if(options.fastMath && options.fastMath == commonOptions.fastMath)
2887          options.fastMath = unset;
2888
2889       if(options.warnings && options.warnings == commonOptions.warnings)
2890          options.warnings = unset;
2891       if(options.optimization && options.optimization == commonOptions.optimization)
2892          options.optimization = unset;
2893
2894       if(options.defaultNameSpace && !strcmp(options.defaultNameSpace, commonOptions.defaultNameSpace))
2895          delete options.defaultNameSpace;
2896
2897       RemovePlatformsCommonStrings(commonIncludeDirs, options.includeDirs);
2898       RemovePlatformsCommonStrings(commonPreprocessorDefinitions, options.preprocessorDefinitions);
2899    }
2900
2901    delete countIncludeDirs;
2902    delete countPreprocessorDefinitions;
2903    delete commonIncludeDirs;
2904    delete commonPreprocessorDefinitions;
2905 }
2906
2907 static ComplexComparison PlatformsOptionsGreaterEqual(Map<Platform, ProjectOptions> byPlatformOptions,
2908       Map<Platform, ProjectOptions> parentByPlatformOptions,
2909       Map<Platform, ProjectOptions> additionsByPlatformOptions)
2910 {
2911    ComplexComparison result = equal;
2912    ComplexComparison compare;
2913    Platform platform;
2914    for(platform = (Platform)0; platform < Platform::enumSize; platform++)
2915    {
2916       ProjectOptions additionalOptions;
2917       additionsByPlatformOptions[platform] = { };
2918       additionalOptions = additionsByPlatformOptions[platform];
2919       compare = ExtractPlatformsOptionsAdditions(byPlatformOptions[platform], parentByPlatformOptions[platform], additionalOptions);
2920       if(compare == greater && result == equal)
2921          result = greater;
2922       else if(compare == different)
2923       {
2924          result = different;
2925          break;
2926       }
2927    }
2928    return result;
2929 }
2930
2931 static ComplexComparison ExtractPlatformsOptionsAdditions(ProjectOptions options, ProjectOptions parentOptions, ProjectOptions additionalOptions)
2932 {
2933    ComplexComparison result = equal;
2934    if(options.debug != parentOptions.debug ||
2935          options.memoryGuard != parentOptions.memoryGuard ||
2936          options.profile != parentOptions.profile ||
2937          options.noLineNumbers != parentOptions.noLineNumbers ||
2938          options.strictNameSpaces != parentOptions.strictNameSpaces ||
2939          options.fastMath != parentOptions.fastMath ||
2940          options.warnings != parentOptions.warnings ||
2941          options.optimization != parentOptions.optimization ||
2942          (options.defaultNameSpace != parentOptions.defaultNameSpace &&
2943                strcmp(options.defaultNameSpace, parentOptions.defaultNameSpace)))
2944       result = different;
2945    else
2946    {
2947       if(!StringsAreSameOrMore(options.includeDirs, parentOptions.includeDirs, &additionalOptions.includeDirs) ||
2948             !StringsAreSameOrMore(options.preprocessorDefinitions, parentOptions.preprocessorDefinitions, &additionalOptions.preprocessorDefinitions))
2949          result = different;
2950       if((additionalOptions.includeDirs && additionalOptions.includeDirs.count) ||
2951             (additionalOptions.preprocessorDefinitions && additionalOptions.preprocessorDefinitions.count))
2952          result = greater;
2953    }
2954    return result;
2955 }
2956
2957 enum ComplexComparison { different/*, smaller*/, equal, greater };
2958
2959 static bool StringsAreSameOrMore(Array<String> strings, Array<String> originals, Array<String> * additions)
2960 {
2961    bool result = true;
2962    if((!strings || !strings.count) && originals && originals.count)
2963       result = false;
2964    else if(strings && strings.count && (!originals || !originals.count))
2965    {
2966       if(!*additions)
2967          *additions = { };
2968       for(s : strings)
2969          additions->Add(CopyString(s));
2970    }
2971    else if(strings && strings.count && originals && originals.count)
2972    {
2973       Map<String, String> map { };
2974       MapIterator<String, bool> mit { map = map };
2975       for(it : strings)
2976       {
2977          char * s = strstr(it, "\n");
2978          s = s ? s+1 : it;
2979          map[s] = it;
2980       }
2981       for(it : originals)
2982       {
2983          char * s = strstr(it, "\n");
2984          s = s ? s+1 : it;
2985          if(!mit.Index(s, false))
2986          {
2987             result = false;
2988             break;
2989          }
2990          else
2991             map[s] = null;
2992       }
2993       if(result)
2994       {
2995          if(!*additions)
2996             *additions = { };
2997          for(it : map)
2998          {
2999             if(it)
3000                additions->Add(CopyString(it));
3001          }
3002       }
3003       delete map;
3004    }
3005    return result;
3006 }
3007
3008 static void CountSameNonEmptyOrNullStrings(Array<String> strings, Map<String, int> counts)
3009 {
3010    if(strings)
3011    {
3012       for(it : strings)
3013       {
3014          char * s = it;
3015          if(s && s[0])
3016             counts[s]++;
3017       }
3018    }
3019 }
3020
3021 static void GetPlatformsCommonStrings(Map<String, int> counts, int goodCount, Map<String, bool> common, Array<String> strings)
3022 {
3023    for(it : counts)
3024    {
3025       int i = it;
3026       if(i == goodCount)
3027       {
3028          const char * s = &it;
3029          strings.Add(CopyString(s));
3030          common[s] = true;
3031       }
3032    }
3033 }
3034
3035 static void RemovePlatformsCommonStrings(Map<String, bool> common, Array<String> strings)
3036 {
3037    if(strings)
3038    {
3039       Array<String> tmp { };
3040       MapIterator<String, bool> mit { map = common };
3041       for(it : strings)
3042       {
3043          char * s = it;
3044          if(!mit.Index(s, false))
3045             tmp.Add(CopyString(s));
3046       }
3047       strings.Free();
3048       if(tmp.count)
3049       {
3050          for(s : tmp)
3051             strings.Add(CopyString(s));
3052          tmp.Free();
3053       }
3054       delete tmp;
3055    }
3056 }
3057
3058 static void GenMakePrintNodeFlagsVariable(ProjectNode node, Map<intptr, int> nodeFlagsMapping, const String variableName, File f)
3059 {
3060    int customFlags;
3061    customFlags = nodeFlagsMapping[(intptr)node];
3062    if(customFlags > 1)
3063       f.Printf(" $(CUSTOM%d_%s)", customFlags-1, variableName);
3064    else
3065       f.Printf(" $(%s)", variableName);
3066 }
3067
3068 static void DynStringPrintNodeFlagsVariable(ProjectNode node, Map<intptr, int> nodeFlagsMapping, const String variableName, DynamicString s)
3069 {
3070    int customFlags;
3071    customFlags = nodeFlagsMapping[(intptr)node];
3072    if(customFlags > 1)
3073       s.concatf(" $(CUSTOM%d_%s)", customFlags-1, variableName);
3074    else
3075       s.concatf(" $(%s)", variableName);
3076 }
3077
3078 static void GenCFlagsFromProjectOptions(ProjectOptions options, bool prjWithEcFiles, bool commonOptions, bool isGreater, DynamicString s)
3079 {
3080    if(!isGreater)
3081    {
3082       //if(gccCompiler)
3083       {
3084          if(options.optimization == speed || options.optimization == size ||
3085                options.fastMath == true || options.debug == true)
3086          {
3087             if(options.debug != true)
3088             {
3089                s.concat(" $(if $(DEBUG),");
3090                s.concat(" -g");
3091                s.concat(",");
3092             }
3093             switch(options.optimization)
3094             {
3095                case speed: s.concat(" -O2"); break;
3096                case size: s.concat(" -Os"); break;
3097             }
3098             if(options.fastMath == true)
3099                s.concat(" -ffast-math");
3100             if(options.debug == true)
3101                s.concat(" -g");
3102             if(options.debug != true)
3103                s.concat(")");
3104          }
3105          else if(commonOptions)
3106             s.concat(" $(if $(DEBUG),-g)");
3107          if(commonOptions)
3108             s.concat(" $(FPIC)");
3109       }
3110       switch(options.warnings)
3111       {
3112          case all: s.concat(" -Wall"); break;
3113          case none: s.concat(" -w"); break;
3114       }
3115       if(options.profile)
3116          s.concat(" -pg");
3117       if(commonOptions)
3118          s.concat(" -DREPOSITORY_VERSION=\"\\\"$(REPOSITORY_VER)\\\"\"");
3119    }
3120
3121    if(options && options.preprocessorDefinitions)
3122       ListOptionToDynamicString(s, _D, options.preprocessorDefinitions, false, lineEach, "\t\t\t");
3123    if(options && options.includeDirs)
3124       ListOptionToDynamicString(s, _I, options.includeDirs, true, lineEach, "\t\t\t");
3125 }
3126
3127 static void GenECFlagsFromProjectOptions(ProjectOptions options, bool prjWithEcFiles, DynamicString s)
3128 {
3129    if(options.memoryGuard == true)
3130       s.concat(" -memguard");
3131    if(options.noLineNumbers == true)
3132       s.concat(" -nolinenumbers");
3133    if(options.strictNameSpaces == true)
3134       s.concat(" -strictns");
3135    if(options.defaultNameSpace && options.defaultNameSpace[0])
3136       s.concatf(" -defaultns %s", options.defaultNameSpace);
3137 }
3138
3139 static void ListOptionToDynamicString(DynamicString output, ToolchainFlag flag, Array<String> list, bool prioritize,
3140       LineOutputMethod lineMethod, const String newLineStart)
3141 {
3142    if(list.count)
3143    {
3144       if(lineMethod == newLine)
3145       {
3146          output.concat(" \\\n");
3147          output.concat(newLineStart);
3148       }
3149       if(prioritize)
3150       {
3151          Map<String, int> sortedList { };
3152          MapNode<String, int> mn;
3153          for(item : list)
3154             sortedList[item] = 1;
3155          for(mn = sortedList.root.minimum; mn; mn = mn.next)
3156          {
3157             char * start = strstr(mn.key, "\n");
3158             if(lineMethod == lineEach)
3159             {
3160                output.concat(" \\\n");
3161                output.concat(newLineStart);
3162             }
3163             output.concat(" ");
3164             output.concat(flagNames[flag]);
3165             EscapeForMakeToDynString(output, start ? start+1 : mn.key, false, true, flag == _D);
3166          }
3167          delete sortedList;
3168       }
3169       else
3170       {
3171          for(item : list)
3172          {
3173             if(lineMethod == lineEach)
3174             {
3175                output.concat(" \\\n");
3176                output.concat(newLineStart);
3177             }
3178             output.concat(" ");
3179             output.concat(flagNames[flag]);
3180             EscapeForMakeToDynString(output, item, false, true, flag == _D);
3181          }
3182       }
3183    }
3184 }
3185
3186 class GenericOptionTools<class X>
3187 {
3188    bool mergeValues, configReplaces;
3189
3190    virtual bool OptionSet(ProjectOptions options, int option) {
3191       if(*(X*)((byte *)options + option))
3192          return true;
3193       return false;
3194    }
3195
3196    // BUG: OptionCheck = OptionSet; // Overrides derived classes OptionCheck ?
3197
3198    virtual bool OptionCheck(ProjectOptions options, int option) {
3199       return OptionSet(options, option);
3200    }
3201
3202    virtual void LoadOption(ProjectOptions options, int option, int priority, Array<Array<String>> optionTempStrings, ProjectOptions output);
3203    virtual void FinalizeLoading(int option, Array<Array<String>> optionTempStrings, ProjectOptions output);
3204 }
3205
3206 class StringArrayOptionTools : GenericOptionTools<Array<String>>
3207 {
3208    bool caseSensitive;
3209 }
3210
3211 class NameCollisionInfo
3212 {
3213    bool ec;
3214    bool s;
3215    bool c;
3216    bool rc;
3217    bool cpp;
3218    bool cc;
3219    bool cxx;
3220    bool m;
3221    bool mm;
3222    byte count;
3223
3224    bool IsExtensionColliding(char * extension)
3225    {
3226       bool colliding;
3227       if(count > 1 &&
3228             ((!strcmpi(extension, "c")   && ec) ||
3229              (!strcmpi(extension, "rc")  && (ec || c)) ||
3230              (!strcmpi(extension, "s")   && (ec || c || rc)) ||
3231              (!strcmpi(extension, "cpp") && (ec || c || rc || s)) ||
3232              (!strcmpi(extension, "cc")  && (ec || c || rc || s || cpp)) ||
3233              (!strcmpi(extension, "cxx") && (ec || c || rc || s || cpp || cc)) ||
3234              (!strcmpi(extension, "m")   && (ec || c || rc || s || cpp || cc || m)) ||
3235               !strcmpi(extension, "mm")))
3236          colliding = true;
3237       else
3238          colliding = false;
3239      return colliding;
3240    }
3241 }