--- /dev/null
+#include <msp/builder/builder.h>
+#include <msp/fs/utils.h>
+#include <msp/io/file.h>
+#include <msp/io/print.h>
+#include "pkgconfigfile.h"
+#include "pkgconfiggenerator.h"
+
+using namespace std;
+using namespace Msp;
+
+PkgConfigGenerator::PkgConfigGenerator(Builder &b):
+ Tool(b, "PCG")
+{
+ set_run_internal(_run);
+}
+
+Target *PkgConfigGenerator::create_target(const vector<Target *> &, const string &)
+{
+ throw logic_error("Not implemented");
+}
+
+bool PkgConfigGenerator::_run(const PkgConfigFile &pkgc)
+{
+ Builder &builder = pkgc.get_package()->get_builder();
+ const SourcePackage &spkg = *pkgc.get_package();
+
+ IO::BufferedFile out(pkgc.get_path().str(), IO::M_WRITE);
+ IO::print(out, "prefix=%s\n", builder.get_prefix().str());
+ IO::print(out, "source=%s\n\n", spkg.get_source_directory());
+
+ IO::print(out, "Name: %s\n", spkg.get_label());
+ IO::print(out, "Description: %s\n", spkg.get_description());
+ IO::print(out, "Version: %s\n", spkg.get_version());
+
+ IO::print(out, "Requires:");
+ for(const Package *r: spkg.get_required_packages())
+ if(r->uses_pkgconfig())
+ IO::print(out, " %s", r->get_name());
+ out.put('\n');
+
+ const BuildInfo &binfo = spkg.get_exported_build_info();
+ IO::print(out, "Libs:");
+ for(const FS::Path &p: binfo.libpath)
+ IO::print(out, " -L%s", prefixify(p, builder.get_prefix()));
+ for(const string &l: binfo.libs)
+ IO::print(out, " -l%s", l);
+ if(binfo.threads)
+ out.write("-pthread");
+ out.put('\n');
+
+ IO::print(out, "Cflags:");
+ for(const FS::Path &p: binfo.incpath)
+ IO::print(out, " -I%s", prefixify(p, builder.get_prefix()));
+ for(const auto &kvp: binfo.defines)
+ if(kvp.second.empty())
+ IO::print(out, " -D%s", kvp.first);
+ else
+ IO::print(out, " -D%s=%s", kvp.first, kvp.second);
+ out.put('\n');
+
+ return true;
+}
+
+string PkgConfigGenerator::prefixify(const FS::Path &path, const FS::Path &prefix)
+{
+ if(FS::descendant_depth(path, prefix)>=0)
+ {
+ FS::Path rel_path = FS::relative(path, prefix);
+ return "${prefix}"+rel_path.str().substr(1);
+ }
+ else
+ return path.str();
+}