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