]> git.tdb.fi Git - builder.git/blob - source/mingwdlltool.cpp
Add utility class for executing chains of tasks
[builder.git] / source / mingwdlltool.cpp
1 #include <cstdlib>
2 #include <msp/fs/utils.h>
3 #include <msp/strings/format.h>
4 #include "builder.h"
5 #include "component.h"
6 #include "exportdefinitions.h"
7 #include "externaltask.h"
8 #include "importlibrary.h"
9 #include "installedfile.h"
10 #include "mingwdlltool.h"
11 #include "objectfile.h"
12 #include "sharedlibrary.h"
13 #include "sourcepackage.h"
14
15 using namespace std;
16 using namespace Msp;
17
18 MingwDllTool::MingwDllTool(Builder &b, const Architecture &a):
19         Tool(b, a, "DLL")
20 {
21         set_command("dlltool", true);
22 }
23
24 Target *MingwDllTool::create_target(const list<Target *> &sources, const string &)
25 {
26         if(sources.size()!=1)
27                 throw invalid_argument("MingwDllTool::create_target");
28         SharedLibrary &shlib = dynamic_cast<SharedLibrary &>(*sources.front());
29
30         list<ObjectFile *> objs;
31         const Target::Dependencies &depends = shlib.get_dependencies();
32         for(Target::Dependencies::const_iterator i=depends.begin(); i!=depends.end(); ++i)
33                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(*i))
34                         objs.push_back(obj);
35
36         ExportDefinitions *exp = new ExportDefinitions(builder, *shlib.get_component(), objs);
37         exp->set_tool(*this);
38
39         ImportLibrary *imp = new ImportLibrary(builder, *shlib.get_component(), shlib, *exp);
40         imp->set_tool(*this);
41
42         return imp;
43 }
44
45 Target *MingwDllTool::create_install(Target &target) const
46 {
47         if(ImportLibrary *imp = dynamic_cast<ImportLibrary *>(&target))
48         {
49                 Tool &copy = builder.get_toolchain().get_tool("CP");
50                 InstalledFile *inst_tgt = dynamic_cast<InstalledFile *>(copy.create_target(target));
51                 string link_name = format("lib%s.dll.a", imp->get_shared_library()->get_libname());
52                 if(link_name!=FS::basename(inst_tgt->get_path()))
53                         inst_tgt->set_symlink(link_name);
54                 return inst_tgt;
55         }
56         else
57                 return 0;
58 }
59
60 Task *MingwDllTool::run(const Target &target) const
61 {
62         const ImportLibrary *imp = dynamic_cast<const ImportLibrary *>(&target);
63         const ExportDefinitions *exp = 0;
64         if(imp)
65                 exp = &dynamic_cast<const ExportDefinitions &>(*imp->get_dependencies().front());
66         else
67                 exp = dynamic_cast<const ExportDefinitions *>(&target);
68         if(!imp && !exp)
69                 throw invalid_argument("MingwDllTool::run");
70
71         vector<string> argv;
72         argv.push_back(executable->get_path().str());
73
74         /* dlltool is stupid and puts temporary files in the working directory by
75         default */
76         argv.push_back("--temp-prefix");
77         char random[8];
78         for(unsigned i=0; i<8; ++i)
79                 random[i] = 'a'+(rand()%26);
80         argv.push_back(string("/tmp/")+string(random, 8));
81
82         const Component &comp = *target.get_component();
83         FS::Path work_dir = comp.get_package().get_source_directory();
84
85         if(imp)
86         {
87                 const SharedLibrary &shlib = *imp->get_shared_library();
88
89                 argv.push_back("-d");
90                 argv.push_back(relative(exp->get_path(), work_dir).str());
91
92                 argv.push_back("-D");
93                 if(shlib.get_install_filename().empty())
94                         argv.push_back(FS::basename(shlib.get_path()));
95                 else
96                         argv.push_back(shlib.get_install_filename());
97
98                 argv.push_back("-l");
99                 argv.push_back(relative(imp->get_path(), work_dir).str());
100         }
101         else
102         {
103                 const Target::Dependencies &depends = exp->get_dependencies();
104                 for(Target::Dependencies::const_iterator i=depends.begin(); i!=depends.end(); ++i)
105                 {
106                         if(ObjectFile *obj = dynamic_cast<ObjectFile *>(*i))
107                                 argv.push_back(relative(obj->get_path(), work_dir).str());
108                 }
109
110                 // XXX Should use dllexport, but that has some other problems to solve
111                 argv.push_back("--export-all-symbols");
112
113                 argv.push_back("-z");
114                 argv.push_back(relative(exp->get_path(), work_dir).str());
115         }
116
117         return new ExternalTask(argv, work_dir);
118 }