]> git.tdb.fi Git - builder.git/blob - plugins/builtin/pkgconfiggenerator.cpp
Allow plugins to create additional targets in SourcePackages
[builder.git] / plugins / builtin / pkgconfiggenerator.cpp
1 #include <msp/builder/builder.h>
2 #include <msp/fs/utils.h>
3 #include <msp/io/file.h>
4 #include <msp/io/print.h>
5 #include "pkgconfigfile.h"
6 #include "pkgconfiggenerator.h"
7
8 using namespace std;
9 using namespace Msp;
10
11 PkgConfigGenerator::PkgConfigGenerator(Builder &b):
12         Tool(b, "PCG")
13 {
14         set_run_internal(_run);
15 }
16
17 Target *PkgConfigGenerator::create_target(const vector<Target *> &, const string &)
18 {
19         throw logic_error("Not implemented");
20 }
21
22 bool PkgConfigGenerator::_run(const PkgConfigFile &pkgc)
23 {
24         Builder &builder = pkgc.get_package()->get_builder();
25         const SourcePackage &spkg = *pkgc.get_package();
26
27         IO::BufferedFile out(pkgc.get_path().str(), IO::M_WRITE);
28         IO::print(out, "prefix=%s\n", builder.get_prefix().str());
29         IO::print(out, "source=%s\n\n", spkg.get_source_directory());
30
31         IO::print(out, "Name: %s\n", spkg.get_label());
32         IO::print(out, "Description: %s\n", spkg.get_description());
33         IO::print(out, "Version: %s\n", spkg.get_version());
34
35         IO::print(out, "Requires:");
36         for(const Package *r: spkg.get_required_packages())
37                 if(r->uses_pkgconfig())
38                         IO::print(out, " %s", r->get_name());
39         out.put('\n');
40
41         const BuildInfo &binfo = spkg.get_exported_build_info();
42         IO::print(out, "Libs:");
43         for(const FS::Path &p: binfo.libpath)
44                 IO::print(out, " -L%s", prefixify(p, builder.get_prefix()));
45         for(const string &l: binfo.libs)
46                 IO::print(out, " -l%s", l);
47         if(binfo.threads)
48                 out.write("-pthread");
49         out.put('\n');
50
51         IO::print(out, "Cflags:");
52         for(const FS::Path &p: binfo.incpath)
53                 IO::print(out, " -I%s", prefixify(p, builder.get_prefix()));
54         for(const auto &kvp: binfo.defines)
55                 if(kvp.second.empty())
56                         IO::print(out, " -D%s", kvp.first);
57                 else
58                         IO::print(out, " -D%s=%s", kvp.first, kvp.second);
59         out.put('\n');
60
61         return true;
62 }
63
64 string PkgConfigGenerator::prefixify(const FS::Path &path, const FS::Path &prefix)
65 {
66         if(FS::descendant_depth(path, prefix)>=0)
67         {
68                 FS::Path rel_path = FS::relative(path, prefix);
69                 return "${prefix}"+rel_path.str().substr(1);
70         }
71         else
72                 return path.str();
73 }