Files
lifting-bits-sleigh/support/Support.cpp
T
Alex Cameron 92671daec5 libsleigh: Add helpers for finding spec files (#24)
* libsleigh: Add helpers for finding spec files

* doc: Update README

* support: Move spec file helpers to separate `support` lib

* support: Use `std::filesystem::path` over `std::string`

* support: Generate helpers with CMake configure instead of macros

* sleigh-lift: Rework optional args and include new SLA path arg

* README: Document support helper

* sleigh-lift: Fix compile warning

* sleigh-lift: Rework default args to `FindSpecFile`

* Apply suggestions from code review

Co-authored-by: Eric Kilmer <eric.d.kilmer@gmail.com>

* cmake: Fix quotes

* support: Move build/install directories into separate header

* support: Add missing copyright notice

* support: Mark spec file dirs as static

* cmake: Fix source list since we're generating a header now

* support: Fix search path

* sleigh-lift: Rename arg to `root_sla_dir`

* README: Update usage text

* sleight-lift: Rename option

* Update support/Support.cpp

Co-authored-by: Eric Kilmer <eric.d.kilmer@gmail.com>

Co-authored-by: Eric Kilmer <eric.d.kilmer@gmail.com>
2021-12-09 23:45:53 -05:00

71 lines
2.0 KiB
C++

/*
Copyright (c) 2021-present, Trail of Bits, Inc.
All rights reserved.
This source code is licensed in accordance with the terms specified in
the LICENSE file found in the root directory of this source tree.
*/
#include "SpecFilePaths.h"
#include "Support.h"
namespace sleigh {
namespace {
std::optional<std::filesystem::path>
FindSpecFileInSearchPath(std::string_view file_name,
std::filesystem::path search_path) {
search_path.append("Ghidra").append("Processors");
// Check whether a SLEIGH installation exists at this path
if (!std::filesystem::is_directory(search_path)) {
return {};
}
// Each directory under Processors/ represents a family of architectures
//
// Spec files should reside under:
// <root_sla_dir>/Ghidra/Processors/<arch>/data/languages
std::filesystem::directory_iterator install_iter(search_path);
for (auto &dir_entry : install_iter) {
if (!dir_entry.is_directory()) {
continue;
}
// Check whether the spec file exists under data/languages/
auto dir_path = dir_entry.path();
dir_path.append("data").append("languages").append(file_name);
if (!std::filesystem::exists(dir_path)) {
continue;
}
return dir_path;
}
return {};
}
} // namespace
const std::vector<std::filesystem::path> gDefaultSearchPaths = {
// Derived from the installation
kSleighSpecInstallDir,
// Derived from the build
kSleighSpecBuildDir,
// Common install locations
"/usr/local/share/sleigh/specfiles", "/usr/share/sleigh/specfiles",
"/share/sleigh/specfiles"};
std::optional<std::filesystem::path>
FindSpecFile(std::string_view file_name,
const std::vector<std::filesystem::path> &search_paths) {
// Search each path for spec files
for (const auto &path : search_paths) {
auto file_path = FindSpecFileInSearchPath(file_name, path);
if (file_path) {
return file_path;
}
}
// Cannot find the spec file
return {};
}
} // namespace sleigh