File indexing completed on 2024-05-05 04:39:26

0001 /*
0002     SPDX-FileCopyrightText: 2009 Andreas Pakulat <apaku@gmx.de>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef CMAKEUTILS_H
0008 #define CMAKEUTILS_H
0009 
0010 #include "cmakecommonexport.h"
0011 
0012 #include <QStringList>
0013 #include <util/path.h>
0014 #include <QStandardPaths>
0015 
0016 namespace KDevelop
0017 {
0018     class ProjectBaseItem;
0019     class IProject;
0020     class Path;
0021 }
0022 
0023 struct CMakeTest;
0024 class ICMakeDocumentation;
0025 class CMakeCacheModel;
0026 
0027 template <typename T, typename Q, typename W>
0028 static T kTransform(const Q& list, W func)
0029 {
0030     T ret;
0031     ret.reserve(list.size());
0032     for (auto it = list.constBegin(), itEnd = list.constEnd(); it!=itEnd; ++it)
0033         ret += func(*it);
0034     return ret;
0035 }
0036 
0037 template <typename Q, typename W>
0038 static int kIndexOf(const Q& list, W func)
0039 {
0040     int i = 0;
0041     for (auto it = list.constBegin(), itEnd = list.constEnd(); it!=itEnd; ++it) {
0042         if (func(*it))
0043             return i;
0044         ++i;
0045     }
0046     return -1;
0047 }
0048 
0049 template <typename T, typename Q, typename _UnaryOperation>
0050 static T kFilter(const Q &input, _UnaryOperation op)
0051 {
0052     T ret;
0053     for(const auto& v : input) {
0054         if (op(v))
0055             ret += v;
0056     }
0057     return ret;
0058 }
0059 
0060 namespace CMake
0061 {
0062     /**
0063      * Checks whether there's a need to run cmake for the given project item
0064      * This is the case if no builddir has been specified, in which case
0065      * it asks for one.
0066      *
0067      * @returns true if configure should be run, false otherwise
0068      */
0069     KDEVCMAKECOMMON_EXPORT bool checkForNeedingConfigure( KDevelop::IProject* project );
0070 
0071     /**
0072      * @returns the current builddir for the given project or an empty url if none
0073      * has been set by the user.
0074      */
0075     KDEVCMAKECOMMON_EXPORT KDevelop::Path currentBuildDir( KDevelop::IProject* project, int builddir = -1 );
0076 
0077     /**
0078      * @returns the path to the 'compile_commands.json' file in the current builddir for the given project
0079      * or an empty url if none has been set by the user.
0080      */
0081     KDEVCMAKECOMMON_EXPORT KDevelop::Path commandsFile( KDevelop::IProject* project );
0082     /**
0083      * @returns the path to the 'CMakeFiles/TargetDirectories.txt' file in the current builddir for the given project
0084      * or an empty url if none has been set by the user.
0085      */
0086     KDEVCMAKECOMMON_EXPORT KDevelop::Path targetDirectoriesFile( KDevelop::IProject* project );
0087 
0088     /**
0089      * @returns the current build type for the given project or "Release" as default value.
0090      */
0091     KDEVCMAKECOMMON_EXPORT QString currentBuildType( KDevelop::IProject* project, int builddir = -1 );
0092 
0093     /**
0094     * @returns the CMake executable, taking into account standard
0095     * installation dirs on Windows, or empty string in case of failure.
0096     */
0097     KDEVCMAKECOMMON_EXPORT QString findExecutable();
0098     /**
0099      * @returns the current CMake executable for the given project, falling back to
0100      * CMakeBuilderSettings::self()->cmakeExecutable() as the user-specified KDevelop-wide default value. 
0101      */
0102     KDEVCMAKECOMMON_EXPORT KDevelop::Path currentCMakeExecutable(KDevelop::IProject* project, int builddir = -1);
0103 
0104     /**
0105      * @returns the version string reported by the given CMake executable
0106      */
0107     KDEVCMAKECOMMON_EXPORT QString cmakeExecutableVersion(const QString& cmakeExecutable);
0108 
0109     /**
0110      * @returns the current install dir for the given project or an empty string if none has been defined
0111      * (in that case, whatever default CMake sets for the platform will be used.)
0112      */
0113     KDEVCMAKECOMMON_EXPORT KDevelop::Path currentInstallDir( KDevelop::IProject* project, int builddir = -1 );
0114     
0115     /**
0116      * @returns the current extra arguments for the given project or "" as default value.
0117      */
0118     KDEVCMAKECOMMON_EXPORT QString currentExtraArguments( KDevelop::IProject* project, int builddir = -1 );
0119 
0120     /**
0121      * @returns the current build dir for the given project.
0122      */
0123     KDEVCMAKECOMMON_EXPORT QString projectRootRelative( KDevelop::IProject* project );
0124     
0125     /**
0126      * @returns whether there's projectRootRelative defined
0127      */
0128     KDEVCMAKECOMMON_EXPORT bool hasProjectRootRelative( KDevelop::IProject* project );
0129     
0130     /**
0131     * Extracts target names from builddir/CMakeFiles/TargetDirectories.txt and maps corresponding source locations to them.
0132     */
0133 
0134     KDEVCMAKECOMMON_EXPORT QHash<KDevelop::Path, QStringList> enumerateTargets(const KDevelop::Path& targetsFilePath, const QString& sourceDir, const KDevelop::Path &buildDir);
0135     /**
0136      * Convenience function to get the project root.
0137      */
0138     KDEVCMAKECOMMON_EXPORT KDevelop::Path projectRoot( KDevelop::IProject* project );
0139     
0140     /**
0141      * @returns the environment configuration for a @p project
0142      */
0143     KDEVCMAKECOMMON_EXPORT QString currentEnvironment( KDevelop::IProject* project, int builddir = -1 );
0144 
0145     /**
0146      * Sets the current install dir for the given project.
0147      */
0148     KDEVCMAKECOMMON_EXPORT void setCurrentInstallDir( KDevelop::IProject* project, const KDevelop::Path &path );
0149 
0150     /**
0151      * Sets the current build type for the given project.
0152      */
0153     KDEVCMAKECOMMON_EXPORT void setCurrentBuildType( KDevelop::IProject* project, const QString& type );
0154 
0155      /**
0156      * Sets the current CMake executable for the given project.
0157      */
0158     KDEVCMAKECOMMON_EXPORT void setCurrentCMakeExecutable(KDevelop::IProject* project, const KDevelop::Path& path);
0159 
0160     /**
0161      * Sets the current build dir for the given project.
0162      */
0163     KDEVCMAKECOMMON_EXPORT void setCurrentBuildDir( KDevelop::IProject* project, const KDevelop::Path& path );
0164     
0165     /**
0166      * Sets the current build dir for the given project.
0167      */
0168     KDEVCMAKECOMMON_EXPORT void setProjectRootRelative( KDevelop::IProject* project, const QString& path);
0169     
0170     /**
0171      * Sets the current extra arguments for the given project.
0172      */
0173     KDEVCMAKECOMMON_EXPORT void setCurrentExtraArguments( KDevelop::IProject* project, const QString& args );
0174 
0175     /**
0176      * Obtains a cmake documentation instance if it exists
0177      */
0178     KDEVCMAKECOMMON_EXPORT ICMakeDocumentation* cmakeDocumentation();
0179     
0180     /**
0181      * Retrieves the configured build directories for @p project.
0182      */
0183     KDEVCMAKECOMMON_EXPORT QStringList allBuildDirs(KDevelop::IProject* project);
0184 
0185     /**
0186      * Attempts to update CMake configuration keys from the cache data (CMakeCache.txt)
0187      *
0188      * The model is created based on build directory path for the given index @p buildDirectory
0189      */
0190     KDEVCMAKECOMMON_EXPORT void updateConfig( KDevelop::IProject* project, int buildDirectory);
0191 
0192     /**
0193      * Returns the current build directory count.
0194      */
0195     KDEVCMAKECOMMON_EXPORT int buildDirCount( KDevelop::IProject* project );
0196 
0197     /**
0198      * Sets the build directory count (equivalent to adding a new build directory).
0199      */
0200     KDEVCMAKECOMMON_EXPORT void setBuildDirCount( KDevelop::IProject* project, int count );
0201 
0202     /**
0203      * @returns the current builddir index for the given project or -1 if none
0204      * has been set by the user.
0205      */
0206     KDEVCMAKECOMMON_EXPORT int currentBuildDirIndex( KDevelop::IProject *project );
0207 
0208     /**
0209      * Sets the current build dir index for the given project.
0210      */
0211     KDEVCMAKECOMMON_EXPORT void setCurrentBuildDirIndex( KDevelop::IProject* project, int buildDirIndex );
0212 
0213     /**
0214      * A hack to avoid adding an optional "build directory index" parameter to all functions here.
0215      * This function sets an alternate build directory index key that overrides regular build directory index.
0216      */
0217     KDEVCMAKECOMMON_EXPORT void setOverrideBuildDirIndex( KDevelop::IProject* project, int overrideBuildDirIndex );
0218 
0219     /**
0220      * This removes build directory override key (\ref setOverrideBuildDirIndex).
0221      * Silently returns if there is no override.
0222      *
0223      * @param writeToMainIndex Whether the overridden index should be saved to regular
0224      */
0225     KDEVCMAKECOMMON_EXPORT void removeOverrideBuildDirIndex( KDevelop::IProject* project, bool writeToMainIndex = false );
0226 
0227     /**
0228      * Sets the environment configuration for the given project.
0229      */
0230     KDEVCMAKECOMMON_EXPORT void setCurrentEnvironment( KDevelop::IProject* project, const QString& environment );
0231 
0232     /**
0233      * Removes current build directory (overridden or not) from the project configuration.
0234      * Override is then cleared and index set to -1.
0235      */
0236     KDEVCMAKECOMMON_EXPORT void removeBuildDirConfig( KDevelop::IProject* project );
0237 
0238     KDEVCMAKECOMMON_EXPORT KDevelop::Path::List resolveSystemDirs(KDevelop::IProject* project, const QStringList& dirs);
0239 
0240     /** Runs the process specified by @p execName with @p args */
0241     KDEVCMAKECOMMON_EXPORT QString executeProcess(const QString& execName, const QStringList& args=QStringList());
0242 
0243     /** Opens @p cmakeCachePath and parses it, returns a hash with the specified keys in @p variables */
0244     KDEVCMAKECOMMON_EXPORT QHash<QString, QString> readCacheValues(const KDevelop::Path& cmakeCachePath, QSet<QString> variables);
0245 
0246     KDEVCMAKECOMMON_EXPORT QStringList supportedGenerators();
0247 
0248     KDEVCMAKECOMMON_EXPORT QString defaultGenerator();
0249 
0250     KDEVCMAKECOMMON_EXPORT QVector<CMakeTest> importTestSuites(const KDevelop::Path &buildDir);
0251 }
0252 
0253 #endif