Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Commit

Permalink
use boost::filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
ctiller committed Nov 25, 2017
1 parent ad36710 commit 76de733
Show file tree
Hide file tree
Showing 18 changed files with 149 additions and 111 deletions.
13 changes: 11 additions & 2 deletions BUILD
Expand Up @@ -133,6 +133,7 @@ cc_library(
"@com_google_absl//absl/strings",
"@com_google_absl//absl/time",
"@com_google_absl//absl/types:optional",
"@boost//:filesystem",
]
)

Expand Down Expand Up @@ -255,6 +256,7 @@ cc_library(
'@com_google_absl//absl/strings',
":log",
':project',
"@boost//:filesystem",
],
)

Expand All @@ -269,7 +271,8 @@ cc_library(
':config',
':read',
':run',
':project'
':project',
"@boost//:filesystem",
],
)

Expand Down Expand Up @@ -317,7 +320,12 @@ cc_library(
name = "run",
srcs = ["run.cc"],
hdrs = ["run.h"],
deps = [":wrap_syscall", ":log", "@com_google_absl//absl/strings"]
deps = [
":wrap_syscall",
":log",
"@com_google_absl//absl/strings",
"@boost//:filesystem",
]
)

cc_library(
Expand Down Expand Up @@ -349,6 +357,7 @@ cc_library(
'@json//:json',
"@com_google_absl//absl/synchronization",
"@com_google_absl//absl/strings",
"@boost//:filesystem",
":read",
]
)
Expand Down
9 changes: 9 additions & 0 deletions WORKSPACE
Expand Up @@ -200,3 +200,12 @@ git_repository(
commit = "30dbc81fb5ffdc98ea9b14b1918bfe4e8779b26e", # v2.2.0 + fix of include path
remote = "https://github.com/gflags/gflags.git"
)

git_repository(
name = "com_github_nelhage_boost",
commit = 'd6446dc9de6e43b039af07482a9361bdc6da5237',
remote = "https://github.com/nelhage/rules_boost",
)

load("@com_github_nelhage_boost//:boost/boost.bzl", "boost_deps")
boost_deps()
10 changes: 5 additions & 5 deletions buffer.cc
Expand Up @@ -39,7 +39,7 @@ class CollaboratorRegistry {

} // namespace

