]> git.tdb.fi Git - builder.git/blobdiff - source/builder.cpp
Move the logic for creating targets into the Component class
[builder.git] / source / builder.cpp
index 38a71390ac5b5130a940da0f9c707ec24fa99b29..962429672c213180e7900bd1fd7c1db20e3e57a8 100644 (file)
 #include "action.h"
 #include "analyzer.h"
 #include "builder.h"
-#include "executable.h"
 #include "header.h"
 #include "install.h"
 #include "misc.h"
-#include "objectfile.h"
 #include "package.h"
 #include "pkgconfig.h"
 #include "sharedlibrary.h"
-#include "staticlibrary.h"
 #include "systemlibrary.h"
 #include "unlink.h"
 #include "virtualtarget.h"
@@ -190,9 +187,9 @@ Package *Builder::get_package(const string &n)
 /**
 Returns the target with the given name, or 0 if no such target exists.
 */
-Target *Builder::get_target(const string &n)
+Target *Builder::get_target(const string &n) const
 {
-       TargetMap::iterator i=targets.find(n);
+       TargetMap::const_iterator i=targets.find(n);
        if(i!=targets.end())
                return i->second;
        return 0;
@@ -321,6 +318,16 @@ void Builder::apply_profile_template(Config &config, const string &pt) const
        }
 }
 
