forked from oneapi-src/SYCLomatic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMigrateCmakeScript.cpp
More file actions
634 lines (531 loc) · 20.3 KB
/
Copy pathMigrateCmakeScript.cpp
File metadata and controls
634 lines (531 loc) · 20.3 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
//===--------------- MigrateCmakeScript.cpp--------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "MigrateCmakeScript.h"
#include "Diagnostics/Diagnostics.h"
#include "ErrorHandle/Error.h"
#include "FileGenerator/GenFiles.h"
#include "MigrationReport/Statics.h"
#include "UserDefinedRules/PatternRewriter.h"
#include "Utility.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_os_ostream.h"
#include <algorithm>
#include <cstring>
#include <fstream>
#include <numeric>
#include <string>
using namespace clang::dpct;
using namespace llvm::cl;
namespace path = llvm::sys::path;
namespace fs = llvm::sys::fs;
namespace clang {
namespace dpct {
std::map<std::string /*CMake command*/,
std::tuple<bool /*ProcessedOrNot*/, bool /*CUDASpecificOrNot*/>>
cmake_commands{
#define ENTRY_TYPE(TYPENAME, VALUE1, COMMENT, VALUE2) \
{#TYPENAME, {VALUE1, VALUE2}},
#include "CMakeCommands.inc"
#undef ENTRY_TYPE
};
static std::map<clang::tooling::UnifiedPath /*file path*/,
std::string /*content*/>
CmakeScriptFileBufferMap;
static std::map<clang::tooling::UnifiedPath /*file name*/, bool /*is crlf*/>
ScriptFileCRLFMap;
static std::map<std::string /*variable name*/, std::string /*value*/>
CmakeVarMap;
static std::map<std::string /*cmake syntax*/,
MetaRuleObject::PatternRewriter /*cmake migration rule*/>
CmakeBuildInRules;
static std::map<std::string /*file path*/,
std::vector<std::string> /*warning msg*/>
FileWarningsMap;
void cmakeSyntaxProcessed(std::string &Input);
static size_t skipWhiteSpaces(const std::string Input, size_t Index) {
size_t Size = Input.size();
for (; Index < Size && isWhitespace(Input[Index]); Index++) {
}
return Index;
}
static size_t gotoEndOfCmakeWord(const std::string Input, size_t Index,
char Delim) {
size_t Size = Input.size();
for (; Index < Size && !isWhitespace(Input[Index]) && Input[Index] != Delim;
Index++) {
}
return Index;
}
static size_t gotoEndOfCmakeCommand(const std::string Input, size_t Index) {
size_t Size = Input.size();
for (; Index < Size && !isWhitespace(Input[Index]) && Input[Index] != '(';
Index++) {
}
if (Index < Size && Input[Index] == '(') {
return Index;
}
return std::string::npos;
}
static size_t gotoEndOfCmakeCommandStmt(const std::string Input, size_t Index) {
size_t Size = Input.size();
for (; Index < Size && Input[Index] != ')'; Index++) {
}
return Index;
}
std::string getVarName(const std::string &Variable) {
std::string Value;
if (Variable[0] == '$' && Variable[1] == '{') {
auto Name = Variable.substr(2, Variable.size() - 3);
auto Iter = CmakeVarMap.find(Name);
if (Iter != CmakeVarMap.end()) {
Value = Iter->second;
}
} else {
Value = Variable;
}
return Value;
}
static void parseVariable(const std::string &Input) {
const int Size = Input.size();
int Index = 0;
while (Index < Size) {
// Skip comments
if (Input[Index] == '#') {
for (; Index < Size && Input[Index] != '\n'; Index++) {
}
}
int Begin, End;
// Go the begin of cmake command
Index = skipWhiteSpaces(Input, Index);
Begin = Index;
// Go the end of cmake command
Index = gotoEndOfCmakeWord(Input, Begin + 1, '(');
End = Index;
// Skip possible space
Index = skipWhiteSpaces(Input, Index);
if (Index < Size && Input[Index] == '(') {
std::string Command = Input.substr(Begin, End - Begin);
std::transform(Command.begin(), Command.end(), Command.begin(),
[](unsigned char c) { return std::tolower(c); });
if (Command == "set") {
Index++; // Skip '('
std::string VarName;
// Get the begin of first argument of set
Index = skipWhiteSpaces(Input, Index);
Begin = Index;
// Get the end of the first argument of set
Index = gotoEndOfCmakeWord(Input, Index, ')');
End = Index;
// Get the name of the first argument
VarName = Input.substr(Begin, End - Begin);
// Get the begin of the second argument
for (Index = End + 1; Index < Size && isWhitespace(Input[Index]);
Index++) {
}
Begin = Index;
// Get the end of the second argument
Index = gotoEndOfCmakeWord(Input, Begin + 1, ')');
End = Index;
// Only check the set commands which has two arguments.
// And skip cmake reserves identifiers that begins with "CMAKE_" and
// "_CMAKE_".
if (Input[Index] == ')' &&
!llvm::StringRef(VarName).starts_with("CMAKE_") &&
!llvm::StringRef(VarName).starts_with("_CMAKE_ ")) {
// Get the name of the second argument
CmakeVarMap[VarName] = Input.substr(Begin, End - Begin);
}
}
// Go the ')' of cmake command
Index = gotoEndOfCmakeCommandStmt(Input, Index);
}
Index++;
}
}
static std::string processArgOfCmakeVersionRequired(
const std::string &Arg,
const std::map<std::string, std::string> &CmakeVarMap) {
const std::string CmakeMinVerion =
"3.24"; // The minimal version used in SYCL compiler
size_t Pos = Arg.find("...");
std::string ReplArg;
if (Pos != std::string::npos) {
const auto StrArray = split(Arg, "...");
std::string MinVer = getVarName(StrArray[0]);
std::string MaxVer = getVarName(StrArray[1]);
if (std::atof(MinVer.c_str()) >= std::atof(CmakeMinVerion.c_str())) {
ReplArg = MinVer + "..." + MaxVer;
} else if (std::atof(MinVer.c_str()) < std::atof(CmakeMinVerion.c_str()) &&
std::atof(MaxVer.c_str()) > std::atof(CmakeMinVerion.c_str())) {
ReplArg = CmakeMinVerion + "..." + MaxVer;
} else {
ReplArg = std::move(CmakeMinVerion);
}
} else {
std::string Ver = getVarName(Arg);
if (std::atof(Ver.c_str()) < std::atof(CmakeMinVerion.c_str())) {
ReplArg = std::move(CmakeMinVerion);
} else {
ReplArg = std::move(Ver);
}
}
return ReplArg;
}
static bool skipCmakeComments(const std::string &Input, size_t &Index) {
const size_t Size = Input.size();
bool CommentFound = false;
if (Input[Index] == '#') {
CommentFound = true;
for (; Index < Size && Input[Index] != '\n'; Index++) {
}
Index++;
}
return CommentFound;
}
void processCmakeMinimumRequired(std::string &Input, size_t &Size,
size_t &Index) {
std::string VarName;
std::string Value;
size_t Begin, End;
// Get the begin of first argument of cmake_minimum_required
Index = skipWhiteSpaces(Input, Index);
Begin = Index;
// Get the end of the first argument of cmake_minimum_required
Index = gotoEndOfCmakeWord(Input, Index, ')');
End = Index;
// Get the name of the first argument
VarName = Input.substr(Begin, End - Begin);
// Get the begin of the second argument
Index = skipWhiteSpaces(Input, End + 1);
Begin = Index;
// Get the end of the second argument
Index = gotoEndOfCmakeWord(Input, Begin + 1, ')');
End = Index;
// Get the name of the second argument
Value = Input.substr(Begin, End - Begin);
std::string ReplStr = processArgOfCmakeVersionRequired(Value, CmakeVarMap);
Input.replace(Begin, End - Begin, ReplStr);
Size = Input.size(); // Update string size
Index = Begin + ReplStr.size(); // Update index
}
void processExecuteProcess(std::string &Input, size_t &Size, size_t &Index) {
std::string VarName;
std::string Value;
size_t Begin, End;
// Get the begin of first argument
Index = skipWhiteSpaces(Input, Index);
Begin = Index;
Index = gotoEndOfCmakeCommandStmt(Input, Index);
End = Index;
// Get the value of the second argument
Value = Input.substr(Begin, End - Begin);
size_t Pos = Value.find("-Xcompiler");
if (Pos != std::string::npos) {
size_t NextPos = Pos + strlen("-Xcompiler");
NextPos = skipWhiteSpaces(Value, NextPos);
// clang-format off
// To check if the value of opition "-Xcompiler" is a string literal, if it
// is a string literal, just remove '"' for outmost '"' and '\\' for inner string, like:
// -Xcompiler "-dumpfullversion" -> -Xcompiler -dumpfullversion
// -Xcompiler "\"-dumpfullversion \"" -> -Xcompiler -dumpfullversion
// clang-format on
if (Value[NextPos] == '"') {
Value[NextPos] = ' ';
size_t Size = Value.size();
size_t Idx = NextPos;
for (; Idx < Size; Idx++) {
if (Value[Idx] == '\\') {
Value[Idx] = ' ';
} else if (Value[Idx] == '"') {
Value[Idx] = ' ';
}
}
}
}
Input.replace(Begin, End - Begin, Value);
Size = Input.size(); // Update string size
Index = Begin + Value.size(); // Update index
}
void processCudaCompile(std::string &Input, size_t &Size, size_t &Index) {
size_t Begin, End;
// Get the beginning of the first argument
Index = skipWhiteSpaces(Input, Index);
Begin = Index;
// Get the end of the Cmake Stmt
Index = gotoEndOfCmakeCommandStmt(Input, Index);
End = Index;
// Extract the content of the command
std::string Content = Input.substr(Begin, End - Begin);
// Locate "OPTIONS" in the command
size_t OptionsPos = Content.find("OPTIONS");
if (OptionsPos != std::string::npos) {
// Locate Options Begin and End
size_t Pos = Content.find("OPTIONS", OptionsPos) + strlen("OPTIONS");
size_t OptionsBegin = skipWhiteSpaces(Content, Pos);
size_t OptionsEnd = gotoEndOfCmakeCommandStmt(Content, Pos);
// Split by Whitespaces and Filter
std::string Options =
Content.substr(OptionsBegin, OptionsEnd - OptionsBegin);
std::istringstream Stream(Options);
std::string Token;
std::vector<std::string> FilteredOptions;
// Find the Options starting with "-D"
while (Stream >> Token) {
if (Token.rfind("-D", 0) == 0) { // Keep only options that start with "-D"
FilteredOptions.push_back(Token);
}
}
// Create a new replacement string with the filtered options
std::string FilteredOptionsString;
if (!FilteredOptions.empty()) {
FilteredOptionsString =
"OPTIONS " + std::accumulate(std::next(FilteredOptions.begin()),
FilteredOptions.end(),
FilteredOptions[0],
[](std::string a, std::string b) {
return a + " " + b;
});
} else {
// If there are no options, remove the OPTIONS keyword
OptionsPos--;
}
Content.replace(OptionsPos, OptionsEnd - OptionsPos, FilteredOptionsString);
Input.replace(Begin, End - Begin, Content);
Size = Input.size(); // Update the size of the input
Index = Begin + Content.size(); // Update the index
}
}
// Implicit migration rule is used when the migration logic is difficult to be
// described with yaml based rule syntax. Currently only migration of
// cmake_minimum_required() is implemented by implicit migration rule.
void applyImplicitMigrationRule(std::string &Input,
const std::string &BuildScriptSyntax,
void (*Func)(std::string &, size_t &,
size_t &)) {
size_t Size = Input.size();
size_t Index = 0;
while (Index < Size) {
Index = skipWhiteSpaces(Input, Index);
// Skip comments
if (skipCmakeComments(Input, Index)) {
continue;
}
size_t Begin, End;
// Go the begin of cmake command
Index = skipWhiteSpaces(Input, Index);
Begin = Index;
// Go the end of cmake command
Index = gotoEndOfCmakeWord(Input, Begin + 1, '(');
End = Index;
// Skip possible space
Index = skipWhiteSpaces(Input, Index);
if (Index < Size && Input[Index] == '(') {
std::string Command = Input.substr(Begin, End - Begin);
// Process implict cmake syntax
if (Command == BuildScriptSyntax) {
(*Func)(Input, Size, Index);
}
// Go the ')' of cmake command
Index = gotoEndOfCmakeCommandStmt(Input, Index);
}
Index++;
}
}
static std::string convertCmakeCommandsToLower(const std::string &InputString,
const std::string FileName) {
std::stringstream OutputStream;
const auto Lines = split(InputString, '\n');
std::vector<std::string> Output;
unsigned int Count = 1;
for (auto Line : Lines) {
size_t Size = Line.size();
size_t Index = 0;
// Go the begin of cmake command
Index = skipWhiteSpaces(Line, Index);
int Begin = Index;
// Go the end of cmake command
Index = gotoEndOfCmakeCommand(Line, Begin + 1);
int End = Index;
if (Index < Size && (Line[Index] == '(' || isWhitespace(Line[Index]))) {
std::string Str = Line.substr(Begin, End - Begin);
std::transform(Str.begin(), Str.end(), Str.begin(),
[](unsigned char Char) { return std::tolower(Char); });
auto Iter = cmake_commands.find(Str);
if (Iter != cmake_commands.end()) {
for (int Idx = Begin; Idx < End; Idx++) {
Line[Idx] = Str[Idx - Begin];
}
if (!std::get<0>(Iter->second) && !std::get<1>(Iter->second)) {
std::string WarningMsg =
FileName + ":" + std::to_string(Count) + ":warning:";
WarningMsg += DiagnosticsUtils::getMsgText(
CMakeScriptMigrationMsgs::CMAKE_CONFIG_FILE_WARNING, Str);
WarningMsg += "\n";
FileWarningsMap[FileName].push_back(std::move(WarningMsg));
OutputStream
<< "# "
<< DiagnosticsUtils::getMsgText(
CMakeScriptMigrationMsgs::CMAKE_CONFIG_FILE_WARNING, Str)
<< "\n";
}
if (!std::get<0>(Iter->second) && std::get<1>(Iter->second)) {
std::string WarningMsg =
FileName + ":" + std::to_string(Count) + ":warning:";
WarningMsg += DiagnosticsUtils::getMsgText(
CMakeScriptMigrationMsgs::CMAKE_NOT_SUPPORT_WARNING, Str);
WarningMsg += "\n";
FileWarningsMap[FileName].push_back(std::move(WarningMsg));
OutputStream
<< "# "
<< DiagnosticsUtils::getMsgText(
CMakeScriptMigrationMsgs::CMAKE_NOT_SUPPORT_WARNING, Str)
<< "\n";
}
}
}
OutputStream << Line << "\n";
Count++;
}
return OutputStream.str();
}
void addCmakeWarningMsg(const std::string &WarningMsg,
const std::string FileName) {
FileWarningsMap[FileName].push_back(WarningMsg);
}
static void doCmakeScriptAnalysis() {
for (auto &Entry : CmakeScriptFileBufferMap) {
auto &Buffer = Entry.second;
// Collect varible name and its value
parseVariable(Buffer);
}
}
static void convertAllCmakeCommandsToLowerCase() {
for (auto &Entry : CmakeScriptFileBufferMap) {
auto &Buffer = Entry.second;
// Convert cmake command to lower case in cmake script files
Buffer = convertCmakeCommandsToLower(Buffer, Entry.first.getPath().str());
}
}
static void
applyCmakeMigrationRules(const clang::tooling::UnifiedPath InRoot,
const clang::tooling::UnifiedPath OutRoot) {
static const std::map<std::string,
void (*)(std::string &, size_t &, size_t &)>
DispatchTable = {
{"cmake_minimum_required", processCmakeMinimumRequired},
{"execute_process", processExecuteProcess},
{"cuda_compile", processCudaCompile},
{"cuda_compile_fatbin", processCudaCompile},
};
setFileTypeProcessed(SourceFileType::SFT_CMakeScript);
for (auto &Entry : CmakeScriptFileBufferMap) {
llvm::outs() << "Processing: " + Entry.first.getPath() + "\n";
auto &Buffer = Entry.second;
clang::tooling::UnifiedPath FileName = Entry.first.getPath();
// Apply user define migration rules
for (const auto &CmakeSyntaxEntry : CmakeBuildInRules) {
const auto &PR = CmakeSyntaxEntry.second;
if (PR.In.empty() && PR.Out.empty()) {
// Implicit migration rule is used when the migration logic is difficult
// to be described with yaml based rule syntax. Currently only migration
// of cmake_minimum_required() is implemented by implicit migration
// rule.
applyImplicitMigrationRule(Buffer, PR.BuildScriptSyntax,
DispatchTable.at(PR.BuildScriptSyntax));
} else {
if (PR.RuleId == "rule_project") {
auto NewPR = PR;
SmallString<512> RelativePath(FileName.getCanonicalPath());
llvm::sys::path::replace_path_prefix(RelativePath,
OutRoot.getCanonicalPath(), ".");
#ifdef _WIN32
std::vector<std::string> SplitedStr =
split(RelativePath.c_str(), '\\');
#else
std::vector<std::string> SplitedStr =
split(RelativePath.c_str(), '/');
#endif
std::string RelativePathPrefix = "";
auto Size = SplitedStr.size();
for (size_t Idx = 0; Size > 2 && Idx < Size - 2; Idx++) {
RelativePathPrefix += "../";
}
RelativePathPrefix += "dpct.cmake";
NewPR.Out += "include(" + RelativePathPrefix + ")\n";
Buffer = applyPatternRewriter(
NewPR, Buffer, Entry.first.getPath().str(), "", OutRoot);
} else {
Buffer = applyPatternRewriter(PR, Buffer, Entry.first.getPath().str(),
"", OutRoot);
}
}
}
auto Iter = FileWarningsMap.find(FileName.getPath().str());
if (Iter != FileWarningsMap.end()) {
std::vector WarningsVec = Iter->second;
for (auto &Warning : WarningsVec) {
llvm::outs() << Warning;
}
}
}
}
// cmake systaxes need to be processed by implicit migration rules, as they are
// difficult to be described with yaml based rule syntax.
static const std::vector<std::string> BuiltinMigrationRules = {
"cmake_minimum_required", "execute_process", "cuda_compile",
"cuda_compile_fatbin"};
static void applyBuiltinMigrationRules() {
for (const auto &Rule : BuiltinMigrationRules) {
MetaRuleObject::PatternRewriter BuiltinPR;
BuiltinPR.BuildScriptSyntax = Rule;
BuiltinPR.RuleId = "builtin_rule_" + Rule;
auto &PR = CmakeBuildInRules[Rule];
if (PR.Priority != RulePriority::Takeover) {
PR = BuiltinPR;
} else {
llvm::outs() << "[Warning]: Two migration rules (Rule:"
<< BuiltinPR.RuleId << ", Rule:" << PR.RuleId
<< ") are duplicated, the migration rule (Rule:"
<< BuiltinPR.RuleId << ") is ignored.\n";
}
}
}
void doCmakeScriptMigration(const clang::tooling::UnifiedPath &InRoot,
const clang::tooling::UnifiedPath &OutRoot) {
loadBufferFromFile(InRoot, OutRoot, CmakeScriptFileBufferMap);
unifyInputFileFormat(CmakeScriptFileBufferMap, ScriptFileCRLFMap);
convertAllCmakeCommandsToLowerCase();
applyBuiltinMigrationRules();
doCmakeScriptAnalysis();
applyCmakeMigrationRules(InRoot, OutRoot);
storeBufferToFile(CmakeScriptFileBufferMap, ScriptFileCRLFMap);
}
void registerCmakeMigrationRule(MetaRuleObject &R) {
if (!validateOutFieldAndWarn(R))
return;
auto PR = MetaRuleObject::PatternRewriter(R.In, R.Out.value(), R.Subrules,
R.MatchMode, R.Warning, R.RuleId,
R.BuildScriptSyntax, R.Priority);
auto Iter = CmakeBuildInRules.find(PR.BuildScriptSyntax);
if (Iter != CmakeBuildInRules.end()) {
if (PR.Priority == RulePriority::Takeover &&
Iter->second.Priority > PR.Priority) {
CmakeBuildInRules[PR.BuildScriptSyntax] = PR;
} else {
llvm::outs() << "[Warning]: Two migration rules (Rule:" << R.RuleId
<< ", Rule:" << Iter->second.RuleId
<< ") are duplicated, the migration rule (Rule:" << R.RuleId
<< ") is ignored.\n";
}
} else {
CmakeBuildInRules[PR.BuildScriptSyntax] = PR;
}
}
} // namespace dpct
} // namespace clang