2010-01-23 09:54:51 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2011-01-09 20:33:36 +01:00
|
|
|
* Copyright (C) 2007-2011 Daniel Marjamäki and Cppcheck team.
|
2010-01-23 09:54:51 +01:00
|
|
|
*
|
|
|
|
* 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)
|
2011-01-18 18:37:15 +01:00
|
|
|
#include "../cli/fileLister_win32.h"
|
2010-03-11 21:30:06 +01:00
|
|
|
#else // POSIX-style system
|
2011-01-18 18:37:15 +01:00
|
|
|
#include "../cli/filelister_unix.h"
|
2010-03-11 21:30:06 +01:00
|
|
|
#endif
|
2010-01-23 09:54:51 +01:00
|
|
|
|
|
|
|
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 07:30:58 +02:00
|
|
|
if (std::find(depfiles.begin(), depfiles.end(), filename) != depfiles.end())
|
2010-01-23 09:54:51 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
std::ifstream f(filename.c_str());
|
2010-04-02 07:30:58 +02:00
|
|
|
if (! f.is_open())
|
2010-01-23 09:54:51 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (filename.compare(0, 4, "cli/") == 0 || filename.compare(0, 5, "test/") == 0)
|
2010-01-23 09:54:51 +01:00
|
|
|
getDeps("lib" + filename.substr(filename.find("/")), depfiles);
|
|
|
|
return;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
if (filename.find(".c") == std::string::npos)
|
2010-01-23 09:54:51 +01:00
|
|
|
depfiles.push_back(filename);
|
|
|
|
|
|
|
|
std::string path(filename);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (path.find("/") != std::string::npos)
|
2010-01-23 09:54:51 +01:00
|
|
|
path.erase(1 + path.rfind("/"));
|
|
|
|
|
|
|
|
std::string line;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (std::getline(f, line))
|
2010-01-23 09:54:51 +01:00
|
|
|
{
|
|
|
|
std::string::size_type pos1 = line.find("#include \"");
|
2010-04-02 07:30:58 +02:00
|
|
|
if (pos1 == std::string::npos)
|
2010-01-23 09:54:51 +01:00
|
|
|
continue;
|
|
|
|
pos1 += 10;
|
|
|
|
|
|
|
|
std::string::size_type pos2 = line.find("\"", pos1);
|
|
|
|
std::string hfile(path + line.substr(pos1, pos2 - pos1));
|
2010-04-02 07:30:58 +02:00
|
|
|
if (hfile.find("/../") != std::string::npos) // TODO: Ugly fix
|
2010-01-23 09:54:51 +01:00
|
|
|
hfile.erase(0, 4 + hfile.find("/../"));
|
|
|
|
getDeps(hfile, depfiles);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-17 23:42:01 +02:00
|
|
|
static void compilefiles(std::ostream &fout, const std::vector<std::string> &files, const std::string &args)
|
2010-01-23 09:54:51 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
for (unsigned int i = 0; i < files.size(); ++i)
|
2010-01-23 09:54:51 +01:00
|
|
|
{
|
|
|
|
fout << objfile(files[i]) << ": " << files[i];
|
|
|
|
std::vector<std::string> depfiles;
|
|
|
|
getDeps(files[i], depfiles);
|
2010-04-02 07:30:58 +02:00
|
|
|
for (unsigned int dep = 0; dep < depfiles.size(); ++dep)
|
2010-01-23 09:54:51 +01:00
|
|
|
fout << " " << depfiles[dep];
|
2010-12-26 18:12:14 +01:00
|
|
|
fout << "\n\t$(CXX) $(CPPFLAGS) $(CXXFLAGS) " << args << " -c -o " << objfile(files[i]) << " " << files[i] << "\n\n";
|
2010-01-23 09:54:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-19 17:35:38 +01:00
|
|
|
static void getCppFiles(std::vector<std::string> &files, const std::string &path)
|
|
|
|
{
|
2010-11-09 19:11:43 +01:00
|
|
|
getFileLister()->recursiveAddFiles(files, path);
|
2010-02-19 17:35:38 +01:00
|
|
|
// only get *.cpp files..
|
2010-04-02 07:30:58 +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 07:30:58 +02:00
|
|
|
if (it->find(".cpp") == std::string::npos)
|
2010-02-19 17:35:38 +01:00
|
|
|
it = files.erase(it);
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-19 13:05:46 +02:00
|
|
|
|
|
|
|
static void makeConditionalVariable(std::ostream &os, const std::string &variable, const std::string &value)
|
|
|
|
{
|
|
|
|
os << "ifndef " << variable << '\n'
|
|
|
|
<< " " << variable << '=' << value << '\n'
|
|
|
|
<< "endif\n"
|
|
|
|
<< "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-09 08:10:05 +01:00
|
|
|
int main(int argc, char **argv)
|
2010-01-23 09:54:51 +01:00
|
|
|
{
|
2010-03-09 08:10:05 +01:00
|
|
|
const bool release(argc >= 2 && std::string(argv[1]) == "--release");
|
|
|
|
|
2010-01-23 09:54:51 +01:00
|
|
|
// Get files..
|
|
|
|
std::vector<std::string> libfiles;
|
2010-02-19 17:35:38 +01:00
|
|
|
getCppFiles(libfiles, "lib/");
|
2010-01-23 09:54:51 +01:00
|
|
|
|
|
|
|
std::vector<std::string> clifiles;
|
2010-02-19 17:35:38 +01:00
|
|
|
getCppFiles(clifiles, "cli/");
|
2010-01-23 09:54:51 +01:00
|
|
|
|
|
|
|
std::vector<std::string> testfiles;
|
2010-02-19 17:35:38 +01:00
|
|
|
getCppFiles(testfiles, "test/");
|
2010-01-23 09:54:51 +01:00
|
|
|
|
2010-09-19 13:05:46 +02:00
|
|
|
if (libfiles.empty() && clifiles.empty() && testfiles.empty())
|
|
|
|
{
|
|
|
|
std::cerr << "No files found. Are you in the correct directory?" << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2010-12-12 11:56:22 +01:00
|
|
|
std::vector<std::string> externalfiles;
|
|
|
|
getCppFiles(externalfiles, "externals/");
|
|
|
|
|
2010-02-23 21:27:01 +01:00
|
|
|
|
|
|
|
// QMAKE - lib/lib.pri
|
|
|
|
{
|
|
|
|
std::ofstream fout1("lib/lib.pri");
|
2010-04-02 07:30:58 +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";
|
2010-12-12 17:26:13 +01:00
|
|
|
fout1 << "LIBS += -L../externals -lpcre\n";
|
|
|
|
fout1 << "INCLUDEPATH += ../externals\n";
|
2010-12-18 16:11:19 +01:00
|
|
|
fout1 << "HEADERS += $${BASEPATH}check.h \\\n";
|
2010-04-02 07:30:58 +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 07:30:58 +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"));
|
2010-12-18 16:11:19 +01:00
|
|
|
fout1 << std::string(11, ' ') << "$${BASEPATH}" << fname << ".h";
|
2010-04-02 07:30:58 +02:00
|
|
|
if (i < libfiles.size() - 1)
|
2010-02-23 21:27:01 +01:00
|
|
|
fout1 << " \\\n";
|
|
|
|
}
|
|
|
|
fout1 << "\n\nSOURCES += ";
|
2010-04-02 07:30:58 +02:00
|
|
|
for (unsigned int i = 0; i < libfiles.size(); ++i)
|
2010-02-23 21:27:01 +01:00
|
|
|
{
|
2010-12-18 16:11:19 +01:00
|
|
|
fout1 << "$${BASEPATH}" << libfiles[i].substr(4);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (i < libfiles.size() - 1)
|
2010-02-23 21:27:01 +01:00
|
|
|
fout1 << " \\\n" << std::string(11, ' ');
|
|
|
|
}
|
|
|
|
fout1 << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-19 13:05:46 +02:00
|
|
|
static const char makefile[] = "Makefile";
|
|
|
|
std::ofstream fout(makefile, std::ios_base::trunc);
|
|
|
|
if (!fout.is_open())
|
|
|
|
{
|
|
|
|
std::cerr << "An error occurred while trying to open "
|
|
|
|
<< makefile
|
|
|
|
<< ".\n";
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2010-01-23 09:54:51 +01:00
|
|
|
|
2010-12-12 17:26:13 +01:00
|
|
|
fout << "# This file is generated by tools/dmake, do not edit.\n\n";
|
|
|
|
|
2010-03-09 08:10:05 +01:00
|
|
|
// Makefile settings..
|
2010-05-08 12:57:44 +02:00
|
|
|
if (release)
|
|
|
|
{
|
2010-09-19 13:05:46 +02:00
|
|
|
makeConditionalVariable(fout, "CXXFLAGS", "-O2 -DNDEBUG -Wall");
|
2010-05-08 12:57:44 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// TODO: add more compiler warnings.
|
|
|
|
// -Wlogical-op : doesn't work on older GCC
|
2011-01-01 09:16:31 +01:00
|
|
|
// -Wconversion : too many warnings
|
2010-05-08 12:57:44 +02:00
|
|
|
|
2010-08-13 21:31:04 +02:00
|
|
|
// The _GLIBCXX_DEBUG doesn't work in cygwin
|
2010-09-19 13:05:46 +02:00
|
|
|
makeConditionalVariable(fout, "CXXFLAGS",
|
|
|
|
"-Wall "
|
|
|
|
"-Wextra "
|
|
|
|
"-Wshadow "
|
|
|
|
"-pedantic "
|
|
|
|
"-Wno-long-long "
|
|
|
|
"-Wfloat-equal "
|
|
|
|
"-Wcast-qual "
|
|
|
|
"-Wsign-conversion "
|
2011-01-01 09:16:31 +01:00
|
|
|
// "-Wconversion "
|
2010-09-19 13:05:46 +02:00
|
|
|
"-g");
|
2010-05-08 12:57:44 +02:00
|
|
|
}
|
2010-09-19 13:05:46 +02:00
|
|
|
makeConditionalVariable(fout, "CXX", "g++");
|
|
|
|
makeConditionalVariable(fout, "PREFIX", "/usr");
|
|
|
|
fout << "BIN=$(DESTDIR)$(PREFIX)/bin\n\n";
|
|
|
|
fout << "# For 'make man': sudo apt-get install xsltproc docbook-xsl docbook-xml on Linux\n";
|
2010-02-23 18:37:46 +01:00
|
|
|
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";
|
2010-01-23 09:54:51 +01:00
|
|
|
|
|
|
|
fout << "\n###### Object Files\n\n";
|
|
|
|
fout << "LIBOBJ = " << objfile(libfiles[0]);
|
2010-04-02 07:30:58 +02:00
|
|
|
for (unsigned int i = 1; i < libfiles.size(); ++i)
|
2010-01-23 09:54:51 +01:00
|
|
|
fout << " \\" << std::endl << std::string(14, ' ') << objfile(libfiles[i]);
|
|
|
|
fout << "\n\n";
|
|
|
|
fout << "CLIOBJ = " << objfile(clifiles[0]);
|
2010-04-02 07:30:58 +02:00
|
|
|
for (unsigned int i = 1; i < clifiles.size(); ++i)
|
2010-01-23 09:54:51 +01:00
|
|
|
fout << " \\" << std::endl << std::string(14, ' ') << objfile(clifiles[i]);
|
|
|
|
fout << "\n\n";
|
|
|
|
fout << "TESTOBJ = " << objfile(testfiles[0]);
|
2010-04-02 07:30:58 +02:00
|
|
|
for (unsigned int i = 1; i < testfiles.size(); ++i)
|
2010-01-23 09:54:51 +01:00
|
|
|
fout << " \\" << std::endl << std::string(14, ' ') << objfile(testfiles[i]);
|
|
|
|
fout << "\n\n";
|
2010-12-12 11:56:22 +01:00
|
|
|
fout << "EXTOBJ = " << objfile(externalfiles[0]);
|
|
|
|
for (unsigned int i = 1; i < externalfiles.size(); ++i)
|
|
|
|
fout << " \\" << std::endl << std::string(14, ' ') << objfile(externalfiles[i]);
|
|
|
|
fout << "\n\n";
|
2010-01-23 09:54:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
fout << "\n###### Targets\n\n";
|
2010-12-12 11:56:22 +01:00
|
|
|
fout << "cppcheck:\t$(LIBOBJ)\t$(CLIOBJ)\t$(EXTOBJ)\n";
|
2010-12-26 18:12:14 +01:00
|
|
|
fout << "\t$(CXX) $(CPPFLAGS) $(CXXFLAGS) -o cppcheck $(CLIOBJ) $(LIBOBJ) $(EXTOBJ) -lpcre $(LDFLAGS)\n\n";
|
2010-09-22 20:25:27 +02:00
|
|
|
fout << "all:\tcppcheck\ttestrunner\n\n";
|
2011-01-18 18:37:15 +01:00
|
|
|
fout << "testrunner:\t$(TESTOBJ)\t$(LIBOBJ)\t$(EXTOBJ)\tcli/threadexecutor.o\tcli/cmdlineparser.o\tcli/cppcheckexecutor.o\tcli/filelister.o\tcli/filelister_unix.o\n";
|
|
|
|
fout << "\t$(CXX) $(CPPFLAGS) $(CXXFLAGS) -o testrunner $(TESTOBJ) $(LIBOBJ) $(EXTOBJ) -lpcre cli/threadexecutor.o cli/cmdlineparser.o\tcli/filelister.o\tcli/filelister_unix.o $(LDFLAGS)\n\n";
|
2010-01-23 09:54:51 +01:00
|
|
|
fout << "test:\tall\n";
|
|
|
|
fout << "\t./testrunner\n\n";
|
2010-09-26 05:29:23 +02:00
|
|
|
fout << "check:\tall\n";
|
|
|
|
fout << "\t./testrunner -g -q\n\n";
|
2010-09-22 20:25:27 +02:00
|
|
|
fout << "dmake:\ttools/dmake.cpp\n";
|
2011-01-21 20:51:01 +01:00
|
|
|
fout << "\t$(CXX) -o dmake tools/dmake.cpp cli/filelister*.cpp lib/path.cpp -Ilib\n\n";
|
2010-01-23 09:54:51 +01:00
|
|
|
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
|
2010-12-13 14:47:53 +01:00
|
|
|
fout << "\trm -f lib/*.o cli/*.o test/*.o externals/tinyxml/*.o testrunner cppcheck cppcheck.1\n\n";
|
2010-09-22 20:25:27 +02:00
|
|
|
fout << "man:\tman/cppcheck.1\n\n";
|
|
|
|
fout << "man/cppcheck.1:\t$(MAN_SOURCE)\n\n";
|
2010-02-23 18:37:46 +01:00
|
|
|
fout << "\t$(XP) $(DB2MAN) $(MAN_SOURCE)\n\n";
|
2010-10-12 14:04:06 +02:00
|
|
|
fout << "tags:\n";
|
|
|
|
fout << "\tctags -R --exclude=doxyoutput .\n\n";
|
2010-02-23 18:37:46 +01:00
|
|
|
fout << "install:\tcppcheck\n";
|
|
|
|
fout << "\tinstall -d ${BIN}\n";
|
|
|
|
fout << "\tinstall cppcheck ${BIN}\n\n";
|
2010-02-19 17:35:38 +01:00
|
|
|
#endif
|
2010-01-23 09:54:51 +01:00
|
|
|
|
|
|
|
fout << "\n###### Build\n\n";
|
|
|
|
|
2010-06-17 23:42:01 +02:00
|
|
|
compilefiles(fout, libfiles, "-Ilib");
|
2010-12-12 11:56:22 +01:00
|
|
|
compilefiles(fout, clifiles, "-Ilib -Iexternals");
|
|
|
|
compilefiles(fout, testfiles, "-Ilib -Icli -Iexternals");
|
2010-01-23 09:54:51 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|