LibIDL: Support multiple import base paths for resolving imports

This allows us to specify multiple base paths to look for imported IDL
files in. This will allow us to import IDL files from sources and from
the Build directory (i.e. for generated IDL files).
This commit is contained in:
Luke Wilde 2024-11-14 16:13:44 +00:00 committed by Andreas Kling
commit 300f212044
Notes: github-actions[bot] 2024-11-14 18:51:33 +00:00
4 changed files with 49 additions and 21 deletions

View file

@ -160,9 +160,22 @@ HashMap<ByteString, ByteString> Parser::parse_extended_attributes()
static HashTable<ByteString> import_stack;
Optional<Interface&> Parser::resolve_import(auto path)
{
auto include_path = LexicalPath::join(import_base_path, path).string();
if (!FileSystem::exists(include_path))
report_parsing_error(ByteString::formatted("{}: No such file or directory", include_path), filename, input, lexer.tell());
ByteString include_path;
for (auto import_base_path : import_base_paths) {
auto maybe_include_path = LexicalPath::join(import_base_path, path).string();
if (!FileSystem::exists(maybe_include_path))
continue;
include_path = maybe_include_path;
break;
}
if (include_path.is_empty()) {
StringBuilder error_message;
error_message.appendff("Failed to find {} in the following directories:\n", path);
error_message.join('\n', import_base_paths);
report_parsing_error(error_message.to_byte_string(), filename, input, lexer.tell());
}
auto real_path_error_or = FileSystem::real_path(include_path);
if (real_path_error_or.is_error())
@ -183,7 +196,7 @@ Optional<Interface&> Parser::resolve_import(auto path)
auto data_or_error = file_or_error.value()->read_until_eof();
if (data_or_error.is_error())
report_parsing_error(ByteString::formatted("Failed to read {}: {}", real_path, data_or_error.error()), filename, input, lexer.tell());
auto& result = Parser(this, real_path, data_or_error.value(), import_base_path).parse();
auto& result = Parser(this, real_path, data_or_error.value(), import_base_paths).parse();
import_stack.remove(real_path);
top_level_resolved_imports().set(real_path, &result);
@ -1229,16 +1242,16 @@ Interface& Parser::parse()
return interface;
}
Parser::Parser(ByteString filename, StringView contents, ByteString import_base_path)
: import_base_path(move(import_base_path))
Parser::Parser(ByteString filename, StringView contents, Vector<StringView> import_base_paths)
: import_base_paths(move(import_base_paths))
, filename(move(filename))
, input(contents)
, lexer(input)
{
}
Parser::Parser(Parser* parent, ByteString filename, StringView contents, ByteString import_base_path)
: import_base_path(move(import_base_path))
Parser::Parser(Parser* parent, ByteString filename, StringView contents, Vector<StringView> import_base_paths)
: import_base_paths(move(import_base_paths))
, filename(move(filename))
, input(contents)
, lexer(input)