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
|
#include <cstring>
#include "Ninja.hpp"
#include "../../include/FileUtil.hpp"
#include "../../include/Exec.hpp"
#include "../../include/PkgConfig.hpp"
#include "../../include/GlobalLib.hpp"
using namespace std;
using namespace sibs;
namespace backend
{
string join(const vector<string> &list, const char *joinStr)
{
if(list.empty()) return "";
string result;
long stringSize = 0;
long joinStrLen = strlen(joinStr);
int i = 0;
for(const string &str : list)
{
stringSize += str.size();
if(!str.empty() && i > 0)
stringSize += joinStrLen;
++i;
}
result.reserve(stringSize);
i = 0;
for(const string &str : list)
{
if(i > 0);
result += joinStr;
result += str;
++i;
}
return move(result);
}
Ninja::Ninja(LibraryType _libraryType) :
libraryType(_libraryType)
{
}
void Ninja::addSourceFile(const char *filepath)
{
if(filepath && !containsSourceFile(filepath))
sourceFiles.emplace_back(filepath);
}
const std::vector<std::string>& Ninja::getSourceFiles() const
{
return sourceFiles;
}
bool Ninja::containsSourceFile(const char *filepath) const
{
for(const string &sourceFile : sourceFiles)
{
if(sourceFile == filepath)
return true;
}
return false;
}
Result<bool> validatePkgConfigPackageVersionExists(const Dependency &dependency)
{
Result<bool> dependencyValidationResult = PkgConfig::validatePackageExists(dependency.name);
if(dependencyValidationResult.isErr())
return Result<bool>::Err(dependencyValidationResult.getErrMsg());
Result<bool> dependencyVersionValidationResult = PkgConfig::validatePackageVersionAtLeast(dependency.name, dependency.version);
if(dependencyVersionValidationResult.isErr())
return Result<bool>::Err(dependencyVersionValidationResult.getErrMsg());
return Result<bool>::Ok(true);
}
// TODO: First check if pkg-config is installed. If it's not, only check dependencies that exists in the dependencies sub directory.
// If pkg-config is installed and dependency is not installed, check in dependencies sub directory.
Result<string> Ninja::getLinkerFlags(const vector<Dependency> &dependencies, LinkerFlagCallbackFunc linkerFlagCallbackFunc) const
{
if(dependencies.empty()) return Result<string>::Ok("");
// TODO: Global library dir should be created during sibs installation
string globalLibDir = getHomeDir();
globalLibDir += "/.sibs/lib";
string globalLibLinkerFlags;
vector<string> pkgConfigDependencies;
for(const sibs::Dependency &dependency : dependencies)
{
Result<bool> pkgConfigDependencyValidation = validatePkgConfigPackageVersionExists(dependency);
if(pkgConfigDependencyValidation.isOk())
{
pkgConfigDependencies.push_back(dependency.name);
}
else
{
printf("%s, trying global lib\n", pkgConfigDependencyValidation.getErrMsg().c_str());
Result<string> globalLibLinkerFlagsResult = GlobalLib::getStaticLibsLinkerFlags(globalLibDir, dependency.name, dependency.version, linkerFlagCallbackFunc);
if(globalLibLinkerFlagsResult.isErr())
return globalLibLinkerFlagsResult;
globalLibLinkerFlags += " ";
globalLibLinkerFlags += globalLibLinkerFlagsResult.unwrap();
// TODO: If package doesn't exist, download it from github/server
}
}
Result<string> pkgConfigLinkerFlagsResult = PkgConfig::getDynamicLibsLinkerFlags(pkgConfigDependencies);
if(pkgConfigLinkerFlagsResult.isErr())
return pkgConfigLinkerFlagsResult;
linkerFlagCallbackFunc(pkgConfigLinkerFlagsResult.unwrap());
string allLinkerFlags = pkgConfigLinkerFlagsResult.unwrap();
allLinkerFlags += globalLibLinkerFlags;
return Result<string>::Ok(allLinkerFlags);
}
Result<bool> Ninja::createBuildFile(const std::string &packageName, const vector<Dependency> &dependencies, const char *savePath, LinkerFlagCallbackFunc linkerFlagCallbackFunc)
{
if(sourceFiles.empty())
return Result<bool>::Err("No source files provided");
string ninjaBuildFilePath = savePath;
ninjaBuildFilePath += "/build.ninja";
string result;
result.reserve(16384);
string globalLibDir = getHomeDir();
globalLibDir += "/.sibs/lib";
result += "globalLibDir = '-I";
result += globalLibDir;
result += "'\n\n";
result += "rule cpp_COMPILER\n";
result += " command = ccache c++ $ARGS -c $in -o $out\n\n";
string linkerJob;
switch(libraryType)
{
case LibraryType::EXECUTABLE:
{
result += "rule cpp_EXEC_LINKER\n";
result += " command = ccache c++ $ARGS -o $out $in $LINK_ARGS $aliasing\n\n";
linkerJob = "cpp_EXEC_LINKER";
break;
}
case LibraryType::STATIC:
{
result += "rule cpp_STATIC_LINKER\n";
result += " command = ar rcs lib";
result += packageName;
result += ".a";
result += " $in\n\n";
linkerJob = "cpp_STATIC_LINKER";
break;
}
default:
assert(false);
return Result<bool>::Err("NOT IMPLEMENTED YET!");
}
vector<string> objectNames;
for(const string &sourceFile : sourceFiles)
{
// TODO: Handle tests differently.
// Maybe test directory should have project file and sub directories with project files
// should be their own project?
if(_tinydir_strncmp(sourceFile.c_str(), "tests", 5) == 0)
continue;
//string sourceFileEncoded = sourceFile;
//replace(sourceFileEncoded, '/', '@');
string objectName = packageName + "@exe/" + sourceFile + ".o";
result += "build ";
result += objectName;
result += ": cpp_COMPILER ../../";
result += sourceFile;
result += "\n";
result += " ARGS = $globalLibDir '-I" + packageName + "@exe' '-I.' '-I..' '-fdiagnostics-color=always' '-pipe' '-D_FILE_OFFSET_BITS=64' '-Wall' '-Winvalid-pch' '-Wnon-virtual-dtor' '-O0' '-g'\n\n";
objectNames.emplace_back(objectName);
}
switch(libraryType)
{
case LibraryType::EXECUTABLE:
{
string allLinkerFlags;
Result<string> linkerFlags = getLinkerFlags(dependencies, [&allLinkerFlags](const string &linkerFlag)
{
allLinkerFlags += " ";
allLinkerFlags += linkerFlag;
});
if(linkerFlags.isErr())
return Result<bool>::Err(linkerFlags.getErrMsg());
result += "build ";
result += packageName;
result += ": " + linkerJob + " ";
result += join(objectNames, " ");
result += "\n";
result += " LINK_ARGS = '-Wl,--no-undefined' '-Wl,--as-needed' ";
result += allLinkerFlags;
result += "\n\n";
break;
}
case LibraryType::STATIC:
{
string allLinkerFlags;
Result<string> linkerFlags = getLinkerFlags(dependencies, linkerFlagCallbackFunc);
if(linkerFlags.isErr())
return Result<bool>::Err(linkerFlags.getErrMsg());
result += "build ";
result += packageName;
result += ": " + linkerJob + " ";
result += join(objectNames, " ");
result += "\n\n";
break;
}
default:
assert(false);
return Result<bool>::Err("NOT IMPLEMENTED YET!");
}
bool fileOverwritten = sibs::fileOverwrite(ninjaBuildFilePath.c_str(), sibs::StringView(result.data(), result.size()));
if(!fileOverwritten)
{
string errMsg = "Failed to overwrite ninja build file: ";
errMsg += ninjaBuildFilePath;
return Result<bool>::Err(errMsg);
}
printf("Created ninja build file: %s\n", ninjaBuildFilePath.c_str());
return Result<bool>::Ok(true);
}
Result<bool> Ninja::build(const char *buildFilePath)
{
string command = "ninja -C '";
command += buildFilePath;
command += "'";
Result<ExecResult> execResult = exec(command.c_str(), true);
if(execResult.isOk())
return Result<bool>::Ok(true);
else
return Result<bool>::Err(execResult.getErrMsg());
}
}
|