+/**
+Adds a target to both the target map and the new target queue.  Called from
+Target constructor.
+*/
+void Builder::add_target(Target *t)
+{
+       targets.insert(TargetMap::value_type(t->get_name(), t));
+       new_tgts.push_back(t);
+}
+
 int Builder::main()
 {
        if(load_build_file(cwd/build_file))
@@ -487,14 +494,11 @@ Creates targets for all packages and prepares them for building.
 int Builder::create_targets()
 {
        Target *world=new VirtualTarget(*this, "world");
-       add_target(world);
 
        Target *def_tgt=new VirtualTarget(*this, "default");
-       add_target(def_tgt);
        world->add_depend(def_tgt);
 
        Target *install=new VirtualTarget(*this, "install");
-       add_target(install);
        world->add_depend(install);
 
        for(PackageMap::iterator i=packages.begin(); i!=packages.end(); ++i)
@@ -504,123 +508,14 @@ int Builder::create_targets()
                if(!i->second->get_buildable())
                        continue;
 
-               Path::Path inst_base;
-               if(i->second->get_buildable() && i->second->get_install_flags())
-                       inst_base=i->second->get_prefix();
-
                const ComponentList &components=i->second->get_components();
                for(ComponentList::const_iterator j=components.begin(); j!=components.end(); ++j)
-               {
-                       // Collect all files belonging to the component
-                       PathList files;
-                       const PathList &sources=j->get_sources();
-                       for(PathList::const_iterator k=sources.begin(); k!=sources.end(); ++k)
-                       {
-                               struct stat st;
-                               stat(*k, st);
-                               if(S_ISDIR(st.st_mode))
-                               {
-                                       list<string> sfiles=list_files(*k);
-                                       for(list<string>::iterator l=sfiles.begin(); l!=sfiles.end(); ++l)
-                                               files.push_back(*k / *l);
-                               }
-                               else
-                                       files.push_back(*k);
-                       }
-
-                       bool build_exe=j->get_type()!=Component::HEADERS;
-
-                       list<ObjectFile *> objs;
-                       for(PathList::iterator k=files.begin(); k!=files.end(); ++k)
-                       {
-                               string basename=(*k)[-1];
-                               string ext=Path::splitext(basename).ext;
-                               if((ext==".cpp" || ext==".c") && build_exe)
-                               {
-                                       SourceFile *src=new SourceFile(*this, &*j, k->str());
-                                       add_target(src);
-
-                                       // Compile sources
-                                       ObjectFile *obj=new ObjectFile(*this, *j, *src);
-                                       add_target(obj);
-                                       objs.push_back(obj);
-                               }
-                               else if(ext==".h")
-                               {
-                                       Target *hdr=get_target(k->str());
-                                       if(!hdr)
-                                       {
-                                               hdr=new Header(*this, &*j, k->str());
-                                               add_target(hdr);
-                                       }
-
-                                       // Install headers if requested
-                                       if(!j->get_install_headers().empty())
-                                       {
-                                               Path::Path inst_path=inst_base/"include"/j->get_install_headers()/basename;
-                                               Install *inst=new Install(*this, *i->second, *hdr, inst_path.str());
-                                               add_target(inst);
-                                               install->add_depend(inst);
-                                       }
-                               }
-                       }
-
-                       if(build_exe)
-                       {
-                               Executable    *exe=0;
-                               StaticLibrary *slib=0;
-                               if(j->get_type()==Component::LIBRARY)
-                               {
-                                       exe=new SharedLibrary(*this, *j, objs);
-                                       slib=new StaticLibrary(*this, *j, objs);
-                                       add_target(slib);
-                               }
-                               else
-                                       exe=new Executable(*this, *j, objs);
-
-                               add_target(exe);
-                               if(i->second==default_pkg && j->get_default())
-                               {
-                                       def_tgt->add_depend(exe);
-                                       if(slib) def_tgt->add_depend(slib);
-                               }
-                               else
-                               {
-                                       world->add_depend(exe);
-                                       if(slib) world->add_depend(slib);
-                               }
-
-                               if(j->get_install())
-                               {
-                                       string inst_dir;
-                                       if(j->get_type()==Component::PROGRAM)
-                                               inst_dir="bin";
-                                       else if(j->get_type()==Component::LIBRARY)
-                                               inst_dir="lib";
-                                       if(!inst_dir.empty())
-                                       {
-                                               Install *inst=new Install(*this, *i->second, *exe, (inst_base/inst_dir/Path::basename(exe->get_name())).str());
-                                               add_target(inst);
-                                               install->add_depend(inst);
-
-                                               if(slib)
-                                               {
-                                                       inst=new Install(*this, *i->second, *slib, (inst_base/inst_dir/Path::basename(slib->get_name())).str());
-                                                       add_target(inst);
-                                                       install->add_depend(inst);
-                                               }
-                                       }
-                               }
-                       }
-               }
+                       j->create_targets();
 
                if(i->second->get_install_flags()&(Package::LIB|Package::INCLUDE))
                {
                        PkgConfig *pc=new PkgConfig(*this, *i->second);
-                       add_target(pc);
-                       Install *inst=new Install(*this, *i->second, *pc, (inst_base/"lib"/"pkgconfig"/Path::basename(pc->get_name())).str());
-                       add_target(inst);
-                       install->add_depend(inst);
+                       install->add_depend(new Install(*this, *i->second, *pc));
                }
        }
 
@@ -648,7 +543,6 @@ int Builder::create_targets()
 
        // Make the cmdline target depend on all targets mentioned on the command line
        Target *cmdline=new VirtualTarget(*this, "cmdline");
-       add_target(cmdline);
        bool build_world=false;
        for(list<string>::iterator i=cmdline_targets.begin(); i!=cmdline_targets.end(); ++i)
        {
@@ -690,7 +584,7 @@ Target *Builder::get_header(const Msp::Path::Path &fn)
 
        if(Path::exists(fn))
        {
-               add_target(tgt=new SystemHeader(*this, fn.str()));
+               tgt=new SystemHeader(*this, fn.str());
                return tgt;
        }
        return 0;
@@ -736,7 +630,6 @@ Target *Builder::get_library(const string &lib, const string &arch, const Path::
                else if(Path::exists(full))
                {
                        tgt=new SystemLibrary(*this, full);
-                       add_target(tgt);
                        return tgt;
                }
        }
@@ -744,15 +637,6 @@ Target *Builder::get_library(const string &lib, const string &arch, const Path::
        return 0;
 }
 
-/**
-Adds a target to both the target map and the new target queue.
-*/
-void Builder::add_target(Target *t)
-{
-       targets.insert(TargetMap::value_type(t->get_name(), t));
-       new_tgts.push_back(t);
-}
-
 /**
 Updates a hash with a string.  This is used from get_header and get_library.
 */