summaryrefslogtreecommitdiffstats
path: root/Gcc4Compiler.cc
blob: c587be4ba511af216aa65bee0a2d6c443dcf8243 (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
#include "Gcc4Compiler.h"
#include "CCSource.h"
#include "Component.h"
#include "Executable.h"
#include "FileSystemUtilities.h"
#include "MakeDependencyFile.h"
#include "Project.h"
#include "SystemConfiguration.h"

#include <iostream>
#include <sstream>

class Gcc4Compilation : public ProcessController, public LoudMouth
{
	public:
		Gcc4Compilation(const std::string& CompilerPath, CCSource& aSource);

		virtual void finished(int ret);
		virtual void standardOutput(const std::string& aString);
		virtual void standardError(const std::string& aString);
		virtual void failed();
	private:
		Gcc4Compilation& operator=(const Gcc4Compilation&) { return *this; }
		CCSource& theSource;
};


Gcc4Compilation::Gcc4Compilation(const std::string& CompilerPath, CCSource& aSource)
: theSource(aSource)
{
	std::vector<std::string> Args;
	if (theSource.configuration().includeDebugInformation())
		Args.push_back("-g");
	for (unsigned int i=0; i<theSource.configuration().defines(); ++i)
		Args.push_back("-D"+theSource.configuration().define(i));
	for (unsigned int i=0; i<theSource.configuration().includeDirectories(); ++i)
		Args.push_back("-I"+theSource.configuration().includeDirectory(i));
	Args.push_back("-MMD");
	Args.push_back("-MF");
	Args.push_back(theSource.dependencyName());
	Args.push_back("-c");
	Args.push_back(theSource.sourceName());
	Args.push_back("-o");
	Args.push_back(theSource.objectName());
	createDirectoryForFile(theSource.objectName());
	createDirectoryForFile(theSource.dependencyName());
	launchProcess(this,CompilerPath,Args);
	whisper("g++ ");
	for (unsigned int i=0; i<Args.size(); ++i)
		whisper(Args[i]+" ");
	whisper("\n");
}

void Gcc4Compilation::finished(int ret)
{
	if (ret)
	{
		std::ostringstream str;
		str << "Non zero return :  " << ret << std::endl;
		say(str.str());
	}
	yieldFloor();
	theSource.compilationFinished(ret == 0);
}

void Gcc4Compilation::standardError(const std::string& Out)
{
	say(Out);
}

void Gcc4Compilation::standardOutput(const std::string& Out)
{
	say(Out);
}

void Gcc4Compilation::failed()
{
	std::cout << "failed" << std::endl;
}



/* GCC4LINKAGE */

class Gcc4Linkage : public LineProcessController, public LoudMouth
{
	public:
		Gcc4Linkage(const std::string& LinkerPath, Executable& anExec);

		virtual void finished(int ret);
		virtual void standardOutputByLine(const std::string& aString);
		virtual void standardErrorByLine(const std::string& aString);
		virtual void failed();

	private:
		Gcc4Linkage& operator=(const Gcc4Linkage&) { return *this; }
		Executable& theExecutable;
		bool MayIgnoreNextEmpty;
};


Gcc4Linkage::Gcc4Linkage(const std::string& LinkerPath, Executable& anExec)
: theExecutable(anExec)
{
	std::vector<std::string> Args;
	for (unsigned int i=0; i<theExecutable.configuration().libraryDirectories(); ++i)
		Args.push_back("-L"+theExecutable.configuration().libraryDirectory(i));
	for (unsigned int i=0; i<theExecutable.ccSources(); ++i)
		Args.push_back(theExecutable.getCcSource(i)->objectName());
	for (unsigned int i=0; i<theExecutable.configuration().libraries(); ++i)
		Args.push_back("-l"+theExecutable.configuration().library(i));
	Args.push_back("-o");
	Args.push_back(theExecutable.executableName());
	launchProcess(this,LinkerPath,Args);
	whisper(LinkerPath);
	for (unsigned int i=0; i<Args.size(); ++i)
		whisper(" "+Args[i]);
	whisper("\n");
}

void Gcc4Linkage::finished(int)
{
	yieldFloor();
}

void Gcc4Linkage::standardErrorByLine(const std::string& Out)
{
	sayLn(Out);
}

void Gcc4Linkage::standardOutputByLine(const std::string& Out)
{
	sayLn(Out);
}

void Gcc4Linkage::failed()
{
	std::cout << "failed" << std::endl;
}


/* GCC4TOOLCHAIN */

Gcc4ToolChain::Gcc4ToolChain(const std::string& Name, bool anIsMingW, const std::string& aPath)
: ToolChain(Name), IsMingW(anIsMingW), Path(aPath)
{
}

std::string Gcc4ToolChain::staticLibraryName(const std::string& Stem) const
{
	return "lib"+Stem+".a";
}

bool Gcc4ToolChain::isValid() const
{
	return Path.length() != 0;
}

std::string Gcc4ToolChain::type() const
{
	return IsMingW?"mingw":"gcc4";
}

void Gcc4ToolChain::initResultNames(CCSource& theSource) const
{
	theSource.setObjectName(pathAppend(
			theSource.configuration().buildDir(),
			theSource.name()+".o"));
	theSource.setDependencyName(pathAppend(
			theSource.configuration().buildDir(),
			theSource.name()+".d"));
}

ProcessController* Gcc4ToolChain::createCompilation(CCSource& aSource)
{
	MakeDependencyFile mf(aSource.dependencyName());
	if (!mf.upToDate())
		return new Gcc4Compilation(Path, aSource);
	else
	{
		aSource.compilationFinished(true);
		return 0;
	}
}

void Gcc4ToolChain::initResultNames(Executable& theExec) const
{
	if (IsMingW)
		theExec.setExecutableName(pathAppend(
				theExec.configuration().buildDir(),
				theExec.name()+".exe"));
	else
		theExec.setExecutableName(pathAppend(
				theExec.configuration().buildDir(),	theExec.name()));
}

ProcessController* Gcc4ToolChain::createLinking(Executable& aExec)
{
	for (unsigned int i=0; i<aExec.ccSources(); ++i)
		if (fileIsNewer(aExec.getCcSource(i)->objectName(),aExec.executableName()))
			return new Gcc4Linkage(Path, aExec);
	return 0;
}

void Gcc4ToolChain::storeToCache() const
{
	SystemConfiguration& Cache(Project::it().systemConfiguration());
	std::string Prefix("toolchain."+name()+".");
	Cache.configureComment("GNU Compiler Collection 4");
	Cache.configureKey(isValid(),"toolchain."+name()+".type",IsMingW?"mingw":"gcc4");
	if (Path.length() != 0)
		Project::it().systemConfiguration().configureKey(true,Prefix+"cc_compiler",Path);
	else
		Project::it().systemConfiguration().configureKey(false,Prefix+"cc_compiler",
			"<full gcc4 executable path>");
}

static bool discoverCcFromProcessName(bool LookForMingW, const std::string& ProcessName)
{
	if (!ProcessName.length())
		return false;
	std::string StdOut, StdErr;
	std::vector<std::string> Args;
	Args.push_back("-v");
	int r = syncExecuteProcess(ProcessName,Args,StdOut,StdErr);
	if (r)
		return false;
	bool IsMingW = (StdErr.find("--target=mingw32") != std::string::npos)
		|| (StdErr.find("Target: mingw32") != std::string::npos);
	if (IsMingW != LookForMingW)
		return false;
	return true;
}

SharedPtr<ToolChain> Gcc4ToolChain::discoverMingW(const std::string& Name)
{
	std::string CompilerProcessName(findExecutableInPath("g++.exe", "c:\\MinGW\\bin\\"));
	if (!discoverCcFromProcessName(true, CompilerProcessName))
		CompilerProcessName = ""; 
	return SharedPtr<ToolChain>(new Gcc4ToolChain(Name,true,CompilerProcessName));
}

SharedPtr<ToolChain> Gcc4ToolChain::discoverRegular(const std::string& Name)
{
	std::string CompilerProcessName(findExecutableInPath("g++"));
	if (!discoverCcFromProcessName(false, CompilerProcessName))
		CompilerProcessName = ""; 
	return SharedPtr<ToolChain>(new Gcc4ToolChain(Name,false,CompilerProcessName));
}

SharedPtr<ToolChain> Gcc4ToolChain::createFromCache(const VariablePtr& Item, const std::string& Name)
{
	std::string Type(stringValue(childOrVoid(Item,"type")));
	bool IsMingW;
	if (Type == "mingw")
		IsMingW = true;
	else if (Type == "gcc4")
		IsMingW = false;
	else
		return SharedPtr<ToolChain>();
	std::string ExecutableCompiler(stringValue(childOrVoid(Item,"cc_compiler")));
	if ( ExecutableCompiler.length() != 0)
		return SharedPtr<ToolChain>(new Gcc4ToolChain(Name,IsMingW,ExecutableCompiler));
	return SharedPtr<ToolChain>();
}