@@ -463,6 +463,59 @@ void parseFormatStyle() {
463463 DpctGlobalInfo::setCodeFormatStyle (Style);
464464}
465465
466+ void updateCompatibilityVersionInfo (clang::tooling::UnifiedPath OutRoot,
467+ std::string Major, std::string Minor) {
468+ const std::string CmakeHelpFile =
469+ appendPath (OutRoot.getCanonicalPath ().str (), " dpct.cmake" );
470+ std::ifstream InFile (CmakeHelpFile);
471+ if (!InFile) {
472+ std::string ErrMsg = " Failed to open file: " + CmakeHelpFile;
473+ ShowStatus (MigrationErrorReadWriteCMakeHelperFile, std::move (ErrMsg));
474+ dpctExit (MigrationErrorReadWriteCMakeHelperFile);
475+ }
476+
477+ const std::string VersionStr = Major + " ." + Minor;
478+ const int CompatibilityValue = std::stoi (Major) * 10 + std::stoi (Minor);
479+ std::vector<std::string> Lines;
480+ std::string Line;
481+ bool Inserted = false ;
482+
483+ // Constant block of content to insert
484+ const std::vector<std::string> CompatibilityBlock = {
485+ " set(COMPATIBILITY_VERSION " + VersionStr + " )" ,
486+ " set(COMPATIBILITY_VALUE " + std::to_string (CompatibilityValue) + " )" ,
487+ " set(COMPATIBILITY_VERSION_MAJOR " + Major + " )" ,
488+ " set(COMPATIBILITY_VERSION_MINOR " + Minor + " )" ,
489+ " " // Add an empty line for good format
490+ };
491+
492+ auto isCommentOrEmpty = [](const std::string &Line) {
493+ return Line.empty () || Line[0 ] == ' #' ;
494+ };
495+
496+ while (std::getline (InFile, Line)) {
497+ // Insert the compatibility definition block after the first comment section
498+ if (!Inserted && !isCommentOrEmpty (Line)) {
499+ Lines.insert (Lines.end (), CompatibilityBlock.begin (),
500+ CompatibilityBlock.end ());
501+ Inserted = true ;
502+ }
503+ Lines.push_back (Line);
504+ }
505+ InFile.close ();
506+
507+ std::ofstream OutFile (CmakeHelpFile);
508+ if (!OutFile) {
509+ std::string ErrMsg = " Failed to write to file: " + CmakeHelpFile;
510+ ShowStatus (MigrationErrorReadWriteCMakeHelperFile, std::move (ErrMsg));
511+ dpctExit (MigrationErrorReadWriteCMakeHelperFile);
512+ }
513+ for (const auto &Line : Lines) {
514+ OutFile << Line << " \n " ;
515+ }
516+ OutFile.close ();
517+ }
518+
466519static void loadMainSrcFileInfo (clang::tooling::UnifiedPath OutRoot) {
467520 std::string YamlFilePath = appendPath (OutRoot.getCanonicalPath ().str (),
468521 DpctGlobalInfo::getYamlFileName ());
@@ -471,7 +524,16 @@ static void loadMainSrcFileInfo(clang::tooling::UnifiedPath OutRoot) {
471524 if (loadFromYaml (YamlFilePath, *PreTU) != 0 ) {
472525 llvm::errs () << getLoadYamlFailWarning (YamlFilePath);
473526 }
527+
528+ if (MigrateBuildScriptOnly || DpctGlobalInfo::migrateCMakeScripts ()) {
529+ std::string Major = PreTU->SDKVersionMajor ;
530+ std::string Minor = PreTU->SDKVersionMinor ;
531+ if (!Major.empty () && !Minor.empty ()) {
532+ updateCompatibilityVersionInfo (OutRoot, Major, Minor);
533+ }
534+ }
474535 }
536+
475537 for (auto &Entry : PreTU->MainSourceFilesDigest ) {
476538 if (Entry.HasCUDASyntax )
477539 MainSrcFilesHasCudaSyntex.insert (Entry.MainSourceFile );
0 commit comments