BindingsGenerator: Write generated code to disk instead of stdout

This commit adds the `-o` option for specifying an output path to
BindingsGenerator.
This commit is contained in:
Daniel Bertalan 2022-11-05 10:47:44 +01:00 committed by Andreas Kling
parent 30d3f2789e
commit 8d1ad592a1
4 changed files with 31 additions and 46 deletions

View file

@ -113,7 +113,7 @@ function (generate_js_bindings target)
list(TRANSFORM include_paths PREPEND -i)
add_custom_command(
OUTPUT "${bindings_src}"
COMMAND "$<TARGET_FILE:Lagom::BindingsGenerator>" "--${bindings_type}" ${include_paths} "${LIBWEB_INPUT_FOLDER}/${class}.idl" "${LIBWEB_INPUT_FOLDER}" > "${bindings_src}.tmp"
COMMAND "$<TARGET_FILE:Lagom::BindingsGenerator>" "--${bindings_type}" -o "${bindings_src}.tmp" ${include_paths} "${LIBWEB_INPUT_FOLDER}/${class}.idl" "${LIBWEB_INPUT_FOLDER}"
COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${bindings_src}.tmp" "${bindings_src}"
COMMAND "${CMAKE_COMMAND}" -E remove "${bindings_src}.tmp"
VERBATIM

View file

@ -3,5 +3,5 @@ set(SOURCES
main.cpp
)
lagom_tool(BindingsGenerator LIBS LibIDL)
lagom_tool(BindingsGenerator LIBS LibIDL LibMain)
target_compile_options(BindingsGenerator PUBLIC -g)

View file

@ -1980,9 +1980,8 @@ JS_DEFINE_NATIVE_FUNCTION(@class_name@::@function.name:snakecase@)
)~~~");
}
void generate_constructor_header(IDL::Interface const& interface)
void generate_constructor_header(IDL::Interface const& interface, StringBuilder& builder)
{
StringBuilder builder;
SourceGenerator generator { builder };
generator.set("name", interface.name);
@ -2032,13 +2031,10 @@ private:
} // namespace Web::Bindings
)~~~");
outln("{}", generator.as_string_view());
}
void generate_constructor_implementation(IDL::Interface const& interface)
void generate_constructor_implementation(IDL::Interface const& interface, StringBuilder& builder)
{
StringBuilder builder;
SourceGenerator generator { builder };
generator.set("name", interface.name);
@ -2236,13 +2232,10 @@ void @constructor_class@::initialize(JS::Realm& realm)
generator.append(R"~~~(
} // namespace Web::Bindings
)~~~");
outln("{}", generator.as_string_view());
}
void generate_prototype_header(IDL::Interface const& interface)
void generate_prototype_header(IDL::Interface const& interface, StringBuilder& builder)
{
StringBuilder builder;
SourceGenerator generator { builder };
generator.set("name", interface.name);
@ -2356,13 +2349,10 @@ inline DeprecatedString idl_enum_to_deprecated_string(@enum.type.name@ value) {
generator.append(R"~~~(
} // namespace Web::Bindings
)~~~");
outln("{}", generator.as_string_view());
}
void generate_prototype_implementation(IDL::Interface const& interface)
void generate_prototype_implementation(IDL::Interface const& interface, StringBuilder& builder)
{
StringBuilder builder;
SourceGenerator generator { builder };
generator.set("name", interface.name);
@ -2801,14 +2791,11 @@ JS_DEFINE_NATIVE_FUNCTION(@prototype_class@::values)
generator.append(R"~~~(
} // namespace Web::Bindings
)~~~");
outln("{}", generator.as_string_view());
}
void generate_iterator_prototype_header(IDL::Interface const& interface)
void generate_iterator_prototype_header(IDL::Interface const& interface, StringBuilder& builder)
{
VERIFY(interface.pair_iterator_types.has_value());
StringBuilder builder;
SourceGenerator generator { builder };
generator.set("prototype_class", DeprecatedString::formatted("{}IteratorPrototype", interface.name));
@ -2833,14 +2820,11 @@ private:
} // namespace Web::Bindings
)~~~");
outln("{}", generator.as_string_view());
}
void generate_iterator_prototype_implementation(IDL::Interface const& interface)
void generate_iterator_prototype_implementation(IDL::Interface const& interface, StringBuilder& builder)
{
VERIFY(interface.pair_iterator_types.has_value());
StringBuilder builder;
SourceGenerator generator { builder };
generator.set("name", DeprecatedString::formatted("{}Iterator", interface.name));
@ -2922,7 +2906,5 @@ JS_DEFINE_NATIVE_FUNCTION(@prototype_class@::next)
} // namespace Web::Bindings
)~~~");
outln("{}", generator.as_string_view());
}
}

