From 1db8fe5d80496be390e19d31ca9c8b87ed7579a4 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Wed, 6 Oct 2021 10:58:53 +0200 Subject: 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 Pick-to: 6.2 Change-Id: I988bb9e963f6205a6433d3e48c05b7a8823f8398 Reviewed-by: Qt CI Bot Reviewed-by: Cristian Maureira-Fredes --- .../ApiExtractor/clangparser/clangbuilder.cpp | 40 +++--- .../ApiExtractor/clangparser/compilersupport.cpp | 156 ++++++++------------- .../ApiExtractor/clangparser/compilersupport.h | 15 ++ 3 files changed, 93 insertions(+), 118 deletions(-) diff --git a/sources/shiboken6/ApiExtractor/clangparser/clangbuilder.cpp b/sources/shiboken6/ApiExtractor/clangparser/clangbuilder.cpp index 6f5a4d47f..5ea4af308 100644 --- a/sources/shiboken6/ApiExtractor/clangparser/clangbuilder.cpp +++ b/sources/shiboken6/ApiExtractor/clangparser/clangbuilder.cpp @@ -830,24 +830,30 @@ bool BuilderPrivate::visitHeader(const QString &fileName) const || baseName == u"stddef.h") { // size_t return true; } -#ifdef Q_OS_LINUX - if (fileName == u"/usr/include/stdlib.h" - || baseName == u"types.h" - || baseName == u"stdint-intn.h" // int32_t - || baseName == u"stdint-uintn.h") { // uint32_t - 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" + || baseName == u"types.h" + || baseName == u"stdint-intn.h" // int32_t + || baseName == u"stdint-uintn.h") { // uint32_t + 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.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 = 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 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 -- cgit v1.2.3