From: <hug...@li...> - 2010-07-11 17:50:00
|
details: http://hugin.hg.sourceforge.net/hgweb/hugin/hugin/hgrepo/h/hu/hugin/hugin/rev/4d7bd2c01c47 changeset: 4128:4d7bd2c01c47 user: Florian Achleitner <flo...@gm...> date: Sun Jul 11 19:46:49 2010 +0200 description: add and implement conditionals diffstat: src/hugin_base/makefilelib/CMakeLists.txt | 4 +- src/hugin_base/makefilelib/Conditional.cpp | 33 +++++++ src/hugin_base/makefilelib/Conditional.h | 123 +++++++++++++++++++++++++++++ src/hugin_base/makefilelib/main.cpp | 53 ++++++++++++- 4 files changed, 209 insertions(+), 4 deletions(-) diffs (250 lines): diff -r 380782bc646c -r 4d7bd2c01c47 src/hugin_base/makefilelib/CMakeLists.txt --- a/src/hugin_base/makefilelib/CMakeLists.txt Fri Jul 09 21:59:24 2010 +0200 +++ b/src/hugin_base/makefilelib/CMakeLists.txt Sun Jul 11 19:46:49 2010 +0200 @@ -1,7 +1,7 @@ SET(MAKEFILELIB_SRC Comment.cpp MakefileItem.cpp Variable.cpp VariableRef.cpp - Filename.cpp Makefile.cpp Rule.cpp VariableDef.cpp AutoVariable.cpp) + Filename.cpp Makefile.cpp Rule.cpp VariableDef.cpp AutoVariable.cpp Conditional.cpp) SET(MAKEFILELIB_HEADER Comment.h Makefile.h Rule.h Variable.h Filename.h - MakefileItem.h VariableDef.h VariableRef.h AutoVariable.h Newline.h) + MakefileItem.h VariableDef.h VariableRef.h AutoVariable.h Newline.h Conditional.h) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") diff -r 380782bc646c -r 4d7bd2c01c47 src/hugin_base/makefilelib/Conditional.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/hugin_base/makefilelib/Conditional.cpp Sun Jul 11 19:46:49 2010 +0200 @@ -0,0 +1,33 @@ +/** + * @file Conditional.cpp + * @brief + * Created on: Jul 10, 2010 + * @author Florian Achleitner <flo...@gm...> + */ + +#include "Conditional.h" + +namespace makefile +{ + +std::string Conditional::toString() +{ + std::string str = printif(); + std::vector<MakefileItem*>::iterator i; + for(i = ifblock.begin(); i != ifblock.end(); i++) + str.append((*i)->toString()); + str.append("\n"); + + // only if else block has contents. + if(!elseblock.empty()) + { + str.append("else\n"); + for(i = elseblock.begin(); i != elseblock.end(); i++) + str.append((*i)->toString()); + str.append("\n"); + } + str.append("endif\n"); + return str; +} + +} diff -r 380782bc646c -r 4d7bd2c01c47 src/hugin_base/makefilelib/Conditional.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/hugin_base/makefilelib/Conditional.h Sun Jul 11 19:46:49 2010 +0200 @@ -0,0 +1,123 @@ +/** + * @file Conditional.h + * @brief + * Created on: Jul 10, 2010 + * @author Florian Achleitner <flo...@gm...> + */ + +#ifndef CONDITIONAL_H_ +#define CONDITIONAL_H_ + +/** + * + */ +#include "MakefileItem.h" + +#include <vector> + +namespace makefile +{ + +/** + * Makefiles can have conditional parts, that are only seen by make if the condition + * is true. Therefore the conditional blocks can contain any one or more MakefileItem. + * There are four kinds of conditionals, they can start with ifeq, ifneq, ifdef and ifndef. + * The first two test for equality or non-equality, the latter two are true if a Variable is + * defined or not. + * This is the abstract baseclass for all those. + * @note The output does intentionally not have intentions. Maybe that would be good and unproblematic + * but I'm not sure, and intention matters in makefiles (see rules). + */ +class Conditional: public makefile::MakefileItem +{ + /// Output the header line, like ifeq (arg1, arg2). + virtual std::string printif()=0; + /// MakefileItem active if the condition is true. + std::vector<MakefileItem*> ifblock; + /// MakefileItem in the else block. The else-block is ommited if this is empty. + std::vector<MakefileItem*> elseblock; +public: + Conditional() + {} + virtual ~Conditional() + {} + + void addToIf(MakefileItem& item) + { + ifblock.push_back(&item); + } + void addToElse(MakefileItem& item) + { + elseblock.push_back(&item); + } + + virtual std::string toString(); +}; + +class ConditionalEQ: public makefile::Conditional +{ + std::string arg1, arg2; + virtual std::string printif() + { + return "ifeq (" + arg1 + "," + arg2 + ")\n"; + } +public: + ConditionalEQ(std::string arg1_, std::string arg2_) + :arg1(arg1_), arg2(arg2_) + {} + virtual ~ConditionalEQ() + {} + +}; + +class ConditionalNEQ: public makefile::Conditional +{ + std::string arg1, arg2; + virtual std::string printif() + { + return "ifneq (" + arg1 + "," + arg2 + ")\n"; + } +public: + ConditionalNEQ(std::string arg1_, std::string arg2_) + :arg1(arg1_), arg2(arg2_) + {} + virtual ~ConditionalNEQ() + {} + +}; + +class ConditionalDEF: public makefile::Conditional +{ + std::string varname; + virtual std::string printif() + { + return "ifdef " + varname + "\n"; + } +public: + ConditionalDEF(std::string varname_) + :varname(varname_) + {} + virtual ~ConditionalDEF() + {} + +}; + +class ConditionalNDEF: public makefile::Conditional +{ + std::string varname; + virtual std::string printif() + { + return "ifndef " + varname + "\n"; + } +public: + ConditionalNDEF(std::string varname_) + :varname(varname_) + {} + virtual ~ConditionalNDEF() + {} + +}; + +} + +#endif /* CONDITIONAL_H_ */ diff -r 380782bc646c -r 4d7bd2c01c47 src/hugin_base/makefilelib/main.cpp --- a/src/hugin_base/makefilelib/main.cpp Fri Jul 09 21:59:24 2010 +0200 +++ b/src/hugin_base/makefilelib/main.cpp Sun Jul 11 19:46:49 2010 +0200 @@ -16,6 +16,7 @@ #include "AutoVariable.h" #include "Newline.h" #include "Rule.h" +#include "Conditional.h" #include <boost/regex.hpp> #include <boost/scoped_ptr.hpp> @@ -143,11 +144,59 @@ return 0; } +int trycond() +{ + Variable t1("TARGET1", "t1.o"); t1.getDef().add(); + Variable t2("TARGET2", "t2.o"); t2.getDef().add(); + Variable p1("PRERE1", "t1.c"); p1.getDef().add(); + Variable p2("PRERE2", "t2.c"); p2.getDef().add(); + AutoVariable all("@"); + Rule r; + r.addTarget(t1.getRef().toString()); + r.addTarget(t2.getRef().toString()); + r.addPrereq(p1.getRef().toString()); + r.addPrereq(p2.getRef().toString()); + r.addCommand("echo " + all.getRef().toString()); + + Variable iftrue("TRUE", "if_is_true"); + Variable iffales("FALSE", "if_is_false"); + + ConditionalEQ cond1(iftrue.getRef().toString(), "if_is_true"); + cond1.addToIf(r); + cond1.addToIf(iftrue.getDef()); + cond1.addToElse(iffales.getDef()); + cond1.addToElse(r); + cond1.add(); + + ConditionalNEQ cond2(iftrue.getRef().toString(), "if_is_true"); + cond2.addToIf(r); + cond2.addToIf(iftrue.getDef()); + cond2.addToElse(iffales.getDef()); + cond2.addToElse(r); + cond2.add(); + + ConditionalDEF cond3(iftrue.getName()); + cond3.addToIf(r); + cond3.addToIf(iftrue.getDef()); + cond3.addToElse(iffales.getDef()); + cond3.addToElse(r); + cond3.add(); + + ConditionalNDEF cond4(iftrue.getName()); + cond4.addToIf(r); + cond4.addToIf(iftrue.getDef()); + cond4.add(); + + Makefile::getSingleton().writeMakefile(cout); + Makefile::clean(); + return 0; +} int main(int argc, char *argv[]) { // return trymakefile(); // return tryreplace(); - return tryall(); - return tryrule(); +// return tryall(); +// return tryrule(); + return trycond(); return 0; } |