cmake/Source/cmUseMangledMesaCommand.cxx

104 lines
3.4 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmUseMangledMesaCommand.h"
2016-07-09 11:21:54 +02:00
2017-07-20 19:35:53 +02:00
#include "cmsys/FStream.hxx"
#include "cmsys/RegularExpression.hxx"
2020-02-01 23:06:01 +01:00
#include "cmExecutionStatus.h"
#include "cmStringAlgorithms.h"
2017-04-14 19:02:05 +02:00
#include "cmSystemTools.h"
2020-02-01 23:06:01 +01:00
namespace {
void CopyAndFullPathMesaHeader(const std::string& source,
const std::string& outdir);
}
2017-04-14 19:02:05 +02:00
2020-02-01 23:06:01 +01:00
bool cmUseMangledMesaCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
2013-03-16 19:13:01 +02:00
{
// expected two arguments:
2018-04-23 21:13:27 +02:00
// argument one: the full path to gl_mangle.h
// argument two : directory for output of edited headers
2016-07-09 11:21:54 +02:00
if (args.size() != 2) {
2020-02-01 23:06:01 +01:00
status.SetError("called with incorrect number of arguments");
return false;
2016-07-09 11:21:54 +02:00
}
2019-11-11 23:01:05 +01:00
const std::string& inputDir = args[0];
2020-02-01 23:06:01 +01:00
std::string glh = cmStrCat(inputDir, "/gl.h");
2018-04-23 21:13:27 +02:00
if (!cmSystemTools::FileExists(glh)) {
2020-02-01 23:06:01 +01:00
std::string e = cmStrCat("Bad path to Mesa, could not find: ", glh, ' ');
status.SetError(e);
return false;
2016-07-09 11:21:54 +02:00
}
2019-11-11 23:01:05 +01:00
const std::string& destDir = args[1];
std::vector<std::string> files;
cmSystemTools::Glob(inputDir, "\\.h$", files);
2016-07-09 11:21:54 +02:00
if (files.empty()) {
2019-11-11 23:01:05 +01:00
cmSystemTools::Error("Could not open Mesa Directory " + inputDir);
return false;
2016-07-09 11:21:54 +02:00
}
cmSystemTools::MakeDirectory(destDir);
2018-01-26 17:06:56 +01:00
for (std::string const& f : files) {
2020-02-01 23:06:01 +01:00
std::string path = cmStrCat(inputDir, '/', f);
CopyAndFullPathMesaHeader(path, destDir);
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
return true;
}
2020-02-01 23:06:01 +01:00
namespace {
void CopyAndFullPathMesaHeader(const std::string& source,
const std::string& outdir)
{
2020-02-01 23:06:01 +01:00
std::string dir;
std::string file;
cmSystemTools::SplitProgramPath(source, dir, file);
2020-02-01 23:06:01 +01:00
std::string outFile = cmStrCat(outdir, '/', file);
std::string tempOutputFile = cmStrCat(outFile, ".tmp");
2014-08-03 19:52:23 +02:00
cmsys::ofstream fout(tempOutputFile.c_str());
2016-07-09 11:21:54 +02:00
if (!fout) {
2019-11-11 23:01:05 +01:00
cmSystemTools::Error("Could not open file for write in copy operation: " +
tempOutputFile + outdir);
cmSystemTools::ReportLastSystemError("");
return;
2016-07-09 11:21:54 +02:00
}
2019-11-11 23:01:05 +01:00
cmsys::ifstream fin(source.c_str());
2016-07-09 11:21:54 +02:00
if (!fin) {
2019-11-11 23:01:05 +01:00
cmSystemTools::Error("Could not open file for read in copy operation" +
source);
return;
2016-07-09 11:21:54 +02:00
}
// now copy input to output and expand variables in the
// input file at the same time
2013-03-16 19:13:01 +02:00
std::string inLine;
// regular expression for any #include line
cmsys::RegularExpression includeLine(
"^[ \t]*#[ \t]*include[ \t]*[<\"]([^\">]+)[\">]");
// regular expression for gl/ or GL/ in a file (match(1) of above)
2019-11-11 23:01:05 +01:00
cmsys::RegularExpression glDirLine(R"((gl|GL)(/|\\)([^<"]+))");
// regular expression for gl GL or xmesa in a file (match(1) of above)
cmsys::RegularExpression glLine("(gl|GL|xmesa)");
2016-07-09 11:21:54 +02:00
while (cmSystemTools::GetLineFromStream(fin, inLine)) {
2018-10-28 12:09:07 +01:00
if (includeLine.find(inLine)) {
std::string includeFile = includeLine.match(1);
2018-10-28 12:09:07 +01:00
if (glDirLine.find(includeFile)) {
std::string gfile = glDirLine.match(3);
2015-04-27 22:25:09 +02:00
fout << "#include \"" << outdir << "/" << gfile << "\"\n";
2018-10-28 12:09:07 +01:00
} else if (glLine.find(includeFile)) {
2016-07-09 11:21:54 +02:00
fout << "#include \"" << outdir << "/" << includeLine.match(1)
<< "\"\n";
} else {
fout << inLine << "\n";
}
2016-07-09 11:21:54 +02:00
} else {
fout << inLine << "\n";
}
2016-07-09 11:21:54 +02:00
}
// close the files before attempting to copy
fin.close();
fout.close();
2020-02-01 23:06:01 +01:00
cmSystemTools::MoveFileIfDifferent(tempOutputFile, outFile);
}
}