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