1 #include <msp/core/algorithm.h>
2 #include <msp/fs/stat.h>
3 #include <msp/fs/utils.h>
4 #include <msp/strings/format.h>
5 #include <msp/strings/utils.h>
6 #include <msp/time/utils.h>
8 #include "filetarget.h"
9 #include "sourcepackage.h"
16 FileTarget::FileTarget(Builder &b, const FS::Path &a):
17 Target(b, generate_name(b, 0, a)),
23 FileTarget::FileTarget(Builder &b, const SourcePackage &p, const FS::Path &a):
24 Target(b, generate_name(b, &p, a)),
30 void FileTarget::init(const SourcePackage *p)
34 nested_build_sig = false;
35 arch_in_build_sig = false;
37 builder.get_vfs().register_path(path, this);
42 void FileTarget::stat()
44 if(FS::Stat st = FS::lstat(path))
46 mtime = st.get_modify_time();
51 string FileTarget::generate_name(Builder &builder, const SourcePackage *pkg, const FS::Path &path)
53 if(pkg && FS::descendant_depth(path, pkg->get_source_directory())>=0)
55 FS::Path relpath = FS::relative(path, pkg->get_source_directory());
56 return format("<%s>%s", pkg->get_name(), relpath.str().substr(1));
58 else if(FS::descendant_depth(path, builder.get_prefix())>=0)
60 FS::Path relpath = FS::relative(path, builder.get_prefix());
61 return "<prefix>"+relpath.str().substr(1);
67 void FileTarget::touch()
71 signal_bubble_rebuild.emit();
74 void FileTarget::check_rebuild()
76 if(!tool || needs_rebuild())
80 mark_rebuild("Does not exist");
83 for(Target *d: depends)
85 FileTarget *ft = dynamic_cast<FileTarget *>(d);
86 if(ft && ft->get_mtime()>mtime)
87 mark_rebuild(d->get_name()+" has changed");
88 else if(d->needs_rebuild())
89 mark_rebuild(d->get_name()+" needs rebuilding");
97 // Some side effects might not exist
98 auto i = find_if(side_effects, [](const Target *s){ return s->needs_rebuild(); });
99 if(i!=side_effects.end())
100 mark_rebuild((*i)->get_name()+" needs rebuilding");
103 if(!needs_rebuild() && package)
105 if(package->get_config().get_mtime()>mtime)
106 mark_rebuild("Package options changed");
108 if(tool->get_executable())
110 string build_sig = create_build_signature();
111 if(package->get_cache().has_key(this, "build_sig"))
113 if(package->get_cache().get_value(this, "build_sig")!=build_sig)
114 mark_rebuild("Build signature changed");
120 string FileTarget::create_build_signature() const
125 const BuildInfo &binfo = (component ? component->get_build_info() : package->get_build_info());
127 if(nested_build_sig && component)
129 set<const Tool *> depend_tools;
130 for(Target *d: depends)
131 if(d->get_component()==component && d->get_tool())
132 depend_tools.insert(d->get_tool());
134 for(const Tool *t: depend_tools)
135 sigs.push_back(t->create_build_signature(binfo));
137 sigs.push_front(tool->create_build_signature(binfo));
140 sigs.push_back(tool->create_build_signature(binfo));
142 if(arch_in_build_sig)
143 if(const Architecture *arch = tool->get_architecture())
144 sigs.push_front(arch->get_name());
146 return join(sigs.begin(), sigs.end(), ";");
149 void FileTarget::build(Task &task)
152 task.set_unlink(true);
155 void FileTarget::build_finished(bool success)
162 string build_sig = create_build_signature();
163 if(!build_sig.empty())
164 package->get_cache().set_value(this, "build_sig", build_sig);
168 Target::build_finished(success);
171 void FileTarget::clean()
176 mtime = Time::TimeStamp();