1 #include "CppBase.h"
2 #include "Internal.h"
3 
4 namespace Upp {
5 
6 #define LTIMING(x) // DTIMING(x)
7 #define LLOG(x) // DLOG(x)
8 
9 static VectorMap<String, String> sSrcFile;
10 static Index<uint64>             sIncludes;
11 
NormalizeSourcePath(const String & path,const String & currdir)12 String NormalizeSourcePath(const String& path, const String& currdir)
13 {
14 	LTIMING("NormalizeSourcePath");
15 #ifdef PLATFORM_WIN32
16 	return ToLower(NormalizePath(path, currdir));
17 #else
18 	return NormalizePath(path, currdir);
19 #endif
20 }
21 
NormalizeSourcePath(const String & path)22 String NormalizeSourcePath(const String& path)
23 {
24 	return NormalizeSourcePath(path, GetCurrentDirectory());
25 }
26 
ClearSources()27 void ClearSources()
28 {
29 	sSrcFile.Clear();
30 	sIncludes.Clear();
31 }
32 
GetAllSources()33 const Index<String>& GetAllSources()
34 {
35 	return sSrcFile.GetIndex();
36 }
37 
GetAllSourceMasters()38 const VectorMap<String, String>& GetAllSourceMasters()
39 {
40 	return sSrcFile;
41 }
42 
GatherSources(const String & master_path,const String & path_,Vector<int> & parents)43 void GatherSources(const String& master_path, const String& path_, Vector<int>& parents)
44 {
45 	String path = NormalizeSourcePath(path_);
46 	LLOG("--- GatherSources " << master_path << " " << path);
47 	if(sSrcFile.Find(path) >= 0)
48 		return;
49 	int ii = sSrcFile.GetCount();
50 	for(int i = 0; i < parents.GetCount(); i++)
51 		sIncludes.Add(MAKEQWORD(parents[i], ii));
52 	sSrcFile.Add(path, master_path);
53 	parents.Add(ii);
54 	const PPFile& f = GetPPFile(path);
55 	Index<String> todo;
56 	for(String inc : f.includes) {
57 		String p = GetIncludePath(inc, GetFileFolder(path));
58 		if(p.GetCount())
59 			todo.FindAdd(p);
60 	}
61 	MakePP(todo); // parse PP files in parallel to accelerate things...
62 	for(String p : todo)
63 		GatherSources(master_path, p, parents);
64 	parents.Drop();
65 }
66 
GatherSources(const String & path)67 void GatherSources(const String& path)
68 {
69 	LTIMING("GatherSources");
70 	Vector<int> parents;
71 	LLOG("=== GatherSources " << path);
72 	MakePP({ NormalizeSourcePath(path) });
73 	GatherSources(NormalizeSourcePath(path), path, parents);
74 }
75 
GetMasterFile(const String & file)76 String GetMasterFile(const String& file)
77 {
78 	return sSrcFile.Get(file, Null);
79 }
80 
IncludesFile(const String & parent_path,const String & header_path)81 bool   IncludesFile(const String& parent_path, const String& header_path)
82 {
83 	LTIMING("IncludesFile");
84 	int pi = sSrcFile.Find(parent_path);
85 	int i = sSrcFile.Find(header_path);
86 	return pi >= 0 && i >= 0 && sIncludes.Find(MAKEQWORD(pi, i)) >= 0;
87 }
88 
89 }
90