2 #include <msp/io/print.h>
3 #include <msp/strings/utils.h>
4 #include "binarypackage.h"
6 #include "filetarget.h"
11 BinaryPackage::BinaryPackage(Builder &b, const string &n):
14 use_pkgconfig = false;
17 void BinaryPackage::do_prepare()
19 bool has_relative_paths = false;
20 for(BuildInfo::PathList::const_iterator i=export_binfo.libpath.begin(); (!has_relative_paths && i!=export_binfo.libpath.end()); ++i)
21 has_relative_paths = !i->is_absolute();
22 for(BuildInfo::PathList::const_iterator i=export_binfo.incpath.begin(); (!has_relative_paths && i!=export_binfo.incpath.end()); ++i)
23 has_relative_paths = !i->is_absolute();
27 /* If we have any relative paths that need resolving, or we have no paths at
28 all and are not using pkg-config, look for files in prefix */
29 if(has_relative_paths || (!use_pkgconfig && export_binfo.libpath.empty() && export_binfo.incpath.empty()))
30 bases.push_back(builder.get_prefix());
32 // Always look in system locations
33 bases.push_back(FS::Path());
36 for(list<FS::Path>::const_iterator i=bases.begin(); i!=bases.end(); ++i)
39 system = prefix.empty();
43 const Architecture &arch = builder.get_current_arch();
45 prefix /= arch.get_cross_prefix();
48 BuildInfo::PathList libpath = export_binfo.libpath;
49 if(!system && libpath.empty())
50 libpath.push_back("lib");
51 for(BuildInfo::PathList::iterator j=libpath.begin(); j!=libpath.end(); ++j)
54 bool all_found = true;
55 for(BuildInfo::WordList::const_iterator j=export_binfo.libs.begin(); j!=export_binfo.libs.end(); ++j)
56 all_found &= (builder.get_vfs().find_library(*j, libpath, export_binfo.libmode, system)!=0);
58 BuildInfo::PathList incpath = export_binfo.incpath;
59 if(!system && incpath.empty())
60 incpath.push_back("include");
61 for(BuildInfo::PathList::iterator j=incpath.begin(); j!=incpath.end(); ++j)
64 for(HeaderList::const_iterator j=headers.begin(); j!=headers.end(); ++j)
65 all_found &= (builder.get_vfs().find_header(*j, 0, incpath, system)!=0);
70 builder.get_logger().log("configure", format("%s found in %s", name, ((system && use_pkgconfig) ? "system" : base_path.str())));
77 // TODO report which files were not found
78 problems.push_back("Cannot locate files");
82 /* Add default entries to paths if they're empty and the package was found
83 in a non-system location */
84 if(!system && export_binfo.incpath.empty())
85 export_binfo.incpath.push_back(base_path/"include");
86 if(!system && export_binfo.libpath.empty())
87 export_binfo.libpath.push_back(base_path/"lib");
89 if(has_relative_paths)
91 for(BuildInfo::PathList::iterator i=export_binfo.incpath.begin(); i!=export_binfo.incpath.end(); ++i)
93 for(BuildInfo::PathList::iterator i=export_binfo.libpath.begin(); i!=export_binfo.libpath.end(); ++i)
97 if(!static_binfo.libs.empty())
99 BuildInfo::PathList combined_libpath = static_binfo.libpath;
100 combined_libpath.insert(combined_libpath.end(), export_binfo.libpath.begin(), export_binfo.libpath.end());
102 Target::Dependencies static_dep_libs;
103 for(BuildInfo::WordList::const_iterator i=static_binfo.libs.begin(); i!=static_binfo.libs.end(); ++i)
104 if(Target *lib = builder.get_vfs().find_library(*i, combined_libpath, BuildInfo::STATIC, system))
105 static_dep_libs.push_back(lib);
107 for(BuildInfo::WordList::const_iterator i=export_binfo.libs.begin(); i!=export_binfo.libs.end(); ++i)
108 if(Target *lib = builder.get_vfs().find_library(*i, export_binfo.libpath, BuildInfo::FORCE_STATIC, system))
110 for(Target::Dependencies::const_iterator j=static_dep_libs.begin(); j!=static_dep_libs.end(); ++j)
111 lib->add_transitive_dependency(**j);
116 BinaryPackage *BinaryPackage::from_flags(Builder &builder, const string &name, const Flags &flags, const Flags &static_flags)
118 BinaryPackage *pkg = new BinaryPackage(builder, name);
119 pkg->use_pkgconfig = true;
121 process_flags(flags, pkg->export_binfo);
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);
132 void BinaryPackage::process_flags(const Flags &flags, BuildInfo &binfo)
134 for(Flags::const_iterator i=flags.begin(); i!=flags.end(); ++i)
136 if(!i->compare(0, 2, "-I"))
137 binfo.incpath.push_back(i->substr(2));
138 else if(!i->compare(0, 2, "-D"))
140 string::size_type equals = i->find('=');
141 if(equals!=string::npos)
142 binfo.defines[i->substr(2, equals-2)] = i->substr(equals+1);
144 binfo.defines[i->substr(2)] = string();
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;
156 BinaryPackage::Loader::Loader(BinaryPackage &p):
157 DataFile::DerivedObjectLoader<BinaryPackage, Package::Loader>(p)
159 add("build_info", &Loader::build_info);
160 add("header", &Loader::header);
163 void BinaryPackage::Loader::build_info()
165 load_sub(obj.export_binfo);
168 void BinaryPackage::Loader::header(const string &h)
170 obj.headers.push_back(h);