File indexing completed on 2024-05-12 04:39:42

0001 /*
0002     SPDX-FileCopyrightText: 2007 David Nolden <david.nolden.kdevelop@art-master.de>
0003     SPDX-FileCopyrightText: 2014 Kevin Funk <kfunk@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef MAKEFILERESOLVER_H
0009 #define MAKEFILERESOLVER_H
0010 
0011 #include <QString>
0012 
0013 #include <language/editor/modificationrevisionset.h>
0014 
0015 #include <util/path.h>
0016 
0017 struct PathResolutionResult
0018 {
0019   explicit PathResolutionResult(bool success = false, const QString& errorMessage = QString(), const QString& longErrorMessage = QString());
0020 
0021   bool success;
0022   QString errorMessage;
0023   QString longErrorMessage;
0024 
0025   KDevelop::ModificationRevisionSet includePathDependency;
0026 
0027   KDevelop::Path::List paths;
0028   // the list of framework directories specified with explicit -iframework and/or -F arguments.
0029   // Mainly for OS X, but available everywhere to avoid #ifdefs and
0030   // because clang is an out-of-the-box cross-compiler.
0031   KDevelop::Path::List frameworkDirectories;
0032   QHash<QString, QString> defines;
0033 
0034   void mergeWith(const PathResolutionResult& rhs);
0035 
0036   operator bool() const;
0037 };
0038 
0039 class SourcePathInformation;
0040 
0041 // reuse cached instances of strings, to share memory where possible
0042 class StringInterner
0043 {
0044 public:
0045     QString internString(const QString& string);
0046 
0047 private:
0048     QSet<QString> m_stringCache;
0049 };
0050 
0051 // reuse cached instances of paths, to share memory where possible
0052 class PathInterner
0053 {
0054 public:
0055     PathInterner(const KDevelop::Path& base = {});
0056 
0057     KDevelop::Path internPath(const QString& path);
0058 
0059 private:
0060     const KDevelop::Path m_base;
0061     QHash<QString, KDevelop::Path> m_pathCache;
0062 };
0063 
0064 ///One resolution-try can issue up to 4 make-calls in worst case
0065 class MakeFileResolver
0066 {
0067   public:
0068     MakeFileResolver();
0069     ///Same as below, but uses the directory of the file as working-directory. The argument must be absolute.
0070     PathResolutionResult resolveIncludePath( const QString& file );
0071     ///The include-path is only computed once for a whole directory, then it is cached using the modification-time of the Makefile.
0072     ///source and build must be absolute paths
0073     void setOutOfSourceBuildSystem( const QString& source, const QString& build );
0074     ///resets to in-source build system
0075     void resetOutOfSourceBuild();
0076 
0077     static void clearCache();
0078 
0079     KDevelop::ModificationRevisionSet findIncludePathDependency(const QString& file);
0080 
0081     void enableMakeResolution(bool enable);
0082     PathResolutionResult processOutput(const QString& fullOutput, const QString& workingDirectory) const;
0083 
0084     static QRegularExpression defineRegularExpression();
0085     static QHash<QString, QString> extractDefinesFromCompileFlags(const QString& compileFlags, StringInterner& stringInterner, QHash<QString, QString> defines);
0086 
0087   private:
0088     PathResolutionResult resolveIncludePath( const QString& file, const QString& workingDirectory, int maxStepsUp = 20 );
0089 
0090     bool m_isResolving = false;
0091     bool m_outOfSource = false;
0092 
0093     QString mapToBuild(const QString &path) const;
0094 
0095     ///Executes the command using KProcess
0096     bool executeCommand( const QString& command, const QString& workingDirectory, QString& result ) const;
0097     ///file should be the name of the target, without extension(because that may be different)
0098     PathResolutionResult resolveIncludePathInternal( const QString& file, const QString& workingDirectory,
0099                                                       const QString& makeParameters, const SourcePathInformation& source, int maxDepth );
0100     QString m_source;
0101     QString m_build;
0102 
0103     mutable StringInterner m_stringInterner;
0104     mutable PathInterner m_pathInterner;
0105 };
0106 
0107 #endif