From 4934cfa62273f5b2ab825d7bb81dfc48a4fdb600 Mon Sep 17 00:00:00 2001 From: Pete Johns Date: Sun, 19 Sep 2010 21:05:46 +1000 Subject: [PATCH] Wrapped variables in conditions This allows CXX and CXXFLAGS to be overridden without patching. Added PREFIX for MacPorts compatibility, allowing staging to $(DESTDIR)$(PREFIX)/bin, leaving original behaviour unchanged. Exit early if run in incorrect directory or if Makefile cannot be written. --- tools/dmake.cpp | 60 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 17 deletions(-) diff --git a/tools/dmake.cpp b/tools/dmake.cpp index c4b7199f4..939367c24 100644 --- a/tools/dmake.cpp +++ b/tools/dmake.cpp @@ -98,6 +98,16 @@ static void getCppFiles(std::vector &files, const std::string &path } } + +static void makeConditionalVariable(std::ostream &os, const std::string &variable, const std::string &value) +{ + os << "ifndef " << variable << '\n' + << " " << variable << '=' << value << '\n' + << "endif\n" + << "\n"; +} + + int main(int argc, char **argv) { const bool release(argc >= 2 && std::string(argv[1]) == "--release"); @@ -112,6 +122,12 @@ int main(int argc, char **argv) std::vector testfiles; getCppFiles(testfiles, "test/"); + if (libfiles.empty() && clifiles.empty() && testfiles.empty()) + { + std::cerr << "No files found. Are you in the correct directory?" << std::endl; + return EXIT_FAILURE; + } + // QMAKE - lib/lib.pri { @@ -142,35 +158,46 @@ int main(int argc, char **argv) } - std::ofstream fout("Makefile"); + 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; + } // Makefile settings.. if (release) { - fout << "CXXFLAGS=-O2 -DNDEBUG -Wall\n"; + makeConditionalVariable(fout, "CXXFLAGS", "-O2 -DNDEBUG -Wall"); } else { + fout << "# This file is generated by tools/dmake, do not edit.\n\n"; + // TODO: add more compiler warnings. // -Wconversion : generates too many compiler warnings currently // -Wlogical-op : doesn't work on older GCC // The _GLIBCXX_DEBUG doesn't work in cygwin - fout << "CXXFLAGS=" - << "-Wall " - << "-Wextra " - << "-Wshadow " - << "-pedantic " - << "-Wno-long-long " - << "-Wfloat-equal " - << "-Wcast-qual " - << "-Wsign-conversion " - // << "-Wconversion " - << "-g\n"; + makeConditionalVariable(fout, "CXXFLAGS", + "-Wall " + "-Wextra " + "-Wshadow " + "-pedantic " + "-Wno-long-long " + "-Wfloat-equal " + "-Wcast-qual " + "-Wsign-conversion " + // "-Wconversion " + "-g"); } - fout << "CXX=g++\n"; - fout << "BIN=${DESTDIR}/usr/bin\n\n"; - fout << "# For 'make man': sudo apt-get install xsltproc docbook-xsl docbook-xml\n"; + 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"; 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"; @@ -221,4 +248,3 @@ int main(int argc, char **argv) return 0; } -