summaryrefslogtreecommitdiffstats
path: root/src/Detect.cxx
blob: 21bfef2156c65ac4772170288917c8a5aa32f4e3 (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
/*
  Copyright Kitware, Inc.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

#include "Detect.h"
#include "Options.h"
#include "Utils.h"

#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Triple.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Host.h"

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <string.h>

static std::string getClangBuiltinIncludeDir()
{
  return getClangResourceDir() + "/include";
}

static bool failedCC(const char* id, std::vector<const char*> const& args,
                     std::string const& out, std::string const& err,
                     std::string const& msg)
{
  std::cerr << "error: '--castxml-cc-" << id
            << "' compiler command failed:\n\n";
  for (std::vector<const char *>::const_iterator i = args.begin(),
                                                 e = args.end();
       i != e; ++i) {
    std::cerr << " '" << *i << "'";
  }
  std::cerr << "\n";
  if (!msg.empty()) {
    std::cerr << msg << "\n";
  } else {
    std::cerr << out << "\n";
    std::cerr << err << "\n";
  }
  return false;
}

static void fixPredefines(Options& opts)
{
  // Remove any detected conflicting definition of a Clang builtin macro.
  std::string& pd = opts.Predefines;
  std::string::size_type beg = 0;
  while ((beg = pd.find("#define __has", beg), beg != std::string::npos)) {
    std::string::size_type end = pd.find('\n', beg);
    if (end != std::string::npos) {
      pd.erase(beg, end + 1 - beg);
    } else {
      pd.erase(beg);
    }
  }
}

static void setTriple(Options& opts)
{
  std::string const& pd = opts.Predefines;
  llvm::Triple triple(llvm::sys::getDefaultTargetTriple());
  if (pd.find("#define __x86_64__ 1") != pd.npos ||
      pd.find("#define _M_X64 ") != pd.npos) {
    triple.setArchName("x86_64");
  } else if (pd.find("#define __amd64__ 1") != pd.npos ||
             pd.find("#define _M_AMD64 ") != pd.npos) {
    triple.setArchName("amd64");
  } else if (pd.find("#define __i386__ 1") != pd.npos ||
             pd.find("#define _M_IX86 ") != pd.npos) {
    triple.setArchName("i386");
  }
  if (pd.find("#define _WIN32 1") != pd.npos) {
    triple.setVendorName("pc");
    triple.setOSName("windows");
  }
  if (pd.find("#define __MINGW32__ 1") != pd.npos) {
    triple.setEnvironmentName("gnu");
  }
  opts.Triple = triple.getTriple();
}

static bool detectCC_GNU(const char* const* argBeg, const char* const* argEnd,
                         Options& opts, const char* id, const char* ext)
{
  std::string const fwExplicitSuffix = " (framework directory)";
  std::string const fwImplicitSuffix = "/Frameworks";
  std::vector<const char*> cc_args(argBeg, argEnd);
  std::string empty_cpp = getResourceDir() + "/empty." + ext;
  int ret;
  std::string out;
  std::string err;
  std::string msg;
  cc_args.push_back("-E");
  cc_args.push_back("-dM");
  cc_args.push_back("-v");
  cc_args.push_back(empty_cpp.c_str());
  if (runCommand(int(cc_args.size()), &cc_args[0], ret, out, err, msg) &&
      ret == 0) {
    opts.Predefines = out;
    const char* start_line = "#include <...> search starts here:";
    if (const char* c = strstr(err.c_str(), start_line)) {
      if ((c = strchr(c, '\n'), c++)) {
        while (*c++ == ' ') {
          if (const char* e = strchr(c, '\n')) {
            const char* s = c;
            c = e + 1;
            if (*(e - 1) == '\r') {
              --e;
            }
            std::string inc(s, e - s);
            std::replace(inc.begin(), inc.end(), '\\', '/');
            bool fw = ((inc.size() > fwExplicitSuffix.size()) &&
                       (inc.substr(inc.size() - fwExplicitSuffix.size()) ==
                        fwExplicitSuffix));
            if (fw) {
              inc = inc.substr(0, inc.size() - fwExplicitSuffix.size());
            } else {
              fw = ((inc.size() > fwImplicitSuffix.size()) &&
                    (inc.substr(inc.size() - fwImplicitSuffix.size()) ==
                     fwImplicitSuffix));
            }
            // Replace the compiler builtin include directory with ours.
            if (!fw && llvm::sys::fs::exists(inc + "/emmintrin.h")) {
              inc = getClangBuiltinIncludeDir();
            }
            opts.Includes.push_back(Options::Include(inc, fw));
          }
        }
      }
    }
    fixPredefines(opts);
    setTriple(opts);
    return true;
  } else {
    return failedCC(id, cc_args, out, err, msg);
  }
}

static bool detectCC_MSVC(const char* const* argBeg, const char* const* argEnd,
                          Options& opts, const char* id, const char* ext)
{
  std::vector<const char*> cc_args(argBeg, argEnd);
  std::string detect_vs_cpp = getResourceDir() + "/detect_vs." + ext;
  int ret;
  std::string out;
  std::string err;
  std::string msg;
  cc_args.push_back("-c");
  cc_args.push_back("-FoNUL");
  cc_args.push_back(detect_vs_cpp.c_str());
  if (runCommand(int(cc_args.size()), &cc_args[0], ret, out, err, msg) &&
      ret == 0) {
    if (const char* predefs = strstr(out.c_str(), "\n#define")) {
      opts.Predefines = predefs + 1;
    }
    if (const char* includes_str = std::getenv("INCLUDE")) {
      llvm::SmallVector<llvm::StringRef, 8> includes;
      llvm::StringRef includes_ref(includes_str);
      includes_ref.split(includes, ";", -1, false);
      for (llvm::StringRef i : includes) {
        if (!i.empty()) {
          std::string inc = i;
          std::replace(inc.begin(), inc.end(), '\\', '/');
          opts.Includes.push_back(inc);
        }
      }
    }
    fixPredefines(opts);
    setTriple(opts);
    return true;
  } else {
    return failedCC(id, cc_args, out, err, msg);
  }
}

bool detectCC(const char* id, const char* const* argBeg,
              const char* const* argEnd, Options& opts)
{
  if (strcmp(id, "gnu") == 0) {
    return detectCC_GNU(argBeg, argEnd, opts, id, "cpp");
  } else if (strcmp(id, "gnu-c") == 0) {
    return detectCC_GNU(argBeg, argEnd, opts, id, "c");
  } else if (strcmp(id, "msvc") == 0) {
    return detectCC_MSVC(argBeg, argEnd, opts, id, "cpp");
  } else if (strcmp(id, "msvc-c") == 0) {
    return detectCC_MSVC(argBeg, argEnd, opts, id, "c");
  } else {
    std::cerr << "error: '--castxml-cc-" << id << "' not known!\n";
    return false;
  }
}