]> git.tdb.fi Git - builder.git/blob - source/binarypackage.cpp
Locate and check the existence of the libraries of binary packages
[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
21         list<FS::Path> bases;
22
23         /* If we have any relative paths that need resolving, or we have no paths at
24         all and are not using pkg-config, look for files in prefix */
25         if(has_relative_paths || (!use_pkgconfig && export_binfo.libpath.empty()))
26                 bases.push_back(builder.get_prefix());
27
28         // Always look in system locations
29         bases.push_back(FS::Path());
30
31         bool system = false;
32         for(list<FS::Path>::const_iterator i=bases.begin(); i!=bases.end(); ++i)
33         {
34                 FS::Path prefix = *i;
35                 system = prefix.empty();
36                 if(system)
37                 {
38                         prefix = "/usr";
39                         const Architecture &arch = builder.get_current_arch();
40                         if(arch.is_cross())
41                                 prefix /= arch.get_cross_prefix();
42                 }
43
44                 BuildInfo::PathList libpath = export_binfo.libpath;
45                 if(!system && libpath.empty())
46                         libpath.push_back("lib");
47                 for(BuildInfo::PathList::iterator j=libpath.begin(); j!=libpath.end(); ++j)
48                         *j = prefix/ *j;
49
50                 bool all_found = true;
51                 for(BuildInfo::WordList::const_iterator j=export_binfo.libs.begin(); j!=export_binfo.libs.end(); ++j)
52                         all_found &= (builder.get_vfs().find_library(*j, libpath, export_binfo.libmode, system)!=0);
53
54                 if(all_found)
55                 {
56                         base_path = prefix;
57                         builder.get_logger().log("configure", format("%s found in %s", name, ((system && use_pkgconfig) ? "system" : base_path.str())));
58                         break;
59                 }
60         }
61
62         if(base_path.empty())
63         {
64                 builder.problem(name, "Cannot locate files");
65                 return;
66         }
67
68         /* Add default entries to paths if they're empty and the package was found
69         in a non-system location */
70         if(!system && export_binfo.incpath.empty())
71                 export_binfo.incpath.push_back(base_path/"include");
72         if(!system && export_binfo.libpath.empty())
73                 export_binfo.libpath.push_back(base_path/"lib");
74
75         for(BuildInfo::PathList::iterator i=export_binfo.incpath.begin(); i!=export_binfo.incpath.end(); ++i)
76                 *i = base_path/ *i;
77         for(BuildInfo::PathList::iterator i=export_binfo.libpath.begin(); i!=export_binfo.libpath.end(); ++i)
78                 *i = base_path/ *i;
79 }
80
81 BinaryPackage *BinaryPackage::from_flags(Builder &builder, const string &name, const vector<string> &flags)
82 {
83         BinaryPackage *pkg = new BinaryPackage(builder, name);
84         pkg->use_pkgconfig = true;
85         BuildInfo &binfo = pkg->export_binfo;
86
87         for(vector<string>::const_iterator i=flags.begin(); i!=flags.end(); ++i)
88         {
89                 if(!i->compare(0, 2, "-I"))
90                         binfo.incpath.push_back(i->substr(2));
91                 else if(!i->compare(0, 2, "-D"))
92                 {
93                         string::size_type equals = i->find('=');
94                         if(equals!=string::npos)
95                                 binfo.defines[i->substr(2, equals-2)] = i->substr(equals+1);
96                         else
97                                 binfo.defines[i->substr(2)] = string();
98                 }
99                 else if(!i->compare(0, 2, "-L"))
100                         binfo.libpath.push_back(i->substr(2));
101                 else if(!i->compare(0, 2, "-l"))
102                         binfo.libs.push_back(i->substr(2));
103                 else if(*i=="-pthread")
104                         binfo.threads = true;
105         }
106
107         return pkg;
108 }
109
110
111 BinaryPackage::Loader::Loader(BinaryPackage &p):
112         DataFile::DerivedObjectLoader<BinaryPackage, Package>(p)
113 {
114         add("build_info", &Loader::build_info);
115 }
116
117 void BinaryPackage::Loader::build_info()
118 {
119         load_sub(obj.export_binfo);
120 }