1 #include <msp/builder/builder.h>
2 #include <msp/builder/component.h>
3 #include <msp/builder/executable.h>
4 #include <msp/builder/importlibrary.h>
5 #include <msp/builder/installedfile.h>
6 #include <msp/builder/objectfile.h>
7 #include <msp/builder/sharedlibrary.h>
8 #include <msp/builder/sourcepackage.h>
9 #include <msp/builder/staticlibrary.h>
10 #include <msp/core/environ.h>
11 #include <msp/fs/utils.h>
12 #include <msp/strings/utils.h>
13 #include "microsofttools.h"
14 #include "msvclinker.h"
19 MsvcLinker::MsvcLinker(Builder &b, const Architecture &a, const MicrosoftTools &m):
23 input_suffixes.push_back(".obj");
24 input_suffixes.push_back(".lib");
26 processing_unit = COMPONENT;
28 set_command((ms_tools.get_vc_bin_dir()/"link.exe").str(), false);
29 set_run_external(_run);
32 Target *MsvcLinker::create_target(const vector<Target *> &sources, const string &arg)
35 throw invalid_argument("MsvcLinker::create_target");
37 vector<ObjectFile *> objs;
38 objs.reserve(sources.size());
39 for(Target *s: sources)
40 objs.push_back(&dynamic_cast<ObjectFile &>(*s));
42 const Component &comp = *objs.front()->get_component();
46 SharedLibrary *shlib = new SharedLibrary(builder, comp, objs);
48 ImportLibrary *imp = new ImportLibrary(builder, comp, *shlib);
49 for(ObjectFile *o: objs)
50 imp->add_dependency(*o);
52 shlib->add_side_effect(*imp);
55 bin = new Executable(builder, comp, objs);
60 Target *MsvcLinker::create_install(Target &target) const
62 if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(&target))
64 Tool © = builder.get_toolchain().get_tool("CP");
65 InstalledFile *inst_tgt = dynamic_cast<InstalledFile *>(copy.create_target(target));
66 builder.get_build_graph().add_installed_target(*shlib->get_import_library());
73 string MsvcLinker::create_build_signature(const BuildInfo &binfo) const
75 string result = Tool::create_build_signature(binfo);
79 if(!binfo.libs.empty())
82 result += join(binfo.libs.begin(), binfo.libs.end(), ",l");
87 void MsvcLinker::do_prepare(ToolData &tool) const
89 const std::string &tool_tag = static_cast<Tool &>(tool).get_tag();
90 const Architecture &arch = *static_cast<Tool &>(tool).get_architecture();
91 string arch_dir = (arch.get_bits()==64 ? "x64" : "x86");
93 const FS::Path &vc_base_dir = ms_tools.get_vc_base_dir();
94 tool.system_path.push_back(vc_base_dir/"lib"/arch_dir);
96 const FS::Path &win_sdk_dir = ms_tools.get_windows_sdk_dir();
97 const string &win_sdk_ver = ms_tools.get_windows_sdk_version();
98 tool.system_path.push_back(win_sdk_dir/"lib"/win_sdk_ver/"ucrt"/arch_dir);
99 tool.system_path.push_back(win_sdk_dir/"lib"/win_sdk_ver/"um"/arch_dir);
102 for(const FS::Path &p: tool.system_path)
104 append(path, ";", p.str());
105 builder.get_logger().log("tools", "Got %s system path: %s", tool_tag, p);
111 ExternalTask::Arguments MsvcLinker::_run(const Binary &bin, FS::Path &work_dir)
113 const Tool &tool = *bin.get_tool();
116 argv.push_back(tool.get_executable()->get_path().str());
117 argv.push_back("/NOLOGO");
120 if(const SharedLibrary *shlib = dynamic_cast<const SharedLibrary *>(&bin))
122 argv.push_back("/DLL");
123 if(const ImportLibrary *imp = shlib->get_import_library())
124 imp_fn = relative(imp->get_path(), work_dir).str();
128 bin.collect_build_info(binfo);
130 /*for(const FS::Path &p: binfo.libpath)
131 argv.push_back("/LIBPATH:"+p.str());*/
133 argv.push_back("/INCREMENTAL:NO");
135 argv.push_back("/DEBUG:FULL");
137 argv.push_back("/OUT:"+relative(bin.get_path(), work_dir).str());
139 argv.push_back("/IMPLIB:"+imp_fn);
141 argv.push_back("/NOIMPLIB");
143 for(Target *d: bin.get_dependencies())
145 FileTarget *file = dynamic_cast<FileTarget *>(d);
146 Target *tgt = d->get_real_target();
148 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
149 argv.push_back(relative(obj->get_path(), work_dir).str());
150 else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
151 argv.push_back((file?file:stlib)->get_path().str());
152 else if(ImportLibrary *imp = dynamic_cast<ImportLibrary *>(tgt))
153 argv.push_back((file?file:imp)->get_path().str());
156 argv.push_back("/SUBSYSTEM:CONSOLE");