X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Ffiletarget.cpp;h=07be2c9fc806ff234fe77cd1292d4159ea44d871;hb=d1f9551e05c9d341149eb490e05b1465d3d6b711;hp=08b0f3115e984e25d30b73f6d2e21d5a5543236c;hpb=a40db37e1e681d397d4bfc6c893e15945c533b72;p=builder.git diff --git a/source/filetarget.cpp b/source/filetarget.cpp index 08b0f31..07be2c9 100644 --- a/source/filetarget.cpp +++ b/source/filetarget.cpp @@ -1,27 +1,177 @@ -/* $Id$ - -This file is part of builder -Copyright © 2009 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - +#include #include #include -#include "file.h" +#include +#include +#include +#include "builder.h" +#include "filetarget.h" +#include "sourcepackage.h" +#include "task.h" +#include "tool.h" using namespace std; using namespace Msp; -FileTarget::FileTarget(Builder &b, const Package *p, const FS::Path &a): - // XXX Builder depends on target name being its path for locating file targets - Target(b, p, /*FS::basename*/(a.str())), - path(a), - size(0) +FileTarget::FileTarget(Builder &b, const FS::Path &a): + FileTarget(b, 0, a) +{ } + +FileTarget::FileTarget(Builder &b, const SourcePackage &p, const FS::Path &a): + FileTarget(b, &p, a) +{ } + +FileTarget::FileTarget(Builder &b, const SourcePackage *p, const FS::Path &a): + Target(b, generate_name(b, p, a)), + path(a) +{ + package = p; + + builder.get_vfs().register_path(path, this); + + stat(); +} + +void FileTarget::stat() +{ + if(FS::Stat st = FS::lstat(path)) + { + mtime = st.get_modify_time(); + size = st.get_size(); + } +} + +string FileTarget::generate_name(Builder &builder, const SourcePackage *pkg, const FS::Path &path) +{ + if(pkg && FS::descendant_depth(path, pkg->get_source_directory())>=0) + { + FS::Path relpath = FS::relative(path, pkg->get_source_directory()); + return format("<%s>%s", pkg->get_name(), relpath.str().substr(1)); + } + else if(FS::descendant_depth(path, builder.get_prefix())>=0) + { + FS::Path relpath = FS::relative(path, builder.get_prefix()); + return ""+relpath.str().substr(1); + } + + return path.str(); +} + +void FileTarget::touch() +{ + mtime = Time::now(); + modified(); + signal_bubble_rebuild.emit(); +} + +void FileTarget::check_rebuild() +{ + if(!tool || needs_rebuild()) + return; + + if(!mtime) + mark_rebuild("Does not exist"); + else + { + for(Target *d: depends) + { + FileTarget *ft = dynamic_cast(d); + if(ft && ft->get_mtime()>mtime) + mark_rebuild(d->get_name()+" has changed"); + else if(d->needs_rebuild()) + mark_rebuild(d->get_name()+" needs rebuilding"); + if(needs_rebuild()) + break; + } + } + + if(!needs_rebuild()) + { + // Some side effects might not exist + auto i = find_if(side_effects, [](const Target *s){ return s->needs_rebuild(); }); + if(i!=side_effects.end()) + mark_rebuild((*i)->get_name()+" needs rebuilding"); + } + + if(!needs_rebuild() && package) + { + if(package->get_config().get_mtime()>mtime) + mark_rebuild("Package options changed"); + + if(tool->get_executable()) + { + string build_sig = create_build_signature(); + if(package->get_cache().has_key(this, "build_sig")) + { + if(package->get_cache().get_value(this, "build_sig")!=build_sig) + mark_rebuild("Build signature changed"); + } + } + } +} + +string FileTarget::create_build_signature() const +{ + if(!package) + return string(); + + const BuildInfo &binfo = (component ? component->get_build_info() : package->get_build_info()); + vector sigs; + + if(arch_in_build_sig) + if(const Architecture *arch = tool->get_architecture()) + sigs.push_back(arch->get_name()); + + sigs.push_back(tool->create_build_signature(binfo)); + + if(nested_build_sig && component) + { + vector seen_tools; + vector tool_sigs; + for(Target *d: depends) + if(const Tool *t = d->get_tool()) + if(d->get_component()==component && !any_equals(seen_tools, t)) + { + seen_tools.push_back(t); + tool_sigs.push_back(t->create_build_signature(binfo)); + } + + sort(tool_sigs); + sigs.insert(sigs.end(), make_move_iterator(tool_sigs.begin()), make_move_iterator(tool_sigs.end())); + } + + return join(sigs.begin(), sigs.end(), ";"); +} + +void FileTarget::build(Task &task) +{ + task.add_file(path); + task.set_unlink(true); +} + +void FileTarget::build_finished(bool success) +{ + if(success) + { + stat(); + if(package) + { + string build_sig = create_build_signature(); + if(!build_sig.empty()) + package->get_cache().set_value(this, "build_sig", build_sig); + } + } + + Target::build_finished(success); +} + +void FileTarget::clean() { - struct stat st; - if(!FS::stat(path, st)) + if(mtime) { - mtime=Time::TimeStamp::from_unixtime(st.st_mtime); - size=st.st_size; + FS::unlink(path); + mtime = Time::TimeStamp(); + size = 0; + check_rebuild(); } }