]> git.tdb.fi Git - builder.git/blob - source/msvclinker.cpp
Save caches before starting the build
[builder.git] / source / msvclinker.cpp
1 #include <msp/core/environ.h>
2 #include <msp/fs/utils.h>
3 #include <msp/strings/format.h>
4 #include <msp/strings/utils.h>
5 #include "builder.h"
6 #include "component.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"
16
17 using namespace std;
18 using namespace Msp;
19
20 MsvcLinker::MsvcLinker(Builder &b, const Architecture &a, const MicrosoftTools &m):
21         Tool(b, a, "LINK"),
22         ms_tools(m)
23 {
24         input_suffixes.push_back(".o");
25         input_suffixes.push_back(".a");
26
27         processing_unit = COMPONENT;
28
29         set_command((ms_tools.get_vc_bin_dir()/"link.exe").str(), false);
30 }
31
32 Target *MsvcLinker::create_target(const list<Target *> &sources, const string &arg)
33 {
34         if(sources.empty())
35                 throw invalid_argument("MsvcLinker::create_target");
36
37         list<ObjectFile *> objs;
38         for(list<Target *>::const_iterator i=sources.begin(); i!=sources.end(); ++i)
39         {
40                 if(ObjectFile *obj = dynamic_cast<ObjectFile *>(*i))
41                         objs.push_back(obj);
42                 else
43                         throw invalid_argument("MsvcLinker::create_target");
44         }
45
46         const Component &comp = *objs.front()->get_component();
47         Binary *bin = 0;
48         if(arg=="shared")
49                 bin = new SharedLibrary(builder, comp, objs);
50         else
51                 bin = new Executable(builder, comp, objs);
52         bin->set_tool(*this);
53         return bin;
54 }
55
56 string MsvcLinker::create_build_signature(const BuildInfo &binfo) const
57 {
58         string result = FS::basename(executable->get_path());
59         result += ',';
60         if(binfo.strip)
61                 result += 's';
62         if(!binfo.libs.empty())
63         {
64                 result += ",l";
65                 result += join(binfo.libs.begin(), binfo.libs.end(), ",l");
66         }
67         return result;
68 }
69
70 void MsvcLinker::do_prepare()
71 {
72         string arch_dir = (architecture->get_bits()==64 ? "x64" : "x86");
73
74         const FS::Path &vc_base_dir = ms_tools.get_vc_base_dir();
75         system_path.push_back(vc_base_dir/"lib"/arch_dir);
76
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);
81
82         string path;
83         for(SearchPath::const_iterator i=system_path.begin(); i!=system_path.end(); ++i)
84         {
85                 append(path, ";", i->str());
86                 builder.get_logger().log("tools", format("Got %s system path: %s", tag, *i));
87         }
88
89         setenv("LIB", path);
90 }
91
92 Task *MsvcLinker::run(const Target &target) const
93 {
94         const Binary &bin = dynamic_cast<const Binary &>(target);
95
96         vector<string> argv;
97         argv.push_back(executable->get_path().str());
98         argv.push_back("/NOLOGO");
99
100         FS::Path work_dir = bin.get_component()->get_package().get_source_directory();
101
102         if(dynamic_cast<const SharedLibrary *>(&bin))
103                 argv.push_back("/DLL");
104
105         BuildInfo binfo;
106         target.collect_build_info(binfo);
107
108         /*for(BuildInfo::PathList::const_iterator i=binfo.libpath.begin(); i!=binfo.libpath.end(); ++i)
109                 argv.push_back("/LIBPATH:"+i->str());*/
110         if(binfo.strip)
111                 argv.push_back("/INCREMENTAL:NO");
112         else
113                 argv.push_back("/DEBUG:FULL");
114
115         argv.push_back("/OUT:"+relative(bin.get_path(), work_dir).str());
116
117         const Target::Dependencies &depends = target.get_dependencies();
118         for(Target::Dependencies::const_iterator i=depends.begin(); i!=depends.end(); ++i)
119         {
120                 FileTarget *file = dynamic_cast<FileTarget *>(*i);
121                 Target *tgt = (*i)->get_real_target();
122
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());
129         }
130
131         argv.push_back("/SUBSYSTEM:CONSOLE");
132
133         return new ExternalTask(argv, work_dir);
134 }