]> git.tdb.fi Git - builder.git/blobdiff - source/objectfile.cpp
More flexible way to manage filename patterns
[builder.git] / source / objectfile.cpp
index 3fd88d25b06f83af74eb8a051e55d4af05a2cf06..1e6cb232135ca489aeeacd2045d86168394b0098 100644 (file)
@@ -1,15 +1,7 @@
-/* $Id$
-
-This file is part of builder
-Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
-#include <msp/path/utils.h>
+#include <algorithm>
+#include <msp/fs/utils.h>
 #include "builder.h"
-#include "compile.h"
 #include "component.h"
-#include "install.h"
 #include "objectfile.h"
 #include "sourcefile.h"
 #include "sourcepackage.h"
@@ -17,82 +9,106 @@ Distributed under the LGPL
 using namespace std;
 using namespace Msp;
 
-ObjectFile::ObjectFile(Builder &b, const Component &c, SourceFile &src):
-       Target(b, &c.get_package(), generate_target_name(c, src.get_name())),
-       comp(c)
+ObjectFile::ObjectFile(Builder &b, const Component &c, SourceFile &s):
+       FileTarget(b, c.get_package(), generate_target_path(c, s.get_path())),
+       source(s),
+       used_in_shlib(false)
 {
-       buildable=true;
-       add_depend(&src);
+       component = &c;
+       add_dependency(source);
 }
 
-/**
-Processes as many new dependences as possible.  Some may be left unprocessed
-if their own dependencies are not ready, requiring another call to this
-function.  Use the get_deps_ready() function to determine whether this is the
-case.
-*/
-void ObjectFile::find_depends()
+FS::Path ObjectFile::generate_target_path(const Component &comp, const FS::Path &src)
 {
-       for(TargetList::iterator i=new_deps.begin(); i!=new_deps.end();)
+       const SourcePackage &pkg = comp.get_package();
+       FS::Path temp_dir = pkg.get_temp_directory()/comp.get_name();
+       FS::Path rel_src;
+       if(FS::descendant_depth(src, temp_dir)>=0)
+               rel_src = FS::relative(src, temp_dir);
+       else
+               rel_src = FS::relative(src, pkg.get_source_directory());
+       string fn;
+       for(FS::Path::Iterator i=rel_src.begin(); i!=rel_src.end(); ++i)
        {
-               Target *tgt=*i;
-               if(tgt->get_depends_ready())
-               {
-                       i=new_deps.erase(i);
-                       find_depends(tgt);
-               }
-               else
-                       ++i;
+               if(!fn.empty())
+                       fn += '_';
+               if(*i!=".")
+                       fn += *i;
        }
-
-       deps_ready=new_deps.empty();
+       const Architecture &arch = comp.get_package().get_builder().get_current_arch();
+       return temp_dir/arch.create_filename<ObjectFile>(FS::basepart(fn));
 }
 
-Action *ObjectFile::build()
+void ObjectFile::set_used_in_shared_library(bool u)
 {
-       return Target::build(new Compile(builder, *this));
+       used_in_shlib = u;
 }
 
-/**
-Recursively looks for header targets and adds them as dependencies.
-*/
-void ObjectFile::find_depends(Target *tgt)
+void ObjectFile::collect_build_info(BuildInfo &binfo) const
 {
-       const string &tname=tgt->get_name();
-       string path=tname.substr(0, tname.rfind('/'));
-
-       SourceFile *src=dynamic_cast<SourceFile *>(tgt);
-       if(!src)
-       {
-               Install *inst=dynamic_cast<Install *>(tgt);
-               if(inst)
-                       src=dynamic_cast<SourceFile *>(inst->get_depends().front());
-       }
-       if(!src)
-               return;
-
-       const StringList &incpath=comp.get_build_info().incpath;
+       Target::collect_build_info(binfo);
+       binfo.update_from(component->get_build_info_for_path(source.get_path()));
+}
 
-       const list<string> &includes=src->get_includes();
-       for(list<string>::const_iterator i=includes.begin(); i!=includes.end(); ++i)
+void ObjectFile::find_dependencies()
+{
+       for(Dependencies::iterator i=depends.begin(); i!=depends.end(); ++i)
        {
-               Target *hdr2=builder.get_header(*i, path, incpath);
-               if(hdr2 && find(depends.begin(), depends.end(), hdr2)==depends.end())
-                       add_depend(hdr2);
+               (*i)->prepare();
+               find_dependencies(dynamic_cast<FileTarget *>(*i));
        }
 }
 
-/**
-Adds a target to the dependency list as well as the new dependencies list.
-*/
-void ObjectFile::add_depend(Target *tgt)
+void ObjectFile::find_dependencies(FileTarget *tgt)
 {
-       Target::add_depend(tgt);
-       new_deps.push_back(tgt);
-}
+       FileTarget *rtgt = dynamic_cast<FileTarget *>(tgt->get_real_target());
+       const Dependencies &tdeps = rtgt->get_transitive_dependencies();
+       Dependencies deps_to_add;
+       if(rtgt==tgt)
+       {
+               /* We are using the target from its original location, so dependencies
+               apply directly */
+               deps_to_add = tdeps;
+       }
+       else
+       {
+               FS::Path inst_dir = rtgt->get_component()->get_install_map().get_install_location(*rtgt);
+               /* The target has been displaced by installing it.  Displace any
+               dependencies that come from the same package as well. */
+               const SourcePackage *tpkg = rtgt->get_package();
+               for(Dependencies::const_iterator i=tdeps.begin(); i!=tdeps.end(); ++i)
+               {
+                       FileTarget *file = dynamic_cast<FileTarget *>(*i);
+                       if(file && file->get_package()==tpkg && FS::descendant_depth(file->get_path(), tpkg->get_source_directory())>=0)
+                       {
+                               const Component *tcomp = file->get_component();
+                               FS::Path dep_inst = tcomp->get_install_map().get_install_location(*file);
+                               FS::Path displaced = FS::dirname(tgt->get_path())/FS::relative(dep_inst, inst_dir)/FS::basename(file->get_path());
+                               if(Target *ddep = builder.get_vfs().get_target(displaced))
+                                       deps_to_add.push_back(ddep);
+                               else
+                               {
+                                       const Component::OverlayList &overlays = tcomp->get_overlays();
+                                       string last_dir = FS::basename(FS::dirname(displaced));
+                                       for(Component::OverlayList::const_iterator j=overlays.begin(); j!=overlays.end(); ++j)
+                                               if(last_dir==*j)
+                                               {
+                                                       displaced = displaced.subpath(0, displaced.size()-2)/FS::basename(file->get_path());
+                                                       if((ddep = builder.get_vfs().get_target(displaced)))
+                                                               deps_to_add.push_back(ddep);
+                                               }
+                               }
+                       }
+                       else
+                               deps_to_add.push_back(*i);
+               }
+       }
 
-string ObjectFile::generate_target_name(const Component &comp, const string &src)
-{
-       const SourcePackage &pkg=comp.get_package();
-       return (pkg.get_temp_dir()/comp.get_name()/(splitext(basename(src)).base+".o")).str();
+       for(Dependencies::const_iterator i=deps_to_add.begin(); i!=deps_to_add.end(); ++i)
+               if(find(depends.begin(), depends.end(), *i)==depends.end())
+               {
+                       add_dependency(**i);
+                       if((*i)->get_real_target()->is_buildable())
+                               (*i)->signal_modified.connect(sigc::mem_fun(this, static_cast<void (ObjectFile::*)()>(&ObjectFile::find_dependencies)));
+               }
 }