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