]> git.tdb.fi Git - builder.git/blob - source/csourcefile.cpp
Add target and tools for compiling Objective-C sources
[builder.git] / source / csourcefile.cpp
1 #include <msp/core/maputils.h>
2 #include <msp/fs/utils.h>
3 #include <msp/io/print.h>
4 #include <msp/strings/regex.h>
5 #include "builder.h"
6 #include "component.h"
7 #include "csourcefile.h"
8 #include "sourcepackage.h"
9
10 using namespace std;
11 using namespace Msp;
12
13 CSourceFile::CSourceFile(Builder &b, const FS::Path &p):
14         SourceFile(b, p)
15 { }
16
17 CSourceFile::CSourceFile(Builder &b, const Component &c, const FS::Path &p):
18         SourceFile(b, c, p)
19 {
20         string ext = FS::extpart(FS::basename(path));
21         if(ext==".h" || ext==".H" || ext==".hpp")
22                 install_location = FS::Path("include")/package->get_name();
23 }
24
25 void CSourceFile::parse_includes(IO::Base &in)
26 {
27         static Regex r_include("^[ \t]*#include[ \t]+([\"<].*)[\">]");
28
29         string line;
30         while(in.getline(line))
31                 if(RegMatch match = r_include.match(line))
32                         includes.push_back(match[1].str);
33 }
34
35 void CSourceFile::find_dependencies()
36 {
37         if(!component || !mtime)
38                 return;
39
40         const SourcePackage &spkg = component->get_package();
41
42         Cache &cache = spkg.get_cache();
43         if(mtime<cache.get_mtime() && cache.has_key(this, "includes"))
44                 includes = cache.get_values(this, "includes");
45         else
46         {
47                 IO::BufferedFile in(path.str());
48
49                 builder.get_logger().log("files", format("Reading includes from %s", path.str()));
50
51                 parse_includes(in);
52                 cache.set_values(this, "includes", includes);
53         }
54
55         const BuildInfo &build_info = component->get_build_info_for_path(path);
56         const BuildInfo::PathList &incpath = build_info.incpath;
57         BuildInfo::PathList local_incpath = incpath;
58         local_incpath.insert(local_incpath.begin(), build_info.local_incpath.begin(), build_info.local_incpath.end());
59         local_incpath.push_front(FS::dirname(path).str());
60
61         for(IncludeList::iterator i=includes.begin(); i!=includes.end(); ++i)
62         {
63                 Target *hdr = builder.get_vfs().find_header(i->substr(1), ((*i)[0]=='"' ? local_incpath : incpath));
64                 if(hdr)
65                         add_dependency(*hdr);
66         }
67 }
68
69 void CSourceFile::modified()
70 {
71         includes.clear();
72         depends.clear();
73         find_dependencies();
74 }