1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmUseMangledMesaCommand.cxx,v $
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
31 this->SetError("called with incorrect number of arguments");
34 const char* inputDir
= args
[0].c_str();
35 std::string glh
= inputDir
;
38 if(!cmSystemTools::FileExists(glh
.c_str()))
40 std::string e
= "Bad path to Mesa, could not find: ";
43 this->SetError(e
.c_str());
46 const char* destDir
= args
[1].c_str();
47 std::vector
<std::string
> files
;
48 cmSystemTools::Glob(inputDir
, "\\.h$", files
);
51 cmSystemTools::Error("Could not open Mesa Directory ", inputDir
);
54 cmSystemTools::MakeDirectory(destDir
);
55 for(std::vector
<std::string
>::iterator i
= files
.begin();
56 i
!= files
.end(); ++i
)
58 std::string path
= inputDir
;
61 this->CopyAndFullPathMesaHeader(path
.c_str(), destDir
);
68 cmUseMangledMesaCommand::
69 CopyAndFullPathMesaHeader(const char* source
,
72 std::string dir
, file
;
73 cmSystemTools::SplitProgramPath(source
, dir
, file
);
74 std::string outFile
= outdir
;
77 std::string tempOutputFile
= outFile
;
78 tempOutputFile
+= ".tmp";
79 std::ofstream
fout(tempOutputFile
.c_str());
82 cmSystemTools::Error("Could not open file for write in copy operation: ",
83 tempOutputFile
.c_str(), outdir
);
84 cmSystemTools::ReportLastSystemError("");
87 std::ifstream
fin(source
);
90 cmSystemTools::Error("Could not open file for read in copy operation",
94 // now copy input to output and expand variables in the
95 // input file at the same time
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";
121 fout
<< inLine
<< "\n";
126 fout
<< inLine
<< "\n";
129 // close the files before attempting to copy
132 cmSystemTools::CopyFileIfDifferent(tempOutputFile
.c_str(),
134 cmSystemTools::RemoveFile(tempOutputFile
.c_str());