]> git.tdb.fi Git - builder.git/commitdiff
Refactor transitive dependencies to work on all targets master
authorMikko Rasa <tdb@tdb.fi>
Sun, 12 Mar 2023 13:23:06 +0000 (15:23 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sun, 12 Mar 2023 13:48:17 +0000 (15:48 +0200)
plugins/datafile/datacollection.cpp
plugins/datafile/datacollection.h
source/lib/filetarget.cpp
source/lib/filetarget.h
source/lib/objectfile.cpp
source/lib/objectfile.h
source/lib/target.cpp
source/lib/target.h

index 189eb61eac2e60d7eed86072862a17a190a15925..b2384a3f9130c5080833db88e3ff53fbc6311a71 100644 (file)
@@ -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");
 }
 {
        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);
-}
index 2fca294a72c610c2c13596e0e6d25f08f56d0c71..8ae54d7729addb3912002345d236f0672e971b71 100644 (file)
@@ -18,9 +18,6 @@ private:
 public:
        const char *get_type() const override { return "DataCollection"; }
        DataTransform &get_source() const { return source; }
 public:
        const char *get_type() const override { return "DataCollection"; }
        DataTransform &get_source() const { return source; }
-
-private:
-       void find_dependencies() override;
 };
 
 #endif
 };
 
 #endif
index 7ab13c3d5bf93833580a687982d7dd2acbdcc2ce..570f5f499ec22b1e957c526515f3ad4dfd1fe3ef 100644 (file)
@@ -64,6 +64,39 @@ void FileTarget::touch()
        signal_bubble_rebuild.emit();
 }
 
        signal_bubble_rebuild.emit();
 }
 
+Target *FileTarget::resolve_transitive_dependency(Target &from, Target &dep) const
+{
+       FileTarget *from_file = dynamic_cast<FileTarget *>(&from);
+       FileTarget *dep_file = dynamic_cast<FileTarget *>(&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())
 void FileTarget::check_rebuild()
 {
        if(!tool || needs_rebuild())
index 4a969b81b2d138a6164d4ce3942896e28ab40fd1..4261cbb7e8617616e75b7240345457adef892cd4 100644 (file)
@@ -40,6 +40,8 @@ public:
        FileTarget *get_real_target() override { return this; }
 
 protected:
        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;
        void check_rebuild() override;
 
        virtual std::string create_build_signature() const;
index 46ec607119d82c10511eef32ef23e9d50657b958..34f2f8c0b80f575241395f7e0bd493cad5e85228 100644 (file)
@@ -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()));
 }
        Target::collect_build_info(binfo);
        binfo.update_from(component->get_build_info_for_path(source.get_path()));
 }
-
-void ObjectFile::find_dependencies()
-{
-       vector<FileTarget *> headers;
-       find_dependencies(source, headers);
-       for(FileTarget *h: headers)
-               if(!any_equals(depends, static_cast<Target *>(h)))
-               {
-                       add_dependency(*h);
-                       if(h->get_real_target()->is_buildable())
-                               h->signal_modified.connect(sigc::mem_fun(this, static_cast<void (ObjectFile::*)()>(&ObjectFile::find_dependencies)));
-               }
-}
-
-void ObjectFile::find_dependencies(FileTarget &tgt, vector<FileTarget *> &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<FileTarget *>(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<FileTarget *>(d))
-               {
-                       auto i = lower_bound(headers, file);
-                       if(i==headers.end() || *i!=file)
-                       {
-                               headers.insert(i, file);
-                               find_dependencies(*file, headers);
-                       }
-               }
-}
index 27c9b3339c5801c693004f056f88215b35ffa88c..7cf11d59d31f577c2e42d9d3accc8cfbe2d84442 100644 (file)
@@ -28,11 +28,6 @@ public:
        bool is_used_in_shared_library() const { return used_in_shlib; }
 
        void collect_build_info(BuildInfo &) const override;
        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<FileTarget *> &);
 };
 
 #endif
 };
 
 #endif
index 558b3b2c107485594cb8831c40d5cdffb2c238fb..0d823eef6618814c01043f9a71805b562ccad536 100644 (file)
@@ -48,6 +48,50 @@ void Target::add_side_effect(Target &se)
        se.signal_bubble_rebuild.connect(sigc::mem_fun(this, &Target::check_rebuild));
 }
 
        se.signal_bubble_rebuild.connect(sigc::mem_fun(this, &Target::check_rebuild));
 }
 
+bool Target::find_transitive_dependencies()
+{
+       vector<Target *> 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<Target *> &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)
 Target *Target::get_buildable_target()
 {
        if(primary_target)
@@ -70,7 +114,16 @@ Target *Target::get_buildable_target()
        }
 
        if(self_ok)
        }
 
        if(self_ok)
+       {
+               if(rescan_trans_deps)
+               {
+                       rescan_trans_deps = false;
+                       if(find_transitive_dependencies())
+                               return get_buildable_target();
+               }
+
                return this;
                return this;
+       }
 
        return 0;
 }
 
        return 0;
 }
@@ -150,6 +203,7 @@ void Target::prepare()
                tool->prepare();
 
        find_dependencies();
                tool->prepare();
 
        find_dependencies();
+       find_transitive_dependencies();
        bool broken = !problems.empty();
 
        if(tool)
        bool broken = !problems.empty();
 
        if(tool)
index 1ae268cce54b5c7096c48aa6d9c8a7a665e50785..c6c8baa89d6e5046001afbf4ffb8c3c628316e99 100644 (file)
@@ -67,6 +67,7 @@ protected:
        Dependencies trans_depends;
        Dependencies side_effects;
        Target *primary_target = 0;
        Dependencies trans_depends;
        Dependencies side_effects;
        Target *primary_target = 0;
+       bool rescan_trans_deps = false;
 
        static std::vector<Target *> prepare_stack;
 
 
        static std::vector<Target *> prepare_stack;
 
@@ -96,6 +97,13 @@ protected:
        direct dependencies first. */
        virtual void find_dependencies() { }
 
        direct dependencies first. */
        virtual void find_dependencies() { }
 
+private:
+       bool find_transitive_dependencies();
+       void find_transitive_dependencies(Target &, std::vector<Target *> &) 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; }
 public:
        /// Returns the dependencies of the target, in the order they were added.
        const Dependencies &get_dependencies() const { return depends; }