-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[CMake] Use DEPFILE instead of IMPLICIT_DEPENDS for dictionaries #23011
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
guitargeek
wants to merge
2
commits into
root-project:master
Choose a base branch
from
guitargeek:issue-21203
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+107
−5
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3709,6 +3709,10 @@ static llvm::cl::list<std::string> | |
| gOptWDiags("W", llvm::cl::Prefix, llvm::cl::ZeroOrMore, | ||
| llvm::cl::desc("Specify compiler diagnostics options."), | ||
| llvm::cl::cat(gRootclingOptions)); | ||
| static llvm::cl::opt<std::string> | ||
| gOptDepFile("MF", | ||
| llvm::cl::desc("Write dependency output to the specified file."), | ||
| llvm::cl::cat(gRootclingOptions)); | ||
| // Really OneOrMore, will be changed in RootClingMain below. | ||
| static llvm::cl::list<std::string> | ||
| gOptDictionaryHeaderFiles(llvm::cl::Positional, llvm::cl::ZeroOrMore, | ||
|
|
@@ -4517,6 +4521,11 @@ int RootClingMain(int argc, | |
| // Check if code goes to stdout or rootcling file | ||
| std::ofstream fileout; | ||
| string main_dictname(gOptDictionaryFileName.getValue()); | ||
| // Keep the original dictionary output file name (with extension) for the | ||
| // dependency file target: `main_dictname` gets its extension stripped below | ||
| // and `gOptDictionaryFileName` is turned into a temporary name by the | ||
| // tmpCatalog a few lines down. | ||
| const std::string dictOutputFileName(gOptDictionaryFileName.getValue()); | ||
| std::ostream *splitDictStream = nullptr; | ||
| std::unique_ptr<std::ostream> splitDeleter(nullptr); | ||
| // Store the temp files | ||
|
|
@@ -4999,6 +5008,93 @@ int RootClingMain(int argc, | |
| // make sure the file is closed before committing | ||
| fileout.close(); | ||
|
|
||
| // Write the dependency file if requested (-MF <file>). It uses the | ||
| // Makefile format understood by CMake's DEPFILE and Ninja's "deps = gcc", | ||
| // listing every real header that was opened while generating the dictionary | ||
| // so that incremental builds pick up changes to transitively included files. | ||
| if (!gOptDepFile.empty() && rootclingRetCode == 0 && !dictOutputFileName.empty()) { | ||
| std::ofstream depFile(gOptDepFile.c_str()); | ||
| if (!depFile) { | ||
| ROOT::TMetaUtils::Error(nullptr, | ||
| "rootcling: failed to open dependency file %s\n", | ||
| gOptDepFile.c_str()); | ||
| rootclingRetCode = 1; | ||
| } else { | ||
| // Escape a path for the Makefile-format dependency file: forward | ||
| // slashes (needed on Windows) and backslash-escape the characters that | ||
| // are special to make (space, tab, '#', ':'). | ||
| auto escapeForDepFile = [](std::string path) { | ||
| std::replace(path.begin(), path.end(), '\\', '/'); | ||
| std::string escaped; | ||
| escaped.reserve(path.size()); | ||
| for (char c : path) { | ||
| if (c == ' ' || c == '\t' || c == '#' || c == ':') | ||
| escaped += '\\'; | ||
| escaped += c; | ||
| } | ||
| return escaped; | ||
| }; | ||
|
|
||
| // The target is the final dictionary source file. Note that | ||
| // gOptDictionaryFileName has been turned into a temporary name by the | ||
| // tmpCatalog, so we use the original name captured earlier. | ||
| depFile << escapeForDepFile(dictOutputFileName) << ":"; | ||
|
|
||
| // Collect all files that were read by clang during dictionary | ||
| // generation (headers included directly or indirectly). | ||
| clang::SourceManager &SM = CI->getSourceManager(); | ||
| clang::FileManager &FM = SM.getFileManager(); | ||
|
|
||
| llvm::SmallVector<clang::OptionalFileEntryRef, 64> files; | ||
| FM.GetUniqueIDMapping(files); | ||
|
|
||
| std::set<std::string> includedFiles; | ||
| for (const auto &FEOpt : files) { | ||
| if (!FEOpt) | ||
| continue; | ||
| llvm::StringRef filename = FEOpt->getName(); | ||
| if (filename.empty()) | ||
| continue; | ||
| // Skip cling's in-memory buffers, which the FileManager also | ||
| // reports: "input_line_N", "<<< cling interactive line includer >>>", | ||
| // "<built-in>", "<command line>", ... These are not real files; | ||
| // some contain spaces or angle brackets that would corrupt the | ||
| // dependency file, and all of them would make the dictionary appear | ||
| // perpetually out of date. Requiring the entry to exist on disk | ||
| // filters them out (together with the explicit angle-bracket check). | ||
| if (filename.contains('<') || filename.contains('>')) | ||
| continue; | ||
| // Make the path absolute so it is unambiguous regardless of the | ||
| // working directory: rootcling may run from a different directory | ||
| // than the one the dependency file is later consumed from (with | ||
| // CMP0116 OLD the depfile is not rewritten, and a relative entry | ||
| // like "./Foo.hxx" would be resolved against the wrong base and | ||
| // leave the dictionary permanently out of date). | ||
| llvm::SmallString<256> absPath(filename); | ||
| llvm::sys::fs::make_absolute(absPath); | ||
| if (!llvm::sys::fs::exists(absPath)) | ||
| continue; | ||
| std::string filenameStr(absPath.str()); | ||
| // Skip the output dictionary file itself (final or temporary name). | ||
| if (filenameStr == dictOutputFileName || filenameStr == gOptDictionaryFileName.getValue()) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are |
||
| continue; | ||
| includedFiles.insert(std::move(filenameStr)); | ||
| } | ||
|
|
||
| // Each dependency line except the last ends with a backslash. | ||
| for (const auto &file : includedFiles) | ||
| depFile << " \\\n " << escapeForDepFile(file); | ||
| if (!includedFiles.empty()) | ||
| depFile << "\n"; | ||
|
|
||
| depFile.close(); | ||
| if (!depFile.good()) { | ||
| ROOT::TMetaUtils::Error(nullptr, "rootcling: failed to write dependency file %s\n", gOptDepFile.c_str()); | ||
| rootclingRetCode = 1; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Before returning, rename the files if no errors occurred | ||
| // otherwise clean them to avoid remnants (see ROOT-10015) | ||
| if(rootclingRetCode == 0) { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that the current
LLVMcode can handle (when it needs to)CMP0116to beNEW(See content ofinterpreter/llvm-project/llvm/cmake/modules/TableGen.cmake; and the fact that the code refered to in 7392b02 in no longer ininterpreter/llvm-project/cmake/Modules/CMakePolicy.cmake).I would strongly recommend that we also (in a separate PR) set
CMP0116to beNEWglobally.