]> git.tdb.fi Git - builder.git/blob - source/mingwdlltool.cpp
Don't add a symlink with the same name as the file
[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                 if(link_name!=FS::basename(inst_tgt->get_path()))
45                         inst_tgt->set_symlink(link_name);
46                 return inst_tgt;
47         }
48         else
49                 return 0;
50 }
51
52 Task *MingwDllTool::run(const Target &target) const
53 {
54         const ImportLibrary &imp = dynamic_cast<const ImportLibrary &>(target);
55         const ExportDefinitions &exp = dynamic_cast<const ExportDefinitions &>(*imp.get_dependencies().front());
56         const SharedLibrary &shlib = exp.get_library();
57
58         vector<string> argv;
59         argv.push_back(executable->get_path().str());
60
61         /* dlltool is stupid and puts temporary files in the working directory by
62         default */
63         argv.push_back("--temp-prefix");
64         char random[8];
65         for(unsigned i=0; i<8; ++i)
66                 random[i] = 'a'+(rand()%26);
67         argv.push_back(string("/tmp/")+string(random, 8));
68
69         const Component &comp = *imp.get_component();
70         FS::Path work_dir = comp.get_package().get_source_directory();
71
72         argv.push_back("-d");
73         argv.push_back(relative(exp.get_path(), work_dir).str());
74
75         argv.push_back("-D");
76         if(shlib.get_install_filename().empty())
77                 argv.push_back(FS::basename(shlib.get_path()));
78         else
79                 argv.push_back(shlib.get_install_filename());
80
81         argv.push_back("-l");
82         argv.push_back(relative(imp.get_path(), work_dir).str());
83
84         return new ExternalTask(argv, work_dir);
85 }