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