]> git.tdb.fi Git - builder.git/blob - source/pkgconfiggenerator.cpp
Redesign how tools are run
[builder.git] / source / pkgconfiggenerator.cpp
1 #include <msp/fs/utils.h>
2 #include <msp/io/file.h>
3 #include <msp/io/print.h>
4 #include "builder.h"
5 #include "internaltask.h"
6 #include "pkgconfigfile.h"
7 #include "pkgconfiggenerator.h"
8
9 using namespace std;
10 using namespace Msp;
11
12 PkgConfigGenerator::PkgConfigGenerator(Builder &b):
13         Tool(b, "PCG")
14 {
15         set_run_internal(_run);
16 }
17
18 Target *PkgConfigGenerator::create_target(const vector<Target *> &, const string &)
19 {
20         throw logic_error("Not implemented");
21 }
22
23 bool PkgConfigGenerator::_run(const PkgConfigFile &pkgc)
24 {
25         Builder &builder = pkgc.get_package()->get_builder();
26         const SourcePackage &spkg = *pkgc.get_package();
27
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());
31
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());
35
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());
40         out.put('\n');
41
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);
48         if(binfo.threads)
49                 out.write("-pthread");
50         out.put('\n');
51
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);
58                 else
59                         IO::print(out, " -D%s=%s", kvp.first, kvp.second);
60         out.put('\n');
61
62         return true;
63 }
64
65 string PkgConfigGenerator::prefixify(const FS::Path &path, const FS::Path &prefix)
66 {
67         if(FS::descendant_depth(path, prefix)>=0)
68         {
69                 FS::Path rel_path = FS::relative(path, prefix);
70                 return "${prefix}"+rel_path.str().substr(1);
71         }
72         else
73                 return path.str();
74 }