]> git.tdb.fi Git - builder.git/blob - plugins/gnu/mingwdlltool.cpp
Remove ExportDefinitions from ImportLibrary's constructor
[builder.git] / plugins / gnu / mingwdlltool.cpp
1 #include <cstdlib>
2 #include <msp/builder/builder.h>
3 #include <msp/builder/component.h>
4 #include <msp/builder/exportdefinitions.h>
5 #include <msp/builder/externaltask.h>
6 #include <msp/builder/importlibrary.h>
7 #include <msp/builder/installedfile.h>
8 #include <msp/builder/objectfile.h>
9 #include <msp/builder/sharedlibrary.h>
10 #include <msp/builder/sourcepackage.h>
11 #include <msp/fs/utils.h>
12 #include <msp/strings/format.h>
13 #include "mingwdlltool.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         set_run(_run);
23 }
24
25 Target *MingwDllTool::create_target(const vector<Target *> &sources, const string &)
26 {
27         if(sources.size()!=1)
28                 throw invalid_argument("MingwDllTool::create_target");
29         SharedLibrary &shlib = dynamic_cast<SharedLibrary &>(*sources.front());
30
31         vector<ObjectFile *> objs;
32         objs.reserve(shlib.get_dependencies().size());
33         for(Target *d: shlib.get_dependencies())
34                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(d))
35                         objs.push_back(obj);
36
37         ExportDefinitions *exp = new ExportDefinitions(builder, *shlib.get_component(), objs);
38         exp->set_tool(*this);
39
40         ImportLibrary *imp = new ImportLibrary(builder, *shlib.get_component(), shlib);
41         imp->add_dependency(*exp);
42         imp->set_tool(*this);
43
44         return imp;
45 }
46
47 Target *MingwDllTool::create_install(Target &target) const
48 {
49         if(ImportLibrary *imp = dynamic_cast<ImportLibrary *>(&target))
50         {
51                 Tool &copy = builder.get_toolchain().get_tool("CP");
52                 InstalledFile *inst_tgt = dynamic_cast<InstalledFile *>(copy.create_target(target));
53                 string link_name = format("lib%s.dll.a", imp->get_shared_library()->get_libname());
54                 if(link_name!=FS::basename(inst_tgt->get_path()))
55                         inst_tgt->set_symlink(link_name);
56                 return inst_tgt;
57         }
58         else
59                 return 0;
60 }
61
62 Task *MingwDllTool::_run(const Target &target)
63 {
64         const Tool &tool = *target.get_tool();
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(tool.get_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                 for(Target *d: exp->get_dependencies())
108                         if(ObjectFile *obj = dynamic_cast<ObjectFile *>(d))
109                                 argv.push_back(relative(obj->get_path(), work_dir).str());
110
111                 // XXX Should use dllexport, but that has some other problems to solve
112                 argv.push_back("--export-all-symbols");
113
114                 argv.push_back("-z");
115                 argv.push_back(relative(exp->get_path(), work_dir).str());
116         }
117
118         return new ExternalTask(argv, work_dir);
119 }