View file

@ -12,25 +12,27 @@
#include <AK/LexicalPath.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/File.h>
#include <LibCore/Stream.h>
#include <LibIDL/IDLParser.h>
#include <LibIDL/Types.h>
extern Vector<StringView> s_header_search_paths;
namespace IDL {
void generate_constructor_header(IDL::Interface const&);
void generate_constructor_implementation(IDL::Interface const&);
void generate_prototype_header(IDL::Interface const&);
void generate_prototype_implementation(IDL::Interface const&);
void generate_iterator_prototype_header(IDL::Interface const&);
void generate_iterator_prototype_implementation(IDL::Interface const&);
void generate_constructor_header(IDL::Interface const&, StringBuilder&);
void generate_constructor_implementation(IDL::Interface const&, StringBuilder&);
void generate_prototype_header(IDL::Interface const&, StringBuilder&);
void generate_prototype_implementation(IDL::Interface const&, StringBuilder&);
void generate_iterator_prototype_header(IDL::Interface const&, StringBuilder&);
void generate_iterator_prototype_implementation(IDL::Interface const&, StringBuilder&);
}
int main(int argc, char** argv)
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
Core::ArgsParser args_parser;
StringView path;
StringView import_base_path;
StringView output_path = "-"sv;
bool constructor_header_mode = false;
bool constructor_implementation_mode = false;
bool prototype_header_mode = false;
@ -54,24 +56,23 @@ int main(int argc, char** argv)
return true;
},
});
args_parser.add_option(output_path, "Path to output generated file into", "output-path", 'o', "output-path");
args_parser.add_positional_argument(path, "IDL file", "idl-file");
args_parser.add_positional_argument(import_base_path, "Import base path", "import-base-path", Core::ArgsParser::Required::No);
args_parser.parse(argc, argv);
args_parser.parse(arguments);
auto file_or_error = Core::File::open(path, Core::OpenMode::ReadOnly);
if (file_or_error.is_error()) {
warnln("Failed to open {}: {}", path, file_or_error.error());
return 1;
}
auto file = TRY(Core::Stream::File::open(path, Core::Stream::OpenMode::Read));
LexicalPath lexical_path(path);
auto& namespace_ = lexical_path.parts_view().at(lexical_path.parts_view().size() - 2);
auto data = file_or_error.value()->read_all();
auto data = TRY(file->read_all());
if (import_base_path.is_null())
import_base_path = lexical_path.dirname();
auto output_file = TRY(Core::Stream::File::open_file_or_standard_stream(output_path, Core::Stream::OpenMode::Write));
IDL::Parser parser(path, data, import_base_path);
auto& interface = parser.parse();
@ -125,23 +126,25 @@ int main(int argc, char** argv)
}
}
StringBuilder output_builder;
if (constructor_header_mode)
IDL::generate_constructor_header(interface);
IDL::generate_constructor_header(interface, output_builder);
if (constructor_implementation_mode)
IDL::generate_constructor_implementation(interface);
IDL::generate_constructor_implementation(interface, output_builder);
if (prototype_header_mode)
IDL::generate_prototype_header(interface);
IDL::generate_prototype_header(interface, output_builder);
if (prototype_implementation_mode)
IDL::generate_prototype_implementation(interface);
IDL::generate_prototype_implementation(interface, output_builder);
if (iterator_prototype_header_mode)
IDL::generate_iterator_prototype_header(interface);
IDL::generate_iterator_prototype_header(interface, output_builder);
if (iterator_prototype_implementation_mode)
IDL::generate_iterator_prototype_implementation(interface);
IDL::generate_iterator_prototype_implementation(interface, output_builder);
TRY(output_file->write(output_builder.string_view().bytes()));
return 0;
}