Skip to content

Commit 70238cc

Browse files
authored
[SYCLomatic] Copy Makefile and CMakeLists.txt to codepin output. (#2214)
Signed-off-by: Chen, Sheng S <[email protected]>
1 parent 630327d commit 70238cc

File tree

14 files changed

+269
-30
lines changed

14 files changed

+269
-30
lines changed

clang/lib/DPCT/DPCT.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@ int runDPCT(int argc, const char **argv) {
558558

559559
InRootPath = InRoot;
560560
OutRootPath = OutRoot;
561+
std::string OutRootPathCUDACodepin = "";
561562
CudaIncludePath = CudaInclude;
562563
SDKPath = SDKPathOpt;
563564
std::transform(
@@ -906,6 +907,11 @@ int runDPCT(int argc, const char **argv) {
906907
ShowStatus(MigrationErrorInvalidInRootOrOutRoot);
907908
dpctExit(MigrationErrorInvalidInRootOrOutRoot, false);
908909
}
910+
if (EnableCodePin) {
911+
OutRootPathCUDACodepin = OutRootPath.getPath().str() + "_codepin_cuda";
912+
OutRootPath = OutRootPath.getPath().str() + "_codepin_sycl";
913+
}
914+
909915
dpct::DpctGlobalInfo::setOutRoot(OutRootPath);
910916
}
911917

@@ -1292,9 +1298,8 @@ int runDPCT(int argc, const char **argv) {
12921298
return MigrationSucceeded;
12931299
}
12941300
}
1295-
12961301
// if run was successful
1297-
int Status = saveNewFiles(Tool, InRootPath, OutRootPath, ReplCUDA, ReplSYCL);
1302+
int Status = saveNewFiles(Tool, InRootPath, OutRootPath, OutRootPathCUDACodepin, ReplCUDA, ReplSYCL);
12981303

12991304
if (DpctGlobalInfo::getBuildScript() == BuildScriptKind::BS_Cmake) {
13001305
loadMainSrcFileInfo(OutRootPath);

clang/lib/DPCT/SaveNewFiles.cpp

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,44 @@ static bool checkOverwriteAndWarn(StringRef OutFilePath, StringRef InFilePath) {
233233
return Overwrites;
234234
}
235235

236+
// Search all the files through Pattern, and copy the files to the OutRoot
237+
// directory.
238+
void copyFileToOutRoot(clang::tooling::UnifiedPath &InRoot,
239+
clang::tooling::UnifiedPath &OutRoot,
240+
const std::string &Pattern) {
241+
std::error_code EC;
242+
for (fs::recursive_directory_iterator Iter(Twine(InRoot.getPath()), EC), End;
243+
Iter != End; Iter.increment(EC)) {
244+
if ((bool)EC) {
245+
std::string ErrMsg = "[ERROR] Access : " + std::string(InRoot.getPath()) +
246+
" fail: " + EC.message() + "\n";
247+
PrintMsg(ErrMsg);
248+
continue;
249+
}
250+
if (Iter->type() == fs::file_type::regular_file) {
251+
std::string FileName = llvm::sys::path::filename(Iter->path()).str();
252+
std::transform(FileName.begin(), FileName.end(), FileName.begin(),
253+
::toUpper);
254+
if (FileName.find(Pattern) != std::string::npos) {
255+
tooling::UnifiedPath FilePath = Iter->path();
256+
std::string CanFilePath = FilePath.getCanonicalPath().str();
257+
258+
if (CanFilePath.find("_codepin_") != std::string::npos) {
259+
continue;
260+
}
261+
rewriteDir(CanFilePath, InRoot, OutRoot);
262+
createDirectories(path::parent_path(CanFilePath));
263+
if (llvm::sys::fs::exists(CanFilePath)) {
264+
std::string ErrMsg = "[Warning] The " + CanFilePath +
265+
" is exists. Skip copy the file.\n";
266+
PrintMsg(ErrMsg);
267+
continue;
268+
}
269+
fs::copy_file(Iter->path(), CanFilePath);
270+
}
271+
}
272+
}
273+
}
236274
void processallOptionAction(clang::tooling::UnifiedPath &InRoot,
237275
clang::tooling::UnifiedPath &OutRoot,
238276
bool IsForSYCL) {
@@ -850,8 +888,9 @@ void genCodePinHeader(dpct::RawFDOStream &RS, bool IsForCUDADebug) {
850888
/// Prerequisite: InRoot and OutRoot are both absolute paths
851889
int saveNewFiles(clang::tooling::RefactoringTool &Tool,
852890
clang::tooling::UnifiedPath InRoot,
853-
clang::tooling::UnifiedPath OutRoot, ReplTy &ReplCUDA,
854-
ReplTy &ReplSYCL) {
891+
clang::tooling::UnifiedPath OutRoot,
892+
clang::tooling::UnifiedPath CUDAMigratedOutRoot,
893+
ReplTy &ReplCUDA, ReplTy &ReplSYCL) {
855894
using namespace clang;
856895
volatile ProcessStatus status = MigrationSucceeded;
857896
// Set up Rewriter.
@@ -872,15 +911,8 @@ int saveNewFiles(clang::tooling::RefactoringTool &Tool,
872911
MainSrcFileMap;
873912

874913
std::string SrcFile = "MainSrcFiles_placehold";
875-
std::string OutRootStr = OutRoot.getCanonicalPath().str();
876-
std::string CodePinCUDAFolder = OutRootStr + "_codepin_cuda";
877-
if (DpctGlobalInfo::isCodePinEnabled()) {
878-
OutRootStr = OutRootStr + "_codepin_sycl";
879-
}
880-
clang::tooling::UnifiedPath SYCLMigratedOutRoot(OutRootStr);
881-
clang::tooling::UnifiedPath CUDAMigratedOutRoot(CodePinCUDAFolder);
882-
std::string YamlFile =
883-
appendPath(OutRootStr, DpctGlobalInfo::getYamlFileName());
914+
std::string YamlFile = appendPath(OutRoot.getCanonicalPath().str(),
915+
DpctGlobalInfo::getYamlFileName());
884916
if (clang::dpct::DpctGlobalInfo::isIncMigration()) {
885917
auto PreTU = clang::dpct::DpctGlobalInfo::getMainSourceYamlTUR();
886918
for (const auto &Repl : PreTU->Replacements) {
@@ -923,13 +955,15 @@ int saveNewFiles(clang::tooling::RefactoringTool &Tool,
923955
auto GroupResult = groupReplacementsByFile(
924956
Rewrite.getSourceMgr().getFileManager(), ReplSYCL);
925957
if (auto RewriteStatus = writeReplacementsToFiles(
926-
ReplSYCL, Rewrite, OutRootStr, InRoot, MainSrcFilesDigest,
927-
MainSrcFileMap, MainSrcFilesRepls, FileRangesMap,
928-
FileBlockLevelFormatRangesMap, clang::dpct::RT_ForSYCLMigration))
958+
ReplSYCL, Rewrite, OutRoot.getCanonicalPath().str(), InRoot,
959+
MainSrcFilesDigest, MainSrcFileMap, MainSrcFilesRepls,
960+
FileRangesMap, FileBlockLevelFormatRangesMap,
961+
clang::dpct::RT_ForSYCLMigration))
929962
return RewriteStatus;
930963
if (DpctGlobalInfo::isCodePinEnabled()) {
931964
if (auto RewriteStatus = writeReplacementsToFiles(
932-
ReplCUDA, DebugCUDARewrite, CodePinCUDAFolder, InRoot,
965+
ReplCUDA, DebugCUDARewrite,
966+
CUDAMigratedOutRoot.getCanonicalPath().str(), InRoot,
933967
MainSrcFilesDigest, MainSrcFileMap, MainSrcFilesRepls,
934968
FileRangesMap, FileBlockLevelFormatRangesMap,
935969
clang::dpct::RT_CUDAWithCodePin))
@@ -1048,12 +1082,12 @@ int saveNewFiles(clang::tooling::RefactoringTool &Tool,
10481082
}
10491083
FilePath = TempFilePath;
10501084

1051-
if (!rewriteCanonicalDir(FilePath, InRoot, SYCLMigratedOutRoot)) {
1085+
if (!rewriteCanonicalDir(FilePath, InRoot, OutRoot)) {
10521086
continue;
10531087
}
10541088

10551089
if (dpct::DpctGlobalInfo::isCodePinEnabled() &&
1056-
!rewriteCanonicalDir(DebugFilePath, InRoot, CodePinCUDAFolder)) {
1090+
!rewriteCanonicalDir(DebugFilePath, InRoot, CUDAMigratedOutRoot)) {
10571091
continue;
10581092
}
10591093

@@ -1116,14 +1150,19 @@ int saveNewFiles(clang::tooling::RefactoringTool &Tool,
11161150
std::string ScriptFineName = "Makefile.dpct";
11171151
if (!BuildScriptFile.empty())
11181152
ScriptFineName = BuildScriptFile;
1119-
if (GenBuildScript)
1120-
genBuildScript(Tool, InRoot, SYCLMigratedOutRoot, ScriptFineName);
1121-
1153+
if (GenBuildScript) {
1154+
genBuildScript(Tool, InRoot, OutRoot, ScriptFineName);
1155+
}
11221156
saveUpdatedMigrationDataIntoYAML(MainSrcFilesRepls, MainSrcFilesDigest,
11231157
YamlFile, SrcFile, MainSrcFileMap);
11241158
if (dpct::DpctGlobalInfo::isCodePinEnabled()) {
1125-
std::string SchemaPathCUDA = CodePinCUDAFolder + "/codepin_autogen_util.hpp";
1126-
std::string SchemaPathSYCL = OutRootStr + "/codepin_autogen_util.hpp";
1159+
copyFileToOutRoot(InRoot, CUDAMigratedOutRoot, "MAKEFILE");
1160+
copyFileToOutRoot(InRoot, CUDAMigratedOutRoot, "CMAKELISTS.TXT");
1161+
copyFileToOutRoot(InRoot, CUDAMigratedOutRoot, ".CMAKE");
1162+
std::string SchemaPathCUDA = CUDAMigratedOutRoot.getCanonicalPath().str() +
1163+
"/codepin_autogen_util.hpp";
1164+
std::string SchemaPathSYCL =
1165+
OutRoot.getCanonicalPath().str() + "/codepin_autogen_util.hpp";
11271166
std::error_code EC;
11281167
createDirectories(path::parent_path(SchemaPathCUDA));
11291168
createDirectories(path::parent_path(SchemaPathSYCL));
@@ -1136,7 +1175,7 @@ int saveNewFiles(clang::tooling::RefactoringTool &Tool,
11361175

11371176
processallOptionAction(InRoot, CUDAMigratedOutRoot, false);
11381177
}
1139-
processallOptionAction(InRoot, SYCLMigratedOutRoot, true);
1178+
processallOptionAction(InRoot, OutRoot, true);
11401179

11411180
return status;
11421181
}

clang/lib/DPCT/SaveNewFiles.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ class StringRef;
3232
/// Prerequisite: InRoot and OutRoot are both absolute paths
3333
int saveNewFiles(clang::tooling::RefactoringTool &Tool,
3434
clang::tooling::UnifiedPath InRoot,
35-
clang::tooling::UnifiedPath OutRoot, ReplTy &ReplCUDA,
36-
ReplTy &ReplSYCL);
35+
clang::tooling::UnifiedPath OutRoot,
36+
clang::tooling::UnifiedPath CUDAMigratedOutRoot,
37+
ReplTy &ReplCUDA, ReplTy &ReplSYCL);
3738

3839
void loadYAMLIntoFileInfo(clang::tooling::UnifiedPath Path);
3940

clang/lib/DPCT/ValidateArguments.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,11 @@ enum class HelperFuncPreference : unsigned int { NoQueueDevice = 0 };
9898
enum class SYCLFileExtensionEnum { DP_CPP, SYCL_CPP, CPP };
9999

100100
bool makeInRootCanonicalOrSetDefaults(
101-
clang::tooling::UnifiedPath &InRoot, const std::vector<std::string> SourceFiles);
102-
bool makeAnalysisScopeCanonicalOrSetDefaults(clang::tooling::UnifiedPath &AnalysisScope,
103-
const clang::tooling::UnifiedPath &InRoot);
101+
clang::tooling::UnifiedPath &InRoot,
102+
const std::vector<std::string> SourceFiles);
103+
bool makeAnalysisScopeCanonicalOrSetDefaults(
104+
clang::tooling::UnifiedPath &AnalysisScope,
105+
const clang::tooling::UnifiedPath &InRoot);
104106
bool getDefaultOutRoot(clang::tooling::UnifiedPath &OutRootPar,
105107
bool NeedCheckOutRootEmpty = true);
106108
/// Make sure files passed to tool are under the
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.0)
2+
3+
include(${CMAKE_CURRENT_SOURCE_DIR}/TestCMAKE.cmake)
4+
MY_PRINT_FUNCTION("Hello cmake")
5+
project(foo)
6+
7+
add_executable(foo foo.cpp)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
macro(MY_PRINT_FUNCTION message)
2+
message(STATUS "My print: ${message}")
3+
endmacro()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.0)
2+
3+
include(${CMAKE_CURRENT_SOURCE_DIR}/TestCMAKE.cmake)
4+
MY_PRINT_FUNCTION("Hello cmake")
5+
project(foo)
6+
7+
add_executable(foo foo.cpp)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
macro(MY_PRINT_FUNCTION message)
2+
message(STATUS "My print: ${message}")
3+
endmacro()
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// UNSUPPORTED: system-windows
2+
// RUN: dpct --in-root %S -out-root %T/foo --cuda-include-path="%cuda-path/include" %s --enable-codepin -- -std=c++14 -x cuda --cuda-host-only || true
3+
4+
// RUN: cd %T/foo_codepin_cuda
5+
// RUN: ls > default2.log
6+
// RUN: ls test >> default2.log
7+
// RUN: FileCheck --input-file default2.log --match-full-lines %T/foo_codepin_cuda/foo.cpp -check-prefix=DEFAULT
8+
// DEFAULT: CMakeLists.txt
9+
// DEFAULT: TestCMAKE.cmake
10+
// DEFAULT: test.cmake
11+
12+
13+
// RUN: echo "begin" > %T/diff.txt
14+
// RUN: diff --strip-trailing-cr %S/cmake_expected %T/foo_codepin_cuda/CMakeLists.txt >> %T/diff.txt
15+
// RUN: diff --strip-trailing-cr %S/expected.cmake %T/foo_codepin_cuda/TestCMAKE.cmake >> %T/diff.txt
16+
// RUN: echo "end" >> %T/diff.txt
17+
// RUN: FileCheck --input-file %T/diff.txt --check-prefix=CHECK %s
18+
// CHECK: begin
19+
// CHECK-NEXT:end
20+
21+
#include <cuda.h>
22+
23+
int main() {
24+
return 0;
25+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
macro(MY_PRINT_FUNCTION message)
2+
message(STATUS "My print: ${message}")
3+
endmacro()

0 commit comments

Comments
 (0)