1 #include <msp/fs/stat.h>
2 #include <msp/fs/utils.h>
3 #include <msp/strings/format.h>
4 #include <msp/strings/utils.h>
5 #include <msp/time/utils.h>
7 #include "filetarget.h"
8 #include "sourcepackage.h"
15 FileTarget::FileTarget(Builder &b, const FS::Path &a):
16 Target(b, generate_name(b, 0, a)),
22 FileTarget::FileTarget(Builder &b, const SourcePackage &p, const FS::Path &a):
23 Target(b, generate_name(b, &p, a)),
29 void FileTarget::init(const SourcePackage *p)
33 nested_build_sig = false;
34 arch_in_build_sig = false;
36 builder.get_vfs().register_path(path, this);
41 void FileTarget::stat()
43 if(FS::Stat st = FS::lstat(path))
45 mtime = st.get_modify_time();
50 string FileTarget::generate_name(Builder &builder, const SourcePackage *pkg, const FS::Path &path)
52 if(pkg && FS::descendant_depth(path, pkg->get_source_directory())>=0)
54 FS::Path relpath = FS::relative(path, pkg->get_source_directory());
55 return format("<%s>%s", pkg->get_name(), relpath.str().substr(1));
57 else if(FS::descendant_depth(path, builder.get_prefix())>=0)
59 FS::Path relpath = FS::relative(path, builder.get_prefix());
60 return "<prefix>"+relpath.str().substr(1);
66 void FileTarget::touch()
70 signal_bubble_rebuild.emit();
73 void FileTarget::check_rebuild()
75 if(!tool || needs_rebuild())
79 mark_rebuild("Does not exist");
82 for(Dependencies::iterator i=depends.begin(); (i!=depends.end() && !needs_rebuild()); ++i)
84 FileTarget *ft = dynamic_cast<FileTarget *>(*i);
85 if(ft && ft->get_mtime()>mtime)
86 mark_rebuild((*i)->get_name()+" has changed");
87 else if((*i)->needs_rebuild())
88 mark_rebuild((*i)->get_name()+" needs rebuilding");
94 // Some side effects might not exist
95 for(Dependencies::iterator i=side_effects.begin(); (i!=side_effects.end() && !needs_rebuild()); ++i)
96 if((*i)->needs_rebuild())
97 mark_rebuild((*i)->get_name()+" needs rebuilding");
100 if(!needs_rebuild() && package)
102 if(package->get_config().get_mtime()>mtime)
103 mark_rebuild("Package options changed");
105 if(tool->get_executable())
107 string build_sig = create_build_signature();
108 if(package->get_cache().has_key(this, "build_sig"))
110 if(package->get_cache().get_value(this, "build_sig")!=build_sig)
111 mark_rebuild("Build signature changed");
117 string FileTarget::create_build_signature() const
122 const BuildInfo &binfo = (component ? component->get_build_info() : package->get_build_info());
124 if(nested_build_sig && component)
126 set<const Tool *> depend_tools;
127 for(Dependencies::const_iterator i=depends.begin(); i!=depends.end(); ++i)
128 if((*i)->get_component()==component && (*i)->get_tool())
129 depend_tools.insert((*i)->get_tool());
131 for(set<const Tool *>::const_iterator i=depend_tools.begin(); i!=depend_tools.end(); ++i)
132 sigs.push_back((*i)->create_build_signature(binfo));
134 sigs.push_front(tool->create_build_signature(binfo));
137 sigs.push_back(tool->create_build_signature(binfo));
139 if(arch_in_build_sig)
140 if(const Architecture *arch = tool->get_architecture())
141 sigs.push_front(arch->get_name());
143 return join(sigs.begin(), sigs.end(), ";");
146 void FileTarget::build(Task &task)
149 task.set_unlink(true);
152 void FileTarget::build_finished(bool success)
159 string build_sig = create_build_signature();
160 if(!build_sig.empty())
161 package->get_cache().set_value(this, "build_sig", build_sig);
165 Target::build_finished(success);
168 void FileTarget::clean()
173 mtime = Time::TimeStamp();