1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
/**
* Program to copy and patch MSVC header files to work with GCC-XML.
*/
#include "gxSystemTools.h"
#include <direct.h>
#include <errno.h>
#include <fstream>
#include <iostream>
#include <process.h>
#include <string>
#include <vector>
#include <windows.h>
bool InstallSupport(const char* patchCommand, const char* catCommand,
const char* patchFile, const char* sourcePath,
const char* destPath);
bool FindTool(const char* vcDir, const char* name, std::string& result);
//----------------------------------------------------------------------------
int main(int argc, char* argv[])
{
if(argc < 2)
{
std::cout << "Usage:" << std::endl
<< " " << argv[0]
<< " patch_dir [gccxml_root] [timestamp_file]" << std::endl;
return 0;
}
std::string patchDir = argv[1];
std::string gccxmlRoot = ".";
std::string timestamp;
if(argc >= 3)
{
gccxmlRoot = argv[2];
// Make sure the output directory exists.
gxSystemTools::MakeDirectory(gccxmlRoot.c_str());
}
if(argc >= 4)
{
timestamp = argv[3];
}
// Clean up the paths.
patchDir = gxSystemTools::CollapseDirectory(patchDir.c_str());
gccxmlRoot = gxSystemTools::CollapseDirectory(gccxmlRoot.c_str());
gxSystemTools::ConvertToUnixSlashes(patchDir);
gxSystemTools::ConvertToUnixSlashes(gccxmlRoot);
// The registry keys for MSVC install detection.
const char* vc6Registry =
"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"
"DevStudio\\6.0\\Products\\Microsoft Visual C++;ProductDir";
const char* vc7Registry =
"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\7.0;InstallDir";
const char* vc71Registry =
"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\7.1;InstallDir";
const char* vc8Registry =
"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\8.0;InstallDir";
// Check which versions of MSVC are installed.
std::string msvc6;
std::string msvc7;
std::string msvc71;
std::string msvc8;
bool have6 = gxSystemTools::ReadRegistryValue(vc6Registry, msvc6);
bool have7 = gxSystemTools::ReadRegistryValue(vc7Registry, msvc7);
bool have71 = gxSystemTools::ReadRegistryValue(vc71Registry, msvc71);
bool have8 = false;
// Look for a VS8 that is not the beta release.
if(gxSystemTools::ReadRegistryValue(vc8Registry, msvc8))
{
// The "CLR Version" registry entry in VS 8 has value "v2.0.40607"
// for the beta and "v2.0.50727" for the release.
const char* vc8RegistryVersion =
"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\8.0;CLR Version";
std::string version;
if(gxSystemTools::ReadRegistryValue(vc8RegistryVersion, version))
{
int vnum;
if((sscanf(version.c_str(), "v2.0.%d", &vnum) == 1) &&
vnum >= 50727)
{
have8 = true;
}
}
}
// See if there is anything to do.
if(!have6 && !have7 && !have71 && !have8)
{
std::cout << "None of MSVC 6, 7, 7.1, or 8 is installed.\n";
}
// Need to install at least one of the support directories. We need
// to find the cat and patch executables.
std::string patchCommand;
if(!FindTool(patchDir.c_str(), "patch", patchCommand) &&
(have6||have7||have71||have8))
{
std::cerr << "Cannot find patch executable.\n";
return 1;
}
std::string catCommand;
if(!FindTool(patchDir.c_str(), "cat", catCommand) &&
(have6||have7||have71||have8))
{
std::cerr << "Cannot find cat executable.\n";
return 1;
}
int result = 0;
if(have6)
{
msvc6 += "/Include";
std::string patchFile = patchDir + "/vc6Include.patch";
std::string destPath = gccxmlRoot+"/Vc6/Include";
if(gxSystemTools::FileExists(patchFile.c_str()))
{
if(!InstallSupport(patchCommand.c_str(), catCommand.c_str(),
patchFile.c_str(), msvc6.c_str(), destPath.c_str()))
{
result = 1;
}
}
else
{
std::cerr << "Have MSVC 6, but cannot find vc6Include.patch.\n";
result = 1;
}
}
if(have7)
{
std::string msvc7i = msvc7 + "/../../Vc7/Include";
std::string msvc7p = msvc7 + "/../../Vc7/PlatformSDK/Include";
msvc7i = gxSystemTools::CollapseDirectory(msvc7i.c_str());
msvc7p = gxSystemTools::CollapseDirectory(msvc7p.c_str());
std::string patchI = patchDir + "/vc7Include.patch";
std::string patchP = patchDir + "/vc7PlatformSDK.patch";
std::string destPathI = gccxmlRoot+"/Vc7/Include";
std::string destPathP = gccxmlRoot+"/Vc7/PlatformSDK";
if(gxSystemTools::FileExists(patchI.c_str()))
{
if(!InstallSupport(patchCommand.c_str(), catCommand.c_str(),
patchI.c_str(), msvc7i.c_str(), destPathI.c_str()))
{
result = 1;
}
}
else
{
std::cerr << "Have MSVC 7, but cannot find vc7Include.patch.\n";
result = 1;
}
if(gxSystemTools::FileExists(patchP.c_str()))
{
if(!InstallSupport(patchCommand.c_str(), catCommand.c_str(),
patchP.c_str(), msvc7p.c_str(), destPathP.c_str()))
{
result = 1;
}
}
else
{
std::cerr << "Have MSVC 7, but cannot find vc7PlatformSDK.patch.\n";
result = 1;
}
}
if(have71)
{
std::string msvc71i = msvc71 + "/../../Vc7/Include";
std::string msvc71p = msvc71 + "/../../Vc7/PlatformSDK/Include";
msvc71i = gxSystemTools::CollapseDirectory(msvc71i.c_str());
msvc71p = gxSystemTools::CollapseDirectory(msvc71p.c_str());
std::string patchI = patchDir + "/vc71Include.patch";
std::string patchP = patchDir + "/vc71PlatformSDK.patch";
std::string destPathI = gccxmlRoot+"/Vc71/Include";
std::string destPathP = gccxmlRoot+"/Vc71/PlatformSDK";
if(gxSystemTools::FileExists(patchI.c_str()))
{
if(!InstallSupport(patchCommand.c_str(), catCommand.c_str(),
patchI.c_str(), msvc71i.c_str(), destPathI.c_str()))
{
result = 1;
}
}
else
{
std::cerr << "Have MSVC 7.1, but cannot find vc71Include.patch.\n";
result = 1;
}
if(gxSystemTools::FileExists(patchP.c_str()))
{
if(!InstallSupport(patchCommand.c_str(), catCommand.c_str(),
patchP.c_str(), msvc71p.c_str(), destPathP.c_str()))
{
result = 1;
}
}
else
{
std::cerr << "Have MSVC 7.1, but cannot find vc71PlatformSDK.patch.\n";
result = 1;
}
}
if(have8)
{
std::string msvc8i = msvc8 + "/../../Vc/Include";
std::string msvc8p = msvc8 + "/../../Vc/PlatformSDK/Include";
msvc8i = gxSystemTools::CollapseDirectory(msvc8i.c_str());
msvc8p = gxSystemTools::CollapseDirectory(msvc8p.c_str());
std::string patchI = patchDir + "/vc8Include.patch";
std::string patchP = patchDir + "/vc8PlatformSDK.patch";
std::string destPathI = gccxmlRoot+"/Vc8/Include";
std::string destPathP = gccxmlRoot+"/Vc8/PlatformSDK";
if(gxSystemTools::FileExists(patchI.c_str()))
{
if(!InstallSupport(patchCommand.c_str(), catCommand.c_str(),
patchI.c_str(), msvc8i.c_str(), destPathI.c_str()))
{
result = 1;
}
}
else
{
std::cerr << "Have MSVC 8, but cannot find vc8Include.patch.\n";
result = 1;
}
if(gxSystemTools::FileExists(patchP.c_str()))
{
if(!InstallSupport(patchCommand.c_str(), catCommand.c_str(),
patchP.c_str(), msvc8p.c_str(), destPathP.c_str()))
{
result = 1;
}
}
else
{
std::cerr << "Have MSVC 8, but cannot find vc8PlatformSDK.patch.\n";
result = 1;
}
}
// If we succeeded, write the timestamp file.
if(result == 0 && (timestamp.length() > 0))
{
std::ofstream tfile(timestamp.c_str(), std::ios::out | std::ios::binary);
tfile << "int main() { return 0; }\n";
if(!tfile)
{
std::cerr << "Error writing timestamp file \""
<< timestamp.c_str() << "\".\n";
result = 1;
}
}
return result;
}
//----------------------------------------------------------------------------
bool InstallSupport(const char* patchCommand, const char* catCommand,
const char* patchFile, const char* sourcePath,
const char* destPath)
{
// Look at the patch file to see what headers need to be copied.
std::ifstream patch(patchFile);
if(!patch)
{
std::cerr << "Error opening patch file " << patchFile << std::endl;
return false;
}
// Make sure the destination path exists before trying to put files
// there.
gxSystemTools::MakeDirectory(destPath);
// Copy the files over before patching.
char buf[4096];
for(patch.getline(buf, 4096); !patch.eof(); patch.getline(buf, 4096))
{
std::string line = buf;
if(line.substr(0,6) == "Index:")
{
std::string source = sourcePath;
source += "/"+line.substr(7);
std::string dest = destPath;
dest += "/"+line.substr(7);
gxSystemTools::FileCopy(source.c_str(), dest.c_str());
}
}
std::string cmd = catCommand;
if(cmd.find(" ") != cmd.npos)
{
if(gxSystemTools::GetShortPath(catCommand, cmd))
{
catCommand = cmd.c_str();
}
}
std::string patchCmd = gxSystemTools::ConvertToOutputPath(catCommand);
patchCmd += " ";
patchCmd += gxSystemTools::ConvertToOutputPath(patchFile);
patchCmd += " | ";
patchCmd += gxSystemTools::ConvertToOutputPath(patchCommand);
patchCmd += " -p0 -t -d ";
patchCmd += gxSystemTools::ConvertToOutputPath(destPath);
// Patch the copies of the header files.
std::cout << "Executing " << patchCmd.c_str() << std::endl;
std::string output;
int retVal;
if(gxSystemTools::RunCommand(patchCmd.c_str(), output, retVal) &&
(retVal == 0))
{
return true;
}
std::cerr << "Error running patch. Output is ["
<< output.c_str() << "]\n";
return false;
}
//----------------------------------------------------------------------------
bool FindTool(const char* vcDir, const char* name, std::string& result)
{
// check for executable in the source directory
std::string command = vcDir;
command += "/vc";
command += name;
command += ".exe";
if(gxSystemTools::FileExists(command.c_str()))
{
result = command;
return true;
}
// Find the executable.
command = name;
command += ".exe";
if(gxSystemTools::FileExists(command.c_str()))
{
result = command;
return true;
}
else
{
// The registry key to use to find the executable from cygwin.
const char* cygwinRegistry1 =
"HKEY_LOCAL_MACHINE\\SOFTWARE\\Cygnus Solutions\\Cygwin\\mounts v2\\/usr/bin;native";
const char* cygwinRegistry2 =
"HKEY_CURRENT_USER\\Software\\Cygnus Solutions\\Cygwin\\mounts v2\\/usr/bin;native";
if((gxSystemTools::ReadRegistryValue(cygwinRegistry1, command) ||
gxSystemTools::ReadRegistryValue(cygwinRegistry2, command)) &&
gxSystemTools::FileExists((command+"/"+name+".exe").c_str()))
{
// Found the binary location from cygwin's registry entry.
command += "/";
command += name;
command += ".exe";
result = command;
return true;
}
else
{
// Try to find it in the path.
command = gxSystemTools::FindProgram(name);
if(command.length() > 0)
{
result = command;
return true;
}
}
}
return false;
}
|