summaryrefslogtreecommitdiffstats
path: root/Component.cc
blob: 7b8ab81d58c80f4b815a65dc50539359d13c18af (plain) (blame)
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include "Component.h"
#include "FileSystemUtilities.h"
#include "Project.h"
#include "ToolChain.h"

#include <iostream>

/* CONFIGUREDCOMPONENT */

ConfiguredComponent::ConfiguredComponent(const Configurable& UseIn)
	: ToolName(UseIn.configuration().toolChain()), Debug(UseIn.configuration().includeDebugInformation())
{
}

std::vector<std::string>& ConfiguredComponent::includeDirectories()
{
	return Includes;
}

std::vector<std::string>& ConfiguredComponent::linkDirectories()
{
	return Links;
}

std::vector<std::string>& ConfiguredComponent::linkParts()
{
	return Objects;
}

bool ConfiguredComponent::includeDebugInformation() const
{
	return Debug;
}

std::string ConfiguredComponent::toolChain() const
{
	return ToolName;
}

/* CREATECOMPONENTBYNAME */

class BoostLinkComponent : public LinkComponent
{
	public:
		BoostLinkComponent(const std::string& Name);

	protected:
		virtual std::string uniqueString(const Configurable& UseIn);
	private:
		virtual std::string adornedStem(SharedPtr<ToolChain> Tools, const Configurable& UseIn);
		virtual bool matches(SharedPtr<ConfiguredComponent> CC, const Configurable& UseIn);
};

class QtLinkComponent : public LinkComponent
{
	public:
		QtLinkComponent(const std::string& Name, const std::string& Stem);

	protected:
		virtual std::string uniqueString(const Configurable& UseIn);
	private:
		virtual std::string adornedStem(SharedPtr<ToolChain> Tools, const Configurable& UseIn);
		virtual bool matches(SharedPtr<ConfiguredComponent> CC, const Configurable& UseIn);
};


SharedPtr<Component> createNewComponent(const std::string& Name)
{
	if (Name == "boost::lexical_cast")
	{
		HeaderOnlyComponent* Result = new HeaderOnlyComponent("boost_lexical_cast");
		Result->addHeader("boost/lexical_cast.hpp");
		return SharedPtr<Component>(Result);
	}
	else if (Name == "boost::date_time")
	{
		BoostLinkComponent* Result = new BoostLinkComponent("boost_date_time");
		Result->addHeader("boost/date_time.hpp");
		return SharedPtr<Component>(Result);
	}
	else if (Name == "qt::core")
	{
		LinkComponent* Result =  new QtLinkComponent("qtcore","QtCore");
		Result->addHeader("QtCore/QObject");
		return Result;
	}
	else if (Name == "qt::gui")
	{
		LinkComponent* Result =  new QtLinkComponent("qtgui","QtGui");
		Result->addHeader("QtGui/QMessageBox");
		return Result;
	}
	std::cout << "Don't know lib " << Name << std::endl;
	return SharedPtr<Component>();
}

SharedPtr<Component> createComponentFromName(const std::string& Name)
{
	static std::map<std::string, SharedPtr<Component> > All;
	if (All[Name].isNull())
		All[Name] = createNewComponent(Name);
	return All[Name];
}


Component::~Component()
{
}

SharedPtr<ConfiguredComponent> Component::configuredFor(const Configurable& UseIn)
{
	for (unsigned int i=0; i<Instances.size(); ++i)
		if (matches(Instances[i],UseIn))
			return Instances[i];
	for (unsigned int i=0; i<Failures.size(); ++i)
		if (matches(Failures[i],UseIn))
			return SharedPtr<ConfiguredComponent>();
	bool Failed = false;
	SharedPtr<ConfiguredComponent> Result(readFromSystemConfiguration(UseIn));
	if (Result.isNull())
		Result = discoverFor(UseIn);
	if (Result.isNull())
		Failures.push_back( (Failed=true,Result = new ConfiguredComponent(UseIn)) );
	else
		Instances.push_back(Result);
	writeSystemConfiguration(Result, UseIn);
	if (Failed) return SharedPtr<ConfiguredComponent>();
	return Result;
}

