cppcheck/tools/dmake.cpp

205 lines
6.9 KiB
C++
Raw Normal View History

/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2009 Daniel Marjamäki and Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Generate Makefile for cppcheck
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
2010-03-11 21:30:06 +01:00
#if defined(_WIN32)
#include "../lib/fileLister_win32.h"
2010-03-11 21:30:06 +01:00
#else // POSIX-style system
#include "../lib/filelister_unix.h"
2010-03-11 21:30:06 +01:00
#endif
std::string objfile(std::string cppfile)
{
cppfile.erase(cppfile.rfind("."));
return cppfile + ".o";
}
void getDeps(const std::string &filename, std::vector<std::string> &depfiles)
{
// Is the dependency already included?
2010-04-02 02:21:53 +02:00
if(std::find(depfiles.begin(), depfiles.end(), filename) != depfiles.end())
return;
std::ifstream f(filename.c_str());
2010-04-02 02:21:53 +02:00
if(! f.is_open())
{
2010-04-02 02:21:53 +02:00
if(filename.compare(0, 4, "cli/") == 0 || filename.compare(0, 5, "test/") == 0)
getDeps("lib" + filename.substr(filename.find("/")), depfiles);
return;
}
2010-04-02 02:21:53 +02:00
if(filename.find(".c") == std::string::npos)
depfiles.push_back(filename);
std::string path(filename);
2010-04-02 02:21:53 +02:00
if(path.find("/") != std::string::npos)
path.erase(1 + path.rfind("/"));
std::string line;
2010-04-02 02:21:53 +02:00
while(std::getline(f, line))
{
std::string::size_type pos1 = line.find("#include \"");
2010-04-02 02:21:53 +02:00
if(pos1 == std::string::npos)
continue;
pos1 += 10;
std::string::size_type pos2 = line.find("\"", pos1);
std::string hfile(path + line.substr(pos1, pos2 - pos1));
2010-04-02 02:21:53 +02:00
if(hfile.find("/../") != std::string::npos) // TODO: Ugly fix
hfile.erase(0, 4 + hfile.find("/../"));
getDeps(hfile, depfiles);
}
}
static void compilefiles(std::ostream &fout, const std::vector<std::string> &files)
{
2010-04-02 02:21:53 +02:00
for(unsigned int i = 0; i < files.size(); ++i)
{
fout << objfile(files[i]) << ": " << files[i];
std::vector<std::string> depfiles;
getDeps(files[i], depfiles);
2010-04-02 02:21:53 +02:00
for(unsigned int dep = 0; dep < depfiles.size(); ++dep)
fout << " " << depfiles[dep];
fout << "\n\t$(CXX) $(CXXFLAGS) -Ilib -c -o " << objfile(files[i]) << " " << files[i] << "\n\n";
}
}
2010-02-19 17:35:38 +01:00
static void getCppFiles(std::vector<std::string> &files, const std::string &path)
{
2010-03-11 21:30:06 +01:00
getFileLister()->recursiveAddFiles(files, path, true);
2010-02-19 17:35:38 +01:00
// only get *.cpp files..
2010-04-02 02:21:53 +02:00
for(std::vector<std::string>::iterator it = files.begin(); it != files.end();)
2010-02-19 17:35:38 +01:00
{
2010-04-02 02:21:53 +02:00
if(it->find(".cpp") == std::string::npos)
2010-02-19 17:35:38 +01:00
it = files.erase(it);
else
++it;
}
}
int main(int argc, char **argv)
{
const bool release(argc >= 2 && std::string(argv[1]) == "--release");
// Get files..
std::vector<std::string> libfiles;
2010-02-19 17:35:38 +01:00
getCppFiles(libfiles, "lib/");
std::vector<std::string> clifiles;
2010-02-19 17:35:38 +01:00
getCppFiles(clifiles, "cli/");
std::vector<std::string> testfiles;
2010-02-19 17:35:38 +01:00
getCppFiles(testfiles, "test/");
2010-02-23 21:27:01 +01:00
// QMAKE - lib/lib.pri
{
std::ofstream fout1("lib/lib.pri");
2010-04-02 02:21:53 +02:00
if(fout1.is_open())
2010-02-23 21:27:01 +01:00
{
fout1 << "# no manual edits - this file is autogenerated by dmake\n\n";
fout1 << "HEADERS += $$PWD/check.h \\\n";
2010-04-02 02:21:53 +02:00
for(unsigned int i = 0; i < libfiles.size(); ++i)
2010-02-23 21:27:01 +01:00
{
std::string fname(libfiles[i].substr(4));
2010-04-02 02:21:53 +02:00
if(fname.find(".cpp") == std::string::npos)
2010-02-23 21:27:01 +01:00
continue; // shouldn't happen
fname.erase(fname.find(".cpp"));
fout1 << std::string(11, ' ') << "$$PWD/" << fname << ".h";
2010-04-02 02:21:53 +02:00
if(i < libfiles.size() - 1)
2010-02-23 21:27:01 +01:00
fout1 << " \\\n";
}
fout1 << "\n\nSOURCES += ";
2010-04-02 02:21:53 +02:00
for(unsigned int i = 0; i < libfiles.size(); ++i)
2010-02-23 21:27:01 +01:00
{
fout1 << "$$PWD/" << libfiles[i].substr(4);
2010-04-02 02:21:53 +02:00
if(i < libfiles.size() - 1)
2010-02-23 21:27:01 +01:00
fout1 << " \\\n" << std::string(11, ' ');
}
fout1 << "\n";
}
}
std::ofstream fout("Makefile");
// Makefile settings..
2010-03-13 08:05:07 +01:00
// TODO: add more compiler warnings.
// -Wsign-conversion : generates too many compiler warnings currently
// -Wlogical-op : doesn't work on older GCC
fout << "CXXFLAGS=-Wall -Wextra -pedantic -Wno-long-long -Wfloat-equal -Wcast-qual ";
fout << (release ? "-O2 -DNDEBUG" : "-g -D_GLIBCXX_DEBUG") << "\n";
fout << "CXX=g++\n";
2010-02-23 18:37:46 +01:00
fout << "BIN=${DESTDIR}/usr/bin\n\n";
fout << "# For 'make man': sudo apt-get install xsltproc docbook-xsl docbook-xml\n";
fout << "DB2MAN=/usr/share/sgml/docbook/stylesheet/xsl/nwalsh/manpages/docbook.xsl\n";
fout << "XP=xsltproc -''-nonet -''-param man.charmap.use.subset \"0\"\n";
fout << "MAN_SOURCE=man/cppcheck.1.xml\n\n";
fout << "\n###### Object Files\n\n";
fout << "LIBOBJ = " << objfile(libfiles[0]);
2010-04-02 02:21:53 +02:00
for(unsigned int i = 1; i < libfiles.size(); ++i)
fout << " \\" << std::endl << std::string(14, ' ') << objfile(libfiles[i]);
fout << "\n\n";
fout << "CLIOBJ = " << objfile(clifiles[0]);
2010-04-02 02:21:53 +02:00
for(unsigned int i = 1; i < clifiles.size(); ++i)
fout << " \\" << std::endl << std::string(14, ' ') << objfile(clifiles[i]);
fout << "\n\n";
fout << "TESTOBJ = " << objfile(testfiles[0]);
2010-04-02 02:21:53 +02:00
for(unsigned int i = 1; i < testfiles.size(); ++i)
fout << " \\" << std::endl << std::string(14, ' ') << objfile(testfiles[i]);
fout << "\n\n";
fout << "\n###### Targets\n\n";
fout << "cppcheck:\t$(LIBOBJ)\t$(CLIOBJ)\n";
fout << "\t$(CXX) $(CXXFLAGS) -o cppcheck $(CLIOBJ) $(LIBOBJ) $(LDFLAGS)\n\n";
fout << "all:\tcppcheck\ttestrunner\ttools\n\n";
fout << "testrunner:\t$(TESTOBJ)\t$(LIBOBJ)\n";
fout << "\t$(CXX) $(CXXFLAGS) -o testrunner $(TESTOBJ) $(LIBOBJ) $(LDFLAGS)\n\n";
fout << "test:\tall\n";
fout << "\t./testrunner\n\n";
fout << "clean:\n";
2010-02-19 17:35:38 +01:00
#ifdef _WIN32
2010-02-23 18:37:46 +01:00
fout << "\tdel lib\*.o\n\tdel cli\*.o\n\tdel test\*.o\n\tdel *.exe\n";
2010-02-19 17:35:38 +01:00
#else
fout << "\trm -f lib/*.o cli/*.o test/*.o testrunner cppcheck\n\n";
2010-02-23 18:37:46 +01:00
fout << "man:\t$(MAN_SOURCE)\n";
fout << "\t$(XP) $(DB2MAN) $(MAN_SOURCE)\n\n";
fout << "install:\tcppcheck\n";
fout << "\tinstall -d ${BIN}\n";
fout << "\tinstall cppcheck ${BIN}\n\n";
2010-02-19 17:35:38 +01:00
#endif
fout << "\n###### Build\n\n";
compilefiles(fout, libfiles);
compilefiles(fout, clifiles);
compilefiles(fout, testfiles);
return 0;
}