@@ -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+ }
236274void 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
851889int 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}
0 commit comments