Commit 94b5e96d authored by Pádraig Ó Conbhuí's avatar Pádraig Ó Conbhuí
Browse files

Add first steps of megadep executable.

parent 13970fdb
#include <megadep/filesystem.hpp>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <string_view>
#include <vector>
// List of file suffixes the program will consider
std::vector<std::string> suffixes = {
".c", ".cc", ".cpp", ".cxx", ".h", ".hh", ".hpp", ".hxx",
};
// Test if a given file name has a suffix in suffixes.
bool contains_suffix(std::string_view file_name) {
for (const auto &suffix : suffixes) {
if (file_name.compare(file_name.size() - suffix.size(), suffix.size(),
suffix) == 0) {
return true;
}
}
return false;
}
int main(int argc, char *argv[]) {
std::vector<std::string> project_directories;
std::vector<std::string> include_directories;
for (int i = 1; i < argc; i++) {
std::string_view argv_str = argv[i];
if (argv_str.compare(0, 2, "-I") == 0) {
// -I for include directory
argv_str.remove_prefix(2);
include_directories.emplace_back(argv_str);
continue;
} else {
// Regular positional argument for project directory
project_directories.emplace_back(argv_str);
continue;
}
}
std::cout << "Scanning:\n";
for (const auto &directory : project_directories) {
std::cout << " - " << directory << '\n';
}
std::cout << '\n';
std::cout << "Including:\n";
for (const auto &directory : include_directories) {
std::cout << " - " << directory << '\n';
}
std::cout << '\n';
std::vector<std::string> file_names;
for (const auto &directory : project_directories) {
auto directory_file_names =
megadep::list_files(std::string{directory.data(), directory.size()});
for (const auto &file_name : directory_file_names) {
if (contains_suffix(file_name)) {
file_names.emplace_back(file_name);
}
}
}
// Sort the files so we can refer to them by index, and look them up
// quickly by index.
std::sort(file_names.begin(), file_names.end());
// Map from a file index to the file indices it #includes.
std::vector<std::vector<uint64_t>> file_to_includes(file_names.size());
for (size_t i = 0; i < file_names.size(); i++) {
const auto &file_name = file_names[i];
const auto include_strings = megadep::read_includes(file_name);
std::cout << '\n';
std::cout << "file: " << file_name << '\n';
for (const auto &include : include_strings) {
auto resolved_include =
megadep::resolve_include(include, file_name, include_directories);
// If the resolved include is in the form <filename>, we assume it's
// an external depenency.
if (resolved_include.front() == '<' && resolved_include.back() == '>') {
std::cout << " - include:\n";
std::cout << " external: " << resolved_include << '\n';
continue;
}
auto file_name_ptr = std::lower_bound(file_names.begin(),
file_names.end(), resolved_include);
if (*file_name_ptr == resolved_include) {
// If we found the exact value in file_names, add it to the includes
auto file_name_index = std::distance(file_names.begin(), file_name_ptr);
std::cout << " - include:\n";
std::cout << " raw: " << include << '\n';
std::cout << " resolved: " << resolved_include << '\n';
std::cout << " index: " << file_name_index << '\n';
file_to_includes[i].emplace_back(file_name_index);
} else {
// If we haven't found the exact value in file_names, we assume we're
// not tracking this dependency.
std::cout << " - include:\n";
std::cout << " unresolved: " << include << '\n';
}
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment