Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Index: InitHeaderSearch.cpp
- ===================================================================
- --- InitHeaderSearch.cpp (revision 160614)
- +++ InitHeaderSearch.cpp (working copy)
- @@ -28,6 +28,12 @@
- #include "clang/Config/config.h" // C_INCLUDE_DIRS
- +#ifdef __MINGW32__
- +#include <iostream>
- +#include <fstream>
- +#include <Windows.h>
- +#endif
- +
- using namespace clang;
- using namespace clang::frontend;
- @@ -46,6 +52,14 @@
- std::string IncludeSysroot;
- bool IsNotEmptyOrRoot;
- +#ifdef __MINGW32__
- + std::string mingwGCCPath;
- + std::string mingwArch;
- + std::string mingwGCCVersion;
- + void ParseMinGWGCCInstallation(const char * gccPath);
- + void DetectMinGWInstallation();
- + bool PathFileExistsA(const char * pszPath);
- +#endif
- public:
- InitHeaderSearch(HeaderSearch &HS, bool verbose, StringRef sysroot)
- @@ -101,6 +115,7 @@
- /// Realize - Merges all search path lists into one list and send it to
- /// HeaderSearch.
- void Realize(const LangOptions &Lang);
- +
- };
- } // end anonymous namespace.
- @@ -381,25 +396,11 @@
- AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.2");
- break;
- case llvm::Triple::MinGW32:
- + DetectMinGWInstallation();
- // mingw-w64 C++ include paths (i686-w64-mingw32 and x86_64-w64-mingw32)
- - AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.0");
- - AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.1");
- - AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.2");
- - AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.3");
- - AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.4");
- - AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.0");
- - AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.1");
- - AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.2");
- - AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.3");
- - AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.7.0");
- + AddMinGW64CXXPaths(HSOpts.ResourceDir, mingwGCCVersion);
- // mingw.org C++ include paths
- - AddMinGWCPlusPlusIncludePaths("/mingw/lib/gcc", "mingw32", "4.5.2"); //MSYS
- - AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.6.2");
- - AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.6.1");
- - AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.5.2");
- - AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.5.0");
- - AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0");
- - AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0");
- + AddMinGWCPlusPlusIncludePaths(mingwGCCPath + "/../lib/gcc", mingwArch, mingwGCCVersion);
- break;
- case llvm::Triple::DragonFly:
- AddPath("/usr/include/c++/4.1", CXXSystem, true, false, false);
- @@ -654,6 +655,191 @@
- }
- }
- +#ifdef __MINGW32__
- +void InitHeaderSearch::ParseMinGWGCCInstallation(const char * gccPath)
- +{
- + // get gcc target and version
- + char szCmdLine[MAX_PATH] = {0};
- + strcpy(szCmdLine, gccPath);
- + strcat(szCmdLine, " -v");
- +
- + HANDLE hStdInRead;
- + HANDLE hStdInWrite;
- + HANDLE hStdOutRead;
- + HANDLE hStdOutWrite;
- + HANDLE hStdErrWrite;
- +
- + SECURITY_ATTRIBUTES sa ;
- + sa.nLength = sizeof( SECURITY_ATTRIBUTES ) ;
- + sa.lpSecurityDescriptor = NULL ;
- + sa.bInheritHandle = TRUE ;
- +
- + if (!CreatePipe(&hStdInRead, &hStdInWrite,&sa, 0))
- + return;
- +
- + if (!CreatePipe(&hStdOutRead, &hStdOutWrite,&sa, 0))
- + return;
- +
- + if (!DuplicateHandle(GetCurrentProcess(), hStdOutWrite, GetCurrentProcess(), &hStdErrWrite, 0, TRUE, DUPLICATE_SAME_ACCESS))
- + return;
- +
- + STARTUPINFOA si = {0};
- + si.cb = sizeof(si);
- + si.dwFlags = STARTF_USESTDHANDLES;
- + si.dwFlags |= STARTF_USESHOWWINDOW;
- + si.hStdError = hStdErrWrite;
- + si.hStdOutput = hStdOutWrite;
- + si.hStdInput = hStdInRead;
- + si.wShowWindow = SW_HIDE;
- +
- + PROCESS_INFORMATION pi;
- + char szWorkingDir[MAX_PATH] = {0};
- + strcpy(szWorkingDir, gccPath);
- + char *p = strrchr(szWorkingDir, '\\');
- + p++;
- + *p = 0;
- + BOOL bSuccess = CreateProcessA(NULL,
- + szCmdLine,
- + NULL,
- + NULL,
- + TRUE,
- + 0,
- + NULL,
- + szWorkingDir,
- + &si,
- + &pi
- + );
- + if (bSuccess)
- + {
- + DWORD bytesRead = 0 ;
- + std::string output;
- + WaitForSingleObject(pi.hProcess, INFINITE);
- + while ( true )
- + {
- + char buffer[512] = { 0 } ;
- + bSuccess = ReadFile( hStdOutRead , buffer , 512 , &bytesRead , NULL );
- + output.append(buffer, bytesRead);
- + if( bSuccess == FALSE || bytesRead < 512 )
- + break;
- + }
- +
- + // parse the output
- + size_t targetPos = output.find("Target:", 0);
- + if (targetPos != std::string::npos)
- + {
- + // extract the target info
- + char * start = const_cast<char*>(output.c_str());
- + start = start + targetPos + strlen("Target:");
- + while(!isalnum(*start))start++;
- + char * end = start+1;
- + while(*end != '\r' && *end != '\n')end++;
- + mingwArch.append(start, end - start);
- + }
- + size_t versionPos = output.find("gcc version");
- + if (versionPos != std::string::npos)
- + {
- + // extract the version info
- + char * start = const_cast<char*>(output.c_str());
- + start = start + versionPos + strlen("gcc version");
- + while(!isalnum(*start))start++;
- + char * end = start+1;
- + while(*end != ' ')end++;
- + mingwGCCVersion.append(start, end - start);
- + }
- +
- + // extract the path info
- + const char *p = strrchr(gccPath , '\\');
- + mingwGCCPath.append(gccPath, p - gccPath);
- + }
- + CloseHandle( hStdInRead ) ;
- + CloseHandle(hStdInWrite);
- + CloseHandle( hStdOutRead ) ;
- + CloseHandle(hStdOutWrite);
- + CloseHandle( hStdErrWrite ) ;
- + CloseHandle(pi.hThread);
- + CloseHandle(pi.hProcess);
- +
- +}
- +
- +void InitHeaderSearch::DetectMinGWInstallation()
- +{
- + if (!(mingwArch.empty() && mingwGCCPath.empty() && mingwGCCVersion.empty()))
- + return;
- +
- + char szPath[MAX_PATH] = {0};
- + // application directory
- + GetModuleFileNameA(NULL, szPath, MAX_PATH);
- + char* p = strrchr(szPath, '\\');
- + p++;
- + strcpy(p, "gcc.exe");
- + if (PathFileExistsA(szPath))
- + {
- + ParseMinGWGCCInstallation(szPath);
- + return;
- + }
- +
- + // in path environment
- + char * pPath = getenv ("PATH");
- + if (pPath)
- + {
- + size_t len = strlen(pPath);
- + char * pszPathEnv = new char[len + 1];
- + strcpy(pszPathEnv, pPath);
- + // split by ;
- + char * dirInPath = strtok( pszPathEnv, ";" );
- +
- + do
- + {
- + strcpy(szPath, dirInPath);
- + strcat(szPath, "\\gcc.exe");
- +
- + if (PathFileExistsA(szPath))
- + {
- + ParseMinGWGCCInstallation(szPath);
- + delete[] pszPathEnv;
- + return;
- + }
- + } while ((dirInPath = strtok( NULL, ";" )) != NULL);
- +
- + delete[] pszPathEnv;
- + }
- +
- + // in Drives root path
- + DWORD dwDrives = ::GetLogicalDrives();
- + dwDrives >>= 2; // start from C drive
- + for (char i = 0; i < 30; i++)
- + {
- + char A = 'C';
- + if (dwDrives & 0x01)
- + {
- + A += i;
- + szPath[0] = A;
- + szPath[1] = 0;
- + strcat(szPath, ":\\mingw\\bin\\gcc.exe");
- +
- + if (PathFileExistsA(szPath))
- + {
- + ParseMinGWGCCInstallation(szPath);
- + return;
- + }
- + }
- + dwDrives >>= 1;
- + }
- +}
- +bool InitHeaderSearch::PathFileExistsA(const char * pszPath)
- +{
- + std::fstream _file;
- + _file.open(pszPath,std::ios::in);
- + if(!_file)
- + {
- + return false;
- + }
- +
- + _file.close();
- + return true;
- +}
- +#endif
- +
- void clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
- const HeaderSearchOptions &HSOpts,
- const LangOptions &Lang,
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement