]> git.tdb.fi Git - builder.git/blob - source/msvclinker.cpp
Refactor the use of external tasks in tools
[builder.git] / source / msvclinker.cpp
1 #include <msp/core/environ.h>
2 #include <msp/fs/utils.h>
3 #include <msp/strings/utils.h>
4 #include "builder.h"
5 #include "component.h"
6 #include "executable.h"
7 #include "importlibrary.h"
8 #include "microsofttools.h"
9 #include "msvclinker.h"
10 #include "objectfile.h"
11 #include "sharedlibrary.h"
12 #include "sourcepackage.h"
13 #include "staticlibrary.h"
14
15 using namespace std;
16 using namespace Msp;
17
18 MsvcLinker::MsvcLinker(Builder &b, const Architecture &a, const MicrosoftTools &m):
19         Tool(b, &a, "LINK"),
20         ms_tools(m)
21 {
22         input_suffixes.push_back(".o");
23         input_suffixes.push_back(".a");
24
25         processing_unit = COMPONENT;
26
27         set_command((ms_tools.get_vc_bin_dir()/"link.exe").str(), false);
28         set_run_external(_run);
29 }
30
31 Target *MsvcLinker::create_target(const vector<Target *> &sources, const string &arg)
32 {
33         if(sources.empty())
34                 throw invalid_argument("MsvcLinker::create_target");
35
36         vector<ObjectFile *> objs;
37         objs.reserve(sources.size());
38         for(Target *s: sources)
39                 objs.push_back(&dynamic_cast<ObjectFile &>(*s));
40
41         const Component &comp = *objs.front()->get_component();
42         Binary *bin = 0;
43         if(arg=="shared")
44                 bin = new SharedLibrary(builder, comp, objs);
45         else
46                 bin = new Executable(builder, comp, objs);
47         bin->set_tool(*this);
48         return bin;
49 }
50
51 string MsvcLinker::create_build_signature(const BuildInfo &binfo) const
52 {
53         string result = Tool::create_build_signature(binfo);
54         result += ',';
55         if(binfo.strip)
56                 result += 's';
57         if(!binfo.libs.empty())
58         {
59                 result += ",l";
60                 result += join(binfo.libs.begin(), binfo.libs.end(), ",l");
61         }
62         return result;
63 }
64
65 void MsvcLinker::do_prepare(ToolData &tool) const
66 {
67         const std::string &tool_tag = static_cast<Tool &>(tool).get_tag();
68         const Architecture &arch = *static_cast<Tool &>(tool).get_architecture();
69         string arch_dir = (arch.get_bits()==64 ? "x64" : "x86");
70
71         const FS::Path &vc_base_dir = ms_tools.get_vc_base_dir();
72         tool.system_path.push_back(vc_base_dir/"lib"/arch_dir);
73
74         const FS::Path &win_sdk_dir = ms_tools.get_windows_sdk_dir();
75         const string &win_sdk_ver = ms_tools.get_windows_sdk_version();
76         tool.system_path.push_back(win_sdk_dir/"lib"/win_sdk_ver/"ucrt"/arch_dir);
77         tool.system_path.push_back(win_sdk_dir/"lib"/win_sdk_ver/"um"/arch_dir);
78
79         string path;
80         for(const FS::Path &p: tool.system_path)
81         {
82                 append(path, ";", p.str());
83                 builder.get_logger().log("tools", "Got %s system path: %s", tool_tag, p);
84         }
85
86         setenv("LIB", path);
87 }
88
89 ExternalTask::Arguments MsvcLinker::_run(const Binary &bin, FS::Path &work_dir)
90 {
91         const Tool &tool = *bin.get_tool();
92
93         vector<string> argv;
94         argv.push_back(tool.get_executable()->get_path().str());
95         argv.push_back("/NOLOGO");
96
97         if(dynamic_cast<const SharedLibrary *>(&bin))
98                 argv.push_back("/DLL");
99
100         BuildInfo binfo;
101         bin.collect_build_info(binfo);
102
103         /*for(const FS::Path &p: binfo.libpath)
104                 argv.push_back("/LIBPATH:"+p.str());*/
105         if(binfo.strip)
106                 argv.push_back("/INCREMENTAL:NO");
107         else
108                 argv.push_back("/DEBUG:FULL");
109
110         argv.push_back("/OUT:"+relative(bin.get_path(), work_dir).str());
111
112         for(Target *d: bin.get_dependencies())
113         {
114                 FileTarget *file = dynamic_cast<FileTarget *>(d);
115                 Target *tgt = d->get_real_target();
116
117                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
118                         argv.push_back(relative(obj->get_path(), work_dir).str());
119                 else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
120                         argv.push_back((file?file:stlib)->get_path().str());
121                 else if(ImportLibrary *imp = dynamic_cast<ImportLibrary *>(tgt))
122                         argv.push_back((file?file:imp)->get_path().str());
123         }
124
125         argv.push_back("/SUBSYSTEM:CONSOLE");
126
127         return argv;
128 }