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