1 /*============================================================================
2 CMake - Cross Platform Makefile Generator
3 Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
5 Distributed under the OSI-approved BSD License (the "License");
6 see accompanying file Copyright.txt for details.
8 This software is distributed WITHOUT ANY WARRANTY; without even the
9 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License for more information.
11 ============================================================================*/
12 #include "cmUseMangledMesaCommand.h"
13 #include "cmSystemTools.h"
15 #include <cmsys/RegularExpression.hxx>
17 // cmUseMangledMesaCommand
18 bool cmUseMangledMesaCommand
19 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
21 // expected two arguments:
22 // arguement one: the full path to gl_mangle.h
23 // arguement two : directory for output of edited headers
26 this->SetError("called with incorrect number of arguments");
29 const char* inputDir = args[0].c_str();
30 std::string glh = inputDir;
33 if(!cmSystemTools::FileExists(glh.c_str()))
35 std::string e = "Bad path to Mesa, could not find: ";
38 this->SetError(e.c_str());
41 const char* destDir = args[1].c_str();
42 std::vector<std::string> files;
43 cmSystemTools::Glob(inputDir, "\\.h$", files);
46 cmSystemTools::Error("Could not open Mesa Directory ", inputDir);
49 cmSystemTools::MakeDirectory(destDir);
50 for(std::vector<std::string>::iterator i = files.begin();
51 i != files.end(); ++i)
53 std::string path = inputDir;
56 this->CopyAndFullPathMesaHeader(path.c_str(), destDir);
63 cmUseMangledMesaCommand::
64 CopyAndFullPathMesaHeader(const char* source,
67 std::string dir, file;
68 cmSystemTools::SplitProgramPath(source, dir, file);
69 std::string outFile = outdir;
72 std::string tempOutputFile = outFile;
73 tempOutputFile += ".tmp";
74 std::ofstream fout(tempOutputFile.c_str());
77 cmSystemTools::Error("Could not open file for write in copy operation: ",
78 tempOutputFile.c_str(), outdir);
79 cmSystemTools::ReportLastSystemError("");
82 std::ifstream fin(source);
85 cmSystemTools::Error("Could not open file for read in copy operation",
89 // now copy input to output and expand variables in the
90 // input file at the same time
92 // regular expression for any #include line
93 cmsys::RegularExpression includeLine(
94 "^[ \t]*#[ \t]*include[ \t]*[<\"]([^\">]+)[\">]");
95 // regular expression for gl/ or GL/ in a file (match(1) of above)
96 cmsys::RegularExpression glDirLine("(gl|GL)(/|\\\\)([^<\"]+)");
97 // regular expression for gl GL or xmesa in a file (match(1) of above)
98 cmsys::RegularExpression glLine("(gl|GL|xmesa)");
99 while(cmSystemTools::GetLineFromStream(fin,inLine))
101 if(includeLine.find(inLine.c_str()))
103 std::string includeFile = includeLine.match(1);
104 if(glDirLine.find(includeFile.c_str()))
106 std::string gfile = glDirLine.match(3);
107 fout << "#include \"" << outdir << "/" << gfile.c_str() << "\"\n";
109 else if(glLine.find(includeFile.c_str()))
111 fout << "#include \"" << outdir << "/" <<
112 includeLine.match(1).c_str() << "\"\n";
116 fout << inLine << "\n";
121 fout << inLine << "\n";
124 // close the files before attempting to copy
127 cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(),
129 cmSystemTools::RemoveFile(tempOutputFile.c_str());