1 #include <msp/core/environ.h>
2 #include <msp/fs/utils.h>
3 #include <msp/strings/format.h>
4 #include <msp/strings/utils.h>
7 #include "executable.h"
8 #include "externaltask.h"
9 #include "importlibrary.h"
10 #include "microsofttools.h"
11 #include "msvclinker.h"
12 #include "objectfile.h"
13 #include "sharedlibrary.h"
14 #include "sourcepackage.h"
15 #include "staticlibrary.h"
20 MsvcLinker::MsvcLinker(Builder &b, const Architecture &a, const MicrosoftTools &m):
24 input_suffixes.push_back(".o");
25 input_suffixes.push_back(".a");
27 processing_unit = COMPONENT;
29 set_command((ms_tools.get_vc_bin_dir()/"link.exe").str(), false);
32 Target *MsvcLinker::create_target(const list<Target *> &sources, const string &arg)
35 throw invalid_argument("MsvcLinker::create_target");
37 list<ObjectFile *> objs;
38 for(list<Target *>::const_iterator i=sources.begin(); i!=sources.end(); ++i)
40 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(*i))
43 throw invalid_argument("MsvcLinker::create_target");
46 const Component &comp = *objs.front()->get_component();
49 bin = new SharedLibrary(builder, comp, objs);
51 bin = new Executable(builder, comp, objs);
56 string MsvcLinker::create_build_signature(const BuildInfo &binfo) const
58 string result = FS::basename(executable->get_path());
62 if(!binfo.libs.empty())
65 result += join(binfo.libs.begin(), binfo.libs.end(), ",l");
70 void MsvcLinker::do_prepare()
72 string arch_dir = (architecture->get_bits()==64 ? "x64" : "x86");
74 const FS::Path &vc_base_dir = ms_tools.get_vc_base_dir();
75 system_path.push_back(vc_base_dir/"lib"/arch_dir);
77 const FS::Path &win_sdk_dir = ms_tools.get_windows_sdk_dir();
78 const string &win_sdk_ver = ms_tools.get_windows_sdk_version();
79 system_path.push_back(win_sdk_dir/"lib"/win_sdk_ver/"ucrt"/arch_dir);
80 system_path.push_back(win_sdk_dir/"lib"/win_sdk_ver/"um"/arch_dir);
83 for(SearchPath::const_iterator i=system_path.begin(); i!=system_path.end(); ++i)
85 append(path, ";", i->str());
86 builder.get_logger().log("tools", format("Got %s system path: %s", tag, *i));
92 Task *MsvcLinker::run(const Target &target) const
94 const Binary &bin = dynamic_cast<const Binary &>(target);
97 argv.push_back(executable->get_path().str());
98 argv.push_back("/NOLOGO");
100 FS::Path work_dir = bin.get_component()->get_package().get_source_directory();
102 if(dynamic_cast<const SharedLibrary *>(&bin))
103 argv.push_back("/DLL");
106 target.collect_build_info(binfo);
108 /*for(BuildInfo::PathList::const_iterator i=binfo.libpath.begin(); i!=binfo.libpath.end(); ++i)
109 argv.push_back("/LIBPATH:"+i->str());*/
111 argv.push_back("/INCREMENTAL:NO");
113 argv.push_back("/DEBUG:FULL");
115 argv.push_back("/OUT:"+relative(bin.get_path(), work_dir).str());
117 const Target::Dependencies &depends = target.get_dependencies();
118 for(Target::Dependencies::const_iterator i=depends.begin(); i!=depends.end(); ++i)
120 FileTarget *file = dynamic_cast<FileTarget *>(*i);
121 Target *tgt = (*i)->get_real_target();
123 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
124 argv.push_back(relative(obj->get_path(), work_dir).str());
125 else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
126 argv.push_back((file?file:stlib)->get_path().str());
127 else if(ImportLibrary *imp = dynamic_cast<ImportLibrary *>(tgt))
128 argv.push_back((file?file:imp)->get_path().str());
131 argv.push_back("/SUBSYSTEM:CONSOLE");
133 return new ExternalTask(argv, work_dir);