]> git.tdb.fi Git - builder.git/blobdiff - source/component.cpp
Collect libpath for static library dependencies from the libs themselves
[builder.git] / source / component.cpp
index 26a0ab4b7246d5c24eccd6f26c494fa0fea54335..4e7f547f36f7d05b9212a09f957907e1c79c75a8 100644 (file)
-/* $Id$
-
-This file is part of builder
-Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
-#include <msp/core/except.h>
+#include <algorithm>
 #include <msp/fs/dir.h>
 #include <msp/fs/stat.h>
 #include <msp/fs/utils.h>
+#include <msp/io/print.h>
 #include <msp/strings/lexicalcast.h>
+#include "booleanevaluator.h"
 #include "builder.h"
 #include "component.h"
-#include "header.h"
-#include "install.h"
+#include "csourcefile.h"
+#include "datapack.h"
+#include "executable.h"
+#include "file.h"
 #include "objectfile.h"
 #include "sharedlibrary.h"
 #include "sourcepackage.h"
 #include "staticlibrary.h"
+#include "tarball.h"
 #include "target.h"
+#include "tool.h"
+#include "toolchain.h"
 
 using namespace std;
 using namespace Msp;
 
 Component::Component(SourcePackage &p, Type t, const string &n):
-       pkg(p),
+       package(p),
        type(t),
        name(n),
        install(false),
-       module_host(0),
-       modular(false),
        deflt(true)
 { }
 
-/**
-Prepares the build information for building.
-*/
+void Component::prepare()
+{
+       for(Package::Requirements::const_iterator i=requires.begin(); i!=requires.end(); ++i)
+               (*i)->prepare();
+}
+
 void Component::create_build_info()
 {
-       build_info.add(pkg.get_build_info());
+       BuildInfo final_build_info;
+
+       const Package::Requirements &pkg_reqs = package.get_required_packages();
+       Package::Requirements direct_reqs = requires;
+       direct_reqs.insert(direct_reqs.end(), pkg_reqs.begin(), pkg_reqs.end());
+
+       Package::Requirements all_reqs = direct_reqs;
+       for(Package::Requirements::iterator i=all_reqs.begin(); i!=all_reqs.end(); ++i)
+       {
+               BuildInfo::UpdateLevel level = BuildInfo::CHAINED;
+               if(find(direct_reqs.begin(), direct_reqs.end(), *i)!=direct_reqs.end())
+                       level = BuildInfo::DEPENDENCY;
+               final_build_info.update_from((*i)->get_exported_build_info(), level);
+
+               const Package::Requirements &reqs = (*i)->get_required_packages();
+               for(Package::Requirements::const_iterator j=reqs.begin(); j!=reqs.end(); ++j)
+                       if(find(all_reqs.begin(), all_reqs.end(), *j)==all_reqs.end())
+                               all_reqs.push_back(*j);
+       }
 
-       for(PackageList::iterator i=requires.begin(); i!=requires.end(); ++i)
-               build_info.add((*i)->get_exported_binfo());
+       final_build_info.update_from(package.get_build_info());
+       final_build_info.update_from(build_info);
+       build_info = final_build_info;
 
-       for(StringList::iterator i=build_info.incpath.begin(); i!=build_info.incpath.end(); ++i)
-               *i=(pkg.get_source() / *i).str();
-       for(StringList::iterator i=build_info.libpath.begin(); i!=build_info.libpath.end(); ++i)
-               *i=(pkg.get_source() / *i).str();
+       for(BuildInfo::PathList::iterator i=build_info.incpath.begin(); i!=build_info.incpath.end(); ++i)
+               *i = (package.get_source_directory() / *i).str();
+       for(BuildInfo::PathList::iterator i=build_info.libpath.begin(); i!=build_info.libpath.end(); ++i)
+               *i = (package.get_source_directory() / *i).str();
 
-       if(pkg.get_library_mode()!=DYNAMIC)
+       for(UseList::const_iterator i=uses.begin(); i!=uses.end(); ++i)
        {
-               // XXX This may pull in some unnecessary libpaths too.  More thought required.
-               PackageList reqs=pkg.collect_requires();
-               for(PackageList::iterator i=reqs.begin(); i!=reqs.end(); ++i)
+               /* Select an include path that contains all the sources for this and the
+               used component.  This should produce a sensible result in most cases. */
+               FS::Path base;
+               for(SourceList::const_iterator j=sources.begin(); j!=sources.end(); ++j)
+                       base = base.empty() ? *j : FS::common_ancestor(base, *j);
+               const SourceList &use_sources = (*i)->get_sources();
+               for(SourceList::const_iterator j=use_sources.begin(); j!=use_sources.end(); ++j)
+                       base = FS::common_ancestor(base, *j);
+               build_info.incpath.push_back(base);
+               build_info.libs.push_back((*i)->get_name());
+               if(!(*i)->get_install())
                {
-                       const BuildInfo &ebi=(*i)->get_exported_binfo();
-                       build_info.libpath.insert(build_info.libpath.end(), ebi.libpath.begin(), ebi.libpath.end());
+                       build_info.libmodes[(*i)->get_name()] = BuildInfo::STATIC;
+                       build_info.libpath.push_back((*i)->get_package().get_source_directory());
                }
        }
 
-       if(type==PROGRAM)
-       {
-               string strip=pkg.get_config().get_option("strip").value;
-               if(lexical_cast<bool>(strip))
-                       build_info.ldflags.push_back("-s");
-       }
+       if(type==LIBRARY || type==MODULE)
+               if(build_info.libmode<BuildInfo::DYNAMIC)
+                       build_info.libmode = BuildInfo::DYNAMIC;
+}
 
