https://github.com/zhouronghua updated https://github.com/llvm/llvm-project/pull/119513
>From 3dc7d8f9a7bd7f27ae8aa49e373c5febb0813d30 Mon Sep 17 00:00:00 2001 From: "ronghua.zhou" <ronghua.z...@enflame-tech.com> Date: Fri, 14 Feb 2025 01:04:51 +0000 Subject: [PATCH] [Feature]: support for the BC library file into the compile dependencies --- clang/lib/Driver/ToolChains/Clang.cpp | 19 +++- clang/lib/Frontend/CompilerInstance.cpp | 10 +- clang/lib/Frontend/DependencyFile.cpp | 119 ++++++++++++++++++++++-- 3 files changed, 137 insertions(+), 11 deletions(-) diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index 55ec3db0ee994..e27c1fe95af42 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -1056,8 +1056,23 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA, DepFile = getDependencyFileName(Args, Inputs); C.addFailureResultFile(DepFile, &JA); } - CmdArgs.push_back("-dependency-file"); - CmdArgs.push_back(DepFile); + // for host compile, we changed the dep file name to *.d.CUID.host + // so it will not overide kernel dep file, + // and merge it with *.d (kernel dep) file in DependencyFile.cpp + // for example, abc.d -> abc.d.2282B80C.host + auto AT = getToolChain().getAuxTriple(); + if (!AT && std::string(DepFile) != "-") { + SmallString<128> NewDepFile(DepFile); + NewDepFile.append( + "." + llvm::utohexstr(llvm::sys::Process::GetRandomNumber()) + + ".host"); + CmdArgs.push_back("-dependency-file"); + CmdArgs.push_back(Args.MakeArgString(NewDepFile)); + // else keep the original dep file name + } else { + CmdArgs.push_back("-dependency-file"); + CmdArgs.push_back(DepFile); + } } bool HasTarget = false; diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp index c11c857ea0606..60ac343391e18 100644 --- a/clang/lib/Frontend/CompilerInstance.cpp +++ b/clang/lib/Frontend/CompilerInstance.cpp @@ -494,8 +494,14 @@ void CompilerInstance::createPreprocessor(TranslationUnitKind TUKind) { // Handle generating dependencies, if requested. const DependencyOutputOptions &DepOpts = getDependencyOutputOpts(); - if (!DepOpts.OutputFile.empty()) - addDependencyCollector(std::make_shared<DependencyFileGenerator>(DepOpts)); + if (!DepOpts.OutputFile.empty()) { + auto DFG = std::make_shared<DependencyFileGenerator>(DepOpts); + for (auto F : getCodeGenOpts().LinkBitcodeFiles) { + DFG->maybeAddDependency(F.Filename, false, false, false, false); + } + addDependencyCollector(DFG); + } + if (!DepOpts.DOTOutputFile.empty()) AttachDependencyGraphGen(*PP, DepOpts.DOTOutputFile, getHeaderSearchOpts().Sysroot); diff --git a/clang/lib/Frontend/DependencyFile.cpp b/clang/lib/Frontend/DependencyFile.cpp index 15fa7de35df97..e62e5dc8e0223 100644 --- a/clang/lib/Frontend/DependencyFile.cpp +++ b/clang/lib/Frontend/DependencyFile.cpp @@ -10,6 +10,9 @@ // //===----------------------------------------------------------------------===// +#include <set> +#include <string> + #include "clang/Basic/FileManager.h" #include "clang/Basic/SourceManager.h" #include "clang/Frontend/DependencyOutputOptions.h" @@ -343,20 +346,122 @@ static void PrintFilename(raw_ostream &OS, StringRef Filename, } } +static std::vector<std::string> SplitToLines(llvm::StringRef &Dep) { + std::vector<std::string> Deps; + + for (const auto &line : llvm::split(KernelDepContent, '\n')) + // Remove empty lines and comment lines + if (!line.empty() && line[0] != '#') + Deps.insert(line); + + return Deps; +} + +static std::string GetKernelDepFileName(std::string &HostDepFileName) { + + // merge host dependency file (*.d.CUID.host) + // to kernel dependency file (*.d) for tops target + // for example, abc.d -> abc.d.2282B80C.host + const int CUIDLEN = 9; + llvm::StringRef SubStr = ".host"; + SmallString<128> OutputFileS(HostDepFileName); + size_t Pos = OutputFileS.find(SubStr); + // for tops target, trim .CUID.host in dep file name + if (Pos != llvm::StringRef::npos) + // abc.d.2282B80C.host -> abc.d + return OutputFileS.substr(0, Pos - CUIDLEN); + else + return ""; +} + +static void TryMergeDependencyFile(std::vector<std::string> &KD, + std::vector<std::string> &HD, + std::string &KDFN) { + // both kernel and host dep file is empty + if (HD.empty() && KD.empty()) + return; + + // Write merged dep file + llvm::raw_fd_ostream DF(KDFN, EC, llvm::sys::fs::OF_Text); + if (EC) { + Diags.Report(diag::err_fe_error_opening) << KDFN << EC.message(); + return; + } + // if host dep file is empty, just write kernel dep file + if (HD.empty()) + for (const auto &DL : KD) + DF << DL << "\n"; + // if kernel dep file is empty, just write host dep file + else if (KD.empty()) + for (const auto &DL : HD) + DF << DL << "\n"; + // both kernel and host dep file is not empty, we need to merge them + else { + // guarentee the object file name of kernel dep and host dep is the same + if (KD.front() != HD.front()) + Diags.Report(diag::err_fe_error_opening) + << OutputFile + << "host dep file is not match kernel dep file for object file name"; + else { + // Write first line, which is the object file name + DF << KD.front() << "\n"; + // merge kernel and host dep file except first and last line + std::set<std::string> D; + D.insert(KD.begen() + 1, KD.end() - 1); + D.insert(HD.begen() + 1, HD.end() - 1); + for (const auto &DL : D) + DF << DL << "\n"; + // Write last line + if (KD.back() != HD.back()) { + DF << KD.back() << "\\\n"; + DF << HD.back() << "\n"; + } else { + DF << KD.back() << "\n"; + } + } + } +} + void DependencyFileGenerator::outputDependencyFile(DiagnosticsEngine &Diags) { if (SeenMissingHeader) { llvm::sys::fs::remove(OutputFile); return; } - std::error_code EC; - llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::OF_TextWithCRLF); - if (EC) { - Diags.Report(diag::err_fe_error_opening) << OutputFile << EC.message(); - return; - } + std::string KDFN = GetKernelDepFileName(OutputFile); + // if need to merge kernel and host dep file + if (KDFN != "") { + // Read kernel dep file + std::vector<std::string> KD; + llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> KDF = + llvm::MemoryBuffer::getFile(KDFN); + if (KDF) { + llvm::StringRef KDC = KDF.get()->getBuffer(); + KD = SplitToLines(KDC); + } + + // Read host dep file + std::vector<std::string> HD; + std::string HDC; + llvm::raw_string_ostream OSS(HDC); + outputDependencyFile(OSS); + OSS.flush(); + if (!HDC.empty()) + HD = SplitToLines(HDC); + + // Merge kernel and host dep file + TryMergeDependencyFile(KD, HD, KDFN); + } else { + // merge is not needed, just write the dep file + std::error_code EC; + llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::OF_Text); + if (EC) { + Diags.Report(diag::err_fe_error_opening) << OutputFile << EC.message(); + return; + } - outputDependencyFile(OS); + outputDependencyFile(OS); + } } void DependencyFileGenerator::outputDependencyFile(llvm::raw_ostream &OS) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits