From e931840e83ca8501a18a209aef5f4f7d78c7ce8e Mon Sep 17 00:00:00 2001 From: eidheim Date: Sat, 4 Nov 2017 09:16:58 +0100 Subject: [PATCH] Related to #352: more general way of finding version specific clang-format and lldb-server executables --- src/config.cc | 15 --------------- src/config.h | 1 - src/debug_lldb.cc | 22 ++++++---------------- src/filesystem.cc | 41 +++++++++++++++++++++++++++++++++++++++++ src/filesystem.h | 5 ++++- src/source.cc | 4 +++- 6 files changed, 54 insertions(+), 34 deletions(-) diff --git a/src/config.cc b/src/config.cc index ed13045..59bf54c 100644 --- a/src/config.cc +++ b/src/config.cc @@ -207,19 +207,4 @@ void Config::read(const boost::property_tree::ptree &cfg) { terminal.font=cfg.get("terminal.font"); terminal.show_progress=cfg.get("terminal.show_progress"); - - terminal.clang_format_command="clang-format"; -#ifdef __linux - if(terminal.clang_format_command=="clang-format" && - !boost::filesystem::exists("/usr/bin/clang-format") && !boost::filesystem::exists("/usr/local/bin/clang-format")) { - auto versions={"5.0", "5", "4.0", "4", "3.9", "3.8", "3.7", "3.6", "3.5"}; - for(auto &version: versions) { - auto corrected_command=std::string("/usr/bin/clang-format-")+version; - if(boost::filesystem::exists(corrected_command)) { - terminal.clang_format_command=corrected_command; - break; - } - } - } -#endif } diff --git a/src/config.h b/src/config.h index 64d2308..3164754 100644 --- a/src/config.h +++ b/src/config.h @@ -25,7 +25,6 @@ public: class Terminal { public: - std::string clang_format_command; int history_size; std::string font; bool show_progress; diff --git a/src/debug_lldb.cc b/src/debug_lldb.cc index 303c943..3c1fcca 100644 --- a/src/debug_lldb.cc +++ b/src/debug_lldb.cc @@ -17,27 +17,17 @@ void log(const char *msg, void *) { } Debug::LLDB::LLDB(): state(lldb::StateType::eStateInvalid), buffer_size(131072) { -#ifdef __APPLE__ if(!getenv("LLDB_DEBUGSERVER_PATH")) { +#ifdef __APPLE__ std::string debug_server_path("/usr/local/opt/llvm/bin/debugserver"); if(boost::filesystem::exists(debug_server_path)) setenv("LLDB_DEBUGSERVER_PATH", debug_server_path.c_str(), 0); - } -#elif __linux - if(!getenv("LLDB_DEBUGSERVER_PATH")) { - std::string debug_server_path("/usr/bin/lldb-server"); - if(!boost::filesystem::exists(debug_server_path)) { - auto versions={"5.0", "5", "4.0", "4", "3.9", "3.8", "3.7", "3.6", "3.5"}; - for(auto &version: versions) { - auto corrected_debug_server_path=debug_server_path+'-'+version; - if(boost::filesystem::exists(corrected_debug_server_path)) { - setenv("LLDB_DEBUGSERVER_PATH", corrected_debug_server_path.c_str(), 0); - break; - } - } - } - } +#else + auto debug_server_path = filesystem::get_executable("lldb-server").string(); + if(debug_server_path != "lldb-server") + setenv("LLDB_DEBUGSERVER_PATH", debug_server_path.c_str(), 0); #endif + } } std::tuple, std::string, std::vector > Debug::LLDB::parse_run_arguments(const std::string &command) { diff --git a/src/filesystem.cc b/src/filesystem.cc index b792c9c..c16c0c4 100644 --- a/src/filesystem.cc +++ b/src/filesystem.cc @@ -173,3 +173,44 @@ boost::filesystem::path filesystem::get_relative_path(const boost::filesystem::p return relative_path; } + +boost::filesystem::path filesystem::get_executable(const boost::filesystem::path &executable_name) noexcept { +#if defined(__APPLE__) || defined(_WIN32) + return executable_name; +#endif + + static std::vector bin_paths={"/usr/bin", "/usr/local/bin"}; + + try { + for(auto &path: bin_paths) { + if(boost::filesystem::exists(path/executable_name)) + return executable_name; + } + + auto executable_name_str = executable_name.string(); + for(auto &path: bin_paths) { + boost::filesystem::path executable; + for(boost::filesystem::directory_iterator it(path), end; it != end; ++it) { + auto it_path = it->path(); + auto it_path_filename_str = it_path.filename().string(); + if(!it_path_filename_str.empty() && it_path_filename_str.compare(0, executable_name_str.size(), executable_name_str)==0) { + if(it_path > executable && + ((it_path_filename_str.size() > executable_name_str.size() && + it_path_filename_str[executable_name_str.size()]>='0' && + it_path_filename_str[executable_name_str.size()]<='9') || + (it_path_filename_str.size() > executable_name_str.size()+1 && + it_path_filename_str[executable_name_str.size()]=='-' && + it_path_filename_str[executable_name_str.size()+1]>='0' && + it_path_filename_str[executable_name_str.size()+1]<='9')) && + !boost::filesystem::is_directory(it_path)) + executable=it_path; + } + } + if(!executable.empty()) + return executable; + } + } + catch(...) {} + + return executable_name; +} diff --git a/src/filesystem.h b/src/filesystem.h index 7f05b44..2b6d40a 100644 --- a/src/filesystem.h +++ b/src/filesystem.h @@ -29,7 +29,10 @@ public: /// Return path with dot, dot-dot and directory separator elements removed static boost::filesystem::path get_normal_path(const boost::filesystem::path &path) noexcept; - ///Returns empty path on failure + /// Returns empty path on failure static boost::filesystem::path get_relative_path(const boost::filesystem::path &path, const boost::filesystem::path &base) noexcept; + + /// Return executable with latest version in filename on systems that is lacking executable_name symbolic link + static boost::filesystem::path get_executable(const boost::filesystem::path &executable_name) noexcept; }; #endif // JUCI_FILESYSTEM_H_ diff --git a/src/source.cc b/src/source.cc index 0155a68..d5d4358 100644 --- a/src/source.cc +++ b/src/source.cc @@ -165,7 +165,9 @@ Source::View::View(const boost::filesystem::path &file_path, Glib::RefPtr