]> git.tdb.fi Git - builder.git/blobdiff - source/filetarget.cpp
Target::prepare shouldn't be virtual
[builder.git] / source / filetarget.cpp
index 37cc65ca06ee282db3b8a62d41c9bc14ba1f8463..9908c0d4d2836c36c5e05e1eda9aa221bab067b2 100644 (file)
@@ -1,27 +1,92 @@
-/* $Id$
-
-This file is part of builder
-Copyright © 2009  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
 #include <msp/fs/stat.h>
 #include <msp/fs/utils.h>
-#include "file.h"
+#include <msp/strings/format.h>
+#include <msp/time/utils.h>
+#include "builder.h"
+#include "filetarget.h"
+#include "sourcepackage.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):
+       Target(b, generate_name(b, 0, a)),
+       path(a)
+{
+       init(0);
+}
+
+FileTarget::FileTarget(Builder &b, const SourcePackage &p, const FS::Path &a):
+       Target(b, generate_name(b, &p, a)),
+       path(a)
+{
+       init(&p);
+}
+
+void FileTarget::init(const SourcePackage *p)
+{
+       size = 0;
+       package = p;
+
+       builder.get_vfs().register_path(path, this);
+
+       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())>=0)
+       {
+               FS::Path relpath = FS::relative(path, pkg->get_source());
+               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());
+               builder.get_logger().log("debug", format("%s %s %s", path, builder.get_prefix(), relpath));
+               return "<prefix>"+relpath.str().substr(1);
+       }
+
+       return path.str();
+}
+
+void FileTarget::touch()
+{
+       mtime = Time::now();
+       signal_bubble_rebuild.emit();
+}
+
+void FileTarget::check_rebuild()
 {
-       struct stat st;
-       if(!FS::stat(path, st))
+       if(!tool)
+               return;
+
+       if(!mtime)
+               mark_rebuild("Does not exist");
+       else
        {
-               mtime = Time::TimeStamp::from_unixtime(st.st_mtime);
-               size = st.st_size;
+               for(Dependencies::iterator i=depends.begin(); (i!=depends.end() && !needs_rebuild()); ++i)
+               {
+                       FileTarget *ft = dynamic_cast<FileTarget *>(*i);
+                       if(ft && ft->get_mtime()>mtime)
+                               mark_rebuild((*i)->get_name()+" has changed");
+                       else if((*i)->needs_rebuild())
+                               mark_rebuild((*i)->get_name()+" needs rebuilding");
+               }
        }
+
+       if(!needs_rebuild() && package && package->get_config().get_mtime()>mtime)
+               mark_rebuild("Package options changed");
+}
+
+Task *FileTarget::build()
+{
+       if(tool && !builder.get_dry_run() && mtime)
+               FS::unlink(path);
+
+       return Target::build();
 }