ENH: add submit via cp mode
[cmake.git] / Source / cmUseMangledMesaCommand.cxx
blobe89cf38ea4fae29b9cb8f7cf1457316e7dccfaa7
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmUseMangledMesaCommand.cxx,v $
5 Language: C++
6 Date: $Date: 2008-01-23 15:27:59 $
7 Version: $Revision: 1.18 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #include "cmUseMangledMesaCommand.h"
18 #include "cmSystemTools.h"
20 #include <cmsys/RegularExpression.hxx>
22 // cmUseMangledMesaCommand
23 bool cmUseMangledMesaCommand
24 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
26 // expected two arguments:
27 // arguement one: the full path to gl_mangle.h
28 // arguement two : directory for output of edited headers
29 if(args.size() != 2)
31 this->SetError("called with incorrect number of arguments");
32 return false;
34 const char* inputDir = args[0].c_str();
35 std::string glh = inputDir;
36 glh += "/";
37 glh += "gl.h";
38 if(!cmSystemTools::FileExists(glh.c_str()))
40 std::string e = "Bad path to Mesa, could not find: ";
41 e += glh;
42 e += " ";
43 this->SetError(e.c_str());
44 return false;
46 const char* destDir = args[1].c_str();
47 std::vector<std::string> files;
48 cmSystemTools::Glob(inputDir, "\\.h$", files);
49 if(files.size() == 0)
51 cmSystemTools::Error("Could not open Mesa Directory ", inputDir);
52 return false;
54 cmSystemTools::MakeDirectory(destDir);
55 for(std::vector<std::string>::iterator i = files.begin();
56 i != files.end(); ++i)
58 std::string path = inputDir;
59 path += "/";
60 path += *i;
61 this->CopyAndFullPathMesaHeader(path.c_str(), destDir);
64 return true;
67 void
68 cmUseMangledMesaCommand::
69 CopyAndFullPathMesaHeader(const char* source,
70 const char* outdir)
72 std::string dir, file;
73 cmSystemTools::SplitProgramPath(source, dir, file);
74 std::string outFile = outdir;
75 outFile += "/";
76 outFile += file;
77 std::string tempOutputFile = outFile;
78 tempOutputFile += ".tmp";
79 std::ofstream fout(tempOutputFile.c_str());
80 if(!fout)
82 cmSystemTools::Error("Could not open file for write in copy operation: ",
83 tempOutputFile.c_str(), outdir);
84 cmSystemTools::ReportLastSystemError("");
85 return;
87 std::ifstream fin(source);
88 if(!fin)
90 cmSystemTools::Error("Could not open file for read in copy operation",
91 source);
92 return;
94 // now copy input to output and expand variables in the
95 // input file at the same time
96 std::string inLine;
97 // regular expression for any #include line
98 cmsys::RegularExpression includeLine(
99 "^[ \t]*#[ \t]*include[ \t]*[<\"]([^\">]+)[\">]");
100 // regular expression for gl/ or GL/ in a file (match(1) of above)
101 cmsys::RegularExpression glDirLine("(gl|GL)(/|\\\\)([^<\"]+)");
102 // regular expression for gl GL or xmesa in a file (match(1) of above)
103 cmsys::RegularExpression glLine("(gl|GL|xmesa)");
104 while(cmSystemTools::GetLineFromStream(fin,inLine))
106 if(includeLine.find(inLine.c_str()))
108 std::string includeFile = includeLine.match(1);
109 if(glDirLine.find(includeFile.c_str()))
111 std::string gfile = glDirLine.match(3);
112 fout << "#include \"" << outdir << "/" << gfile.c_str() << "\"\n";
114 else if(glLine.find(includeFile.c_str()))
116 fout << "#include \"" << outdir << "/" <<
117 includeLine.match(1).c_str() << "\"\n";
119 else
121 fout << inLine << "\n";
124 else
126 fout << inLine << "\n";
129 // close the files before attempting to copy
130 fin.close();
131 fout.close();
132 cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(),
133 outFile.c_str());
134 cmSystemTools::RemoveFile(tempOutputFile.c_str());