]> git.tdb.fi Git - builder.git/blob - source/install.cpp
Add support for building datafiles
[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 "datafile.h"
13 #include "header.h"
14 #include "install.h"
15 #include "package.h"
16 #include "pkgconfig.h"
17 #include "sharedlibrary.h"
18 #include "staticlibrary.h"
19
20 using namespace std;
21 using namespace Msp;
22
23 Install::Install(Builder &b, const SourcePackage &p, FileTarget &s, const std::string &loc):
24         FileTarget(b, &p, generate_target_path(s, loc)),
25         source(s)
26 {
27         buildable=true;
28         add_depend(&source);
29 }
30
31 void Install::check_rebuild()
32 {
33         if(!mtime)
34                 mark_rebuild("Does not exist");
35         else if(source.get_mtime()>mtime)
36                 mark_rebuild(FS::basename(source.get_name())+" has changed");
37         else if(source.get_rebuild())
38                 mark_rebuild(FS::basename(source.get_name())+" needs rebuilding");
39 }
40
41 Action *Install::create_action()
42 {
43         return new Copy(builder, *package, source.get_path(), path);
44 }
45
46 FS::Path Install::generate_target_path(const FileTarget &tgt, const std::string &loc)
47 {
48         if(!tgt.get_package())
49                 throw InvalidParameterValue("Can't install package-less targets");
50
51         FS::Path base=tgt.get_package()->get_builder().get_prefix();
52         string tgtname=FS::basename(tgt.get_path());
53
54         string mid;
55         if(!loc.empty())
56                 mid=loc;
57         else if(const Header *hdr=dynamic_cast<const Header *>(&tgt))
58         {
59                 if(hdr->get_component()->get_type()!=Component::HEADERS)
60                         throw Exception("Header install from non-header component?");
61                 mid="include/"+hdr->get_component()->get_name();
62         }
63         else if(dynamic_cast<const Executable *>(&tgt))
64                 mid="bin";
65         else if(const SharedLibrary *shlib=dynamic_cast<const SharedLibrary *>(&tgt))
66         {
67                 const Component &comp=shlib->get_component();
68                 if(comp.get_type()==Component::LIBRARY)
69                         mid="lib";
70                 else if(comp.get_type()==Component::MODULE)
71                         mid="lib/"+tgt.get_package()->get_name();
72         }
73         else if(dynamic_cast<const StaticLibrary *>(&tgt))
74                 mid="lib";
75         else if(dynamic_cast<const PkgConfig *>(&tgt))
76                 mid="lib/pkgconfig";
77         else if(dynamic_cast<const ::DataFile *>(&tgt))
78                 mid="share/"+tgt.get_package()->get_name();
79
80         if(mid.empty())
81                 throw InvalidParameterValue("Don't know where to install "+tgtname);
82
83         return (base/mid/tgtname).str();
84 }