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