/* HEADERONLYCOMPONENT */

HeaderOnlyComponent::HeaderOnlyComponent(const std::string& aName)
: Name(aName)
{
}

void HeaderOnlyComponent::addHeader(const std::string& H)
{
	Headers.push_back(H);
}

bool HeaderOnlyComponent::matches(SharedPtr<ConfiguredComponent>, const Configurable&)
{
	return true;
}

SharedPtr<ConfiguredComponent> HeaderOnlyComponent::discoverFor(const Configurable& UseIn)
{
	std::string IncludeDir;
	for (unsigned int i=0; i<UseIn.configuration().includeSearchPaths(); ++i)
		if (works(UseIn.configuration().includeSearchPath(i)))
		{
			IncludeDir = UseIn.configuration().includeSearchPath(i);
			break;
		}
	if (IncludeDir.empty())
		return SharedPtr<ConfiguredComponent>();
	SharedPtr<ConfiguredComponent> Result(new ConfiguredComponent(UseIn));
	Result->includeDirectories().push_back(IncludeDir);
	return Result;
}

SharedPtr<ConfiguredComponent> HeaderOnlyComponent::readFromSystemConfiguration(const Configurable& UseIn)
{
	SharedPtr<ConfiguredComponent> Result(new ConfiguredComponent(UseIn));
	Result->includeDirectories() = Project::it().systemConfiguration().valueAsStringList(
		Name+uniqueString(UseIn)+".includedir");
	if (!Result->includeDirectories().size())
		return SharedPtr<ConfiguredComponent>();
	return Result;
}

void HeaderOnlyComponent::writeSystemConfiguration(SharedPtr<ConfiguredComponent> CC, const Configurable& UseIn)
{
	Project::it().systemConfiguration().configureComment("Configuration for lib "+Name);
	Project::it().systemConfiguration().configureKey(Name+uniqueString(UseIn)+".includedir",CC->includeDirectories());
}

bool HeaderOnlyComponent::works(const std::string& IncludeDir)
{
	for (unsigned int i=0; i<Headers.size(); ++i)
		if (!fileExists(pathAppend(IncludeDir,Headers[i])))
			return false;
	return true;
}

std::string HeaderOnlyComponent::uniqueString(const Configurable&)
{
	return "";
}

const std::string& HeaderOnlyComponent::name() const
{
	return Name;
}

/* LINKCOMPONENT */

LinkComponent::LinkComponent(const std::string& Name, const std::string& aStem)
: HeaderOnlyComponent(Name), Stem(aStem)
{
}

const std::string& LinkComponent::stem() const
{
	return Stem;
}

bool LinkComponent::matches(SharedPtr<ConfiguredComponent> CC, const Configurable& UseIn)
{
	return UseIn.configuration().toolChain() == CC->toolChain();
}

SharedPtr<ConfiguredComponent> LinkComponent::discoverFor(const Configurable& UseIn)
{
	SharedPtr<ConfiguredComponent> Result = HeaderOnlyComponent::discoverFor(UseIn);
	if (Result.isNull()) return Result;
	SharedPtr<ToolChain> Tools(ToolChain::createByName(UseIn.configuration().toolChain()));
	std::string LibDir;
	for (unsigned int i=0; i<UseIn.configuration().librarySearchPaths(); ++i)
		if (linkWorks(Tools, UseIn, UseIn.configuration().librarySearchPath(i)))
		{
			LibDir = UseIn.configuration().librarySearchPath(i);
			break;
		}
	if (LibDir.empty()) return SharedPtr<ConfiguredComponent>();
	Result->linkDirectories().push_back(LibDir);
	Result->linkParts().push_back(adornedStem(Tools,UseIn));
	return Result;
}

