Implement a search for the clang binary

This commit is contained in:
Jonathan Müller 2018-09-24 16:28:16 +02:00
commit a4c64dbc8d
2 changed files with 39 additions and 4 deletions

View file

@ -118,11 +118,10 @@ public:
/// \effects Sets the path to the location of the `clang++` binary and the version of that
/// binary.
/// If the binary is not found, it will try and search for it.
/// \returns `true` if the binary was found exactly as specified.
/// \notes It will be used for preprocessing.
void set_clang_binary(std::string binary)
{
clang_binary_ = std::move(binary);
}
bool set_clang_binary(std::string binary);
/// \effects Sets whether or not the preprocessed file will be written out.
/// Default value is `false`.

View file

@ -9,6 +9,7 @@
#include <vector>
#include <clang-c/CXCompilationDatabase.h>
#include <process.hpp>
#include "cxtokenizer.hpp"
#include "libclang_visitor.hpp"
@ -18,6 +19,7 @@
#include "raii_wrapper.hpp"
using namespace cppast;
namespace tpl = TinyProcessLib;
const std::string& detail::libclang_compile_config_access::clang_binary(
const libclang_compile_config& config)
@ -240,6 +242,40 @@ libclang_compile_config::libclang_compile_config(const libclang_compilation_data
}
}
namespace
{
bool is_valid_binary(const std::string& binary)
{
tpl::Process process(binary + " -v", "", [](const char*, std::size_t) {},
[](const char*, std::size_t) {});
return process.get_exit_status() == 0;
}
} // namespace
bool libclang_compile_config::set_clang_binary(std::string binary)
{
if (is_valid_binary(binary))
{
clang_binary_ = binary;
return true;
}
else
{
// first search in current directory, then in PATH
static const char* paths[]
= {"./clang++", "clang++", "./clang++-4.0", "clang++-4.0", "./clang++-5.0",
"clang++-5.0", "./clang++-6.0", "clang++-6.0", "./clang-7", "clang-7"};
for (auto& p : paths)
if (is_valid_binary(p))
{
clang_binary_ = p;
return false;
}
throw std::invalid_argument("unable to find clang binary '" + binary + "'");
}
}
void libclang_compile_config::do_set_flags(cpp_standard standard, compile_flags flags)
{
switch (standard)