2 #include <msp/fs/utils.h>
3 #include <msp/strings/format.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"
18 MingwDllTool::MingwDllTool(Builder &b, const Architecture &a):
21 set_command("dlltool", true);
24 Target *MingwDllTool::create_target(const list<Target *> &sources, const string &)
27 throw invalid_argument("MingwDllTool::create_target");
28 SharedLibrary &shlib = dynamic_cast<SharedLibrary &>(*sources.front());
30 list<ObjectFile *> objs;
31 for(Target *d: shlib.get_dependencies())
32 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(d))
35 ExportDefinitions *exp = new ExportDefinitions(builder, *shlib.get_component(), objs);
38 ImportLibrary *imp = new ImportLibrary(builder, *shlib.get_component(), shlib, *exp);
44 Target *MingwDllTool::create_install(Target &target) const
46 if(ImportLibrary *imp = dynamic_cast<ImportLibrary *>(&target))
48 Tool © = 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);
59 Task *MingwDllTool::run(const Target &target) const
61 const ImportLibrary *imp = dynamic_cast<const ImportLibrary *>(&target);
62 const ExportDefinitions *exp = 0;
64 exp = &dynamic_cast<const ExportDefinitions &>(*imp->get_dependencies().front());
66 exp = dynamic_cast<const ExportDefinitions *>(&target);
68 throw invalid_argument("MingwDllTool::run");
71 argv.push_back(executable->get_path().str());
73 /* dlltool is stupid and puts temporary files in the working directory by
75 argv.push_back("--temp-prefix");
77 for(unsigned i=0; i<8; ++i)
78 random[i] = 'a'+(rand()%26);
79 argv.push_back(string("/tmp/")+string(random, 8));
81 const Component &comp = *target.get_component();
82 FS::Path work_dir = comp.get_package().get_source_directory();
86 const SharedLibrary &shlib = *imp->get_shared_library();
89 argv.push_back(relative(exp->get_path(), work_dir).str());
92 if(shlib.get_install_filename().empty())
93 argv.push_back(FS::basename(shlib.get_path()));
95 argv.push_back(shlib.get_install_filename());
98 argv.push_back(relative(imp->get_path(), work_dir).str());
102 for(Target *d: exp->get_dependencies())
103 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(d))
104 argv.push_back(relative(obj->get_path(), work_dir).str());
106 // XXX Should use dllexport, but that has some other problems to solve
107 argv.push_back("--export-all-symbols");
109 argv.push_back("-z");
110 argv.push_back(relative(exp->get_path(), work_dir).str());
113 return new ExternalTask(argv, work_dir);