bool LinkComponent::linkWorks(SharedPtr<ToolChain> Tools, const Configurable& UseIn, const std::string& LibDir)
{
	std::string StaticLib(Tools->staticLibraryName(adornedStem(Tools,UseIn)));
	return fileExists(pathAppend(LibDir,StaticLib));
}

SharedPtr<ConfiguredComponent> LinkComponent::readFromSystemConfiguration(const Configurable& UseIn)
{
	SharedPtr<ConfiguredComponent> Result(HeaderOnlyComponent::readFromSystemConfiguration(UseIn));
	if (Result.isNull()) return Result;
	Result->linkDirectories() = Project::it().systemConfiguration().valueAsStringList(
		name()+uniqueString(UseIn)+".librarydir");
	if (!Result->linkDirectories().size()) return SharedPtr<ConfiguredComponent>();
	Result->linkParts() = Project::it().systemConfiguration().valueAsStringList(
		name()+uniqueString(UseIn)+".linkpart");
	if (!Result->linkParts().size()) return SharedPtr<ConfiguredComponent>();
	return Result;
}

void LinkComponent::writeSystemConfiguration(SharedPtr<ConfiguredComponent> CC, const Configurable& UseIn)
{
	Project::it().systemConfiguration().configureKey(name()+uniqueString(UseIn)+".librarydir",CC->linkDirectories());
	Project::it().systemConfiguration().configureKey(name()+uniqueString(UseIn)+".linkpart",CC->linkParts());
}

std::string LinkComponent::adornedStem(SharedPtr<ToolChain>, const Configurable&)
{
	return Stem;
}

std::string LinkComponent::uniqueString(const Configurable& UseIn)
{
	return "."+UseIn.configuration().toolChain();
}


/* BOOSTCOMPONENT */

BoostLinkComponent::BoostLinkComponent(const std::string& Name)
: LinkComponent(Name,Name)
{
}

std::string BoostLinkComponent::adornedStem(SharedPtr<ToolChain> Tools, const Configurable& UseIn)
{
	std::string Adorn, Debug;
	if (UseIn.configuration().includeDebugInformation())
		Debug = "-gd";
	std::string Version("-1_42");	// TODO find out real version
	if (Tools->type() == "msvc2005")
		Adorn = "-vc80-mt"+Debug+Version;
	else if (Tools->type() == "msvc2008")
		Adorn = "-vc90-mt"+Debug+Version;
	else if (Tools->type() == "msvc2010")
		Adorn = "-vc80-mt"+Debug+Version;	// TODO find out the real naming scheme
	return stem()+Adorn;
}

bool BoostLinkComponent::matches(SharedPtr<ConfiguredComponent> CC, const Configurable& UseIn)
{
	return LinkComponent::matches(CC,UseIn) &&
		UseIn.configuration().includeDebugInformation() == CC->includeDebugInformation();
}

std::string BoostLinkComponent::uniqueString(const Configurable& UseIn)
{
	return LinkComponent::uniqueString(UseIn)+
		(UseIn.configuration().includeDebugInformation()?"_debug":"");
}

/* QTLINKCOMPONENT */

QtLinkComponent::QtLinkComponent(const std::string& Name, const std::string& Stem)
: LinkComponent(Name, Stem)
{
}

std::string QtLinkComponent::adornedStem(SharedPtr<ToolChain>, const Configurable& UseIn)
{
	if (UseIn.configuration().includeDebugInformation())
		return stem()+"d4";
	else
		return stem()+"4";
}

bool QtLinkComponent::matches(SharedPtr<ConfiguredComponent> CC, const Configurable& UseIn)
{
	return LinkComponent::matches(CC,UseIn) &&
		UseIn.configuration().includeDebugInformation() == CC->includeDebugInformation();
}

std::string QtLinkComponent::uniqueString(const Configurable& UseIn)
{
	return LinkComponent::uniqueString(UseIn)+
		(UseIn.configuration().includeDebugInformation()?"_debug":"");
}