-       if(modular)
-       {
-               build_info.ldflags.push_back("-rdynamic");
-               build_info.libs.push_back("dl");
-       }
-       else if(module_host)
+BuildInfo Component::get_build_info_for_path(const FS::Path &path) const
+{
+       // XXX Cache these and check that the directories actually exist before adding them
+       BuildInfo binfo = build_info;
+       if(!overlays.empty())
        {
-               const PathList &host_src=module_host->get_sources();
-               for(PathList::const_iterator i=host_src.begin(); i!=host_src.end(); ++i)
-                       build_info.incpath.push_back(i->str());
-       }
+               FS::Path dir = FS::dirname(path);
+               string last = FS::basename(dir);
+               for(OverlayList::const_iterator i=overlays.begin(); i!=overlays.end(); ++i)
+                       if(last==*i)
+                       {
+                               dir = FS::dirname(dir);
+                               break;
+                       }
 
-       build_info.unique();
+               for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
+                       if(dir==*i)
+                       {
+                               binfo.local_incpath.push_back(dir);
+                               for(OverlayList::const_iterator j=overlays.begin(); j!=overlays.end(); ++j)
+                                       binfo.local_incpath.push_back(*i/ *j);
+                       }
+       }
+       return binfo;
 }
 
-/**
-Creates all targets needed for building this component.
-*/
 void Component::create_targets() const
 {
-       Builder &builder=pkg.get_builder();
-       Target *world=builder.get_target("world");
-       Target *def_tgt=builder.get_target("default");
+       Builder &builder = package.get_builder();
+       BuildGraph &build_graph = builder.get_build_graph();
+       const Toolchain &toolchain = builder.get_toolchain();
 
-       PathList files=collect_source_files();
+       SourceList source_filenames = collect_source_files();
 
-       bool build_exe=(type!=HEADERS);
-
-       list<ObjectFile *> objs;
-       list<Target *> inst_tgts;
-       for(PathList::const_iterator i=files.begin(); i!=files.end(); ++i)
+       string inst_loc;
+       if(type==TARBALL)
        {
-               string ext=FS::extpart(FS::basename(*i));
-               if((ext==".cpp" || ext==".c") && build_exe)
+               Tool &tar = toolchain.get_tool("TAR");
+
+               list<Target *> files;
+               for(SourceList::const_iterator i=source_filenames.begin(); i!=source_filenames.end(); ++i)
+               {
+                       FileTarget *file = builder.get_vfs().get_target(*i);
+                       if(!file)
+                               file = new File(builder, package, *i);
+                       files.push_back(file);
+               }
+
+               string tarname = name;
+               if(name=="@src")
                {
-                       SourceFile *src=new SourceFile(builder, this, i->str());
+                       tarname = package.get_name()+"-"+package.get_version();
+                       files.insert(files.begin(), &package.get_build_file());
 
-                       // Compile sources
-                       ObjectFile *obj=new ObjectFile(builder, *this, *src);
-                       objs.push_back(obj);
+                       const BuildGraph::TargetMap &targets = build_graph.get_targets();
+                       for(BuildGraph::TargetMap::const_iterator i=targets.begin(); i!=targets.end(); ++i)
+                               if(i->second->get_package()==&package && !i->second->is_buildable())
+                                       if(find(files.begin(), files.end(), i->second)==files.end())
+                                               files.push_back(i->second);
                }
-               else if(ext==".h")
+
+               Target *result = tar.create_target(files, tarname);
+
+               build_graph.get_target("tarballs")->add_dependency(*result);
+
+               return;
+       }
+       else if(type==INSTALL)
+       {
+               Target *inst = build_graph.get_target("install");
+               Tool &copy = toolchain.get_tool("CP");
+               for(SourceList::const_iterator i=source_filenames.begin(); i!=source_filenames.end(); ++i)
                {
-                       Target *hdr=builder.get_target(i->str());
-                       if(!hdr)
-                               hdr=new Header(builder, this, i->str());
+                       FileTarget *ft;
+                       if(Target *tgt = builder.get_vfs().get_target(*i))
+                               ft = dynamic_cast<FileTarget *>(tgt);
+                       else
+                               ft = new File(builder, package, *i);
+                       inst->add_dependency(*copy.create_target(*ft, name));
+               }
+       }
+       else if(type==DATAPACK)
+       {
+               Tool &dcomp = toolchain.get_tool("DATA");
 
-                       // Install headers if requested
-                       if(!install_headers.empty())
-                               inst_tgts.push_back(hdr);
+               list<Target *> files;
+               for(SourceList::const_iterator i=source_filenames.begin(); i!=source_filenames.end(); ++i)
+               {
+                       string ext = FS::extpart(FS::basename(*i));
+                       if(ext==".mdt")
+                       {
+                               Target *src = dcomp.create_source(*this, *i);
+                               files.push_back(dcomp.create_target(*src, "collection"));
+                       }
+                       else if(Target *tgt = builder.get_vfs().get_target(*i))
+                               files.push_back(tgt);
+                       else
+                               files.push_back(new File(builder, package, *i));
                }
+
+               Target *result = dcomp.create_target(files, "pack");
+
+               build_graph.add_primary_target(*result);
+               if(install)
+                       build_graph.add_installed_target(*result);
        }
 
-       if(build_exe)
+       if(type==PROGRAM || type==LIBRARY || type==MODULE)
        {
-               Executable    *exe=0;
-               StaticLibrary *slib=0;
-               if(type==LIBRARY)
+               list<Target *> objs;
+               const Toolchain &pkg_tools = package.get_toolchain();
+               for(SourceList::const_iterator i=source_filenames.begin(); i!=source_filenames.end(); ++i)
                {
-                       exe=new SharedLibrary(builder, *this, objs);
-                       slib=new StaticLibrary(builder, *this, objs);
+                       string ext = FS::extpart(FS::basename(*i));
+                       Target *src = 0;
+
+                       Tool *gen = pkg_tools.get_tool_for_suffix(ext);
+                       if(gen)
+                       {
+                               Target *tmpl = gen->create_source(*this, *i);
+                               if(tmpl)
+                               {
+                                       src = gen->create_target(*tmpl);
+                                       ext = FS::extpart(FS::basename(dynamic_cast<FileTarget &>(*src).get_path()));
+                               }
+                       }
+
+                       Tool *tool = toolchain.get_tool_for_suffix(ext, true);
+                       if(tool)
+                       {
+                               if(!src)
+                                       src = tool->create_source(*this, *i);
+                               if(!src)
+                                       continue;
+
+                               if(tool->accepts_suffix(ext))
+                               {
+                                       Target *obj = tool->create_target(*src);
+                                       objs.push_back(obj);
+                               }
+
+                               if(type==LIBRARY && install)
+                               {
+                                       if(dynamic_cast<FileTarget *>(src)->is_installable())
+                                               build_graph.add_installed_target(*src);
+
+                                       const Target::Dependencies &side_effects = src->get_side_effects();
+                                       for(Target::Dependencies::const_iterator j=side_effects.begin(); j!=side_effects.end(); ++j)
+                                               if(dynamic_cast<FileTarget *>(*j)->is_installable())
+                                                       build_graph.add_installed_target(**j);
+                               }
+                       }
                }
-               else
-                       exe=new Executable(builder, *this, objs);
 
-               if(&pkg==builder.get_main_package() && deflt)
+               Tool &linker = toolchain.get_tool("LINK");
+
+               list<Target *> results;
+               if(type==LIBRARY)
                {
-                       def_tgt->add_depend(exe);
-                       if(slib) def_tgt->add_depend(slib);
+                       Tool &archiver = toolchain.get_tool("AR");
+                       results.push_back(linker.create_target(objs, "shared"));
+                       results.push_back(archiver.create_target(objs));
                }
+               else if(type==MODULE)
+                       results.push_back(linker.create_target(objs, "shared"));
                else
-               {
-                       world->add_depend(exe);
-                       if(slib) world->add_depend(slib);
-               }
+                       results.push_back(linker.create_target(objs));
 
-               if(install)
+               for(list<Target *>::const_iterator i=results.begin(); i!=results.end(); ++i)
                {
-                       inst_tgts.push_back(exe);
-                       if(slib)
-                               inst_tgts.push_back(slib);
+                       build_graph.add_primary_target(**i);
+                       if(install)
+                               build_graph.add_installed_target(**i);
                }
        }
-
-       Target *inst_tgt=builder.get_target("install");
-       for(TargetList::const_iterator i=inst_tgts.begin(); i!=inst_tgts.end(); ++i)
-               inst_tgt->add_depend(new Install(builder, pkg, **i));
 }
 
