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