2 #include <msp/io/print.h>
3 #include <msp/strings/utils.h>
4 #include "binarypackage.h"
6 #include "filetarget.h"
7 #include "staticlibrary.h"
12 BinaryPackage::BinaryPackage(Builder &b, const string &n):
15 use_pkgconfig = false;
18 void BinaryPackage::do_prepare()
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();
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());
33 // Always look in system locations
34 bases.push_back(FS::Path());
37 for(list<FS::Path>::const_iterator i=bases.begin(); i!=bases.end(); ++i)
40 system = prefix.empty();
44 const Architecture &arch = builder.get_current_arch();
46 prefix /= arch.get_cross_prefix();
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)
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);
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)
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);
71 builder.get_logger().log("configure", format("%s found in %s", name, ((system && use_pkgconfig) ? "system" : base_path.str())));
78 // TODO report which files were not found
79 problems.push_back("Cannot locate files");
83 /* Add default entries to paths if they're empty and the package was found
84 in a non-system location */
85 if(!system && export_binfo.incpath.empty())
86 export_binfo.incpath.push_back(base_path/"include");
87 if(!system && export_binfo.libpath.empty())
88 export_binfo.libpath.push_back(base_path/"lib");
90 if(has_relative_paths)
92 for(BuildInfo::PathList::iterator i=export_binfo.incpath.begin(); i!=export_binfo.incpath.end(); ++i)
94 for(BuildInfo::PathList::iterator i=export_binfo.libpath.begin(); i!=export_binfo.libpath.end(); ++i)
98 if(!static_binfo.libs.empty())
100 for(BuildInfo::WordList::const_iterator i=export_binfo.libs.begin(); i!=export_binfo.libs.end(); ++i)
101 if(Target *lib = builder.get_vfs().find_library(*i, export_binfo.libpath, BuildInfo::FORCE_STATIC, system))
102 if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(lib))
104 for(BuildInfo::WordList::const_iterator j=static_binfo.libs.begin(); j!=static_binfo.libs.end(); ++j)
105 stlib->add_required_library(*j);
110 BinaryPackage *BinaryPackage::from_flags(Builder &builder, const string &name, const Flags &flags, const Flags &static_flags)
112 BinaryPackage *pkg = new BinaryPackage(builder, name);
113 pkg->use_pkgconfig = true;
115 process_flags(flags, pkg->export_binfo);
117 Flags exclusive_static_flags;
118 for(Flags::const_iterator i=static_flags.begin(); i!=static_flags.end(); ++i)
119 if(find(flags.begin(), flags.end(), *i)==flags.end())
120 exclusive_static_flags.push_back(*i);
121 process_flags(exclusive_static_flags, pkg->static_binfo);
126 void BinaryPackage::process_flags(const Flags &flags, BuildInfo &binfo)
128 for(Flags::const_iterator i=flags.begin(); i!=flags.end(); ++i)
130 if(!i->compare(0, 2, "-I"))
131 binfo.incpath.push_back(i->substr(2));
132 else if(!i->compare(0, 2, "-D"))
134 string::size_type equals = i->find('=');
135 if(equals!=string::npos)
136 binfo.defines[i->substr(2, equals-2)] = i->substr(equals+1);
138 binfo.defines[i->substr(2)] = string();
140 else if(!i->compare(0, 2, "-L"))
141 binfo.libpath.push_back(i->substr(2));
142 else if(!i->compare(0, 2, "-l"))
143 binfo.libs.push_back(i->substr(2));
144 else if(*i=="-pthread")
145 binfo.threads = true;
150 BinaryPackage::Loader::Loader(BinaryPackage &p):
151 DataFile::DerivedObjectLoader<BinaryPackage, Package::Loader>(p)
153 add("build_info", &Loader::build_info);
154 add("header", &Loader::header);
157 void BinaryPackage::Loader::build_info()
159 load_sub(obj.export_binfo);
162 void BinaryPackage::Loader::header(const string &h)
164 obj.headers.push_back(h);