forked from google/ced
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clang_config.cc
188 lines (173 loc) · 5.9 KB
/
clang_config.cc
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
// Copyright 2017 Google LLC
//
// 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
//
// https://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 "clang_config.h"
#include <sys/param.h>
#include <boost/filesystem.hpp>
#include <stdexcept>
#include "absl/strings/str_cat.h"
#include "absl/strings/str_join.h"
#include "absl/strings/str_split.h"
#include "absl/strings/string_view.h"
#include "compilation_database.h"
#include "config.h"
#include "log.h"
#include "project.h"
#include "re2/re2.h"
#include "read.h"
#include "run.h"
#define PLAT_LIB_PFX "lib"
#if defined(__APPLE__)
#define PLAT_LIB_SFX ".dylib"
#else
#define PLAT_LIB_SFX ".so"
#endif
static Config<std::string> clang_version("project.clang-version");
static ConfigMap<std::string, boost::filesystem::path> clang_location(
"clang.location");
static Config<std::string> compile_commands_regen(
"project.compile-commands-regen");
static std::vector<boost::filesystem::path> Path() {
const char* path = getenv("PATH");
std::vector<boost::filesystem::path> out;
for (auto p : absl::StrSplit(path, ':')) {
out.emplace_back(std::string(p.data(), p.length()));
}
return out;
}
boost::filesystem::path ClangToolPath(const std::string& tool_name) {
auto version = clang_version.get();
if (!version.empty()) {
auto path = clang_location.get(version);
if (!path.empty()) {
auto cmd = path / "bin" / tool_name;
if (boost::filesystem::exists(cmd)) return cmd;
}
for (auto path : Path()) {
auto cmd = path / absl::StrCat(tool_name, "-", version);
if (boost::filesystem::exists(cmd)) return cmd;
}
}
for (auto path : Path()) {
auto cmd = path / tool_name;
if (boost::filesystem::exists(cmd)) return cmd;
}
throw std::runtime_error(
absl::StrCat("Clang tool '", tool_name, "' not found"));
}
boost::filesystem::path ClangLibPath(const std::string& lib_base) {
auto lib_name = absl::StrCat(PLAT_LIB_PFX, lib_base, PLAT_LIB_SFX);
auto version = clang_version.get();
if (!version.empty()) {
auto path = clang_location.get(version);
if (!path.empty()) {
auto lib = path / "lib" / lib_name;
if (boost::filesystem::exists(lib)) return lib;
}
for (boost::filesystem::path path :
{"/lib", "/usr/lib", "/usr/local/lib"}) {
auto lib = path / absl::StrCat(lib_name, ".", version);
if (boost::filesystem::exists(lib)) return lib;
}
}
for (boost::filesystem::path path : {"/lib", "/usr/lib", "/usr/local/lib"}) {
auto lib = path / lib_name;
if (boost::filesystem::exists(lib)) return lib;
}
throw std::runtime_error(
absl::StrCat("Clang lib '", lib_name, "' not found"));
}
static void ExtractIncludePathsFromTool(const boost::filesystem::path& toolname,
std::vector<std::string>* args) {
/*
#include <...> search starts here:
/usr/local/google/home/ctiller/clang+llvm-4.0.0-x86_64-linux-gnu-ubuntu-14.04/bin/../include/c++/v1
/usr/local/include
/usr/local/google/home/ctiller/clang+llvm-4.0.0-x86_64-linux-gnu-ubuntu-14.04/bin/../lib/clang/4.0.0/include
/usr/include/x86_64-linux-gnu
/usr/include
End of search list.
*/
RE2 r_start(R"(#include <...> search starts here:.*)");
RE2 r_ent(R"(\s+(.*))");
RE2 r_end(R"(End of search list\..*)");
bool started = false;
std::string ent;
for (auto line : absl::StrSplit(
run(toolname,
{"-std=c++11", "-stdlib=libc++", "-v", "-E", "-x", "c++", "-"},
"")
.err,
'\n')) {
re2::StringPiece spline(line.data(), line.length());
Log() << "CLANGARGLINE: " << line;
if (!started && RE2::FullMatch(spline, r_start)) {
started = true;
} else if (started && RE2::FullMatch(spline, r_ent, &ent)) {
args->push_back("-isystem");
args->push_back(ent);
} else if (started && RE2::FullMatch(spline, r_end)) {
started = false;
}
}
}
void ClangCompileArgs(const boost::filesystem::path& filename,
std::vector<std::string>* args) {
if (CompilationDatabase* db = Project::GetAspect<CompilationDatabase>()) {
if (db->ClangCompileArgs(filename, args)) {
return;
}
}
args->push_back("-x");
args->push_back("c++");
args->push_back("-std=c++11");
#ifdef __APPLE__
for (auto path : Path()) {
auto cmd = path / "clang";
if (boost::filesystem::exists(cmd) && cmd != ClangToolPath("clang")) {
ExtractIncludePathsFromTool(cmd, args);
}
}
#endif
ExtractIncludePathsFromTool(ClangToolPath("clang"), args);
char cwd[MAXPATHLEN];
getcwd(cwd, sizeof(cwd));
std::vector<absl::string_view> segments = absl::StrSplit(cwd, '/');
while (!segments.empty()) {
auto path = absl::StrJoin(segments, "/");
try {
auto clang_config = Read(absl::StrCat(path, "/.clang_complete"));
for (auto arg : absl::StrSplit(clang_config, '\n')) {
args->emplace_back(arg.data(), arg.length());
}
return;
} catch (std::exception& e) {
Log() << e.what();
}
segments.pop_back();
}
}
boost::filesystem::path ClangCompileCommand(
const boost::filesystem::path& filename,
const boost::filesystem::path& src_file,
const boost::filesystem::path& dst_file, std::vector<std::string>* args) {
ClangCompileArgs(filename, args);
args->push_back("-g");
args->push_back("-O2");
args->push_back("-DNDEBUG");
args->push_back("-c");
args->push_back("-o");
args->push_back(dst_file.string());
args->push_back(src_file.string());
return ClangToolPath("clang++");
}