]> git.tdb.fi Git - builder.git/blob - source/mingwdlltool.cpp
Build and install import libraries on windows
[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 "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         string command = "dlltool";
22         if(architecture->is_cross())
23                 command = format("%s-%s", architecture->get_cross_prefix(), command);
24         executable = builder.get_vfs().find_binary(command);
25 }
26
27 Target *MingwDllTool::create_target(const list<Target *> &sources, const string &) const
28 {
29         if(sources.size()!=1)
30                 throw invalid_argument("MingwDllTool::create_target");
31         ExportDefinitions &def = dynamic_cast<ExportDefinitions &>(*sources.front());
32         ImportLibrary *imp = new ImportLibrary(builder, *def.get_component(), def);
33         imp->set_tool(*this);
34         return imp;
35 }
36
37 Target *MingwDllTool::create_install(Target &target) const
38 {
39         if(ImportLibrary *imp = dynamic_cast<ImportLibrary *>(&target))
40         {
41                 const Tool &copy = builder.get_toolchain().get_tool("CP");
42                 InstalledFile *inst_tgt = dynamic_cast<InstalledFile *>(copy.create_target(target));
43                 string link_name = format("lib%s.dll.a", imp->get_shared_library()->get_libname());
44                 inst_tgt->set_symlink(link_name);
45                 return inst_tgt;
46         }
47         else
48                 return 0;
49 }
50
51 Task *MingwDllTool::run(const Target &target) const
52 {
53         const ImportLibrary &imp = dynamic_cast<const ImportLibrary &>(target);
54         const ExportDefinitions &exp = dynamic_cast<const ExportDefinitions &>(*imp.get_dependencies().front());
55         const SharedLibrary &shlib = exp.get_library();
56
57         vector<string> argv;
58         argv.push_back(executable->get_path().str());
59
60         /* dlltool is stupid and puts temporary files in the working directory by
61         default */
62         argv.push_back("--temp-prefix");
63         char random[8];
64         for(unsigned i=0; i<8; ++i)
65                 random[i] = 'a'+(rand()%26);
66         argv.push_back(string("/tmp/")+string(random, 8));
67
68         const Component &comp = *imp.get_component();
69         FS::Path work_dir = comp.get_package().get_source_directory();
70
71         argv.push_back("-d");
72         argv.push_back(relative(exp.get_path(), work_dir).str());
73
74         argv.push_back("-D");
75         if(shlib.get_install_filename().empty())
76                 argv.push_back(FS::basename(shlib.get_path()));
77         else
78                 argv.push_back(shlib.get_install_filename());
79
80         argv.push_back("-l");
81         argv.push_back(relative(imp.get_path(), work_dir).str());
82
83         return new ExternalTask(argv, work_dir);
84 }