1 #include <msp/fs/utils.h>
2 #include <msp/io/file.h>
3 #include <msp/io/print.h>
5 #include "internaltask.h"
6 #include "pkgconfigfile.h"
7 #include "pkgconfiggenerator.h"
12 Target *PkgConfigGenerator::create_target(const vector<Target *> &, const string &)
14 throw logic_error("Not implemented");
17 Task *PkgConfigGenerator::run(const Target &target) const
19 const PkgConfigFile &pkgc = dynamic_cast<const PkgConfigFile &>(target);
20 return new InternalTask([&pkgc]{ return _run(pkgc); });
23 bool PkgConfigGenerator::_run(const PkgConfigFile &pkgc)
25 Builder &builder = pkgc.get_package()->get_builder();
26 const SourcePackage &spkg = *pkgc.get_package();
28 IO::BufferedFile out(pkgc.get_path().str(), IO::M_WRITE);
29 IO::print(out, "prefix=%s\n", builder.get_prefix().str());
30 IO::print(out, "source=%s\n\n", spkg.get_source_directory());
32 IO::print(out, "Name: %s\n", spkg.get_label());
33 IO::print(out, "Description: %s\n", spkg.get_description());
34 IO::print(out, "Version: %s\n", spkg.get_version());
36 IO::print(out, "Requires:");
37 for(const Package *r: spkg.get_required_packages())
38 if(r->uses_pkgconfig())
39 IO::print(out, " %s", r->get_name());
42 const BuildInfo &binfo = spkg.get_exported_build_info();
43 IO::print(out, "Libs:");
44 for(const FS::Path &p: binfo.libpath)
45 IO::print(out, " -L%s", prefixify(p, builder.get_prefix()));
46 for(const string &l: binfo.libs)
47 IO::print(out, " -l%s", l);
49 out.write("-pthread");
52 IO::print(out, "Cflags:");
53 for(const FS::Path &p: binfo.incpath)
54 IO::print(out, " -I%s", prefixify(p, builder.get_prefix()));
55 for(const auto &kvp: binfo.defines)
56 if(kvp.second.empty())
57 IO::print(out, " -D%s", kvp.first);
59 IO::print(out, " -D%s=%s", kvp.first, kvp.second);
65 string PkgConfigGenerator::prefixify(const FS::Path &path, const FS::Path &prefix)
67 if(FS::descendant_depth(path, prefix)>=0)
69 FS::Path rel_path = FS::relative(path, prefix);
70 return "${prefix}"+rel_path.str().substr(1);