]> git.tdb.fi Git - builder.git/blob - source/pkgconfiggenerator.cpp
Add a field for a human-readable name for packages
[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         const Package::Requirements &reqs = spkg.get_required_packages();
48         for(Package::Requirements::const_iterator i=reqs.begin(); i!=reqs.end(); ++i)
49                 if((*i)->uses_pkgconfig())
50                         IO::print(out, " %s", (*i)->get_name());
51         out.put('\n');
52
53         const BuildInfo &binfo = spkg.get_exported_build_info();
54         IO::print(out, "Libs:");
55         for(BuildInfo::PathList::const_iterator i=binfo.libpath.begin(); i!=binfo.libpath.end(); ++i)
56                 IO::print(out, " -L%s", prefixify(*i, builder.get_prefix()));
57         for(BuildInfo::WordList::const_iterator i=binfo.libs.begin(); i!=binfo.libs.end(); ++i)
58                 IO::print(out, " -l%s", *i);
59         if(binfo.threads)
60                 out.write("-pthread");
61         out.put('\n');
62
63         IO::print(out, "Cflags:");
64         for(BuildInfo::PathList::const_iterator i=binfo.incpath.begin(); i!=binfo.incpath.end(); ++i)
65                 IO::print(out, " -I%s", prefixify(*i, builder.get_prefix()));
66         for(BuildInfo::DefineMap::const_iterator i=binfo.defines.begin(); i!=binfo.defines.end(); ++i)
67                 if(i->second.empty())
68                         IO::print(out, " -D%s", i->first);
69                 else
70                         IO::print(out, " -D%s=%s", i->first, i->second);
71         out.put('\n');
72
73         status = Task::SUCCESS;
74 }
75
76 string PkgConfigGenerator::Worker::prefixify(const FS::Path &path, const FS::Path &prefix)
77 {
78         if(FS::descendant_depth(path, prefix)>=0)
79         {
80                 FS::Path rel_path = FS::relative(path, prefix);
81                 return "${prefix}"+rel_path.str().substr(1);
82         }
83         else
84                 return path.str();
85 }