File: FileCache.cc.orig

package info (click to toggle)
fastdep 0.16-13
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 600 kB
  • ctags: 327
  • sloc: cpp: 2,161; ansic: 814; sh: 208; makefile: 123
file content (262 lines) | stat: -rw-r--r-- 6,476 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include "FileCache.h"
#include "FileStructure.h"
#include "CompileState.h"
#include "os.h"
#include "MappedFile.h"

#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>


#include <sys/types.h>
#include <sys/stat.h>

#include <iostream>
#include <fstream>

#include <algorithm>

#ifdef WIN32
extern char *realpath(const char *path, char resolved_path[]);
#define PATH_MAX MAX_PATH
#endif

using namespace std;

bool FileCache::QuietMode = false;


FileCache::FileCache(const std::string& aBaseDir)
: BaseDir(aBaseDir),
	ObjectsDir(""),
	DebugMode(false),
	WarnAboutNonExistingSystemHeaders(false)
{
  CompileCommand = "";
  SetObjectsShouldContainDirectories();
	IncludeDirs.push_back(".");
}

FileCache::FileCache(const FileCache& anOther)
{
}

FileCache::~FileCache()
{
}

FileCache& FileCache::operator=(const FileCache& anOther)
{
	return *this;
}

void FileCache::inDebugMode()
{
	DebugMode = true;
}

void FileCache::addPreDefine(const std::string& aName)
{
	PreDefined.push_back(aName.substr(0, aName.find('=')));
}

void FileCache::generate(std::ostream& theStream, const std::string& aDirectory, const std::string& aFilename)
{
	FileStructure* Structure = update(aDirectory, aFilename, false);
	if (Structure)
	{
		CompileState State(aDirectory);
		if (DebugMode)
			State.inDebugMode();
		for (unsigned int i=0; i<PreDefined.size(); ++i)
			State.define(PreDefined[i],"");
		Structure->getDependencies(&State);
		std::string Basename(aFilename,0,aFilename.rfind("."));
		if (!ObjectsContainDirectories && Basename.rfind(sPathSep)!=Basename.npos)
		{
		    std::string newBasename(Basename,Basename.rfind(sPathSep)+1,Basename.size());
		    theStream << newBasename << ".o: " << aFilename;
		}
		else
		{
			theStream << ObjectsDir << Basename << ".o: " << aFilename;
		}
		theStream << State.getDependenciesLine() << std::endl;
//		State.dump();
		State.mergeDeps(AllDependencies);
		if (std::find(AllDependencies.begin(),AllDependencies.end(),aFilename) == AllDependencies.end())
			AllDependencies.push_back(aFilename);
		if (CompileCommand.size()>0)
		  {
		    theStream << "\t " << CompileCommand << " " << aFilename << std::endl << std::endl ;
		  }
	}
}

void FileCache::SetCompileCommand( const std::string &cmd )
{
  CompileCommand = cmd;
}

void FileCache::addIncludeDir(const std::string& aBaseDir, const std::string& aDir)
{
	if (std::find(IncludeDirs.begin(),IncludeDirs.end(),aDir) == IncludeDirs.end())
		IncludeDirs.push_back(aDir);
}

void FileCache::addExcludeDir(const std::string& aBaseDir, const std::string& aDir)
{
	if (std::find(ExcludeDirs.begin(),ExcludeDirs.end(),aDir) == ExcludeDirs.end())
		ExcludeDirs.push_back(aDir);
}

void FileCache::addIncludeDirFromFile(const std::string& aBaseDir, const std::string& aFilename)
{
    std::ifstream ifile(aFilename.c_str());
    if(!ifile){
        // XXX Error message here
        if(!QuietMode){
            std::cerr << "error opening " << aFilename.c_str() << std::endl;
        }
        // exit or return ?
        return;
    }
    while(!ifile.eof()){
        std::string aDir;
        std::getline(ifile, aDir);
		  aDir = aDir.substr(0,aDir.find('\n'));
		  aDir = aDir.substr(0,aDir.find('\r'));
		  if (aDir != "")
        	addIncludeDir(aBaseDir, aDir);
    }
}


