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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
#include "../include/CmakeModule.hpp"
#include "../include/Exec.hpp"
#include "../include/GlobalLib.hpp"
#include "../include/PkgConfig.hpp"
#include "../backend/BackendUtils.hpp"
#include <stdlib.h>
#if OS_FAMILY == OS_FAMILY_POSIX
#define nprintf printf
#else
#define nprintf(fmt, ...) wprintf(TINYDIR_STRING(fmt), __VA_ARGS__)
#endif
using namespace std;
using namespace backend;
namespace sibs
{
static FileString cmakePath = TINYDIR_STRING("cmake");
static FileString getIncludeOptionFlag(Compiler compiler, const FileString &filepath)
{
FileString result;
switch (compiler)
{
case Compiler::MINGW_W64:
case Compiler::GCC:
{
result = FileString("'-I");
result += filepath;
result += FileString("'");
break;
}
case Compiler::MSVC:
{
result = FileString("/I \"");
result += filepath;
result += FileString("\"");
break;
}
default:
assert(false);
break;
}
return result;
}
void CmakeModule::setCmakePath(const FileString &path)
{
cmakePath = path;
}
Result<bool> CmakeModule::compile(const SibsConfig &config, const FileString &buildPath, LinkerFlagCallbackFunc staticLinkerFlagCallbackFunc, LinkerFlagCallbackFunc dynamicLinkerFlagCallbackFunc, GlobalIncludeDirCallbackFunc globalIncludeDirCallback)
{
// TODO: Make packaging work with cmake projects
if(config.packaging)
return Result<bool>::Err("Project " + config.getPackageName() + " is a cmake project, such projects are currently not supported when building a package");
Result<FileString> globalLibDirResult = getHomeDir();
if (!globalLibDirResult)
return Result<bool>::Err(globalLibDirResult);
FileString globalLibDir = globalLibDirResult.unwrap();
globalLibDir += TINYDIR_STRING("/.cache/sibs/lib/");
globalLibDir += toFileString(asString(config.platform));
Result<bool> createGlobalLibDirResult = createDirectoryRecursive(globalLibDir.c_str());
if(createGlobalLibDirResult.isErr())
return createGlobalLibDirResult;
// TODO: This code was copied from Ninja.cpp, convert it to work for CMake
#if OS_TYPE == OS_TYPE_LINUX
// TODO: Allow configuring default linking flags. Maybe have `package.useThreads = false` to disable this flag
string allLinkerFlags = "-pthread";
#else
string allLinkerFlags = "";
#endif
if(config.getCompiler() == Compiler::GCC || config.getCompiler() == Compiler::MINGW_W64)
{
switch(config.getSanitize()) {
case Sanitize::NONE:
break;
case Sanitize::ADDRESS:
allLinkerFlags += TINYDIR_STRING(" -lasan");
break;
case Sanitize::UNDEFINED:
allLinkerFlags += TINYDIR_STRING(" -lubsan");
break;
case Sanitize::LEAK:
allLinkerFlags += TINYDIR_STRING(" -llsan");
break;
case Sanitize::THREAD:
allLinkerFlags += TINYDIR_STRING(" -ltsan");
break;
}
}
#if 0
// TODO: Somehow check loading order, because it has to be correct to work.. Or does it for dynamic libraries?
// Anyways it's required for static libraries (especially on Windows)
for (const string &binaryDependency : binaryDependencies)
{
allLinkerFlags += " ";
allLinkerFlags += binaryDependency;
}
#endif
vector<string> linkerFlags;
auto parentProjStaticLinkerFlagCallbackFunc = staticLinkerFlagCallbackFunc;
if (!staticLinkerFlagCallbackFunc || (config.getPackageType() == PackageType::DYNAMIC || config.getPackageType() == PackageType::LIBRARY))
{
staticLinkerFlagCallbackFunc = [&linkerFlags](const string &linkerFlag)
{
linkerFlags.push_back(linkerFlag);
};
}
auto parentProjDynamicLinkerFlagCallbackFunc = dynamicLinkerFlagCallbackFunc;
// TODO: If project contains no source files, then we shouldn't override this function... why?
if(!dynamicLinkerFlagCallbackFunc || config.getPackageType() != PackageType::STATIC)
{
dynamicLinkerFlagCallbackFunc = [&linkerFlags, &parentProjDynamicLinkerFlagCallbackFunc](const string &linkerFlag)
{
if(parentProjDynamicLinkerFlagCallbackFunc)
parentProjDynamicLinkerFlagCallbackFunc(linkerFlag);
linkerFlags.push_back(linkerFlag);
};
}
// TODO: Create a cmake module that contains library/include path for the dependencies (https://cmake.org/Wiki/CMake:How_To_Find_Libraries).
// Modify the project CMakeLists.txt and add: list(APPEND CMAKE_MODULE_PATH "PathToDependenciesCmakeModulesGoesHere").
// CMakeLists.txt may contain: set(CMAKE_MODULE_PATH "PathToModules"). This needs to be replaced with list append,
// otherwise our added module path is replaced.
// It may work to do like vcpkg instead - to use -DCMAKE_TOOLCHAIN_FILE program argument to specify path to script (https://github.com/Microsoft/vcpkg/blob/master/docs/examples/using-sqlite.md)
vector<PackageListDependency> globalLibDependencies;
#if OS_FAMILY == OS_FAMILY_POSIX
vector<PackageListDependency> pkgConfigDependencies;
for(const PackageListDependency &dependency : config.getPackageListDependencies())
{
Result<bool> pkgConfigDependencyValidation = PkgConfig::validatePkgConfigPackageVersionExists(dependency);
if(pkgConfigDependencyValidation.isOk())
{
pkgConfigDependencies.push_back(dependency);
}
else
{
globalLibDependencies.push_back(dependency);
}
}
// TODO: Use getDynamicLibsFlags instead which includes cflags?
Result<string> pkgConfigLinkerFlagsResult = PkgConfig::getDynamicLibsLinkerFlags(pkgConfigDependencies);
if (pkgConfigLinkerFlagsResult.isErr())
{
printf("%s, using global lib...\n", pkgConfigLinkerFlagsResult.getErrMsg().c_str());
globalLibDependencies.reserve(globalLibDependencies.size() + pkgConfigDependencies.size());
for (const PackageListDependency &pkgConfigDependency : pkgConfigDependencies)
{
globalLibDependencies.push_back(pkgConfigDependency);
}
pkgConfigDependencies.clear();
}
else
{
if (!pkgConfigLinkerFlagsResult.unwrap().empty())
dynamicLinkerFlagCallbackFunc(pkgConfigLinkerFlagsResult.unwrap());
}
#else
for (const PackageListDependency &dependency : config.getPackageListDependencies())
{
globalLibDependencies.push_back(dependency);
}
#endif
Result<bool> globalLibResult = GlobalLib::getLibs(globalLibDependencies, config, globalLibDir, staticLinkerFlagCallbackFunc, dynamicLinkerFlagCallbackFunc, globalIncludeDirCallback);
if(!globalLibResult)
return globalLibResult;
for(vector<string>::reverse_iterator it = linkerFlags.rbegin(), end = linkerFlags.rend(); it != end; ++it)
{
allLinkerFlags += " ";
allLinkerFlags += *it;
}
Result<bool> createBuildDirResult = createDirectoryRecursive(buildPath.c_str());
if (createBuildDirResult.isErr())
return createBuildDirResult;
#if 0
#if OS_FAMILY == OS_FAMILY_POSIX
setenv("CFLAGS", "-fPIC", 1);
setenv("CXXFLAGS", "-fPIC", 1);
#else
_putenv("CFLAGS=-fPIC");
_putenv("CXXFLAGS=-fPIC");
#endif
#endif
std::vector<FileString> cmd = { cmakePath };
FileString cflags = TINYDIR_STRING("-fPIC");
FileString cxxflags;
if(config.getCompiler() == Compiler::GCC || config.getCompiler() == Compiler::MINGW_W64)
{
cflags += TINYDIR_STRING(" -fno-omit-frame-pointer");
switch(config.getSanitize()) {
case Sanitize::NONE:
break;
case Sanitize::ADDRESS:
cflags += TINYDIR_STRING(" -fsanitize=address");
break;
case Sanitize::UNDEFINED:
cflags += TINYDIR_STRING(" -fsanitize=undefined");
break;
case Sanitize::LEAK:
cflags += TINYDIR_STRING(" -fsanitize=leak");
break;
case Sanitize::THREAD:
cflags += TINYDIR_STRING(" -fsanitize=thread");
break;
}
}
#if OS_FAMILY == OS_FAMILY_POSIX
cflags += TINYDIR_STRING(" -I/usr/local/include");
#endif
for(const auto &includeDir : config.getIncludeDirs())
{
cflags += TINYDIR_STRING(" ");
if(isPathAbsolute(includeDir))
{
cflags += getIncludeOptionFlag(config.getCompiler(), toFileString(includeDir));
}
else
{
FileString includeDirRelative = FileString("../../../");
includeDirRelative += toFileString(includeDir);
cflags += getIncludeOptionFlag(config.getCompiler(), includeDirRelative);
}
}
cxxflags = cflags;
cmd.push_back(TINYDIR_STRING("-DCMAKE_C_FLAGS=") + cflags);
cmd.push_back(TINYDIR_STRING("-DCMAKE_CXX_FLAGS=") + cxxflags);
switch(config.getPackageType())
{
case PackageType::EXECUTABLE:
{
auto cmake_args = config.getCmakeArgs();
cmd.insert(cmd.end(), cmake_args.begin(), cmake_args.end());
break;
}
case PackageType::STATIC:
{
auto cmake_args = config.getCmakeArgsStatic();
cmd.insert(cmd.end(), cmake_args.begin(), cmake_args.end());
break;
}
case PackageType::DYNAMIC:
case PackageType::LIBRARY:
{
auto cmake_args = config.getCmakeArgsDynamic();
cmd.insert(cmd.end(), cmake_args.begin(), cmake_args.end());
break;
}
}
//cmd.push_back(TINYDIR_STRING("-DCMAKE_SKIP_RPATH=1"));
cmd.push_back(TINYDIR_STRING("-DCMAKE_EXPORT_COMPILE_COMMANDS=ON"));
cmd.push_back(TINYDIR_STRING("-B") + buildPath);
cmd.push_back(TINYDIR_STRING("-H"));
switch(config.getPackageType())
{
case PackageType::EXECUTABLE:
{
cmd.back() += config.getCmakeDir();
break;
}
case PackageType::STATIC:
{
cmd.back() += config.getCmakeDirStatic();
break;
}
case PackageType::DYNAMIC:
case PackageType::LIBRARY:
{
cmd.back() += config.getCmakeDirDynamic();
break;
}
}
//nprintf("Compiling with cmake with arguments: %s\n", cmd.c_str());
Result<ExecResult> execResult = exec(cmd, true);
if(execResult.isOk())
{
if(execResult.unwrap().exitCode != 0)
return Result<bool>::Err(execResult.unwrap().execStdout);
}
else
return Result<bool>::Err(execResult);
//nprintf("Compiling cmake generated ninja file: %s\n", buildPath.c_str());
execResult = exec({ TINYDIR_STRING("ninja"), TINYDIR_STRING("-C"), buildPath }, true);
if(execResult.isOk())
{
if(execResult.unwrap().exitCode != 0)
return Result<bool>::Err(execResult.unwrap().execStdout);
}
else
return Result<bool>::Err(execResult);
if(config.getPackageType() != PackageType::EXECUTABLE)
{
const _tinydir_char_t *dynamicLibExtension = CONFIG_DYNAMIC_LIB_FILE_EXTENSION;
if(config.getCompiler() == Compiler::MINGW_W64)
dynamicLibExtension = TINYDIR_STRING("dll");
string buildPathUtf8 = toUtf8(buildPath);
nprintf("Searching for libraries generate by cmake in build path: %s\n", buildPathUtf8.c_str());
walkDirFilesRecursiveSortTimestamp(buildPath.c_str(),
[&config, &parentProjStaticLinkerFlagCallbackFunc, &parentProjDynamicLinkerFlagCallbackFunc, dynamicLibExtension](const FileWithTimestamp &filedata)
{
bool isStatic = config.getPackageType() == PackageType::STATIC;
if((!isStatic && _tinydir_strcmp(filedata.extension.c_str(), dynamicLibExtension) == 0) || (isStatic && _tinydir_strcmp(filedata.extension.c_str(), CONFIG_STATIC_LIB_FILE_EXTENSION) == 0))
{
string libFileUtf8 = toUtf8(filedata.filepath);
nprintf("Library generated by cmake: %s\n", libFileUtf8.c_str());
switch(config.getPackageType())
{
case PackageType::STATIC:
{
string libFileCmd = "\"";
libFileCmd += libFileUtf8;
libFileCmd += "\"";
if(parentProjStaticLinkerFlagCallbackFunc)
parentProjStaticLinkerFlagCallbackFunc(libFileCmd);
break;
}
case PackageType::DYNAMIC:
case PackageType::LIBRARY:
{
string libFileCmd = "\"";
libFileCmd += libFileUtf8;
libFileCmd += "\"";
if(parentProjDynamicLinkerFlagCallbackFunc)
parentProjDynamicLinkerFlagCallbackFunc(libFileCmd);
break;
}
}
}
return true;
});
// TODO: Clean this up. The below code is indentical to code in Ninja.cpp.......
string projectPathUtf8 = toUtf8(config.getProjectPath());
for (const string &globalIncludeDir : config.getGlobalIncludeDirs())
{
string globalIncludeDirFull = projectPathUtf8;
globalIncludeDirFull += "/";
globalIncludeDirFull += globalIncludeDir;
if(globalIncludeDirCallback)
globalIncludeDirCallback(globalIncludeDirFull);
}
}
return Result<bool>::Ok(true);
}
}
|