forked from qt-creator/qt-creator
		
	Clang: Unify compiler options builders
Make build command the same for all builders. Minimize differences. Change-Id: I1cfe5071b3afb4944ed178fff1e57d3aee45d8a9 Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
This commit is contained in:
		| @@ -26,7 +26,6 @@ | ||||
| #include "clangcompileroptionsbuilder.h" | ||||
|  | ||||
| #include <coreplugin/icore.h> | ||||
|  | ||||
| #include <projectexplorer/projectexplorerconstants.h> | ||||
|  | ||||
| #include <utils/qtcassert.h> | ||||
| @@ -37,6 +36,15 @@ namespace CppTools { | ||||
|  | ||||
| static QString creatorResourcePath() | ||||
| { | ||||
| #ifndef UNIT_TESTS | ||||
|     return Core::ICore::instance()->resourcePath(); | ||||
| #else | ||||
|     return QString(); | ||||
| #endif | ||||
| } | ||||
|  | ||||
| static QString creatorLibexecPath() | ||||
| { | ||||
| #ifndef UNIT_TESTS | ||||
|     return Core::ICore::instance()->libexecPath(); | ||||
| #else | ||||
| @@ -44,40 +52,31 @@ static QString creatorResourcePath() | ||||
| #endif | ||||
| } | ||||
|  | ||||
| QStringList ClangCompilerOptionsBuilder::build(const CppTools::ProjectPart *projectPart, | ||||
|                                                CppTools::ProjectFile::Kind fileKind, | ||||
|                                                PchUsage pchUsage, | ||||
|                                                const QString &clangVersion, | ||||
|                                                const QString &clangResourceDirectory) | ||||
| QStringList ClangCompilerOptionsBuilder::build(CppTools::ProjectFile::Kind fileKind, | ||||
|                                                PchUsage pchUsage) | ||||
| { | ||||
|     if (projectPart) { | ||||
|         ClangCompilerOptionsBuilder builder(*projectPart, clangVersion, clangResourceDirectory); | ||||
|     addWordWidth(); | ||||
|     addTargetTriple(); | ||||
|     addLanguageOption(fileKind); | ||||
|     addOptionsForLanguage(/*checkForBorlandExtensions*/ true); | ||||
|     enableExceptions(); | ||||
|  | ||||
|         builder.addWordWidth(); | ||||
|         builder.addTargetTriple(); | ||||
|         builder.addLanguageOption(fileKind); | ||||
|         builder.addOptionsForLanguage(/*checkForBorlandExtensions*/ true); | ||||
|         builder.enableExceptions(); | ||||
|     addDefineFloat128ForMingw(); | ||||
|     addToolchainAndProjectMacros(); | ||||
|     undefineClangVersionMacrosForMsvc(); | ||||
|     undefineCppLanguageFeatureMacrosForMsvc2015(); | ||||
|  | ||||
|         builder.addDefineToAvoidIncludingGccOrMinGwIntrinsics(); | ||||
|         builder.addDefineFloat128ForMingw(); | ||||
|         builder.addToolchainAndProjectDefines(); | ||||
|         builder.undefineCppLanguageFeatureMacrosForMsvc2015(); | ||||
|     addPredefinedHeaderPathsOptions(); | ||||
|     addWrappedQtHeadersIncludePath(); | ||||
|     addPrecompiledHeaderOptions(pchUsage); | ||||
|     addHeaderPathOptions(); | ||||
|     addProjectConfigFileInclude(); | ||||
|  | ||||
|         builder.addPredefinedMacrosAndHeaderPathsOptions(); | ||||
|         builder.addWrappedQtHeadersIncludePath(); | ||||
|         builder.addPrecompiledHeaderOptions(pchUsage); | ||||
|         builder.addHeaderPathOptions(); | ||||
|         builder.addProjectConfigFileInclude(); | ||||
|     addMsvcCompatibilityVersion(); | ||||
|  | ||||
|         builder.addMsvcCompatibilityVersion(); | ||||
|     addExtraOptions(); | ||||
|  | ||||
|         builder.addExtraOptions(); | ||||
|  | ||||
|         return builder.options(); | ||||
|     } | ||||
|  | ||||
|     return QStringList(); | ||||
|     return options(); | ||||
| } | ||||
|  | ||||
| ClangCompilerOptionsBuilder::ClangCompilerOptionsBuilder(const CppTools::ProjectPart &projectPart, | ||||
| @@ -91,47 +90,35 @@ ClangCompilerOptionsBuilder::ClangCompilerOptionsBuilder(const CppTools::Project | ||||
|  | ||||
| bool ClangCompilerOptionsBuilder::excludeHeaderPath(const QString &path) const | ||||
| { | ||||
|     if (m_projectPart.toolchainType == ProjectExplorer::Constants::CLANG_TOOLCHAIN_TYPEID) { | ||||
|         if (path.contains("lib/gcc/i686-apple-darwin")) | ||||
|             return true; | ||||
|     if (m_projectPart.toolchainType == ProjectExplorer::Constants::CLANG_TOOLCHAIN_TYPEID | ||||
|             && path.contains("lib/gcc/i686-apple-darwin")) { | ||||
|         return true; | ||||
|     } | ||||
|  | ||||
|     return CompilerOptionsBuilder::excludeHeaderPath(path); | ||||
| } | ||||
|  | ||||
| void ClangCompilerOptionsBuilder::addPredefinedMacrosAndHeaderPathsOptions() | ||||
| void ClangCompilerOptionsBuilder::addPredefinedHeaderPathsOptions() | ||||
| { | ||||
|     if (m_projectPart.toolchainType == ProjectExplorer::Constants::MSVC_TOOLCHAIN_TYPEID) | ||||
|         addPredefinedMacrosAndHeaderPathsOptionsForMsvc(); | ||||
|     else | ||||
|         addPredefinedMacrosAndHeaderPathsOptionsForNonMsvc(); | ||||
| } | ||||
|  | ||||
| void ClangCompilerOptionsBuilder::addPredefinedMacrosAndHeaderPathsOptionsForMsvc() | ||||
| { | ||||
|     add("-nostdinc"); | ||||
|     add("-undef"); | ||||
| } | ||||
|     add("-nostdinc"); | ||||
|     add("-nostdlibinc"); | ||||
|  | ||||
| void ClangCompilerOptionsBuilder::addPredefinedMacrosAndHeaderPathsOptionsForNonMsvc() | ||||
| { | ||||
|     static const QString resourceDir = clangIncludeDirectory(); | ||||
|     if (QTC_GUARD(!resourceDir.isEmpty())) { | ||||
|         add("-nostdlibinc"); | ||||
|         add("-I" + QDir::toNativeSeparators(resourceDir)); | ||||
|         add("-undef"); | ||||
|     } | ||||
|     if (m_projectPart.toolchainType != ProjectExplorer::Constants::MSVC_TOOLCHAIN_TYPEID) | ||||
|         add(includeDirOption() + clangIncludeDirectory()); | ||||
| } | ||||
|  | ||||
| void ClangCompilerOptionsBuilder::addWrappedQtHeadersIncludePath() | ||||
| { | ||||
|     static const QString wrappedQtHeadersPath = creatorResourcePath() | ||||
|             + "/cplusplus/wrappedQtHeaders"; | ||||
|     static const QString resourcePath = creatorResourcePath(); | ||||
|     static QString wrappedQtHeadersPath = resourcePath + "/cplusplus/wrappedQtHeaders"; | ||||
|     QDir dir(wrappedQtHeadersPath); | ||||
|     QTC_ASSERT(QDir(wrappedQtHeadersPath).exists(), return;); | ||||
|  | ||||
|     if (m_projectPart.qtVersion != CppTools::ProjectPart::NoQt) { | ||||
|         const QString wrappedQtCoreHeaderPath = wrappedQtHeadersPath + "/QtCore"; | ||||
|         add("-I" + QDir::toNativeSeparators(wrappedQtHeadersPath)); | ||||
|         add("-I" + QDir::toNativeSeparators(wrappedQtCoreHeaderPath)); | ||||
|         add(includeDirOption() + QDir::toNativeSeparators(wrappedQtHeadersPath)); | ||||
|         add(includeDirOption() + QDir::toNativeSeparators(wrappedQtCoreHeaderPath)); | ||||
|     } | ||||
| } | ||||
|  | ||||
| @@ -154,12 +141,26 @@ void ClangCompilerOptionsBuilder::addExtraOptions() | ||||
|  | ||||
| QString ClangCompilerOptionsBuilder::clangIncludeDirectory() const | ||||
| { | ||||
|     QDir dir(creatorResourcePath() + "/clang/lib/clang/" + m_clangVersion + "/include"); | ||||
|  | ||||
|     QDir dir(creatorLibexecPath() + "/clang/lib/clang/" + m_clangVersion + "/include"); | ||||
|     if (!dir.exists() || !QFileInfo(dir, "stdint.h").exists()) | ||||
|         dir = QDir(m_clangResourceDirectory); | ||||
|     return QDir::toNativeSeparators(dir.canonicalPath()); | ||||
| } | ||||
|  | ||||
|     return dir.canonicalPath(); | ||||
| void ClangCompilerOptionsBuilder::undefineClangVersionMacrosForMsvc() | ||||
| { | ||||
|     if (m_projectPart.toolchainType == ProjectExplorer::Constants::MSVC_TOOLCHAIN_TYPEID) { | ||||
|         static QStringList macroNames { | ||||
|             "__clang__", | ||||
|             "__clang_major__", | ||||
|             "__clang_minor__", | ||||
|             "__clang_patchlevel__", | ||||
|             "__clang_version__" | ||||
|         }; | ||||
|  | ||||
|         foreach (const QString ¯oName, macroNames) | ||||
|             add(undefineOption() + macroName); | ||||
|     } | ||||
| } | ||||
|  | ||||
| } // namespace CppTools | ||||
|   | ||||
| @@ -34,33 +34,24 @@ namespace CppTools { | ||||
| class CPPTOOLS_EXPORT ClangCompilerOptionsBuilder : public CompilerOptionsBuilder | ||||
| { | ||||
| public: | ||||
|     static QStringList build(const ProjectPart *projectPart, | ||||
|                              ProjectFile::Kind fileKind, | ||||
|                              PchUsage pchUsage, | ||||
|                              const QString &clangVersion, | ||||
|                              const QString &clangResourceDirectory); | ||||
|     QStringList build(ProjectFile::Kind fileKind, | ||||
|                       PchUsage pchUsage); | ||||
|  | ||||
|     ClangCompilerOptionsBuilder(const ProjectPart &projectPart, | ||||
|                                 const QString &clangVersion, | ||||
|                                 const QString &clangResourceDirectory); | ||||
|                                 const QString &clangVersion = QString(), | ||||
|                                 const QString &clangResourceDirectory = QString()); | ||||
|  | ||||
|     virtual void addPredefinedHeaderPathsOptions(); | ||||
|     virtual void addExtraOptions(); | ||||
|  | ||||
|     bool excludeHeaderPath(const QString &path) const override; | ||||
|  | ||||
|     void addPredefinedMacrosAndHeaderPathsOptions(); | ||||
|  | ||||
|     void addPredefinedMacrosAndHeaderPathsOptionsForMsvc(); | ||||
|  | ||||
|     void addPredefinedMacrosAndHeaderPathsOptionsForNonMsvc(); | ||||
|  | ||||
|     void addWrappedQtHeadersIncludePath(); | ||||
|  | ||||
|     virtual void addWrappedQtHeadersIncludePath(); | ||||
|     void addProjectConfigFileInclude(); | ||||
|  | ||||
|     void addExtraOptions(); | ||||
|     void undefineClangVersionMacrosForMsvc(); | ||||
| private: | ||||
|     QString clangIncludeDirectory() const; | ||||
|  | ||||
| private: | ||||
|     QString m_clangVersion; | ||||
|     QString m_clangResourceDirectory; | ||||
| }; | ||||
|   | ||||
| @@ -125,7 +125,7 @@ void CompilerOptionsBuilder::addPrecompiledHeaderOptions(PchUsage pchUsage) | ||||
|     m_options.append(result); | ||||
| } | ||||
|  | ||||
| void CompilerOptionsBuilder::addToolchainAndProjectDefines() | ||||
| void CompilerOptionsBuilder::addToolchainAndProjectMacros() | ||||
| { | ||||
|     addMacros(m_projectPart.toolChainMacros); | ||||
|     addMacros(m_projectPart.projectMacros); | ||||
| @@ -258,21 +258,6 @@ void CompilerOptionsBuilder::addOptionsForLanguage(bool checkForBorlandExtension | ||||
|     m_options.append(opts); | ||||
| } | ||||
|  | ||||
| void CompilerOptionsBuilder::addDefineToAvoidIncludingGccOrMinGwIntrinsics() | ||||
| { | ||||
|     // In gcc headers, lots of built-ins are referenced that clang does not understand. | ||||
|     // Therefore, prevent the inclusion of the header that references them. Of course, this | ||||
|     // will break if code actually requires stuff from there, but that should be the less common | ||||
|     // case. | ||||
|  | ||||
|     const Core::Id type = m_projectPart.toolchainType; | ||||
|     if (type == ProjectExplorer::Constants::MINGW_TOOLCHAIN_TYPEID | ||||
|             || type == ProjectExplorer::Constants::GCC_TOOLCHAIN_TYPEID) { | ||||
|         addDefine({"_X86INTRIN_H_INCLUDED"}); | ||||
|         addDefine({"BOOST_UUID_NO_SIMD"}); | ||||
|     } | ||||
| } | ||||
|  | ||||
| static QByteArray toMsCompatibilityVersionFormat(const QByteArray &mscFullVer) | ||||
| { | ||||
|     return mscFullVer.left(2) | ||||
|   | ||||
| @@ -54,13 +54,11 @@ public: | ||||
|     virtual void enableExceptions(); | ||||
|     void addHeaderPathOptions(); | ||||
|     void addPrecompiledHeaderOptions(PchUsage pchUsage); | ||||
|     void addToolchainAndProjectDefines(); | ||||
|     void addToolchainAndProjectMacros(); | ||||
|     void addMacros(const ProjectExplorer::Macros ¯os); | ||||
|     virtual void addLanguageOption(ProjectFile::Kind fileKind); | ||||
|     virtual void addOptionsForLanguage(bool checkForBorlandExtensions = true); | ||||
|  | ||||
|     void addDefineToAvoidIncludingGccOrMinGwIntrinsics(); | ||||
|  | ||||
|     void addMsvcCompatibilityVersion(); | ||||
|     void undefineCppLanguageFeatureMacrosForMsvc2015(); | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user