]> git.tdb.fi Git - builder.git/blob - msvclinker.cpp
5ce9d32112688e36cd65768e8199ae6ade9ea315
[builder.git] / msvclinker.cpp
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"
15
16 using namespace std;
17 using namespace Msp;
18
19 MsvcLinker::MsvcLinker(Builder &b, const Architecture &a, const MicrosoftTools &m):
20         Tool(b, &a, "LINK"),
21         ms_tools(m)
22 {
23         input_suffixes.push_back(".obj");
24         input_suffixes.push_back(".lib");
25
26         processing_unit = COMPONENT;
27
28         set_command((ms_tools.get_vc_bin_dir()/"link.exe").str(), false);
29         set_run_external(_run);
30 }
31
32 Target *MsvcLinker::create_target(const vector<Target *> &sources, const string &arg)
33 {
34         if(sources.empty())
35                 throw invalid_argument("MsvcLinker::create_target");
36
37         vector<ObjectFile *> objs;
38         objs.reserve(sources.size());
39         for(Target *s: sources)
40                 objs.push_back(&dynamic_cast<ObjectFile &>(*s));
41
42         const Component &comp = *objs.front()->get_component();
43         Binary *bin = 0;
44         if(arg=="shared")
45         {
46                 SharedLibrary *shlib = new SharedLibrary(builder, comp, objs);
47                 bin = shlib;
48                 ImportLibrary *imp = new ImportLibrary(builder, comp, *shlib);
49                 for(ObjectFile *o: objs)
50                         imp->add_dependency(*o);
51                 imp->set_tool(*this);
52                 shlib->add_side_effect(*imp);
53         }
54         else
55                 bin = new Executable(builder, comp, objs);
56         bin->set_tool(*this);
57         return bin;
58 }
59
60 Target *MsvcLinker::create_install(Target &target) const
61 {
62         if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(&target))
63         {
64                 Tool &copy = 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());
67                 return inst_tgt;
68         }
69         else
70                 return 0;
71 }
72
73 string MsvcLinker::create_build_signature(const BuildInfo &binfo) const
74 {
75         string result = Tool::create_build_signature(binfo);
76         result += ',';
77         if(binfo.strip)
78                 result += 's';
79         if(!binfo.libs.empty())
80         {
81                 result += ",l";
82                 result += join(binfo.libs.begin(), binfo.libs.end(), ",l");
83         }
84         return result;
85 }
86
87 void MsvcLinker::do_prepare(ToolData &tool) const
88 {
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");
92
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);
95
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);
100
101         string path;
102         for(const FS::Path &p: tool.system_path)
103         {
104                 append(path, ";", p.str());
105                 builder.get_logger().log("tools", "Got %s system path: %s", tool_tag, p);
106         }
107
108         setenv("LIB", path);
109 }
110
111 ExternalTask::Arguments MsvcLinker::_run(const Binary &bin, FS::Path &work_dir)
112 {
113         const Tool &tool = *bin.get_tool();
114
115         vector<string> argv;
116         argv.push_back(tool.get_executable()->get_path().str());
117         argv.push_back("/NOLOGO");
118
119         string imp_fn;
120         if(const SharedLibrary *shlib = dynamic_cast<const SharedLibrary *>(&bin))
121         {
122                 argv.push_back("/DLL");
123                 if(const ImportLibrary *imp = shlib->get_import_library())
124                         imp_fn = relative(imp->get_path(), work_dir).str();
125         }
126
127         BuildInfo binfo;
128         bin.collect_build_info(binfo);
129
130         /*for(const FS::Path &p: binfo.libpath)
131                 argv.push_back("/LIBPATH:"+p.str());*/
132         if(binfo.strip)
133                 argv.push_back("/INCREMENTAL:NO");
134         else
135                 argv.push_back("/DEBUG:FULL");
136
137         argv.push_back("/OUT:"+relative(bin.get_path(), work_dir).str());
138         if(!imp_fn.empty())
139                 argv.push_back("/IMPLIB:"+imp_fn);
140         else
141                 argv.push_back("/NOIMPLIB");
142
143         for(Target *d: bin.get_dependencies())
144         {
145                 FileTarget *file = dynamic_cast<FileTarget *>(d);
146                 Target *tgt = d->get_real_target();
147
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());
154         }
155
156         argv.push_back("/SUBSYSTEM:CONSOLE");
157
158         return argv;
159 }