-#include <msp/algo.h>
-#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 "package.h"
#include "sourcefile.h"
+#include "sourcepackage.h"
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, FS::relative(s.get_path(), c.get_package().get_source()).str())),
+ comp(c),
+ source(s)
{
- buildable=true;
- add_depend(&src);
+ buildable = true;
+ add_depend(&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()
{
for(TargetList::iterator i=new_deps.begin(); i!=new_deps.end();)
{
- Target *tgt=*i;
+ Target *tgt = *i;
if(tgt->get_depends_ready())
{
- i=new_deps.erase(i);
+ i = new_deps.erase(i);
find_depends(tgt);
}
else
++i;
}
- deps_ready=new_deps.empty();
+ deps_ready = new_deps.empty();
}
-Action *ObjectFile::build()
-{
- return Target::build(new Compile(builder, *this));
-}
-/**
-Recursively looks for header targets and adds them as dependencies.
-*/
void ObjectFile::find_depends(Target *tgt)
{
- 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)
+ SourceFile *src = dynamic_cast<SourceFile *>(tgt->get_real_target());
+ FileTarget *file = dynamic_cast<FileTarget *>(tgt);
+ if(!src || !file)
return;
- const list<string> &includes=src->get_includes();
+ FS::Path spath = FS::dirname(file->get_path());
+ const StringList &incpath = comp.get_build_info().incpath;
+
+ const list<string> &includes = src->get_includes();
for(list<string>::const_iterator i=includes.begin(); i!=includes.end(); ++i)
{
- Target *hdr2=builder.get_header(*i, path, comp.get_build_info().incpath);
- if(hdr2 && !contains(depends, hdr2))
- add_depend(hdr2);
+ Target *hdr = builder.get_header(*i, spath, incpath);
+ if(hdr && find(depends.begin(), depends.end(), hdr)==depends.end())
+ add_depend(hdr);
}
}
-/**
-Adds a target to the dependency list as well as the new dependencies list.
-*/
void ObjectFile::add_depend(Target *tgt)
{
Target::add_depend(tgt);
new_deps.push_back(tgt);
}
-string ObjectFile::generate_target_name(const Component &comp, const string &src)
+Action *ObjectFile::create_action()
+{
+ return new Compile(builder, *this);
+}
+
+FS::Path ObjectFile::generate_target_path(const Component &comp, const string &src)
{
- return (comp.get_package().get_temp_dir()/comp.get_name()/(Path::splitext(src.substr(src.rfind('/')+1)).base+".o")).str();
+ const SourcePackage &pkg = comp.get_package();
+ string fn = FS::basepart(src)+".o";
+ if(!fn.compare(0, 2, "./"))
+ fn.erase(0, 2);
+ for(string::iterator i=fn.begin(); i!=fn.end(); ++i)
+ if(*i=='/')
+ *i = '_';
+ return pkg.get_temp_dir()/comp.get_name()/fn;
}