projectpartbuilder.cpp 14 KB
Newer Older
1 2
/****************************************************************************
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
5 6 7 8 9 10 11
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
12 13 14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
15
**
16 17 18 19 20 21 22
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 24 25 26 27 28 29 30
**
****************************************************************************/

#include "projectpartbuilder.h"

#include "cppprojectfile.h"
#include "cpptoolsconstants.h"

31
#include <projectexplorer/abi.h>
32
#include <projectexplorer/headerpath.h>
33 34 35
#include <projectexplorer/kitinformation.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projectexplorerconstants.h>
36
#include <projectexplorer/runconfiguration.h>
37
#include <projectexplorer/target.h>
38
#include <projectexplorer/toolchain.h>
39 40 41 42 43 44 45 46 47 48

#include <utils/mimetypes/mimedatabase.h>
#include <utils/qtcassert.h>

namespace CppTools {

namespace {
class ProjectFileCategorizer
{
public:
49 50 51 52
    ProjectFileCategorizer(const QString &partName,
                           const QStringList &files,
                           ProjectPartBuilder::FileClassifier fileClassifier
                                = ProjectPartBuilder::FileClassifier())
53 54 55 56
        : m_partName(partName)
    {
        using CppTools::ProjectFile;

57 58
        QVector<ProjectFile> cHeaders;
        QVector<ProjectFile> cxxHeaders;
59 60

        foreach (const QString &file, files) {
61 62 63
            const ProjectFile::Kind kind = fileClassifier
                    ? fileClassifier(file)
                    : ProjectFile::classify(file);
64
            const ProjectFile projectFile(file, kind);
65 66

            switch (kind) {
67 68 69 70 71 72
            case ProjectFile::CSource: m_cSources += projectFile; break;
            case ProjectFile::CHeader: cHeaders += projectFile; break;
            case ProjectFile::CXXSource: m_cxxSources += projectFile; break;
            case ProjectFile::CXXHeader: cxxHeaders += projectFile; break;
            case ProjectFile::ObjCSource: m_objcSources += projectFile; break;
            case ProjectFile::ObjCXXSource: m_objcxxSources += projectFile; break;
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
            default:
                continue;
            }
        }

        const bool hasC = !m_cSources.isEmpty();
        const bool hasCxx = !m_cxxSources.isEmpty();
        const bool hasObjc = !m_objcSources.isEmpty();
        const bool hasObjcxx = !m_objcxxSources.isEmpty();

        if (hasObjcxx)
            m_objcxxSources += cxxHeaders + cHeaders;
        if (hasCxx)
            m_cxxSources += cxxHeaders + cHeaders;
        else if (!hasObjcxx)
            m_cxxSources += cxxHeaders;
        if (hasObjc)
            m_objcSources += cHeaders;
        if (hasC || (!hasObjc && !hasObjcxx && !hasCxx))
            m_cSources += cHeaders;

        m_partCount =
                (m_cSources.isEmpty() ? 0 : 1) +
                (m_cxxSources.isEmpty() ? 0 : 1) +
                (m_objcSources.isEmpty() ? 0 : 1) +
                (m_objcxxSources.isEmpty() ? 0 : 1);
    }

    bool hasCSources() const { return !m_cSources.isEmpty(); }
    bool hasCxxSources() const { return !m_cxxSources.isEmpty(); }
    bool hasObjcSources() const { return !m_objcSources.isEmpty(); }
    bool hasObjcxxSources() const { return !m_objcxxSources.isEmpty(); }

106 107 108 109
    QVector<ProjectFile> cSources() const { return m_cSources; }
    QVector<ProjectFile> cxxSources() const { return m_cxxSources; }
    QVector<ProjectFile> objcSources() const { return m_objcSources; }
    QVector<ProjectFile> objcxxSources() const { return m_objcxxSources; }
110 111 112 113 114 115 116 117 118 119 120 121 122 123

    bool hasMultipleParts() const { return m_partCount > 1; }
    bool hasNoParts() const { return m_partCount == 0; }

