-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuild.cpp
More file actions
344 lines (299 loc) · 12.6 KB
/
Copy pathBuild.cpp
File metadata and controls
344 lines (299 loc) · 12.6 KB
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
/* runcpp2
PassScriptPath: true
OverrideCompileFlags:
DefaultPlatform:
msvc:
Append: "/wd4267 /wd4018 /wd4996 /wd4244"
DefaultProfile:
Append: "-Wno-return-local-addr -Wno-sign-compare -Wno-unused-parameter -Wno-switch"
Dependencies:
- Name: ghc_filesystem
Platforms: [DefaultPlatform]
Source:
Local:
Path: "./External/filesystem"
LibraryType: Header
IncludePaths: ["include"]
- Name: System2
Platforms: [DefaultPlatform]
Source:
Local:
Path: "./External/System2"
LibraryType: Header
IncludePaths: ["."]
- Name: DSResult
Platforms: [DefaultPlatform]
Source:
Local:
Path: "./External/DSResult"
LibraryType: Header
IncludePaths: ["Include", "./External/expected/include"]
*/
#include "ghc/filesystem.hpp"
#include "System2.h"
#include "DSResult/DSResult.hpp"
//#include "./Src/runcpp2/StringUtil.hpp"
#include <string>
#include <stdio.h>
#include <stdint.h>
DS::Result<void> RunCommand(const std::string& command, bool noWait = false)
{
System2CommandInfo commandInfo = {};
printf("Running command %s\n", command.c_str());
SYSTEM2_RESULT system2Result = System2Run(command.c_str(), &commandInfo);
DS_ASSERT_EQ(system2Result, SYSTEM2_RESULT_SUCCESS);
if(!noWait)
{
int returnCode = -1;
system2Result = System2GetCommandReturnValue(&commandInfo, 300, &returnCode);
DS_ASSERT_EQ(system2Result, SYSTEM2_RESULT_SUCCESS);
if(returnCode != 0)
{
return DS_ERROR_MSG("Failed to run command " + command + " with return code " +
DS_STR(returnCode));
}
}
System2CleanupCommand(&commandInfo);
return {};
}
DS::Result<std::string> RunAndGetOutput(const std::string& command)
{
System2CommandInfo commandInfo = {};
commandInfo.RedirectOutput = true;
printf("Running command %s\n", command.c_str());
SYSTEM2_RESULT system2Result = System2Run(command.c_str(), &commandInfo);
DS_ASSERT_EQ(system2Result, SYSTEM2_RESULT_SUCCESS);
std::string output = std::string(" ", 1024);
uint32_t outputSize = 0;
int returnCode = -1;
do
{
do
{
uint32_t readBytes = 0;
system2Result = System2ReadFromOutput( &commandInfo,
output.data() + outputSize,
output.size() - outputSize,
&readBytes);
#if defined(_WIN32)
for(int i = outputSize; i < outputSize + readBytes; ++i)
{
if(output[i] == '\r')
output[i] = ' ';
}
#endif
outputSize += readBytes;
if(output.size() / 2 < outputSize)
output.resize(output.size() * 2);
DS_ASSERT_TRUE( system2Result == SYSTEM2_RESULT_SUCCESS ||
system2Result == SYSTEM2_RESULT_READ_NOT_FINISHED);
}
while(system2Result == SYSTEM2_RESULT_READ_NOT_FINISHED);
output.resize(outputSize);
system2Result = System2GetCommandReturnValue(&commandInfo, 5, &returnCode);
}
while(system2Result == SYSTEM2_RESULT_COMMAND_NOT_FINISHED);
System2CleanupCommand(&commandInfo);
DS_ASSERT_EQ(system2Result, SYSTEM2_RESULT_SUCCESS);
if(returnCode != 0)
{
return DS_ERROR_MSG("Failed to run command " + command + " \nWith output: " + output +
"\nWith return code " + DS_STR(returnCode));
}
return output;
}
std::string EscapePath(const std::string& path)
{
std::string ret = path;
#if defined(_WIN32)
for(int i = 0; i < ret.size(); ++i)
{
if(ret[i] == '/')
ret[i] = '\\';
}
#endif
return ret;
}
DS::Result<void> GenerateDefaultYAMLs(const std::string runcpp2Path, const std::string& runcpp2Args)
{
#if defined(_WIN32)
ghc::filesystem::path embed2CPath = ".\\External\\Embed2C\\embed.c";
#else
ghc::filesystem::path embed2CPath = "./External/Embed2C/embed.c";
#endif
ghc::filesystem::path yamlPaths[] =
{
"./DefaultYAMLs/DefaultScriptInfo.yaml",
"./DefaultYAMLs/DefaultUserConfig.yaml",
"./DefaultYAMLs/Default/AnnotatedG++.yaml",
"./DefaultYAMLs/Default/CommonFileTypes.yaml",
"./DefaultYAMLs/Default/clang++.yaml",
"./DefaultYAMLs/Default/g++.yaml",
"./DefaultYAMLs/Default/vs2022_v17+.yaml",
};
std::error_code ec;
ghc::filesystem::file_time_type lastWriteTime = ghc::filesystem::last_write_time( yamlPaths[0],
ec);
for(int i = 0; i < sizeof(yamlPaths) / sizeof(yamlPaths[0]); ++i)
{
if(!ghc::filesystem::exists(yamlPaths[i], ec))
return DS_ERROR_MSG(yamlPaths[i].string() + " does not exist.");
if(ghc::filesystem::last_write_time(yamlPaths[i], ec) > lastWriteTime)
lastWriteTime = ghc::filesystem::last_write_time(yamlPaths[i], ec);
}
if(ghc::filesystem::exists("./Src/runcpp2/DefaultYAMLs.c", ec))
{
if(ghc::filesystem::last_write_time("./Src/runcpp2/DefaultYAMLs.c", ec) > lastWriteTime)
{
printf("No changes found, default YAML regeneration skipped\n");
return {};
}
}
std::string cmd = runcpp2Path + " run --log-level error " + runcpp2Args + embed2CPath.string() +
" ./DefaultYAMLs/DefaultScriptInfo.yaml DefaultScriptInfo " +
" ./DefaultYAMLs/DefaultUserConfig.yaml DefaultUserConfig " +
" ./DefaultYAMLs/Default/AnnotatedG++.yaml AnnotatedG_PlusPlus " +
" ./DefaultYAMLs/Default/CommonFileTypes.yaml CommonFileTypes " +
" ./DefaultYAMLs/Default/clang++.yaml ClangPlusPlus " +
" ./DefaultYAMLs/Default/g++.yaml G_PlusPlus " +
" ./DefaultYAMLs/Default/vs2022_v17+.yaml Vs2022_v17Plus ";
std::string output = RunAndGetOutput(cmd).DS_TRY();
FILE* defaultYamlFile = fopen("./Src/runcpp2/DefaultYAMLs.c", "w");
if(!defaultYamlFile)
return DS_ERROR_MSG("Failed to open ./Src/runcpp2/DefaultYAMLs.c");
DS_ASSERT_EQ(fwrite(output.c_str(), 1, output.size(), defaultYamlFile), output.size());
DS_ASSERT_EQ(fclose(defaultYamlFile), 0);
printf("Default YAMLs generation successful\n");
return {};
}
DS::Result<void> Main(int argc, char** argv)
{
if(argc <= 2 || strcmp(argv[2], "--help") == 0)
{
printf( "runcpp2 run Build.cpp [--runcpp2-path <path>] [--no-werror] [--info] [--rebuild] "
"[--no-test] [--release]\n");
return {};
}
#if defined(_WIN32)
std::string runcpp2Path = "runcpp2.exe";
#else
std::string runcpp2Path = "runcpp2";
#endif
std::string configVersion = "5";
bool warnError = true;
bool info = false;
bool rebuild = false;
bool buildTest = true;
bool release = false;
for(int i = 2; i < argc; ++i)
{
if(strcmp(argv[i], "--runcpp2-path") == 0)
{
if(i + 1 >= argc)
return DS_ERROR_MSG("Runcpp2 path expected");
if(!ghc::filesystem::exists(argv[i + 1]))
return DS_ERROR_MSG(std::string(argv[i + 1]) + " does not exist");
runcpp2Path = argv[i + 1];
++i;
}
else if(strcmp(argv[i], "--no-werror") == 0)
warnError = false;
else if(strcmp(argv[i], "--info") == 0)
info = true;
else if(strcmp(argv[i], "--rebuild") == 0)
rebuild = true;
else if(strcmp(argv[i], "--no-test") == 0)
buildTest = false;
else if(strcmp(argv[i], "--release") == 0)
release = true;
else
break;
}
if(!ghc::filesystem::exists("./External/cfgpath/cfgpath.h"))
return DS_ERROR_MSG("./External/cfgpath/cfgpath.h doesn't exist");
std::error_code ec;
ghc::filesystem::copy_file( "./External/cfgpath/cfgpath.h",
"./Src/cfgpath.h",
ghc::filesystem::copy_options::update_existing,
ec);
std::string runcpp2Args = "-l -c " + EscapePath("./DefaultYAMLs/DefaultUserConfig.yaml ");
GenerateDefaultYAMLs(runcpp2Path, runcpp2Args).DS_TRY();
RunCommand("git tag -d nightly");
DS::Result<std::string> gitTagResult = RunAndGetOutput("git describe --tags --abbrev=0");
std::string gitHash = RunAndGetOutput("git rev-parse --short HEAD").DS_TRY();
if(gitHash.back() == '\n')
gitHash.erase(gitHash.end() - 1);
std::string versionString;
if(gitTagResult.HasValue())
{
std::string& gitTag = gitTagResult.Value();
if(gitTag.back() == '\n')
gitTag.erase(gitTag.end() - 1);
DS::Result<void> tagExact = RunCommand("git describe --tags --exact-match");
if(tagExact.HasValue())
versionString = gitTagResult.Value();
else
versionString = gitTagResult.Value() + "-" + gitHash;
}
else
{
//versionString = "v0.0.0-" + gitHash;
return DS_ERROR_MSG("Failed to get git tag");
}
ghc::filesystem::path rootDir = ghc::filesystem::path(argv[1]).parent_path();
std::string params = "RUNCPP2_VERSION=" + versionString + ";RootPath=\\\"" + rootDir.string() + "\\\"";
if(warnError)
{
//TODO: Replace this with platform/profile map in parameter/variable
#if defined(_WIN32)
params += ";ExtraFlags=/WX";
#elif defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
params += ";ExtraFlags=-Werror";
#else
#error "Unsupported platform..."
#endif
}
if(release)
params += ";Profile.CompileMode=Release";
const std::string commonBuildCommand = runcpp2Path +
" build -o " + EscapePath("./TempBuild ") +
(rebuild ? "--rebuild " : "") +
(info ? "--log-level info " : "") +
runcpp2Args +
"--parameters \"" + params + "\" ";
RunCommand(commonBuildCommand + EscapePath("./Src/runcpp2/runcpp2.cpp")).DS_TRY();
if(buildTest)
{
RunCommand(commonBuildCommand + EscapePath("./Src/Tests/BuildsManagerTest.cpp")).DS_TRY();
RunCommand(commonBuildCommand + EscapePath("./Src/Tests/IncludeManagerTest.cpp")).DS_TRY();
RunCommand(commonBuildCommand + EscapePath("./Src/Tests/ConfigParsingTest.cpp")).DS_TRY();
RunCommand(commonBuildCommand + EscapePath("./Src/Tests/Data/BuildTypeTest.cpp")).DS_TRY();
RunCommand( commonBuildCommand +
EscapePath("./Src/Tests/Data/DependencyInfoTest.cpp")).DS_TRY();
RunCommand( commonBuildCommand +
EscapePath("./Src/Tests/Data/DependencySourceTest.cpp")).DS_TRY();
RunCommand(commonBuildCommand + EscapePath("./Src/Tests/Data/ProfileTest.cpp")).DS_TRY();
RunCommand(commonBuildCommand + EscapePath("./Src/Tests/Data/ScriptInfoTest.cpp")).DS_TRY();
}
ghc::filesystem::path buildDir = ghc::filesystem::exists(runcpp2Path) ?
ghc::filesystem::path(runcpp2Path).parent_path() :
"./Build";
#if defined(_WIN32)
RunCommand( "copy /y " + EscapePath("./Src/Tests/RunAllTests.bat") + " " +
EscapePath(buildDir.string() + "/RunAllTests.bat")).DS_TRY();
RunCommand( "ping 127.0.0.1 -n 2 -w 1000 > NUL && xcopy /h /y /s /e /q .\\TempBuild\\* " +
EscapePath(buildDir.string()) + "> NUL && rmdir .\\TempBuild /s /q && " +
"ECHO Build Done\n", true).DS_TRY();
#else
RunCommand( "ln -srf ./Src/Tests/RunAllTests.sh " +
EscapePath(buildDir.string() + "/RunAllTests.sh")).DS_TRY();
RunCommand( "sleep 1s && cp -rf ./TempBuild/* " + buildDir.string() +
" && rm -Rf ./TempBuild && printf \"Build Done\\n\"", true).DS_TRY();
#endif
return {};
}
int main(int argc, char** argv)
{
Main(argc, argv).DS_TRY_ACT(printf("%s\n", DS_TMP_ERROR.ToString().c_str()); return 1);
return 0;
}