example.cpp

Go to the documentation of this file.
00001 /*
00002 This file is part of hugin.
00003 
00004 hugin is free software: you can redistribute it and/or modify
00005 it under the terms of the GNU General Public License as published by
00006 the Free Software Foundation, either version 2 of the License, or
00007 (at your option) any later version.
00008 
00009 hugin is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with hugin.  If not, see <http://www.gnu.org/licenses/>.
00016 */
00017 
00024 #include <iostream>
00025 #include <fstream>
00026 #include "Comment.h"
00027 #include "Conditional.h"
00028 #include "VariableDef.h"
00029 #include "VariableRef.h"
00030 #include "AutoVariable.h"
00031 #include "Rule.h"
00032 #include "Makefile.h"
00033 #include "Manager.h"
00034 
00035 using namespace makefile;
00036 
00046 int main(int argc, char *argv[])
00047 {
00048         // The Manager owns our MakefileItems and destructs them when
00049         // itself is destructed. Only heap-allocateed objects can be owned
00050         // by the Manager, otherwise delete would fail.
00051         Manager mgr;
00052 
00053         // own_add is a shortcut, if you don't need the pointer for anything else.
00054         mgr.own_add(new Comment("This example program creates a makefile to build itself"));
00055 
00056         // The other way is this:
00057         // We create a Variable.
00058         Variable* cc = mgr.own(new Variable("CXX", "g++"));
00059 
00060         // We create a Conditional that checks if CC is defined, and otherwise defines it.
00061         // Never forget to call mgr.own, or delete yourself, otherwise valgrind will tell you bad news ;)
00062         ConditionalNDEF* have_cc = mgr.own(new ConditionalNDEF("CXX"));
00063         // and add it.
00064         have_cc->add();
00065 
00066         // A Conditional can hold MakefileItems in it's if- and else-blocks
00067         // Variables have a VariableRef(erence) and a VariableDef(inition).
00068         // If CC is not defined, we want to define it.
00069         have_cc->addToIf(cc->getDef());
00070 
00071         // We want another Variable containing compiler flags
00072         // Variables quotemode defines how there value is quoted in the definition.
00073         Variable* cflags = mgr.own(new Variable("CFLAGS", "-Wall -c", Makefile::NONE));
00074         // ..and add the definition to the Makefile right here.
00075         cflags->getDef().add();
00076 
00077         // And another Var for the executable name
00078         Variable* executable = mgr.own(new Variable("OBJ", "example.o"));
00079         executable->getDef().add();
00080 
00081         // The default rule is the first that appears. We want to build the
00082         // executable.
00083         Rule* all = mgr.own(new Rule());
00084         all->add();
00085         all->addTarget("all");
00086         all->addPrereq(executable->getRef());
00087 
00088         // Now we need a rule to build all that. We create a rule, that says
00089         // how to get a .o file from a .cpp file. (Yes I know there are implicitrules..)
00090         Rule* build = mgr.own(new Rule());
00091         build->add();
00092         build->addTarget("%.o");
00093         build->addPrereq("%.cpp");
00094 
00095         // We use Automatic Variables in the command.
00096         // This is a special Varialbe having only a name.
00097         Variable* target = mgr.own(new AutoVariable("@"));
00098         Variable* prereq = mgr.own(new AutoVariable("<"));
00099 
00100         // Now we can add a command to the rule.
00101         build->addCommand(cc->getRef() +" "+ cflags->getRef() + " -o " + target->getRef() +" "+ prereq->getRef());
00102 
00103         // The Makefile singleton has pointers to our added objects. We open a file and
00104         // let it write our makefile into it.
00105         std::ofstream outfile("example.mk");
00106         Makefile::getSingleton().writeMakefile(outfile);
00107         outfile.close();
00108 
00109         // Now run make and look what it does. Maybe it won't work, because the cpp file is somewhere else
00110         // Run make -n -f example.mk to do a dryrun.
00111         // To get a running executable, this has to be linked against libmakefilelib.so.
00112 
00113         return 0;
00114 }

Generated on 1 Nov 2014 for Hugintrunk by  doxygen 1.4.7