    QString partName(const QString &languageName) const
    {
        if (hasMultipleParts())
            return QString::fromLatin1("%1 (%2)").arg(m_partName).arg(languageName);

        return m_partName;
    }

private:
    QString m_partName;
124 125 126 127
    QVector<ProjectFile> m_cSources;
    QVector<ProjectFile> m_cxxSources;
    QVector<ProjectFile> m_objcSources;
    QVector<ProjectFile> m_objcxxSources;
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
    int m_partCount;
};
} // anonymous namespace

ProjectPartBuilder::ProjectPartBuilder(ProjectInfo &pInfo)
    : m_templatePart(new ProjectPart)
    , m_pInfo(pInfo)
{
    m_templatePart->project = pInfo.project();
    m_templatePart->displayName = pInfo.project()->displayName();
    m_templatePart->projectFile = pInfo.project()->projectFilePath().toString();
}

void ProjectPartBuilder::setQtVersion(ProjectPart::QtVersion qtVersion)
{
    m_templatePart->qtVersion = qtVersion;
}

void ProjectPartBuilder::setCFlags(const QStringList &flags)
{
    m_cFlags = flags;
}

void ProjectPartBuilder::setCxxFlags(const QStringList &flags)
{
    m_cxxFlags = flags;
}

void ProjectPartBuilder::setDefines(const QByteArray &defines)
{
    m_templatePart->projectDefines = defines;
}

void ProjectPartBuilder::setHeaderPaths(const ProjectPartHeaderPaths &headerPaths)
{
    m_templatePart->headerPaths = headerPaths;
}

void ProjectPartBuilder::setIncludePaths(const QStringList &includePaths)
{
    m_templatePart->headerPaths.clear();

    foreach (const QString &includeFile, includePaths) {
        ProjectPartHeaderPath hp(includeFile, ProjectPartHeaderPath::IncludePath);

        // The simple project managers are utterly ignorant of frameworks on OSX, and won't report
        // framework paths. The work-around is to check if the include path ends in ".framework",
        // and if so, add the parent directory as framework path.
        if (includeFile.endsWith(QLatin1String(".framework"))) {
            const int slashIdx = includeFile.lastIndexOf(QLatin1Char('/'));
            if (slashIdx != -1) {
                hp = ProjectPartHeaderPath(includeFile.left(slashIdx),
                                             ProjectPartHeaderPath::FrameworkPath);
            }
        }

        m_templatePart->headerPaths += hp;
    }
}

void ProjectPartBuilder::setPreCompiledHeaders(const QStringList &pchs)
{
    m_templatePart->precompiledHeaders = pchs;
}

void ProjectPartBuilder::setProjectFile(const QString &projectFile)
{
    m_templatePart->projectFile = projectFile;
}

void ProjectPartBuilder::setDisplayName(const QString &displayName)
{
    m_templatePart->displayName = displayName;
}

void ProjectPartBuilder::setConfigFileName(const QString &configFileName)
{
    m_templatePart->projectConfigFile = configFileName;
}

208 209
QList<Core::Id> ProjectPartBuilder::createProjectPartsForFiles(const QStringList &files,
                                                               FileClassifier fileClassifier)
210 211 212
{
    QList<Core::Id> languages;

213
    ProjectFileCategorizer cat(m_templatePart->displayName, files, fileClassifier);
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
    if (cat.hasNoParts())
        return languages;

    using CppTools::ProjectFile;
    using CppTools::ProjectPart;

    if (cat.hasCSources()) {
        createProjectPart(cat.cSources(),
                          cat.partName(QCoreApplication::translate("CppTools", "C11")),
                          ProjectPart::C11,
                          ProjectPart::NoExtensions);
        // TODO: there is no C...
//        languages += ProjectExplorer::Constants::LANG_C;
    }
    if (cat.hasObjcSources()) {
        createProjectPart(cat.objcSources(),
                          cat.partName(QCoreApplication::translate("CppTools", "Obj-C11")),
                          ProjectPart::C11,
                          ProjectPart::ObjectiveCExtensions);
        // TODO: there is no Ojective-C...
//        languages += ProjectExplorer::Constants::LANG_OBJC;
    }
    if (cat.hasCxxSources()) {
        createProjectPart(cat.cxxSources(),
                          cat.partName(QCoreApplication::translate("CppTools", "C++11")),
                          ProjectPart::CXX11,
                          ProjectPart::NoExtensions);
        languages += ProjectExplorer::Constants::LANG_CXX;
    }
    if (cat.hasObjcxxSources()) {
        createProjectPart(cat.objcxxSources(),
                          cat.partName(QCoreApplication::translate("CppTools", "Obj-C++11")),
                          ProjectPart::CXX11,
                          ProjectPart::ObjectiveCExtensions);
        // TODO: there is no Objective-C++...
        languages += ProjectExplorer::Constants::LANG_CXX;
    }

    return languages;
}

255 256 257 258 259 260 261 262 263 264 265 266
namespace {

ProjectPartHeaderPath toProjectPartHeaderPath(const ProjectExplorer::HeaderPath &headerPath)
{
    const ProjectPartHeaderPath::Type headerPathType =
        headerPath.kind() == ProjectExplorer::HeaderPath::FrameworkHeaderPath
            ? ProjectPartHeaderPath::FrameworkPath
            : ProjectPartHeaderPath::IncludePath;

    return ProjectPartHeaderPath(headerPath.path(), headerPathType);
}

267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
QString targetTriple(ProjectExplorer::Project *project, const Core::Id &toolchainId)
{
    using namespace ProjectExplorer;

    if (toolchainId == ProjectExplorer::Constants::MSVC_TOOLCHAIN_TYPEID)
        return QLatin1String("i686-pc-windows-msvc");

    if (project) {
        if (Target *target = project->activeTarget()) {
            if (ToolChain *toolChain = ToolChainKitInformation::toolChain(target->kit()))
                return toolChain->originalTargetTriple();
        }
    }

    return QString();
}

284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
}

/*!
    \brief Retrieves info from concrete compiler using it's flags.

    \param projectPart Project part which can never be an null pointer.
    \param toolChain Either nullptr or toolchain for project's active target.
    \param cxxflags C++ or Objective-C++ flags.
    \param cflags C or ObjectiveC flags if possible, \a cxxflags otherwise.
*/
void ProjectPartBuilder::evaluateProjectPartToolchain(
        ProjectPart *projectPart,
        const ProjectExplorer::ToolChain *toolChain,
        const QStringList &commandLineFlags,
        const Utils::FileName &sysRoot)
{
    if (toolChain == nullptr)
        return;

    using namespace ProjectExplorer;
    ToolChain::CompilerFlags flags = toolChain->compilerFlags(commandLineFlags);
    auto &languageVersion = projectPart->languageVersion;

    if (flags & ToolChain::StandardC11)
        languageVersion = ProjectPart::C11;
    else if (flags & ToolChain::StandardC99)
        languageVersion = ProjectPart::C99;
    else if (flags & ToolChain::StandardCxx17)
        languageVersion = ProjectPart::CXX17;
    else if (flags & ToolChain::StandardCxx14)
        languageVersion = ProjectPart::CXX14;
    else if (flags & ToolChain::StandardCxx11)
        languageVersion = ProjectPart::CXX11;
    else if (flags & ToolChain::StandardCxx98)
        languageVersion = ProjectPart::CXX98;
    else
        languageVersion = ProjectPart::CXX11;

    auto &languageExtensions = projectPart->languageExtensions;

    if (flags & ToolChain::BorlandExtensions)
        languageExtensions |= ProjectPart::BorlandExtensions;
    if (flags & ToolChain::GnuExtensions)
        languageExtensions |= ProjectPart::GnuExtensions;
    if (flags & ToolChain::MicrosoftExtensions)
        languageExtensions |= ProjectPart::MicrosoftExtensions;
    if (flags & ToolChain::OpenMP)
        languageExtensions |= ProjectPart::OpenMPExtensions;
    if (flags & ToolChain::ObjectiveC)
        languageExtensions |= ProjectPart::ObjectiveCExtensions;

    projectPart->warningFlags = toolChain->warningFlags(commandLineFlags);

    const QList<ProjectExplorer::HeaderPath> headers = toolChain->systemHeaderPaths(commandLineFlags, sysRoot);
    foreach (const ProjectExplorer::HeaderPath &header, headers) {
        const ProjectPartHeaderPath headerPath = toProjectPartHeaderPath(header);
        if (!projectPart->headerPaths.contains(headerPath))
            projectPart->headerPaths.push_back(headerPath);
    }

    projectPart->toolchainDefines = toolChain->predefinedMacros(commandLineFlags);
    projectPart->toolchainType = toolChain->typeId();
346 347
    projectPart->isMsvc2015Toolchain
            = toolChain->targetAbi().osFlavor() == ProjectExplorer::Abi::WindowsMsvc2015Flavor;
348
    projectPart->targetTriple = targetTriple(projectPart->project, toolChain->typeId());
349 350 351
    projectPart->updateLanguageFeatures();
}

352
void ProjectPartBuilder::createProjectPart(const QVector<ProjectFile> &theSources,
353 354 355 356 357 358
                                           const QString &partName,
                                           ProjectPart::LanguageVersion languageVersion,
                                           ProjectPart::LanguageExtensions languageExtensions)
{
    ProjectPart::Ptr part(m_templatePart->copy());
    part->displayName = partName;
359
    part->files = theSources;
360 361 362 363 364 365 366

    QTC_ASSERT(part->project, return);
    if (ProjectExplorer::Target *activeTarget = part->project->activeTarget()) {
        if (ProjectExplorer::Kit *kit = activeTarget->kit()) {
            if (ProjectExplorer::ToolChain *toolChain = ProjectExplorer::ToolChainKitInformation::toolChain(kit)) {
                const QStringList flags = languageVersion >= ProjectPart::CXX98 ? m_cxxFlags
                                                                                : m_cFlags;
367 368 369 370
                evaluateProjectPartToolchain(part.data(),
                                             toolChain,
                                             flags,
                                             ProjectExplorer::SysRootKitInformation::sysRoot(kit));
371 372 373 374 375 376 377 378 379 380
            }
        }
    }

    part->languageExtensions |= languageExtensions;

    m_pInfo.appendProjectPart(part);
}

} // namespace CppTools