]> git.tdb.fi Git - builder.git/blob - source/objectfile.cpp
809e70d40ba769b5ec4e64b00c5ad75557d78dbb
[builder.git] / source / objectfile.cpp
1 /* $Id$
2
3 This file is part of builder
4 Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <msp/path/utils.h>
9 #include "builder.h"
10 #include "compile.h"
11 #include "component.h"
12 #include "install.h"
13 #include "objectfile.h"
14 #include "sourcefile.h"
15 #include "sourcepackage.h"
16
17 using namespace std;
18 using namespace Msp;
19
20 ObjectFile::ObjectFile(Builder &b, const Component &c, SourceFile &src):
21         Target(b, &c.get_package(), generate_target_name(c, src.get_name())),
22         comp(c)
23 {
24         buildable=true;
25         add_depend(&src);
26 }
27
28 /**
29 Processes as many new dependences as possible.  Some may be left unprocessed
30 if their own dependencies are not ready, requiring another call to this
31 function.  Use the get_deps_ready() function to determine whether this is the
32 case.
33 */
34 void ObjectFile::find_depends()
35 {
36         for(TargetList::iterator i=new_deps.begin(); i!=new_deps.end();)
37         {
38                 Target *tgt=*i;
39                 if(tgt->get_depends_ready())
40                 {
41                         i=new_deps.erase(i);
42                         find_depends(tgt);
43                 }
44                 else
45                         ++i;
46         }
47
48         deps_ready=new_deps.empty();
49 }
50
51 Action *ObjectFile::build()
52 {
53         return Target::build(new Compile(builder, *this));
54 }
55
56 /**
57 Recursively looks for header targets and adds them as dependencies.
58 */
59 void ObjectFile::find_depends(Target *tgt)
60 {
61         const string &tname=tgt->get_name();
62         string path=tname.substr(0, tname.rfind('/'));
63
64         SourceFile *src=dynamic_cast<SourceFile *>(tgt);
65         if(!src)
66         {
67                 Install *inst=dynamic_cast<Install *>(tgt);
68                 if(inst)
69                         src=dynamic_cast<SourceFile *>(inst->get_depends().front());
70         }
71         if(!src)
72                 return;
73
74         const string &arch=comp.get_package().get_arch();
75         const StringList &incpath=comp.get_build_info().incpath;
76
77         const list<string> &includes=src->get_includes();
78         for(list<string>::const_iterator i=includes.begin(); i!=includes.end(); ++i)
79         {
80                 Target *hdr2=builder.get_header(*i, arch, path, incpath);
81                 if(hdr2 && find(depends.begin(), depends.end(), hdr2)==depends.end())
82                         add_depend(hdr2);
83         }
84 }
85
86 /**
87 Adds a target to the dependency list as well as the new dependencies list.
88 */
89 void ObjectFile::add_depend(Target *tgt)
90 {
91         Target::add_depend(tgt);
92         new_deps.push_back(tgt);
93 }
94
95 string ObjectFile::generate_target_name(const Component &comp, const string &src)
96 {
97         return (comp.get_package().get_temp_dir()/comp.get_name()/(Path::splitext(src.substr(src.rfind('/')+1)).base+".o")).str();
98 }