Buffer::Buffer(const std::string& filename,
Buffer::Buffer(const boost::filesystem::path& filename,
absl::optional<AnnotatedString> initial_string)
: synthetic_(initial_string),
version_(0),
Expand Down Expand Up @@ -73,7 +73,7 @@ static const std::unordered_map<std::string, Language> kExtToLanguage = {
};

Language Buffer::language() const {
auto ext = filename_.substr(filename_.rfind('.'));
auto ext = filename_.extension().string();
auto it = kExtToLanguage.find(ext);
if (it != kExtToLanguage.end()) return it->second;
return Language::Unknown;
Expand Down Expand Up @@ -289,9 +289,9 @@ std::vector<std::string> Buffer::ProfileData() const {
absl::Time timestamp) {
auto age = now - timestamp;
if (age > absl::Seconds(5)) return;
out.emplace_back(absl::StrCat(filename(), ":", c->name(), ":", name, ": ",
absl::FormatTime(timestamp), " (",
absl::FormatDuration(age), " ago)"));
out.emplace_back(absl::StrCat(filename().string(), ":", c->name(), ":",
name, ": ", absl::FormatTime(timestamp),
" (", absl::FormatDuration(age), " ago)"));
};
report("chg", c->last_change());
report("rsp", c->last_response());
Expand Down
7 changes: 4 additions & 3 deletions buffer.h
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.
#pragma once

#include <boost/filesystem.hpp>
#include <thread>
#include "absl/synchronization/mutex.h"
#include "absl/time/clock.h"
Expand Down Expand Up @@ -111,7 +112,7 @@ enum class Language {

class Buffer {
public:
Buffer(const std::string& filename,
Buffer(const boost::filesystem::path& filename,
absl::optional<AnnotatedString> initial_string =
absl::optional<AnnotatedString>());
~Buffer();
Expand All @@ -126,7 +127,7 @@ class Buffer {
return p;
}

const std::string& filename() const { return filename_; }
const boost::filesystem::path& filename() const { return filename_; }
Language language() const;
bool read_only() const { return false; }
bool synthetic() const { return synthetic_; }
Expand Down Expand Up @@ -161,7 +162,7 @@ class Buffer {
std::set<Collaborator*> done_collaborators_ GUARDED_BY(mu_);
bool updating_ GUARDED_BY(mu_);
absl::Time last_used_ GUARDED_BY(mu_);
const std::string filename_;
const boost::filesystem::path filename_;
EditNotification state_ GUARDED_BY(mu_);
std::vector<CollaboratorPtr> collaborators_ GUARDED_BY(mu_);
std::vector<std::thread> collaborator_threads_ GUARDED_BY(mu_);
Expand Down
72 changes: 36 additions & 36 deletions clang_config.cc
Expand Up @@ -13,6 +13,7 @@
// 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"
Expand All @@ -34,66 +35,65 @@
#endif

static Config<std::string> clang_version("project.clang-version");
static ConfigMap<std::string, std::string> clang_location("clang.location");
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<absl::string_view> Path() {
static std::vector<boost::filesystem::path> Path() {
const char* path = getenv("PATH");
return absl::StrSplit(path, ':');
}

static bool Exists(const std::string& path) {
FILE* f = fopen(path.c_str(), "r");
if (!f) return false;
fclose(f);
return true;
std::vector<boost::filesystem::path> out;
for (auto p : absl::StrSplit(path, ':')) {
out.emplace_back(std::string(p.data(), p.length()));
}
return out;
}

std::string ClangToolPath(const std::string& tool_name) {
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 = absl::StrCat(path, "/bin/", tool_name);
if (Exists(cmd)) return cmd;
auto cmd = path / "bin" / tool_name;
if (boost::filesystem::exists(cmd)) return cmd;
}
for (auto path : Path()) {
auto cmd = absl::StrCat(path, "/", tool_name, "-", version);
if (Exists(cmd)) return cmd;
auto cmd = path / absl::StrCat(tool_name, "-", version);
if (boost::filesystem::exists(cmd)) return cmd;
}
}
for (auto path : Path()) {
auto cmd = absl::StrCat(path, "/", tool_name);
if (Exists(cmd)) return cmd;
auto cmd = path / tool_name;
if (boost::filesystem::exists(cmd)) return cmd;
}
throw std::runtime_error(
absl::StrCat("Clang tool '", tool_name, "' not found"));
}

std::string ClangLibPath(const std::string& lib_base) {
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 = absl::StrCat(path, "/lib/", lib_name);
if (Exists(lib)) return lib;
auto lib = path / "lib" / lib_name;
if (boost::filesystem::exists(lib)) return lib;
}
for (auto path : {"/lib", "/usr/lib", "/usr/local/lib"}) {
auto lib = absl::StrCat(path, "/", lib_name, ".", version);
if (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 (auto path : {"/lib", "/usr/lib", "/usr/local/lib"}) {
auto lib = absl::StrCat(path, "/", lib_name);
if (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 std::string& toolname,
static void ExtractIncludePathsFromTool(const boost::filesystem::path& toolname,
std::vector<std::string>* args) {
/*
#include <...> search starts here:
Expand Down Expand Up @@ -128,7 +128,7 @@ End of search list.
}
}

void ClangCompileArgs(const std::string& filename,
void ClangCompileArgs(const boost::filesystem::path& filename,
std::vector<std::string>* args) {
if (CompilationDatabase* db = Project::GetAspect<CompilationDatabase>()) {
if (db->ClangCompileArgs(filename, args)) {
Expand All @@ -142,8 +142,8 @@ void ClangCompileArgs(const std::string& filename,

#ifdef __APPLE__
for (auto path : Path()) {
auto cmd = absl::StrCat(path, "/clang");
if (Exists(cmd) && cmd != ClangToolPath("clang")) {
auto cmd = path / "clang";
if (boost::filesystem::exists(cmd) && cmd != ClangToolPath("clang")) {
ExtractIncludePathsFromTool(cmd, args);
}
}
Expand All @@ -169,10 +169,10 @@ void ClangCompileArgs(const std::string& filename,
}
}

std::string ClangCompileCommand(const std::string& filename,
const std::string& src_file,
const std::string& dst_file,
std::vector<std::string>* args) {
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");
Expand All @@ -181,8 +181,8 @@ std::string ClangCompileCommand(const std::string& filename,

args->push_back("-c");
args->push_back("-o");
args->push_back(dst_file);
args->push_back(src_file);
args->push_back(dst_file.string());
args->push_back(src_file.string());

return ClangToolPath("clang++");
}
15 changes: 8 additions & 7 deletions clang_config.h
Expand Up @@ -13,14 +13,15 @@
// limitations under the License.
#pragma once

#include <boost/filesystem/path.hpp>
#include <string>
#include <vector>

std::string ClangToolPath(const std::string& name);
std::string ClangLibPath(const std::string& name);
std::string ClangCompileCommand(const std::string& filename,
const std::string& src_file,
const std::string& dst_file,
std::vector<std::string>* args);
void ClangCompileArgs(const std::string& filename,
boost::filesystem::path ClangToolPath(const std::string& name);
boost::filesystem::path ClangLibPath(const std::string& name);
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);
void ClangCompileArgs(const boost::filesystem::path& filename,
std::vector<std::string>* args);
9 changes: 5 additions & 4 deletions clang_format_collaborator.cc
Expand Up @@ -39,10 +39,11 @@ EditResponse ClangFormatCollaborator::Edit(
auto text = str.Render();
auto clang_format = ClangToolPath("clang-format");
Log() << "clang-format command: " << clang_format;
auto res = run(clang_format,
{"-output-replacements-xml",
absl::StrCat("-assume-filename=", buffer_->filename())},
text);
auto res =
run(clang_format,
{"-output-replacements-xml",
absl::StrCat("-assume-filename=", buffer_->filename().string())},
text);
Log() << res.out;

pugi::xml_document doc;
Expand Down
30 changes: 13 additions & 17 deletions compilation_database.cc
Expand Up @@ -12,19 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "compilation_database.h"
#include <boost/filesystem.hpp>
#include "absl/strings/str_cat.h"
#include "absl/strings/str_split.h"
#include "absl/synchronization/mutex.h"
#include "read.h"
#include "src/json.hpp"

static bool Exists(const std::string& path) {
FILE* f = fopen(path.c_str(), "r");
if (!f) return false;
fclose(f);
return true;
}

struct CompilationDatabase::Impl {
absl::Mutex mu;
nlohmann::json js GUARDED_BY(mu);
Expand All @@ -36,10 +30,10 @@ CompilationDatabase::CompilationDatabase() : impl_(new Impl) {}

CompilationDatabase::~CompilationDatabase() {}

bool CompilationDatabase::ClangCompileArgs(const std::string& filename,
std::vector<std::string>* args) {
bool CompilationDatabase::ClangCompileArgs(
const boost::filesystem::path& filename, std::vector<std::string>* args) {
absl::MutexLock lock(&impl_->mu);
auto it = impl_->cache.find(filename);
auto it = impl_->cache.find(boost::filesystem::absolute(filename).string());
if (it != impl_->cache.end()) {
if (!it->second) return false;
*args = *it->second;
Expand All @@ -50,7 +44,8 @@ bool CompilationDatabase::ClangCompileArgs(const std::string& filename,
impl_->js = nlohmann::json::parse(Read(CompileCommandsFile()));
}
for (const auto& child : impl_->js) {
if (child["file"] == filename) {
std::string file = child["file"];
if (boost::filesystem::equivalent(file, filename)) {
args->clear();
std::string command = child["command"];
std::string directory = child["directory"];
Expand All @@ -66,20 +61,21 @@ bool CompilationDatabase::ClangCompileArgs(const std::string& filename,
}
std::string arg(arg_view.data(), arg_view.length());
if (arg.length() && arg[0] != '/' && arg[0] != '-') {
auto as_if_under_dir = absl::StrCat(directory, "/", arg);
if (Exists(as_if_under_dir)) {
arg = as_if_under_dir;
auto as_if_under_dir = boost::filesystem::path(directory) / arg;
if (exists(as_if_under_dir)) {
arg = as_if_under_dir.string();
}
}
args->emplace_back(std::move(arg));
}
impl_->cache.emplace(
std::make_pair(filename, std::unique_ptr<std::vector<std::string>>(
new std::vector<std::string>(*args))));
std::make_pair(boost::filesystem::absolute(filename).string(),
std::unique_ptr<std::vector<std::string>>(
new std::vector<std::string>(*args))));
return true;
}
}
// cache negative result
impl_->cache[filename];
impl_->cache[boost::filesystem::absolute(filename).string()];
return false;
}

0 comments on commit 76de733

Please sign in to comment.