]> git.tdb.fi Git - builder.git/blob - source/install.cpp
a0029ac1eed33244fea945686d43347d3a8b6628
[builder.git] / source / install.cpp
1 /* $Id$
2
3 This file is part of builder
4 Copyright © 2006-2009  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <msp/fs/utils.h>
9 #include "builder.h"
10 #include "copy.h"
11 #include "executable.h"
12 #include "header.h"
13 #include "install.h"
14 #include "package.h"
15 #include "pkgconfig.h"
16 #include "sharedlibrary.h"
17 #include "staticlibrary.h"
18
19 using namespace std;
20 using namespace Msp;
21
22 Install::Install(Builder &b, const SourcePackage &p, FileTarget &s, const std::string &loc):
23         FileTarget(b, &p, generate_target_path(s, loc)),
24         source(s)
25 {
26         buildable=true;
27         add_depend(&source);
28 }
29
30 void Install::check_rebuild()
31 {
32         if(!mtime)
33                 mark_rebuild("Does not exist");
34         else if(source.get_mtime()>mtime)
35                 mark_rebuild(FS::basename(source.get_name())+" has changed");
36         else if(source.get_rebuild())
37                 mark_rebuild(FS::basename(source.get_name())+" needs rebuilding");
38 }
39
40 Action *Install::create_action()
41 {
42         return new Copy(builder, *package, source.get_path(), path);
43 }
44
45 FS::Path Install::generate_target_path(const FileTarget &tgt, const std::string &loc)
46 {
47         if(!tgt.get_package())
48                 throw InvalidParameterValue("Can't install package-less targets");
49
50         FS::Path base=tgt.get_package()->get_builder().get_prefix();
51         string tgtname=FS::basename(tgt.get_path());
52
53         string mid;
54         if(!loc.empty())
55                 mid=loc;
56         else if(const Header *hdr=dynamic_cast<const Header *>(&tgt))
57         {
58                 if(hdr->get_component()->get_type()!=Component::HEADERS)
59                         throw Exception("Header install from non-header component?");
60                 mid="include/"+hdr->get_component()->get_name();
61         }
62         else if(dynamic_cast<const Executable *>(&tgt))
63                 mid="bin";
64         else if(const SharedLibrary *shlib=dynamic_cast<const SharedLibrary *>(&tgt))
65         {
66                 const Component &comp=shlib->get_component();
67                 if(comp.get_type()==Component::LIBRARY)
68                         mid="lib";
69                 else if(comp.get_type()==Component::MODULE)
70                         mid="lib/"+tgt.get_package()->get_name();
71         }
72         else if(dynamic_cast<const StaticLibrary *>(&tgt))
73                 mid="lib";
74         else if(dynamic_cast<const PkgConfig *>(&tgt))
75                 mid="lib/pkgconfig";
76
77         if(mid.empty())
78                 throw InvalidParameterValue("Don't know where to install "+tgtname);
79
80         return (base/mid/tgtname).str();
81 }