]> git.tdb.fi Git - builder.git/blob - source/binarypackage.cpp
94667c989dede480f3b36afbddb04108f84881cb
[builder.git] / source / binarypackage.cpp
1 #include <msp/io/print.h>
2 #include <msp/strings/utils.h>
3 #include "binarypackage.h"
4 #include "builder.h"
5
6 using namespace std;
7 using namespace Msp;
8
9 BinaryPackage::BinaryPackage(Builder &b, const string &n):
10         Package(b, n)
11 {
12         use_pkgconfig = false;
13 }
14
15 void BinaryPackage::do_prepare()
16 {
17         bool has_relative_paths = false;
18         for(BuildInfo::PathList::const_iterator i=export_binfo.libpath.begin(); (!has_relative_paths && i!=export_binfo.libpath.end()); ++i)
19                 has_relative_paths = !i->is_absolute();
20         for(BuildInfo::PathList::const_iterator i=export_binfo.incpath.begin(); (!has_relative_paths && i!=export_binfo.incpath.end()); ++i)
21                 has_relative_paths = !i->is_absolute();
22
23         list<FS::Path> bases;
24
25         /* If we have any relative paths that need resolving, or we have no paths at
26         all and are not using pkg-config, look for files in prefix */
27         if(has_relative_paths || (!use_pkgconfig && export_binfo.libpath.empty() && export_binfo.incpath.empty()))
28                 bases.push_back(builder.get_prefix());
29
30         // Always look in system locations
31         bases.push_back(FS::Path());
32
33         bool system = false;
34         for(list<FS::Path>::const_iterator i=bases.begin(); i!=bases.end(); ++i)
35         {
36                 FS::Path prefix = *i;
37                 system = prefix.empty();
38                 if(system)
39                 {
40                         prefix = "/usr";
41                         const Architecture &arch = builder.get_current_arch();
42                         if(arch.is_cross())
43                                 prefix /= arch.get_cross_prefix();
44                 }
45
46                 BuildInfo::PathList libpath = export_binfo.libpath;
47                 if(!system && libpath.empty())
48                         libpath.push_back("lib");
49                 for(BuildInfo::PathList::iterator j=libpath.begin(); j!=libpath.end(); ++j)
50                         *j = prefix/ *j;
51
52                 bool all_found = true;
53                 for(BuildInfo::WordList::const_iterator j=export_binfo.libs.begin(); j!=export_binfo.libs.end(); ++j)
54                         all_found &= (builder.get_vfs().find_library(*j, libpath, export_binfo.libmode, system)!=0);
55
56                 BuildInfo::PathList incpath = export_binfo.incpath;
57                 if(!system && incpath.empty())
58                         incpath.push_back("include");
59                 for(BuildInfo::PathList::iterator j=incpath.begin(); j!=incpath.end(); ++j)
60                         *j = prefix/ *j;
61
62                 for(HeaderList::const_iterator j=headers.begin(); j!=headers.end(); ++j)
63                         all_found &= (builder.get_vfs().find_header(*j, 0, incpath, system)!=0);
64
65                 if(all_found)
66                 {
67                         base_path = prefix;
68                         builder.get_logger().log("configure", format("%s found in %s", name, ((system && use_pkgconfig) ? "system" : base_path.str())));
69                         break;
70                 }
71         }
72
73         if(base_path.empty())
74         {
75                 // TODO report which files were not found
76                 problems.push_back("Cannot locate files");
77                 return;
78         }
79
80         /* Add default entries to paths if they're empty and the package was found
81         in a non-system location */
82         if(!system && export_binfo.incpath.empty())
83                 export_binfo.incpath.push_back(base_path/"include");
84         if(!system && export_binfo.libpath.empty())
85                 export_binfo.libpath.push_back(base_path/"lib");
86
87         if(has_relative_paths)
88         {
89                 for(BuildInfo::PathList::iterator i=export_binfo.incpath.begin(); i!=export_binfo.incpath.end(); ++i)
90                         *i = base_path/ *i;
91                 for(BuildInfo::PathList::iterator i=export_binfo.libpath.begin(); i!=export_binfo.libpath.end(); ++i)
92                         *i = base_path/ *i;
93         }
94 }
95
96 BinaryPackage *BinaryPackage::from_flags(Builder &builder, const string &name, const vector<string> &flags)
97 {
98         BinaryPackage *pkg = new BinaryPackage(builder, name);
99         pkg->use_pkgconfig = true;
100         BuildInfo &binfo = pkg->export_binfo;
101
102         for(vector<string>::const_iterator i=flags.begin(); i!=flags.end(); ++i)
103         {
104                 if(!i->compare(0, 2, "-I"))
105                         binfo.incpath.push_back(i->substr(2));
106                 else if(!i->compare(0, 2, "-D"))
107                 {
108                         string::size_type equals = i->find('=');
109                         if(equals!=string::npos)
110                                 binfo.defines[i->substr(2, equals-2)] = i->substr(equals+1);
111                         else
112                                 binfo.defines[i->substr(2)] = string();
113                 }
114                 else if(!i->compare(0, 2, "-L"))
115                         binfo.libpath.push_back(i->substr(2));
116                 else if(!i->compare(0, 2, "-l"))
117                         binfo.libs.push_back(i->substr(2));
118                 else if(*i=="-pthread")
119                         binfo.threads = true;
120         }
121
122         return pkg;
123 }
124
125
126 BinaryPackage::Loader::Loader(BinaryPackage &p):
127         DataFile::DerivedObjectLoader<BinaryPackage, Package::Loader>(p)
128 {
129         add("build_info", &Loader::build_info);
130         add("header",     &Loader::header);
131 }
132
133 void BinaryPackage::Loader::build_info()
134 {
135         load_sub(obj.export_binfo);
136 }
137
138 void BinaryPackage::Loader::header(const string &h)
139 {
140         obj.headers.push_back(h);
141 }