diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-10-06 10:58:53 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-10-08 17:03:45 +0200 |
commit | ccd22d53e1403e9ecd9665e531555ecae9f04a6f (patch) | |
tree | 4e02033079fb97ef6e1fa3ec5c680517fac2426e | |
parent | b4ba0418f107a12b3d74ac8c45c1e292d7e0d604 (diff) |
shiboken6/Clang parser: Replace #ifdefery for compiler/platform by variables
Introduce enumerations for compiler and platform. Add variables,
initialize them by #ifdefs and use accessor functions instead of
versions. This lays the groundwork for cross-compiling.
Task-number: PYSIDE-802
Change-Id: I988bb9e963f6205a6433d3e48c05b7a8823f8398
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
(cherry picked from commit 1db8fe5d80496be390e19d31ca9c8b87ed7579a4)
3 files changed, 92 insertions, 118 deletions
diff --git a/sources/shiboken6/ApiExtractor/clangparser/clangbuilder.cpp b/sources/shiboken6/ApiExtractor/clangparser/clangbuilder.cpp index 71c474391..f583aba99 100644 --- a/sources/shiboken6/ApiExtractor/clangparser/clangbuilder.cpp +++ b/sources/shiboken6/ApiExtractor/clangparser/clangbuilder.cpp @@ -827,24 +827,29 @@ bool BuilderPrivate::visitHeader(const QString &fileName) const const QString baseName = clang::baseName(fileName); if (baseName == u"gl.h") return true; -#if defined(Q_OS_LINUX) || defined(Q_OS_MACOS) - if (fileName == u"/usr/include/stdint.h") - return true; -#endif -#ifdef Q_OS_LINUX - if (fileName == u"/usr/include/stdlib.h" || baseName == u"types.h") - return true; -#endif // Q_OS_LINUX -#ifdef Q_OS_MACOS - // Parse the following system headers to get the correct typdefs for types like - // int32_t, which are used in the macOS implementation of OpenGL framework. - if (baseName == u"gltypes.h" - || fileName.startsWith(u"/usr/include/_types") - || fileName.startsWith(u"/usr/include/_types") - || fileName.startsWith(u"/usr/include/sys/_types")) { - return true; + + switch (clang::platform()) { + case Platform::Unix: + if (fileName == u"/usr/include/stdlib.h" + || fileName == u"/usr/include/stdint.h" + || baseName == u"types.h") + return true; + break; + case Platform::macOS: + // Parse the following system headers to get the correct typdefs for types like + // int32_t, which are used in the macOS implementation of OpenGL framework. + if (baseName == u"gltypes.h" + || fileName == u"/usr/include/stdint.h" + || fileName.startsWith(u"/usr/include/_types") + || fileName.startsWith(u"/usr/include/_types") + || fileName.startsWith(u"/usr/include/sys/_types")) { + return true; + } + break; + default: + break; } -#endif // Q_OS_MACOS + for (const auto &systemInclude : m_systemIncludes) { if (systemInclude == baseName) return true; diff --git a/sources/shiboken6/ApiExtractor/clangparser/compilersupport.cpp b/sources/shiboken6/ApiExtractor/clangparser/compilersupport.cpp index 7631916fb..fb42b78f0 100644 --- a/sources/shiboken6/ApiExtractor/clangparser/compilersupport.cpp +++ b/sources/shiboken6/ApiExtractor/clangparser/compilersupport.cpp @@ -53,6 +53,28 @@ QVersionNumber libClangVersion() return QVersionNumber(CINDEX_VERSION_MAJOR, CINDEX_VERSION_MINOR); } +static Compiler _compiler = +#if defined (Q_CC_CLANG) + Compiler::Clang; +#elif defined (Q_CC_MSVC) + Compiler::Msvc; +#else + Compiler::Gpp; +#endif + +Compiler compiler() { return _compiler; } + +static Platform _platform = +#if defined (Q_OS_DARWIN) + Platform::macOS; +#elif defined (Q_OS_WIN) + Platform::Windows; +#else + Platform::Unix; +#endif + +Platform platform() { return _platform; } + static bool runProcess(const QString &program, const QStringList &arguments, QByteArray *stdOutIn = nullptr, QByteArray *stdErrIn = nullptr) { @@ -91,11 +113,8 @@ static bool runProcess(const QString &program, const QStringList &arguments, return true; } -#if defined(Q_CC_GNU) - static QByteArray frameworkPath() { return QByteArrayLiteral(" (framework directory)"); } -# if defined(Q_OS_MACOS) static void filterHomebrewHeaderPaths(HeaderPaths &headerPaths) { QByteArray homebrewPrefix = qgetenv("HOMEBREW_OPT"); @@ -123,7 +142,6 @@ static void filterHomebrewHeaderPaths(HeaderPaths &headerPaths) } } } -# endif // Determine g++'s internal include paths from the output of // g++ -E -x c++ - -v </dev/null @@ -161,12 +179,11 @@ static HeaderPaths gppInternalIncludePaths(const QString &compiler) } } -# if defined(Q_OS_MACOS) - filterHomebrewHeaderPaths(result); -# endif + if (platform() == Platform::macOS) + filterHomebrewHeaderPaths(result); + return result; } -#endif // Q_CC_MSVC // Detect Vulkan as supported from Qt 5.10 by checking the environment variables. static void detectVulkan(HeaderPaths *headerPaths) @@ -181,40 +198,6 @@ static void detectVulkan(HeaderPaths *headerPaths) } } -#if defined(Q_CC_GNU) -enum class LinuxDistribution { RedHat, CentOs, Other }; - -static LinuxDistribution linuxDistribution() -{ - const QString &productType = QSysInfo::productType(); - if (productType == QLatin1String("rhel")) - return LinuxDistribution::RedHat; - if (productType.compare(QLatin1String("centos"), Qt::CaseInsensitive) == 0) - return LinuxDistribution::CentOs; - return LinuxDistribution::Other; -} - -static bool checkProductVersion(const QVersionNumber &minimum, - const QVersionNumber &excludedMaximum) -{ - const QVersionNumber osVersion = QVersionNumber::fromString(QSysInfo::productVersion()); - return osVersion.isNull() || (osVersion >= minimum && osVersion < excludedMaximum); -} - -static inline bool needsGppInternalHeaders() -{ - const LinuxDistribution distro = linuxDistribution(); - switch (distro) { - case LinuxDistribution::RedHat: - case LinuxDistribution::CentOs: - return checkProductVersion(QVersionNumber(6, 10), QVersionNumber(8)); - case LinuxDistribution::Other: - break; - } - return false; -} -#endif // Q_CC_GNU - // For MSVC, we set the MS compatibility version and let Clang figure out its own // options and include paths. // For the others, we pass "-nostdinc" since libclang tries to add it's own system @@ -222,9 +205,7 @@ static inline bool needsGppInternalHeaders() // which causes std types not being found and construct -I/-F options from the // include paths of the host compiler. -#ifdef Q_CC_CLANG static QByteArray noStandardIncludeOption() { return QByteArrayLiteral("-nostdinc"); } -#endif // The clang builtin includes directory is used to find the definitions for // intrinsic functions and builtin types. It is necessary to use the clang @@ -234,13 +215,12 @@ static QByteArray noStandardIncludeOption() { return QByteArrayLiteral("-nostdin // Besides g++/Linux, as of MSVC 19.28.29334, MSVC needs clang includes // due to PYSIDE-1433, LLVM-47099 -#if !defined(Q_OS_DARWIN) -# define NEED_CLANG_BUILTIN_INCLUDES 1 -#else -# define NEED_CLANG_BUILTIN_INCLUDES 0 -#endif -#if NEED_CLANG_BUILTIN_INCLUDES +static bool needsClangBuiltinIncludes() +{ + return platform() != Platform::macOS; +} + static QString findClangLibDir() { for (const char *envVar : {"LLVM_INSTALL_DIR", "CLANG_INSTALL_DIR"}) { @@ -293,22 +273,18 @@ static QString findClangBuiltInIncludesDir() } return QString(); } -#endif // NEED_CLANG_BUILTIN_INCLUDES -#if defined(Q_CC_CLANG) || defined(Q_CC_GNU) static QString compilerFromCMake(const QString &defaultCompiler) { // Added !defined(Q_OS_DARWIN) due to PYSIDE-1032 -# if defined(CMAKE_CXX_COMPILER) && !defined(Q_OS_DARWIN) - Q_UNUSED(defaultCompiler); - return QString::fromLocal8Bit(CMAKE_CXX_COMPILER); -# else - return defaultCompiler; -# endif + QString result = defaultCompiler; + if (platform() != Platform::macOS) +#ifdef CMAKE_CXX_COMPILER + result = QString::fromLocal8Bit(CMAKE_CXX_COMPILER); +#endif + return result; } -#endif // Q_CC_CLANG, Q_CC_GNU -#if NEED_CLANG_BUILTIN_INCLUDES static void appendClangBuiltinIncludes(HeaderPaths *p) { const QString clangBuiltinIncludesDir = @@ -324,54 +300,32 @@ static void appendClangBuiltinIncludes(HeaderPaths *p) HeaderType::System}); } } -#endif // NEED_CLANG_BUILTIN_INCLUDES // Returns clang options needed for emulating the host compiler QByteArrayList emulatedCompilerOptions() { -#if defined(Q_CC_GNU) - // Needed to silence a warning, but needsGppInternalHeaders is used below. - // This seems to be a compiler bug on macOS. - Q_UNUSED(needsGppInternalHeaders); -#endif QByteArrayList result; -#if defined(Q_CC_MSVC) - HeaderPaths headerPaths; - result.append(QByteArrayLiteral("-fms-compatibility-version=19.26.28806")); - result.append(QByteArrayLiteral("-fdelayed-template-parsing")); - result.append(QByteArrayLiteral("-Wno-microsoft-enum-value")); - // Fix yvals_core.h: STL1000: Unexpected compiler version, expected Clang 7 or newer (MSVC2017 update) - result.append(QByteArrayLiteral("-D_ALLOW_COMPILER_AND_STL_VERSION_MISMATCH")); -# if NEED_CLANG_BUILTIN_INCLUDES - appendClangBuiltinIncludes(&headerPaths); -# endif // NEED_CLANG_BUILTIN_INCLUDES - -#elif defined(Q_CC_CLANG) - HeaderPaths headerPaths = gppInternalIncludePaths(compilerFromCMake(QStringLiteral("clang++"))); - result.append(noStandardIncludeOption()); -#elif defined(Q_CC_GNU) HeaderPaths headerPaths; - -# if NEED_CLANG_BUILTIN_INCLUDES - appendClangBuiltinIncludes(&headerPaths); -# endif // NEED_CLANG_BUILTIN_INCLUDES - - // Append the c++ include paths since Clang is unable to find <list> etc - // on RHEL 7 with g++ 6.3 or CentOS 7.2. - // A fix for this has been added to Clang 5.0, so, the code can be removed - // once Clang 5.0 is the minimum version. - if (needsGppInternalHeaders()) { - const HeaderPaths gppPaths = gppInternalIncludePaths(compilerFromCMake(QStringLiteral("g++"))); - for (const HeaderPath &h : gppPaths) { - if (h.path.contains("c++") - || h.path.contains("sysroot")) { // centOS - headerPaths.append(h); - } - } + switch (compiler()) { + case Compiler::Msvc: + result.append(QByteArrayLiteral("-fms-compatibility-version=19.26.28806")); + result.append(QByteArrayLiteral("-fdelayed-template-parsing")); + result.append(QByteArrayLiteral("-Wno-microsoft-enum-value")); + // Fix yvals_core.h: STL1000: Unexpected compiler version, expected Clang 7 or newer (MSVC2017 update) + result.append(QByteArrayLiteral("-D_ALLOW_COMPILER_AND_STL_VERSION_MISMATCH")); + if (needsClangBuiltinIncludes()) + appendClangBuiltinIncludes(&headerPaths); + break; + case Compiler::Clang: + headerPaths.append(gppInternalIncludePaths(compilerFromCMake(u"clang++"_qs))); + result.append(noStandardIncludeOption()); + break; + case Compiler::Gpp: + if (needsClangBuiltinIncludes()) + appendClangBuiltinIncludes(&headerPaths); + break; } -#else - HeaderPaths headerPaths; -#endif + detectVulkan(&headerPaths); std::transform(headerPaths.cbegin(), headerPaths.cend(), std::back_inserter(result), HeaderPath::includeOption); diff --git a/sources/shiboken6/ApiExtractor/clangparser/compilersupport.h b/sources/shiboken6/ApiExtractor/clangparser/compilersupport.h index d9e213e73..479e211ce 100644 --- a/sources/shiboken6/ApiExtractor/clangparser/compilersupport.h +++ b/sources/shiboken6/ApiExtractor/clangparser/compilersupport.h @@ -42,6 +42,18 @@ enum class LanguageLevel { Cpp1Z }; +enum class Compiler { + Msvc, + Gpp, + Clang +}; + +enum class Platform { + Unix, + Windows, + macOS +}; + namespace clang { QVersionNumber libClangVersion(); @@ -50,6 +62,9 @@ LanguageLevel emulatedCompilerLanguageLevel(); const char *languageLevelOption(LanguageLevel l); LanguageLevel languageLevelFromOption(const char *); + +Compiler compiler(); +Platform platform(); } // namespace clang #endif // COMPILERSUPPORT_H |