]> git.tdb.fi Git - builder.git/blob - source/msvclinker.cpp
Avoid a memory leak if Builder::set_architecture is called again
[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 "externaltask.h"
8 #include "importlibrary.h"
9 #include "microsofttools.h"
10 #include "msvclinker.h"
11 #include "objectfile.h"
12 #include "sharedlibrary.h"
13 #include "sourcepackage.h"
14 #include "staticlibrary.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(".o");
24         input_suffixes.push_back(".a");
25
26         processing_unit = COMPONENT;
27
28         set_command((ms_tools.get_vc_bin_dir()/"link.exe").str(), false);
29         set_run(_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                 bin = new SharedLibrary(builder, comp, objs);
46         else
47                 bin = new Executable(builder, comp, objs);
48         bin->set_tool(*this);
49         return bin;
50 }
51
52 string MsvcLinker::create_build_signature(const BuildInfo &binfo) const
53 {
54         string result = Tool::create_build_signature(binfo);
55         result += ',';
56         if(binfo.strip)
57                 result += 's';
58         if(!binfo.libs.empty())
59         {
60                 result += ",l";
61                 result += join(binfo.libs.begin(), binfo.libs.end(), ",l");
62         }
63         return result;
64 }
65
66 void MsvcLinker::do_prepare(ToolData &tool) const
67 {
68         const std::string &tool_tag = static_cast<Tool &>(tool).get_tag();
69         const Architecture &arch = *static_cast<Tool &>(tool).get_architecture();
70         string arch_dir = (arch.get_bits()==64 ? "x64" : "x86");
71
72         const FS::Path &vc_base_dir = ms_tools.get_vc_base_dir();
73         tool.system_path.push_back(vc_base_dir/"lib"/arch_dir);
74
75         const FS::Path &win_sdk_dir = ms_tools.get_windows_sdk_dir();
76         const string &win_sdk_ver = ms_tools.get_windows_sdk_version();
77         tool.system_path.push_back(win_sdk_dir/"lib"/win_sdk_ver/"ucrt"/arch_dir);
78         tool.system_path.push_back(win_sdk_dir/"lib"/win_sdk_ver/"um"/arch_dir);
79
80         string path;
81         for(const FS::Path &p: tool.system_path)
82         {
83                 append(path, ";", p.str());
84                 builder.get_logger().log("tools", "Got %s system path: %s", tool_tag, p);
85         }
86
87         setenv("LIB", path);
88 }
89
90 Task *MsvcLinker::_run(const Binary &bin)
91 {
92         const Tool &tool = *bin.get_tool();
93
94         vector<string> argv;
95         argv.push_back(tool.get_executable()->get_path().str());
96         argv.push_back("/NOLOGO");
97
98         FS::Path work_dir = bin.get_component()->get_package().get_source_directory();
99
100         if(dynamic_cast<const SharedLibrary *>(&bin))
101                 argv.push_back("/DLL");
102
103         BuildInfo binfo;
104         bin.collect_build_info(binfo);
105
106         /*for(const FS::Path &p: binfo.libpath)
107                 argv.push_back("/LIBPATH:"+p.str());*/
108         if(binfo.strip)
109                 argv.push_back("/INCREMENTAL:NO");
110         else
111                 argv.push_back("/DEBUG:FULL");
112
113         argv.push_back("/OUT:"+relative(bin.get_path(), work_dir).str());
114
115         for(Target *d: bin.get_dependencies())
116         {
117                 FileTarget *file = dynamic_cast<FileTarget *>(d);
118                 Target *tgt = d->get_real_target();
119
120                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
121                         argv.push_back(relative(obj->get_path(), work_dir).str());
122                 else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
123                         argv.push_back((file?file:stlib)->get_path().str());
124                 else if(ImportLibrary *imp = dynamic_cast<ImportLibrary *>(tgt))
125                         argv.push_back((file?file:imp)->get_path().str());
126         }
127
128         argv.push_back("/SUBSYSTEM:CONSOLE");
129
130         return new ExternalTask(argv, work_dir);
131 }