aboutsummaryrefslogtreecommitdiff
path: root/build2/cxx/msvc.cxx
blob: 97d8fd529ad70c8d05e8e8d35a95e5c8ab1d44fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// file      : build2/cxx/msvc.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <build2/scope>
#include <build2/target>
#include <build2/context>
#include <build2/variable>
#include <build2/filesystem>
#include <build2/diagnostics>

#include <build2/cxx/common>

using namespace std;
using namespace butl;

namespace build2
{
  namespace cxx
  {
    using namespace bin;

    // Translate the target triplet CPU to lib.exe/link.exe /MACHINE option.
    //
    const char*
    msvc_machine (const string& cpu)
    {
      const char* m (cpu == "i386" || cpu == "i686"  ? "/MACHINE:x86"   :
                     cpu == "x86_64"                 ? "/MACHINE:x64"   :
                     cpu == "arm"                    ? "/MACHINE:ARM"   :
                     cpu == "arm64"                  ? "/MACHINE:ARM64" :
                     nullptr);

      if (m == nullptr)
        fail << "unable to translate CPU " << cpu << " to /MACHINE";

      return m;
    }

    // Extract system library search paths from MSVC.
    //
    void
    msvc_library_search_paths (scope&, const string&, dir_paths&)
    {
      // The linker doesn't seem to have any built-in paths and all of them
      // come from the LIB environment variable.

      // @@ VC: how are we going to do this? E.g., cl-14 does this internally.
      //    cl.exe /Be prints LIB.
      //
      //    Should we actually bother? LIB is normally used for system
      //    libraries and its highly unlikely we will see an explicit import
      //    for a library from one of those directories.
      //
    }

    // Inspect the file and determine if it is static or import library.
    // Return otype::e if it is neither (which we quietly ignore).
    //
    static otype
    library_type (const path& ld, const path& l)
    {
      // The are several reasonably reliable methods to tell whether it is a
      // static or import library. One is lib.exe /LIST -- if there aren't any
      // .obj members, then it is most likely an import library (it can also
      // be an empty static library in which case there won't be any members).
      // For an import library /LIST will print a bunch of .dll members.
      //
      // Another approach is dumpbin.exe (link.exe /DUMP) with /ARCHIVEMEMBERS
      // (similar to /LIST) and /LINKERMEMBER (looking for __impl__ symbols or
      // _IMPORT_DESCRIPTOR_).
      //
      // Note also, that apparently it is possible to have a hybrid library.
      //
      // While the lib.exe approach is probably the simplest, the problem is
      // it will require us loading the bin.ar module even if we are not
      // building any static libraries. On the other hand, if we are searching
      // for libraries then we have bin.ld. So we will use the link.exe /DUMP
      // /ARCHIVEMEMBERS.
      //
      const char* args[] = {ld.string ().c_str (),
                            "/DUMP",               // Must come first.
                            "/NOLOGO",
                            "/ARCHIVEMEMBERS",
                            l.string ().c_str (),
                            nullptr};

      // Link.exe seem to always dump everything to stdout but just in case
      // redirect stderr to stdout.
      //
      process pr (start_run (args, false));
      ifdstream is (pr.in_ofd);

      bool obj (false), dll (false);

      string s;
      while (getline (is, s))
      {
        // Detect the one error we should let through.
        //
        if (s.compare (0, 18, "unable to execute ") == 0)
          break;

        // The lines we are interested in seem to have this form (though
        // presumably the "Archive member name at" part can be translated):
        //
        // Archive member name at 746: [...]hello.dll[/][ ]*
        // Archive member name at 8C70: [...]hello.lib.obj[/][ ]*
        //
        size_t n (s.size ());

        for (; n != 0 && s[n - 1] == ' '; --n) ; // Skip trailing spaces.

        if (n >= 7) // At least ": X.obj" or ": X.dll".
        {
          --n;

          if (s[n] == '/') // Skip trailing slash if one is there.
            --n;

          n -= 3; // Beginning of extension.

          if (s[n] == '.')
          {
            // Make sure there is ": ".
            //
            size_t p (s.rfind (':', n - 1));

            if (p != string::npos && s[p + 1] == ' ')
            {
              if (s.compare (n + 1, 3, "obj") == 0) // @@ CASE
                obj = true;

              if (s.compare (n + 1, 3, "dll") == 0) // @@ CASE
                dll = true;
            }
          }
        }
      }

      is.close (); // Don't block.

      if (!finish_run (args, false, pr, s))
        return otype::e;

      if (obj && dll)
      {
        warn << l << " looks like hybrid static/import library, ignoring";
        return otype::e;
      }

      if (!obj && !dll)
      {
        warn << l << " looks like empty static or import library, ignoring";
        return otype::e;
      }

      return obj ? otype::a : otype::s;
    }

    template <typename T>
    static T*
    search_library (const path& ld,
                    const dir_path& d,
                    prerequisite& p,
                    otype lt,
                    const char* pfx,
                    const char* sfx)
    {
      // Pretty similar logic to link::search_library().
      //
      tracer trace ("cxx::msvc_search_library");

      // Assemble the file path.
      //
      path f (d);

      if (*pfx != '\0')
      {
        f /= pfx;
        f += p.name;
      }
      else
        f /= p.name;

      if (*sfx != '\0')
        f += sfx;

      const string& e (
        p.ext == nullptr || p.is_a<lib> () // Only for liba/libs.
        ? extension_pool.find ("lib")
        : *p.ext);

      if (!e.empty ())
      {
        f += '.';
        f += e;
      }

      // Check if the file exists and is of the expected type.
      //
      timestamp mt (file_mtime (f));

      if (mt != timestamp_nonexistent && library_type (ld, f) == lt)
      {
        // Enter the target.
        //
        T& t (targets.insert<T> (d, dir_path (), p.name, &e, trace));

        if (t.path ().empty ())
          t.path (move (f));

        t.mtime (mt);
        return &t;
      }

      return nullptr;
    }

    liba*
    msvc_search_static (const path& ld, const dir_path& d, prerequisite& p)
    {
      liba* r (nullptr);

      auto search = [&r, &ld, &d, &p] (const char* pf, const char* sf) -> bool
      {
        r = search_library<liba> (ld, d, p, otype::a, pf, sf);
        return r != nullptr;
      };

      // Try:
      //      foo.lib
      //   libfoo.lib
      //      foolib.lib
      //      foo_static.lib
      //
      return
        search ("",    "")    ||
        search ("lib", "")    ||
        search ("",    "lib") ||
        search ("",    "_static") ? r : nullptr;
    }

    libs*
    msvc_search_shared (const path& ld, const dir_path& d, prerequisite& p)
    {
      tracer trace ("cxx::msvc_search_shared");

      libs* r (nullptr);

      auto search = [&r, &ld, &d, &p, &trace] (
        const char* pf, const char* sf) -> bool
      {
        if (libi* i = search_library<libi> (ld, d, p, otype::s, pf, sf))
        {
          r = &targets.insert<libs> (d, dir_path (), p.name, nullptr, trace);

          if (r->member == nullptr)
          {
            r->mtime (i->mtime ());
            r->member = i;
          }
        }

        return r != nullptr;
      };

      // Try:
      //      foo.lib
      //   libfoo.lib
      //      foodll.lib
      //
      return
        search ("",    "")    ||
        search ("lib", "")    ||
        search ("",    "dll") ? r : nullptr;
    }
  }
}