2017-01-30 11:24:46 +01:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
|
|
|
|
**
|
|
|
|
|
** 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
|
|
|
|
|
** 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.
|
|
|
|
|
**
|
|
|
|
|
** 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.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "pchcreator.h"
|
|
|
|
|
|
|
|
|
|
#include "environment.h"
|
|
|
|
|
#include "includecollector.h"
|
|
|
|
|
#include "pchnotcreatederror.h"
|
|
|
|
|
|
2017-09-21 11:43:59 +02:00
|
|
|
#include <filepathcachinginterface.h>
|
2017-01-30 11:24:46 +01:00
|
|
|
#include <projectpartpch.h>
|
|
|
|
|
|
|
|
|
|
#include <QCryptographicHash>
|
2018-02-15 14:29:20 +01:00
|
|
|
#include <QDateTime>
|
2017-01-30 11:24:46 +01:00
|
|
|
#include <QFile>
|
2017-02-16 18:25:19 +01:00
|
|
|
#include <QTemporaryFile>
|
2017-01-30 11:24:46 +01:00
|
|
|
|
|
|
|
|
namespace ClangBackEnd {
|
|
|
|
|
|
2017-09-21 11:43:59 +02:00
|
|
|
PchCreator::PchCreator(Environment &environment, FilePathCachingInterface &filePathCache)
|
2017-01-30 11:24:46 +01:00
|
|
|
: m_environment(environment),
|
|
|
|
|
m_filePathCache(filePathCache)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PchCreator::PchCreator(V2::ProjectPartContainers &&projectsParts,
|
|
|
|
|
Environment &environment,
|
2017-09-21 11:43:59 +02:00
|
|
|
FilePathCachingInterface &filePathCache,
|
2017-02-01 13:43:28 +01:00
|
|
|
PchGeneratorInterface *pchGenerator,
|
|
|
|
|
V2::FileContainers &&generatedFiles)
|
2017-01-30 11:24:46 +01:00
|
|
|
: m_projectParts(std::move(projectsParts)),
|
2017-02-01 13:43:28 +01:00
|
|
|
m_generatedFiles(std::move(generatedFiles)),
|
2017-01-30 11:24:46 +01:00
|
|
|
m_environment(environment),
|
2017-01-31 14:21:05 +01:00
|
|
|
m_filePathCache(filePathCache),
|
|
|
|
|
m_pchGenerator(pchGenerator)
|
2017-01-30 11:24:46 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-01 13:43:28 +01:00
|
|
|
void PchCreator::setGeneratedFiles(V2::FileContainers &&generatedFiles)
|
|
|
|
|
{
|
|
|
|
|
m_generatedFiles = generatedFiles;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-30 11:24:46 +01:00
|
|
|
namespace {
|
|
|
|
|
template <typename Source,
|
|
|
|
|
typename Target>
|
|
|
|
|
void append(Target &target, const Source &source)
|
|
|
|
|
{
|
2017-08-29 12:54:10 +02:00
|
|
|
using ValueType = typename Target::value_type;
|
2017-01-30 11:24:46 +01:00
|
|
|
Source clonedSource = source.clone();
|
|
|
|
|
|
|
|
|
|
target.reserve(target.size() + source.size());
|
|
|
|
|
|
2017-08-29 18:41:21 +02:00
|
|
|
for (auto &&entry : clonedSource)
|
2017-08-29 12:54:10 +02:00
|
|
|
target.push_back(ValueType(std::move(entry)));
|
2017-01-30 11:24:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename Source,
|
|
|
|
|
typename Target>
|
|
|
|
|
void append(Target &target, Source &source)
|
|
|
|
|
{
|
|
|
|
|
target.reserve(target.size() + source.size());
|
|
|
|
|
|
2017-08-29 18:41:21 +02:00
|
|
|
for (auto &&entry : source)
|
2018-01-22 14:21:01 +01:00
|
|
|
target.emplace_back(entry);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void appendFilePathId(Utils::PathStringVector &target,
|
|
|
|
|
const ClangBackEnd::FilePathIds &source,
|
|
|
|
|
const ClangBackEnd::FilePathCachingInterface &filePathCache)
|
|
|
|
|
{
|
|
|
|
|
for (FilePathId id : source)
|
|
|
|
|
target.emplace_back(filePathCache.filePath(id).path());
|
2017-01-30 11:24:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename GetterFunction>
|
|
|
|
|
std::size_t globalCount(const V2::ProjectPartContainers &projectsParts,
|
|
|
|
|
GetterFunction getterFunction)
|
|
|
|
|
{
|
|
|
|
|
auto sizeFunction = [&] (std::size_t size, const V2::ProjectPartContainer &projectContainer) {
|
|
|
|
|
return size + getterFunction(projectContainer).size();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return std::accumulate(projectsParts.begin(),
|
|
|
|
|
projectsParts.end(),
|
|
|
|
|
std::size_t(0),
|
|
|
|
|
sizeFunction);
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-01 13:43:28 +01:00
|
|
|
template <typename Container,
|
|
|
|
|
typename GetterFunction>
|
|
|
|
|
void generateGlobal(Container &entries,
|
2017-01-30 11:24:46 +01:00
|
|
|
const V2::ProjectPartContainers &projectsParts,
|
|
|
|
|
GetterFunction getterFunction)
|
|
|
|
|
{
|
2017-02-01 13:43:28 +01:00
|
|
|
entries.reserve(entries.capacity() + globalCount(projectsParts, getterFunction));
|
2017-01-30 11:24:46 +01:00
|
|
|
|
|
|
|
|
for (const V2::ProjectPartContainer &projectPart : projectsParts) {
|
2018-01-22 14:21:01 +01:00
|
|
|
auto &&projectPartPaths = getterFunction(projectPart);
|
2017-01-30 11:24:46 +01:00
|
|
|
|
|
|
|
|
append(entries, projectPartPaths);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-01 13:43:28 +01:00
|
|
|
template <typename Container,
|
|
|
|
|
typename GetterFunction>
|
|
|
|
|
Utils::PathStringVector generateGlobal(
|
2017-01-30 11:24:46 +01:00
|
|
|
const V2::ProjectPartContainers &projectsParts,
|
2017-02-01 13:43:28 +01:00
|
|
|
GetterFunction getterFunction,
|
|
|
|
|
std::size_t prereserve = 0)
|
2017-01-30 11:24:46 +01:00
|
|
|
{
|
2017-02-01 13:43:28 +01:00
|
|
|
Container entries;
|
|
|
|
|
entries.reserve(prereserve);
|
2017-01-30 11:24:46 +01:00
|
|
|
|
|
|
|
|
generateGlobal(entries, projectsParts, getterFunction);
|
|
|
|
|
|
|
|
|
|
return entries;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-01 13:43:28 +01:00
|
|
|
|
|
|
|
|
Utils::PathStringVector generatedFilePaths(const V2::FileContainers &generaredFiles)
|
|
|
|
|
{
|
|
|
|
|
Utils::PathStringVector generaredFilePaths;
|
|
|
|
|
generaredFilePaths.reserve(generaredFiles.size());
|
|
|
|
|
|
|
|
|
|
for (const V2::FileContainer &generatedFile : generaredFiles)
|
2018-04-08 01:20:42 +03:00
|
|
|
generaredFilePaths.push_back(generatedFile.filePath.path());
|
2017-02-01 13:43:28 +01:00
|
|
|
|
|
|
|
|
return generaredFilePaths;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-30 11:24:46 +01:00
|
|
|
}
|
|
|
|
|
|
2017-02-01 13:43:28 +01:00
|
|
|
Utils::PathStringVector PchCreator::generateGlobalHeaderPaths() const
|
2017-01-30 11:24:46 +01:00
|
|
|
{
|
2018-01-22 14:21:01 +01:00
|
|
|
auto includeFunction = [&] (const V2::ProjectPartContainer &projectPart) {
|
2018-04-08 01:20:42 +03:00
|
|
|
return m_filePathCache.filePaths(projectPart.headerPathIds);
|
2017-01-30 11:24:46 +01:00
|
|
|
};
|
|
|
|
|
|
2017-02-01 13:43:28 +01:00
|
|
|
Utils::PathStringVector headerPaths = generateGlobal<Utils::PathStringVector>(m_projectParts,
|
|
|
|
|
includeFunction,
|
|
|
|
|
m_generatedFiles.size());
|
|
|
|
|
|
|
|
|
|
Utils::PathStringVector generatedPath = generatedFilePaths(m_generatedFiles);
|
|
|
|
|
|
|
|
|
|
headerPaths.insert(headerPaths.end(),
|
|
|
|
|
std::make_move_iterator(generatedPath.begin()),
|
|
|
|
|
std::make_move_iterator(generatedPath.end()));
|
|
|
|
|
|
|
|
|
|
return headerPaths;
|
2017-01-30 11:24:46 +01:00
|
|
|
}
|
|
|
|
|
|
2017-02-01 13:43:28 +01:00
|
|
|
Utils::PathStringVector PchCreator::generateGlobalSourcePaths() const
|
2017-01-30 11:24:46 +01:00
|
|
|
{
|
2018-01-22 14:21:01 +01:00
|
|
|
auto sourceFunction = [&] (const V2::ProjectPartContainer &projectPart) {
|
2018-04-08 01:20:42 +03:00
|
|
|
return m_filePathCache.filePaths(projectPart.sourcePathIds);
|
2017-01-30 11:24:46 +01:00
|
|
|
};
|
|
|
|
|
|
2017-02-01 13:43:28 +01:00
|
|
|
return generateGlobal<Utils::PathStringVector>(m_projectParts, sourceFunction);
|
2017-01-30 11:24:46 +01:00
|
|
|
}
|
|
|
|
|
|
2017-02-01 13:43:28 +01:00
|
|
|
Utils::PathStringVector PchCreator::generateGlobalHeaderAndSourcePaths() const
|
2017-01-30 11:24:46 +01:00
|
|
|
{
|
|
|
|
|
const auto &sourcePaths = generateGlobalSourcePaths();
|
|
|
|
|
auto includePaths = generateGlobalHeaderPaths();
|
|
|
|
|
|
|
|
|
|
append(includePaths, sourcePaths);
|
|
|
|
|
|
|
|
|
|
return includePaths;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utils::SmallStringVector PchCreator::generateGlobalArguments() const
|
|
|
|
|
{
|
|
|
|
|
Utils::SmallStringVector arguments;
|
|
|
|
|
|
|
|
|
|
auto argumentFunction = [] (const V2::ProjectPartContainer &projectPart)
|
|
|
|
|
-> const Utils::SmallStringVector & {
|
2018-04-08 01:20:42 +03:00
|
|
|
return projectPart.arguments;
|
2017-01-30 11:24:46 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
generateGlobal(arguments, m_projectParts, argumentFunction);
|
|
|
|
|
|
|
|
|
|
return arguments;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utils::SmallStringVector PchCreator::generateGlobalCommandLine() const
|
|
|
|
|
{
|
|
|
|
|
Utils::SmallStringVector commandLine;
|
|
|
|
|
commandLine.emplace_back(m_environment.clangCompilerPath());
|
|
|
|
|
|
|
|
|
|
auto argumentFunction = [] (const V2::ProjectPartContainer &projectPart)
|
|
|
|
|
-> const Utils::SmallStringVector & {
|
2018-04-08 01:20:42 +03:00
|
|
|
return projectPart.arguments;
|
2017-01-30 11:24:46 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
generateGlobal(commandLine, m_projectParts, argumentFunction);
|
|
|
|
|
|
|
|
|
|
return commandLine;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utils::SmallStringVector PchCreator::generateGlobalPchCompilerArguments() const
|
|
|
|
|
{
|
|
|
|
|
Utils::SmallStringVector arguments;
|
|
|
|
|
arguments.reserve(5);
|
|
|
|
|
|
|
|
|
|
arguments.emplace_back("-x");
|
|
|
|
|
arguments.emplace_back("c++-header");
|
|
|
|
|
arguments.emplace_back("-Xclang");
|
|
|
|
|
arguments.emplace_back("-emit-pch");
|
|
|
|
|
arguments.emplace_back("-o");
|
|
|
|
|
arguments.emplace_back(generateGlobalPchFilePath());
|
|
|
|
|
arguments.emplace_back(generateGlobalPchHeaderFilePath());
|
|
|
|
|
|
|
|
|
|
return arguments;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utils::SmallStringVector PchCreator::generateGlobalClangCompilerArguments() const
|
|
|
|
|
{
|
|
|
|
|
auto compilerArguments = generateGlobalArguments();
|
|
|
|
|
const auto pchArguments = generateGlobalPchCompilerArguments();
|
|
|
|
|
|
|
|
|
|
append(compilerArguments, pchArguments);
|
|
|
|
|
|
|
|
|
|
return compilerArguments;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-21 11:43:59 +02:00
|
|
|
FilePathIds PchCreator::generateGlobalPchIncludeIds() const
|
2017-01-30 11:24:46 +01:00
|
|
|
{
|
|
|
|
|
IncludeCollector collector(m_filePathCache);
|
|
|
|
|
|
2017-02-16 18:25:19 +01:00
|
|
|
collector.setExcludedIncludes(generateGlobalHeaderAndSourcePaths());
|
2017-01-30 11:24:46 +01:00
|
|
|
|
|
|
|
|
collector.addFiles(generateGlobalHeaderAndSourcePaths(), generateGlobalCommandLine());
|
|
|
|
|
|
2017-02-01 13:43:28 +01:00
|
|
|
collector.addUnsavedFiles(m_generatedFiles);
|
|
|
|
|
|
2017-01-30 11:24:46 +01:00
|
|
|
collector.collectIncludes();
|
|
|
|
|
|
|
|
|
|
return collector.takeIncludeIds();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
2017-09-21 11:43:59 +02:00
|
|
|
std::size_t contentSize(const FilePaths &includes)
|
2017-01-30 11:24:46 +01:00
|
|
|
{
|
2018-01-22 14:21:01 +01:00
|
|
|
auto countIncludeSize = [] (std::size_t size, const auto &include) {
|
2017-01-30 11:24:46 +01:00
|
|
|
return size + include.size();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return std::accumulate(includes.begin(), includes.end(), std::size_t(0), countIncludeSize);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-21 11:43:59 +02:00
|
|
|
Utils::SmallString PchCreator::generatePchIncludeFileContent(const FilePathIds &includeIds) const
|
2017-01-30 11:24:46 +01:00
|
|
|
{
|
|
|
|
|
Utils::SmallString fileContent;
|
|
|
|
|
const std::size_t lineTemplateSize = 12;
|
2017-09-21 11:43:59 +02:00
|
|
|
auto includes = m_filePathCache.filePaths(includeIds);
|
2017-01-30 11:24:46 +01:00
|
|
|
|
|
|
|
|
fileContent.reserve(includes.size() * lineTemplateSize + contentSize(includes));
|
|
|
|
|
|
2018-01-22 14:21:01 +01:00
|
|
|
for (const Utils::SmallStringView &include : includes)
|
2017-09-21 11:43:59 +02:00
|
|
|
fileContent += {"#include \"", include, "\"\n"};
|
2017-01-30 11:24:46 +01:00
|
|
|
|
|
|
|
|
return fileContent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utils::SmallString PchCreator::generateGlobalPchHeaderFileContent() const
|
|
|
|
|
{
|
|
|
|
|
return generatePchIncludeFileContent(generateGlobalPchIncludeIds());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<QFile> PchCreator::generateGlobalPchHeaderFile()
|
|
|
|
|
{
|
2017-02-16 18:25:19 +01:00
|
|
|
return generateFileWithContent(generateGlobalPchHeaderFilePath(),
|
2017-01-30 11:24:46 +01:00
|
|
|
generateGlobalPchHeaderFileContent());
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-31 14:21:05 +01:00
|
|
|
void PchCreator::generatePch(Utils::SmallStringVector &&compilerArguments,
|
|
|
|
|
ProjectPartPch &&projectPartPch)
|
2017-01-30 11:24:46 +01:00
|
|
|
{
|
2017-01-31 14:21:05 +01:00
|
|
|
m_pchGenerator->startTask(std::move(compilerArguments), std::move(projectPartPch));
|
2017-01-30 11:24:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PchCreator::generateGlobalPch()
|
|
|
|
|
{
|
|
|
|
|
generateGlobalPchHeaderFile();
|
|
|
|
|
|
2017-01-31 14:21:05 +01:00
|
|
|
generatePch(generateGlobalClangCompilerArguments(), ProjectPartPch());
|
2017-01-30 11:24:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QStringList PchCreator::convertToQStringList(const Utils::SmallStringVector &compilerArguments)
|
|
|
|
|
{
|
|
|
|
|
QStringList qStringList;
|
|
|
|
|
|
|
|
|
|
append(qStringList, compilerArguments);
|
|
|
|
|
|
|
|
|
|
return qStringList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
void hashProjectPart(QCryptographicHash &hash, const V2::ProjectPartContainer &projectPart)
|
|
|
|
|
{
|
2018-04-08 01:20:42 +03:00
|
|
|
const auto &projectPartId = projectPart.projectPartId;
|
2017-01-30 11:24:46 +01:00
|
|
|
hash.addData(projectPartId.data(), projectPartId.size());
|
|
|
|
|
|
2018-04-08 01:20:42 +03:00
|
|
|
for (const auto &argument : projectPart.arguments)
|
2017-01-30 11:24:46 +01:00
|
|
|
hash.addData(argument.data(), argument.size());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QByteArray PchCreator::globalProjectHash() const
|
|
|
|
|
{
|
|
|
|
|
QCryptographicHash hash(QCryptographicHash::Sha1);
|
|
|
|
|
|
|
|
|
|
for (const auto &projectPart : m_projectParts)
|
|
|
|
|
hashProjectPart(hash, projectPart);
|
|
|
|
|
|
|
|
|
|
auto result = hash.result();
|
|
|
|
|
|
|
|
|
|
return result.toBase64(QByteArray::Base64UrlEncoding | QByteArray::OmitTrailingEquals);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utils::SmallString PchCreator::generateGlobalPchFilePathWithoutExtension() const
|
|
|
|
|
{
|
|
|
|
|
QByteArray fileName = m_environment.pchBuildDirectory().toUtf8();
|
|
|
|
|
fileName += '/';
|
|
|
|
|
fileName += globalProjectHash();
|
|
|
|
|
|
|
|
|
|
return Utils::SmallString::fromQByteArray(fileName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utils::SmallString PchCreator::generateGlobalPchHeaderFilePath() const
|
|
|
|
|
{
|
|
|
|
|
Utils::SmallString filePath = generateGlobalPchFilePathWithoutExtension();
|
|
|
|
|
|
|
|
|
|
filePath += ".h";
|
|
|
|
|
|
|
|
|
|
return filePath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utils::SmallString PchCreator::generateGlobalPchFilePath() const
|
|
|
|
|
{
|
|
|
|
|
Utils::SmallString filePath = generateGlobalPchFilePathWithoutExtension();
|
|
|
|
|
|
|
|
|
|
filePath += ".pch";
|
|
|
|
|
|
|
|
|
|
return filePath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utils::SmallStringVector PchCreator::generateProjectPartCommandLine(
|
|
|
|
|
const V2::ProjectPartContainer &projectPart) const
|
|
|
|
|
{
|
2018-04-08 01:20:42 +03:00
|
|
|
const Utils::SmallStringVector &arguments = projectPart.arguments;
|
2017-01-30 11:24:46 +01:00
|
|
|
|
|
|
|
|
Utils::SmallStringVector commandLine;
|
|
|
|
|
commandLine.reserve(arguments.size() + 1);
|
|
|
|
|
|
|
|
|
|
commandLine.emplace_back(m_environment.clangCompilerPath());
|
|
|
|
|
|
|
|
|
|
append(commandLine , arguments);
|
|
|
|
|
|
|
|
|
|
return commandLine;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utils::SmallString PchCreator::generateProjectPartPchFilePathWithoutExtension(
|
|
|
|
|
const V2::ProjectPartContainer &projectPart) const
|
|
|
|
|
{
|
|
|
|
|
QByteArray fileName = m_environment.pchBuildDirectory().toUtf8();
|
|
|
|
|
fileName += '/';
|
|
|
|
|
fileName += projectPartHash(projectPart);
|
|
|
|
|
|
|
|
|
|
return Utils::SmallString::fromQByteArray(fileName);
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-01 13:43:28 +01:00
|
|
|
Utils::PathStringVector PchCreator::generateProjectPartHeaders(
|
|
|
|
|
const V2::ProjectPartContainer &projectPart) const
|
|
|
|
|
{
|
|
|
|
|
Utils::PathStringVector headerPaths;
|
2018-04-08 01:20:42 +03:00
|
|
|
headerPaths.reserve(projectPart.headerPathIds.size() + m_generatedFiles.size());
|
2017-02-01 13:43:28 +01:00
|
|
|
|
2018-04-08 01:20:42 +03:00
|
|
|
std::transform(projectPart.headerPathIds.begin(),
|
|
|
|
|
projectPart.headerPathIds.end(),
|
2018-01-22 14:21:01 +01:00
|
|
|
std::back_inserter(headerPaths),
|
|
|
|
|
[&] (FilePathId filePathId) {
|
|
|
|
|
return m_filePathCache.filePath(filePathId);
|
|
|
|
|
});
|
2017-02-01 13:43:28 +01:00
|
|
|
|
|
|
|
|
Utils::PathStringVector generatedPath = generatedFilePaths(m_generatedFiles);
|
|
|
|
|
|
|
|
|
|
std::copy(std::make_move_iterator(generatedPath.begin()),
|
|
|
|
|
std::make_move_iterator(generatedPath.end()),
|
|
|
|
|
std::back_inserter(headerPaths));
|
|
|
|
|
|
|
|
|
|
return headerPaths;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-16 18:25:19 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
std::size_t sizeOfContent(const Utils::PathStringVector &paths)
|
|
|
|
|
{
|
|
|
|
|
return std::accumulate(paths.begin(),
|
|
|
|
|
paths.end(),
|
|
|
|
|
std::size_t(0),
|
|
|
|
|
[] (std::size_t size, const Utils::PathString &path) {
|
|
|
|
|
const char includeTemplate[] = "#include \"\"\n";
|
|
|
|
|
return size + path.size() + sizeof(includeTemplate);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utils::SmallString concatContent(const Utils::PathStringVector &paths, std::size_t size)
|
|
|
|
|
{
|
|
|
|
|
Utils::SmallString content;
|
|
|
|
|
content.reserve(size);
|
|
|
|
|
|
|
|
|
|
for (const Utils::PathString &path : paths) {
|
|
|
|
|
content += "#include \"";
|
|
|
|
|
content += path;
|
|
|
|
|
content += "\"\n";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utils::SmallString PchCreator::generateProjectPartHeaderAndSourcesContent(
|
2018-01-22 14:21:01 +01:00
|
|
|
const V2::ProjectPartContainer &projectPart) const
|
2017-02-16 18:25:19 +01:00
|
|
|
{
|
|
|
|
|
Utils::PathStringVector paths = generateProjectPartHeaderAndSourcePaths(projectPart);
|
|
|
|
|
|
|
|
|
|
return concatContent(paths, sizeOfContent(paths));
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-01 13:43:28 +01:00
|
|
|
Utils::PathStringVector PchCreator::generateProjectPartHeaderAndSourcePaths(
|
2018-01-22 14:21:01 +01:00
|
|
|
const V2::ProjectPartContainer &projectPart) const
|
2017-01-30 11:24:46 +01:00
|
|
|
{
|
2017-02-01 13:43:28 +01:00
|
|
|
Utils::PathStringVector includeAndSources;
|
2018-04-08 01:20:42 +03:00
|
|
|
includeAndSources.reserve(projectPart.headerPathIds.size() + projectPart.sourcePathIds.size());
|
2017-01-30 11:24:46 +01:00
|
|
|
|
2018-04-08 01:20:42 +03:00
|
|
|
appendFilePathId(includeAndSources, projectPart.headerPathIds, m_filePathCache);
|
|
|
|
|
appendFilePathId(includeAndSources, projectPart.sourcePathIds, m_filePathCache);
|
2017-01-30 11:24:46 +01:00
|
|
|
|
|
|
|
|
return includeAndSources;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-28 16:00:10 +02:00
|
|
|
std::pair<FilePathIds,FilePathIds> PchCreator::generateProjectPartPchIncludes(
|
2017-01-30 11:24:46 +01:00
|
|
|
const V2::ProjectPartContainer &projectPart) const
|
|
|
|
|
{
|
2017-02-16 18:25:19 +01:00
|
|
|
Utils::SmallString jointedFileContent = generateProjectPartHeaderAndSourcesContent(projectPart);
|
|
|
|
|
Utils::SmallString jointedFilePath = generateProjectPartSourceFilePath(projectPart);
|
|
|
|
|
auto jointFile = generateFileWithContent(jointedFilePath, jointedFileContent);
|
|
|
|
|
Utils::SmallStringVector arguments = generateProjectPartCommandLine(projectPart);
|
|
|
|
|
arguments.push_back(jointedFilePath);
|
2017-08-29 12:54:10 +02:00
|
|
|
FilePath filePath{Utils::PathString(jointedFilePath)};
|
2017-02-16 18:25:19 +01:00
|
|
|
|
2017-01-30 11:24:46 +01:00
|
|
|
IncludeCollector collector(m_filePathCache);
|
|
|
|
|
|
2017-02-16 18:25:19 +01:00
|
|
|
collector.setExcludedIncludes(generateProjectPartHeaderAndSourcePaths(projectPart));
|
2017-01-30 11:24:46 +01:00
|
|
|
|
2017-08-29 12:54:10 +02:00
|
|
|
collector.addFile(std::string(filePath.directory()),
|
|
|
|
|
std::string(filePath.name()),
|
2017-02-16 18:25:19 +01:00
|
|
|
{},
|
|
|
|
|
arguments);
|
2017-01-30 11:24:46 +01:00
|
|
|
|
2017-02-01 13:43:28 +01:00
|
|
|
collector.addUnsavedFiles(m_generatedFiles);
|
|
|
|
|
|
2017-01-30 11:24:46 +01:00
|
|
|
collector.collectIncludes();
|
|
|
|
|
|
2017-02-16 18:25:19 +01:00
|
|
|
jointFile->remove();
|
|
|
|
|
|
2018-03-28 16:00:10 +02:00
|
|
|
return {collector.takeIncludeIds(), collector.takeTopIncludeIds()};
|
2017-01-30 11:24:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utils::SmallString PchCreator::generateProjectPathPchHeaderFilePath(
|
|
|
|
|
const V2::ProjectPartContainer &projectPart) const
|
|
|
|
|
{
|
2017-02-16 18:25:19 +01:00
|
|
|
return Utils::SmallString{generateProjectPartPchFilePathWithoutExtension(projectPart), ".h"};
|
2017-01-30 11:24:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utils::SmallString PchCreator::generateProjectPartPchFilePath(
|
|
|
|
|
const V2::ProjectPartContainer &projectPart) const
|
|
|
|
|
{
|
2017-02-16 18:25:19 +01:00
|
|
|
return Utils::SmallString{generateProjectPartPchFilePathWithoutExtension(projectPart), ".pch"};
|
|
|
|
|
}
|
2017-01-30 11:24:46 +01:00
|
|
|
|
2017-02-16 18:25:19 +01:00
|
|
|
Utils::SmallString PchCreator::generateProjectPartSourceFilePath(const V2::ProjectPartContainer &projectPart) const
|
|
|
|
|
{
|
|
|
|
|
return Utils::SmallString{generateProjectPartPchFilePathWithoutExtension(projectPart), ".cpp"};
|
2017-01-30 11:24:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utils::SmallStringVector PchCreator::generateProjectPartPchCompilerArguments(
|
|
|
|
|
const V2::ProjectPartContainer &projectPart) const
|
|
|
|
|
{
|
|
|
|
|
Utils::SmallStringVector arguments;
|
|
|
|
|
arguments.reserve(5);
|
|
|
|
|
|
|
|
|
|
arguments.emplace_back("-x");
|
|
|
|
|
arguments.emplace_back("c++-header");
|
|
|
|
|
// arguments.emplace_back("-Xclang");
|
|
|
|
|
// arguments.emplace_back("-include-pch");
|
|
|
|
|
// arguments.emplace_back("-Xclang");
|
|
|
|
|
// arguments.emplace_back(generateGlobalPchFilePath());
|
|
|
|
|
arguments.emplace_back("-Xclang");
|
|
|
|
|
arguments.emplace_back("-emit-pch");
|
|
|
|
|
arguments.emplace_back("-o");
|
|
|
|
|
arguments.emplace_back(generateProjectPartPchFilePath(projectPart));
|
|
|
|
|
arguments.emplace_back(generateProjectPathPchHeaderFilePath(projectPart));
|
|
|
|
|
|
|
|
|
|
return arguments;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utils::SmallStringVector PchCreator::generateProjectPartClangCompilerArguments(
|
|
|
|
|
const V2::ProjectPartContainer &projectPart) const
|
|
|
|
|
{
|
2018-04-08 01:20:42 +03:00
|
|
|
Utils::SmallStringVector compilerArguments = projectPart.arguments.clone();
|
2017-01-30 11:24:46 +01:00
|
|
|
const auto pchArguments = generateProjectPartPchCompilerArguments(projectPart);
|
|
|
|
|
|
|
|
|
|
append(compilerArguments, pchArguments);
|
|
|
|
|
|
|
|
|
|
return compilerArguments;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-31 14:21:05 +01:00
|
|
|
IdPaths PchCreator::generateProjectPartPch(const V2::ProjectPartContainer &projectPart)
|
2017-01-30 11:24:46 +01:00
|
|
|
{
|
2018-02-15 14:29:20 +01:00
|
|
|
long long lastModified = QDateTime::currentSecsSinceEpoch();
|
2018-03-28 16:00:10 +02:00
|
|
|
FilePathIds allExternalIncludes;
|
|
|
|
|
FilePathIds topExternalIncludes;
|
|
|
|
|
std::tie(allExternalIncludes, topExternalIncludes) = generateProjectPartPchIncludes(projectPart);
|
|
|
|
|
auto content = generatePchIncludeFileContent(topExternalIncludes);
|
2017-01-30 11:24:46 +01:00
|
|
|
auto pchIncludeFilePath = generateProjectPathPchHeaderFilePath(projectPart);
|
|
|
|
|
auto pchFilePath = generateProjectPartPchFilePath(projectPart);
|
2017-02-16 18:25:19 +01:00
|
|
|
generateFileWithContent(pchIncludeFilePath, content);
|
2017-01-30 11:24:46 +01:00
|
|
|
|
2017-01-31 14:21:05 +01:00
|
|
|
generatePch(generateProjectPartClangCompilerArguments(projectPart),
|
2018-04-08 01:20:42 +03:00
|
|
|
{projectPart.projectPartId.clone(), std::move(pchFilePath), lastModified});
|
2017-01-30 11:24:46 +01:00
|
|
|
|
2018-04-08 01:20:42 +03:00
|
|
|
return {projectPart.projectPartId.clone(), std::move(allExternalIncludes)};
|
2017-01-30 11:24:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PchCreator::generatePchs()
|
|
|
|
|
{
|
|
|
|
|
for (const V2::ProjectPartContainer &projectPart : m_projectParts) {
|
2017-01-31 14:21:05 +01:00
|
|
|
auto includePaths = generateProjectPartPch(projectPart);
|
|
|
|
|
m_projectsIncludeIds.push_back(std::move(includePaths));
|
2017-01-30 11:24:46 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PchCreator::generatePchs(V2::ProjectPartContainers &&projectsParts)
|
|
|
|
|
{
|
|
|
|
|
m_projectParts = std::move(projectsParts);
|
|
|
|
|
|
|
|
|
|
generatePchs();
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-31 14:21:05 +01:00
|
|
|
std::vector<IdPaths> PchCreator::takeProjectsIncludes()
|
2017-01-30 11:24:46 +01:00
|
|
|
{
|
2017-01-31 14:21:05 +01:00
|
|
|
return std::move(m_projectsIncludeIds);
|
2017-01-30 11:24:46 +01:00
|
|
|
}
|
|
|
|
|
|
2017-01-31 14:21:05 +01:00
|
|
|
void PchCreator::setGenerator(PchGeneratorInterface *pchGenerator)
|
2017-01-30 11:24:46 +01:00
|
|
|
{
|
2017-01-31 14:21:05 +01:00
|
|
|
m_pchGenerator = pchGenerator;
|
2017-01-30 11:24:46 +01:00
|
|
|
}
|
|
|
|
|
|
2017-02-16 18:25:19 +01:00
|
|
|
std::unique_ptr<QFile> PchCreator::generateFileWithContent(
|
2017-01-30 11:24:46 +01:00
|
|
|
const Utils::SmallString &filePath,
|
|
|
|
|
const Utils::SmallString &content)
|
|
|
|
|
{
|
2017-08-29 12:54:10 +02:00
|
|
|
std::unique_ptr<QFile> precompiledIncludeFile(new QFile(QString(filePath)));
|
2017-01-30 11:24:46 +01:00
|
|
|
|
|
|
|
|
precompiledIncludeFile->open(QIODevice::WriteOnly);
|
|
|
|
|
|
2017-08-29 12:54:10 +02:00
|
|
|
precompiledIncludeFile->write(content.data(), qint64(content.size()));
|
2017-01-30 11:24:46 +01:00
|
|
|
precompiledIncludeFile->close();
|
|
|
|
|
|
|
|
|
|
return precompiledIncludeFile;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QByteArray PchCreator::projectPartHash(const V2::ProjectPartContainer &projectPart)
|
|
|
|
|
{
|
|
|
|
|
QCryptographicHash hash(QCryptographicHash::Sha1);
|
|
|
|
|
|
|
|
|
|
hashProjectPart(hash, projectPart);
|
|
|
|
|
|
|
|
|
|
auto result = hash.result();
|
|
|
|
|
|
|
|
|
|
return result.toBase64(QByteArray::Base64UrlEncoding | QByteArray::OmitTrailingEquals);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace ClangBackEnd
|