]> git.tdb.fi Git - builder.git/blobdiff - source/installedfile.cpp
Add support for statically linking the C++ standard library
[builder.git] / source / installedfile.cpp
index 2a96066c9202e6deadcc5addfa842fe7b0783308..6e45ad27efc66ac65d8e9643b1763c75a4811a17 100644 (file)
@@ -1,31 +1,61 @@
+#include <msp/fs/stat.h>
 #include <msp/fs/utils.h>
 #include "builder.h"
-#include "copy.h"
-#include "executable.h"
-#include "datafile.h"
-#include "header.h"
 #include "installedfile.h"
-#include "package.h"
-#include "pkgconfig.h"
 #include "sharedlibrary.h"
-#include "staticlibrary.h"
 
 using namespace std;
 using namespace Msp;
 
-InstalledFile::InstalledFile(Builder &b, const SourcePackage &p, FileTarget &s, const std::string &loc):
-       FileTarget(b, &p, generate_target_path(s, loc)),
+InstalledFile::InstalledFile(Builder &b, const SourcePackage &p, FileTarget &s, const string &loc):
+       FileTarget(b, p, generate_target_path(b.get_prefix(), s, loc)),
        source(s)
 {
-       buildable = true;
-       add_depend(&source);
+       add_dependency(source);
+}
+
+FS::Path InstalledFile::generate_target_path(const FS::Path &global_prefix, const FileTarget &tgt, const string &loc)
+{
+       if(!tgt.is_installable() && loc.empty())
+               throw invalid_argument(tgt.get_name()+" is not installable");
+
+       FS::Path prefix;
+       FS::Path mid;
+       if(!loc.compare(0, 2, "//"))
+       {
+               if(!tgt.get_package())
+                       throw invalid_argument("No private install location for "+tgt.get_name());
+
+               prefix = tgt.get_package()->get_temp_directory();
+               mid = loc.substr(2);
+       }
+       else
+       {
+               prefix = global_prefix;
+
+               if(!loc.empty())
+                       mid = loc;
+               else if(const Component *comp = tgt.get_component())
+                       mid = comp->get_install_map().get_install_location(tgt);
+       }
+
+       if(mid.empty())
+               mid = tgt.get_install_location();
+
+       string fn = tgt.get_install_filename();
+       if(fn.empty())
+               fn = FS::basename(tgt.get_path());
 
-       if(const SharedLibrary *shlib = dynamic_cast<const SharedLibrary *>(&source))
-               if(!shlib->get_soname().empty())
-                       link = FS::dirname(path)/FS::basename(shlib->get_path());
+       return prefix/mid/fn;
+}
 
-       if(!link.empty())
-               builder.get_vfs().register_path(link, this);
+void InstalledFile::set_symlink(const FS::Path &l)
+{
+       FS::Path al = FS::dirname(path)/l;
+       if(al==path)
+               throw invalid_argument("InstalledFile::set_symlink");
+       link = FS::dirname(path)/l;
+       builder.get_vfs().register_path(link, this);
 }
 
 Target *InstalledFile::get_real_target()
@@ -39,50 +69,24 @@ void InstalledFile::check_rebuild()
                mark_rebuild("Does not exist");
        else if(source.get_mtime()>mtime || source.get_size()!=size)
                mark_rebuild(source.get_name()+" has changed");
-       else if(source.get_rebuild())
+       else if(source.needs_rebuild())
                mark_rebuild(source.get_name()+" needs rebuilding");
-}
-
-FS::Path InstalledFile::generate_target_path(const FileTarget &tgt, const std::string &loc)
-{
-       if(!tgt.get_package())
-               throw invalid_argument("Can't install package-less targets");
-
-       FS::Path base = tgt.get_package()->get_builder().get_prefix();
-       string tgtname = FS::basename(tgt.get_path());
-
-       string mid;
-       if(!loc.empty())
-               mid = loc;
-       else if(const Header *hdr = dynamic_cast<const Header *>(&tgt))
+       if(!needs_rebuild() && !link.empty())
        {
-               if(hdr->get_component()->get_type()!=Component::HEADERS)
-                       throw logic_error("Header install from non-header component?");
-               mid = "include/"+hdr->get_component()->get_name();
-       }
-       else if(dynamic_cast<const Executable *>(&tgt))
-               mid = "bin";
-       else if(const SharedLibrary *shlib = dynamic_cast<const SharedLibrary *>(&tgt))
-       {
-               const Component &comp = shlib->get_component();
-               if(comp.get_type()==Component::LIBRARY)
+               if(!FS::exists(link))
+                       mark_rebuild("Symlink does not exist");
+               else
                {
-                       mid = "lib";
-                       if(!shlib->get_soname().empty())
-                               tgtname = shlib->get_soname();
+                       FS::Path rel_path = FS::relative(path, FS::dirname(link));
+                       if(FS::readlink(link)!=rel_path)
+                               mark_rebuild("Symlink needs updating");
                }
-               else if(comp.get_type()==Component::MODULE)
-                       mid = "lib/"+tgt.get_package()->get_name();
        }
-       else if(dynamic_cast<const StaticLibrary *>(&tgt))
-               mid = "lib";
-       else if(dynamic_cast<const PkgConfig *>(&tgt))
-               mid = "lib/pkgconfig";
-       else if(dynamic_cast<const ::DataFile *>(&tgt))
-               mid = "share/"+tgt.get_package()->get_name();
-
-       if(mid.empty())
-               throw invalid_argument("Don't know where to install "+tgtname);
+}
 
-       return base/mid/tgtname;
+void InstalledFile::clean()
+{
+       if(!link.empty() && mtime)
+               FS::unlink(link);
+       FileTarget::clean();
 }