FileStructure* FileCache::update(const std::string& aDirectory, const std::string& aFilename, bool isSystem)
{
	FileMap::iterator iti = Files.find(FileLocation(aDirectory,aFilename));
	if (iti != Files.end())
	{
/*		if(!iti->second && !QuietMode && (!isSystem || WarnAboutNonExistingSystemHeaders))
		{
			std::cerr << "error opening " << aFilename.c_str() << std::endl;
		} */
		return iti->second;
	}
	
	auto_ptr<MappedFile> mfile(new MappedFile);
	
	if (DebugMode)
		std::cout << "[DEBUG] FileCache::update(" << aDirectory << "," << aFilename << ","
			<< isSystem << ");" << std::endl;
	char ResolvedBuffer[PATH_MAX+1];
	{
		unsigned int i;
		for (i=0; i<IncludeDirs.size(); ++i)
		{
			std::string theFilename = IncludeDirs[i]+cPathSep+aFilename;
			if (IncludeDirs[i][0] != cPathSep)
				theFilename = BaseDir+cPathSep+theFilename;
			if (DebugMode)
				std::cout << "[DEBUG] realpath(" << theFilename << ",buf);" << std::endl;
			if (!realpath(theFilename.c_str(), ResolvedBuffer))
				continue;
		
			if(mfile->open(ResolvedBuffer))
					break;
		}

		if (i == IncludeDirs.size())
		{
			for (i=0; i<ExcludeDirs.size(); ++i)
			{
				std::string theFilename = ExcludeDirs[i]+cPathSep+aFilename;
				if (ExcludeDirs[i][0] != cPathSep)
					theFilename = BaseDir+cPathSep+theFilename;
				if (DebugMode)
				{
					std::cout 	<< "[DEBUG] realpath(" 
								<< theFilename << ",buf);" 
								<< std::endl;
				}
				if (!realpath(theFilename.c_str(), ResolvedBuffer))
					continue;
				if(mfile->open(ResolvedBuffer))
				{
					if (DebugMode)
						std::cout 	<< "[DEBUG] excluding : " 
									<< ResolvedBuffer 
									<< std::endl;
					Files[FileLocation(aDirectory, aFilename)] = 0;
					return 0;	
				}
			}
		
			std::string theFilename = aDirectory+cPathSep+aFilename;
			if (aFilename[0]==cPathSep)
			{
				theFilename = aFilename;
			}
			if (!realpath(theFilename.c_str(), ResolvedBuffer))
			{
				if(!QuietMode && (!isSystem || WarnAboutNonExistingSystemHeaders))
				{
					std::cerr 	<< "error opening " 
								<< aFilename.c_str() 
								<< std::endl;
				}
				Files[FileLocation(aDirectory, aFilename)] = 0;
				return 0;
			}
			if(!mfile->open(ResolvedBuffer))
			{
				if(!QuietMode && (!isSystem || WarnAboutNonExistingSystemHeaders))
				{
					std::cerr 	<< "error opening " 
								<< aFilename.c_str() 
								<< std::endl;
				}
				Files[FileLocation(aDirectory, aFilename)] = 0;
				return 0;
			}
		}
	}
	std::string ResolvedName(ResolvedBuffer);

	long file_size		=mfile->size();
	time_t last_change	=mfile->time();

	if (file_size == 0) {
		std::cerr << ResolvedName << " is empty." << std::endl;
		return 0;
	}
	char * map = 0;
	try
	{
		map = mfile->map();
	}
	catch(string s )
	{
		std::cerr << s << std::endl;
		exit(1);
	}

	FileStructure* S = new FileStructure(ResolvedName, this, map, file_size);
	S->setModificationTime(last_change);
	Files.insert(std::make_pair(FileLocation(aDirectory,aFilename), S));

	return S;
}

std::string FileCache::getAllDependenciesLine() const
{
	std::string Result;
	for (unsigned int i=0; i<AllDependencies.size(); ++i)
		Result += " \\\n\t"+AllDependencies[i];
	return Result;
}

// vim:ts=4:nu
//