aboutsummaryrefslogtreecommitdiff
path: root/libbutl/target-triplet.cxx
blob: 17337b3887e3ec8cd3d1c3ebedc837c765128a76 (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
// file      : libbutl/target-triplet.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#ifndef __cpp_modules_ts
#include <libbutl/target-triplet.mxx>
#endif

// C includes.

#ifndef __cpp_lib_modules_ts
#include <string>
#include <ostream>

#include <stdexcept> // invalid_argument
#endif

// Other includes.

#ifdef __cpp_modules_ts
module butl.target_triplet;

// Only imports additional to interface.
#ifdef __clang__
#ifdef __cpp_lib_modules_ts
import std.core;
import std.io;
#endif
#endif

#endif

using namespace std;

namespace butl
{
  target_triplet::
  target_triplet (const std::string& s)
  {
    using std::string;

    auto bad = [](const char* m) {throw invalid_argument (m);};

    // Find the first and the last components. The first is CPU and the last is
    // (part of) SYSTEM, that we know for sure.
    //
    string::size_type f (s.find ('-')), l (s.rfind ('-'));

    if (f == 0 || f == string::npos)
      bad ("missing cpu");

    cpu.assign (s, 0, f);

    // If we have something in between, then the first component after CPU is
    // VENDOR. Unless it is a first component of two-component system, as in
    // i686-linux-gnu.
    //
    // There are also cases like x86_64--netbsd.
    //
    if (l - f > 1)
    {
      // [f, p) is VENDOR.
      //
      string::size_type p (s.find ('-', ++f)), n (p - f);

      // Do we have all four components? If so, then we don't need to do any
      // special recognition of two-component systems.
      //
      if (l != p)
      {
        l = s.rfind ('-', --l);

        if (l != p)
          bad ("too many components");
      }
      else
      {
        // See if this is one of the well-known non-vendors.
        //
        if (s.compare (f, n, "linux") == 0    ||
            s.compare (f, n, "windows") == 0  ||
            s.compare (f, n, "kfreebsd") == 0 ||
            s.compare (f, n, "nto") == 0)
        {
          l = f - 1;
          n = 0; // No VENDOR.
        }
      }

      // Handle special VENDOR values.
      //
      if (n != 0)
      {
        if (s.compare (f, n, "pc") != 0 &&
            s.compare (f, n, "none") != 0 &&
            s.compare (f, n, "unknown") != 0)
          vendor.assign (s, f, n);
      }
    }

    // (l, npos) is SYSTEM
    //
    system.assign (s, ++l, string::npos);

    if (system.empty ())
      bad ("missing os/kernel/abi");

    if (system.front () == '-' || system.back () == '-')
      bad ("invalid os/kernel/abi");

    // Extract VERSION for some recognized systems.
    //
    string::size_type v (0);
    if (system.compare (0, (v = 6),  "darwin") == 0       ||
        system.compare (0, (v = 7),  "freebsd") == 0      ||
        system.compare (0, (v = 7),  "openbsd") == 0      ||
        system.compare (0, (v = 6),  "netbsd") == 0       ||
        system.compare (0, (v = 7),  "solaris") == 0      ||
        system.compare (0, (v = 3),  "aix") == 0          ||
        system.compare (0, (v = 4),  "hpux") == 0         ||
        system.compare (0, (v = 10), "win32-msvc") == 0   ||
        system.compare (0, (v = 12), "windows-msvc") == 0 ||
        system.compare (0, (v = 7),  "nto-qnx") == 0)
    {
      version.assign (system, v, string::npos);
      system.resize (system.size () - version.size ());
    }

    // Determine class for some recognized systems.
    //
    if (system.compare (0, 5, "linux") == 0)
      class_ = "linux";
    else if (vendor == "apple" && system == "darwin")
      class_ = "macos";
    else if (system == "freebsd" ||
             system == "openbsd" ||
             system == "netbsd")
      class_ = "bsd";
    else if (system.compare (0, 5, "win32") == 0   ||
             system.compare (0, 7, "windows") == 0 ||
             system == "mingw32")
      class_ = "windows";
    else
      class_ = "other";
  }

  std::string target_triplet::
  string () const
  {
    std::string r (cpu);

    if (!vendor.empty ())
    {
      if (!r.empty ()) r += '-';
      r += vendor;
    }

    if (!system.empty ())
    {
      if (!r.empty ()) r += '-';
      r += system;
    }

    if (!version.empty ())
    {
      r += version;
    }

    return r;
  }

  std::string target_triplet::
  representation () const
  {
    std::string r (cpu);

    {
      if (!r.empty ()) r += '-';
      r += vendor.empty () ? "unknown" : vendor.c_str ();
    }

    if (!system.empty ())
    {
      if (!r.empty ()) r += '-';
      r += system;
    }

    if (!version.empty ())
    {
      r += version;
    }

    return r;
  }
}