-/**
-Collects all files belonging to the component.
-*/
-PathList Component::collect_source_files() const
+Component::SourceList Component::collect_source_files() const
 {
-       PathList files;
-       for(PathList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
+       SourceList files;
+       for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
        {
-               if(FS::is_dir(*i))
+               FS::Path path(*i);
+               if(FS::is_dir(path))
                {
-                       list<string> sfiles=list_files(*i);
-                       for(list<string>::iterator j=sfiles.begin(); j!=sfiles.end(); ++j)
-                               files.push_back(*i / *j);
+                       SourceList dirs;
+                       dirs.push_back(path);
+                       for(OverlayList::const_iterator j=overlays.begin(); j!=overlays.end(); ++j)
+                       {
+                               FS::Path opath = path / *j;
+                               if(FS::is_dir(opath))
+                                       dirs.push_back(opath);
+                       }
+                       for(SourceList::const_iterator j=dirs.begin(); j!=dirs.end(); ++j)
+                       {
+                               package.get_builder().get_logger().log("files", format("Traversing %s", *j));
+                               list<string> sfiles = list_files(*j);
+                               for(list<string>::iterator k=sfiles.begin(); k!=sfiles.end(); ++k)
+                                       files.push_back(*j / *k);
+                       }
                }
                else
-                       files.push_back(*i);
+               {
+                       files.push_back(path);
+                       for(OverlayList::const_iterator j=overlays.begin(); j!=overlays.end(); ++j)
+                       {
+                               FS::Path opath = FS::dirname(path)/ *j/FS::basename(path);
+                               if(FS::is_reg(opath))
+                                       files.push_back(opath);
+                       }
+               }
        }
 
        return files;
@@ -178,53 +310,77 @@ PathList Component::collect_source_files() const
 
 
 Component::Loader::Loader(Component &c):
-       comp(c)
+       DataFile::ObjectLoader<Component>(c)
 {
+       add("if_arch",         &Loader::if_arch);
+       add("if_feature",      &Loader::if_feature);
+       add("overlay",         &Loader::overlay);
        add("source",          &Loader::source);
        add("install",         &Component::install);
-       add("install_headers", &Component::install_headers);
+       add("install_map",     &Loader::install_map);
        add("build_info",      &Loader::build_info);
        add("require",         &Loader::require);
-       add("modular",         &Loader::modular);
-       add("host",            &Loader::host);
        add("default",         &Component::deflt);
+       add("use",             &Loader::use);
 }
 
-void Component::Loader::source(const string &s)
+void Component::Loader::build_info()
 {
-       comp.sources.push_back(comp.pkg.get_source()/s);
+       load_sub(obj.build_info);
+}
+
+void Component::Loader::if_arch(const string &cond)
+{
+       BooleanEvaluator eval(sigc::hide<1>(sigc::mem_fun(&obj.package.get_builder().get_current_arch(), &Architecture::match_name)), false);
+       bool match = eval.evaluate(cond);
+       obj.package.get_builder().get_logger().log("configure",
+               format("%s/%s: arch %s %smatched", obj.package.get_name(), obj.name, cond, (match ? "" : "not ")));
+       if(match)
+               load_sub_with(*this);
+}
+
+void Component::Loader::if_feature(const string &cond)
+{
+       BooleanEvaluator eval(sigc::mem_fun(&obj.package, &SourcePackage::match_feature));
+       bool match = eval.evaluate(cond);
+       obj.package.get_builder().get_logger().log("configure",
+               format("%s/%s: feature %s %smatched", obj.package.get_name(), obj.name, cond, (match ? "" : "not ")));
+       if(match)
+               load_sub_with(*this);
+}
+
+void Component::Loader::install_map()
+{
+       load_sub(obj.install_map, obj.package.get_source_directory());
+}
+
+void Component::Loader::overlay(const string &o)
+{
+       obj.overlays.push_back(o);
 }
 
 void Component::Loader::require(const string &n)
 {
-       Package *req=comp.pkg.get_builder().get_package(n);
+       Package *req = obj.package.get_builder().get_package_manager().find_package(n);
        if(req)
-               comp.requires.push_back(req);
+               obj.requires.push_back(req);
+       else
+               obj.problems.push_back(format("Required package %s not found", n));
 }
 
-void Component::Loader::modular()
+void Component::Loader::source(const string &s)
 {
-       if(comp.type!=PROGRAM)
-               throw Msp::Exception("Only programs can be modular");
-       comp.modular=true;
+       obj.sources.push_back((obj.package.get_source_directory()/s).str());
 }
 
-void Component::Loader::host(const string &n)
+void Component::Loader::use(const string &n)
 {
-       const ComponentList &comps=comp.pkg.get_components();
-       for(ComponentList::const_iterator i=comps.begin(); i!=comps.end(); ++i)
-               if(i->get_name()==n)
+       const SourcePackage::ComponentList &components = obj.package.get_components();
+       for(SourcePackage::ComponentList::const_iterator i=components.begin(); i!=components.end(); ++i)
+               if(i->get_name()==n && i->get_type()==LIBRARY)
                {
-                       if(i->get_type()!=PROGRAM || !i->get_modular())
-                               throw Msp::Exception("Module host must be a modular program");
-                       comp.module_host=&*i;
+                       obj.uses.push_back(&*i);
                        return;
                }
-
-       throw Msp::Exception("Unknown component");
-}
-
-void Component::Loader::build_info()
-{
-       load_sub(comp.build_info);
+       throw invalid_argument("Component::Loader::use");
 }