]> git.tdb.fi Git - builder.git/blob - plugins/gnu/mingwdlltool.cpp
Remove automatic export/import of symbols from GNU tools on Windows
[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 Task *MingwDllTool::_run(const Target &target)
48 {
49         const Tool &tool = *target.get_tool();
50
51         const ImportLibrary *imp = dynamic_cast<const ImportLibrary *>(&target);
52         const ExportDefinitions *exp = 0;
53         if(imp)
54                 exp = &dynamic_cast<const ExportDefinitions &>(*imp->get_dependencies().front());
55         else
56                 exp = dynamic_cast<const ExportDefinitions *>(&target);
57         if(!imp && !exp)
58                 throw invalid_argument("MingwDllTool::run");
59
60         vector<string> argv;
61         argv.push_back(tool.get_executable()->get_path().str());
62
63         /* dlltool is stupid and puts temporary files in the working directory by
64         default */
65         argv.push_back("--temp-prefix");
66         char random[8];
67         for(unsigned i=0; i<8; ++i)
68                 random[i] = 'a'+(rand()%26);
69         argv.push_back(string("/tmp/")+string(random, 8));
70
71         const Component &comp = *target.get_component();
72         FS::Path work_dir = comp.get_package().get_source_directory();
73
74         if(imp)
75         {
76                 const SharedLibrary &shlib = *imp->get_shared_library();
77
78                 argv.push_back("-d");
79                 argv.push_back(relative(exp->get_path(), work_dir).str());
80
81                 argv.push_back("-D");
82                 argv.push_back(FS::basename(shlib.get_path()));
83
84                 argv.push_back("-l");
85                 argv.push_back(relative(imp->get_path(), work_dir).str());
86         }
87         else
88         {
89                 for(Target *d: exp->get_dependencies())
90                         if(ObjectFile *obj = dynamic_cast<ObjectFile *>(d))
91                                 argv.push_back(relative(obj->get_path(), work_dir).str());
92
93                 argv.push_back("-z");
94                 argv.push_back(relative(exp->get_path(), work_dir).str());
95         }
96
97         return new ExternalTask(argv, work_dir);
98 }