From: Mikko Rasa Date: Sun, 12 Mar 2023 13:23:06 +0000 (+0200) Subject: Refactor transitive dependencies to work on all targets X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=HEAD;p=builder.git Refactor transitive dependencies to work on all targets --- diff --git a/plugins/datafile/datacollection.cpp b/plugins/datafile/datacollection.cpp index 189eb61..b2384a3 100644 --- a/plugins/datafile/datacollection.cpp +++ b/plugins/datafile/datacollection.cpp @@ -18,10 +18,3 @@ Msp::FS::Path DataCollection::generate_target_path(const Component &comp, const { return comp.get_package().get_temp_directory()/comp.get_name()/(FS::basepart(FS::basename(src))+".mdc"); } - -void DataCollection::find_dependencies() -{ - source.prepare(); - for(Target *d: source.get_transitive_dependencies()) - add_dependency(*d); -} diff --git a/plugins/datafile/datacollection.h b/plugins/datafile/datacollection.h index 2fca294..8ae54d7 100644 --- a/plugins/datafile/datacollection.h +++ b/plugins/datafile/datacollection.h @@ -18,9 +18,6 @@ private: public: const char *get_type() const override { return "DataCollection"; } DataTransform &get_source() const { return source; } - -private: - void find_dependencies() override; }; #endif diff --git a/source/lib/filetarget.cpp b/source/lib/filetarget.cpp index 7ab13c3..570f5f4 100644 --- a/source/lib/filetarget.cpp +++ b/source/lib/filetarget.cpp @@ -64,6 +64,39 @@ void FileTarget::touch() signal_bubble_rebuild.emit(); } +Target *FileTarget::resolve_transitive_dependency(Target &from, Target &dep) const +{ + FileTarget *from_file = dynamic_cast(&from); + FileTarget *dep_file = dynamic_cast(&dep); + if(!from_file || !dep_file) + return &dep; + + const SourcePackage *from_pkg = from.get_package(); + if(dep_file->get_package()!=from_pkg || FS::descendant_depth(dep_file->get_path(), from_pkg->get_source_directory())<0) + return &dep; + + /* Adjust the path of the dependency to account for the install location + of the dependent target. */ + const Component *from_comp = from_file->get_real_target()->get_component(); + const Component *dep_comp = dep_file->get_component(); + FS::Path from_inst = from_comp->get_install_map().get_install_location(*from_file->get_real_target()); + FS::Path dep_inst = dep_comp->get_install_map().get_install_location(*dep_file); + FS::Path displaced = FS::dirname(from_file->get_path())/FS::relative(dep_inst, from_inst)/FS::basename(dep_file->get_path()); + if(Target *result = builder.get_vfs().get_target(displaced)) + return result; + + /* If the target was in an overlay directory and the displaced + dependency is not found, try removing the overlay from the path. */ + string last_dir = FS::basename(FS::dirname(displaced)); + if(any_equals(dep_comp->get_overlays(), last_dir)) + { + displaced = displaced.subpath(0, displaced.size()-2)/FS::basename(dep_file->get_path()); + return builder.get_vfs().get_target(displaced); + } + + return nullptr; +} + void FileTarget::check_rebuild() { if(!tool || needs_rebuild()) diff --git a/source/lib/filetarget.h b/source/lib/filetarget.h index 4a969b8..4261cbb 100644 --- a/source/lib/filetarget.h +++ b/source/lib/filetarget.h @@ -40,6 +40,8 @@ public: FileTarget *get_real_target() override { return this; } protected: + Target *resolve_transitive_dependency(Target &, Target &) const override; + void check_rebuild() override; virtual std::string create_build_signature() const; diff --git a/source/lib/objectfile.cpp b/source/lib/objectfile.cpp index 46ec607..34f2f8c 100644 --- a/source/lib/objectfile.cpp +++ b/source/lib/objectfile.cpp @@ -34,64 +34,3 @@ void ObjectFile::collect_build_info(BuildInfo &binfo) const Target::collect_build_info(binfo); binfo.update_from(component->get_build_info_for_path(source.get_path())); } - -void ObjectFile::find_dependencies() -{ - vector headers; - find_dependencies(source, headers); - for(FileTarget *h: headers) - if(!any_equals(depends, static_cast(h))) - { - add_dependency(*h); - if(h->get_real_target()->is_buildable()) - h->signal_modified.connect(sigc::mem_fun(this, static_cast(&ObjectFile::find_dependencies))); - } -} - -void ObjectFile::find_dependencies(FileTarget &tgt, vector &headers) -{ - tgt.prepare(); - - FileTarget *rtgt = tgt.get_real_target(); - Dependencies deps_to_add = rtgt->get_transitive_dependencies(); - if(rtgt!=&tgt) - { - FS::Path inst_dir = rtgt->get_component()->get_install_map().get_install_location(*rtgt); - /* The target has been displaced by installing it. Displace any - dependencies that come from the same package as well. */ - const SourcePackage *tpkg = rtgt->get_package(); - for(Target *&d: deps_to_add) - { - FileTarget *file = dynamic_cast(d); - if(file && file->get_package()==tpkg && FS::descendant_depth(file->get_path(), tpkg->get_source_directory())>=0) - { - const Component *tcomp = file->get_component(); - FS::Path dep_inst = tcomp->get_install_map().get_install_location(*file); - FS::Path displaced = FS::dirname(tgt.get_path())/FS::relative(dep_inst, inst_dir)/FS::basename(file->get_path()); - d = builder.get_vfs().get_target(displaced); - if(!d) - { - /* If the target was in an overlay directory and the displaced - dependency is not found, try removing the overlay from the path. */ - string last_dir = FS::basename(FS::dirname(displaced)); - if(any_equals(tcomp->get_overlays(), last_dir)) - { - displaced = displaced.subpath(0, displaced.size()-2)/FS::basename(file->get_path()); - d = builder.get_vfs().get_target(displaced); - } - } - } - } - } - - for(Target *d: deps_to_add) - if(FileTarget *file = dynamic_cast(d)) - { - auto i = lower_bound(headers, file); - if(i==headers.end() || *i!=file) - { - headers.insert(i, file); - find_dependencies(*file, headers); - } - } -} diff --git a/source/lib/objectfile.h b/source/lib/objectfile.h index 27c9b33..7cf11d5 100644 --- a/source/lib/objectfile.h +++ b/source/lib/objectfile.h @@ -28,11 +28,6 @@ public: bool is_used_in_shared_library() const { return used_in_shlib; } void collect_build_info(BuildInfo &) const override; - -private: - void find_dependencies() override; - - void find_dependencies(FileTarget &, std::vector &); }; #endif diff --git a/source/lib/target.cpp b/source/lib/target.cpp index 558b3b2..0d823ee 100644 --- a/source/lib/target.cpp +++ b/source/lib/target.cpp @@ -48,6 +48,50 @@ void Target::add_side_effect(Target &se) se.signal_bubble_rebuild.connect(sigc::mem_fun(this, &Target::check_rebuild)); } +bool Target::find_transitive_dependencies() +{ + vector found_deps; + for(Target *d: depends) + if(!d->is_buildable()) + find_transitive_dependencies(*d, found_deps); + + bool any_added = false; + for(Target *d: found_deps) + if(!any_equals(depends, d)) + { + any_added = true; + add_dependency(*d); + if(d->get_real_target()->is_buildable()) + d->signal_modified.connect([this]{ rescan_trans_deps = true; }); + } + + return any_added; +} + +void Target::find_transitive_dependencies(Target &tgt, vector &found_deps) const +{ + tgt.prepare(); + + Target *rtgt = tgt.get_real_target(); + Dependencies deps_to_add = rtgt->get_transitive_dependencies(); + if(rtgt!=&tgt) + { + for(Target *&d: deps_to_add) + d = resolve_transitive_dependency(tgt, *d); + } + + for(Target *d: deps_to_add) + if(d) + { + auto i = lower_bound(found_deps, d); + if(i==found_deps.end() || *i!=d) + { + found_deps.insert(i, d); + find_transitive_dependencies(*d, found_deps); + } + } +} + Target *Target::get_buildable_target() { if(primary_target) @@ -70,7 +114,16 @@ Target *Target::get_buildable_target() } if(self_ok) + { + if(rescan_trans_deps) + { + rescan_trans_deps = false; + if(find_transitive_dependencies()) + return get_buildable_target(); + } + return this; + } return 0; } @@ -150,6 +203,7 @@ void Target::prepare() tool->prepare(); find_dependencies(); + find_transitive_dependencies(); bool broken = !problems.empty(); if(tool) diff --git a/source/lib/target.h b/source/lib/target.h index 1ae268c..c6c8baa 100644 --- a/source/lib/target.h +++ b/source/lib/target.h @@ -67,6 +67,7 @@ protected: Dependencies trans_depends; Dependencies side_effects; Target *primary_target = 0; + bool rescan_trans_deps = false; static std::vector prepare_stack; @@ -96,6 +97,13 @@ protected: direct dependencies first. */ virtual void find_dependencies() { } +private: + bool find_transitive_dependencies(); + void find_transitive_dependencies(Target &, std::vector &) const; + +protected: + virtual Target *resolve_transitive_dependency(Target &, Target &dep) const { return &dep; } + public: /// Returns the dependencies of the target, in the order they were added. const Dependencies &get_dependencies() const { return depends; }